Skip to content
This repository has been archived by the owner on Mar 12, 2024. It is now read-only.

Commit

Permalink
refactor(arbitration): split players into different Lua VMs
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenctw committed Sep 2, 2023
1 parent 96393c9 commit b560f3c
Show file tree
Hide file tree
Showing 11 changed files with 362 additions and 172 deletions.
2 changes: 1 addition & 1 deletion onchain/permissionless-arbitration/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
FROM cartesi/machine-emulator:main

USER 0
RUN apt-get -y update; apt-get -y install curl git
RUN apt-get -y update; apt-get -y install curl git; apt-get install -y procps
RUN curl -sSL https://github.com/foundry-rs/foundry/releases/download/nightly/foundry_nightly_linux_$(dpkg --print-architecture).tar.gz | \
tar -zx -C /usr/local/bin

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,11 +142,12 @@ end
local Client = {}
Client.__index = Client

function Client:new(blockchain)
function Client:new(account_index)
local blockchain_data = require "blockchain.constants"

local client = {
endpoint = blockchain.endpoint,
account = blockchain:new_account(),
blockchain = blockchain,
endpoint = blockchain_data.endpoint,
pk = blockchain_data.pks[account_index],
}

setmetatable(client, self)
Expand Down Expand Up @@ -193,8 +194,6 @@ function Client:_read_logs(tournament_address, sig, topics, data_sig)
end

local ret = parse_logs(logs, data_sig)

self.blockchain:read_to("eth_getLogs")
return ret
end

Expand Down Expand Up @@ -234,7 +233,6 @@ function Client:_call(address, sig, args)
end
handle:close()

self.blockchain:read_to("eth_call")
return ret
end

Expand Down Expand Up @@ -348,7 +346,7 @@ function Client:_send_tx(tournament_address, sig, args)

