diff --git a/l1-contracts/contracts/bridgehub/IMessageRoot.sol b/l1-contracts/contracts/bridgehub/IMessageRoot.sol index da2d5131b..0f292ce69 100644 --- a/l1-contracts/contracts/bridgehub/IMessageRoot.sol +++ b/l1-contracts/contracts/bridgehub/IMessageRoot.sol @@ -7,5 +7,5 @@ import {IBridgehub} from "./IBridgehub.sol"; interface IMessageRoot { function BRIDGE_HUB() external view returns (IBridgehub); - // function chainMessageRoot(uint256 _chainId) external view returns (bytes32); + function chainMessageRoot(uint256 _chainId) external view returns (bytes32); } diff --git a/l1-contracts/contracts/bridgehub/MessageRoot.sol b/l1-contracts/contracts/bridgehub/MessageRoot.sol index 1a649f9bf..e7f7c3426 100644 --- a/l1-contracts/contracts/bridgehub/MessageRoot.sol +++ b/l1-contracts/contracts/bridgehub/MessageRoot.sol @@ -24,17 +24,17 @@ contract MessageRoot is IMessageRoot, ReentrancyGuard, Ownable2StepUpgradeable, /// @dev Bridgehub smart contract that is used to operate with L2 via asynchronous L2 <-> L1 communication. IBridgehub public immutable override BRIDGE_HUB; - FullMerkle.FullTree public sharedTree; - - /// @dev the incremental merkle tree storing the chain message roots - mapping(uint256 chainId => DynamicIncrementalMerkle.Bytes32PushTree tree) internal chainTree; + uint256 public chainCount; mapping(uint256 chainId => uint256 chainIndex) public chainIndex; - uint256 public chainCount; - mapping(uint256 chainIndex => uint256 chainId) public chainIndexToId; + FullMerkle.FullTree public sharedTree; + + /// @dev the incremental merkle tree storing the chain message roots + mapping(uint256 chainId => DynamicIncrementalMerkle.Bytes32PushTree tree) internal chainTree; + /// @notice only the bridgehub can call modifier onlyBridgehub() { require(msg.sender == address(BRIDGE_HUB), "MR: only bridgehub"); @@ -54,16 +54,20 @@ contract MessageRoot is IMessageRoot, ReentrancyGuard, Ownable2StepUpgradeable, BRIDGE_HUB = _bridgehub; } + function chainMessageRoot(uint256 _chainId) external view override returns (bytes32) { + return chainTree[_chainId].root(); + } + /// @dev add a new chainBatchRoot to the chainTree function addChainBatchRoot( uint256 _chainId, bytes32 _chainBatchRoot, - bool _updateFMT + bool _updateFullTree ) external onlyChain(_chainId) { bytes32 chainRoot; (, chainRoot) = chainTree[_chainId].push(_chainBatchRoot); - if (_updateFMT) { + if (_updateFullTree) { sharedTree.updateLeaf(chainIndex[_chainId], chainRoot); } } diff --git a/l1-contracts/contracts/common/libraries/openzeppelin/IncrementalMerkle.sol b/l1-contracts/contracts/common/libraries/openzeppelin/IncrementalMerkle.sol index 135164b06..76e3fe4a3 100644 --- a/l1-contracts/contracts/common/libraries/openzeppelin/IncrementalMerkle.sol +++ b/l1-contracts/contracts/common/libraries/openzeppelin/IncrementalMerkle.sol @@ -143,23 +143,8 @@ library DynamicIncrementalMerkle { /** * @dev Tree's height (set at initialization) */ - function height(Bytes32PushTree storage self) internal view returns (uint256) { - return self._height; - } function root(Bytes32PushTree storage self) internal view returns (bytes32) { return Arrays.unsafeAccess(self._sides, self._height).value; } - - function nextLeafIndex(Bytes32PushTree storage self) internal view returns (uint256) { - return self._nextLeafIndex; - } - - function side(Bytes32PushTree storage self, uint256 i) internal view returns (bytes32) { - return Arrays.unsafeAccess(self._sides, i).value; - } - - function zeros(Bytes32PushTree storage self, uint256 i) internal view returns (bytes32) { - return Arrays.unsafeAccess(self._zeros, i).value; - } } diff --git a/l1-contracts/contracts/dev-contracts/test/IncrementalMerkleTest.sol b/l1-contracts/contracts/dev-contracts/test/IncrementalMerkleTest.sol index 130c74007..f4ec0bbbb 100644 --- a/l1-contracts/contracts/dev-contracts/test/IncrementalMerkleTest.sol +++ b/l1-contracts/contracts/dev-contracts/test/IncrementalMerkleTest.sol @@ -3,6 +3,8 @@ pragma solidity 0.8.24; import {DynamicIncrementalMerkle} from "../../common/libraries/openzeppelin/IncrementalMerkle.sol"; +import {Arrays} from "../../common/libraries/openzeppelin/Arrays.sol"; + contract IncrementalMerkleTest { using DynamicIncrementalMerkle for DynamicIncrementalMerkle.Bytes32PushTree; @@ -22,18 +24,18 @@ contract IncrementalMerkleTest { } function height() external view returns (uint256) { - return tree.height(); + return tree._height; } function index() external view returns (uint256) { - return tree.nextLeafIndex(); + return tree._nextLeafIndex; } function side(uint256 _index) external view returns (bytes32) { - return tree.side(_index); + return Arrays.unsafeAccess(tree._sides, _index).value; } function zeros(uint256 _index) external view returns (bytes32) { - return tree.zeros(_index); + return Arrays.unsafeAccess(tree._zeros, _index).value; } }