Skip to content

Commit

Permalink
#3376 - check GlobalExitRoot before working on a batch
Browse files Browse the repository at this point in the history
  • Loading branch information
joanestebanr committed Feb 27, 2024
1 parent d1e9fda commit a1247bd
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 45 deletions.

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

30 changes: 23 additions & 7 deletions synchronizer/l2_sync/l2_shared/processor_trusted_batch_sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,31 +127,47 @@ type SyncTrustedBatchExecutor interface {
NothingProcess(ctx context.Context, data *ProcessData, dbTx pgx.Tx) (*ProcessResponse, error)
}

// L1SyncChecker is the interface to check if we are enough synced from L1 to process a batch
type L1SyncChecker interface {
CheckL1SyncStatusEnoughToProcessBatch(ctx context.Context, batchNumber uint64, globalExitRoot common.Hash, dbTx pgx.Tx) error
}

// ProcessorTrustedBatchSync is a template to sync trusted state. It classify what kind of update is needed and call to SyncTrustedStateBatchExecutorSteps
//
// that is the one that execute the sync process
//
// the real implementation of the steps is in the SyncTrustedStateBatchExecutorSteps interface that known how to process a batch
type ProcessorTrustedBatchSync struct {
Steps SyncTrustedBatchExecutor
timeProvider syncCommon.TimeProvider
Cfg l2_sync.Config
Steps SyncTrustedBatchExecutor
timeProvider syncCommon.TimeProvider
l1SyncChecker L1SyncChecker
Cfg l2_sync.Config
}

// NewProcessorTrustedBatchSync creates a new SyncTrustedStateBatchExecutorTemplate
func NewProcessorTrustedBatchSync(steps SyncTrustedBatchExecutor,
timeProvider syncCommon.TimeProvider, cfg l2_sync.Config) *ProcessorTrustedBatchSync {
timeProvider syncCommon.TimeProvider, l1SyncChecker L1SyncChecker, cfg l2_sync.Config) *ProcessorTrustedBatchSync {
return &ProcessorTrustedBatchSync{
Steps: steps,
timeProvider: timeProvider,
Cfg: cfg,
Steps: steps,
timeProvider: timeProvider,
l1SyncChecker: l1SyncChecker,
Cfg: cfg,
}
}

