Skip to content

Commit

Permalink
feat(prt-test): create scoped require
Browse files Browse the repository at this point in the history
  • Loading branch information
GCdePaula committed Sep 13, 2024
1 parent 710f7bd commit 07e1dec
Show file tree
Hide file tree
Showing 10 changed files with 166 additions and 72 deletions.
20 changes: 2 additions & 18 deletions prt/client-lua/player/player.lua
Original file line number Diff line number Diff line change
@@ -1,27 +1,11 @@
local StateFetcher = require "player.state"
local HonestStrategy = require "player.strategy"
local GarbageCollector = require "player.gc"
local Sender = require "player.sender"
local CommitmentBuilder = require "computation.commitment"
local time = require "utils.time"
local helper = require "utils.helper"


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

local function react()
local state = state_fetcher:fetch()

gc_strategy:react(state)
local log = honest_strategy:react(state)
local log = strategy:react(state)

if hook then
hook(state, log)
Expand Down
19 changes: 18 additions & 1 deletion prt/client-lua/player/strategy.lua
Original file line number Diff line number Diff line change
@@ -1,21 +1,33 @@
local helper = require "utils.helper"
local Machine = require "computation.machine"
local constants = require "computation.constants"
local GarbageCollector = require "player.gc"

local HonestStrategy = {}
HonestStrategy.__index = HonestStrategy

function HonestStrategy:new(commitment_builder, machine_path, sender)
local gc_strategy = GarbageCollector:new(sender)

local honest_strategy = {
commitment_builder = commitment_builder,
machine_path = machine_path,
sender = sender
sender = sender,
gc_strategy = gc_strategy,
}

setmetatable(honest_strategy, self)
return honest_strategy
end

function HonestStrategy:disable_gc()
self.gc_strategy = false
end

function HonestStrategy:enable_gc()
self.gc_strategy = GarbageCollector:new(self.sender)
end

function HonestStrategy:_join_tournament(tournament, commitment)
local f, left, right = commitment:children(commitment.root_hash)
assert(f)
Expand Down Expand Up @@ -320,6 +332,11 @@ function HonestStrategy:react(tournament)
self:_react_tournament(tournament, log)
log.idle = tx_count == self.sender.tx_count


if self.gc_strategy then
self.gc_strategy:react(tournament)
end

return log
end

Expand Down
4 changes: 4 additions & 0 deletions prt/tests/compute/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ DOOM_MACHINE_PATH := "/app/tests/compute/program/doom-compute-machine"
LUA_NODE ?= "true"

help:
@echo ' clean-graphics - deletes doom graphics'
@echo ' create-image - create `prt-compute:lua` docker image'
@echo ' test-simple - run PRT simple test'
@echo ' test-stress - run PRT stress test'
Expand Down Expand Up @@ -36,6 +37,9 @@ create-doom-dirs:
@mkdir -p pixels
@mkdir -p outputs

clean-graphics:
@rm -r pixels outputs

test-doom-with-graphics: create-image create-doom-dirs
@docker run --rm \
--env MACHINE_PATH=$(DOOM_MACHINE_PATH) \
Expand Down
43 changes: 43 additions & 0 deletions prt/tests/compute/runners/helpers/sybil_player.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
local StateFetcher = require "player.state"
local Sender = require "player.sender"
local HonestStrategy = require "player.strategy"
local helper = require "utils.helper"

local function new(wallet, tournament_address, machine_path, blokchain_endpoint, fake_commitment_count)
local state_fetcher = StateFetcher:new(tournament_address, blokchain_endpoint)
local sender = Sender:new(wallet.pk, wallet.player_id, blokchain_endpoint)


local FakeCommitmentBuilder = require "runners.helpers.fake_commitment"
local builder = FakeCommitmentBuilder:new(machine_path)
local strategy = HonestStrategy:new(builder, machine_path, sender)
strategy:disable_gc()

local function react()
local idle = true
local finished = true

-- an dishonest player can send multiple fake commitments
-- each of them is determined by the `fake_index` of `FakeCommitmentBuilder`
for i = 1, fake_commitment_count do
local state = state_fetcher:fetch()
helper.log_timestamp(string.format("react with fake index: %d", i))
strategy.commitment_builder.fake_index = i

local log = strategy:react(state)
idle = idle and log.idle
finished = finished and log.finished
end

return { idle = idle, finished = finished }
end

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

return new
19 changes: 15 additions & 4 deletions prt/tests/compute/runners/hero_runner.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@ 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"

-- Main Execution
local player_id = tonumber(arg[1])
local tournament = arg[2]
local tournament_address = arg[2]
local machine_path = arg[3]
local extra_data = helper.str_to_bool(arg[4])
local hook
Expand All @@ -21,10 +24,16 @@ end
local Player = require "player.player"
local blockchain_consts = require "blockchain.constants"

