MerkleTreeMock.sol 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. // SPDX-License-Identifier: MIT
  2. pragma solidity ^0.8.20;
  3. import {MerkleTree} from "../utils/structs/MerkleTree.sol";
  4. contract MerkleTreeMock {
  5. using MerkleTree for MerkleTree.Bytes32PushTree;
  6. MerkleTree.Bytes32PushTree private _tree;
  7. // This mock only stored the latest root.
  8. // Production contract may want to store historical values.
  9. bytes32 public root;
  10. event LeafInserted(bytes32 leaf, uint256 index, bytes32 root);
  11. function setup(uint8 _depth, bytes32 _zero) public {
  12. root = _tree.setup(_depth, _zero);
  13. }
  14. function push(bytes32 leaf) public {
  15. (uint256 leafIndex, bytes32 currentRoot) = _tree.push(leaf);
  16. emit LeafInserted(leaf, leafIndex, currentRoot);
  17. root = currentRoot;
  18. }
  19. function depth() public view returns (uint256) {
  20. return _tree.depth();
  21. }
  22. // internal state
  23. function nextLeafIndex() public view returns (uint256) {
  24. return _tree._nextLeafIndex;
  25. }
  26. function sides(uint256 i) public view returns (bytes32) {
  27. return _tree._sides[i];
  28. }
  29. function zeros(uint256 i) public view returns (bytes32) {
  30. return _tree._zeros[i];
  31. }
  32. }