-
Notifications
You must be signed in to change notification settings - Fork 338
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: kelemeno <[email protected]> Co-authored-by: Vlad Bochok <[email protected]>
- Loading branch information
1 parent
8c0b80c
commit 1c4dce4
Showing
10 changed files
with
411 additions
and
98 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
94 changes: 0 additions & 94 deletions
94
l1-contracts/test/foundry/unit/concrete/common/libraries/FullMerkle/FullMerkle.t.sol
This file was deleted.
Oops, something went wrong.
82 changes: 82 additions & 0 deletions
82
l1-contracts/test/foundry/unit/concrete/common/libraries/FullMerkle/PushNewLeaf.t.sol
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,82 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.24; | ||
|
||
import {FullMerkleTest} from "./_FullMerkle_Shared.t.sol"; | ||
|
||
contract PushNewLeafTest is FullMerkleTest { | ||
function test_oneLeaf() public { | ||
// Inserting one leaf | ||
bytes32 leaf0 = keccak256("Leaf 0"); | ||
merkleTest.pushNewLeaf(leaf0); | ||
|
||
// Checking the tree structure | ||
assertEq(merkleTest.height(), 0, "Height should be 0 after one insert"); | ||
assertEq(merkleTest.index(), 1, "Leaf number should be 1 after one insert"); | ||
|
||
// Checking leaf node | ||
assertEq(merkleTest.node(0, 0), leaf0, "Node 0,0 should be correctly inserted"); | ||
|
||
// Chekcking zeros tree structure | ||
assertEq(merkleTest.zeros(0), zeroHash, "Zero 0 should be correctly inserted"); | ||
} | ||
|
||
function test_twoLeaves() public { | ||
// Inserting two leaves | ||
bytes32 leaf0 = keccak256("Leaf 0"); | ||
bytes32 leaf1 = keccak256("Leaf 1"); | ||
merkleTest.pushNewLeaf(leaf0); | ||
merkleTest.pushNewLeaf(leaf1); | ||
|
||
// Checking the tree structure | ||
assertEq(merkleTest.height(), 1, "Height should be 1 after two inserts"); | ||
assertEq(merkleTest.index(), 2, "Leaf number should be 2 after two inserts"); | ||
|
||
// Checking leaf nodes | ||
assertEq(merkleTest.node(0, 0), leaf0, "Node 0,0 should be correctly inserted"); | ||
assertEq(merkleTest.node(0, 1), leaf1, "Node 0,1 should be correctly inserted"); | ||
|
||
// Checking parent node | ||
bytes32 l01Hashed = keccak(leaf0, leaf1); | ||
assertEq(merkleTest.node(1, 0), l01Hashed, "Node 1,0 should be correctly inserted"); | ||
|
||
// Checking zeros | ||
bytes32 zeroHashed = keccak(zeroHash, zeroHash); | ||
assertEq(merkleTest.zeros(1), zeroHashed, "Zero 1 should be correctly inserted"); | ||
} | ||
|
||
function test_threeLeaves() public { | ||
// Insert three leaves | ||
bytes32 leaf0 = keccak256("Leaf 0"); | ||
bytes32 leaf1 = keccak256("Leaf 1"); | ||
bytes32 leaf2 = keccak256("Leaf 2"); | ||
merkleTest.pushNewLeaf(leaf0); | ||
merkleTest.pushNewLeaf(leaf1); | ||
merkleTest.pushNewLeaf(leaf2); | ||
|
||
// Checking the tree structure | ||
assertEq(merkleTest.height(), 2, "Height should be 2 after three inserts"); | ||
assertEq(merkleTest.index(), 3, "Leaf number should be 3 after three inserts"); | ||
|
||
// Checking leaf nodes | ||
assertEq(merkleTest.node(0, 0), leaf0, "Node 0,0 should be correctly inserted"); | ||
assertEq(merkleTest.node(0, 1), leaf1, "Node 0,1 should be correctly inserted"); | ||
assertEq(merkleTest.node(0, 2), leaf2, "Node 0,2 should be correctly inserted"); | ||
|
||
// Checking parent nodes | ||
bytes32 l01Hashed = keccak(leaf0, leaf1); | ||
assertEq(merkleTest.node(1, 0), l01Hashed, "Node 1,0 should be correctly inserted"); | ||
// there is no leaf3 so we hash leaf2 with zero | ||
bytes32 l23Hashed = keccak(leaf2, merkleTest.zeros(0)); | ||
assertEq(merkleTest.node(1, 1), l23Hashed, "Node 1,1 should be correctly inserted"); | ||
|
||
// Checking root node | ||
bytes32 l01l23Hashed = keccak(l01Hashed, l23Hashed); | ||
assertEq(merkleTest.node(2, 0), l01l23Hashed, "Node 2,0 should be correctly inserted"); | ||
|
||
// Checking zero | ||
bytes32 zeroHashed = keccak(zeroHash, zeroHash); | ||
assertEq(merkleTest.zeros(1), zeroHashed, "Zero 1 should be correctly inserted"); | ||
bytes32 zhHashed = keccak(zeroHashed, zeroHashed); | ||
assertEq(merkleTest.zeros(2), zhHashed, "Zero 2 should be correctly inserted"); | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
l1-contracts/test/foundry/unit/concrete/common/libraries/FullMerkle/Root.t.sol
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,56 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.24; | ||
|
||
import {FullMerkleTest} from "./_FullMerkle_Shared.t.sol"; | ||
|
||
contract RootTest is FullMerkleTest { | ||
function test_emptyTree() public view { | ||
// Initially tree is empty, root is the zero hash | ||
assertEq(merkleTest.root(), zeroHash, "Root should be zero hash initially"); | ||
} | ||
|
||
function test_oneLeaf() public { | ||
// Inserting one leaf | ||
bytes32 leaf = keccak256("Leaf 0"); | ||
merkleTest.pushNewLeaf(leaf); | ||
|
||
// With one leaf, root is the leaf itself | ||
assertEq(merkleTest.root(), leaf, "Root should be the leaf hash"); | ||
} | ||
|
||
function test_twoLeaves() public { | ||
// Inserting two leaves | ||
bytes32 leaf0 = keccak256("Leaf 0"); | ||
bytes32 leaf1 = keccak256("Leaf 1"); | ||
merkleTest.pushNewLeaf(leaf0); | ||
merkleTest.pushNewLeaf(leaf1); | ||
|
||
// Calculate expected root | ||
bytes32 expectedRoot = keccak(leaf0, leaf1); | ||
assertEq(merkleTest.root(), expectedRoot, "Root should be the hash of the two leaves"); | ||
} | ||
|
||
function test_nodeCountAndRoot() public { | ||
// Initially tree is empty | ||
assertEq(merkleTest.nodeCount(0), 1, "Initial node count at height 0 should be 1"); | ||
|
||
// Inserting three leaves and checking counts and root | ||
bytes32 leaf0 = keccak256("Leaf 0"); | ||
bytes32 leaf1 = keccak256("Leaf 1"); | ||
bytes32 leaf2 = keccak256("Leaf 2"); | ||
merkleTest.pushNewLeaf(leaf0); | ||
merkleTest.pushNewLeaf(leaf1); | ||
merkleTest.pushNewLeaf(leaf2); | ||
|
||
assertEq(merkleTest.nodeCount(0), 3, "Node count at height 0 should be 3 after three inserts"); | ||
assertEq(merkleTest.nodeCount(1), 2, "Node count at height 1 should be 2"); | ||
assertEq(merkleTest.nodeCount(2), 1, "Node count at height 2 should be 1"); | ||
|
||
// Calculate expected root to verify correctness | ||
bytes32 leftChild = keccak(leaf0, leaf1); | ||
bytes32 rightChild = keccak(leaf2, merkleTest.zeros(0)); | ||
bytes32 expectedRoot = keccak(leftChild, rightChild); | ||
|
||
assertEq(merkleTest.root(), expectedRoot, "Root should match expected value after inserts"); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
l1-contracts/test/foundry/unit/concrete/common/libraries/FullMerkle/Setup.t.sol
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,12 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.24; | ||
|
||
import {FullMerkleTest} from "./_FullMerkle_Shared.t.sol"; | ||
|
||
contract SetupTest is FullMerkleTest { | ||
function test_checkInit() public view { | ||
assertEq(merkleTest.height(), 0, "Height should be 0"); | ||
assertEq(merkleTest.index(), 0, "Leaf number should be 0"); | ||
assertEq(merkleTest.zeros(0), zeroHash, "Zero hash should be correctly initialized"); | ||
} | ||
} |
99 changes: 99 additions & 0 deletions
99
l1-contracts/test/foundry/unit/concrete/common/libraries/FullMerkle/UpdateAllLeaves.t.sol
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,99 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.24; | ||
|
||
import {FullMerkleTest} from "./_FullMerkle_Shared.t.sol"; | ||
|
||
contract UpdateAllLeavesTest is FullMerkleTest { | ||
function test_revertWhen_wrongLength() public { | ||
// Inserting two leaves | ||
bytes32 leaf0 = keccak256("Leaf 0"); | ||
bytes32 leaf1 = keccak256("Leaf 1"); | ||
merkleTest.pushNewLeaf(leaf0); | ||
merkleTest.pushNewLeaf(leaf1); | ||
|
||
// Preparing new leaves for full update | ||
bytes32[] memory newLeaves = new bytes32[](3); | ||
newLeaves[0] = keccak256("New Leaf 0"); | ||
newLeaves[1] = keccak256("New Leaf 1"); | ||
newLeaves[2] = keccak256("New Leaf 2"); | ||
|
||
// Updating all leaves with wrong length | ||
vm.expectRevert(bytes("FMT, wrong length")); | ||
merkleTest.updateAllLeaves(newLeaves); | ||
} | ||
|
||
function test_oneLeaf() public { | ||
// Inserting one leaf | ||
bytes32 leaf0 = keccak256("Leaf 0"); | ||
merkleTest.pushNewLeaf(leaf0); | ||
|
||
// Preparing new leaves for full update | ||
bytes32[] memory newLeaves = new bytes32[](1); | ||
newLeaves[0] = keccak256("New Leaf 0"); | ||
|
||
// Updating all leaves | ||
merkleTest.updateAllLeaves(newLeaves); | ||
|
||
// Checking leaf nodes | ||
assertEq(merkleTest.node(0, 0), newLeaves[0], "Node 0,0 should be correctly updated"); | ||
} | ||
|
||
function test_twoLeaves() public { | ||
// Inserting two leaves | ||
bytes32 leaf0 = keccak256("Leaf 0"); | ||
bytes32 leaf1 = keccak256("Leaf 1"); | ||
merkleTest.pushNewLeaf(leaf0); | ||
merkleTest.pushNewLeaf(leaf1); | ||
|
||
// Preparing new leaves for full update | ||
bytes32[] memory newLeaves = new bytes32[](2); | ||
newLeaves[0] = keccak256("New Leaf 0"); | ||
newLeaves[1] = keccak256("New Leaf 1"); | ||
|
||
// Updating all leaves | ||
merkleTest.updateAllLeaves(newLeaves); | ||
|
||
// Checking leaf nodes | ||
assertEq(merkleTest.node(0, 0), newLeaves[0], "Node 0,0 should be correctly updated"); | ||
assertEq(merkleTest.node(0, 1), newLeaves[1], "Node 0,1 should be correctly updated"); | ||
|
||
// Checking parent node | ||
bytes32 l01Hashed = keccak(newLeaves[0], newLeaves[1]); | ||
assertEq(merkleTest.node(1, 0), l01Hashed, "Node 1,0 should be correctly updated"); | ||
} | ||
|
||
function test_threeLeaves() public { | ||
// Inserting two leaves | ||
bytes32 leaf0 = keccak256("Leaf 0"); | ||
bytes32 leaf1 = keccak256("Leaf 1"); | ||
bytes32 leaf2 = keccak256("Leaf 2"); | ||
merkleTest.pushNewLeaf(leaf0); | ||
merkleTest.pushNewLeaf(leaf1); | ||
merkleTest.pushNewLeaf(leaf2); | ||
|
||
// Preparing new leaves for full update | ||
bytes32[] memory newLeaves = new bytes32[](3); | ||
newLeaves[0] = keccak256("New Leaf 0"); | ||
newLeaves[1] = keccak256("New Leaf 1"); | ||
newLeaves[2] = keccak256("New Leaf 2"); | ||
|
||
// Updating all leaves | ||
merkleTest.updateAllLeaves(newLeaves); | ||
|
||
// Checking leaf nodes | ||
assertEq(merkleTest.node(0, 0), newLeaves[0], "Node 0,0 should be correctly updated"); | ||
assertEq(merkleTest.node(0, 1), newLeaves[1], "Node 0,1 should be correctly updated"); | ||
assertEq(merkleTest.node(0, 2), newLeaves[2], "Node 0,2 should be correctly updated"); | ||
|
||
// Checking parent nodes | ||
bytes32 l01Hashed = keccak(newLeaves[0], newLeaves[1]); | ||
assertEq(merkleTest.node(1, 0), l01Hashed, "Node 1,0 should be correctly updated"); | ||
// There is no leaf3 so we hash leaf2 with zero | ||
bytes32 l23Hashed = keccak(newLeaves[2], merkleTest.zeros(0)); | ||
assertEq(merkleTest.node(1, 1), l23Hashed, "Node 1,1 should be correctly updated"); | ||
|
||
// Checking root node | ||
bytes32 l01l23Hashed = keccak(l01Hashed, l23Hashed); | ||
assertEq(merkleTest.node(2, 0), l01l23Hashed, "Node 2,0 should be correctly updated"); | ||
} | ||
} |
Oops, something went wrong.