eth/catalyst: adjust eta for themerge (#25601)

* eth/catalyst: adjust eta for themerge

* squash

* squash

* eth/catalyst: address review concerns
pull/25606/merge
Martin Holst Swende 2 years ago committed by GitHub
parent d10c280309
commit 362256ebff
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 85
      eth/catalyst/api.go

@ -564,16 +564,16 @@ func (api *ConsensusAPI) heartbeat() {
var ( var (
offlineLogged time.Time offlineLogged time.Time
ttd = api.eth.BlockChain().Config().TerminalTotalDifficulty
) )
// If the network is not yet merged/merging, don't bother continuing.
if ttd == nil {
return
}
for { for {
// Sleep a bit and retrieve the last known consensus updates // Sleep a bit and retrieve the last known consensus updates
time.Sleep(5 * time.Second) time.Sleep(5 * time.Second)
// If the network is not yet merged/merging, don't bother scaring the user
ttd := api.eth.BlockChain().Config().TerminalTotalDifficulty
if ttd == nil {
continue
}
api.lastTransitionLock.Lock() api.lastTransitionLock.Lock()
lastTransitionUpdate := api.lastTransitionUpdate lastTransitionUpdate := api.lastTransitionUpdate
api.lastTransitionLock.Unlock() api.lastTransitionLock.Unlock()
@ -589,7 +589,10 @@ func (api *ConsensusAPI) heartbeat() {
// If there have been no updates for the past while, warn the user // If there have been no updates for the past while, warn the user
// that the beacon client is probably offline // that the beacon client is probably offline
if api.eth.BlockChain().Config().TerminalTotalDifficultyPassed || api.eth.Merger().TDDReached() { if api.eth.BlockChain().Config().TerminalTotalDifficultyPassed || api.eth.Merger().TDDReached() {
if time.Since(lastForkchoiceUpdate) > beaconUpdateConsensusTimeout && time.Since(lastNewPayloadUpdate) > beaconUpdateConsensusTimeout { if time.Since(lastForkchoiceUpdate) <= beaconUpdateConsensusTimeout || time.Since(lastNewPayloadUpdate) <= beaconUpdateConsensusTimeout {
offlineLogged = time.Time{}
continue
}
if time.Since(lastTransitionUpdate) > beaconUpdateExchangeTimeout { if time.Since(lastTransitionUpdate) > beaconUpdateExchangeTimeout {
if time.Since(offlineLogged) > beaconUpdateWarnFrequency { if time.Since(offlineLogged) > beaconUpdateWarnFrequency {
if lastTransitionUpdate.IsZero() { if lastTransitionUpdate.IsZero() {
@ -610,68 +613,62 @@ func (api *ConsensusAPI) heartbeat() {
offlineLogged = time.Now() offlineLogged = time.Now()
} }
continue continue
} else { }
if time.Since(lastTransitionUpdate) <= beaconUpdateExchangeTimeout {
offlineLogged = time.Time{} offlineLogged = time.Time{}
continue
} }
} else {
if time.Since(lastTransitionUpdate) > beaconUpdateExchangeTimeout {
if time.Since(offlineLogged) > beaconUpdateWarnFrequency { if time.Since(offlineLogged) > beaconUpdateWarnFrequency {
// Retrieve the last few blocks and make a rough estimate as // Retrieve the last few blocks and make a rough estimate as
// to when the merge transition should happen // to when the merge transition should happen
var ( var (
chain = api.eth.BlockChain() chain = api.eth.BlockChain()
head = chain.CurrentBlock() head = chain.CurrentHeader()
htd = chain.GetTd(head.Hash(), head.NumberU64()) htd = chain.GetTd(head.Hash(), head.Number.Uint64())
eta time.Duration
) )
if head.NumberU64() > 0 && htd.Cmp(ttd) < 0 { if htd.Cmp(ttd) >= 0 {
if lastTransitionUpdate.IsZero() {
log.Warn("Merge already reached, but no beacon client seen. Please launch one to follow the chain!")
} else {
log.Warn("Merge already reached, but previously seen beacon client is offline. Please ensure it is operational to follow the chain!")
}
offlineLogged = time.Now()
continue
}
var eta time.Duration
if head.Number.Uint64() > 0 {
// Accumulate the last 64 difficulties to estimate the growth // Accumulate the last 64 difficulties to estimate the growth
var diff float64 var (
deltaDiff uint64
block := head deltaTime uint64
current = head
)
for i := 0; i < 64; i++ { for i := 0; i < 64; i++ {
diff += float64(block.Difficulty().Uint64()) parent := chain.GetHeader(current.ParentHash, current.Number.Uint64()-1)
if parent := chain.GetBlock(block.ParentHash(), block.NumberU64()-1); parent == nil { if parent == nil {
break break
} else {
block = parent
} }
deltaDiff += current.Difficulty.Uint64()
deltaTime += current.Time - parent.Time
current = parent
} }
// Estimate an ETA based on the block times and the difficulty growth // Estimate an ETA based on the block times and the difficulty growth
growth := diff / float64(head.Time()-block.Time()+1) // +1 to avoid div by zero if deltaTime > 0 {
if growth > 0 { growth := deltaDiff / deltaTime
if left := new(big.Int).Sub(ttd, htd); left.IsUint64() { left := new(big.Int).Sub(ttd, htd)
eta = time.Duration(float64(left.Uint64())/growth) * time.Second eta = time.Duration(new(big.Int).Div(left, new(big.Int).SetUint64(growth)).Uint64()) * time.Second
} else {
eta = time.Duration(new(big.Int).Div(left, big.NewInt(int64(growth))).Uint64()) * time.Second
}
} }
} }
var message string message := "Merge is configured, but previously seen beacon client is offline. Please ensure it is operational before the transition arrives!"
if htd.Cmp(ttd) > 0 {
if lastTransitionUpdate.IsZero() {
message = "Merge already reached, but no beacon client seen. Please launch one to follow the chain!"
} else {
message = "Merge already reached, but previously seen beacon client is offline. Please ensure it is operational to follow the chain!"
}
} else {
if lastTransitionUpdate.IsZero() { if lastTransitionUpdate.IsZero() {
message = "Merge is configured, but no beacon client seen. Please ensure you have one available before the transition arrives!" message = "Merge is configured, but no beacon client seen. Please ensure you have one available before the transition arrives!"
} else {
message = "Merge is configured, but previously seen beacon client is offline. Please ensure it is operational before the transition arrives!"
} }
} if eta < time.Second {
if eta == 0 {
log.Warn(message) log.Warn(message)
} else { } else {
log.Warn(message, "eta", common.PrettyAge(time.Now().Add(-eta))) // weird hack, but duration formatted doesn't handle days log.Warn(message, "eta", common.PrettyAge(time.Now().Add(-eta))) // weird hack, but duration formatted doesn't handle days
} }
offlineLogged = time.Now() offlineLogged = time.Now()
} }
continue
} else {
offlineLogged = time.Time{}
}
}
} }
} }

Loading…
Cancel
Save