local react = Player.new(
{ pk = blockchain_consts.pks[player_id], player_id = player_id },
tournament,
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 react = Player.new(
tournament_address,
honest_strategy,
blockchain_consts.endpoint,
hook
)
Expand All @@ -33,6 +42,8 @@ repeat
local status, log = coroutine.resume(react)
assert(status)

if log.finished then return end

if log.idle then
helper.log_timestamp("player idling")
helper.touch_player_idle(player_id)
Expand Down
72 changes: 23 additions & 49 deletions prt/tests/compute/runners/sybil_runner.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,66 +2,40 @@
require "setup_path"

-- Required Modules
local StateFetcher = require "player.state"
local Sender = require "player.sender"
local HonestStrategy = require "player.strategy"
local time = require "utils.time"
local helper = require "utils.helper"

-- Function to start dishonest player
local function start_dishonest_player(wallet, tournament, machine_path, endpoint, fake_commitment_count)
local state_fetcher = StateFetcher:new(tournament, endpoint)
local sender = Sender:new(wallet.pk, wallet.player_id, endpoint)
local honest_strategy
do
local FakeCommitmentBuilder = require "runners.helpers.fake_commitment"
local builder = FakeCommitmentBuilder:new(machine_path)
honest_strategy = HonestStrategy:new(builder, machine_path, sender)
end

while true do
local tx_count = sender.tx_count

-- an dishonest player can send multiple fake commitments
-- each of them is determined by the `fake_index` of `FakeCommitmentBuilder`
local finish_count = 0
for i = 1, fake_commitment_count do
local state = state_fetcher:fetch()
helper.log_timestamp(string.format("react with fake index: %d", i))
honest_strategy.commitment_builder.fake_index = i
if honest_strategy:react(state).finished then
finish_count = finish_count + 1
end
end

if finish_count == fake_commitment_count then
-- all fake commitments are done
break
end

-- player is considered idle if no tx sent in current iteration
if tx_count == sender.tx_count then
helper.log_timestamp("player idling")
helper.touch_player_idle(wallet.player_id)
else
helper.rm_player_idle(wallet.player_id)
end

time.sleep(5)
end
end

-- Main Execution
local player_index = tonumber(arg[1])
local player_id = tonumber(arg[1])
local tournament = arg[2]
local machine_path = arg[3]
local fake_commitment_count = tonumber(arg[4])

local blockchain_consts = require "blockchain.constants"
start_dishonest_player(
{ pk = blockchain_consts.pks[player_index], player_id = player_index },
local wallet = { pk = blockchain_consts.pks[player_id], player_id = player_id }

local new_sybil = require "runners.helpers.sybil_player"

local react = new_sybil(
wallet,
tournament,
machine_path,
blockchain_consts.endpoint,
fake_commitment_count
)

repeat
local status, log = coroutine.resume(react)
assert(status)

if log.finished then return end

if log.idle then
helper.log_timestamp("player idling")
helper.touch_player_idle(player_id)
else
helper.rm_player_idle(player_id)
end

time.sleep(5)
until coroutine.status(react) == "dead"
6 changes: 6 additions & 0 deletions prt/tests/compute/todo
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Update readmes
corotine players
move rust compute here
refactor orchestration
refactor setup
improve config
15 changes: 15 additions & 0 deletions prt/tests/compute/utils/example.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
assert(#package.loaded == 0)
require "setup_path"

local const0 = require "blockchain.constants"

local new_scoped_require = require "utils.scoped_require"
local scoped_require1 = new_scoped_require(_ENV)
local env1, const1 = scoped_require1 "utils.test"
assert(_ENV ~= env1)
assert(const0 ~= const1)

local scoped_require2 = new_scoped_require(_ENV)
local env2, const2 = scoped_require2 "utils.test"
assert(env1 ~= env2)
assert(const1 ~= const2)
36 changes: 36 additions & 0 deletions prt/tests/compute/utils/scoped_require.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
local function find_chunk(name)
local x, err = package.searchpath(name, package.path)

if err then
error(string.format("module '%s' not found\n", name))
end

return x
end

local function new_scoped_require(env)
local new_env = { bananas = "bananas" }
setmetatable(new_env, { __index = env })
local loaded = {}

local function scoped_require(name)
if loaded[name] == nil then
local path = find_chunk(name)

local chunk, err = loadfile(path, "t", new_env)
if not chunk then error(err) end

local result = { chunk(name) }
loaded[name] = result
end

return table.unpack(loaded[name])
end

new_env.require = scoped_require

return scoped_require
end


return new_scoped_require
4 changes: 4 additions & 0 deletions prt/tests/compute/utils/test.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
assert(#package.loaded == 0)
require "setup_path"
local const = assert(require "blockchain.constants")
return _ENV, const

0 comments on commit 07e1dec

Please sign in to comment.