diff --git a/.github/workflows/protocol.yml b/.github/workflows/protocol.yml index 184c898102e..9ec582faeb2 100644 --- a/.github/workflows/protocol.yml +++ b/.github/workflows/protocol.yml @@ -21,7 +21,7 @@ jobs: - name: Prepare environment continue-on-error: true - run: sudo apt-get update && sudo apt-get install -y git netcat + run: sudo apt-get update && sudo apt-get install -y git netcat wget - name: Checkout repository uses: actions/checkout@v4 diff --git a/packages/protocol/contracts/layer1/based/LibUtils.sol b/packages/protocol/contracts/layer1/based/LibUtils.sol index d302a7b31d6..26ffc29a0fb 100644 --- a/packages/protocol/contracts/layer1/based/LibUtils.sol +++ b/packages/protocol/contracts/layer1/based/LibUtils.sol @@ -43,6 +43,7 @@ library LibUtils { error L1_BLOCK_MISMATCH(); error L1_INVALID_BLOCK_ID(); + error L1_INVALID_PARAMS(); error L1_INVALID_GENESIS_HASH(); error L1_TRANSITION_NOT_FOUND(); error L1_UNEXPECTED_TRANSITION_ID(); @@ -164,6 +165,31 @@ library LibUtils { return _state.transitions[slot][_tid]; } + /// @dev Retrieves the transitions with a batch of parentHash. + /// @param _state Current TaikoData.State. + /// @param _config Actual TaikoData.Config. + /// @param _blockIds Id array of the block. + /// @param _tids The transition id array. + /// @return transitions_ The state transition pointer array. + function getTransitions( + TaikoData.State storage _state, + TaikoData.Config memory _config, + uint64[] calldata _blockIds, + uint32[] calldata _tids + ) + internal + view + returns (TaikoData.TransitionState[] memory transitions_) + { + if (_blockIds.length == 0 || _blockIds.length != _tids.length) { + revert L1_INVALID_PARAMS(); + } + transitions_ = new TaikoData.TransitionState[](_blockIds.length); + for (uint256 i; i < _blockIds.length; ++i) { + transitions_[i] = getTransition(_state, _config, _blockIds[i], _tids[i]); + } + } + /// @notice This function will revert if the transition is not found. /// @dev Retrieves the transition with a given parentHash. /// @param _state Current TaikoData.State. @@ -189,6 +215,31 @@ library LibUtils { return _state.transitions[slot][tid]; } + /// @dev Retrieves the transitions with a batch of parentHash. + /// @param _state Current TaikoData.State. + /// @param _config Actual TaikoData.Config. + /// @param _blockIds Id array of the blocks. + /// @param _parentHashes Parent hashes of the blocks. + /// @return transitions_ The state transition pointer array. + function getTransitions( + TaikoData.State storage _state, + TaikoData.Config memory _config, + uint64[] calldata _blockIds, + bytes32[] calldata _parentHashes + ) + internal + view + returns (TaikoData.TransitionState[] memory transitions_) + { + if (_blockIds.length == 0 || _blockIds.length != _parentHashes.length) { + revert L1_INVALID_PARAMS(); + } + transitions_ = new TaikoData.TransitionState[](_blockIds.length); + for (uint256 i; i < _blockIds.length; ++i) { + transitions_[i] = getTransition(_state, _config, _blockIds[i], _parentHashes[i]); + } + } + /// @dev Retrieves the ID of the transition with a given parentHash. /// This function will return 0 if the transition is not found. function getTransitionId( diff --git a/packages/protocol/contracts/layer1/based/TaikoL1.sol b/packages/protocol/contracts/layer1/based/TaikoL1.sol index d15f655cf79..2f103841e25 100644 --- a/packages/protocol/contracts/layer1/based/TaikoL1.sol +++ b/packages/protocol/contracts/layer1/based/TaikoL1.sol @@ -212,6 +212,21 @@ contract TaikoL1 is EssentialContract, ITaikoL1, TaikoEvents { return LibUtils.getTransition(state, getConfig(), _blockId, _parentHash); } + /// @notice Gets the state transitions for a batch of block. + /// @param _blockIds Index of the blocks. + /// @param _parentHashes Parent hashes of the blocks. + /// @return The state transition array of the blocks. + function getTransitions( + uint64[] calldata _blockIds, + bytes32[] calldata _parentHashes + ) + external + view + returns (TaikoData.TransitionState[] memory) + { + return LibUtils.getTransitions(state, getConfig(), _blockIds, _parentHashes); + } + /// @notice Gets the state transition for a specific block. /// @param _blockId Index of the block. /// @param _tid The transition id. @@ -227,6 +242,21 @@ contract TaikoL1 is EssentialContract, ITaikoL1, TaikoEvents { return LibUtils.getTransition(state, getConfig(), _blockId, _tid); } + /// @notice Gets the state transitions for a batch of block. + /// @param _blockIds Index array of the blocks. + /// @param _tids The transition id array of the blocks. + /// @return The state transition array of the blocks. + function getTransitions( + uint64[] calldata _blockIds, + uint32[] calldata _tids + ) + external + view + returns (TaikoData.TransitionState[] memory) + { + return LibUtils.getTransitions(state, getConfig(), _blockIds, _tids); + } + /// @notice Returns information about the last verified block. /// @return blockId_ The last verified block's ID. /// @return blockHash_ The last verified block's blockHash.