Skip to content

Commit

Permalink
change implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
colinlyguo committed Sep 18, 2024
1 parent 41ab50d commit d38f656
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 24 deletions.
36 changes: 12 additions & 24 deletions eth/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -836,40 +836,28 @@ func (api *ScrollAPI) CalculateRowConsumptionByBlockNumber(ctx context.Context,

// SetRollupEventSyncedL1Height sets the synced L1 height for rollup event synchronization
func (api *ScrollAPI) SetRollupEventSyncedL1Height(height uint64) error {
prevHeightPtr := rawdb.ReadRollupEventSyncedL1BlockNumber(api.eth.ChainDb())

if prevHeightPtr == nil {
log.Warn("No previous rollup event synced L1 height found in database")
return fmt.Errorf("no previous rollup event synced L1 height found in database")
}

prevHeight := *prevHeightPtr
if height >= prevHeight {
log.Warn("New rollup event synced L1 height is not lower than previous height", "newHeight", height, "prevHeight", prevHeight)
return fmt.Errorf("new rollup event synced L1 height (%d) is not lower than previous height (%d)", height, prevHeight)
rollupSyncService := api.eth.GetRollupSyncService()
if rollupSyncService == nil {
return errors.New("RollupSyncService is not available")
}

log.Info("Setting rollup event synced L1 height", "height", height)
rawdb.WriteRollupEventSyncedL1BlockNumber(api.eth.ChainDb(), height)

rollupSyncService.ResetToHeight(height)

return nil
}

// SetL1MessageSyncedL1Height sets the synced L1 height for L1 message synchronization
func (api *ScrollAPI) SetL1MessageSyncedL1Height(height uint64) error {
prevHeightPtr := rawdb.ReadSyncedL1BlockNumber(api.eth.ChainDb())

if prevHeightPtr == nil {
log.Warn("No previous L1 message synced L1 height found in database")
return fmt.Errorf("no previous L1 message synced L1 height found in database")
}

prevHeight := *prevHeightPtr
if height >= prevHeight {
log.Warn("New L1 message synced L1 height is not lower than previous height", "newHeight", height, "prevHeight", prevHeight)
return fmt.Errorf("new L1 message synced L1 height (%d) is not lower than previous height (%d)", height, prevHeight)
syncService := api.eth.GetSyncService()
if syncService == nil {
return errors.New("SyncService is not available")
}

log.Info("Setting L1 message synced L1 height", "height", height)
rawdb.WriteSyncedL1BlockNumber(api.eth.ChainDb(), height)

syncService.ResetToHeight(height)

return nil
}
12 changes: 12 additions & 0 deletions eth/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -609,3 +609,15 @@ func (s *Ethereum) Stop() error {

return nil
}

// GetRollupSyncService returns the RollupSyncService of the Ethereum instance.
// It returns nil if the service is not initialized.
func (e *Ethereum) GetRollupSyncService() *rollup_sync_service.RollupSyncService {
return e.rollupSyncService
}

// GetSyncService returns the SyncService of the Ethereum instance.
// It returns nil if the service is not initialized.
func (e *Ethereum) GetSyncService() *sync_service.SyncService {
return e.syncService
}
13 changes: 13 additions & 0 deletions rollup/rollup_sync_service/rollup_sync_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,19 @@ func (s *RollupSyncService) Stop() {
}
}

// ResetToHeight resets the RollupSyncService to a specific L1 block height
func (s *RollupSyncService) ResetToHeight(height uint64) {
s.Stop()

s.latestProcessedBlock = height

rawdb.WriteRollupEventSyncedL1BlockNumber(s.db, height)

log.Info("Reset rollup sync service", "height", height)

go s.Start()
}

func (s *RollupSyncService) fetchRollupEvents() {
latestConfirmed, err := s.client.getLatestFinalizedBlockNumber()
if err != nil {
Expand Down
13 changes: 13 additions & 0 deletions rollup/sync_service/sync_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,19 @@ func (s *SyncService) Stop() {
}
}

// ResetToHeight resets the SyncService to a specific L1 block height
func (s *SyncService) ResetToHeight(height uint64) {
s.Stop()

s.latestProcessedBlock = height

rawdb.WriteSyncedL1BlockNumber(s.db, height)

log.Info("Reset sync service", "height", height)

go s.Start()
}

// SubscribeNewL1MsgsEvent registers a subscription of NewL1MsgsEvent and
// starts sending event to the given channel.
func (s *SyncService) SubscribeNewL1MsgsEvent(ch chan<- core.NewL1MsgsEvent) event.Subscription {
Expand Down

0 comments on commit d38f656

Please sign in to comment.