
Detailed Explanation of the OP Stack Rollup Process and Corresponding Code
TechFlow Selected TechFlow Selected

Detailed Explanation of the OP Stack Rollup Process and Corresponding Code
OP Stack is a standardized, shared, and open-source development stack that powers Optimism and is maintained by the Optimism Collective.
Author: Rayer
Optimism Bedrock is the current version of the OP Stack. The Bedrock release provides tools for launching production-quality Optimistic Rollup blockchains. At this stage, the APIs across different layers of the OP Stack remain tightly coupled with the rollup configuration of the Stack.
The primary rollup operations in the Op-stack are handled by two services:
-
op-batcher: responsible for periodically reading transaction data from the sequencer and rolling it up to on-chain DA (data availability).
-
op-proposer: responsible for rolling up transaction states to the contract.
Rollup Architecture
op-batcher
op-batcher Execution Flowchart

Execution Logic of loadBlocksInfoState
loadBlocksInfoState is responsible for reading all blocks starting from the last read block—that is, those blocks that have not yet been processed.
The overall process is as follows:

Code:



Actions performed by loadBlocksIntoState:
1. Retrieve synchronization status from the sequencer.
2. Line 153: Call the calculateL2BlockRangeToStore function.
-
calculateL2BlockRangeToStore retrieves and determines the start and end block numbers of the latest L2 range to be submitted. The starting block is the current highest safe block on L2, and the ending block is the current highest unsafe block on L2.
3. Line 164: After obtaining the start and end blocks for submission, retrieve block information starting from the initial block, calling the loadBlockIntoState function to fetch the block.
-
loadBlockIntoState checks block information and geth information. If valid, on line 200, calls the
AddL2Blockfunction to add the block to channelManager's blocks []*types.Block.
4. Lines 165–168: Verify whether re-submission is required. If so, set l.lastStoredBlock to eth.BlockID{}; otherwise, on line 173, set l.lastStoredBlock to eth.ToBlockID(block), and latestBlock to block.
5. Line 177: L2BlockToBlockRef extracts basic L2BlockRef information from the L2 block reference source, falling back to genesis information when necessary based on the block number.
Execution Logic of publishStateToL1
publishStateToL1 submits all transactions in the queue to L1 until the queue is empty or an error occurs.
Code:

-
publishStateToL1 loops continuously to send queued transactions to the Layer 1 network.
-
Line 377: Calls
publishTxToL1.

publishTxToL1 handles the logic for submitting a single transaction to L1. The publishTxToL1 method gathers the data to be submitted, constructs a transaction, sends it to the Layer 1 network, and places the sent transaction into the receiptCh chan TxReceipt[T] channel.
-
Line 429:
l1Tip: Retrieves the current L1 tip as an L1BlockRef. Assumes the passed context is a lifecycle context, thus it is internally wrapped with network timeout. -
Line 434:
recordL1Tip: Replaces the previous L1BlockRef with the latest L1BlockRef obtained from l1Tip. -
Line 437:
TxData: Collects transaction data to be rolled up; TxData returns the next tx data to submit to L1. Currently, each transaction uses only one frame. If the pending channel is full, it returns only the remaining frames of that channel until successfully fully sent to L1. If there are no pending frames, it returns io.EOF. -
Line 447:
sendTransactionsends the transaction to Layer 1 and updates the transaction status into the receiptCh chan TxReceipt[T] channel. sendTransaction creates a transaction using the given "data" and submits it to the batch inbox address. It currently uses the underlying "txmgr" for transaction sending and fee management. This is a blocking method and should not be called concurrently.
handleReceipt
handleReceipt retrieves the status of transaction processing from the channel and removes successfully processed transactions from the channel.
Code:
op-proposer
Execution Flowchart
Detailed Execution Process

FetchNextOutputInfo
FetchNextOutputInfo: Retrieves output from blocks on L2 for subsequent packaging and submission. The returned output structure is as follows:
type OutputResponse struct {
Version Bytes32 json:"version"
OutputRoot Bytes32 json:"outputRoot"
BlockRef L2BlockRef json:"blockRef"
WithdrawalStorageRoot common.Hash json:"withdrawalStorageRoot"
StateRoot common.Hash json:"stateRoot"
Status *SyncStatus json:"syncStatus"
}
Code:

-
Line 224: NextBlockNumber: Gets the block range for the next batch submission. The calculation is latestBlockNumber() + SUBMISSION_INTERVAL. The value of SUBMISSION_INTERVAL can be specified during deployment of the L2OutputOracle contract.
-
Line 230: Calls
FetchCurrentBlockNumberto obtain the current block number. -
Lines 236–241: After verifying that nextCheckpointBlock complies with rules, calls
FetchOutputto retrieve the stateRoot from L2 that needs to be submitted.
Code for FetchCurrentBlockNumber:

1. Line 254: SyncStatus: Retrieves the SafeL2 and FinalizedL2 status and block information from L2.
Code for FetchOutput:

2. Line 279: OutputAtBlock: Retrieves output based on block height, including stateRoot. Ultimately calls eth_getProof to compute and retrieve stateRoot. Refer to the diagram above for the code call flow. Note: This does not submit a stateRoot per block but computes a batch of blocks' stateRoot according to the configured SUBMISSION_INTERVAL value, finally submitting the stateRoot to the L2OutputOracle contract.
Send Transaction
-
sendTransaction: Constructs a stateRoot submission transaction using output, then submits the transaction to Layer 1. Below are details of the packed transaction data.
return abi.Pack(
"proposeL2Output",
output.OutputRoot,
new(big.Int).SetUint64(output.BlockRef.Number),
output.Status.CurrentL1.Hash,
new(big.Int).SetUint64(output.Status.CurrentL1.Number)
)
Code:

Join TechFlow official community to stay tuned
Telegram:https://t.me/TechFlowDaily
X (Twitter):https://x.com/TechFlowPost
X (Twitter) EN:https://x.com/BlockFlow_News
















