Skip to content

Commit

Permalink
Fix batch GER (0xPolygonHermez#3104)
Browse files Browse the repository at this point in the history
* fix batch GER

* fix use LER
  • Loading branch information
agnusmor authored Jan 19, 2024
1 parent eb78983 commit 0a11d35
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 25 deletions.
24 changes: 7 additions & 17 deletions sequencer/batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ type Batch struct {
initialStateRoot common.Hash // initial stateRoot of the batch
imStateRoot common.Hash // intermediate stateRoot that is updated each time a single tx is processed
finalStateRoot common.Hash // final stateroot of the batch when a L2 block is processed
localExitRoot common.Hash
countOfTxs int
countOfL2Blocks int
remainingResources state.BatchResources
Expand Down Expand Up @@ -64,7 +63,6 @@ func (f *finalizer) setWIPBatch(ctx context.Context, wipStateBatch *state.Batch)
imStateRoot: wipStateBatch.StateRoot,
initialStateRoot: prevStateBatch.StateRoot,
finalStateRoot: wipStateBatch.StateRoot,
localExitRoot: wipStateBatch.LocalExitRoot,
timestamp: wipStateBatch.Timestamp,
countOfTxs: wipStateBatchCountOfTxs,
remainingResources: remainingResources,
Expand Down Expand Up @@ -100,12 +98,7 @@ func (f *finalizer) initWIPBatch(ctx context.Context) {
f.Halt(ctx, fmt.Errorf("finalizer reached stop sequencer on batch number: %d", f.cfg.HaltOnBatchNumber))
}

// Get las GlobalExitRoot
f.lastL1InfoTreeMux.Lock()
lastGER := f.lastL1InfoTree.GlobalExitRoot.GlobalExitRoot
f.lastL1InfoTreeMux.Unlock()

f.wipBatch, err = f.openNewWIPBatch(ctx, lastStateBatch.BatchNumber+1, lastGER, lastStateBatch.StateRoot, lastStateBatch.LocalExitRoot)
f.wipBatch, err = f.openNewWIPBatch(ctx, lastStateBatch.BatchNumber+1, lastStateBatch.StateRoot)
if err != nil {
log.Fatalf("failed to open new wip batch, error: %v", err)
}
Expand All @@ -116,8 +109,8 @@ func (f *finalizer) initWIPBatch(ctx context.Context) {
}
}

log.Infof("initial batch: %d, initialStateRoot: %s, stateRoot: %s, coinbase: %s, LER: %s",
f.wipBatch.batchNumber, f.wipBatch.initialStateRoot, f.wipBatch.finalStateRoot, f.wipBatch.coinbase, f.wipBatch.localExitRoot)
log.Infof("initial batch: %d, initialStateRoot: %s, stateRoot: %s, coinbase: %s",
f.wipBatch.batchNumber, f.wipBatch.initialStateRoot, f.wipBatch.finalStateRoot, f.wipBatch.coinbase)
}

// finalizeBatch retries until successful closes the current batch and opens a new one, potentially processing forced batches between the batch is closed and the resulting new empty batch
Expand Down Expand Up @@ -192,9 +185,7 @@ func (f *finalizer) closeAndOpenNewWIPBatch(ctx context.Context) error {
f.initWIPL2Block(ctx)
}

currentGER := f.wipL2Block.l1InfoTreeExitRoot.GlobalExitRoot.GlobalExitRoot

batch, err := f.openNewWIPBatch(ctx, lastBatchNumber+1, currentGER, stateRoot, f.wipBatch.localExitRoot)
batch, err := f.openNewWIPBatch(ctx, lastBatchNumber+1, stateRoot)
if err != nil {
return fmt.Errorf("failed to open new wip batch, error: %v", err)
}
Expand All @@ -213,15 +204,15 @@ func (f *finalizer) closeAndOpenNewWIPBatch(ctx context.Context) error {
}

// openNewWIPBatch opens a new batch in the state and returns it as WipBatch
func (f *finalizer) openNewWIPBatch(ctx context.Context, batchNumber uint64, ger, stateRoot, LER common.Hash) (*Batch, error) {
func (f *finalizer) openNewWIPBatch(ctx context.Context, batchNumber uint64, stateRoot common.Hash) (*Batch, error) {
// open next batch
newStateBatch := state.Batch{
BatchNumber: batchNumber,
Coinbase: f.sequencerAddress,
Timestamp: now(),
GlobalExitRoot: ger,
StateRoot: stateRoot,
LocalExitRoot: LER,
GlobalExitRoot: state.ZeroHash,
LocalExitRoot: state.ZeroHash,
}

dbTx, err := f.stateIntf.BeginStateTransaction(ctx)
Expand Down Expand Up @@ -258,7 +249,6 @@ func (f *finalizer) openNewWIPBatch(ctx context.Context, batchNumber uint64, ger
imStateRoot: newStateBatch.StateRoot,
finalStateRoot: newStateBatch.StateRoot,
timestamp: newStateBatch.Timestamp,
localExitRoot: newStateBatch.LocalExitRoot,
remainingResources: getMaxRemainingResources(f.batchConstraints),
closingReason: state.EmptyClosingReason,
}, err
Expand Down
10 changes: 4 additions & 6 deletions sequencer/finalizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -495,11 +495,10 @@ func (f *finalizer) processTransaction(ctx context.Context, tx *TxTracker, first

// Update wip batch
f.wipBatch.imStateRoot = batchResponse.NewStateRoot
f.wipBatch.localExitRoot = batchResponse.NewLocalExitRoot

log.Infof("processed tx %s. Batch.batchNumber: %d, batchNumber: %d, newStateRoot: %s, newLocalExitRoot: %s, oldStateRoot: %s, %s",
log.Infof("processed tx %s. Batch.batchNumber: %d, batchNumber: %d, newStateRoot: %s, oldStateRoot: %s, %s",
tx.HashStr, f.wipBatch.batchNumber, batchRequest.BatchNumber, batchResponse.NewStateRoot.String(),
batchResponse.NewLocalExitRoot.String(), batchRequest.OldStateRoot.String(), f.logZKCounters(batchResponse.UsedZkCounters))
batchRequest.OldStateRoot.String(), f.logZKCounters(batchResponse.UsedZkCounters))

return nil, nil
}
Expand Down Expand Up @@ -646,11 +645,10 @@ func (f *finalizer) processEmptyL2Block(ctx context.Context) error {

// Update wip batch
f.wipBatch.imStateRoot = batchResponse.NewStateRoot
f.wipBatch.localExitRoot = batchResponse.NewLocalExitRoot

log.Infof("processed empty L2 block %d, batch.batchNumber: %d, batchNumber: %d, newStateRoot: %s, newLocalExitRoot: %s, oldStateRoot: %s, %s",
log.Infof("processed empty L2 block %d, batch.batchNumber: %d, batchNumber: %d, newStateRoot: %s, oldStateRoot: %s, %s",
batchResponse.BlockResponses[0].BlockNumber, f.wipBatch.batchNumber, batchRequest.BatchNumber, batchResponse.NewStateRoot.String(),
batchResponse.NewLocalExitRoot.String(), batchRequest.OldStateRoot.String(), f.logZKCounters(batchResponse.UsedZkCounters))
batchRequest.OldStateRoot.String(), f.logZKCounters(batchResponse.UsedZkCounters))

return nil
}
Expand Down
1 change: 0 additions & 1 deletion sequencer/finalizer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2196,7 +2196,6 @@ func setupFinalizer(withWipBatch bool) *finalizer {
coinbase: seqAddr,
initialStateRoot: oldHash,
imStateRoot: newHash,
localExitRoot: newHash,
timestamp: now(),
remainingResources: getMaxRemainingResources(bc),
closingReason: state.EmptyClosingReason,
Expand Down
9 changes: 8 additions & 1 deletion sequencer/l2block.go
Original file line number Diff line number Diff line change
Expand Up @@ -369,12 +369,19 @@ func (f *finalizer) storeL2Block(ctx context.Context, l2Block *L2Block) error {
receipt := state.ProcessingReceipt{
BatchNumber: f.wipBatch.batchNumber,
StateRoot: l2Block.batchResponse.NewStateRoot,
GlobalExitRoot: l2Block.l1InfoTreeExitRoot.GlobalExitRoot.GlobalExitRoot,
LocalExitRoot: l2Block.batchResponse.NewLocalExitRoot,
BatchL2Data: batch.BatchL2Data,
BatchResources: batch.Resources,
}

// We need to update the batch GER only in the GER of the block (response) is not zero, since the final GER stored in the batch
// must be the last GER from the blocks that is not zero (last L1InfoRootIndex change)
if blockResponse.GlobalExitRoot != state.ZeroHash {
receipt.GlobalExitRoot = blockResponse.GlobalExitRoot
} else {
receipt.GlobalExitRoot = batch.GlobalExitRoot
}

err = f.stateIntf.UpdateWIPBatch(ctx, receipt, dbTx)
if err != nil {
return rollbackOnError(fmt.Errorf("error when updating wip batch %d, error: %v", f.wipBatch.batchNumber, err))
Expand Down

0 comments on commit 0a11d35

Please sign in to comment.