Skip to content
This repository has been archived by the owner on May 11, 2024. It is now read-only.

Commit

Permalink
feat: update progress tracker
Browse files Browse the repository at this point in the history
  • Loading branch information
davidtaikocha committed Oct 26, 2023
1 parent 1c9d70a commit 341ddb9
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 35 deletions.
29 changes: 7 additions & 22 deletions driver/chain_syncer/beaconsync/progress_tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
)

var (
syncProgressCheckInterval = 10 * time.Second
syncProgressCheckInterval = 12 * time.Second
)

// SyncProgressTracker is responsible for tracking the L2 execution engine's sync progress, after
Expand All @@ -24,10 +24,9 @@ type SyncProgressTracker struct {
client *rpc.EthClient

// Meta data
triggered bool
lastSyncedVerifiedBlockID *big.Int
lastSyncedVerifiedBlockHeight *big.Int
lastSyncedVerifiedBlockHash common.Hash
triggered bool
lastSyncedVerifiedBlockID *big.Int
lastSyncedVerifiedBlockHash common.Hash

// Out-of-sync check related
lastSyncProgress *ethereum.SyncProgress
Expand Down Expand Up @@ -94,12 +93,11 @@ func (t *SyncProgressTracker) track(ctx context.Context) {
return
}

if new(big.Int).SetUint64(headHeight).Cmp(t.lastSyncedVerifiedBlockHeight) >= 0 {
if new(big.Int).SetUint64(headHeight).Cmp(t.lastSyncedVerifiedBlockID) >= 0 {
t.lastProgressedTime = time.Now()
log.Info("L2 execution engine has finished the P2P sync work, all verified blocks synced, "+
"will switch to insert pending blocks one by one",
"lastSyncedVerifiedBlockID", t.lastSyncedVerifiedBlockID,
"lastSyncedVerifiedBlockHeight", t.lastSyncedVerifiedBlockHeight,
"lastSyncedVerifiedBlockHash", t.lastSyncedVerifiedBlockHash,
)
return
Expand Down Expand Up @@ -134,19 +132,18 @@ func (t *SyncProgressTracker) track(ctx context.Context) {
}

// UpdateMeta updates the inner beacon sync meta data.
func (t *SyncProgressTracker) UpdateMeta(id, height *big.Int, blockHash common.Hash) {
func (t *SyncProgressTracker) UpdateMeta(id *big.Int, blockHash common.Hash) {
t.mutex.Lock()
defer t.mutex.Unlock()

log.Debug("Update sync progress tracker meta", "id", id, "height", height, "hash", blockHash)
log.Debug("Update sync progress tracker meta", "id", id, "hash", blockHash)

if !t.triggered {
t.lastProgressedTime = time.Now()
}

t.triggered = true
t.lastSyncedVerifiedBlockID = id
t.lastSyncedVerifiedBlockHeight = height
t.lastSyncedVerifiedBlockHash = blockHash
}

Expand Down Expand Up @@ -203,18 +200,6 @@ func (t *SyncProgressTracker) LastSyncedVerifiedBlockID() *big.Int {
return new(big.Int).Set(t.lastSyncedVerifiedBlockID)
}

// LastSyncedVerifiedBlockHeight returns tracker.lastSyncedVerifiedBlockHeight.
func (t *SyncProgressTracker) LastSyncedVerifiedBlockHeight() *big.Int {
t.mutex.RLock()
defer t.mutex.RUnlock()

if t.lastSyncedVerifiedBlockHeight == nil {
return nil
}

return new(big.Int).Set(t.lastSyncedVerifiedBlockHeight)
}

// LastSyncedVerifiedBlockHash returns tracker.lastSyncedVerifiedBlockHash.
func (t *SyncProgressTracker) LastSyncedVerifiedBlockHash() common.Hash {
t.mutex.RLock()
Expand Down
8 changes: 1 addition & 7 deletions driver/chain_syncer/beaconsync/progress_tracker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func (s *BeaconSyncProgressTrackerTestSuite) TestTrack() {

// Triggered
ctx, cancel = context.WithCancel(context.Background())
s.t.UpdateMeta(common.Big256, common.Big256, testutils.RandomHash())
s.t.UpdateMeta(common.Big256, testutils.RandomHash())
go s.t.Track(ctx)
time.Sleep(syncProgressCheckInterval + 5*time.Second)
cancel()
Expand Down Expand Up @@ -90,12 +90,6 @@ func (s *BeaconSyncProgressTrackerTestSuite) TestLastSyncedVerifiedBlockID() {
s.Equal(common.Big1.Uint64(), s.t.LastSyncedVerifiedBlockID().Uint64())
}

func (s *BeaconSyncProgressTrackerTestSuite) TestLastSyncedVerifiedBlockHeight() {
s.Nil(s.t.LastSyncedVerifiedBlockHeight())
s.t.lastSyncedVerifiedBlockHeight = common.Big1
s.Equal(common.Big1.Uint64(), s.t.LastSyncedVerifiedBlockHeight().Uint64())
}

func (s *BeaconSyncProgressTrackerTestSuite) TestLastSyncedVerifiedBlockHash() {
s.Equal(
common.HexToHash("0x0000000000000000000000000000000000000000000000000000000000000000"),
Expand Down
4 changes: 1 addition & 3 deletions driver/chain_syncer/beaconsync/syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func (s *Syncer) TriggerBeaconSync() error {
if s.progressTracker.lastSyncProgress == nil {
log.Info(
"Syncing beacon headers, please check L2 execution engine logs for progress",
"currentSyncHead", s.progressTracker.LastSyncedVerifiedBlockHeight(),
"currentSyncHead", s.progressTracker.LastSyncedVerifiedBlockID(),
"newBlockID", blockID,
)
}
Expand Down Expand Up @@ -80,14 +80,12 @@ func (s *Syncer) TriggerBeaconSync() error {
// Update sync status.
s.progressTracker.UpdateMeta(
blockID,
new(big.Int).SetUint64(latestVerifiedHeadPayload.Number),
latestVerifiedHeadPayload.BlockHash,
)

log.Info(
"⛓️ Beacon sync triggered",
"newHeadID", blockID,
"newHeadHeight", s.progressTracker.LastSyncedVerifiedBlockHeight(),
"newHeadHash", s.progressTracker.LastSyncedVerifiedBlockHash(),
)

Expand Down
6 changes: 3 additions & 3 deletions driver/chain_syncer/chain_syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func (s *L2ChainSyncer) Sync(l1End *types.Header) error {
}

// Reset to the latest L2 execution engine's chain status.
s.progressTracker.UpdateMeta(l2Head.Number, l2Head.Number, l2Head.Hash())
s.progressTracker.UpdateMeta(l2Head.Number, l2Head.Hash())
}

// Insert the proposed block one by one.
Expand All @@ -131,8 +131,8 @@ func (s *L2ChainSyncer) AheadOfProtocolVerifiedHead() bool {
return false
}

if s.progressTracker.LastSyncedVerifiedBlockHeight() != nil {
return s.state.GetL2Head().Number.Uint64() >= s.progressTracker.LastSyncedVerifiedBlockHeight().Uint64()
if s.progressTracker.LastSyncedVerifiedBlockID() != nil {
return s.state.GetL2Head().Number.Uint64() >= s.progressTracker.LastSyncedVerifiedBlockID().Uint64()
}

return true
Expand Down

0 comments on commit 341ddb9

Please sign in to comment.