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

Commit

Permalink
fix(driver): fix geth lag to verified block when syncing (#294)
Browse files Browse the repository at this point in the history
  • Loading branch information
davidtaikocha committed Jun 19, 2023
1 parent 90e365a commit c57f6e8
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 39 deletions.
26 changes: 0 additions & 26 deletions driver/chain_syncer/beaconsync/syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,19 +52,6 @@ func (s *Syncer) TriggerBeaconSync() error {
)
}

// Keep the heartbeat with L2 execution engine.
fcRes, err := s.rpc.L2Engine.ForkchoiceUpdate(s.ctx, &engine.ForkchoiceStateV1{
HeadBlockHash: s.progressTracker.LastSyncedVerifiedBlockHash(),
SafeBlockHash: s.progressTracker.LastSyncedVerifiedBlockHash(),
FinalizedBlockHash: s.progressTracker.LastSyncedVerifiedBlockHash(),
}, nil)
if err != nil {
return err
}
if fcRes.PayloadStatus.Status != engine.SYNCING {
return fmt.Errorf("unexpected ForkchoiceUpdate response status: %s", fcRes.PayloadStatus.Status)
}

return nil
}

Expand Down Expand Up @@ -109,19 +96,6 @@ 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
44 changes: 31 additions & 13 deletions driver/chain_syncer/chain_syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,7 @@ 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.
needBeaconSyncTriggered, err := s.needNewBeaconSyncTriggered()
if err != nil {
return err
}

if needBeaconSyncTriggered {
if s.needNewBeaconSyncTriggered() {
if err := s.beaconSyncer.TriggerBeaconSync(); err != nil {
return fmt.Errorf("trigger beacon sync error: %w", err)
}
Expand Down Expand Up @@ -124,17 +119,40 @@ 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 whether the current L2 execution engine needs to trigger
// another new beacon sync.
func (s *L2ChainSyncer) needNewBeaconSyncTriggered() (bool, error) {
synced, err := s.BeaconSyncer().Synced()
if err != nil {
return false, err
}
func (s *L2ChainSyncer) needNewBeaconSyncTriggered() bool {
return s.p2pSyncVerifiedBlocks &&
s.state.GetLatestVerifiedBlock().Height.Uint64() > 0 &&
!synced &&
!s.progressTracker.OutOfSync(), nil
!s.AheadOfProtocolVerifiedHead() &&
!s.progressTracker.OutOfSync()
}

// BeaconSyncer returns the inner beacon syncer.
Expand Down
4 changes: 4 additions & 0 deletions driver/chain_syncer/chain_syncer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,7 @@ func (s *ChainSyncerTestSuite) TestSync() {
func TestChainSyncerTestSuite(t *testing.T) {
suite.Run(t, new(ChainSyncerTestSuite))
}

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

0 comments on commit c57f6e8

Please sign in to comment.