From 57e390a2d21c8de593d47c2dc70a3eac6854a6c6 Mon Sep 17 00:00:00 2001 From: eolesinski Date: Mon, 15 Jul 2024 14:12:17 -0400 Subject: [PATCH] lua code improvements Signed-off-by: eolesinski --- scripts/gen_bytecode.lua | 22 ++++++------------- tests/integration/correct_state_update.lua | 12 ++++------ tests/integration/data_hazard_contract.lua | 12 ++++------ .../agent/runners/lua/gen_pay_contract.lua | 22 ++++++------------- 4 files changed, 22 insertions(+), 46 deletions(-) diff --git a/scripts/gen_bytecode.lua b/scripts/gen_bytecode.lua index e135f5349..69709750f 100644 --- a/scripts/gen_bytecode.lua +++ b/scripts/gen_bytecode.lua @@ -3,18 +3,14 @@ function gen_bytecode() 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 + return "account_" .. name 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 + if #account_data > 0 then + return string.unpack("I8 I8", account_data) end return 0, 0 end @@ -59,14 +55,10 @@ function gen_bytecode() 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 + t = {} + for i = 1, #c do + t[#t + 1] = string.format("%02x", string.byte(c, i)) end - return tot + return table.concat(t) end diff --git a/tests/integration/correct_state_update.lua b/tests/integration/correct_state_update.lua index 177662956..8495bc508 100644 --- a/tests/integration/correct_state_update.lua +++ b/tests/integration/correct_state_update.lua @@ -19,14 +19,10 @@ function gen_bytecode() return updates 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 + t = {} + for i = 1, #c do + t[#t + 1] = string.format("%02x", string.byte(c, i)) end - return tot + return table.concat(t) end diff --git a/tests/integration/data_hazard_contract.lua b/tests/integration/data_hazard_contract.lua index db2a759ae..787524935 100644 --- a/tests/integration/data_hazard_contract.lua +++ b/tests/integration/data_hazard_contract.lua @@ -18,14 +18,10 @@ function gen_bytecode() return updates 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 + t = {} + for i = 1, #c do + t[#t + 1] = string.format("%02x", string.byte(c, i)) end - return tot + return table.concat(t) end diff --git a/tests/unit/parsec/agent/runners/lua/gen_pay_contract.lua b/tests/unit/parsec/agent/runners/lua/gen_pay_contract.lua index e135f5349..69709750f 100644 --- a/tests/unit/parsec/agent/runners/lua/gen_pay_contract.lua +++ b/tests/unit/parsec/agent/runners/lua/gen_pay_contract.lua @@ -3,18 +3,14 @@ function gen_bytecode() 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 + return "account_" .. name 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 + if #account_data > 0 then + return string.unpack("I8 I8", account_data) end return 0, 0 end @@ -59,14 +55,10 @@ function gen_bytecode() 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 + t = {} + for i = 1, #c do + t[#t + 1] = string.format("%02x", string.byte(c, i)) end - return tot + return table.concat(t) end