From 92e8a64e558c26e1d622b0d87b9b24787f7d3e2c Mon Sep 17 00:00:00 2001 From: Martin Holst Swende Date: Thu, 25 Aug 2022 09:33:01 +0200 Subject: [PATCH 1/4] eth/catalyst: adjust eta for themerge --- eth/catalyst/api.go | 60 ++++++++++++++++++++++----------------------- 1 file changed, 29 insertions(+), 31 deletions(-) diff --git a/eth/catalyst/api.go b/eth/catalyst/api.go index b159f34e64ba..e913269a3939 100644 --- a/eth/catalyst/api.go +++ b/eth/catalyst/api.go @@ -620,48 +620,46 @@ func (api *ConsensusAPI) heartbeat() { // to when the merge transition should happen var ( chain = api.eth.BlockChain() - head = chain.CurrentBlock() - htd = chain.GetTd(head.Hash(), head.NumberU64()) - eta time.Duration + head = chain.CurrentHeader() + htd = chain.GetTd(head.Hash(), head.Number.Uint64()) ) - 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 && htd.Cmp(ttd) < 0 { // Accumulate the last 64 difficulties to estimate the growth - var diff float64 + var deltaDifficulty uint64 + var deltaTime uint64 - block := head + hdr := head for i := 0; i < 64; i++ { - diff += float64(block.Difficulty().Uint64()) - if parent := chain.GetBlock(block.ParentHash(), block.NumberU64()-1); parent == nil { + parent := chain.GetHeader(hdr.ParentHash, hdr.Number.Uint64()-1) + if parent == nil { break - } else { - block = parent } + deltaDifficulty += hdr.Difficulty.Uint64() + deltaTime += hdr.Time - parent.Time + hdr = parent } // 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 growth > 0 { - if left := new(big.Int).Sub(ttd, htd); left.IsUint64() { - eta = time.Duration(float64(left.Uint64())/growth) * time.Second - } else { - eta = time.Duration(new(big.Int).Div(left, big.NewInt(int64(growth))).Uint64()) * time.Second - } + if deltaTime > 0 { + growth := deltaDifficulty / deltaTime + left := new(big.Int).Sub(ttd, htd) + eta = time.Duration(new(big.Int).Div(left, new(big.Int).SetUint64(growth)).Uint64()) * time.Second } } - var message string - 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() { - 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!" - } + message := "Merge is configured, but previously seen beacon client is offline. Please ensure it is operational before the transition arrives!" + if lastTransitionUpdate.IsZero() { + message = "Merge is configured, but no beacon client seen. Please ensure you have one available before the transition arrives!" } - if eta == 0 { + if eta < time.Second { log.Warn(message) } else { log.Warn(message, "eta", common.PrettyAge(time.Now().Add(-eta))) // weird hack, but duration formatted doesn't handle days From 1891de459fa74798cd4a85ccb9b31dff9199399a Mon Sep 17 00:00:00 2001 From: Martin Holst Swende Date: Thu, 25 Aug 2022 09:47:24 +0200 Subject: [PATCH 2/4] squash --- eth/catalyst/api.go | 98 ++++++++++++++++++++++----------------------- 1 file changed, 48 insertions(+), 50 deletions(-) diff --git a/eth/catalyst/api.go b/eth/catalyst/api.go index e913269a3939..4e99b907ce7a 100644 --- a/eth/catalyst/api.go +++ b/eth/catalyst/api.go @@ -614,61 +614,59 @@ func (api *ConsensusAPI) heartbeat() { offlineLogged = time.Time{} } } else { - if time.Since(lastTransitionUpdate) > beaconUpdateExchangeTimeout { - if time.Since(offlineLogged) > beaconUpdateWarnFrequency { - // Retrieve the last few blocks and make a rough estimate as - // to when the merge transition should happen - var ( - chain = api.eth.BlockChain() - head = chain.CurrentHeader() - htd = chain.GetTd(head.Hash(), head.Number.Uint64()) - ) - 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 && htd.Cmp(ttd) < 0 { - // Accumulate the last 64 difficulties to estimate the growth - var deltaDifficulty uint64 - var deltaTime uint64 - - hdr := head - for i := 0; i < 64; i++ { - parent := chain.GetHeader(hdr.ParentHash, hdr.Number.Uint64()-1) - if parent == nil { - break - } - deltaDifficulty += hdr.Difficulty.Uint64() - deltaTime += hdr.Time - parent.Time - hdr = parent - } - // Estimate an ETA based on the block times and the difficulty growth - if deltaTime > 0 { - growth := deltaDifficulty / deltaTime - left := new(big.Int).Sub(ttd, htd) - eta = time.Duration(new(big.Int).Div(left, new(big.Int).SetUint64(growth)).Uint64()) * time.Second - } - } - message := "Merge is configured, but previously seen beacon client is offline. Please ensure it is operational before the transition arrives!" + if time.Since(lastTransitionUpdate) <= beaconUpdateExchangeTimeout { + offlineLogged = time.Time{} + continue + } + if time.Since(offlineLogged) > beaconUpdateWarnFrequency { + // Retrieve the last few blocks and make a rough estimate as + // to when the merge transition should happen + var ( + chain = api.eth.BlockChain() + head = chain.CurrentHeader() + htd = chain.GetTd(head.Hash(), head.Number.Uint64()) + ) + if htd.Cmp(ttd) >= 0 { if lastTransitionUpdate.IsZero() { - message = "Merge is configured, but no beacon client seen. Please ensure you have one available before the transition arrives!" - } - if eta < time.Second { - log.Warn(message) + log.Warn("Merge already reached, but no beacon client seen. Please launch one to follow the chain!") } else { - log.Warn(message, "eta", common.PrettyAge(time.Now().Add(-eta))) // weird hack, but duration formatted doesn't handle days + 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 } - continue - } else { - offlineLogged = time.Time{} + var eta time.Duration + if head.Number.Uint64() > 0 && htd.Cmp(ttd) < 0 { + // Accumulate the last 64 difficulties to estimate the growth + var deltaDifficulty uint64 + var deltaTime uint64 + hdr := head + for i := 0; i < 64; i++ { + parent := chain.GetHeader(hdr.ParentHash, hdr.Number.Uint64()-1) + if parent == nil { + break + } + deltaDifficulty += hdr.Difficulty.Uint64() + deltaTime += hdr.Time - parent.Time + hdr = parent + } + // Estimate an ETA based on the block times and the difficulty growth + if deltaTime > 0 { + growth := deltaDifficulty / deltaTime + left := new(big.Int).Sub(ttd, htd) + eta = time.Duration(new(big.Int).Div(left, new(big.Int).SetUint64(growth)).Uint64()) * time.Second + } + } + message := "Merge is configured, but previously seen beacon client is offline. Please ensure it is operational before the transition arrives!" + if lastTransitionUpdate.IsZero() { + message = "Merge is configured, but no beacon client seen. Please ensure you have one available before the transition arrives!" + } + if eta < time.Second { + log.Warn(message) + } else { + log.Warn(message, "eta", common.PrettyAge(time.Now().Add(-eta))) // weird hack, but duration formatted doesn't handle days + } + offlineLogged = time.Now() } } } From ea1652b7c9e6dee5448675b396f9012edd443df0 Mon Sep 17 00:00:00 2001 From: Martin Holst Swende Date: Thu, 25 Aug 2022 09:57:29 +0200 Subject: [PATCH 3/4] squash --- eth/catalyst/api.go | 139 ++++++++++++++++++++++---------------------- 1 file changed, 69 insertions(+), 70 deletions(-) diff --git a/eth/catalyst/api.go b/eth/catalyst/api.go index 4e99b907ce7a..514ab68f5467 100644 --- a/eth/catalyst/api.go +++ b/eth/catalyst/api.go @@ -564,16 +564,16 @@ func (api *ConsensusAPI) heartbeat() { var ( 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 { // Sleep a bit and retrieve the last known consensus updates 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() lastTransitionUpdate := api.lastTransitionUpdate api.lastTransitionLock.Unlock() @@ -589,85 +589,84 @@ func (api *ConsensusAPI) heartbeat() { // If there have been no updates for the past while, warn the user // that the beacon client is probably offline if api.eth.BlockChain().Config().TerminalTotalDifficultyPassed || api.eth.Merger().TDDReached() { - if time.Since(lastForkchoiceUpdate) > beaconUpdateConsensusTimeout && time.Since(lastNewPayloadUpdate) > beaconUpdateConsensusTimeout { - if time.Since(lastTransitionUpdate) > beaconUpdateExchangeTimeout { - if time.Since(offlineLogged) > beaconUpdateWarnFrequency { - if lastTransitionUpdate.IsZero() { - log.Warn("Post-merge network, but no beacon client seen. Please launch one to follow the chain!") - } else { - log.Warn("Previously seen beacon client is offline. Please ensure it is operational to follow the chain!") - } - offlineLogged = time.Now() - } - continue - } - if time.Since(offlineLogged) > beaconUpdateWarnFrequency { - if lastForkchoiceUpdate.IsZero() && lastNewPayloadUpdate.IsZero() { - log.Warn("Beacon client online, but never received consensus updates. Please ensure your beacon client is operational to follow the chain!") - } else { - log.Warn("Beacon client online, but no consensus updates received in a while. Please fix your beacon client to follow the chain!") - } - offlineLogged = time.Now() - } - continue - } else { - offlineLogged = time.Time{} - } - } else { - if time.Since(lastTransitionUpdate) <= beaconUpdateExchangeTimeout { + if time.Since(lastForkchoiceUpdate) <= beaconUpdateConsensusTimeout || time.Since(lastNewPayloadUpdate) <= beaconUpdateConsensusTimeout { offlineLogged = time.Time{} continue } - if time.Since(offlineLogged) > beaconUpdateWarnFrequency { - // Retrieve the last few blocks and make a rough estimate as - // to when the merge transition should happen - var ( - chain = api.eth.BlockChain() - head = chain.CurrentHeader() - htd = chain.GetTd(head.Hash(), head.Number.Uint64()) - ) - if htd.Cmp(ttd) >= 0 { + if time.Since(lastTransitionUpdate) > beaconUpdateExchangeTimeout { + if time.Since(offlineLogged) > beaconUpdateWarnFrequency { if lastTransitionUpdate.IsZero() { - log.Warn("Merge already reached, but no beacon client seen. Please launch one to follow the chain!") + log.Warn("Post-merge network, 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!") + log.Warn("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 && htd.Cmp(ttd) < 0 { - // Accumulate the last 64 difficulties to estimate the growth - var deltaDifficulty uint64 - var deltaTime uint64 - hdr := head - for i := 0; i < 64; i++ { - parent := chain.GetHeader(hdr.ParentHash, hdr.Number.Uint64()-1) - if parent == nil { - break - } - deltaDifficulty += hdr.Difficulty.Uint64() - deltaTime += hdr.Time - parent.Time - hdr = parent - } - // Estimate an ETA based on the block times and the difficulty growth - if deltaTime > 0 { - growth := deltaDifficulty / deltaTime - left := new(big.Int).Sub(ttd, htd) - eta = time.Duration(new(big.Int).Div(left, new(big.Int).SetUint64(growth)).Uint64()) * time.Second - } + continue + } + if time.Since(offlineLogged) > beaconUpdateWarnFrequency { + if lastForkchoiceUpdate.IsZero() && lastNewPayloadUpdate.IsZero() { + log.Warn("Beacon client online, but never received consensus updates. Please ensure your beacon client is operational to follow the chain!") + } else { + log.Warn("Beacon client online, but no consensus updates received in a while. Please fix your beacon client to follow the chain!") } - message := "Merge is configured, but previously seen beacon client is offline. Please ensure it is operational before the transition arrives!" + offlineLogged = time.Now() + } + continue + } + if time.Since(lastTransitionUpdate) <= beaconUpdateExchangeTimeout { + offlineLogged = time.Time{} + continue + } + if time.Since(offlineLogged) > beaconUpdateWarnFrequency { + // Retrieve the last few blocks and make a rough estimate as + // to when the merge transition should happen + var ( + chain = api.eth.BlockChain() + head = chain.CurrentHeader() + htd = chain.GetTd(head.Hash(), head.Number.Uint64()) + ) + if htd.Cmp(ttd) >= 0 { if lastTransitionUpdate.IsZero() { - message = "Merge is configured, but no beacon client seen. Please ensure you have one available before the transition arrives!" - } - if eta < time.Second { - log.Warn(message) + log.Warn("Merge already reached, but no beacon client seen. Please launch one to follow the chain!") } else { - log.Warn(message, "eta", common.PrettyAge(time.Now().Add(-eta))) // weird hack, but duration formatted doesn't handle days + 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 && htd.Cmp(ttd) < 0 { + // Accumulate the last 64 difficulties to estimate the growth + var deltaDifficulty uint64 + var deltaTime uint64 + hdr := head + for i := 0; i < 64; i++ { + parent := chain.GetHeader(hdr.ParentHash, hdr.Number.Uint64()-1) + if parent == nil { + break + } + deltaDifficulty += hdr.Difficulty.Uint64() + deltaTime += hdr.Time - parent.Time + hdr = parent + } + // Estimate an ETA based on the block times and the difficulty growth + if deltaTime > 0 { + growth := deltaDifficulty / deltaTime + left := new(big.Int).Sub(ttd, htd) + eta = time.Duration(new(big.Int).Div(left, new(big.Int).SetUint64(growth)).Uint64()) * time.Second + } + } + message := "Merge is configured, but previously seen beacon client is offline. Please ensure it is operational before the transition arrives!" + if lastTransitionUpdate.IsZero() { + message = "Merge is configured, but no beacon client seen. Please ensure you have one available before the transition arrives!" + } + if eta < time.Second { + log.Warn(message) + } else { + log.Warn(message, "eta", common.PrettyAge(time.Now().Add(-eta))) // weird hack, but duration formatted doesn't handle days } + offlineLogged = time.Now() } } } From 42930aec5dd86e24d8b562c60f2537925f97ff85 Mon Sep 17 00:00:00 2001 From: Martin Holst Swende Date: Wed, 31 Aug 2022 12:25:20 +0200 Subject: [PATCH 4/4] eth/catalyst: address review concerns --- eth/catalyst/api.go | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/eth/catalyst/api.go b/eth/catalyst/api.go index 514ab68f5467..19dd28e643ca 100644 --- a/eth/catalyst/api.go +++ b/eth/catalyst/api.go @@ -636,23 +636,25 @@ func (api *ConsensusAPI) heartbeat() { continue } var eta time.Duration - if head.Number.Uint64() > 0 && htd.Cmp(ttd) < 0 { + if head.Number.Uint64() > 0 { // Accumulate the last 64 difficulties to estimate the growth - var deltaDifficulty uint64 - var deltaTime uint64 - hdr := head + var ( + deltaDiff uint64 + deltaTime uint64 + current = head + ) for i := 0; i < 64; i++ { - parent := chain.GetHeader(hdr.ParentHash, hdr.Number.Uint64()-1) + parent := chain.GetHeader(current.ParentHash, current.Number.Uint64()-1) if parent == nil { break } - deltaDifficulty += hdr.Difficulty.Uint64() - deltaTime += hdr.Time - parent.Time - hdr = parent + deltaDiff += current.Difficulty.Uint64() + deltaTime += current.Time - parent.Time + current = parent } // Estimate an ETA based on the block times and the difficulty growth if deltaTime > 0 { - growth := deltaDifficulty / deltaTime + growth := deltaDiff / deltaTime left := new(big.Int).Sub(ttd, htd) eta = time.Duration(new(big.Int).Div(left, new(big.Int).SetUint64(growth)).Uint64()) * time.Second }