// ProcessTrustedBatch processes a trusted batch and return the new state
func (s *ProcessorTrustedBatchSync) ProcessTrustedBatch(ctx context.Context, trustedBatch *types.Batch, status TrustedState, dbTx pgx.Tx, debugPrefix string) (*TrustedState, error) {
log.Debugf("%s Processing trusted batch: %v", debugPrefix, trustedBatch.Number)
stateCurrentBatch, statePreviousBatch := s.GetCurrentAndPreviousBatchFromCache(&status)
if s.l1SyncChecker != nil {
err := s.l1SyncChecker.CheckL1SyncStatusEnoughToProcessBatch(ctx, uint64(trustedBatch.Number), trustedBatch.GlobalExitRoot, dbTx)
if err != nil {
log.Errorf("%s error checking GlobalExitRoot from TrustedBatch. Error: ", debugPrefix, err)
return nil, err
}
} else {
log.Infof("Disabled check L1 sync status for process batch")
}
processMode, err := s.GetModeForProcessBatch(trustedBatch, stateCurrentBatch, statePreviousBatch, debugPrefix)
if err != nil {
log.Error("%s error getting processMode. Error: ", debugPrefix, trustedBatch.Number, err)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ var (
func TestCacheEmpty(t *testing.T) {
mockExecutor := mock_l2_shared.NewSyncTrustedBatchExecutor(t)
mockTimer := &commonSync.MockTimerProvider{}
sut := l2_shared.NewProcessorTrustedBatchSync(mockExecutor, mockTimer, cfg)
mockL1SyncChecker := mock_l2_shared.NewL1SyncChecker(t)
sut := l2_shared.NewProcessorTrustedBatchSync(mockExecutor, mockTimer, mockL1SyncChecker, cfg)

current, previous := sut.GetCurrentAndPreviousBatchFromCache(&l2_shared.TrustedState{
LastTrustedBatches: []*state.Batch{nil, nil},
Expand Down Expand Up @@ -57,7 +58,7 @@ func TestCacheJustCurrent(t *testing.T) {
status := l2_shared.TrustedState{
LastTrustedBatches: []*state.Batch{&batchA},
}
sut := l2_shared.NewProcessorTrustedBatchSync(mockExecutor, mockTimer, cfg)
sut := l2_shared.NewProcessorTrustedBatchSync(mockExecutor, mockTimer, nil, cfg)

current, previous := sut.GetCurrentAndPreviousBatchFromCache(&status)
require.Nil(t, previous)
Expand All @@ -75,7 +76,7 @@ func TestCacheJustPrevious(t *testing.T) {
status := l2_shared.TrustedState{
LastTrustedBatches: []*state.Batch{nil, &batchA},
}
sut := l2_shared.NewProcessorTrustedBatchSync(mockExecutor, mockTimer, cfg)
sut := l2_shared.NewProcessorTrustedBatchSync(mockExecutor, mockTimer, nil, cfg)

current, previous := sut.GetCurrentAndPreviousBatchFromCache(&status)
require.Nil(t, current)
Expand All @@ -98,7 +99,7 @@ func newTestDataForProcessorTrustedBatchSync(t *testing.T) *TestDataForProcessor
return &TestDataForProcessorTrustedBatchSync{
mockTimer: mockTimer,
mockExecutor: mockExecutor,
sut: l2_shared.NewProcessorTrustedBatchSync(mockExecutor, mockTimer, cfg),
sut: l2_shared.NewProcessorTrustedBatchSync(mockExecutor, mockTimer, nil, cfg),
stateCurrentBatch: &state.Batch{
BatchNumber: 123,
Coinbase: common.HexToAddress("0x1230"),
Expand Down
39 changes: 7 additions & 32 deletions synchronizer/l2_sync/l2_sync_etrog/executor_trusted_batch_sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,31 +47,24 @@ type StateInterface interface {
GetLastVirtualBatchNum(ctx context.Context, dbTx pgx.Tx) (uint64, error)
}

// L1SyncChecker is the interface to check if we are synced from L1 to process a batch
type L1SyncChecker interface {
CheckL1SyncStatusEnoughToProcessBatch(ctx context.Context, batchNumber uint64, globalExitRoot common.Hash, dbTx pgx.Tx) error
}

// SyncTrustedBatchExecutorForEtrog is the implementation of the SyncTrustedStateBatchExecutorSteps that
// have the functions to sync a fullBatch, incrementalBatch and reprocessBatch
type SyncTrustedBatchExecutorForEtrog struct {
state StateInterface
sync syncinterfaces.SynchronizerFlushIDManager
l1SyncChecker L1SyncChecker
state StateInterface
sync syncinterfaces.SynchronizerFlushIDManager
}

// NewSyncTrustedBatchExecutorForEtrog creates a new prcessor for sync with L2 batches
func NewSyncTrustedBatchExecutorForEtrog(zkEVMClient syncinterfaces.ZKEVMClientTrustedBatchesGetter,
state l2_shared.StateInterface, stateBatchExecutor StateInterface,
sync syncinterfaces.SynchronizerFlushIDManager, timeProvider syncCommon.TimeProvider, l1SyncChecker L1SyncChecker,
sync syncinterfaces.SynchronizerFlushIDManager, timeProvider syncCommon.TimeProvider, l1SyncChecker l2_shared.L1SyncChecker,
cfg l2_sync.Config) *l2_shared.TrustedBatchesRetrieve {
executorSteps := &SyncTrustedBatchExecutorForEtrog{
state: stateBatchExecutor,
sync: sync,
l1SyncChecker: l1SyncChecker,
state: stateBatchExecutor,
sync: sync,
}

executor := l2_shared.NewProcessorTrustedBatchSync(executorSteps, timeProvider, cfg)
executor := l2_shared.NewProcessorTrustedBatchSync(executorSteps, timeProvider, l1SyncChecker, cfg)
a := l2_shared.NewTrustedBatchesRetrieve(executor, zkEVMClient, state, sync, *l2_shared.NewTrustedStateManager(timeProvider, timeOfLiveBatchOnCache))
return a
}
Expand Down Expand Up @@ -148,12 +141,7 @@ func (b *SyncTrustedBatchExecutorForEtrog) FullProcess(ctx context.Context, data
data.DebugPrefix += " (emptyBatch) "
return b.CreateEmptyBatch(ctx, data, dbTx)
}
err := b.checkIfWeAreSyncedFromL1ToProcessGlobalExitRoot(ctx, data, dbTx)
if err != nil {
log.Errorf("%s error checkIfWeAreSyncedFromL1ToProcessGlobalExitRoot. Error: %v", data.DebugPrefix, err)
return nil, err
}
err = b.openBatch(ctx, data.TrustedBatch, dbTx, data.DebugPrefix)
err := b.openBatch(ctx, data.TrustedBatch, dbTx, data.DebugPrefix)
if err != nil {
log.Errorf("%s error openning batch. Error: %v", data.DebugPrefix, err)
return nil, err
Expand Down Expand Up @@ -213,11 +201,6 @@ func (b *SyncTrustedBatchExecutorForEtrog) IncrementalProcess(ctx context.Contex
log.Errorf("%s error checkThatL2DataIsIncremental. Error: %v", data.DebugPrefix, err)
return nil, err
}
err = b.checkIfWeAreSyncedFromL1ToProcessGlobalExitRoot(ctx, data, dbTx)
if err != nil {
log.Errorf("%s error checkIfWeAreSyncedFromL1ToProcessGlobalExitRoot. Error: %v", data.DebugPrefix, err)
return nil, err
}

PartialBatchL2Data, err := b.composePartialBatch(data.StateBatch, data.TrustedBatch)
if err != nil {
Expand Down Expand Up @@ -271,14 +254,6 @@ func (b *SyncTrustedBatchExecutorForEtrog) IncrementalProcess(ctx context.Contex
return &res, nil
}

func (b *SyncTrustedBatchExecutorForEtrog) checkIfWeAreSyncedFromL1ToProcessGlobalExitRoot(ctx context.Context, data *l2_shared.ProcessData, dbTx pgx.Tx) error {
if b.l1SyncChecker == nil {
log.Infof("Disabled check L1 sync status for process batch")
return nil
}
return b.l1SyncChecker.CheckL1SyncStatusEnoughToProcessBatch(ctx, data.BatchNumber, data.TrustedBatch.GlobalExitRoot, dbTx)
}

func (b *SyncTrustedBatchExecutorForEtrog) updateWIPBatch(ctx context.Context, data *l2_shared.ProcessData, NewStateRoot common.Hash, dbTx pgx.Tx) error {
receipt := state.ProcessingReceipt{
BatchNumber: data.BatchNumber,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,6 @@ func TestReprocessRejectDeleteVirtualBatch(t *testing.T) {
testData.stateMock.EXPECT().GetLastVirtualBatchNum(testData.ctx, mock.Anything).Return(uint64(123), nil).Maybe()
_, err := testData.sut.ReProcess(testData.ctx, &data, nil)
require.Error(t, err)

}

func TestNothingProcessIfBatchMustBeClosedThenCloseBatch(t *testing.T) {
Expand Down

0 comments on commit a1247bd

Please sign in to comment.