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

Commit

Permalink
refactor(arbitration): improve solidity code quality
Browse files Browse the repository at this point in the history
  • Loading branch information
GCdePaula committed Aug 10, 2023
1 parent 6fb7b5c commit 5860a9b
Show file tree
Hide file tree
Showing 5 changed files with 278 additions and 181 deletions.
77 changes: 67 additions & 10 deletions onchain/permissionless-arbitration/src/Commitment.sol
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,84 @@ import "./Machine.sol";
// import "./Merkle.sol";

library Commitment {
using Tree for Tree.Node;
using Commitment for Tree.Node;

function proveFinalState(
Tree.Node root,
function requireState(
Tree.Node commitment,
uint64 level,
Machine.Hash finalState,
uint256 position,
Machine.Hash state,
bytes32[] calldata hashProof
) internal pure {
root.proveHash(
uint64(1 << ArbitrationConstants.height(level)),
finalState,
uint64 treeHeight = ArbitrationConstants.height(level);
Tree.Node expectedCommitment = getRoot(
Machine.Hash.unwrap(state),
treeHeight,
position,
hashProof
);

require(commitment.eq(expectedCommitment), "commitment state doesn't match");
}


function isEven(uint256 x) private pure returns (bool) {
return x % 2 == 0;
}

function proveHash(
Tree.Node root,
function getRoot(
bytes32 leaf,
uint64 treeHeight,
uint256 position,
Machine.Hash hash,
bytes32[] calldata siblings
) internal pure returns (Tree.Node) {
uint nodesCount = treeHeight - 1;
assert(nodesCount == siblings.length);

for (uint i = 0; i < nodesCount; i++) {
if (isEven(position >> i)) {
leaf =
keccak256(abi.encodePacked(leaf, siblings[i]));
} else {
leaf =
keccak256(abi.encodePacked(siblings[i], leaf));
}
}

return Tree.Node.wrap(leaf);
}


function requireFinalState(
Tree.Node commitment,
uint64 level,
Machine.Hash finalState,
bytes32[] calldata hashProof
) internal pure {
// TODO: call Merkle library
uint64 treeHeight = ArbitrationConstants.height(level);
Tree.Node expectedCommitment = getRootForLastLeaf(
treeHeight,
Machine.Hash.unwrap(finalState),
hashProof
);

require(commitment.eq(expectedCommitment), "commitment last state doesn't match");
}


function getRootForLastLeaf(
uint64 treeHeight,
bytes32 leaf,
bytes32[] calldata siblings
) internal pure returns (Tree.Node) {
uint nodesCount = treeHeight - 1;
assert(nodesCount == siblings.length);

for (uint i = 0; i < nodesCount; i++) {
leaf = keccak256(abi.encodePacked(siblings[i], leaf));
}

return Tree.Node.wrap(leaf);
}
}
Loading

0 comments on commit 5860a9b

Please sign in to comment.