@@ -564,16 +564,16 @@ func (api *ConsensusAPI) heartbeat() {
564564
565565 var (
566566 offlineLogged time.Time
567+ ttd = api .eth .BlockChain ().Config ().TerminalTotalDifficulty
567568 )
569+ // If the network is not yet merged/merging, don't bother continuing.
570+ if ttd == nil {
571+ return
572+ }
568573 for {
569574 // Sleep a bit and retrieve the last known consensus updates
570575 time .Sleep (5 * time .Second )
571576
572- // If the network is not yet merged/merging, don't bother scaring the user
573- ttd := api .eth .BlockChain ().Config ().TerminalTotalDifficulty
574- if ttd == nil {
575- continue
576- }
577577 api .lastTransitionLock .Lock ()
578578 lastTransitionUpdate := api .lastTransitionUpdate
579579 api .lastTransitionLock .Unlock ()
@@ -589,89 +589,86 @@ func (api *ConsensusAPI) heartbeat() {
589589 // If there have been no updates for the past while, warn the user
590590 // that the beacon client is probably offline
591591 if api .eth .BlockChain ().Config ().TerminalTotalDifficultyPassed || api .eth .Merger ().TDDReached () {
592- if time .Since (lastForkchoiceUpdate ) > beaconUpdateConsensusTimeout && time .Since (lastNewPayloadUpdate ) > beaconUpdateConsensusTimeout {
593- if time .Since (lastTransitionUpdate ) > beaconUpdateExchangeTimeout {
594- if time .Since (offlineLogged ) > beaconUpdateWarnFrequency {
595- if lastTransitionUpdate .IsZero () {
596- log .Warn ("Post-merge network, but no beacon client seen. Please launch one to follow the chain!" )
597- } else {
598- log .Warn ("Previously seen beacon client is offline. Please ensure it is operational to follow the chain!" )
599- }
600- offlineLogged = time .Now ()
601- }
602- continue
603- }
604- if time .Since (offlineLogged ) > beaconUpdateWarnFrequency {
605- if lastForkchoiceUpdate .IsZero () && lastNewPayloadUpdate .IsZero () {
606- log .Warn ("Beacon client online, but never received consensus updates. Please ensure your beacon client is operational to follow the chain!" )
607- } else {
608- log .Warn ("Beacon client online, but no consensus updates received in a while. Please fix your beacon client to follow the chain!" )
609- }
610- offlineLogged = time .Now ()
611- }
612- continue
613- } else {
592+ if time .Since (lastForkchoiceUpdate ) <= beaconUpdateConsensusTimeout || time .Since (lastNewPayloadUpdate ) <= beaconUpdateConsensusTimeout {
614593 offlineLogged = time.Time {}
594+ continue
615595 }
616- } else {
617596 if time .Since (lastTransitionUpdate ) > beaconUpdateExchangeTimeout {
618597 if time .Since (offlineLogged ) > beaconUpdateWarnFrequency {
619- // Retrieve the last few blocks and make a rough estimate as
620- // to when the merge transition should happen
621- var (
622- chain = api .eth .BlockChain ()
623- head = chain .CurrentBlock ()
624- htd = chain .GetTd (head .Hash (), head .NumberU64 ())
625- eta time.Duration
626- )
627- if head .NumberU64 () > 0 && htd .Cmp (ttd ) < 0 {
628- // Accumulate the last 64 difficulties to estimate the growth
629- var diff float64
630-
631- block := head
632- for i := 0 ; i < 64 ; i ++ {
633- diff += float64 (block .Difficulty ().Uint64 ())
634- if parent := chain .GetBlock (block .ParentHash (), block .NumberU64 ()- 1 ); parent == nil {
635- break
636- } else {
637- block = parent
638- }
639- }
640- // Estimate an ETA based on the block times and the difficulty growth
641- growth := diff / float64 (head .Time ()- block .Time ()+ 1 ) // +1 to avoid div by zero
642- if growth > 0 {
643- if left := new (big.Int ).Sub (ttd , htd ); left .IsUint64 () {
644- eta = time .Duration (float64 (left .Uint64 ())/ growth ) * time .Second
645- } else {
646- eta = time .Duration (new (big.Int ).Div (left , big .NewInt (int64 (growth ))).Uint64 ()) * time .Second
647- }
648- }
649- }
650- var message string
651- if htd .Cmp (ttd ) > 0 {
652- if lastTransitionUpdate .IsZero () {
653- message = "Merge already reached, but no beacon client seen. Please launch one to follow the chain!"
654- } else {
655- message = "Merge already reached, but previously seen beacon client is offline. Please ensure it is operational to follow the chain!"
656- }
657- } else {
658- if lastTransitionUpdate .IsZero () {
659- message = "Merge is configured, but no beacon client seen. Please ensure you have one available before the transition arrives!"
660- } else {
661- message = "Merge is configured, but previously seen beacon client is offline. Please ensure it is operational before the transition arrives!"
662- }
663- }
664- if eta == 0 {
665- log .Warn (message )
598+ if lastTransitionUpdate .IsZero () {
599+ log .Warn ("Post-merge network, but no beacon client seen. Please launch one to follow the chain!" )
666600 } else {
667- log .Warn (message , "eta" , common . PrettyAge ( time . Now (). Add ( - eta ))) // weird hack, but duration formatted doesn't handle days
601+ log .Warn ("Previously seen beacon client is offline. Please ensure it is operational to follow the chain!" )
668602 }
669603 offlineLogged = time .Now ()
670604 }
671605 continue
606+ }
607+ if time .Since (offlineLogged ) > beaconUpdateWarnFrequency {
608+ if lastForkchoiceUpdate .IsZero () && lastNewPayloadUpdate .IsZero () {
609+ log .Warn ("Beacon client online, but never received consensus updates. Please ensure your beacon client is operational to follow the chain!" )
610+ } else {
611+ log .Warn ("Beacon client online, but no consensus updates received in a while. Please fix your beacon client to follow the chain!" )
612+ }
613+ offlineLogged = time .Now ()
614+ }
615+ continue
616+ }
617+ if time .Since (lastTransitionUpdate ) <= beaconUpdateExchangeTimeout {
618+ offlineLogged = time.Time {}
619+ continue
620+ }
621+ if time .Since (offlineLogged ) > beaconUpdateWarnFrequency {
622+ // Retrieve the last few blocks and make a rough estimate as
623+ // to when the merge transition should happen
624+ var (
625+ chain = api .eth .BlockChain ()
626+ head = chain .CurrentHeader ()
627+ htd = chain .GetTd (head .Hash (), head .Number .Uint64 ())
628+ )
629+ if htd .Cmp (ttd ) >= 0 {
630+ if lastTransitionUpdate .IsZero () {
631+ log .Warn ("Merge already reached, but no beacon client seen. Please launch one to follow the chain!" )
632+ } else {
633+ log .Warn ("Merge already reached, but previously seen beacon client is offline. Please ensure it is operational to follow the chain!" )
634+ }
635+ offlineLogged = time .Now ()
636+ continue
637+ }
638+ var eta time.Duration
639+ if head .Number .Uint64 () > 0 {
640+ // Accumulate the last 64 difficulties to estimate the growth
641+ var (
642+ deltaDiff uint64
643+ deltaTime uint64
644+ current = head
645+ )
646+ for i := 0 ; i < 64 ; i ++ {
647+ parent := chain .GetHeader (current .ParentHash , current .Number .Uint64 ()- 1 )
648+ if parent == nil {
649+ break
650+ }
651+ deltaDiff += current .Difficulty .Uint64 ()
652+ deltaTime += current .Time - parent .Time
653+ current = parent
654+ }
655+ // Estimate an ETA based on the block times and the difficulty growth
656+ if deltaTime > 0 {
657+ growth := deltaDiff / deltaTime
658+ left := new (big.Int ).Sub (ttd , htd )
659+ eta = time .Duration (new (big.Int ).Div (left , new (big.Int ).SetUint64 (growth )).Uint64 ()) * time .Second
660+ }
661+ }
662+ message := "Merge is configured, but previously seen beacon client is offline. Please ensure it is operational before the transition arrives!"
663+ if lastTransitionUpdate .IsZero () {
664+ message = "Merge is configured, but no beacon client seen. Please ensure you have one available before the transition arrives!"
665+ }
666+ if eta < time .Second {
667+ log .Warn (message )
672668 } else {
673- offlineLogged = time.Time {}
669+ log . Warn ( message , "eta" , common . PrettyAge ( time .Now (). Add ( - eta ))) // weird hack, but duration formatted doesn't handle days
674670 }
671+ offlineLogged = time .Now ()
675672 }
676673 }
677674}
0 commit comments