local cmd = string.format(
cast_send_template,
self.account.pk,
self.pk,
self.endpoint,
tournament_address,
sig,
Expand All @@ -364,7 +362,6 @@ function Client:_send_tx(tournament_address, sig, args)
error(string.format("Send transaction `%s` reverted:\n%s", sig, ret))
end
handle:close()
self.blockchain:read_to("eth_sendRawTransaction")
end

function Client:tx_join_tournament(tournament_address, final_state, proof, left_child, right_child)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
-- contains default 10 accounts of anvil test node, and deployed tournament contract address
local constants = {
endpoint = "http://127.0.0.1:8545",
addresses = {
"0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266",
"0x70997970C51812dc3A010C7d01b50e0d17dc79C8",
"0x3C44CdDdB6a900fa2b585dd299e03d12FA4293BC",
"0x90F79bf6EB2c4f870365E785982E1f101E93b906",
"0x15d34AAf54267DB7D7c367839AAf71A00a2C6A65",
"0x9965507D1a55bcC2695C58ba16FB37d819B0A4dc",
"0x976EA74026E726554dB657fA54763abd0C3a0aa9",
"0x14dC79964da2C08b23698B3D3cc7Ca32193d9955",
"0x23618e81E3f5cdF7f54C3d65f7FBc0aBf5B21E8f",
"0xa0Ee7A142d267C1f36714E4a8F75612F20a79720",
},
pks = {
"0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80",
"0x59c6995e998f97a5a0044966f0945389dc9e86dae88c7a8412f4603b6b78690d",
"0x5de4111afa1a4b94908f83103eb1f1706367c2e68ca870fc3fb9a804cdab365a",
"0x7c852118294e51e653712a81e05800f419141751be58f605c371e15141b007a6",
"0x47e179ec197488593b187f80a00eb0da91f1b9d0b13f8733639f19c30a34926a",
"0x8b3a350cf5c34c9194ca85829a2df0ec3153be0318b5e2d3348e872092edffba",
"0x92db14e403b83dfe3df233f83dfa3a0d7096f21ca9b0d6d6b8d88b2b4ec1564e",
"0x4bbbf85ce3377467afe5d46f804f221813b2bb87f24d81f60f1fcdbf7cbf4356",
"0xdbda1821b80551c9d65939329250298aa3472ba22feea921c0cf5d620ea67b97",
"0x2a871d0798f97d79848a013d4936a73bf4cc922c825d33c1cf7073dff6d409c6",
},
}

return constants
56 changes: 12 additions & 44 deletions onchain/permissionless-arbitration/offchain/blockchain/node.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ local function start_blockchain(account_num)
account_num = account_num or default_account_number
print(string.format("Starting blockchain with %d accounts...", account_num))

local cmd = string.format([[sh -c "echo $$ ; exec anvil --block-time 1 -a %d"]], account_num)
local cmd = string.format([[sh -c "echo $$ ; exec anvil --block-time 1 -a %d > anvil.log 2>&1"]], account_num)

local reader = io.popen(cmd)
assert(reader, "`popen` returned nil reader")
Expand All @@ -29,37 +29,9 @@ local function start_blockchain(account_num)
return handle
end

local function capture_blockchain_data(reader, account_num)
account_num = account_num or default_account_number
local str

local addresses = {}
repeat
str = reader:read();
local _, _, address = str:find [[%(%d+%) ("0x%x+")]]
if address then
table.insert(addresses, address)
end
until str:find("Private Keys")
assert(#addresses == account_num)

local pks = {}
repeat
str = reader:read();
local _, _, pk = str:find("%(%d+%) (0x%x+)")
if pk then
table.insert(pks, pk)
end
until str:find("Wallet")
assert(#pks == account_num)

local endpoint
repeat
str = reader:read();
_, _, endpoint = str:find("Listening on ([%w%p]+)")
until endpoint

return { address = addresses, pk = pks }, endpoint
local function capture_blockchain_data()
local blockchain_data = require "blockchain.constants"
return { address = blockchain_data.addresses, pk = blockchain_data.pks }, blockchain_data.endpoint
end


Expand All @@ -78,7 +50,7 @@ local function deploy_contracts(endpoint, deployer, initial_hash)

local _, _, sl_factory_address = handle_sl:read("*a"):find("Deployed to: (0x%x+)")
assert(sl_factory_address, "deployment failed, factory_address is nil")
print("Single Level factory deployed at", sl_factory_address)
print("Single Level factory deployed at: " .. sl_factory_address)
handle_sl:close()

--
Expand All @@ -95,7 +67,7 @@ local function deploy_contracts(endpoint, deployer, initial_hash)

local _, _, top_factory_address = handle_top:read("*a"):find("Deployed to: (0x%x+)")
assert(top_factory_address, "deployment failed, factory_address is nil")
print("Top factory deployed at", top_factory_address)
print("Top factory deployed at: " .. top_factory_address)
handle_top:close()

--
Expand All @@ -112,7 +84,7 @@ local function deploy_contracts(endpoint, deployer, initial_hash)

local _, _, mid_factory_address = handle_mid:read("*a"):find("Deployed to: (0x%x+)")
assert(mid_factory_address, "deployment failed, factory_address is nil")
print("Middle factory deployed at", mid_factory_address)
print("Middle factory deployed at: " .. mid_factory_address)
handle_mid:close()

--
Expand All @@ -129,7 +101,7 @@ local function deploy_contracts(endpoint, deployer, initial_hash)

local _, _, bot_factory_address = handle_bot:read("*a"):find("Deployed to: (0x%x+)")
assert(bot_factory_address, "deployment failed, factory_address is nil")
print("Bottom factory deployed at", bot_factory_address)
print("Bottom factory deployed at: " .. bot_factory_address)
handle_bot:close()


Expand All @@ -147,7 +119,7 @@ local function deploy_contracts(endpoint, deployer, initial_hash)

local _, _, tournament_factory_address = handle_tournament:read("*a"):find("Deployed to: (0x%x+)")
assert(tournament_factory_address, "deployment failed, factory_address is nil")
print("tournament factory deployed at", tournament_factory_address)
print("tournament factory deployed at: " .. tournament_factory_address)
handle_tournament:close()


Expand All @@ -166,7 +138,7 @@ local function deploy_contracts(endpoint, deployer, initial_hash)
local _, _, a = handle_root:read("*a"):find [["data":"0x000000000000000000000000(%x+)"]]
local address = "0x" .. a
assert(address, "deployment failed, address is nil")
print("Contract deployed at", address)
print("Contract deployed at: " .. address)
handle_root:close()

return address
Expand All @@ -179,12 +151,12 @@ function Blockchain:new(account_num)
local blockchain = {}

local handle = start_blockchain(account_num)
local accounts, endpoint = capture_blockchain_data(handle.reader, account_num)
local accounts, endpoint = capture_blockchain_data()

blockchain._handle = handle
blockchain._accounts = accounts
blockchain._current_account = 1
blockchain.endpoint = "http://" .. endpoint
blockchain.endpoint = endpoint

setmetatable(blockchain, self)
return blockchain
Expand All @@ -211,10 +183,6 @@ function Blockchain:deploy_contract(initial_hash, deployer)
return address, deployer
end

function Blockchain:read_to(p)
repeat until self._handle.reader:read():find(p)
end

-- local bc = Blockchain:new(100)
-- local initial_hash = "0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470"
-- bc:deploy_contract(initial_hash)
Expand Down
Loading

0 comments on commit b560f3c

Please sign in to comment.