Skip to content

Commit 708c78c

Browse files
maurermiAlexRamRam
authored andcommitted
Generate lua_bench bytecode at runtime
Removes the large bytecode string for the Lua pay contract and generates bytecode at runtime. Now expects that the .lua file containing the contract code is provided as a config input in lua_bench. Makes similar change in account_test.cpp to generate bytecode at runtime. Moves location of and slightly modifies gen_bytecode.lua to better integrate within c++ source code. Signed-off-by: Michael Maurer <[email protected]>
1 parent af97a9c commit 708c78c

File tree

5 files changed

+168
-163
lines changed

5 files changed

+168
-163
lines changed

scripts/gen_bytecode.lua

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
function gen_bytecode()
2+
pay_contract = function(param)
3+
from, to, value, sequence, sig = string.unpack("c32 c32 I8 I8 c64", param)
4+
5+
function get_account_key(name)
6+
account_prefix = "account_"
7+
account_key = account_prefix .. name
8+
return account_key
9+
end
10+
11+
function get_account(name)
12+
account_key = get_account_key(name)
13+
account_data = coroutine.yield(account_key)
14+
if string.len(account_data) > 0 then
15+
account_balance, account_sequence
16+
= string.unpack("I8 I8", account_data)
17+
return account_balance, account_sequence
18+
end
19+
return 0, 0
20+
end
21+
22+
function pack_account(updates, name, balance, seq)
23+
updates[get_account_key(name)] = string.pack("I8 I8", balance, seq)
24+
end
25+
26+
function update_accounts(from_acc, from_bal, from_seq, to_acc, to_bal, to_seq)
27+
ret = {}
28+
pack_account(ret, from_acc, from_bal, from_seq)
29+
if to_acc ~= nil then
30+
pack_account(ret, to_acc, to_bal, to_seq)
31+
end
32+
return ret
33+
end
34+
35+
function sig_payload(to_acc, value, seq)
36+
return string.pack("c32 I8 I8", to_acc, value, seq)
37+
end
38+
39+
from_balance, from_seq = get_account(from)
40+
payload = sig_payload(to, value, sequence)
41+
check_sig(from, sig, payload)
42+
if sequence < from_seq then
43+
error("sequence number too low")
44+
end
45+
46+
if value > from_balance then
47+
error("insufficient balance")
48+
end
49+
50+
if value > 0 then
51+
to_balance, to_seq = get_account(to)
52+
to_balance = to_balance + value
53+
from_balance = from_balance - value
54+
else
55+
to = nil
56+
end
57+
58+
from_seq = sequence + 1
59+
return update_accounts(from, from_balance, from_seq, to, to_balance, to_seq)
60+
end
61+
c = string.dump(pay_contract, true)
62+
tot = ""
63+
for i = 1, string.len(c) do
64+
hex = string.format("%x", string.byte(c, i))
65+
if string.len(hex) < 2 then
66+
hex = "0" .. hex
67+
end
68+
tot = tot .. hex
69+
end
70+
71+
return tot
72+
end

tests/unit/3pc/agent/runners/lua/account_test.cpp

+11-47
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "util/serialization/format.hpp"
1717

1818
#include <gtest/gtest.h>
19+
#include <lua.hpp>
1920
#include <secp256k1.h>
2021
#include <secp256k1_schnorrsig.h>
2122
#include <thread>
@@ -24,53 +25,16 @@ class account_test : public ::testing::Test {
2425
protected:
2526
void SetUp() override {
2627
m_pay_contract_key.append("pay", 3);
27-
m_pay_contract
28-
= cbdc::buffer::from_hex(
29-
"1b4c7561540019930d0a1a0a04080878560000000000000000000000287"
30-
"74001808ac4010008d48b0000058e0001060381030080010000c4000306"
31-
"0f0004050f0003040f0002030f0001020f000001cf0000000f000801cf8"
32-
"000000f000901cf0001000f000a01cf8001000f000b01cf0002000f000c"
33-
"018b0000090b010000c40002030f000e020f000d018b00000c0b0100018"
34-
"b0100020b020003c40004020f000f018b0000100b0100008b0100040b02"
35-
"000fc40004018b0000030b01000eba000200380100808b0000110301090"
36-
"0c40002018b0000020b01000d3a010100380100808b00001103810900c4"
37-
"0002018b000002c0007f00b80700808b0000090b010001c40002030f001"
38-
"5020f0014018b0000140b010002a2000102ae0002060f0014018b00000d"
39-
"0b010002a3000102ae0002070f000d01380000800f8001168b000003950"
40-
"00180af0080060f000e018b00000b0b0100008b01000d0b02000e8b0200"
41-
"010b0300148b030015c5000700c6000000c700010097048566726f6d048"
42-
"3746f048676616c7565048973657175656e636504847369670487737472"
43-
"696e670487756e7061636b0492633332206333322049382049382063363"
44-
"404906765745f6163636f756e745f6b6579048c6765745f6163636f756e"
45-
"74048d7061636b5f6163636f756e7404907570646174655f6163636f756"
46-
"e7473048c7369675f7061796c6f6164048d66726f6d5f62616c616e6365"
47-
"048966726f6d5f73657104887061796c6f6164048a636865636b5f73696"
48-
"704866572726f72049873657175656e6365206e756d62657220746f6f20"
49-
"6c6f770495696e73756666696369656e742062616c616e6365048b746f5"
50-
"f62616c616e63650487746f5f736571008100000085808d91010003880f"
51-
"8000018b00000000010000b50002000f0002018b000002c8000200c7000"
52-
"10083048f6163636f756e745f70726566697804896163636f756e745f04"
53-
"8c6163636f756e745f6b657981000000808080808080939c0100049d8b0"
54-
"0000100010000c40002020f0000018b0000038e0001040b010000c40002"
55-
"020f0002018b0000058e0001060b010002c4000202c0007f00b80400808"
56-
"b0000058e000109030105008b010002c40003030f0008020f0007018b00"
57-
"00070b010008c60003008180ff7f0181ff7fc6000300c70001008b048c6"
58-
"163636f756e745f6b657904906765745f6163636f756e745f6b6579048d"
59-
"6163636f756e745f64617461048a636f726f7574696e6504867969656c6"
60-
"40487737472696e6704846c656e04906163636f756e745f62616c616e63"
61-
"6504916163636f756e745f73657175656e63650487756e7061636b04864"
62-
"938204938810000008080808080809ea00400098b0b0200008002010044"
63-
"0202028b0200018e020502038301008003020000040300c402040210000"
64-
"405470201008404906765745f6163636f756e745f6b6579048773747269"
65-
"6e6704857061636b0486493820493881000000808080808080a2a906000"
66-
"b9413030000520000000f0000060b0300018b0300000004000080040100"
67-
"0005020044030501bc810200b80200800b0300018b03000000040300800"
68-
"4040000050500440305010b030000480302004703010083048472657404"
69-
"8d7061636b5f6163636f756e740081000000808080808080abad0300088"
70-
"98b0100008e01030103020100800200000003010080030200c5010500c6"
71-
"010000c7010100830487737472696e6704857061636b048a63333220493"
72-
"820493881000000808080808080808080")
73-
.value();
28+
29+
lua_State* L = luaL_newstate();
30+
luaL_openlibs(L);
31+
luaL_dofile(
32+
L,
33+
"../tests/unit/3pc/agent/runners/lua/gen_pay_contract.lua");
34+
lua_getglobal(L, "gen_bytecode");
35+
ASSERT_EQ(lua_pcall(L, 0, 1, 0), 0);
36+
m_pay_contract = cbdc::buffer::from_hex(lua_tostring(L, -1)).value();
37+
7438
cbdc::test::add_to_shard(m_broker, m_pay_contract_key, m_pay_contract);
7539

7640
m_init_account_key.append("account_", 8);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
function gen_bytecode()
2+
pay_contract = function(param)
3+
from, to, value, sequence, sig = string.unpack("c32 c32 I8 I8 c64", param)
4+
5+
function get_account_key(name)
6+
account_prefix = "account_"
7+
account_key = account_prefix .. name
8+
return account_key
9+
end
10+
11+
function get_account(name)
12+
account_key = get_account_key(name)
13+
account_data = coroutine.yield(account_key)
14+
if string.len(account_data) > 0 then
15+
account_balance, account_sequence
16+
= string.unpack("I8 I8", account_data)
17+
return account_balance, account_sequence
18+
end
19+
return 0, 0
20+
end
21+
22+
function pack_account(updates, name, balance, seq)
23+
updates[get_account_key(name)] = string.pack("I8 I8", balance, seq)
24+
end
25+
26+
function update_accounts(from_acc, from_bal, from_seq, to_acc, to_bal, to_seq)
27+
ret = {}
28+
pack_account(ret, from_acc, from_bal, from_seq)
29+
if to_acc ~= nil then
30+
pack_account(ret, to_acc, to_bal, to_seq)
31+
end
32+
return ret
33+
end
34+
35+
function sig_payload(to_acc, value, seq)
36+
return string.pack("c32 I8 I8", to_acc, value, seq)
37+
end
38+
39+
from_balance, from_seq = get_account(from)
40+
payload = sig_payload(to, value, sequence)
41+
check_sig(from, sig, payload)
42+
if sequence < from_seq then
43+
error("sequence number too low")
44+
end
45+
46+
if value > from_balance then
47+
error("insufficient balance")
48+
end
49+
50+
if value > 0 then
51+
to_balance, to_seq = get_account(to)
52+
to_balance = to_balance + value
53+
from_balance = from_balance - value
54+
else
55+
to = nil
56+
end
57+
58+
from_seq = sequence + 1
59+
return update_accounts(from, from_balance, from_seq, to, to_balance, to_seq)
60+
end
61+
c = string.dump(pay_contract, true)
62+
tot = ""
63+
for i = 1, string.len(c) do
64+
hex = string.format("%x", string.byte(c, i))
65+
if string.len(hex) < 2 then
66+
hex = "0" .. hex
67+
end
68+
tot = tot .. hex
69+
end
70+
71+
return tot
72+
end

tools/bench/3pc/lua/gen_bytecode.lua

-71
This file was deleted.

tools/bench/3pc/lua/lua_bench.cpp

+13-45
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "crypto/sha256.h"
1313
#include "wallet.hpp"
1414

15+
#include <lua.hpp>
1516
#include <random>
1617
#include <thread>
1718

@@ -26,7 +27,7 @@ auto main(int argc, char** argv) -> int {
2627
log->error("Not enough arguments");
2728
return 1;
2829
}
29-
auto cfg = cbdc::threepc::read_config(argc - 1, argv);
30+
auto cfg = cbdc::threepc::read_config(argc - 2, argv);
3031
if(!cfg.has_value()) {
3132
log->error("Error parsing options");
3233
return 1;
@@ -74,51 +75,18 @@ auto main(int argc, char** argv) -> int {
7475
directory,
7576
log);
7677

78+
auto contract_file = args[args.size() - 2];
7779
auto pay_contract = cbdc::buffer();
78-
pay_contract
79-
= cbdc::buffer::from_hex(
80-
"1b4c7561540019930d0a1a0a040808785600000000000000000000002877400"
81-
"1808ac4010008d48b0000058e0001060381030080010000c40003060f000405"
82-
"0f0003040f0002030f0001020f000001cf0000000f000801cf8000000f00090"
83-
"1cf0001000f000a01cf8001000f000b01cf0002000f000c018b0000090b0100"
84-
"00c40002030f000e020f000d018b00000c0b0100018b0100020b020003c4000"
85-
"4020f000f018b0000100b0100008b0100040b02000fc40004018b0000030b01"
86-
"000eba000200380100808b00001103010900c40002018b0000020b01000d3a0"
87-
"10100380100808b00001103810900c40002018b000002c0007f00b80700808b"
88-
"0000090b010001c40002030f0015020f0014018b0000140b010002a2000102a"
89-
"e0002060f0014018b00000d0b010002a3000102ae0002070f000d0138000080"
90-
"0f8001168b00000395000180af0080060f000e018b00000b0b0100008b01000"
91-
"d0b02000e8b0200010b0300148b030015c5000700c6000000c7000100970485"
92-
"66726f6d0483746f048676616c7565048973657175656e63650484736967048"
93-
"7737472696e670487756e7061636b0492633332206333322049382049382063"
94-
"363404906765745f6163636f756e745f6b6579048c6765745f6163636f756e7"
95-
"4048d7061636b5f6163636f756e7404907570646174655f6163636f756e7473"
96-
"048c7369675f7061796c6f6164048d66726f6d5f62616c616e6365048966726"
97-
"f6d5f73657104887061796c6f6164048a636865636b5f73696704866572726f"
98-
"72049873657175656e6365206e756d62657220746f6f206c6f770495696e737"
99-
"56666696369656e742062616c616e6365048b746f5f62616c616e6365048774"
100-
"6f5f736571008100000085808d91010003880f8000018b00000000010000b50"
101-
"002000f0002018b000002c8000200c700010083048f6163636f756e745f7072"
102-
"6566697804896163636f756e745f048c6163636f756e745f6b6579810000008"
103-
"08080808080939c0100049d8b00000100010000c40002020f0000018b000003"
104-
"8e0001040b010000c40002020f0002018b0000058e0001060b010002c400020"
105-
"2c0007f00b80400808b0000058e000109030105008b010002c40003030f0008"
106-
"020f0007018b0000070b010008c60003008180ff7f0181ff7fc6000300c7000"
107-
"1008b048c6163636f756e745f6b657904906765745f6163636f756e745f6b65"
108-
"79048d6163636f756e745f64617461048a636f726f7574696e6504867969656"
109-
"c640487737472696e6704846c656e04906163636f756e745f62616c616e6365"
110-
"04916163636f756e745f73657175656e63650487756e7061636b04864938204"
111-
"938810000008080808080809ea00400098b0b02000080020100440202028b02"
112-
"00018e020502038301008003020000040300c40204021000040547020100840"
113-
"4906765745f6163636f756e745f6b65790487737472696e6704857061636b04"
114-
"86493820493881000000808080808080a2a906000b9413030000520000000f0"
115-
"000060b0300018b03000000040000800401000005020044030501bc810200b8"
116-
"0200800b0300018b030000000403008004040000050500440305010b0300004"
117-
"803020047030100830484726574048d7061636b5f6163636f756e7400810000"
118-
"00808080808080abad030008898b0100008e010301030201008002000000030"
119-
"10080030200c5010500c6010000c7010100830487737472696e670485706163"
120-
"6b048a63333220493820493881000000808080808080808080")
121-
.value();
80+
lua_State* L = luaL_newstate();
81+
luaL_openlibs(L);
82+
luaL_dofile(L, contract_file.c_str());
83+
lua_getglobal(L, "gen_bytecode");
84+
if(lua_pcall(L, 0, 1, 0) != 0) {
85+
log->error("Contract bytecode generation failed, with error:",
86+
lua_tostring(L, -1));
87+
return 1;
88+
}
89+
pay_contract = cbdc::buffer::from_hex(lua_tostring(L, -1)).value();
12290

12391
auto pay_keys = std::vector<cbdc::buffer>();
12492
auto init_count = std::atomic<size_t>();

0 commit comments

Comments
 (0)