Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(protocol): introduce getTransitions in TaikoL1 #18154

Merged
merged 10 commits into from
Sep 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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