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

refactor(protocol): introduce BlockV2 for client-side compability #17935

Merged
merged 3 commits into from
Aug 16, 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
21 changes: 18 additions & 3 deletions packages/protocol/contracts/L1/TaikoData.sol
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,22 @@ library TaikoData {
/// @dev Struct containing data required for verifying a block.
/// 3 slots used.
struct Block {
bytes32 metaHash; // slot 1
address assignedProver; // slot 2
uint96 livenessBond;
uint64 blockId; // slot 3
uint64 proposedAt; // timestamp
uint64 proposedIn; // L1 block number, required/used by node/client.
uint32 nextTransitionId;
// The ID of the transaction that is used to verify this block. However, if
// this block is not verified as the last block in a batch, verifiedTransitionId
// will remain zero.
uint32 verifiedTransitionId;
}

/// @dev Struct containing data required for verifying a block.
/// 3 slots used.
struct BlockV2 {
bytes32 metaHash; // slot 1
address assignedProver; // slot 2
uint96 livenessBond;
Expand Down Expand Up @@ -213,16 +229,15 @@ library TaikoData {
/// @dev Struct holding the state variables for the {TaikoL1} contract.
struct State {
// Ring buffer for proposed blocks and a some recent verified blocks.
mapping(uint64 blockId_mod_blockRingBufferSize => Block blk) blocks;
mapping(uint64 blockId_mod_blockRingBufferSize => BlockV2 blk) blocks;
// Indexing to transition ids (ring buffer not possible)
mapping(uint64 blockId => mapping(bytes32 parentHash => uint24 transitionId)) transitionIds;
// Ring buffer for transitions
mapping(
uint64 blockId_mod_blockRingBufferSize
=> mapping(uint32 transitionId => TransitionState ts)
) transitions;
// Ring buffer for Ether deposits
bytes32 __reserve1;
bytes32 __reserve1; // Used as a ring buffer for Ether deposits
SlotA slotA; // slot 5
SlotB slotB; // slot 6
mapping(address account => uint256 bond) bondBalance;
Expand Down
10 changes: 9 additions & 1 deletion packages/protocol/contracts/L1/TaikoL1.sol
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,14 @@ contract TaikoL1 is EssentialContract, ITaikoL1, TaikoEvents {
/// @param _blockId Index of the block.
/// @return blk_ The block.
function getBlock(uint64 _blockId) external view returns (TaikoData.Block memory blk_) {
(TaikoData.BlockV2 memory blk,) = LibUtils.getBlock(state, getConfig(), _blockId);
blk_ = LibData.blockV2toV1(blk);
}

/// @notice Gets the details of a block.
/// @param _blockId Index of the block.
/// @return blk_ The block.
function getBlockV2(uint64 _blockId) external view returns (TaikoData.BlockV2 memory blk_) {
(blk_,) = LibUtils.getBlock(state, getConfig(), _blockId);
}

Expand Down Expand Up @@ -291,7 +299,7 @@ contract TaikoL1 is EssentialContract, ITaikoL1, TaikoEvents {
}

/// @inheritdoc ITaikoL1
function getConfig() public pure virtual override returns (TaikoData.Config memory) {
function getConfig() public pure virtual returns (TaikoData.Config memory) {
return TaikoData.Config({
chainId: LibNetwork.TAIKO_MAINNET,
blockMaxProposals: 324_000, // = 7200 * 45
Expand Down
19 changes: 19 additions & 0 deletions packages/protocol/contracts/L1/libs/LibData.sol
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,23 @@ library LibData {
baseFeeConfig: TaikoData.BaseFeeConfig(0, 0, 0, 0, 0)
});
}

function blockV2toV1(
TaikoData.BlockV2 memory _v2
)
internal
pure
returns (TaikoData.Block memory)
{
return TaikoData.Block({
metaHash: _v2.metaHash,
assignedProver: _v2.assignedProver,
livenessBond: _v2.livenessBond,
blockId: _v2.blockId,
proposedAt: _v2.proposedAt,
proposedIn: _v2.proposedIn,
nextTransitionId: _v2.nextTransitionId,
verifiedTransitionId: _v2.verifiedTransitionId
});
}
}
4 changes: 2 additions & 2 deletions packages/protocol/contracts/L1/libs/LibProposing.sol
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ library LibProposing {
}

// Verify params against the parent block.
TaikoData.Block storage parentBlk =
TaikoData.BlockV2 storage parentBlk =
_state.blocks[(local.b.numBlocks - 1) % _config.blockRingBufferSize];

if (local.postFork) {
Expand Down Expand Up @@ -215,7 +215,7 @@ library LibProposing {
}

// Create the block that will be stored onchain
TaikoData.Block memory blk = TaikoData.Block({
TaikoData.BlockV2 memory blk = TaikoData.BlockV2({
metaHash: local.postFork ? keccak256(abi.encode(meta_)) : keccak256(abi.encode(metaV1_)),
assignedProver: address(0),
livenessBond: local.postFork ? 0 : meta_.livenessBond,
Expand Down
6 changes: 3 additions & 3 deletions packages/protocol/contracts/L1/libs/LibProving.sol
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ library LibProving {
}

local.slot = meta.id % _config.blockRingBufferSize;
TaikoData.Block storage blk = _state.blocks[local.slot];
TaikoData.BlockV2 storage blk = _state.blocks[local.slot];

local.proposedAt = local.postFork ? meta.proposedAt : blk.proposedAt;

Expand Down Expand Up @@ -389,7 +389,7 @@ library LibProving {
/// @dev Handle the transition initialization logic
function _fetchOrCreateTransition(
TaikoData.State storage _state,
TaikoData.Block storage _blk,
TaikoData.BlockV2 storage _blk,
TaikoData.Transition memory _tran,
Local memory _local
)
Expand Down Expand Up @@ -468,7 +468,7 @@ library LibProving {
function _overrideWithHigherProof(
TaikoData.State storage _state,
IAddressResolver _resolver,
TaikoData.Block storage _blk,
TaikoData.BlockV2 storage _blk,
TaikoData.TransitionState memory _ts,
TaikoData.Transition memory _tran,
TaikoData.TierProof memory _proof,
Expand Down
12 changes: 6 additions & 6 deletions packages/protocol/contracts/L1/libs/LibUtils.sol
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ library LibUtils {
_state.slotB.numBlocks = 1;

// Init the genesis block
TaikoData.Block storage blk = _state.blocks[0];
TaikoData.BlockV2 storage blk = _state.blocks[0];
blk.nextTransitionId = 2;
blk.proposedAt = uint64(block.timestamp);
blk.verifiedTransitionId = 1;
Expand Down Expand Up @@ -107,7 +107,7 @@ library LibUtils {
)
internal
view
returns (TaikoData.Block storage blk_, uint64 slot_)
returns (TaikoData.BlockV2 storage blk_, uint64 slot_)
{
slot_ = _blockId % _config.blockRingBufferSize;
blk_ = _state.blocks[slot_];
Expand All @@ -129,7 +129,7 @@ library LibUtils {
view
returns (bytes32 blockHash_, bytes32 stateRoot_, uint64 verifiedAt_)
{
(TaikoData.Block storage blk, uint64 slot) = getBlock(_state, _config, _blockId);
(TaikoData.BlockV2 storage blk, uint64 slot) = getBlock(_state, _config, _blockId);

if (blk.verifiedTransitionId != 0) {
TaikoData.TransitionState storage transition =
Expand Down Expand Up @@ -158,7 +158,7 @@ library LibUtils {
view
returns (TaikoData.TransitionState storage)
{
(TaikoData.Block storage blk, uint64 slot) = getBlock(_state, _config, _blockId);
(TaikoData.BlockV2 storage blk, uint64 slot) = getBlock(_state, _config, _blockId);

if (_tid == 0 || _tid >= blk.nextTransitionId) revert L1_TRANSITION_NOT_FOUND();
return _state.transitions[slot][_tid];
Expand All @@ -181,7 +181,7 @@ library LibUtils {
view
returns (TaikoData.TransitionState storage)
{
(TaikoData.Block storage blk, uint64 slot) = getBlock(_state, _config, _blockId);
(TaikoData.BlockV2 storage blk, uint64 slot) = getBlock(_state, _config, _blockId);

uint24 tid = getTransitionId(_state, blk, slot, _parentHash);
if (tid == 0) revert L1_TRANSITION_NOT_FOUND();
Expand All @@ -193,7 +193,7 @@ library LibUtils {
/// This function will return 0 if the transition is not found.
function getTransitionId(
TaikoData.State storage _state,
TaikoData.Block storage _blk,
TaikoData.BlockV2 storage _blk,
uint64 _slot,
bytes32 _parentHash
)
Expand Down
4 changes: 2 additions & 2 deletions packages/protocol/contracts/L1/libs/LibVerifying.sol
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ library LibVerifying {
local.blockId = local.b.lastVerifiedBlockId;
local.slot = local.blockId % _config.blockRingBufferSize;

TaikoData.Block storage blk = _state.blocks[local.slot];
TaikoData.BlockV2 storage blk = _state.blocks[local.slot];
if (blk.blockId != local.blockId) revert L1_BLOCK_MISMATCH();

local.lastVerifiedTransitionId = blk.verifiedTransitionId;
Expand Down Expand Up @@ -197,7 +197,7 @@ library LibVerifying {
view
returns (address)
{
(TaikoData.Block storage blk,) = LibUtils.getBlock(_state, _config, _blockId);
(TaikoData.BlockV2 storage blk,) = LibUtils.getBlock(_state, _config, _blockId);

uint24 tid = blk.verifiedTransitionId;
if (tid == 0) return address(0);
Expand Down