Skip to content

Commit

Permalink
Fix/l1 info root (0xPolygonHermez#2925)
Browse files Browse the repository at this point in the history
* fixes L1InfoRoot

* more fixes
  • Loading branch information
ARR552 authored Dec 15, 2023
1 parent a00c69d commit b013a3a
Show file tree
Hide file tree
Showing 15 changed files with 219 additions and 112 deletions.
2 changes: 1 addition & 1 deletion config/environments/local/local.genesis.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,6 @@
"globalExitRoot": "0x0000000000000000000000000000000000000000000000000000000000000000",
"timestamp": 1700820478,
"sequencer": "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266",
"ForcedBlockHash": "0x7874802e444cbae4df050fe75e2007e7a9180fa91f42df0438ef24717939ff91"
"ForcedBlockHashL1": "0x7874802e444cbae4df050fe75e2007e7a9180fa91f42df0438ef24717939ff91"
}
}
46 changes: 25 additions & 21 deletions sequencer/finalizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -449,16 +449,17 @@ func (f *finalizer) processTransaction(ctx context.Context, tx *TxTracker, first
}()

executorBatchRequest := state.ProcessRequest{
BatchNumber: f.wipBatch.batchNumber,
OldStateRoot: f.wipBatch.imStateRoot,
OldAccInputHash: f.wipBatch.imAccInputHash,
Coinbase: f.wipBatch.coinbase,
L1InfoRoot_V2: mockL1InfoRoot,
TimestampLimit_V2: uint64(f.wipL2Block.timestamp.Unix()),
Caller: stateMetrics.SequencerCallerLabel,
ForkID: f.state.GetForkIDByBatchNumber(f.wipBatch.batchNumber),
BatchNumber: f.wipBatch.batchNumber,
OldStateRoot: f.wipBatch.imStateRoot,
OldAccInputHash: f.wipBatch.imAccInputHash,
Coinbase: f.wipBatch.coinbase,
L1InfoTree: f.wipL2Block.l1InfoTreeExitRoot,
TimestampLimit_V2: uint64(f.wipL2Block.timestamp.Unix()),
Caller: stateMetrics.SequencerCallerLabel,
ForkID: f.state.GetForkIDByBatchNumber(f.wipBatch.batchNumber),
SkipVerifyL1InfoRoot_V2: true,
}

executorBatchRequest.L1InfoTree.L1InfoTreeRoot = mockL1InfoRoot
if f.wipBatch.isEmpty() {
executorBatchRequest.Transactions = f.state.BuildChangeL2Block(f.wipL2Block.deltaTimestamp, f.wipL2Block.l1InfoTreeExitRoot.L1InfoTreeIndex)
executorBatchRequest.SkipFirstChangeL2Block_V2 = false
Expand Down Expand Up @@ -544,7 +545,7 @@ func (f *finalizer) processTransaction(ctx context.Context, tx *TxTracker, first
executorBatchRequest.Transactions = append(executorBatchRequest.Transactions, effectivePercentageAsDecodedHex...)
}

log.Infof("processing tx: %s. Batch.BatchNumber: %d, batchNumber: %d, oldStateRoot: %s, txHash: %s, L1InfoRoot: %s", hashStr, f.wipBatch.batchNumber, executorBatchRequest.BatchNumber, executorBatchRequest.OldStateRoot, hashStr, executorBatchRequest.L1InfoRoot_V2.String())
log.Infof("processing tx: %s. Batch.BatchNumber: %d, batchNumber: %d, oldStateRoot: %s, txHash: %s, L1InfoRoot: %s", hashStr, f.wipBatch.batchNumber, executorBatchRequest.BatchNumber, executorBatchRequest.OldStateRoot, hashStr, executorBatchRequest.L1InfoTree.L1InfoTreeRoot.String())
processBatchResponse, err := f.state.ProcessBatchV2(ctx, executorBatchRequest, false)
if err != nil && errors.Is(err, runtime.ErrExecutorDBError) {
log.Errorf("failed to process transaction: %s", err)
Expand Down Expand Up @@ -1004,9 +1005,10 @@ func (f *finalizer) processForcedBatch2(ctx context.Context, forcedBatchNumber u
func (f *finalizer) processForcedBatch(ctx context.Context, lastBatchNumberInState uint64, stateRoot common.Hash, forcedBatch state.ForcedBatch) (uint64, common.Hash) {
//TODO: review this request for forced txs
executorBatchRequest := state.ProcessRequest{
BatchNumber: lastBatchNumberInState + 1,
OldStateRoot: stateRoot,
L1InfoRoot_V2: forcedBatch.GlobalExitRoot,
BatchNumber: lastBatchNumberInState + 1,
OldStateRoot: stateRoot,
// TODO FIX L1infoRoot
// L1InfoRoot_V2: forcedBatch.GlobalExitRoot,
Transactions: forcedBatch.RawTxsData,
Coinbase: f.sequencerAddress,
TimestampLimit_V2: uint64(forcedBatch.ForcedAt.Unix()), //TODO: review this is the TimeStampLimit we need to use
Expand Down Expand Up @@ -1110,15 +1112,17 @@ func (f *finalizer) reprocessFullBatch(ctx context.Context, batchNum uint64, ini

// TODO: review this request for reprocess full batch
executorBatchRequest := state.ProcessRequest{
BatchNumber: batch.BatchNumber,
L1InfoRoot_V2: mockL1InfoRoot,
OldStateRoot: initialStateRoot,
OldAccInputHash: initialAccInputHash,
Transactions: batch.BatchL2Data,
Coinbase: batch.Coinbase,
TimestampLimit_V2: uint64(time.Now().Unix()),
Caller: caller,
BatchNumber: batch.BatchNumber,
L1InfoTree: f.wipL2Block.l1InfoTreeExitRoot,
OldStateRoot: initialStateRoot,
OldAccInputHash: initialAccInputHash,
Transactions: batch.BatchL2Data,
Coinbase: batch.Coinbase,
TimestampLimit_V2: uint64(time.Now().Unix()),
Caller: caller,
SkipVerifyL1InfoRoot_V2: true,
}
executorBatchRequest.L1InfoTree.L1InfoTreeRoot = mockL1InfoRoot

var result *state.ProcessBatchResponse

Expand Down
2 changes: 1 addition & 1 deletion sequencer/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ type stateInterface interface {
CloseBatch(ctx context.Context, receipt state.ProcessingReceipt, dbTx pgx.Tx) error
CloseWIPBatch(ctx context.Context, receipt state.ProcessingReceipt, dbTx pgx.Tx) error
ExecuteBatch(ctx context.Context, batch state.Batch, updateMerkleTree bool, dbTx pgx.Tx) (*executor.ProcessBatchResponse, error)
ExecuteBatchV2(ctx context.Context, batch state.Batch, l1InfoRoot common.Hash, timestampLimit time.Time, updateMerkleTree bool, dbTx pgx.Tx) (*executor.ProcessBatchResponseV2, error)
ExecuteBatchV2(ctx context.Context, batch state.Batch, l1InfoTree state.L1InfoTreeExitRootStorageEntry, timestampLimit time.Time, updateMerkleTree bool, skipVerifyL1InfoRoot uint32, dbTx pgx.Tx) (*executor.ProcessBatchResponseV2, error)
GetForcedBatch(ctx context.Context, forcedBatchNumber uint64, dbTx pgx.Tx) (*state.ForcedBatch, error)
GetLastBatch(ctx context.Context, dbTx pgx.Tx) (*state.Batch, error)
GetLastBatchNumber(ctx context.Context, dbTx pgx.Tx) (uint64, error)
Expand Down
4 changes: 3 additions & 1 deletion sequencer/l2block.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,14 +245,16 @@ func (f *finalizer) processL2Block(ctx context.Context, l2Block *L2Block) (*stat
OldStateRoot: l2Block.initialStateRoot,
OldAccInputHash: l2Block.initialAccInputHash,
Coinbase: f.wipBatch.coinbase,
L1InfoRoot_V2: mockL1InfoRoot,
L1InfoTree: l2Block.l1InfoTreeExitRoot,
TimestampLimit_V2: uint64(l2Block.timestamp.Unix()),
Transactions: batchL2Data,
SkipFirstChangeL2Block_V2: false,
SkipWriteBlockInfoRoot_V2: false,
Caller: stateMetrics.SequencerCallerLabel,
ForkID: f.state.GetForkIDByBatchNumber(f.wipBatch.batchNumber),
SkipVerifyL1InfoRoot_V2: true,
}
executorBatchRequest.L1InfoTree.L1InfoTreeRoot = mockL1InfoRoot

var (
err error
Expand Down
18 changes: 9 additions & 9 deletions sequencer/mock_state.go

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

106 changes: 67 additions & 39 deletions state/batchV2.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,22 @@ var (
// ProcessingContextV2 is the necessary data that a batch needs to provide to the runtime,
// without the historical state data (processing receipt from previous batch)
type ProcessingContextV2 struct {
BatchNumber uint64
Coinbase common.Address
Timestamp time.Time // Batch timeStamp and also TimestampLimit
L1InfoRoot *common.Hash // If null is used the current L1InfoRoot
ForcedBatchNum *uint64
BatchL2Data *[]byte
ForcedBlockHashL1 *common.Hash
BatchNumber uint64
Coinbase common.Address
Timestamp time.Time // Batch timeStamp and also TimestampLimit
L1InfoRoot L1InfoTreeExitRootStorageEntry
ForcedBatchNum *uint64
BatchL2Data *[]byte
ForcedBlockHashL1 *common.Hash
SkipVerifyL1InfoRoot uint32
}

// ProcessSequencerBatchV2 is used by the sequencers to process transactions into an open batch for forkID >= ETROG
func (s *State) ProcessSequencerBatchV2(ctx context.Context, batchNumber uint64, batchL2Data []byte, caller metrics.CallerLabel, dbTx pgx.Tx) (*ProcessBatchResponse, error) {
func (s *State) ProcessSequencerBatchV2(ctx context.Context, batchNumber uint64, batchL2Data []byte, l1InfoTree L1InfoTreeExitRootStorageEntry, caller metrics.CallerLabel, dbTx pgx.Tx) (*ProcessBatchResponse, error) {
log.Debugf("*******************************************")
log.Debugf("ProcessSequencerBatchV2 start")

processBatchResponse, err := s.processBatchV2(ctx, batchNumber, batchL2Data, nil, nil, nil, caller, dbTx)
processBatchResponse, err := s.processBatchV2(ctx, batchNumber, batchL2Data, nil, l1InfoTree, cTrue, nil, caller, dbTx)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -67,13 +68,18 @@ func (s *State) ProcessBatchV2(ctx context.Context, request ProcessRequest, upda
Coinbase: request.Coinbase.String(),
BatchL2Data: request.Transactions,
OldStateRoot: request.OldStateRoot.Bytes(),
L1InfoRoot: request.L1InfoRoot_V2.Bytes(),
L1InfoRoot: request.L1InfoTree.L1InfoTreeRoot.Bytes(),
OldAccInputHash: request.OldAccInputHash.Bytes(),
TimestampLimit: request.TimestampLimit_V2,
UpdateMerkleTree: updateMT,
ChainId: s.cfg.ChainID,
ForkId: request.ForkID,
ContextId: uuid.NewString(),
L1InfoTreeData: map[uint32]*executor.L1DataV2{request.L1InfoTree.L1InfoTreeIndex: {
GlobalExitRoot: request.L1InfoTree.L1InfoTreeLeaf.GlobalExitRoot.GlobalExitRoot.Bytes(),
BlockHashL1: request.L1InfoTree.L1InfoTreeLeaf.PreviousBlockHash.Bytes(),
MinTimestamp: uint64(request.L1InfoTree.L1InfoTreeLeaf.GlobalExitRoot.Timestamp.Unix()),
}},
}

if request.SkipFirstChangeL2Block_V2 {
Expand All @@ -84,6 +90,10 @@ func (s *State) ProcessBatchV2(ctx context.Context, request ProcessRequest, upda
processBatchRequest.SkipWriteBlockInfoRoot = cTrue
}

if request.SkipVerifyL1InfoRoot_V2 {
processBatchRequest.SkipVerifyL1InfoRoot = cTrue
}

res, err := s.sendBatchRequestToExecutorV2(ctx, processBatchRequest, request.Caller)
if err != nil {
return nil, err
Expand All @@ -102,7 +112,7 @@ func (s *State) ProcessBatchV2(ctx context.Context, request ProcessRequest, upda
}

// ExecuteBatchV2 is used by the synchronizer to reprocess batches to compare generated state root vs stored one
func (s *State) ExecuteBatchV2(ctx context.Context, batch Batch, l1InfoRoot common.Hash, timestampLimit time.Time, updateMerkleTree bool, dbTx pgx.Tx) (*executor.ProcessBatchResponseV2, error) {
func (s *State) ExecuteBatchV2(ctx context.Context, batch Batch, l1InfoTree L1InfoTreeExitRootStorageEntry, timestampLimit time.Time, updateMerkleTree bool, skipVerifyL1InfoRoot uint32, dbTx pgx.Tx) (*executor.ProcessBatchResponseV2, error) {
if dbTx == nil {
return nil, ErrDBTxNil
}
Expand All @@ -122,21 +132,25 @@ func (s *State) ExecuteBatchV2(ctx context.Context, batch Batch, l1InfoRoot comm

// Create Batch
processBatchRequest := &executor.ProcessBatchRequestV2{
OldBatchNum: batch.BatchNumber - 1,
Coinbase: batch.Coinbase.String(),
BatchL2Data: batch.BatchL2Data,
OldStateRoot: previousBatch.StateRoot.Bytes(),
// TODO: Change this to L1InfoRoot
L1InfoRoot: l1InfoRoot.Bytes(),
OldBatchNum: batch.BatchNumber - 1,
Coinbase: batch.Coinbase.String(),
BatchL2Data: batch.BatchL2Data,
OldStateRoot: previousBatch.StateRoot.Bytes(),
L1InfoRoot: l1InfoTree.L1InfoTreeRoot.Bytes(),
OldAccInputHash: previousBatch.AccInputHash.Bytes(),
// TODO: Change this to TimestampLimit
TimestampLimit: uint64(timestampLimit.Unix()),
TimestampLimit: uint64(timestampLimit.Unix()),
// Changed for new sequencer strategy
UpdateMerkleTree: updateMT,
ChainId: s.cfg.ChainID,
ForkId: forkId,
ContextId: uuid.NewString(),
}
UpdateMerkleTree: updateMT,
ChainId: s.cfg.ChainID,
ForkId: forkId,
ContextId: uuid.NewString(),
SkipVerifyL1InfoRoot: skipVerifyL1InfoRoot,
}
processBatchRequest.L1InfoTreeData = map[uint32]*executor.L1DataV2{l1InfoTree.L1InfoTreeIndex: {
GlobalExitRoot: l1InfoTree.L1InfoTreeLeaf.GlobalExitRoot.GlobalExitRoot.Bytes(),
BlockHashL1: l1InfoTree.L1InfoTreeLeaf.PreviousBlockHash.Bytes(),
MinTimestamp: uint64(l1InfoTree.L1InfoTreeLeaf.GlobalExitRoot.Timestamp.Unix()),
}}

// Send Batch to the Executor
log.Debugf("ExecuteBatchV2[processBatchRequest.OldBatchNum]: %v", processBatchRequest.OldBatchNum)
Expand All @@ -151,6 +165,10 @@ func (s *State) ExecuteBatchV2(ctx context.Context, batch Batch, l1InfoRoot comm
log.Debugf("ExecuteBatchV2[processBatchRequest.ChainId]: %v", processBatchRequest.ChainId)
log.Debugf("ExecuteBatchV2[processBatchRequest.ForkId]: %v", processBatchRequest.ForkId)
log.Debugf("ExecuteBatchV2[processBatchRequest.ContextId]: %v", processBatchRequest.ContextId)
log.Debugf("ExecuteBatchV2[processBatchRequest.SkipVerifyL1InfoRoot]: %v", processBatchRequest.SkipVerifyL1InfoRoot)
log.Debugf("ExecuteBatchV2[processBatchRequest.L1InfoTreeData[%d].BlockHashL1]: %v", l1InfoTree.L1InfoTreeIndex, processBatchRequest.L1InfoTreeData[l1InfoTree.L1InfoTreeIndex].BlockHashL1)
log.Debugf("ExecuteBatchV2[processBatchRequest.L1InfoTreeData[%d].GlobalExitRoot]: %v", l1InfoTree.L1InfoTreeIndex, processBatchRequest.L1InfoTreeData[l1InfoTree.L1InfoTreeIndex].GlobalExitRoot)
log.Debugf("ExecuteBatchV2[processBatchRequest.L1InfoTreeData[%d].MinTimestamp]: %v", l1InfoTree.L1InfoTreeIndex, processBatchRequest.L1InfoTreeData[l1InfoTree.L1InfoTreeIndex].MinTimestamp)

processBatchResponse, err := s.executorClient.ProcessBatchV2(ctx, processBatchRequest)
if err != nil {
Expand All @@ -164,7 +182,7 @@ func (s *State) ExecuteBatchV2(ctx context.Context, batch Batch, l1InfoRoot comm
return processBatchResponse, err
}

func (s *State) processBatchV2(ctx context.Context, batchNumber uint64, batchL2Data []byte, timestampLimit *time.Time, l1InfoRoot, forcedBlockHashL1 *common.Hash, caller metrics.CallerLabel, dbTx pgx.Tx) (*executor.ProcessBatchResponseV2, error) {
func (s *State) processBatchV2(ctx context.Context, batchNumber uint64, batchL2Data []byte, timestampLimit *time.Time, l1InfoTree L1InfoTreeExitRootStorageEntry, skipVerifyL1InfoRoot uint32, forcedBlockHashL1 *common.Hash, caller metrics.CallerLabel, dbTx pgx.Tx) (*executor.ProcessBatchResponseV2, error) {
if dbTx == nil {
return nil, ErrDBTxNil
}
Expand Down Expand Up @@ -208,24 +226,31 @@ func (s *State) processBatchV2(ctx context.Context, batchNumber uint64, batchL2D
}
// Create Batch
processBatchRequest := &executor.ProcessBatchRequestV2{
OldBatchNum: lastBatch.BatchNumber - 1,
Coinbase: lastBatch.Coinbase.String(),
BatchL2Data: batchL2Data,
OldStateRoot: previousBatch.StateRoot.Bytes(),
OldAccInputHash: previousBatch.AccInputHash.Bytes(),
TimestampLimit: timestampLimitUnix,
UpdateMerkleTree: cTrue,
ChainId: s.cfg.ChainID,
ForkId: forkID,
ContextId: uuid.NewString(),
}
OldBatchNum: lastBatch.BatchNumber - 1,
Coinbase: lastBatch.Coinbase.String(),
BatchL2Data: batchL2Data,
OldStateRoot: previousBatch.StateRoot.Bytes(),
OldAccInputHash: previousBatch.AccInputHash.Bytes(),
TimestampLimit: timestampLimitUnix,
UpdateMerkleTree: cTrue,
ChainId: s.cfg.ChainID,
ForkId: forkID,
ContextId: uuid.NewString(),
SkipVerifyL1InfoRoot: skipVerifyL1InfoRoot,
}

processBatchRequest.L1InfoTreeData = map[uint32]*executor.L1DataV2{l1InfoTree.L1InfoTreeIndex: {
GlobalExitRoot: l1InfoTree.L1InfoTreeLeaf.GlobalExitRoot.GlobalExitRoot.Bytes(),
BlockHashL1: l1InfoTree.L1InfoTreeLeaf.PreviousBlockHash.Bytes(),
MinTimestamp: uint64(l1InfoTree.L1InfoTreeLeaf.GlobalExitRoot.Timestamp.Unix()),
}}
if forcedBlockHashL1 != nil {
log.Debug("Setting ForcedBlockhashL1: ", forcedBlockHashL1)
processBatchRequest.ForcedBlockhashL1 = forcedBlockHashL1.Bytes()
}

if l1InfoRoot != nil {
processBatchRequest.L1InfoRoot = l1InfoRoot.Bytes()
if l1InfoTree.L1InfoTreeRoot != (common.Hash{}) {
processBatchRequest.L1InfoRoot = l1InfoTree.L1InfoTreeRoot.Bytes()
} else {
currentl1InfoRoot := s.GetCurrentL1InfoRoot()
processBatchRequest.L1InfoRoot = currentl1InfoRoot.Bytes()
Expand Down Expand Up @@ -254,6 +279,9 @@ func (s *State) sendBatchRequestToExecutorV2(ctx context.Context, processBatchRe
log.Debugf("sendBatchRequestToExecutorV2[processBatchRequest.ChainId]: %v", processBatchRequest.ChainId)
log.Debugf("sendBatchRequestToExecutorV2[processBatchRequest.ForkId]: %v", processBatchRequest.ForkId)
log.Debugf("sendBatchRequestToExecutorV2[processBatchRequest.ContextId]: %v", processBatchRequest.ContextId)
log.Debugf("ExecuteBatchV2[processBatchRequest.SkipVerifyL1InfoRoot]: %v", processBatchRequest.SkipVerifyL1InfoRoot)
log.Debugf("ExecuteBatchV2[processBatchRequest.ForcedBlockhashL1]: %v", hex.EncodeToHex(processBatchRequest.ForcedBlockhashL1))
log.Debugf("ExecuteBatchV2[processBatchRequest.L1InfoTreeData: %+v", processBatchRequest.L1InfoTreeData)
}
now := time.Now()
res, err := s.executorClient.ProcessBatchV2(ctx, processBatchRequest)
Expand Down Expand Up @@ -360,7 +388,7 @@ func (s *State) ProcessAndStoreClosedBatchV2(ctx context.Context, processingCtx
return common.Hash{}, noFlushID, noProverID, err
}
processed, err := s.processBatchV2(ctx, processingCtx.BatchNumber, *BatchL2Data,
&processingCtx.Timestamp, processingCtx.L1InfoRoot, processingCtx.ForcedBlockHashL1, caller, dbTx)
&processingCtx.Timestamp, processingCtx.L1InfoRoot, processingCtx.SkipVerifyL1InfoRoot, processingCtx.ForcedBlockHashL1, caller, dbTx)
if err != nil {
log.Errorf("%s error processBatchV2: %v", debugPrefix, err)
return common.Hash{}, noFlushID, noProverID, err
Expand Down
Loading

0 comments on commit b013a3a

Please sign in to comment.