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

Commit

Permalink
chore(arbitration): update node for new contracts
Browse files Browse the repository at this point in the history
  • Loading branch information
GCdePaula committed Aug 18, 2023
1 parent dafb1c2 commit 487e68d
Show file tree
Hide file tree
Showing 13 changed files with 1,934 additions and 86 deletions.
31 changes: 19 additions & 12 deletions onchain/permissionless-arbitration/offchain/blockchain/client.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
local Hash = require "cryptography.hash"
local MerkleTree = require "cryptography.merkle_tree"
local eth_ebi = require "utils.eth_ebi"

local function parse_topics(json)
Expand Down Expand Up @@ -109,7 +110,13 @@ end
local function quote_args(args, not_quote)
local quoted_args = {}
for _, v in ipairs(args) do
if type(v) == "table" then
if type(v) == "table" and (getmetatable(v) == Hash or getmetatable(v) == MerkleTree) then
if not_quote then
table.insert(quoted_args, v:hex_string())
else
table.insert(quoted_args, '"' .. v:hex_string() .. '"')
end
elseif type(v) == "table" then
if v._tag == "tuple" then
local qa = quote_args(v, true)
local ca = table.concat(qa, ",")
Expand All @@ -122,7 +129,7 @@ local function quote_args(args, not_quote)
table.insert(quoted_args, sb)
end
elseif not_quote then
table.insert(quoted_args, v)
table.insert(quoted_args, tostring(v))
else
table.insert(quoted_args, '"' .. v .. '"')
end
Expand Down Expand Up @@ -235,8 +242,8 @@ function Client:read_match_created(tournament_address, commitment_hash)
local sig = "matchCreated(bytes32,bytes32,bytes32)"
local data_sig = "(bytes32)"

local logs1 = self:_read_logs(tournament_address, sig, { commitment_hash, false, false }, data_sig)
local logs2 = self:_read_logs(tournament_address, sig, { false, commitment_hash, false }, data_sig)
local logs1 = self:_read_logs(tournament_address, sig, { commitment_hash:hex_string(), false, false }, data_sig)
local logs2 = self:_read_logs(tournament_address, sig, { false, commitment_hash:hex_string(), false }, data_sig)

local logs = sort_and_dedup(join_tables(logs1, logs2))

Expand All @@ -260,7 +267,7 @@ end
function Client:read_commitment(tournament_address, commitment_hash)
local sig = "getCommitment(bytes32)((uint64,uint64),bytes32)"

