Skip to content

Commit

Permalink
feat(lua-node): adapt blockchain fastforward
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenctw committed Oct 3, 2023
1 parent 6831e87 commit 3078b14
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 30 deletions.
58 changes: 34 additions & 24 deletions permissionless-arbitration/lua_node/blockchain/reader.lua
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,33 @@ function Reader:_read_logs(tournament_address, sig, topics, data_sig)
return ret
end

function Reader:_block(block)
local cmd = string.format("cast block " .. block .. " 2>&1")

local handle = io.popen(cmd)
assert(handle)

local ret
local str = handle:read()
while str do
if str:find "Error" or str:find "error" then
local err_str = handle:read "*a"
handle:close()
error(string.format("Cast block failed:\n%s", err_str))
end

ret = str:match("timestamp (%d+)")
if ret then
break
end

str = handle:read()
end
handle:close()

return ret
end

local cast_call_template = [==[
cast call --rpc-url "%s" "%s" "%s" %s 2>&1
]==]
Expand Down Expand Up @@ -250,17 +277,20 @@ function Reader:read_commitment(tournament_address, commitment_hash)
local call_ret = self:_call(tournament_address, sig, { commitment_hash:hex_string() })
assert(#call_ret == 2)

local last_block = self:_block("latest")

local allowance, last_resume = call_ret[1]:match "%((%d+),(%d+)%)"
assert(allowance)
assert(last_resume)
local clock = {
allowance = tonumber(allowance),
last_resume = tonumber(last_resume)
last_resume = tonumber(last_resume),
}

local ret = {
clock = clock,
final_state = Hash:from_digest_hex(call_ret[2])
final_state = Hash:from_digest_hex(call_ret[2]),
last_block = last_block
}

return ret
Expand Down Expand Up @@ -295,9 +325,10 @@ function Reader:match(address, match_id_hash)
end

function Reader:inner_tournament_winner(address)
local sig = "innerTournamentWinner()(bool,bytes32)"
local sig = "innerTournamentWinner()(bool,bytes32,bytes32)"
local ret = self:_call(address, sig, {})
ret[2] = Hash:from_digest_hex(ret[2])
ret[3] = Hash:from_digest_hex(ret[3])

return ret
end
Expand All @@ -318,25 +349,4 @@ function Reader:maximum_delay(address)
return ret
end

local cast_advance_template = [[
cast rpc -r "%s" evm_increaseTime %d
]]

function Reader:advance_time(seconds)
local cmd = string.format(
cast_advance_template,
self.endpoint,
seconds
)

local handle = io.popen(cmd)
assert(handle)
local ret = handle:read "*a"
handle:close()

if ret:find "Error" then
error(string.format("Advance time `%d`s failed:\n%s", seconds, ret))
end
end

return Reader
2 changes: 1 addition & 1 deletion permissionless-arbitration/lua_node/entrypoint.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ local FF_TIME = 30
local IDLE_LIMIT = 5
local INACTIVE_LIMIT = 10

local helper = require 'utils.helper'
local helper = require "utils.helper"
local blockchain_utils = require "blockchain.utils"
local time = require "utils.time"
local blockchain_constants = require "blockchain.constants"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ local Sender = require "blockchain.sender"
local HonestStrategy = require "player.honest_strategy"

local time = require "utils.time"
local helper = require 'utils.helper'
local helper = require "utils.helper"

local player_index = tonumber(arg[1])
local tournament = arg[2]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ local HonestStrategy = require "player.honest_strategy"
local Sender = require "blockchain.sender"

local time = require "utils.time"
local helper = require 'utils.helper'
local helper = require "utils.helper"

local player_index = tonumber(arg[1])
local tournament = arg[2]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
local constants = require "constants"
local helper = require 'utils.helper'
local helper = require "utils.helper"
local time = require "utils.time"

local Machine = require "computation.machine"

Expand Down Expand Up @@ -244,6 +245,8 @@ function HonestStrategy:_react_tournament(state, tournament)
if not tournament.commitments[commitment.root_hash] then
self:_join_tournament(tournament, commitment)
else
helper.log(self.sender.index, time.prettify_clock(tournament.commitments[commitment.root_hash].status))

local latest_match = tournament.commitments[commitment.root_hash].latest_match
if latest_match then
return self:_react_match(state, latest_match, commitment)
Expand Down
27 changes: 25 additions & 2 deletions permissionless-arbitration/lua_node/utils/time.lua
Original file line number Diff line number Diff line change
@@ -1,8 +1,31 @@
local clock = os.clock
local time = os.time

function sleep(number_of_seconds)
local function sleep(number_of_seconds)
local t0 = clock()
while clock() - t0 <= number_of_seconds do end
end

return {sleep = sleep}
local function prettify_clock(status)
local c = status.clock
local b = status.last_block
local s
if c.last_resume == 0 then
time_left = c.allowance
s = string.format("clock paused, %d seconds left", time_left)
else
local current = tonumber(b)
time_left = c.allowance - (current - c.last_resume)
if time_left >= 0 then
s = string.format("clock running, %d seconds left", time_left)
else
s = string.format("clock running, %d seconds overdue", -time_left)
end
end
return s
end

return {
sleep = sleep,
prettify_clock = prettify_clock
}

0 comments on commit 3078b14

Please sign in to comment.