-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(prt-test): create scoped require
- Loading branch information
Showing
10 changed files
with
166 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |