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): support smaller more frequent L2 blocks (replacing soft blocks) #18743

Draft
wants to merge 18 commits into
base: pacaya_fork
Choose a base branch
from
173 changes: 89 additions & 84 deletions packages/protocol/contracts/layer1/based/ITaikoInbox.sol
Original file line number Diff line number Diff line change
Expand Up @@ -17,76 +17,80 @@ import "src/shared/based/LibSharedData.sol";
/// @dev Registered in the address resolver as "taiko".
/// @custom:security-contact security@taiko.xyz
interface ITaikoInbox {
struct BlockParamsV3 {
struct BlockParams {
uint16 numTransactions;
uint8 timeThift;
}

struct BatchParams {
bytes32 parentMetaHash;
uint64 anchorBlockId;
bytes32 anchorInput;
uint64 timestamp;
uint32 txListOffset;
uint32 txListSize;
uint8 blobIndex;
uint8[] blobIndices;
bytes32[] signalSlots;
BlockParams[] blocks;
}

struct BlockMetadataV3 {
bytes32 difficulty;
struct BatchMetadata {
bytes32 txListHash;
bytes32 extraData;
address coinbase;
uint64 blockId;
uint64 batchId;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this design, when we propose blocks on L2, do we propose the batch or the blocks in the batch?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We propose a batch. We move holistically across the stack from block based to batch based in this design, from proposing to proving.

uint32 gasLimit;
uint64 timestamp;
bytes32 parentMetaHash;
address proposer;
uint96 livenessBond;
uint64 proposedAt; // Used by node/client post block proposal.
uint64 proposedIn; // Used by node/client post block proposal.
uint64 proposedAt; // Used by node/client
uint64 proposedIn; // Used by node/client
uint32 txListOffset;
uint32 txListSize;
uint8 blobIndex;
uint8[] blobIndices;
uint64 anchorBlockId;
bytes32 anchorBlockHash;
bytes32[] signalSlots;
bytes blocks;
bytes32 anchorInput;
LibSharedData.BaseFeeConfig baseFeeConfig;
}

/// @notice Struct representing transition to be proven.
struct TransitionV3 {
struct Transition {
bytes32 parentHash;
bytes32 blockHash;
bytes32 stateRoot;
}

/// @notice 3 slots used.
struct BlockV3 {
struct Batch {
bytes32 metaHash; // slot 1
address _reserved2;
uint96 _reserved3;
uint64 blockId; // slot 3
uint64 batchId; // slot 3
uint64 timestamp;
uint64 anchorBlockId;
uint24 nextTransitionId;
bool _reserved1;
// 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.
uint8 numSubBlocks;
// The ID of the transaction that is used to verify this batch. However, if this batch is
// not verified as the last one in a transaction, verifiedTransitionId will remain zero.
uint24 verifiedTransitionId;
}

/// @notice Forge is only able to run coverage in case the contracts by default capable of
/// compiling without any optimization (neither optimizer runs, no compiling --via-ir flag).
/// @notice In order to resolve stack too deep without optimizations, we needed to introduce
/// outsourcing vars into structs below.
struct Stats1 {
uint64 __reserved1;
uint64 __reserved2;
uint64 lastSyncedBlockId;
uint64 lastSyncedBatch;
uint64 lastSyncedAt;
}

struct Stats2 {
uint64 numBlocks;
uint64 lastVerifiedBlockId;
uint64 numBatches;
uint64 lastVerifiedBatch;
bool paused;
uint56 lastProposedIn;
uint64 lastUnpausedAt;
Expand All @@ -98,20 +102,20 @@ interface ITaikoInbox {
}

/// @notice Struct holding Taiko configuration parameters. See {TaikoConfig}.
struct ConfigV3 {
struct Config {
/// @notice The chain ID of the network where Taiko contracts are deployed.
uint64 chainId;
/// @notice The maximum number of verifications allowed when a block is proposed or proved.
uint64 blockMaxProposals;
/// @notice Size of the block ring buffer, allowing extra space for proposals.
uint64 blockRingBufferSize;
/// @notice The maximum number of verifications allowed when a block is proposed or proved.
uint64 maxBlocksToVerify;
/// @notice The maximum number of verifications allowed when a batch is proposed or proved.
uint64 maxBatchProposals;
/// @notice Size of the batch ring buffer, allowing extra space for proposals.
uint64 batchRingBufferSize;
/// @notice The maximum number of verifications allowed when a batch is proposed or proved.
uint64 maxBatchesToVerify;
/// @notice The maximum gas limit allowed for a block.
uint32 blockMaxGasLimit;
/// @notice The amount of Taiko token as a prover liveness bond.
uint96 livenessBond;
/// @notice The number of L2 blocks between each L2-to-L1 state root sync.
/// @notice The number of batches between two L2-to-L1 state root sync.
uint8 stateRootSyncInternal;
/// @notice The max differences of the anchor height and the current block number.
uint64 maxAnchorHeightOffset;
Expand All @@ -121,20 +125,21 @@ interface ITaikoInbox {
uint16 provingWindow;
/// @notice The maximum number of signals to be received by TaikoL2.
uint8 maxSignalsToReceive;
/// @notice The maximum number of blocks per batch.
uint16 maxBlocksPerBatch;
/// @notice Historical heights of the forks.
ForkHeights forkHeights;
}

/// @notice Struct holding the state variables for the {Taiko} contract.
struct State {
// Ring buffer for proposed blocks and a some recent verified blocks.
mapping(uint256 blockId_mod_blockRingBufferSize => BlockV3 blk) blocks;
// Ring buffer for proposed batches and a some recent verified batches.
mapping(uint256 batchId_mod_batchRingBufferSize => Batch batch) batches;
// Indexing to transition ids (ring buffer not possible)
mapping(uint256 blockId => mapping(bytes32 parentHash => uint24 transitionId)) transitionIds;
mapping(uint256 batchId => mapping(bytes32 parentHash => uint24 transitionId)) transitionIds;
// Ring buffer for transitions
mapping(
uint256 blockId_mod_blockRingBufferSize
=> mapping(uint24 transitionId => TransitionV3 ts)
uint256 batchId_mod_batchRingBufferSize => mapping(uint24 transitionId => Transition ts)
) transitions;
bytes32 __reserve1; // Used as a ring buffer for Ether deposits
Stats1 stats1; // slot 5
Expand Down Expand Up @@ -163,56 +168,56 @@ interface ITaikoInbox {
/// @param amount The amount of tokens debited.
event BondDebited(address indexed user, uint256 amount);

/// @notice Emitted when a block is synced.
/// @notice Emitted when a batch is synced.
/// @param stats1 The Stats1 data structure.
event Stats1Updated(Stats1 stats1);

/// @notice Emitted when some state variable values changed.
/// @param stats2 The Stats2 data structure.
event Stats2Updated(Stats2 stats2);

/// @notice Emitted when multiple blocks are proposed.
/// @param metas The metadata of the proposed blocks.
/// @notice Emitted when a batch is proposed.
/// @param meta The metadata of the proposed batch.
/// @param calldataUsed Whether calldata is used for txList DA.
/// @param txListInCalldata The tx list in calldata.
event BlocksProposedV3(BlockMetadataV3[] metas, bool calldataUsed, bytes txListInCalldata);
event BatchProposed(BatchMetadata meta, bool calldataUsed, bytes txListInCalldata);

/// @notice Emitted when multiple transitions are proved.
/// @param verifier The address of the verifier.
/// @param transitions The transitions data.
event BlocksProvedV3(address verifier, uint64[] blockIds, TransitionV3[] transitions);
event BatchesProved(address verifier, uint64[] batchIds, Transition[] transitions);

/// @notice Emitted when a transition is overwritten by another one.
/// @param blockId The block ID.
/// @param batchId The batch ID.
/// @param tran The transition data that has been overwritten.
event TransitionOverwrittenV3(uint64 blockId, TransitionV3 tran);
event TransitionOverwritten(uint64 batchId, Transition tran);

/// @notice Emitted when a block is verified.
/// @param blockId The ID of the verified block.
/// @param blockHash The hash of the verified block.
event BlockVerifiedV3(uint64 blockId, bytes32 blockHash);
/// @notice Emitted when a batch is verified.
/// @param batchId The ID of the verified batch.
/// @param blockHash The hash of the verified batch.
event BatchesVerified(uint64 batchId, bytes32 blockHash);

error AnchorBlockIdSmallerThanParent();
error AnchorBlockIdTooSmall();
error AnchorBlockIdTooLarge();
error ArraySizesMismatch();
error BatchNotFound();
error BatchVerified();
error BlobIndexZero();
error BlobNotFound();
error BlockNotFound();
error BlockVerified();
error ContractPaused();
error CustomProposerMissing();
error CustomProposerNotAllowed();
error EtherNotPaidAsBond();
error InsufficientBond();
error InvalidBlockParams();
error InvalidForkHeight();
error InvalidGenesisBlockHash();
error InvalidTransitionBlockHash();
error InvalidTransitionParentHash();
error InvalidTransitionStateRoot();
error MetaHashMismatch();
error MsgValueNotZero();
error NoBlocksToPropose();
error NoBlocksToProve();
error NotPreconfRouter();
error ParentMetaHashMismatch();
Expand All @@ -221,35 +226,35 @@ interface ITaikoInbox {
error TimestampSmallerThanParent();
error TimestampTooLarge();
error TimestampTooSmall();
error TooManyBlocks();
error TooManyBatches();
error TooManySignals();
error TransitionNotFound();

/// @notice Proposes multiple blocks with specified parameters and transaction list.
/// @notice Proposes a batch of blocks.
/// @param _proposer The address of the proposer, which is set by the PreconfTaskManager if
/// enabled; otherwise, it must be address(0).
/// @param _coinbase The address that will receive the block rewards; defaults to the proposer's
/// address if set to address(0).
/// @param _blockParams An array containing the parameters for each block being proposed.
/// @param _batchParams Batch parameters.
/// @param _txList The transaction list in calldata. If the txList is empty, blob will be used
/// for data availability.
/// @return An array of block metadata for each block proposed.
function proposeBlocksV3(
/// @return Batch metadata.
function proposeBatch(
address _proposer,
address _coinbase,
BlockParamsV3[] calldata _blockParams,
BatchParams calldata _batchParams,
bytes calldata _txList
)
external
returns (BlockMetadataV3[] memory);

/// @notice Proves state transitions for multiple blocks with a single aggregated proof.
/// @param _metas Array of metadata for each block being proved.
/// @param _transitions Array of block transitions to be proved.
/// @param proof The aggregated cryptographic proof proving the blocks transitions.
function proveBlocksV3(
BlockMetadataV3[] calldata _metas,
TransitionV3[] calldata _transitions,
returns (BatchMetadata memory);

/// @notice Proves state transitions for multiple batches with a single aggregated proof.
/// @param _metas Array of metadata for each batch being proved.
/// @param _transitions Array of batch transitions to be proved.
/// @param proof The aggregated cryptographic proof proving the batches transitions.
function proveBatches(
BatchMetadata[] calldata _metas,
Transition[] calldata _transitions,
bytes calldata proof
)
external;
Expand Down Expand Up @@ -280,49 +285,49 @@ interface ITaikoInbox {
/// @return Stats2 structure containing the statistics.
function getStats2() external view returns (Stats2 memory);

/// @notice Retrieves data about a specific block.
/// @param _blockId The ID of the block to retrieve.
/// @return blk_ The block data.
function getBlockV3(uint64 _blockId) external view returns (BlockV3 memory blk_);
/// @notice Retrieves data about a specific batch.
/// @param _batchId The ID of the batch to retrieve.
/// @return batch_ The batch data.
function getBatch(uint64 _batchId) external view returns (Batch memory batch_);

/// @notice Retrieves a specific transition by block ID and transition ID. This function may
/// @notice Retrieves a specific transition by batch ID and transition ID. This function may
/// revert if the transition is not found.
/// @param _blockId The block ID.
/// @param _batchId The batch ID.
/// @param _tid The transition ID.
/// @return The specified transition.
function getTransitionV3(
uint64 _blockId,
function getTransition(
uint64 _batchId,
uint24 _tid
)
external
view
returns (ITaikoInbox.TransitionV3 memory);
returns (ITaikoInbox.Transition memory);

/// @notice Retrieves the transition used for the last verified block.
/// @return blockId_ The block ID of the last verified transition.
/// @notice Retrieves the transition used for the last verified batch.
/// @return batchId_ The batch ID of the last verified transition.
/// @return tran_ The last verified transition.
function getLastVerifiedTransitionV3()
function getLastVerifiedTransition()
external
view
returns (uint64 blockId_, TransitionV3 memory tran_);
returns (uint64 batchId_, Transition memory tran_);

/// @notice Retrieves the transition used for the last synced block.
/// @return blockId_ The block ID of the last synced transition.
/// @notice Retrieves the transition used for the last synced batch.
/// @return batchId_ The batch ID of the last synced transition.
/// @return tran_ The last synced transition.
function getLastSyncedTransitionV3()
function getLastSyncedTransition()
external
view
returns (uint64 blockId_, TransitionV3 memory tran_);
returns (uint64 batchId_, Transition memory tran_);

/// @notice Retrieves the transition used for verifying a block.
/// @param _blockId The block ID.
/// @return The transition used for verifying the block.
function getBlockVerifyingTransition(uint64 _blockId)
/// @notice Retrieves the transition used for verifying a batch.
/// @param _batchId The batch ID.
/// @return The transition used for verifying the batch.
function getBatchVerifyingTransition(uint64 _batchId)
external
view
returns (TransitionV3 memory);
returns (Transition memory);

/// @notice Retrieves the current protocol configuration.
/// @return The current configuration.
function getConfigV3() external view returns (ConfigV3 memory);
function getConfig() external view returns (Config memory);
}
Loading