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

Commit

Permalink
feat(driver): improve sync progress information (#288)
Browse files Browse the repository at this point in the history
Co-authored-by: Roger <50648015+RogerLamTd@users.noreply.github.com>
  • Loading branch information
davidtaikocha and RogerLamTd committed Jun 16, 2023
1 parent cb4dada commit 1b20465
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 43 deletions.
19 changes: 12 additions & 7 deletions driver/chain_syncer/beaconsync/progress_tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,14 @@ func (t *SyncProgressTracker) track(ctx context.Context) {
return
}

log.Info(
"L2 execution engine sync progress",
"progress", progress,
"lastProgressedTime", t.lastProgressedTime,
"timeout", t.timeout,
)
if progress != nil {
log.Info(
"L2 execution engine sync progress",
"progress", progress,
"lastProgressedTime", t.lastProgressedTime,
"timeout", t.timeout,
)
}

if progress == nil {
headHeight, err := t.client.BlockNumber(ctx)
Expand All @@ -103,7 +105,10 @@ func (t *SyncProgressTracker) track(ctx context.Context) {
return
}

log.Warn("L2 execution engine has not started P2P syncing yet")
log.Debug(
"L2 execution engine has not started P2P syncing yet",
"timeout", t.timeout,
)
}

defer func() { t.lastSyncProgress = progress }()
Expand Down
25 changes: 25 additions & 0 deletions driver/chain_syncer/beaconsync/syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,18 @@ func (s *Syncer) TriggerBeaconSync() error {
return nil
}

if s.progressTracker.Triggered() {
if s.progressTracker.lastSyncProgress == nil {
log.Info(
"Syncing beacon headers, please check L2 execution engine logs for progress",
"currentSyncHead", s.progressTracker.LastSyncedVerifiedBlockHeight(),
"newBlockID", blockID,
)
}

return nil
}

status, err := s.rpc.L2Engine.NewPayload(
s.ctx,
latestVerifiedHeadPayload,
Expand Down Expand Up @@ -84,6 +96,19 @@ func (s *Syncer) TriggerBeaconSync() error {
return nil
}

func (s *Syncer) Synced() (bool, error) {
if !s.progressTracker.Triggered() {
return false, nil
}
heightToSync := s.progressTracker.LastSyncedVerifiedBlockHeight()
l2Head, err := s.rpc.L2.BlockNumber(s.ctx)
if err != nil {
return false, err
}
log.Debug("Check if the L2 execution engine is synced", "heightToSync", heightToSync, "l2Head", l2Head)
return new(big.Int).SetUint64(l2Head).Cmp(heightToSync) >= 0, nil
}

// getVerifiedBlockPayload fetches the latest verified block's header, and converts it to an Engine API executable data,
// which will be used to let the node to start beacon syncing.
func (s *Syncer) getVerifiedBlockPayload(ctx context.Context) (*big.Int, *engine.ExecutableData, error) {
Expand Down
46 changes: 14 additions & 32 deletions driver/chain_syncer/chain_syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,12 @@ func (s *L2ChainSyncer) Sync(l1End *types.Header) error {
// If current L2 execution engine's chain is behind of the protocol's latest verified block head, and the
// `P2PSyncVerifiedBlocks` flag is set, try triggering a beacon sync in L2 execution engine to catch up the
// latest verified block head.
if s.needNewBeaconSyncTriggered() {
needBeaconSyncTriggered, err := s.needNewBeaconSyncTriggered()
if err != nil {
return err
}

if needBeaconSyncTriggered {
if err := s.beaconSyncer.TriggerBeaconSync(); err != nil {
return fmt.Errorf("trigger beacon sync error: %w", err)
}
Expand Down Expand Up @@ -119,40 +124,17 @@ func (s *L2ChainSyncer) Sync(l1End *types.Header) error {
return s.calldataSyncer.ProcessL1Blocks(s.ctx, l1End)
}

// AheadOfProtocolVerifiedHead checks whether the L2 chain is ahead of verified head in protocol.
func (s *L2ChainSyncer) AheadOfProtocolVerifiedHead() bool {
verifiedHeightToCompare := s.state.GetLatestVerifiedBlock().Height.Uint64()
log.Debug(
"Checking whether the execution engine is ahead of protocol's verified head",
"latestVerifiedBlock", verifiedHeightToCompare,
"executionEngineHead", s.state.GetL2Head().Number,
)
if verifiedHeightToCompare > 0 {
// If latest verified head height is equal to L2 execution engine's synced head height minus one,
// we also mark the triggered P2P sync progress as finished to prevent a potential `InsertBlockWithoutSetHead` in
// execution engine, which may cause errors since we do not pass all transactions in ExecutePayload when calling
// NewPayloadV1.
verifiedHeightToCompare -= 1
}

if s.state.GetL2Head().Number.Uint64() < verifiedHeightToCompare {
return false
}

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

return true
}

// needNewBeaconSyncTriggered checks wthether the current L2 execution engine needs to trigger
// needNewBeaconSyncTriggered checks whether the current L2 execution engine needs to trigger
// another new beacon sync.
func (s *L2ChainSyncer) needNewBeaconSyncTriggered() bool {
func (s *L2ChainSyncer) needNewBeaconSyncTriggered() (bool, error) {
synced, err := s.BeaconSyncer().Synced()
if err != nil {
return false, err
}
return s.p2pSyncVerifiedBlocks &&
s.state.GetLatestVerifiedBlock().Height.Uint64() > 0 &&
!s.AheadOfProtocolVerifiedHead() &&
!s.progressTracker.OutOfSync()
!synced &&
!s.progressTracker.OutOfSync(), nil
}

// BeaconSyncer returns the inner beacon syncer.
Expand Down
4 changes: 0 additions & 4 deletions driver/chain_syncer/chain_syncer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,6 @@ func (s *ChainSyncerTestSuite) TestGetInnerSyncers() {
s.NotNil(s.s.CalldataSyncer())
}

func (s *ChainSyncerTestSuite) TestAheadOfProtocolVerifiedHead() {
s.True(s.s.AheadOfProtocolVerifiedHead())
}

func (s *ChainSyncerTestSuite) TestSync() {
head, err := s.RpcClient.L1.HeaderByNumber(context.Background(), nil)
s.Nil(err)
Expand Down

0 comments on commit 1b20465

Please sign in to comment.