|
| 1 | +Transactions flowing through the system reach the smart contract environment after one of two contract call use cases; sequencing coming from the sequencer component, or verifying batches coming from the aggregator component. |
| 2 | + |
| 3 | +This section looks at the sequencing workflow. |
| 4 | + |
| 5 | +## `sequenceBatches(batches, maxSequenceTs, initSequenceBatch, l2Coinbase)` |
| 6 | + |
| 7 | +This function is called on the `PolygonZkEVMEtrog.sol` contract. |
| 8 | + |
| 9 | +The rollup sequencer component calls the [`sequenceBatches`](https://github.com/0xPolygonHermez/zkevm-contracts/blob/1ad7089d04910c319a257ff4f3674ffd6fc6e64e/contracts/v2/lib/PolygonRollupBaseEtrog.sol#L425) function on the [`PolygonZkEVMEtrog.sol`](https://github.com/0xPolygonHermez/zkevm-contracts/blob/1ad7089d04910c319a257ff4f3674ffd6fc6e64e/contracts/v2/consensus/zkEVM/PolygonZkEVMEtrog.sol) contract which inherits the function from [PolygonRollupBaseEtrog.sol](https://github.com/0xPolygonHermez/zkevm-contracts/blob/1ad7089d04910c319a257ff4f3674ffd6fc6e64e/contracts/v2/lib/PolygonRollupBaseEtrog.sol). |
| 10 | + |
| 11 | +The function takes an array of `BatchData` structs from one of the consensus contracts. Each struct contains L2 Ethereum transactions data, and some forced state information. |
| 12 | + |
| 13 | +```solidity |
| 14 | +struct BatchData { |
| 15 | + bytes transactions; |
| 16 | + bytes32 forcedGlobalExitRoot; |
| 17 | + uint64 forcedTimestamp; |
| 18 | + bytes32 forcedBlockHashL1; |
| 19 | +} |
| 20 | +``` |
| 21 | + |
| 22 | +The function validates arguments, checks and organizes the batches, and appends them in the correct sequence while computing an accumulated hash and eventually emits a `SequenceBatch` event which sends a newly sequenced batch of transactions to the `PolygonRollupManager.sol` contract via the [`onSequenceBatches(...)`](#onsequencebatchesnewsequencedbatches-newaccinputhash) function. |
| 23 | + |
| 24 | +Stepwise, the function does the following: |
| 25 | + |
| 26 | +1. Validates arguments. |
| 27 | +1. Tells the bridge to update the global exit root by calling `globalExitRootManager.updateExitRoot(L1LocalExitRoot)` which creates a new global exit root with the newest L1 local exit root. |
| 28 | +1. Gets L1 info root and other variables needed for computation. |
| 29 | +1. Goes through the batches to compute the accumulated hash with `keccak(batch.transaction)` and `keccak(accInputHash, txHash, l1InfoRoot, maxSequenceTs, l2Coinbase, bytes32(0))`. |
| 30 | +1. Stores the accumulated hash. |
| 31 | +1. Caller pays the rollup manager in POL. |
| 32 | +1. Calls the `PolygonRollupManager.onSequenceBatches(...)` function which waits for an `OnSequenceBatches(...)` event callback. |
| 33 | +1. Emits `SequenceBatches(...)` event. |
| 34 | + |
| 35 | +## `onSequenceBatches(newSequencedBatches, newAccInputHash)` |
| 36 | + |
| 37 | +This function is called on the `PolygonRollupManager.sol` contract. |
| 38 | + |
| 39 | +It takes the sequenced batches and the accumulated hash from the caller, adds the batches to the correct stack, and updates the batch count. |
| 40 | + |
| 41 | +Stepwise, the function does the following: |
| 42 | + |
| 43 | +1. Validates the arguments and the caller contract. |
| 44 | +1. Updates the `totalSequencedBatches` storage variable. |
| 45 | +1. Updates the `lastBatchSequenced` and adds a new `SequencedBatchData` struct for the rollup that called `sequenceBatches`. |
| 46 | +1. Attempts to consolidate pending state for the rollup by updating `lastVerifiedBatch`, `batchNumToStateRoot[]`, and `lastLocalExitRoot` state variables, and also by updating `globalExitRootManager.updateExitRoot(L2sLocalExitRoot)`, after which it emits a `ConsolidatePendingState(...)` event. |
| 47 | +1. Emits an `OnSequenceBatches(...)` event back to the original `sequenceBatches(...)` call. |
| 48 | + |
| 49 | +## `sequenceBatchesValidium(...)` |
| 50 | + |
| 51 | +This function is called on the `PolygonValidiumEtrog.sol` contract. |
| 52 | + |
| 53 | +The sequencing logic is nearly the same as for the rollup `sequenceBatches(...)` function except the function takes a `ValidiumBatchData[]` array instead of `BatchData[]` which means instead of passing the actual batch data, the struct passes the hashes of the transactions. |
| 54 | + |
| 55 | +```solidity |
| 56 | +struct ValidiumBatchData { |
| 57 | + bytes32 transactionsHash; |
| 58 | + bytes32 forcedGlobalExitRoot; |
| 59 | + uint64 forcedTimestamp; |
| 60 | + bytes32 forcedBlockHashL1; |
| 61 | +} |
| 62 | +``` |
| 63 | + |
| 64 | +Stepwise, the function has identical logic to the `PolygonRollupBaseEtrog.sequenceBatches(...)` function except for: |
| 65 | + |
| 66 | +1. `ValdiumBatchData` instead of `BatchData`. |
| 67 | +1. Accumulates the txHash into `accumulatedNonForcedTransactionHash`. |
| 68 | +1. Adds a validity check with `dataAvailabilityProtocol.verifyMessage(accumulatedNonForcedTransactionHash, dataAvailabilityMessage)`. |
0 commit comments