Skip to content

Commit

Permalink
Fix new wipL2block on finalizeBatch function (0xPolygonHermez#3113)
Browse files Browse the repository at this point in the history
* fix new wipL2block on finalizeBatch function

* fix sustract wipL2Block usedResources when opening new WIPbatch

* fix openNewWipL2Block

* fix openNewWipL2Block

* update prover image to v4.0.0-RC27

* Update prover image to v4.0.0-RC28

* update prover image to v4.0.0-RC27
  • Loading branch information
agnusmor authored Jan 21, 2024
1 parent 3810148 commit 7a25b19
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 20 deletions.
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ services:
zkevm-prover:
container_name: zkevm-prover
restart: unless-stopped
image: hermeznetwork/zkevm-prover:v4.0.0-RC29
image: hermeznetwork/zkevm-prover:v4.0.0-RC27
depends_on:
zkevm-state-db:
condition: service_healthy
Expand Down
20 changes: 14 additions & 6 deletions sequencer/batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,9 @@ func (f *finalizer) finalizeBatch(ctx context.Context) {
metrics.ProcessingTime(time.Since(start))
}()

prevTimestamp := f.wipL2Block.timestamp
prevL1InfoTreeIndex := f.wipL2Block.l1InfoTreeExitRoot.L1InfoTreeIndex

// Close 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.closeWIPL2Block(ctx)
Expand All @@ -130,7 +133,10 @@ func (f *finalizer) finalizeBatch(ctx context.Context) {
f.Halt(ctx, fmt.Errorf("failed to create new WIP batch, error: %v", err))
}

f.openNewWIPL2Block(ctx, nil)
// If we have closed the wipL2Block then we open a new one
if f.wipL2Block == nil {
f.openNewWIPL2Block(ctx, prevTimestamp, &prevL1InfoTreeIndex)
}
}

