Skip to content

Commit 62eda99

Browse files
committed
core,params: add fork readiness indicator in logs
1 parent 7405dc5 commit 62eda99

File tree

3 files changed

+77
-1
lines changed

3 files changed

+77
-1
lines changed

core/blockchain.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"errors"
2222
"fmt"
2323
"io"
24+
"math"
2425
"math/big"
2526
"runtime"
2627
"slices"
@@ -98,6 +99,10 @@ var (
9899
errInvalidNewChain = errors.New("invalid new chain")
99100
)
100101

102+
var (
103+
forkReadyInterval = 3 * time.Minute
104+
)
105+
101106
const (
102107
bodyCacheLimit = 256
103108
blockCacheLimit = 256
@@ -268,6 +273,8 @@ type BlockChain struct {
268273
processor Processor // Block transaction processor interface
269274
vmConfig vm.Config
270275
logger *tracing.Hooks
276+
277+
lastForkReadyAlert time.Time // Last time there was a fork readiness print out
271278
}
272279

273280
// NewBlockChain returns a fully initialised block chain using information
@@ -1825,6 +1832,9 @@ func (bc *BlockChain) insertChain(chain types.Blocks, setHead bool, makeWitness
18251832
trieDiffNodes, trieBufNodes, _ := bc.triedb.Size()
18261833
stats.report(chain, it.index, snapDiffItems, snapBufItems, trieDiffNodes, trieBufNodes, setHead)
18271834

1835+
// Print confirmation that a future fork is scheduled, but not yet active.
1836+
bc.logForkReadiness(block)
1837+
18281838
if !setHead {
18291839
// After merge we expect few side chains. Simply count
18301840
// all blocks the CL gives us for GC processing time
@@ -1858,6 +1868,7 @@ func (bc *BlockChain) insertChain(chain types.Blocks, setHead bool, makeWitness
18581868
"root", block.Root())
18591869
}
18601870
}
1871+
18611872
stats.ignored += it.remaining()
18621873
return witness, it.index, err
18631874
}
@@ -2455,6 +2466,22 @@ func (bc *BlockChain) reportBlock(block *types.Block, res *ProcessResult, err er
24552466
log.Error(summarizeBadBlock(block, receipts, bc.Config(), err))
24562467
}
24572468

2469+
// logForkReadiness will write a log when a future fork is scheduled, but not
2470+
// active. This is useful so operators know their client is ready for the fork.
2471+
func (bc *BlockChain) logForkReadiness(block *types.Block) {
2472+
c := bc.Config()
2473+
current, last := c.LatestFork(block.Time()), c.LatestFork(math.MaxUint64)
2474+
var at time.Time
2475+
if t := c.Timestamp(last); t != nil {
2476+
at = time.Unix(int64(*t), 0)
2477+
}
2478+
if current < last && time.Now().After(bc.lastForkReadyAlert.Add(forkReadyInterval)) {
2479+
log.Info("Ready for fork activation", "fork", last, "date", at.Format(time.RFC822),
2480+
"remaining", time.Until(at).Round(time.Second), "timestamp", at.Unix())
2481+
bc.lastForkReadyAlert = time.Now()
2482+
}
2483+
}
2484+
24582485
// summarizeBadBlock returns a string summarizing the bad block and other
24592486
// relevant information.
24602487
func summarizeBadBlock(block *types.Block, receipts []*types.Receipt, config *params.ChainConfig, err error) string {

params/config.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -875,6 +875,23 @@ func (c *ChainConfig) LatestFork(time uint64) forks.Fork {
875875
}
876876
}
877877

878+
// Timestamp returns the timestamp associated with the fork or returns nil if
879+
// the fork isn't defined or isn't a time-based fork.
880+
func (c *ChainConfig) Timestamp(fork forks.Fork) *uint64 {
881+
switch {
882+
case fork == forks.Osaka:
883+
return c.OsakaTime
884+
case fork == forks.Prague:
885+
return c.PragueTime
886+
case fork == forks.Cancun:
887+
return c.CancunTime
888+
case fork == forks.Shanghai:
889+
return c.ShanghaiTime
890+
default:
891+
panic("unknown timestamp fork requested")
892+
}
893+
}
894+
878895
// isForkBlockIncompatible returns true if a fork scheduled at block s1 cannot be
879896
// rescheduled to block s2 because head is already past the fork.
880897
func isForkBlockIncompatible(s1, s2, head *big.Int) bool {

params/forks/forks.go

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ package forks
2020
type Fork int
2121

2222
const (
23-
Frontier = iota
23+
Frontier Fork = iota
2424
FrontierThawing
2525
Homestead
2626
DAO
@@ -41,3 +41,35 @@ const (
4141
Prague
4242
Osaka
4343
)
44+
45+
// String implements fmt.Stringer.
46+
func (f Fork) String() string {
47+
s, ok := forkToString[f]
48+
if !ok {
49+
return "Unknown fork"
50+
}
51+
return s
52+
}
53+
54+
var forkToString = map[Fork]string{
55+
Frontier: "Frontier",
56+
FrontierThawing: "Frontier Thawing",
57+
Homestead: "Homestead",
58+
DAO: "DAO",
59+
TangerineWhistle: "Tangerine Whistle",
60+
SpuriousDragon: "Spurious Dragon",
61+
Byzantium: "Byzantium",
62+
Constantinople: "Constantinople",
63+
Petersburg: "Petersburg",
64+
Istanbul: "Istanbul",
65+
MuirGlacier: "Muir Glacier",
66+
Berlin: "Berlin",
67+
London: "London",
68+
ArrowGlacier: "Arrow Glacier",
69+
GrayGlacier: "Gray Glacier",
70+
Paris: "Paris",
71+
Shanghai: "Shanghai",
72+
Cancun: "Cancun",
73+
Prague: "Prague",
74+
Osaka: "Osaka",
75+
}

0 commit comments

Comments
 (0)