Skip to content

Commit

Permalink
synchronizer: unittest and remove pool_reorg (#3111)
Browse files Browse the repository at this point in the history
* removed reorgPool call
* add test for l1 sequenced batches
* removed panic after halt call as PR comments ask for
  • Loading branch information
joanestebanr committed Jan 30, 2024
1 parent 87bb731 commit 9412277
Show file tree
Hide file tree
Showing 3 changed files with 235 additions and 98 deletions.
68 changes: 2 additions & 66 deletions synchronizer/actions/etrog/processor_l1_sequence_batches.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
syncCommon "github.com/0xPolygonHermez/zkevm-node/synchronizer/common"
"github.com/0xPolygonHermez/zkevm-node/synchronizer/common/syncinterfaces"
"github.com/ethereum/go-ethereum/common"
ethTypes "github.com/ethereum/go-ethereum/core/types"
"github.com/jackc/pgx/v4"
)

Expand All @@ -32,19 +31,9 @@ type stateProcessSequenceBatches interface {
AddSequence(ctx context.Context, sequence state.Sequence, dbTx pgx.Tx) error
AddVirtualBatch(ctx context.Context, virtualBatch *state.VirtualBatch, dbTx pgx.Tx) error
AddTrustedReorg(ctx context.Context, trustedReorg *state.TrustedReorg, dbTx pgx.Tx) error
GetReorgedTransactions(ctx context.Context, batchNumber uint64, dbTx pgx.Tx) ([]*ethTypes.Transaction, error)
GetL1InfoTreeDataFromBatchL2Data(ctx context.Context, batchL2Data []byte, dbTx pgx.Tx) (map[uint32]state.L1DataV2, common.Hash, common.Hash, error)
}

type ethermanProcessSequenceBatches interface {
GetLatestBatchNumber() (uint64, error)
}

type poolProcessSequenceBatchesInterface interface {
DeleteReorgedTransactions(ctx context.Context, txs []*ethTypes.Transaction) error
StoreTx(ctx context.Context, tx ethTypes.Transaction, ip string, isWIP bool) error
}

type syncProcessSequenceBatchesInterface interface {
PendingFlushID(flushID uint64, proverID string)
IsTrustedSequencer() bool
Expand All @@ -55,17 +44,13 @@ type syncProcessSequenceBatchesInterface interface {
type ProcessorL1SequenceBatchesEtrog struct {
actions.ProcessorBase[ProcessorL1SequenceBatchesEtrog]
state stateProcessSequenceBatches
etherMan ethermanProcessSequenceBatches
pool poolProcessSequenceBatchesInterface
sync syncProcessSequenceBatchesInterface
timeProvider syncCommon.TimeProvider
halter syncinterfaces.CriticalErrorHandler
}

// NewProcessorL1SequenceBatches returns instance of a processor for SequenceBatchesOrder
func NewProcessorL1SequenceBatches(state stateProcessSequenceBatches,
etherMan ethermanProcessSequenceBatches,
pool poolProcessSequenceBatchesInterface,
sync syncProcessSequenceBatchesInterface,
timeProvider syncCommon.TimeProvider,
halter syncinterfaces.CriticalErrorHandler) *ProcessorL1SequenceBatchesEtrog {
Expand All @@ -74,8 +59,6 @@ func NewProcessorL1SequenceBatches(state stateProcessSequenceBatches,
SupportedEvent: []etherman.EventOrder{etherman.SequenceBatchesOrder},
SupportedForkdIds: &ForksIdOnlyEtrog},
state: state,
etherMan: etherMan,
pool: pool,
sync: sync,
timeProvider: timeProvider,
halter: halter,
Expand Down Expand Up @@ -294,18 +277,6 @@ func (p *ProcessorL1SequenceBatchesEtrog) processSequenceBatches(ctx context.Con
// Call the check trusted state method to compare trusted and virtual state
status := p.checkTrustedState(ctx, batch, tBatch, newRoot, dbTx)
if status {
// Reorg Pool
err := p.reorgPool(ctx, dbTx)
if err != nil {
rollbackErr := dbTx.Rollback(ctx)
if rollbackErr != nil {
log.Errorf("error rolling back state. BatchNumber: %d, BlockNumber: %d, rollbackErr: %s, error : %v", tBatch.BatchNumber, blockNumber, rollbackErr.Error(), err)
return rollbackErr
}
log.Errorf("error: %v. BatchNumber: %d, BlockNumber: %d", err, tBatch.BatchNumber, blockNumber)
return err
}

// Clean trustedState sync variables to avoid sync the trusted state from the wrong starting point.
// This wrong starting point would force the trusted sync to clean the virtualization of the batch reaching an inconsistency.
p.sync.CleanTrustedState()
Expand Down Expand Up @@ -377,43 +348,6 @@ func (p *ProcessorL1SequenceBatchesEtrog) processSequenceBatches(ctx context.Con
return nil
}

func (p *ProcessorL1SequenceBatchesEtrog) reorgPool(ctx context.Context, dbTx pgx.Tx) error {
latestBatchNum, err := p.etherMan.GetLatestBatchNumber()
if err != nil {
log.Error("error getting the latestBatchNumber virtualized in the smc. Error: ", err)
return err
}
batchNumber := latestBatchNum + 1
// Get transactions that have to be included in the pool again
txs, err := p.state.GetReorgedTransactions(ctx, batchNumber, dbTx)
if err != nil {
log.Errorf("error getting txs from trusted state. BatchNumber: %d, error: %v", batchNumber, err)
return err
}
log.Debug("Reorged transactions: ", txs)

// Remove txs from the pool
err = p.pool.DeleteReorgedTransactions(ctx, txs)
if err != nil {
log.Errorf("error deleting txs from the pool. BatchNumber: %d, error: %v", batchNumber, err)
return err
}
log.Debug("Delete reorged transactions")

// Add txs to the pool
for _, tx := range txs {
// Insert tx in WIP status to avoid the sequencer to grab them before it gets restarted
// When the sequencer restarts, it will update the status to pending non-wip
err = p.pool.StoreTx(ctx, *tx, "", true)
if err != nil {
log.Errorf("error storing tx into the pool again. TxHash: %s. BatchNumber: %d, error: %v", tx.Hash().String(), batchNumber, err)
return err
}
log.Debug("Reorged transactions inserted in the pool: ", tx.Hash())
}
return nil
}

func (p *ProcessorL1SequenceBatchesEtrog) checkTrustedState(ctx context.Context, batch state.Batch, tBatch *state.Batch, newRoot common.Hash, dbTx pgx.Tx) bool {
//Compare virtual state with trusted state
var reorgReasons strings.Builder
Expand Down Expand Up @@ -449,7 +383,9 @@ func (p *ProcessorL1SequenceBatchesEtrog) checkTrustedState(ctx context.Context,

if p.sync.IsTrustedSequencer() {
log.Errorf("TRUSTED REORG DETECTED! Batch: %d reson:%s", batch.BatchNumber, reason)
// Halt function never have to return! it must blocks the process
p.halt(ctx, fmt.Errorf("TRUSTED REORG DETECTED! Batch: %d", batch.BatchNumber))
log.Errorf("CRITICAL!!!: Never have to execute this code. Halt function never have to return! it must blocks the process")
}
if !tBatch.WIP {
log.Warnf("missmatch in trusted state detected for Batch Number: %d. Reasons: %s", tBatch.BatchNumber, reason)
Expand Down
Loading

0 comments on commit 9412277

Please sign in to comment.