Skip to content

Commit

Permalink
feat(protocol): introduce getTransitions in TaikoL1 (#18154)
Browse files Browse the repository at this point in the history
Co-authored-by: Karim <karim@taiko.xyz>
Co-authored-by: Daniel Wang <99078276+dantaik@users.noreply.github.com>
Co-authored-by: Daniel Wang <dan@taiko.xyz>
  • Loading branch information
4 people authored Sep 21, 2024
1 parent 92f571a commit 273bf53
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .github/workflows/protocol.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
51 changes: 51 additions & 0 deletions packages/protocol/contracts/layer1/based/LibUtils.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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.
Expand All @@ -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(
Expand Down
30 changes: 30 additions & 0 deletions packages/protocol/contracts/layer1/based/TaikoL1.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand Down

0 comments on commit 273bf53

Please sign in to comment.