From 273bf53fad763b8504353e7cc14c8585e341f9d0 Mon Sep 17 00:00:00 2001 From: Gavin Yu Date: Sat, 21 Sep 2024 10:06:44 +0800 Subject: [PATCH] feat(protocol): introduce `getTransitions` in TaikoL1 (#18154) Co-authored-by: Karim Co-authored-by: Daniel Wang <99078276+dantaik@users.noreply.github.com> Co-authored-by: Daniel Wang --- .github/workflows/protocol.yml | 2 +- .../contracts/layer1/based/LibUtils.sol | 51 +++++++++++++++++++ .../contracts/layer1/based/TaikoL1.sol | 30 +++++++++++ 3 files changed, 82 insertions(+), 1 deletion(-) diff --git a/.github/workflows/protocol.yml b/.github/workflows/protocol.yml index 184c898102..9ec582faeb 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 d302a7b31d..26ffc29a0f 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 d15f655cf7..2f103841e2 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.