Skip to content

Commit

Permalink
connection with interface library
Browse files Browse the repository at this point in the history
  • Loading branch information
joanestebanr committed Aug 27, 2024
1 parent 3acd277 commit 01a9c80
Show file tree
Hide file tree
Showing 6 changed files with 174 additions and 1 deletion.
60 changes: 60 additions & 0 deletions state/mocks/storage_rollback_batches_interface.go

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

60 changes: 60 additions & 0 deletions state/model/mocks/storage_rollback_batches_interface.go

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

25 changes: 24 additions & 1 deletion state/model/reorg_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ type ReorgRequest struct {
ReasonError error
}

type PreExecuteReorgFunc = func(ctx context.Context, reorgRequest *ReorgRequest, dbTx storageTxType) error

func (r *ReorgRequest) String() string {
return fmt.Sprintf("FirstL1BlockNumberToKeep: %d, ReasonError: %s", r.FirstL1BlockNumberToKeep, r.ReasonError)
}
Expand Down Expand Up @@ -49,6 +51,7 @@ type ReorgState struct {
storage StorageReorgInterface
onReorgCallbacks []ReorgCallbackType
lastReorgResult *ReorgExecutionResult
preExecuteReorg []PreExecuteReorgFunc
}

func NewReorgState(storage StorageReorgInterface) *ReorgState {
Expand All @@ -57,15 +60,35 @@ func NewReorgState(storage StorageReorgInterface) *ReorgState {
}
}

func (s *ReorgState) AddPreExecuteReorg(f PreExecuteReorgFunc) {
s.mutex.Lock()
defer s.mutex.Unlock()
s.preExecuteReorg = append(s.preExecuteReorg, f)
}

func (s *ReorgState) AddOnReorgCallback(f ReorgCallbackType) {
s.mutex.Lock()
defer s.mutex.Unlock()
s.onReorgCallbacks = append(s.onReorgCallbacks, f)
}

func (s *ReorgState) executePreExecuteReorg(ctx context.Context, reorgRequest *ReorgRequest, dbTx storageTxType) error {
for _, f := range s.preExecuteReorg {
err := f(ctx, reorgRequest, dbTx)
if err != nil {
return err
}
}
return nil
}

func (s *ReorgState) ExecuteReorg(ctx context.Context, reorgRequest ReorgRequest, dbTx storageTxType) ReorgExecutionResult {
startTime := time.Now()
err := s.storage.ResetToL1BlockNumber(ctx, reorgRequest.FirstL1BlockNumberToKeep, dbTx)
err := s.executePreExecuteReorg(ctx, &reorgRequest, dbTx)
if err != nil {
return s.createNewResult(reorgRequest, err, startTime)
}
err = s.storage.ResetToL1BlockNumber(ctx, reorgRequest.FirstL1BlockNumberToKeep, dbTx)
res := s.createNewResult(reorgRequest, err, startTime)
dbTx.AddCommitCallback(s.onTxCommit)
dbTx.AddCommitCallback(s.onTxRollback)
Expand Down
21 changes: 21 additions & 0 deletions state/model/rollback_batches_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type StorageRollbackBatchesInterface interface {
GetSequencesGreatestOrEqualBatchNumber(ctx context.Context, batchNumber uint64, dbTx storageTxType) (*SequencesBatchesSlice, error)
DeleteSequencesGreatestOrEqualBatchNumber(ctx context.Context, batchNumber uint64, dbTx storageTxType) error
AddRollbackBatchesLogEntry(ctx context.Context, entry *RollbackBatchesLogEntry, dbTx dbTxType) error
GetRollbackBatchesLogEntryGreaterOrEqualL1BlockNumber(ctx context.Context, l1BlockNumber uint64, dbTx dbTxType) ([]RollbackBatchesLogEntry, error)
}

type RollbackBatchesExecutionResult struct {
Expand Down Expand Up @@ -106,3 +107,23 @@ func (s *RollbackBatchesState) ExecuteRollbackBatches(ctx context.Context, rollb
dbTx.AddCommitCallback(func(dbTx storageTxType, err error) { s.onTxCommit(response, dbTx, err) })
return response, nil
}

// PreExecuteReorg check that is possible to execute the reorg
// or add more info to the reorg request
func (s *RollbackBatchesState) PreExecuteReorg(ctx context.Context, reorgRequest *ReorgRequest, dbTx storageTxType) error {
entries, err := s.storage.GetRollbackBatchesLogEntryGreaterOrEqualL1BlockNumber(ctx, reorgRequest.FirstL1BlockNumberToKeep+1, dbTx)
if err != nil {
err = fmt.Errorf("pre_execute_reorg: error getting rollback batches log entries (L1BlockNumber>=%d): %w", reorgRequest.FirstL1BlockNumberToKeep+1, err)
log.Error(err.Error())
return err
}
if len(entries) == 0 {
log.Infof("pre_execute_reorg: no rollback batches log entries affected for reorg (L1BlockNumber>=%d)", reorgRequest.FirstL1BlockNumberToKeep+1)
return nil
}
// Todo a real reorg you must get the lowest UndoFirstBlockNumber and execute the reorg from this point
err = fmt.Errorf("pre_execute_reorg: reorg not possible (L1BlockNumber>=%d), affect to rollback batches! (%v)", reorgRequest.FirstL1BlockNumberToKeep+1, entries)
log.Error(err.Error())
return err

}
2 changes: 2 additions & 0 deletions state/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,7 @@ func NewState(storageImpl storage.Storer) *State {
}
// Connect cache invalidation on Reorg
res.ReorgState.AddOnReorgCallback(res.L1InfoTreeState.OnReorg)
// Before doing a reorg rollbackBatches must validate it
res.ReorgState.AddPreExecuteReorg(res.RollbackBatchesState.PreExecuteReorg)
return res
}
7 changes: 7 additions & 0 deletions synchronizer/synchronizer_adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ func (s *SynchronizerAdapter) SetCallbackOnReorgDone(callback func(reorgData Reo
}

func (s *SynchronizerAdapter) SetCallbackOnRollbackBatches(callback func(data RollbackBatchesData)) {
s.internalSyncrhonizer.SetCallbackOnRollbackBatches(
func(data internal.RollbackBatchesData) {
callback(RollbackBatchesData{
LastBatchNumber: data.LastBatchNumber,
LastBatchAccInputHash: data.LastBatchAccInputHash,
})
})
}

func (s *SynchronizerAdapter) Sync(returnOnSync bool) error {
Expand Down

0 comments on commit 01a9c80

Please sign in to comment.