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
57 changes: 56 additions & 1 deletion packages/protocol/contracts/layer1/based/LibUtils.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -164,11 +165,38 @@ 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 =
new TaikoData.TransitionState[](_blockIds.length);
for (uint256 i; i < _blockIds.length; ++i) {
transitions[i] = 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(
Expand All @@ -189,6 +217,33 @@ 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 =
new TaikoData.TransitionState[](_blockIds.length);
for (uint256 i; i < _blockIds.length; ++i) {
transitions[i] = 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(
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
Loading