feat(incremental-merkle-tree.sol): return root on insert

This commit is contained in:
Chance
2023-03-24 12:00:27 -05:00
parent e2f8f49b45
commit eecb241364
2 changed files with 6 additions and 3 deletions

View File

@@ -132,7 +132,7 @@ library IncrementalBinaryTree {
/// @dev Inserts a leaf in the tree.
/// @param self: Tree data.
/// @param leaf: Leaf to be inserted.
function insert(IncrementalTreeData storage self, uint256 leaf) public {
function insert(IncrementalTreeData storage self, uint256 leaf) public returns (uint256) {
uint256 depth = self.depth;
require(leaf < SNARK_SCALAR_FIELD, "IncrementalBinaryTree: leaf must be < SNARK_SCALAR_FIELD");
@@ -159,6 +159,7 @@ library IncrementalBinaryTree {
self.root = hash;
self.numberOfLeaves += 1;
return hash;
}
/// @dev Updates a leaf in the tree.

View File

@@ -33,9 +33,11 @@ contract IncrementalBinaryTreeTest {
function insertLeaf(bytes32 _treeId, uint256 _leaf) external {
require(trees[_treeId].depth != 0, "IncrementalBinaryTreeTest: tree does not exist");
trees[_treeId].insert(_leaf);
uint256 root = trees[_treeId].insert(_leaf);
emit LeafInserted(_treeId, _leaf, trees[_treeId].root);
require(root == trees[_treeId].root, "root mismatch");
emit LeafInserted(_treeId, _leaf, root);
}
function updateLeaf(