// 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
Expand Down Expand Up @@ -183,7 +189,7 @@ func (f *finalizer) closeAndOpenNewWIPBatch(ctx context.Context) error {
// Process forced batches
if len(f.nextForcedBatches) > 0 {
lastBatchNumber, stateRoot = f.processForcedBatches(ctx, lastBatchNumber, stateRoot)
// We must init/reset the wip L2 block from the state since processForcedBatches has created new L2 blocks
// We must init/reset the wip L2 block from the state since processForcedBatches can create new L2 blocks
f.initWIPL2Block(ctx)
}

Expand All @@ -192,10 +198,12 @@ func (f *finalizer) closeAndOpenNewWIPBatch(ctx context.Context) error {
return fmt.Errorf("failed to open new wip batch, error: %v", err)
}

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

f.wipBatch = batch
Expand Down
1 change: 1 addition & 0 deletions sequencer/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ type stateInterface interface {
GetForcedBatchParentHash(ctx context.Context, forcedBatchNumber uint64, dbTx pgx.Tx) (common.Hash, error)
GetL1InfoRootLeafByIndex(ctx context.Context, l1InfoTreeIndex uint32, dbTx pgx.Tx) (state.L1InfoTreeExitRootStorageEntry, error)
GetVirtualBatch(ctx context.Context, batchNumber uint64, dbTx pgx.Tx) (*state.VirtualBatch, error)
GetLatestBatchGlobalExitRoot(ctx context.Context, dbTx pgx.Tx) (common.Hash, error)
}

type workerInterface interface {
Expand Down
32 changes: 21 additions & 11 deletions sequencer/l2block.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func (f *finalizer) initWIPL2Block(ctx context.Context) {
log.Fatalf("failed to get last L2 block number, error: %v", err)
}

f.openNewWIPL2Block(ctx, &lastL2Block.ReceivedAt)
f.openNewWIPL2Block(ctx, lastL2Block.ReceivedAt, nil)
}

// addPendingL2BlockToProcess adds a pending L2 block that is closed and ready to be processed by the executor
Expand Down Expand Up @@ -415,9 +415,12 @@ func (f *finalizer) storeL2Block(ctx context.Context, l2Block *L2Block) error {
func (f *finalizer) finalizeWIPL2Block(ctx context.Context) {
log.Debugf("finalizing WIP L2 block")

prevTimestamp := f.wipL2Block.timestamp
prevL1InfoTreeIndex := f.wipL2Block.l1InfoTreeExitRoot.L1InfoTreeIndex

f.closeWIPL2Block(ctx)

f.openNewWIPL2Block(ctx, nil)
f.openNewWIPL2Block(ctx, prevTimestamp, &prevL1InfoTreeIndex)
}

func (f *finalizer) closeWIPL2Block(ctx context.Context) {
Expand All @@ -434,17 +437,15 @@ func (f *finalizer) closeWIPL2Block(ctx context.Context) {
f.wipBatch.countOfL2Blocks++

f.addPendingL2BlockToProcess(ctx, f.wipL2Block)

f.wipL2Block = nil
}

func (f *finalizer) openNewWIPL2Block(ctx context.Context, prevTimestamp *time.Time) {
func (f *finalizer) openNewWIPL2Block(ctx context.Context, prevTimestamp time.Time, prevL1InfoTreeIndex *uint32) {
newL2Block := &L2Block{}

newL2Block.timestamp = now()
if prevTimestamp != nil {
newL2Block.deltaTimestamp = uint32(newL2Block.timestamp.Sub(*prevTimestamp).Truncate(time.Second).Seconds())
} else {
newL2Block.deltaTimestamp = uint32(newL2Block.timestamp.Sub(f.wipL2Block.timestamp).Truncate(time.Second).Seconds())
}
newL2Block.deltaTimestamp = uint32(newL2Block.timestamp.Sub(prevTimestamp).Truncate(time.Second).Seconds())

newL2Block.transactions = []*TxTracker{}

Expand All @@ -453,9 +454,18 @@ func (f *finalizer) openNewWIPL2Block(ctx context.Context, prevTimestamp *time.T
f.lastL1InfoTreeMux.Unlock()

// Check if L1InfoTreeIndex has changed, in this case we need to use this index in the changeL2block instead of zero
// If it's the first wip L2 block after starting sequencer (wipL2Block == nil) then we assume that the L1InfoTreeIndex has changed (there is no problem assuming this)
if f.wipL2Block == nil || newL2Block.l1InfoTreeExitRoot.L1InfoTreeIndex != f.wipL2Block.l1InfoTreeExitRoot.L1InfoTreeIndex {
newL2Block.l1InfoTreeExitRootChanged = true
// If it's the first wip L2 block after starting sequencer (prevL1InfoTreeIndex == nil) then we retrieve the last GER and we check if it's
// different from the GER of the current L1InfoTreeIndex (if the GER is different this means that the index also is different)
if prevL1InfoTreeIndex == nil {
lastGER, err := f.stateIntf.GetLatestBatchGlobalExitRoot(ctx, nil)
if err == nil {
newL2Block.l1InfoTreeExitRootChanged = (newL2Block.l1InfoTreeExitRoot.GlobalExitRoot.GlobalExitRoot != lastGER)
} else {
// If we got an error when getting the latest GER then we consider that the index has not changed and it will be updated the next time we have a new L1InfoTreeIndex
log.Warnf("failed to get the latest CER when initializing the WIP L2 block, assuming L1InfoTreeIndex has not changed, error: %v", err)
}
} else {
newL2Block.l1InfoTreeExitRootChanged = (newL2Block.l1InfoTreeExitRoot.L1InfoTreeIndex != *prevL1InfoTreeIndex)
}

f.wipL2Block = newL2Block
Expand Down
30 changes: 30 additions & 0 deletions sequencer/mock_state.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions test/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -512,7 +512,7 @@ services:

zkevm-prover:
container_name: zkevm-prover
image: hermeznetwork/zkevm-prover:v4.0.0-RC29
image: hermeznetwork/zkevm-prover:v4.0.0-RC27
ports:
- 50061:50061 # MT
- 50071:50071 # Executor
Expand Down Expand Up @@ -601,7 +601,7 @@ services:

zkevm-permissionless-prover:
container_name: zkevm-permissionless-prover
image: hermeznetwork/zkevm-prover:v4.0.0-RC29
image: hermeznetwork/zkevm-prover:v4.0.0-RC27
ports:
# - 50058:50058 # Prover
- 50059:50052 # Mock prover
Expand Down

0 comments on commit 7a25b19

Please sign in to comment.