-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathprt_compute.lua
executable file
·158 lines (135 loc) · 5.65 KB
/
prt_compute.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#!/usr/bin/lua
require "setup_path"
-- amount of time to fastforward if `IDLE_LIMIT` is reached
local FAST_FORWARD_TIME = 300
-- delay time for blockchain node to be ready
local NODE_DELAY = 3
-- number of fake commitment to make
local FAKE_COMMITMENT_COUNT = 1
-- number of idle players
local IDLE_PLAYER_COUNT = 0
-- Required Modules
local new_scoped_require = require "test_utils.scoped_require"
local helper = require "utils.helper"
local blockchain_utils = require "blockchain.utils"
local time = require "utils.time"
local blockchain_constants = require "blockchain.constants"
local Blockchain = require "blockchain.node"
local CommitmentBuilder = require "computation.commitment"
local function write_json_file(leafs, root_tournament)
local prev_accumulated = 0
local new_leafs = {}
for i, leaf in ipairs(leafs) do
new_leafs[i] = {
hash = leaf.hash,
repetitions = leaf.accumulated_count - prev_accumulated
}
prev_accumulated = leaf.accumulated_count
end
local inputs_and_leafs = {
leafs = new_leafs
}
local flat = require "utils.flat"
local json = require "utils.json"
local work_path = string.format("/compute_data/%s", root_tournament)
if not helper.exists(work_path) then
helper.mkdir_p(work_path)
end
local file_path = string.format("%s/inputs_and_leafs.json", work_path)
local file = assert(io.open(file_path, "w"))
file:write(json.encode(flat.flatten(inputs_and_leafs).flat_object))
assert(file:close())
end
local function get_root_constants(root_tournament)
local Reader = require "player.reader"
local reader = Reader:new(blockchain_constants.endpoint)
local root_constants = reader:read_constants(root_tournament)
return root_constants
end
-- Function to setup players
local function setup_players(use_lua_node, extra_data, root_tournament, machine_path)
local root_constants = get_root_constants(root_tournament)
local inputs = nil
local player_coroutines = {}
local player_index = 1
print("Calculating root commitment...")
local snapshot_dir = string.format("/compute_data/%s", root_tournament)
local builder = CommitmentBuilder:new(machine_path, snapshot_dir)
local root_commitment = builder:build(0, 0, root_constants.log2_step, root_constants.height, inputs)
if use_lua_node then
-- use Lua node to defend
print("Setting up Lua honest player")
local start_hero = require "runners.hero_runner"
player_coroutines[player_index] = start_hero(player_index, machine_path, root_commitment, root_tournament,
extra_data, inputs)
else
-- use Rust node to defend
print("Setting up Rust honest player")
local rust_hero_runner = require "runners.rust_hero_runner"
player_coroutines[player_index] = rust_hero_runner.create_react_once_runner(player_index, machine_path)
-- write leafs to json file for rust node to use
write_json_file(root_commitment.leafs, root_tournament)
end
player_index = player_index + 1
if FAKE_COMMITMENT_COUNT > 0 then
print(string.format("Setting up dishonest player with %d fake commitments", FAKE_COMMITMENT_COUNT))
local scoped_require = new_scoped_require(_ENV)
local start_sybil = scoped_require "runners.sybil_runner"
player_coroutines[player_index] = start_sybil(player_index, machine_path, root_commitment, root_tournament,
FAKE_COMMITMENT_COUNT, inputs)
player_index = player_index + 1
end
if IDLE_PLAYER_COUNT > 0 then
print(string.format("Setting up %d idle players", IDLE_PLAYER_COUNT))
local scoped_require = new_scoped_require(_ENV)
local start_idle = scoped_require "runners.idle_runner"
for _ = 1, IDLE_PLAYER_COUNT do
player_coroutines[player_index] = start_idle(player_index, machine_path, root_tournament)
player_index = player_index + 1
end
end
return player_coroutines
end
-- Function to run players
local function run_players(player_coroutines)
while true do
local idle = true
local has_live_coroutine = false
for i, c in ipairs(player_coroutines) do
if c then
local success, ret = coroutine.resume(c)
local status = coroutine.status(c)
if status == "dead" then
player_coroutines[i] = false
end
if not success then
print(string.format("coroutine %d fail to resume with error: %s", i, ret))
elseif ret then
has_live_coroutine = true
idle = idle and ret.idle
end
end
end
if not has_live_coroutine then
print("No active players, ending program...")
break
end
if idle then
print(string.format("All players idle, fastforward blockchain for %d blocks...", FAST_FORWARD_TIME))
blockchain_utils.advance_time(FAST_FORWARD_TIME, blockchain_constants.endpoint)
end
end
end
-- Main Execution
local machine_path = os.getenv("MACHINE_PATH")
local use_lua_node = helper.str_to_bool(os.getenv("LUA_NODE"))
local extra_data = helper.str_to_bool(os.getenv("EXTRA_DATA"))
local root_tournament = blockchain_constants.root_tournament
local blockchain_node = Blockchain:new()
time.sleep(NODE_DELAY)
blockchain_utils.deploy_contracts("../../contracts")
time.sleep(NODE_DELAY)
local player_coroutines = setup_players(use_lua_node, extra_data, root_tournament, machine_path)
print("Hello from Dave compute lua prototype!")
run_players(player_coroutines)
print("Good-bye, world!")