-
Notifications
You must be signed in to change notification settings - Fork 201
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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]>
- Loading branch information
Showing
5 changed files
with
168 additions
and
163 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
function gen_bytecode() | ||
pay_contract = function(param) | ||
from, to, value, sequence, sig = string.unpack("c32 c32 I8 I8 c64", param) | ||
|
||
function get_account_key(name) | ||
account_prefix = "account_" | ||
account_key = account_prefix .. name | ||
return account_key | ||
end | ||
|
||
function get_account(name) | ||
account_key = get_account_key(name) | ||
account_data = coroutine.yield(account_key) | ||
if string.len(account_data) > 0 then | ||
account_balance, account_sequence | ||
= string.unpack("I8 I8", account_data) | ||
return account_balance, account_sequence | ||
end | ||
return 0, 0 | ||
end | ||
|
||
function pack_account(updates, name, balance, seq) | ||
updates[get_account_key(name)] = string.pack("I8 I8", balance, seq) | ||
end | ||
|
||
function update_accounts(from_acc, from_bal, from_seq, to_acc, to_bal, to_seq) | ||
ret = {} | ||
pack_account(ret, from_acc, from_bal, from_seq) | ||
if to_acc ~= nil then | ||
pack_account(ret, to_acc, to_bal, to_seq) | ||
end | ||
return ret | ||
end | ||
|
||
function sig_payload(to_acc, value, seq) | ||
return string.pack("c32 I8 I8", to_acc, value, seq) | ||
end | ||
|
||
from_balance, from_seq = get_account(from) | ||
payload = sig_payload(to, value, sequence) | ||
check_sig(from, sig, payload) | ||
if sequence < from_seq then | ||
error("sequence number too low") | ||
end | ||
|
||
if value > from_balance then | ||
error("insufficient balance") | ||
end | ||
|
||
if value > 0 then | ||
to_balance, to_seq = get_account(to) | ||
to_balance = to_balance + value | ||
from_balance = from_balance - value | ||
else | ||
to = nil | ||
end | ||
|
||
from_seq = sequence + 1 | ||
return update_accounts(from, from_balance, from_seq, to, to_balance, to_seq) | ||
end | ||
c = string.dump(pay_contract, true) | ||
tot = "" | ||
for i = 1, string.len(c) do | ||
hex = string.format("%x", string.byte(c, i)) | ||
if string.len(hex) < 2 then | ||
hex = "0" .. hex | ||
end | ||
tot = tot .. hex | ||
end | ||
|
||
return tot | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
function gen_bytecode() | ||
pay_contract = function(param) | ||
from, to, value, sequence, sig = string.unpack("c32 c32 I8 I8 c64", param) | ||
|
||
function get_account_key(name) | ||
account_prefix = "account_" | ||
account_key = account_prefix .. name | ||
return account_key | ||
end | ||
|
||
function get_account(name) | ||
account_key = get_account_key(name) | ||
account_data = coroutine.yield(account_key) | ||
if string.len(account_data) > 0 then | ||
account_balance, account_sequence | ||
= string.unpack("I8 I8", account_data) | ||
return account_balance, account_sequence | ||
end | ||
return 0, 0 | ||
end | ||
|
||
function pack_account(updates, name, balance, seq) | ||
updates[get_account_key(name)] = string.pack("I8 I8", balance, seq) | ||
end | ||
|
||
function update_accounts(from_acc, from_bal, from_seq, to_acc, to_bal, to_seq) | ||
ret = {} | ||
pack_account(ret, from_acc, from_bal, from_seq) | ||
if to_acc ~= nil then | ||
pack_account(ret, to_acc, to_bal, to_seq) | ||
end | ||
return ret | ||
end | ||
|
||
function sig_payload(to_acc, value, seq) | ||
return string.pack("c32 I8 I8", to_acc, value, seq) | ||
end | ||
|
||
from_balance, from_seq = get_account(from) | ||
payload = sig_payload(to, value, sequence) | ||
check_sig(from, sig, payload) | ||
if sequence < from_seq then | ||
error("sequence number too low") | ||
end | ||
|
||
if value > from_balance then | ||
error("insufficient balance") | ||
end | ||
|
||
if value > 0 then | ||
to_balance, to_seq = get_account(to) | ||
to_balance = to_balance + value | ||
from_balance = from_balance - value | ||
else | ||
to = nil | ||
end | ||
|
||
from_seq = sequence + 1 | ||
return update_accounts(from, from_balance, from_seq, to, to_balance, to_seq) | ||
end | ||
c = string.dump(pay_contract, true) | ||
tot = "" | ||
for i = 1, string.len(c) do | ||
hex = string.format("%x", string.byte(c, i)) | ||
if string.len(hex) < 2 then | ||
hex = "0" .. hex | ||
end | ||
tot = tot .. hex | ||
end | ||
|
||
return tot | ||
end |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters