Skip to content

Commit

Permalink
fix finalize l2block when closing batch due to exhausted resource (0x…
Browse files Browse the repository at this point in the history
  • Loading branch information
agnusmor authored Jan 16, 2024
1 parent b0edcf3 commit 3e40964
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 27 deletions.
33 changes: 17 additions & 16 deletions sequencer/batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,22 +127,19 @@ func (f *finalizer) finalizeBatch(ctx context.Context) {
metrics.ProcessingTime(time.Since(start))
}()

var err error
f.wipBatch, err = f.closeAndOpenNewWIPBatch(ctx)
// Finalize the wip L2 block if it has transactions, if not we keep it open to store it in the new wip batch
if !f.wipL2Block.isEmpty() {
f.finalizeL2Block(ctx)
}

err := f.closeAndOpenNewWIPBatch(ctx)
if err != nil {
f.Halt(ctx, fmt.Errorf("failed to create new WIP batch, error: %v", err))
}

log.Infof("new WIP batch %d", f.wipBatch.batchNumber)
}

// closeAndOpenNewWIPBatch closes the current batch and opens a new one, potentially processing forced batches between the batch is closed and the resulting new empty batch
func (f *finalizer) closeAndOpenNewWIPBatch(ctx context.Context) (*Batch, error) {
// Finalize the wip L2 block if it has transactions, if not we keep it open to store it in the new wip batch
if !f.wipL2Block.isEmpty() {
f.finalizeL2Block(ctx)
}

func (f *finalizer) closeAndOpenNewWIPBatch(ctx context.Context) error {
// Wait until all L2 blocks are processed by the executor
startWait := time.Now()
f.pendingL2BlocksToProcessWG.Wait()
Expand All @@ -163,7 +160,7 @@ func (f *finalizer) closeAndOpenNewWIPBatch(ctx context.Context) (*Batch, error)
log.Info("reprocessing batch because the state root has not changed...")
_, err = f.processTransaction(ctx, nil, true)
if err != nil {
return nil, err
return err
}
}

Expand All @@ -173,7 +170,7 @@ func (f *finalizer) closeAndOpenNewWIPBatch(ctx context.Context) (*Batch, error)
_, err := f.batchSanityCheck(ctx, f.wipBatch.batchNumber, f.wipBatch.initialStateRoot, f.wipBatch.finalStateRoot)
if err != nil {
// There is an error reprocessing the batch. We halt the execution of the Sequencer at this point
return nil, fmt.Errorf("halting sequencer because of error reprocessing full batch %d (sanity check), error: %v ", f.wipBatch.batchNumber, err)
return fmt.Errorf("halting sequencer because of error reprocessing full batch %d (sanity check), error: %v ", f.wipBatch.batchNumber, err)
}
} else {
// Do the full batch reprocess in parallel
Expand All @@ -185,7 +182,7 @@ func (f *finalizer) closeAndOpenNewWIPBatch(ctx context.Context) (*Batch, error)
// Close the wip batch
err = f.closeWIPBatch(ctx)
if err != nil {
return nil, fmt.Errorf("failed to close batch, error: %v", err)
return fmt.Errorf("failed to close batch, error: %v", err)
}

log.Infof("batch %d closed", f.wipBatch.batchNumber)
Expand All @@ -209,16 +206,20 @@ func (f *finalizer) closeAndOpenNewWIPBatch(ctx context.Context) (*Batch, error)

batch, err := f.openNewWIPBatch(ctx, lastBatchNumber+1, currentGER, stateRoot, f.wipBatch.localExitRoot)
if err != nil {
return nil, fmt.Errorf("failed to open new wip batch, error: %v", err)
return fmt.Errorf("failed to open new wip batch, error: %v", err)
}

// Subtract the L2 block used resources to batch
err = batch.remainingResources.Sub(l2BlockUsedResources)
if err != nil {
return nil, fmt.Errorf("failed to subtract L2 block used resources to new wip batch %d, error: %v", batch.batchNumber, err)
return fmt.Errorf("failed to subtract L2 block used resources to new wip batch %d, error: %v", batch.batchNumber, err)
}

return batch, nil
f.wipBatch = batch

log.Infof("new WIP batch %d", f.wipBatch.batchNumber)

return nil
}

// openNewWIPBatch opens a new batch in the state and returns it as WipBatch
Expand Down
17 changes: 10 additions & 7 deletions sequencer/l2block.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,9 +212,9 @@ func (f *finalizer) storePendingL2Blocks(ctx context.Context) {

// processL2Block process (executor) a L2 Block and adds it to the pendingL2BlocksToStore channel. It returns the response block from the executor
func (f *finalizer) processL2Block(ctx context.Context, l2Block *L2Block) (*state.ProcessBatchResponse, error) {
processL2BLockError := func() {
processL2BLockError := func(err error) {
log.Errorf("process L2 block error %v, batch: %d, initialStateRoot: %s", err, f.wipBatch.batchNumber, l2Block.initialStateRoot.String())
// Log batch detailed info
log.Infof("process L2 block: batch: %d, initialStateRoot: %s", f.wipBatch.batchNumber, l2Block.initialStateRoot.String())
for i, tx := range l2Block.transactions {
log.Infof("batch: %d, tx position %d, tx hash: %s", f.wipBatch.batchNumber, i, tx.HashStr)
}
Expand Down Expand Up @@ -267,17 +267,17 @@ func (f *finalizer) processL2Block(ctx context.Context, l2Block *L2Block) (*stat
result, err = f.stateIntf.ProcessBatchV2(ctx, executorBatchRequest, true)

if err != nil {
processL2BLockError()
processL2BLockError(err)
return nil, err
}

if result.ExecutorError != nil {
processL2BLockError()
processL2BLockError(err)
return nil, ErrExecutorError
}

if result.IsRomOOCError {
processL2BLockError()
processL2BLockError(err)
return nil, ErrProcessBatchOOC
}

Expand Down Expand Up @@ -403,9 +403,12 @@ func (f *finalizer) closeWIPL2Block(ctx context.Context) {
func (f *finalizer) openNewWIPL2Block(ctx context.Context, prevTimestamp *time.Time) {
err := f.wipBatch.remainingResources.Sub(l2BlockUsedResources)

// we finalize the wip batch if we got an error when subtracting the l2BlockUsedResources or we have exhausted some resources of the batch
// we close the wip batch and open a new one if we got an error when subtracting the l2BlockUsedResources or we have exhausted some resources of the batch
if err != nil || f.isBatchResourcesExhausted() {
f.finalizeBatch(ctx)
err := f.closeAndOpenNewWIPBatch(ctx)
if err != nil {
f.Halt(ctx, fmt.Errorf("failed to create new WIP batch, error: %v", err))
}
}

// Initialize wipL2Block to a new L2 block
Expand Down
8 changes: 4 additions & 4 deletions state/runtime/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,10 @@ var (
ErrInvalidDecodeChangeL2Block = errors.New("error while decoding a change l2 block transaction")
// ErrInvalidNotFirstTxChangeL2Block indicates that there has been an error while decoding a create l2 block transaction
ErrInvalidNotFirstTxChangeL2Block = errors.New("the first transaction in a batch is not a change l2 block transaction")
// ErrInvalidTxChangeL2BlockLimitTimestamp indicates that the change l2 block transaction has trigger an error during while executing
ErrInvalidTxChangeL2BlockLimitTimestamp = errors.New("the change l2 block transaction has trigger an error during while executing (limit timestamp)")
// ErrInvalidTxChangeL2BlockMinTimestamp indicates that the change l2 block transaction has trigger an error during while executing
ErrInvalidTxChangeL2BlockMinTimestamp = errors.New("indicates that the change l2 block transaction has trigger an error during while executing (min timestamp)")
// ErrInvalidTxChangeL2BlockLimitTimestamp indicates that the change l2 block transaction has trigger an error while executing
ErrInvalidTxChangeL2BlockLimitTimestamp = errors.New("the change l2 block transaction has trigger an error while executing (limit timestamp)")
// ErrInvalidTxChangeL2BlockMinTimestamp indicates that the change l2 block transaction has trigger an error while executing
ErrInvalidTxChangeL2BlockMinTimestamp = errors.New("indicates that the change l2 block transaction has trigger an error while executing (min timestamp)")

// EXECUTOR ERRORS
// ===============
Expand Down

0 comments on commit 3e40964

Please sign in to comment.