Skip to content

Commit

Permalink
chore(prt-test): improve scoped require example
Browse files Browse the repository at this point in the history
  • Loading branch information
GCdePaula committed Sep 14, 2024
1 parent 07e1dec commit 55ccf11
Show file tree
Hide file tree
Showing 3 changed files with 142 additions and 19 deletions.
17 changes: 13 additions & 4 deletions prt/client-lua/player/player.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
local StateFetcher = require "player.state"
local HonestStrategy = require "player.strategy"
local CommitmentBuilder = require "computation.commitment"
local Sender = require "player.sender"

local function new(tournament_address, strategy, blokchain_endpoint, hook)
local function new(tournament_address, wallet, machine_path, blokchain_endpoint, hook)
local state_fetcher = StateFetcher:new(tournament_address, blokchain_endpoint)
local strategy = HonestStrategy:new(
CommitmentBuilder:new(machine_path),
machine_path,
Sender:new(wallet.pk, wallet.player_id, blokchain_endpoint)
)

local function react()
local state = state_fetcher:fetch()
Expand All @@ -15,10 +23,11 @@ local function new(tournament_address, strategy, blokchain_endpoint, hook)
end

return coroutine.create(function()
while true do
local log = react()
local log
repeat
log = react()
coroutine.yield(log)
end
until log.finished
end)
end

Expand Down
16 changes: 4 additions & 12 deletions prt/tests/compute/runners/hero_runner.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ require "setup_path"
-- Required Modules
local time = require "utils.time"
local helper = require "utils.helper"
local Sender = require "player.sender"
local CommitmentBuilder = require "computation.commitment"
local HonestStrategy = require "player.strategy"
local blockchain_consts = require "blockchain.constants"

-- Main Execution
local player_id = tonumber(arg[1])
Expand All @@ -21,19 +19,13 @@ else
hook = false
end

local Player = require "player.player"
local blockchain_consts = require "blockchain.constants"

local wallet = { pk = blockchain_consts.pks[player_id], player_id = player_id }
local honest_strategy = HonestStrategy:new(
CommitmentBuilder:new(machine_path),
machine_path,
Sender:new(wallet.pk, wallet.player_id, blockchain_consts.endpoint)
)

local Player = require "player.player"
local react = Player.new(
tournament_address,
honest_strategy,
wallet,
machine_path,
blockchain_consts.endpoint,
hook
)
Expand Down
128 changes: 125 additions & 3 deletions prt/tests/compute/utils/example.lua
Original file line number Diff line number Diff line change
@@ -1,15 +1,137 @@
assert(#package.loaded == 0)
require "setup_path"
assert(#package.loaded == 1)

local const0 = require "blockchain.constants"
-- We're currenly on scope "zero"
local env0, const0 = _ENV, require "blockchain.constants"

-- Scope/sandbox creator
local new_scoped_require = require "utils.scoped_require"

--
-- Create scope/sandbox 1
local scoped_require1 = new_scoped_require(_ENV)

-- In scope/sandbox 1, load "utils.test"
local env1, const1 = scoped_require1 "utils.test"
assert(_ENV ~= env1)

-- Check that in scope 1, both _ENV and "utils.scoped_require" are different
assert(env0 ~= env1)
assert(const0 ~= const1)


--
-- Create scope/sandbox 2
local scoped_require2 = new_scoped_require(_ENV)

-- In sandbox 2, load "utils.test"
local env2, const2 = scoped_require2 "utils.test"

-- Check that in scope 2, both _ENV and "utils.scoped_require" are different
assert(env1 ~= env2)
assert(const1 ~= const2)


--
-- Applying it to players
--

-- Shared setup
local blockchain_consts = require "blockchain.constants"
local tournament_address = "..."
local machine_path = "..."
local hook = false

-- Create honest player 0 in its own scope/sandbox
local _debug_p0 -- debug only
local player0
do
local player_id = 0
local wallet = { pk = blockchain_consts.pks[player_id], player_id = player_id }

local scoped_require = new_scoped_require(_ENV) -- create sandbox
local Player = scoped_require "player.player"
local react = Player.new(
tournament_address,
wallet,
machine_path,
blockchain_consts.endpoint,
hook
)

player0 = react
_debug_p0 = Player
end

-- Create honest player 1 in its own scope/sandbox
local _debug_p1 -- debug only
local player1
do
local player_id = 1
local wallet = { pk = blockchain_consts.pks[player_id], player_id = player_id }

local scoped_require = new_scoped_require(_ENV) -- create sandbox
local Player = scoped_require "player.player"
local react = Player.new(
tournament_address,
wallet,
machine_path,
blockchain_consts.endpoint,
hook
)

player1 = react
_debug_p1 = Player
end

assert(_debug_p0 ~= _debug_p1)

-- now we have to players: player0 and player1.
-- these are actually coroutines!!
-- let's use them.

local function run_player(player, idx)
local ok, log = coroutine.resume(player)

if not ok then
print(string.format("player %d died", idx))
return false
elseif coroutine.status(player0) == "dead" then
print(string.format("player %d has finished", idx))
return false
else
return true, log
end
end

local function run(players)
local finished = false

repeat
finished = true
local idle = true

for i, player in ipairs(players) do
if not player then
goto continue
end

local ok, log = run_player(player, i)

if ok then
finished = finished and log.finished
idle = idle and log.idle
end

::continue::
end

if idle then
-- all players are idle
-- evm advance time
end

time.sleep(5) -- I'm thinking, can we remove this and just rely on advances?
until finished
end

-- run { player0, player1 }

0 comments on commit 55ccf11

Please sign in to comment.