local call_ret = self:_call(tournament_address, sig, { commitment_hash })
local call_ret = self:_call(tournament_address, sig, { commitment_hash:hex_string() })
assert(#call_ret == 2)

local allowance, last_resume = call_ret[1]:match "%((%d+),(%d+)%)"
Expand All @@ -283,23 +290,23 @@ function Client:read_tournament_created(tournament_address, match_id_hash)
local sig = "newInnerTournament(bytes32,address)"
local data_sig = "(address)"

local logs = self:_read_logs(tournament_address, sig, { match_id_hash, false, false }, data_sig)
local logs = self:_read_logs(tournament_address, sig, { match_id_hash:hex_string(), false, false }, data_sig)
assert(#logs <= 1)

if #logs == 0 then return false end
local log = logs[1]

local ret = {
parent_match = Hash:from_digest_hex(match_id_hash),
parent_match = match_id_hash,
new_tournament = log.decoded_data[1],
}

return ret
end

function Client:match(address, match_id_hash)
local sig = "getMatch(bytes32)(bytes32,bytes32,bytes32,uint64,uint64,uint64)"
local ret = self:_call(address, sig, { match_id_hash })
local sig = "getMatch(bytes32)(bytes32,bytes32,bytes32,uint256,uint64,uint64)"
local ret = self:_call(address, sig, { match_id_hash:hex_string() })
ret[1] = Hash:from_digest_hex(ret[1])
ret[2] = Hash:from_digest_hex(ret[2])
ret[3] = Hash:from_digest_hex(ret[3])
Expand Down Expand Up @@ -382,7 +389,7 @@ function Client:tx_seal_inner_match(
self:_send_tx(
tournament_address,
sig,
{ { commitment_one, commitment_two, _tag = "tuple" }, left, right, initial_hash, proof }
{ { commitment_one, commitment_two, _tag = "tuple" }, left, right, initial_hash:hex_string(), proof }
)
end

Expand All @@ -409,14 +416,14 @@ function Client:tx_seal_leaf_match(
end

function Client:tx_win_leaf_match(
tournament_address, commitment_one, commitment_two, left, right
tournament_address, commitment_one, commitment_two, left, right, proof
)
local sig =
[[winLeafMatch((bytes32,bytes32),bytes32,bytes32)]]
self:_send_tx(
tournament_address,
sig,
{ { commitment_one, commitment_two, _tag = "tuple" }, left, right }
{ { commitment_one, commitment_two, _tag = "tuple" }, left, right, proof }
)
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ local function build_small_machine_commitment(base_cycle, log2_stride_count, mac
end
end

return initial_state, builder:build()
return initial_state, builder:build(initial_state)
end


Expand All @@ -76,12 +76,14 @@ local function build_big_machine_commitment(base_cycle, log2_stride, log2_stride
end
end

return initial_state, builder:build()
return initial_state, builder:build(initial_state)
end

local function build_commitment(base_cycle, log2_stride, log2_stride_count, machine_path)
local machine = Machine:new_from_path(machine_path)

print(log2_stride, log2_stride_count)

if log2_stride >= consts.log2_uarch_span then
assert(
log2_stride + log2_stride_count <=
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
local MerkleBuilder = require "cryptography.merkle_builder"
local Hash = require "cryptography.hash"
local consts = require "constants"

local CommitmentBuilder = {}
CommitmentBuilder.__index = CommitmentBuilder

function CommitmentBuilder:new()
local c = {}
function CommitmentBuilder:new(initial_hash)
local c = { initial_hash = initial_hash }
setmetatable(c, self)
return c
end

function CommitmentBuilder:build(_, level)
local commitment = Hash.zero:iterated_merkle(consts.heights[level])
return commitment
local builder = MerkleBuilder:new()
builder:add(Hash.zero, 1 << consts.heights[consts.levels - level + 1])
-- local commitment = Hash.zero:iterated_merkle(consts.heights[level])
return builder:build(self.initial_hash)
end

return CommitmentBuilder
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
local Hash = require "cryptography.hash"
local arithmetic = require "utils.arithmetic"
local consts = require "constants"
local cartesi = require "cartesi"

local ComputationState = {}
Expand Down Expand Up @@ -98,7 +99,6 @@ function Machine:increment_uarch()
end

function Machine:ureset()
assert(self.ucycle == arithmetic.max_uint64)
self.machine:reset_uarch_state()
self.cycle = self.cycle + 1
self.ucycle = 0
Expand Down
6 changes: 3 additions & 3 deletions onchain/permissionless-arbitration/offchain/constants.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ local log2_uarch_span = 16
local log2_emulator_span = 47

local constants = {
levels = 4,
log2step = { 24, 14, 7, 0 },
heights = { 39, 10, 7, 7 },
levels = 3,
log2step = { 31, 16, 0 },
heights = { 32, 15, 16 },

log2_uarch_span = log2_uarch_span,
uarch_span = arithmetic.max_uint(log2_uarch_span),
Expand Down
20 changes: 12 additions & 8 deletions onchain/permissionless-arbitration/offchain/cryptography/hash.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ local keccak = require "cartesi".keccak

local function hex_from_bin(bin)
assert(bin:len() == 32)
return "0x" .. (bin:gsub('.', function (c)
return "0x" .. (bin:gsub('.', function(c)
return string.format('%02x', string.byte(c))
end))
end

local function bin_from_hex(hex)
assert(hex:len() == 66, string.format("%s %d", hex, hex:len()))
local h = assert(hex:match("0x(%x+)"), hex)
return (h:gsub('..', function (cc)
return (h:gsub('..', function(cc)
return string.char(tonumber(cc, 16))
end))
end
Expand All @@ -27,8 +27,8 @@ function Hash:from_digest(digest)
local x = internalized_hahes[digest]
if x then return x end

local h = {digest = digest}
iterateds[h] = {h}
local h = { digest = digest }
iterateds[h] = { h }
setmetatable(h, self)
internalized_hahes[digest] = h
return h
Expand All @@ -46,12 +46,12 @@ function Hash:from_data(data)
end

function Hash:join(other_hash)
Hash:is_of_type_hash(other_hash)
assert(Hash:is_of_type_hash(other_hash))

local digest = keccak(self.digest, other_hash.digest)
local ret = Hash:from_digest(digest)
ret.left = self.digest
ret.right = other_hash.digest
ret.left = self
ret.right = other_hash
return ret
end

Expand Down Expand Up @@ -82,7 +82,11 @@ function Hash:iterated_merkle(level)
return highest_level
end

Hash.__tostring = function (x)
function Hash:hex_string()
return hex_from_bin(self.digest)
end

Hash.__tostring = function(x)
return hex_from_bin(x.digest)
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ function Slice:find_cell_containing(elem)
return l
end


local MerkleBuilder = {}
MerkleBuilder.__index = MerkleBuilder

Expand All @@ -82,17 +81,17 @@ function MerkleBuilder:add(hash, rep)
local accumulated_count = rep + last.accumulated_count

if not math.ult(rep, accumulated_count) then -- overflow...
assert(accumulated_count == 0) -- then it has to be zero, and nothing else can fit.
assert(accumulated_count == 0) -- then it has to be zero, and nothing else can fit.
end

table.insert(self.leafs, {hash = hash, accumulated_count = accumulated_count})
table.insert(self.leafs, { hash = hash, accumulated_count = accumulated_count })
else
table.insert(self.leafs, {hash = hash, accumulated_count = rep})
table.insert(self.leafs, { hash = hash, accumulated_count = rep })
end
end

local function merkle(leafs, log2size, stride)
local first_time = stride * (1 << log2size) + 1
local first_time = stride * (1 << log2size) + 1
local last_time = (stride + 1) * (1 << log2size)

local first_cell = leafs:find_cell_containing(first_time)
Expand All @@ -109,7 +108,7 @@ local function merkle(leafs, log2size, stride)
return hash_left:join(hash_right)
end

function MerkleBuilder:build()
function MerkleBuilder:build(implicit_hash)
local last = assert(self.leafs[#self.leafs], #self.leafs)
local count = last.accumulated_count

Expand All @@ -122,7 +121,7 @@ function MerkleBuilder:build()
end

local root_hash = merkle(Slice:new(self.leafs), log2size, 0)
return MerkleTree:new(self.leafs, root_hash, log2size)
return MerkleTree:new(self.leafs, root_hash, log2size, implicit_hash)
end

-- local Hash = require "cryptography.hash"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
local MerkleTree = {}
MerkleTree.__index = MerkleTree

function MerkleTree:new(leafs, root_hash, log2size)
function MerkleTree:new(leafs, root_hash, log2size, implicit_hash)
local m = {
leafs = leafs,
root_hash = root_hash,
digest_hex = root_hash.digest_hex,
log2size = log2size,
implicit_hash = implicit_hash
}
setmetatable(m, self)
return m
Expand All @@ -24,7 +25,73 @@ function MerkleTree:iterated_merkle(level)
return self.root_hash:iterated_merkle(level)
end

-- TODO add generate proof.
-- TODO add children??
function MerkleTree:hex_string()
return self.root_hash:hex_string()
end

MerkleTree.__tostring = function(x)
return x.root_hash:hex_string()
end


local function generate_proof(proof, root, height, include_index)
if height == 0 then
if include_index == 0 then
proof.leaf = root
return
else
table.insert(proof, root)
end
end

local new_height = height - 1
local ok, left, right = root:children()
assert(ok)

if (include_index >> new_height) == 0 then
generate_proof(proof, left, new_height, include_index)
table.insert(proof, right)
else
generate_proof(proof, right, new_height, include_index)
table.insert(proof, left)
end
end

function MerkleTree:prove_leaf(index)
local height
local l = assert(self.leafs[1])
if l.log2size then
height = l.log2size + self.log2size
else
height = self.log2size
end

assert((index >> height) == 0)
local proof = {}
generate_proof(proof, self.root_hash, height, index)
return proof.leaf, proof
end

local function array_reverse(x)
local n, m = #x, #x / 2
for i = 1, m do
x[i], x[n - i + 1] = x[n - i + 1], x[i]
end
return x
end

function MerkleTree:last()
local proof = {}
local ok, left, right = self.root_hash:children()
local old_right = self.root_hash

while ok do
table.insert(proof, left)
old_right = right
ok, left, right = right:children()
end

return old_right, array_reverse(proof)
end

return MerkleTree
2 changes: 1 addition & 1 deletion onchain/permissionless-arbitration/offchain/entrypoint.lua
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ end
local p2
do
local FakeCommitmentBuilder = require "computation.fake_commitment"
local builder = FakeCommitmentBuilder:new()
local builder = FakeCommitmentBuilder:new(initial_hash)
local client = Client:new(blockchain)
p2 = Player:new(contract, client, builder)
end
Expand Down
Loading

0 comments on commit 487e68d

Please sign in to comment.