From 453d7300065dde3f27f21c2744f9451ad30fe679 Mon Sep 17 00:00:00 2001 From: gavin Date: Fri, 20 Sep 2024 15:33:18 +0800 Subject: [PATCH 1/8] feat(protocol): introduce `getTransitions` in TaikoL1 --- .../contracts/layer1/based/LibUtils.sol | 55 ++++++++++++++++++- .../contracts/layer1/based/TaikoL1.sol | 30 ++++++++++ 2 files changed, 84 insertions(+), 1 deletion(-) diff --git a/packages/protocol/contracts/layer1/based/LibUtils.sol b/packages/protocol/contracts/layer1/based/LibUtils.sol index d302a7b31d6..1b323834841 100644 --- a/packages/protocol/contracts/layer1/based/LibUtils.sol +++ b/packages/protocol/contracts/layer1/based/LibUtils.sol @@ -46,6 +46,7 @@ library LibUtils { error L1_INVALID_GENESIS_HASH(); error L1_TRANSITION_NOT_FOUND(); error L1_UNEXPECTED_TRANSITION_ID(); + error L1_INVALID_PARAMS(); /// @notice Initializes the Taiko protocol state. /// @param _state The state to initialize. @@ -164,11 +165,37 @@ 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 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) + { + if (_blockIds.length == 0 || _blockIds.length != _tids.length) { + revert L1_INVALID_PARAMS(); + } + TaikoData.TransitionState[] memory transitions; + for (uint256 i; i < _blockIds.length; ++i) { + transitions.push(getTransition(_state, _config, _blockIds[i], _tids[i])); + } + return transitions; + } + /// @notice This function will revert if the transition is not found. /// @dev Retrieves the transition with a given parentHash. /// @param _state Current TaikoData.State. /// @param _config Actual TaikoData.Config. - /// @param _blockId Id of the block. + /// @param _blockId Id of the blocks. /// @param _parentHash Parent hash of the block. /// @return The state transition pointer. function getTransition( @@ -189,6 +216,32 @@ 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 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) + { + if (_blockIds.length == 0 || _blockIds.length != _parentHashes.length) { + revert L1_INVALID_PARAMS(); + } + TaikoData.TransitionState[] memory transitions; + for (uint256 i; i < _blockIds.length; ++i) { + transitions.push(getTransition(_state, _config, _blockIds[i], _parentHashes[i])); + } + return transitions; + } + /// @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..308c057faab 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 _blockId Index array of the blocks. + /// @param _parentHash 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. From 5dd741dfd609cf7fc248759d1e0a167efda4a3ff Mon Sep 17 00:00:00 2001 From: gavin Date: Fri, 20 Sep 2024 16:14:31 +0800 Subject: [PATCH 2/8] fix --- packages/protocol/contracts/layer1/based/LibUtils.sol | 10 ++++++---- packages/protocol/contracts/layer1/based/TaikoL1.sol | 4 ++-- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/packages/protocol/contracts/layer1/based/LibUtils.sol b/packages/protocol/contracts/layer1/based/LibUtils.sol index 1b323834841..7694ca15a16 100644 --- a/packages/protocol/contracts/layer1/based/LibUtils.sol +++ b/packages/protocol/contracts/layer1/based/LibUtils.sol @@ -184,9 +184,10 @@ library LibUtils { if (_blockIds.length == 0 || _blockIds.length != _tids.length) { revert L1_INVALID_PARAMS(); } - TaikoData.TransitionState[] memory transitions; + TaikoData.TransitionState[] memory transitions = + new TaikoData.TransitionState[](_blockIds.length); for (uint256 i; i < _blockIds.length; ++i) { - transitions.push(getTransition(_state, _config, _blockIds[i], _tids[i])); + transitions[i] = getTransition(_state, _config, _blockIds[i], _tids[i]); } return transitions; } @@ -235,9 +236,10 @@ library LibUtils { if (_blockIds.length == 0 || _blockIds.length != _parentHashes.length) { revert L1_INVALID_PARAMS(); } - TaikoData.TransitionState[] memory transitions; + TaikoData.TransitionState[] memory transitions = + new TaikoData.TransitionState[](_blockIds.length); for (uint256 i; i < _blockIds.length; ++i) { - transitions.push(getTransition(_state, _config, _blockIds[i], _parentHashes[i])); + transitions[i] = getTransition(_state, _config, _blockIds[i], _parentHashes[i]); } return transitions; } diff --git a/packages/protocol/contracts/layer1/based/TaikoL1.sol b/packages/protocol/contracts/layer1/based/TaikoL1.sol index 308c057faab..2f103841e25 100644 --- a/packages/protocol/contracts/layer1/based/TaikoL1.sol +++ b/packages/protocol/contracts/layer1/based/TaikoL1.sol @@ -243,8 +243,8 @@ contract TaikoL1 is EssentialContract, ITaikoL1, TaikoEvents { } /// @notice Gets the state transitions for a batch of block. - /// @param _blockId Index array of the blocks. - /// @param _parentHash The transition id array of the blocks. + /// @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, From 0b4f33eef320bc420c8c4a8fa7a3cb409549d956 Mon Sep 17 00:00:00 2001 From: Gavin Yu Date: Fri, 20 Sep 2024 17:05:20 +0800 Subject: [PATCH 3/8] Update packages/protocol/contracts/layer1/based/LibUtils.sol --- packages/protocol/contracts/layer1/based/LibUtils.sol | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/protocol/contracts/layer1/based/LibUtils.sol b/packages/protocol/contracts/layer1/based/LibUtils.sol index 7694ca15a16..df2ee2ceb2d 100644 --- a/packages/protocol/contracts/layer1/based/LibUtils.sol +++ b/packages/protocol/contracts/layer1/based/LibUtils.sol @@ -196,7 +196,7 @@ library LibUtils { /// @dev Retrieves the transition with a given parentHash. /// @param _state Current TaikoData.State. /// @param _config Actual TaikoData.Config. - /// @param _blockId Id of the blocks. + /// @param _blockId Id of the block. /// @param _parentHash Parent hash of the block. /// @return The state transition pointer. function getTransition( From 675aa65b9f202895cd722819ac5dfae9157f7297 Mon Sep 17 00:00:00 2001 From: Karim Date: Fri, 20 Sep 2024 11:24:08 +0200 Subject: [PATCH 4/8] add wget pkg to workflow --- .github/workflows/protocol.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 From 8dfb6eb9ba12507b71da8a57827eb9ccf91ed7b0 Mon Sep 17 00:00:00 2001 From: Daniel Wang <99078276+dantaik@users.noreply.github.com> Date: Fri, 20 Sep 2024 21:36:57 +0800 Subject: [PATCH 5/8] Update LibUtils.sol --- packages/protocol/contracts/layer1/based/LibUtils.sol | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/protocol/contracts/layer1/based/LibUtils.sol b/packages/protocol/contracts/layer1/based/LibUtils.sol index df2ee2ceb2d..01b7a14f04c 100644 --- a/packages/protocol/contracts/layer1/based/LibUtils.sol +++ b/packages/protocol/contracts/layer1/based/LibUtils.sol @@ -43,10 +43,10 @@ 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(); - error L1_INVALID_PARAMS(); /// @notice Initializes the Taiko protocol state. /// @param _state The state to initialize. From 047cfca0f590513b6a9421100b78064c648599fa Mon Sep 17 00:00:00 2001 From: Daniel Wang <99078276+dantaik@users.noreply.github.com> Date: Fri, 20 Sep 2024 21:37:59 +0800 Subject: [PATCH 6/8] Update LibUtils.sol --- packages/protocol/contracts/layer1/based/LibUtils.sol | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/packages/protocol/contracts/layer1/based/LibUtils.sol b/packages/protocol/contracts/layer1/based/LibUtils.sol index 01b7a14f04c..161751958fd 100644 --- a/packages/protocol/contracts/layer1/based/LibUtils.sol +++ b/packages/protocol/contracts/layer1/based/LibUtils.sol @@ -179,17 +179,15 @@ library LibUtils { ) internal view - returns (TaikoData.TransitionState[] memory) + returns (TaikoData.TransitionState[] memory transitions_) { if (_blockIds.length == 0 || _blockIds.length != _tids.length) { revert L1_INVALID_PARAMS(); } - TaikoData.TransitionState[] memory transitions = - new TaikoData.TransitionState[](_blockIds.length); + transitions_ = new TaikoData.TransitionState[](_blockIds.length); for (uint256 i; i < _blockIds.length; ++i) { - transitions[i] = getTransition(_state, _config, _blockIds[i], _tids[i]); + transitions_[i] = getTransition(_state, _config, _blockIds[i], _tids[i]); } - return transitions; } /// @notice This function will revert if the transition is not found. From be9fbc3bf5d0bfe34deebbb019ccdab7509ade4a Mon Sep 17 00:00:00 2001 From: Daniel Wang <99078276+dantaik@users.noreply.github.com> Date: Fri, 20 Sep 2024 21:38:19 +0800 Subject: [PATCH 7/8] Update LibUtils.sol --- packages/protocol/contracts/layer1/based/LibUtils.sol | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/protocol/contracts/layer1/based/LibUtils.sol b/packages/protocol/contracts/layer1/based/LibUtils.sol index 161751958fd..ab1e9761a92 100644 --- a/packages/protocol/contracts/layer1/based/LibUtils.sol +++ b/packages/protocol/contracts/layer1/based/LibUtils.sol @@ -170,7 +170,7 @@ library LibUtils { /// @param _config Actual TaikoData.Config. /// @param _blockIds Id array of the block. /// @param _tids The transition id array. - /// @return The state transition pointer array. + /// @return transitions_ The state transition pointer array. function getTransitions( TaikoData.State storage _state, TaikoData.Config memory _config, From cf72a7ada82da6fd62a6aa8d76aaf41281f136e5 Mon Sep 17 00:00:00 2001 From: Daniel Wang Date: Fri, 20 Sep 2024 21:41:29 +0800 Subject: [PATCH 8/8] Update LibUtils.sol --- packages/protocol/contracts/layer1/based/LibUtils.sol | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/packages/protocol/contracts/layer1/based/LibUtils.sol b/packages/protocol/contracts/layer1/based/LibUtils.sol index ab1e9761a92..26ffc29a0fb 100644 --- a/packages/protocol/contracts/layer1/based/LibUtils.sol +++ b/packages/protocol/contracts/layer1/based/LibUtils.sol @@ -220,7 +220,7 @@ library LibUtils { /// @param _config Actual TaikoData.Config. /// @param _blockIds Id array of the blocks. /// @param _parentHashes Parent hashes of the blocks. - /// @return The state transition pointer array. + /// @return transitions_ The state transition pointer array. function getTransitions( TaikoData.State storage _state, TaikoData.Config memory _config, @@ -229,17 +229,15 @@ library LibUtils { ) internal view - returns (TaikoData.TransitionState[] memory) + returns (TaikoData.TransitionState[] memory transitions_) { if (_blockIds.length == 0 || _blockIds.length != _parentHashes.length) { revert L1_INVALID_PARAMS(); } - TaikoData.TransitionState[] memory transitions = - new TaikoData.TransitionState[](_blockIds.length); + transitions_ = new TaikoData.TransitionState[](_blockIds.length); for (uint256 i; i < _blockIds.length; ++i) { - transitions[i] = getTransition(_state, _config, _blockIds[i], _parentHashes[i]); + transitions_[i] = getTransition(_state, _config, _blockIds[i], _parentHashes[i]); } - return transitions; } /// @dev Retrieves the ID of the transition with a given parentHash.