Skip to content

Commit

Permalink
Fix/sync l1 info tree data (0xPolygonHermez#3033)
Browse files Browse the repository at this point in the history
* fix l1InfoTreeData when syncing

* fix

* remove leaves in forcedBatches
  • Loading branch information
ARR552 authored Jan 5, 2024
1 parent e95d482 commit 35fb766
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 67 deletions.
2 changes: 1 addition & 1 deletion sequencer/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,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, l1InfoTree state.L1InfoTreeExitRootStorageEntry, timestampLimit time.Time, updateMerkleTree bool, skipVerifyL1InfoRoot uint32, forcedBlockHashL1 *common.Hash, dbTx pgx.Tx) (*executor.ProcessBatchResponseV2, error)
ExecuteBatchV2(ctx context.Context, batch state.Batch, L1InfoTreeRoot common.Hash, l1InfoTreeData map[uint32]state.L1DataV2, timestampLimit time.Time, updateMerkleTree bool, skipVerifyL1InfoRoot uint32, forcedBlockHashL1 *common.Hash, 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
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.

5 changes: 0 additions & 5 deletions sequencer/mock_worker.go

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

22 changes: 12 additions & 10 deletions state/batchV2.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,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, l1InfoTree L1InfoTreeExitRootStorageEntry, timestampLimit time.Time, updateMerkleTree bool, skipVerifyL1InfoRoot uint32, forcedBlockHashL1 *common.Hash, dbTx pgx.Tx) (*executor.ProcessBatchResponseV2, error) {
func (s *State) ExecuteBatchV2(ctx context.Context, batch Batch, L1InfoTreeRoot common.Hash, l1InfoTreeData map[uint32]L1DataV2, timestampLimit time.Time, updateMerkleTree bool, skipVerifyL1InfoRoot uint32, forcedBlockHashL1 *common.Hash, dbTx pgx.Tx) (*executor.ProcessBatchResponseV2, error) {
if dbTx == nil {
return nil, ErrDBTxNil
}
Expand All @@ -125,7 +125,7 @@ func (s *State) ExecuteBatchV2(ctx context.Context, batch Batch, l1InfoTree L1In
Coinbase: batch.Coinbase.String(),
BatchL2Data: batch.BatchL2Data,
OldStateRoot: previousBatch.StateRoot.Bytes(),
L1InfoRoot: l1InfoTree.L1InfoTreeRoot.Bytes(),
L1InfoRoot: L1InfoTreeRoot.Bytes(),
OldAccInputHash: previousBatch.AccInputHash.Bytes(),
TimestampLimit: uint64(timestampLimit.Unix()),
// Changed for new sequencer strategy
Expand All @@ -139,11 +139,15 @@ func (s *State) ExecuteBatchV2(ctx context.Context, batch Batch, l1InfoTree L1In
if forcedBlockHashL1 != nil {
processBatchRequest.ForcedBlockhashL1 = forcedBlockHashL1.Bytes()
} else {
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()),
}}
l1InfoTree := make(map[uint32]*executor.L1DataV2)
for i, v := range l1InfoTreeData {
l1InfoTree[i] = &executor.L1DataV2{
GlobalExitRoot: v.GlobalExitRoot.Bytes(),
BlockHashL1: v.BlockHashL1.Bytes(),
MinTimestamp: v.MinTimestamp,
}
}
processBatchRequest.L1InfoTreeData = l1InfoTree
}

// Send Batch to the Executor
Expand All @@ -160,9 +164,7 @@ func (s *State) ExecuteBatchV2(ctx context.Context, batch Batch, l1InfoTree L1In
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)
log.Debugf("ExecuteBatchV2[processBatchRequest.L1InfoTreeData]: %+v", l1InfoTreeData)

processBatchResponse, err := s.executorClient.ProcessBatchV2(ctx, processBatchRequest)
if err != nil {
Expand Down
46 changes: 20 additions & 26 deletions synchronizer/actions/etrog/processor_l1_sequence_batches.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ type stateProcessSequenceBatches interface {
GetNextForcedBatches(ctx context.Context, nextForcedBatches int, dbTx pgx.Tx) ([]state.ForcedBatch, error)
GetBatchByNumber(ctx context.Context, batchNumber uint64, dbTx pgx.Tx) (*state.Batch, error)
ProcessAndStoreClosedBatchV2(ctx context.Context, processingCtx state.ProcessingContextV2, dbTx pgx.Tx, caller metrics.CallerLabel) (common.Hash, uint64, string, error)
ExecuteBatchV2(ctx context.Context, batch state.Batch, l1InfoTree state.L1InfoTreeExitRootStorageEntry, timestampLimit time.Time, updateMerkleTree bool, skipVerifyL1InfoRoot uint32, forcedBlockHashL1 *common.Hash, dbTx pgx.Tx) (*executor.ProcessBatchResponseV2, error)
ExecuteBatchV2(ctx context.Context, batch state.Batch, L1InfoTreeRoot common.Hash, l1InfoTreeData map[uint32]state.L1DataV2, timestampLimit time.Time, updateMerkleTree bool, skipVerifyL1InfoRoot uint32, forcedBlockHashL1 *common.Hash, dbTx pgx.Tx) (*executor.ProcessBatchResponseV2, error)
AddAccumulatedInputHash(ctx context.Context, batchNum uint64, accInputHash common.Hash, dbTx pgx.Tx) error
ResetTrustedState(ctx context.Context, batchNumber uint64, dbTx pgx.Tx) error
AddSequence(ctx context.Context, sequence state.Sequence, dbTx pgx.Tx) error
Expand Down Expand Up @@ -119,10 +119,12 @@ func (g *ProcessorL1SequenceBatchesEtrog) processSequenceBatches(ctx context.Con
}
var (
processCtx state.ProcessingContextV2
currentL1InfoTree state.L1InfoTreeExitRootStorageEntry
forcedBlockHashL1 *common.Hash
l1InfoRoot common.Hash
err error
)
leaves := make(map[uint32]state.L1DataV2)

// ForcedBatch must be processed
if sbatch.PolygonRollupBaseEtrogBatchData.ForcedTimestamp > 0 && sbatch.BatchNumber != 1 { // If this is true means that the batch is forced
log.Debug("FORCED BATCH SEQUENCED!")
Expand Down Expand Up @@ -161,32 +163,25 @@ func (g *ProcessorL1SequenceBatchesEtrog) processSequenceBatches(ctx context.Con
}
log.Debug("Setting forcedBatchNum: ", forcedBatches[0].ForcedBatchNumber)
batch.ForcedBatchNum = &forcedBatches[0].ForcedBatchNumber
currentL1InfoTree = state.L1InfoTreeExitRootStorageEntry{
L1InfoTreeLeaf: state.L1InfoTreeLeaf{
PreviousBlockHash: sbatch.PolygonRollupBaseEtrogBatchData.ForcedBlockHashL1,
GlobalExitRoot: state.GlobalExitRoot{
Timestamp: time.Unix(int64(sbatch.PolygonRollupBaseEtrogBatchData.ForcedTimestamp), 0),
GlobalExitRoot: sbatch.PolygonRollupBaseEtrogBatchData.ForcedGlobalExitRoot,
},
},
L1InfoTreeRoot: sbatch.PolygonRollupBaseEtrogBatchData.ForcedGlobalExitRoot,
l1InfoRoot = sbatch.PolygonRollupBaseEtrogBatchData.ForcedGlobalExitRoot
tstampLimit := forcedBatches[0].ForcedAt
txs := forcedBatches[0].RawTxsData
processCtx = state.ProcessingContextV2{
BatchNumber: 1,
Coinbase: sbatch.SequencerAddr,
Timestamp: &tstampLimit,
L1InfoRoot: sbatch.PolygonRollupBaseEtrogBatchData.ForcedGlobalExitRoot,
BatchL2Data: &txs,
ForcedBlockHashL1: forcedBlockHashL1,
SkipVerifyL1InfoRoot: 1,
}
} else if sbatch.PolygonRollupBaseEtrogBatchData.ForcedTimestamp > 0 && sbatch.BatchNumber == 1 {
log.Debug("Processing initial batch")
var fBHL1 common.Hash = sbatch.PolygonRollupBaseEtrogBatchData.ForcedBlockHashL1
forcedBlockHashL1 = &fBHL1
txs := sbatch.PolygonRollupBaseEtrogBatchData.Transactions
currentL1InfoTree = state.L1InfoTreeExitRootStorageEntry{
L1InfoTreeLeaf: state.L1InfoTreeLeaf{
PreviousBlockHash: fBHL1,
GlobalExitRoot: state.GlobalExitRoot{
Timestamp: time.Unix(int64(sbatch.PolygonRollupBaseEtrogBatchData.ForcedTimestamp), 0),
GlobalExitRoot: sbatch.PolygonRollupBaseEtrogBatchData.ForcedGlobalExitRoot,
},
},
L1InfoTreeRoot: sbatch.PolygonRollupBaseEtrogBatchData.ForcedGlobalExitRoot,
}
tstampLimit := time.Unix(int64(sbatch.PolygonRollupBaseEtrogBatchData.ForcedTimestamp), 0)
l1InfoRoot = sbatch.PolygonRollupBaseEtrogBatchData.ForcedGlobalExitRoot
processCtx = state.ProcessingContextV2{
BatchNumber: 1,
Coinbase: sbatch.SequencerAddr,
Expand All @@ -197,7 +192,7 @@ func (g *ProcessorL1SequenceBatchesEtrog) processSequenceBatches(ctx context.Con
SkipVerifyL1InfoRoot: 1,
}
} else {
leafs, l1InfoRoot, err := g.state.GetL1InfoTreeDataFromBatchL2Data(ctx, batch.BatchL2Data, dbTx)
leaves, l1InfoRoot, err = g.state.GetL1InfoTreeDataFromBatchL2Data(ctx, batch.BatchL2Data, dbTx)
if err != nil {
log.Errorf("error getting L1InfoRootLeafByL1InfoRoot. sbatch.L1InfoRoot: %v", *sbatch.L1InfoRoot)
rollbackErr := dbTx.Rollback(ctx)
Expand All @@ -212,10 +207,10 @@ func (g *ProcessorL1SequenceBatchesEtrog) processSequenceBatches(ctx context.Con
Coinbase: batch.Coinbase,
Timestamp: &batch.Timestamp,
L1InfoRoot: l1InfoRoot,
L1InfoTreeData: leafs,
L1InfoTreeData: leaves,
ForcedBatchNum: batch.ForcedBatchNum,
BatchL2Data: &batch.BatchL2Data,
SkipVerifyL1InfoRoot: 0,
SkipVerifyL1InfoRoot: 1,
}
}

Expand Down Expand Up @@ -255,8 +250,7 @@ func (g *ProcessorL1SequenceBatchesEtrog) processSequenceBatches(ctx context.Con
}
} else {
// Reprocess batch to compare the stateRoot with tBatch.StateRoot and get accInputHash
var skipVerifyL1InfoRoot uint32 = 0 // false
p, err := g.state.ExecuteBatchV2(ctx, batch, currentL1InfoTree, *processCtx.Timestamp, false, skipVerifyL1InfoRoot, processCtx.ForcedBlockHashL1, dbTx)
p, err := g.state.ExecuteBatchV2(ctx, batch, l1InfoRoot, leaves, *processCtx.Timestamp, false, processCtx.SkipVerifyL1InfoRoot, processCtx.ForcedBlockHashL1, dbTx)
if err != nil {
log.Errorf("error executing L1 batch: %+v, error: %v", batch, err)
rollbackErr := dbTx.Rollback(ctx)
Expand Down
2 changes: 1 addition & 1 deletion synchronizer/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ type stateInterface interface {
StoreTransaction(ctx context.Context, batchNumber uint64, processedTx *state.ProcessTransactionResponse, coinbase common.Address, timestamp uint64, egpLog *state.EffectiveGasPriceLog, globalExitRoot, blockInfoRoot common.Hash, dbTx pgx.Tx) (*state.L2Header, error)
GetStateRootByBatchNumber(ctx context.Context, batchNum uint64, dbTx pgx.Tx) (common.Hash, error)
ExecuteBatch(ctx context.Context, batch state.Batch, updateMerkleTree bool, dbTx pgx.Tx) (*executor.ProcessBatchResponse, error)
ExecuteBatchV2(ctx context.Context, batch state.Batch, l1InfoTree state.L1InfoTreeExitRootStorageEntry, timestampLimit time.Time, updateMerkleTree bool, skipVerifyL1InfoRoot uint32, forcedBlockHashL1 *common.Hash, dbTx pgx.Tx) (*executor.ProcessBatchResponseV2, error)
ExecuteBatchV2(ctx context.Context, batch state.Batch, L1InfoTreeRoot common.Hash, l1InfoTreeData map[uint32]state.L1DataV2, timestampLimit time.Time, updateMerkleTree bool, skipVerifyL1InfoRoot uint32, forcedBlockHashL1 *common.Hash, dbTx pgx.Tx) (*executor.ProcessBatchResponseV2, error)
GetLastVerifiedBatch(ctx context.Context, dbTx pgx.Tx) (*state.VerifiedBatch, error)
GetLastVirtualBatchNum(ctx context.Context, dbTx pgx.Tx) (uint64, error)
AddSequence(ctx context.Context, sequence state.Sequence, dbTx pgx.Tx) error
Expand Down
Loading

0 comments on commit 35fb766

Please sign in to comment.