Cleanup multisig lua

This commit is contained in:
Greg Fitzgerald 2018-10-09 20:29:05 -06:00
parent 45b8ba9ede
commit 2ba2bc72ca
1 changed files with 20 additions and 12 deletions

View File

@ -4,8 +4,6 @@
-- are subtracted from account 1 and given to account 4. Note that unlike -- are subtracted from account 1 and given to account 4. Note that unlike
-- Rust, Lua is one-based and that account 1 is the first account. -- Rust, Lua is one-based and that account 1 is the first account.
local serialize = load(accounts[2].userdata)().serialize
function find(t, x) function find(t, x)
for i, v in pairs(t) do for i, v in pairs(t) do
if v == x then if v == x then
@ -14,14 +12,25 @@ function find(t, x)
end end
end end
if #accounts[3].userdata == 0 then function deserialize(bytes)
local cfg = load("return" .. data)() return load("return" .. bytes)()
accounts[3].userdata = serialize(cfg, nil, "s") end
local from_account,
serialize_account,
state_account,
to_account = table.unpack(accounts)
local serialize = load(serialize_account.userdata)().serialize
if #state_account.userdata == 0 then
local cfg = deserialize(data)
state_account.userdata = serialize(cfg, nil, "s")
return return
end end
local cfg = load("return" .. accounts[3].userdata)() local cfg = deserialize(state_account.userdata)
local key = load("return" .. data)() local key = deserialize(data)
local i = find(cfg.n, key) local i = find(cfg.n, key)
if i == nil then if i == nil then
@ -30,13 +39,12 @@ end
table.remove(cfg.n, i) table.remove(cfg.n, i)
cfg.m = cfg.m - 1 cfg.m = cfg.m - 1
accounts[3].userdata = serialize(cfg, nil, "s") state_account.userdata = serialize(cfg, nil, "s")
if cfg.m == 0 then if cfg.m == 0 then
accounts[1].tokens = accounts[1].tokens - cfg.tokens from_account.tokens = from_account.tokens - cfg.tokens
accounts[4].tokens = accounts[4].tokens + cfg.tokens to_account.tokens = to_account.tokens + cfg.tokens
-- End of game. -- End of game.
accounts[1].tokens = 0 state_account.tokens = 0
accounts[2].tokens = 0
end end