From a61c0485ac15305671e84f51a7c4cc8551f789c8 Mon Sep 17 00:00:00 2001 From: jsvisa Date: Sat, 10 May 2025 08:51:13 +0800 Subject: [PATCH 1/3] core: use unix time to check fork readiness Signed-off-by: jsvisa --- core/blockchain.go | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/core/blockchain.go b/core/blockchain.go index 3c691600eb78..bf19d034f5c8 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -1838,7 +1838,7 @@ func (bc *BlockChain) insertChain(chain types.Blocks, setHead bool, makeWitness stats.report(chain, it.index, snapDiffItems, snapBufItems, trieDiffNodes, trieBufNodes, setHead) // Print confirmation that a future fork is scheduled, but not yet active. - bc.logForkReadiness(block) + bc.logForkReadiness() if !setHead { // After merge we expect few side chains. Simply count @@ -2541,15 +2541,21 @@ func (bc *BlockChain) reportBlock(block *types.Block, res *ProcessResult, err er // logForkReadiness will write a log when a future fork is scheduled, but not // active. This is useful so operators know their client is ready for the fork. -func (bc *BlockChain) logForkReadiness(block *types.Block) { +func (bc *BlockChain) logForkReadiness() { c := bc.Config() - current, last := c.LatestFork(block.Time()), c.LatestFork(math.MaxUint64) + now := time.Now() + current, last := c.LatestFork(uint64(now.Unix())), c.LatestFork(math.MaxUint64) t := c.Timestamp(last) if t == nil { return } at := time.Unix(int64(*t), 0) - if current < last && time.Now().After(bc.lastForkReadyAlert.Add(forkReadyInterval)) { + + // Only log if: + // 1. Current fork is behind the latest fork + // 2. Current time is before the fork activation time + // 3. Enough time has passed since last alert + if current < last && now.Before(at) && now.After(bc.lastForkReadyAlert.Add(forkReadyInterval)) { log.Info("Ready for fork activation", "fork", last, "date", at.Format(time.RFC822), "remaining", time.Until(at).Round(time.Second), "timestamp", at.Unix()) bc.lastForkReadyAlert = time.Now() From 78f154b170d7866b553a3243557dd40f75429cb4 Mon Sep 17 00:00:00 2001 From: Gary Rong Date: Tue, 13 May 2025 19:52:58 +0800 Subject: [PATCH 2/3] core: polish --- core/blockchain.go | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/core/blockchain.go b/core/blockchain.go index bf19d034f5c8..54663aaeeb86 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -2541,21 +2541,23 @@ func (bc *BlockChain) reportBlock(block *types.Block, res *ProcessResult, err er // logForkReadiness will write a log when a future fork is scheduled, but not // active. This is useful so operators know their client is ready for the fork. -func (bc *BlockChain) logForkReadiness() { - c := bc.Config() - now := time.Now() - current, last := c.LatestFork(uint64(now.Unix())), c.LatestFork(math.MaxUint64) - t := c.Timestamp(last) - if t == nil { +func (bc *BlockChain) logForkReadiness(block *types.Block) { + config := bc.Config() + current, last := config.LatestFork(block.Time()), config.LatestFork(math.MaxUint64) + + // Short circuit if the timestamp of the last fork is undefined, + // or if the network has already passed the last configured fork. + t := config.Timestamp(last) + if t == nil || current >= last { return } at := time.Unix(int64(*t), 0) // Only log if: - // 1. Current fork is behind the latest fork - // 2. Current time is before the fork activation time - // 3. Enough time has passed since last alert - if current < last && now.Before(at) && now.After(bc.lastForkReadyAlert.Add(forkReadyInterval)) { + // - Current time is before the fork activation time + // - Enough time has passed since last alert + now := time.Now() + if now.Before(at) && now.After(bc.lastForkReadyAlert.Add(forkReadyInterval)) { log.Info("Ready for fork activation", "fork", last, "date", at.Format(time.RFC822), "remaining", time.Until(at).Round(time.Second), "timestamp", at.Unix()) bc.lastForkReadyAlert = time.Now() From 15eef92db54b4e1181444e78a667f45eef0e472a Mon Sep 17 00:00:00 2001 From: Gary Rong Date: Tue, 13 May 2025 19:54:48 +0800 Subject: [PATCH 3/3] core: fix compile --- core/blockchain.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/blockchain.go b/core/blockchain.go index 54663aaeeb86..57307f54210b 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -1838,7 +1838,7 @@ func (bc *BlockChain) insertChain(chain types.Blocks, setHead bool, makeWitness stats.report(chain, it.index, snapDiffItems, snapBufItems, trieDiffNodes, trieBufNodes, setHead) // Print confirmation that a future fork is scheduled, but not yet active. - bc.logForkReadiness() + bc.logForkReadiness(block) if !setHead { // After merge we expect few side chains. Simply count