Skip to content

Commit

Permalink
fix wipL2Block stateRoot (0xPolygonHermez#3145)
Browse files Browse the repository at this point in the history
  • Loading branch information
agnusmor authored Jan 25, 2024
1 parent 646603e commit 4ddba20
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 33 deletions.
1 change: 1 addition & 0 deletions sequencer/batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ func (f *finalizer) closeAndOpenNewWIPBatch(ctx context.Context, closeReason sta
}

if f.wipL2Block != nil {
f.wipBatch.imStateRoot = f.wipL2Block.imStateRoot
// Subtract the WIP L2 block used resources to batch
overflow, overflowResource := batch.imRemainingResources.Sub(f.wipL2Block.usedResources)
if overflow {
Expand Down
20 changes: 10 additions & 10 deletions sequencer/finalizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -479,16 +479,6 @@ func (f *finalizer) handleProcessTransactionResponse(ctx context.Context, tx *Tx
return errWg, result.BlockResponses[0].TransactionResponses[0].RomError
}

// Check remaining resources
overflow, overflowResource := f.wipBatch.imRemainingResources.Sub(state.BatchResources{ZKCounters: result.UsedZkCounters, Bytes: uint64(len(tx.RawTx))})
if overflow {
log.Infof("current tx %s exceeds the remaining batch resources, overflow resource: %s, updating metadata for tx in worker and continuing", tx.HashStr, overflowResource)
start := time.Now()
f.workerIntf.UpdateTxZKCounters(result.BlockResponses[0].TransactionResponses[0].TxHash, tx.From, result.UsedZkCounters)
metrics.WorkerProcessingTime(time.Since(start))
return nil, err
}

egpEnabled := f.effectiveGasPrice.IsEnabled()

if !tx.IsLastExecution {
Expand Down Expand Up @@ -531,6 +521,16 @@ func (f *finalizer) handleProcessTransactionResponse(ctx context.Context, tx *Tx
}
}

// Check remaining resources
overflow, overflowResource := f.wipBatch.imRemainingResources.Sub(state.BatchResources{ZKCounters: result.UsedZkCounters, Bytes: uint64(len(tx.RawTx))})
if overflow {
log.Infof("current tx %s exceeds the remaining batch resources, overflow resource: %s, updating metadata for tx in worker and continuing", tx.HashStr, overflowResource)
start := time.Now()
f.workerIntf.UpdateTxZKCounters(result.BlockResponses[0].TransactionResponses[0].TxHash, tx.From, result.UsedZkCounters)
metrics.WorkerProcessingTime(time.Since(start))
return nil, err
}

// Save Enabled, GasPriceOC, BalanceOC and final effective gas price for later logging
tx.EGPLog.Enabled = egpEnabled
tx.EGPLog.GasPriceOC = result.BlockResponses[0].TransactionResponses[0].HasGaspriceOpcode
Expand Down
8 changes: 5 additions & 3 deletions sequencer/l2block.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type L2Block struct {
timestamp uint64
deltaTimestamp uint32
initialStateRoot common.Hash
imStateRoot common.Hash
l1InfoTreeExitRoot state.L1InfoTreeExitRootStorageEntry
l1InfoTreeExitRootChanged bool
usedResources state.BatchResources
Expand Down Expand Up @@ -402,7 +403,7 @@ func (f *finalizer) storeL2Block(ctx context.Context, l2Block *L2Block) error {

endStoring := time.Now()

log.Infof("stored L2 block: %d [%d], batch: %d, deltaTimestamp: %d, timestamp: %d, l1InfoTreeIndex: %d, l1InfoTreeIndexChanged: %v, txs: %d/%d, blockHash: %s, infoRoot: %s, time: %v",
log.Infof("stored L2 block %d [%d], batch: %d, deltaTimestamp: %d, timestamp: %d, l1InfoTreeIndex: %d, l1InfoTreeIndexChanged: %v, txs: %d/%d, blockHash: %s, infoRoot: %s, time: %v",
blockResponse.BlockNumber, l2Block.trackingNum, f.wipBatch.batchNumber, l2Block.deltaTimestamp, l2Block.timestamp, l2Block.l1InfoTreeExitRoot.L1InfoTreeIndex,
l2Block.l1InfoTreeExitRootChanged, len(l2Block.transactions), len(blockResponse.TransactionResponses), blockResponse.BlockHash, blockResponse.BlockInfoRoot.String(), endStoring.Sub(startStoring))

Expand Down Expand Up @@ -488,8 +489,9 @@ func (f *finalizer) openNewWIPL2Block(ctx context.Context, prevTimestamp uint64,
f.Halt(ctx, fmt.Errorf("number of L2 block [%d] responses returned by the executor is %d and must be 1", f.wipL2Block.trackingNum, len(batchResponse.BlockResponses)))
}

// Update imStateRoot and wip L2 block number
f.wipBatch.imStateRoot = batchResponse.NewStateRoot
// Update imStateRoot
f.wipL2Block.imStateRoot = batchResponse.NewStateRoot
f.wipBatch.imStateRoot = f.wipL2Block.imStateRoot

// Save and sustract the resources used by the new WIP L2 block from the wip batch
// We need to increase the poseidon hashes to reserve in the batch the hashes needed to write the L1InfoRoot when processing the final L2 Block (SkipWriteBlockInfoRoot_V2=false)
Expand Down
2 changes: 1 addition & 1 deletion state/batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ const (
// MaxDeltaTimestampClosingReason is the closing reason used when max delta batch timestamp is reached
MaxDeltaTimestampClosingReason ClosingReason = "Max delta timestamp"
// NoTxFitsClosingReason is the closing reason used when any of the txs in the pool (worker) fits in the remaining resources of the batch
NoTxFitsClosingReason ClosingReason = "No transactions fits"
NoTxFitsClosingReason ClosingReason = "No transaction fits"
)

// ProcessingReceipt indicates the outcome (StateRoot, AccInputHash) of processing a batch
Expand Down
38 changes: 19 additions & 19 deletions state/batchV2.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ func (s *State) sendBatchRequestToExecutorV2(ctx context.Context, batchRequest *
return nil, ErrExecutorNil
}

batchRequestLog := "BatchNum: %v, OldBatchNum: %v, From: %v, OldStateRoot: %v, L1InfoRoot: %v, OldAccInputHash: %v, TimestampLimit: %v, Coinbase: %v, UpdateMerkleTree: %v, SkipFirstChangeL2Block: %v, SkipWriteBlockInfoRoot: %v, ChainId: %v, ForkId: %v, ContextId: %v, SkipVerifyL1InfoRoot: %v, ForcedBlockhashL1: %v, L1InfoTreeData: %+v, BatchL2Data: %v"
batchRequestLog := "OldBatchNum: %v, From: %v, OldStateRoot: %v, L1InfoRoot: %v, OldAccInputHash: %v, TimestampLimit: %v, Coinbase: %v, UpdateMerkleTree: %v, SkipFirstChangeL2Block: %v, SkipWriteBlockInfoRoot: %v, ChainId: %v, ForkId: %v, ContextId: %v, SkipVerifyL1InfoRoot: %v, ForcedBlockhashL1: %v, L1InfoTreeData: %+v, BatchL2Data: %v"

l1DataStr := ""
for i, l1Data := range batchRequest.L1InfoTreeData {
Expand All @@ -272,52 +272,52 @@ func (s *State) sendBatchRequestToExecutorV2(ctx context.Context, batchRequest *
l1DataStr = l1DataStr[:len(l1DataStr)-1]
}

batchRequestLog = fmt.Sprintf(batchRequestLog, batchRequest.OldBatchNum+1, batchRequest.OldBatchNum, batchRequest.From, hex.EncodeToHex(batchRequest.OldStateRoot), hex.EncodeToHex(batchRequest.L1InfoRoot),
batchRequestLog = fmt.Sprintf(batchRequestLog, batchRequest.OldBatchNum, batchRequest.From, hex.EncodeToHex(batchRequest.OldStateRoot), hex.EncodeToHex(batchRequest.L1InfoRoot),
hex.EncodeToHex(batchRequest.OldAccInputHash), batchRequest.TimestampLimit, batchRequest.Coinbase, batchRequest.UpdateMerkleTree, batchRequest.SkipFirstChangeL2Block,
batchRequest.SkipWriteBlockInfoRoot, batchRequest.ChainId, batchRequest.ForkId, batchRequest.ContextId, batchRequest.SkipVerifyL1InfoRoot, hex.EncodeToHex(batchRequest.ForcedBlockhashL1),
l1DataStr, hex.EncodeToHex(batchRequest.BatchL2Data))

log.Debugf("executor batchRequest, %s", batchRequestLog)
newBatchNum := batchRequest.OldBatchNum + 1
log.Debugf("executor batch %d request, %s", newBatchNum, batchRequestLog)

now := time.Now()
batchResponse, err := s.executorClient.ProcessBatchV2(ctx, batchRequest)
elapsed := time.Since(now)

//workarroundDuplicatedBlock(res)
if caller != metrics.DiscardCallerLabel {
metrics.ExecutorProcessingTime(string(caller), elapsed)
}

if err != nil {
log.Errorf("error executor ProcessBatchV2: %v", err)
log.Errorf("error executor ProcessBatchV2: %s", err.Error())
log.Errorf("error executor ProcessBatchV2 response: %v", batchResponse)
} else {
batchResponseToString := processBatchResponseToString(batchResponse)
batchResponseToString := processBatchResponseToString(newBatchNum, batchResponse, elapsed)
if batchResponse.Error != executor.ExecutorError_EXECUTOR_ERROR_NO_ERROR {
err = executor.ExecutorErr(batchResponse.Error)
log.Debugf("executor batchResponse, executor error: %v", err)
log.Debug(batchResponseToString)
log.Warnf("executor batch %d response, executor error: %v", newBatchNum, err)
log.Warn(batchResponseToString)
s.eventLog.LogExecutorErrorV2(ctx, batchResponse.Error, batchRequest)
} else if batchResponse.ErrorRom != executor.RomError_ROM_ERROR_NO_ERROR && executor.IsROMOutOfCountersError(batchResponse.ErrorRom) {
log.Warnf("executor batchResponse, ROM OOC, error: %v", err)
log.Warnf("executor batch %d response, ROM OOC, error: %v", newBatchNum, err)
log.Warn(batchResponseToString)
} else if batchResponse.ErrorRom != executor.RomError_ROM_ERROR_NO_ERROR {
err = executor.RomErr(batchResponse.ErrorRom)
log.Warnf("executor batchResponse, ROM error: %v", err)
log.Warnf("executor batch %d response, ROM error: %v", newBatchNum, err)
log.Warn(batchResponseToString)
} else {
log.Debug(batchResponseToString)
}
}

//workarroundDuplicatedBlock(res)
elapsed := time.Since(now)
if caller != metrics.DiscardCallerLabel {
metrics.ExecutorProcessingTime(string(caller), elapsed)
}

log.Infof("batch %d took %v to be processed by the executor ", batchRequest.OldBatchNum+1, elapsed)

return batchResponse, err
}

func processBatchResponseToString(batchResponse *executor.ProcessBatchResponseV2) string {
batchResponseLog := "executor batchResponse, NewStateRoot: %v, NewAccInputHash: %v, NewLocalExitRoot: %v, NewBatchNumber: %v, GasUsed: %v, FlushId: %v, StoredFlushId: %v, ProverId:%v, ForkId:%v, Error: %v\n"
batchResponseLog = fmt.Sprintf(batchResponseLog, hex.EncodeToHex(batchResponse.NewStateRoot), hex.EncodeToHex(batchResponse.NewAccInputHash), hex.EncodeToHex(batchResponse.NewLocalExitRoot),
func processBatchResponseToString(batchNum uint64, batchResponse *executor.ProcessBatchResponseV2, executionTime time.Duration) string {
batchResponseLog := "executor batch %d response, Time: %v, NewStateRoot: %v, NewAccInputHash: %v, NewLocalExitRoot: %v, NewBatchNumber: %v, GasUsed: %v, FlushId: %v, StoredFlushId: %v, ProverId:%v, ForkId:%v, Error: %v\n"
batchResponseLog = fmt.Sprintf(batchResponseLog, batchNum, executionTime, hex.EncodeToHex(batchResponse.NewStateRoot), hex.EncodeToHex(batchResponse.NewAccInputHash), hex.EncodeToHex(batchResponse.NewLocalExitRoot),
batchResponse.NewBatchNum, batchResponse.GasUsed, batchResponse.FlushId, batchResponse.StoredFlushId, batchResponse.ProverId, batchResponse.ForkId, batchResponse.Error)

for blockIndex, block := range batchResponse.BlockResponses {
Expand Down

0 comments on commit 4ddba20

Please sign in to comment.