Skip to content

Commit

Permalink
feat(protocol): improve getTransitions (#18181)
Browse files Browse the repository at this point in the history
Co-authored-by: dantaik <dantaik@users.noreply.github.com>
  • Loading branch information
dantaik and dantaik authored Sep 26, 2024
1 parent 8bd3717 commit 868d733
Show file tree
Hide file tree
Showing 3 changed files with 13,264 additions and 10,871 deletions.
37 changes: 9 additions & 28 deletions packages/protocol/contracts/layer1/based/LibUtils.sol
Original file line number Diff line number Diff line change
Expand Up @@ -165,32 +165,8 @@ 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.
/// @notice This function will revert if the transition is not found. 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.
Expand All @@ -215,7 +191,8 @@ library LibUtils {
return _state.transitions[slot][tid];
}

/// @dev Retrieves the transitions with a batch of parentHash.
/// @notice Gets the state transitions for a batch of block. For transition that doesn't exist,
/// the corresponding transition state will be empty.
/// @param _state Current TaikoData.State.
/// @param _config Actual TaikoData.Config.
/// @param _blockIds Id array of the blocks.
Expand All @@ -236,7 +213,11 @@ library LibUtils {
}
transitions_ = new TaikoData.TransitionState[](_blockIds.length);
for (uint256 i; i < _blockIds.length; ++i) {
transitions_[i] = getTransition(_state, _config, _blockIds[i], _parentHashes[i]);
(TaikoData.BlockV2 storage blk, uint64 slot) = getBlock(_state, _config, _blockIds[i]);
uint24 tid = getTransitionId(_state, blk, slot, _parentHashes[i]);
if (tid != 0) {
transitions_[i] = _state.transitions[slot][tid];
}
}
}

Expand Down
21 changes: 4 additions & 17 deletions packages/protocol/contracts/layer1/based/TaikoL1.sol
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,8 @@ contract TaikoL1 is EssentialContract, ITaikoL1, TaikoEvents {
(blk_,) = LibUtils.getBlock(state, getConfig(), _blockId);
}

/// @notice Gets the state transition for a specific block.
/// @notice This function will revert if the transition is not found. This function will revert
/// if the transition is not found.
/// @param _blockId Index of the block.
/// @param _parentHash Parent hash of the block.
/// @return The state transition data of the block.
Expand All @@ -212,7 +213,8 @@ contract TaikoL1 is EssentialContract, ITaikoL1, TaikoEvents {
return LibUtils.getTransition(state, getConfig(), _blockId, _parentHash);
}

/// @notice Gets the state transitions for a batch of block.
/// @notice Gets the state transitions for a batch of block. For transition that doesn't exist,
/// the corresponding transition state will be empty.
/// @param _blockIds Index of the blocks.
/// @param _parentHashes Parent hashes of the blocks.
/// @return The state transition array of the blocks.
Expand Down Expand Up @@ -242,21 +244,6 @@ 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

0 comments on commit 868d733

Please sign in to comment.