Skip to content

Commit 1cd8782

Browse files
MariusVanDerWijdenkaralabe
authored andcommitted
all: implement forkid changes for shanghai
1 parent ba292b2 commit 1cd8782

File tree

11 files changed

+153
-29
lines changed

11 files changed

+153
-29
lines changed

cmd/devp2p/internal/ethtest/chain.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ func (c *Chain) RootAt(height int) common.Hash {
7676

7777
// ForkID gets the fork id of the chain.
7878
func (c *Chain) ForkID() forkid.ID {
79-
return forkid.NewID(c.chainConfig, c.blocks[0].Hash(), uint64(c.Len()))
79+
return forkid.NewID(c.chainConfig, c.blocks[0].Hash(), uint64(c.Len()), c.blocks[0].Time())
8080
}
8181

8282
// Shorten returns a copy chain of a desired height from the imported

core/forkid/forkid.go

Lines changed: 43 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"math"
2525
"math/big"
2626
"reflect"
27+
"sort"
2728
"strings"
2829

2930
"github.com/ethereum/go-ethereum/common"
@@ -65,19 +66,28 @@ type ID struct {
6566
// Filter is a fork id filter to validate a remotely advertised ID.
6667
type Filter func(id ID) error
6768

68-
// NewID calculates the Ethereum fork ID from the chain config, genesis hash, and head.
69-
func NewID(config *params.ChainConfig, genesis common.Hash, head uint64) ID {
69+
// NewID calculates the Ethereum fork ID from the chain config, genesis hash, head and time.
70+
func NewID(config *params.ChainConfig, genesis common.Hash, head, time uint64) ID {
7071
// Calculate the starting checksum from the genesis hash
7172
hash := crc32.ChecksumIEEE(genesis[:])
7273

7374
// Calculate the current fork checksum and the next fork block
74-
var next uint64
75-
for _, fork := range gatherForks(config) {
75+
forks, forksByTime := gatherForks(config)
76+
for _, fork := range forks {
7677
if fork <= head {
7778
// Fork already passed, checksum the previous hash and the fork number
7879
hash = checksumUpdate(hash, fork)
7980
continue
8081
}
82+
return ID{Hash: checksumToBytes(hash), Next: fork}
83+
}
84+
var next uint64
85+
for _, fork := range forksByTime {
86+
if time >= fork {
87+
// Fork passed, checksum previous hash and fork time
88+
hash = checksumUpdate(hash, fork)
89+
continue
90+
}
8191
next = fork
8292
break
8393
}
@@ -90,6 +100,7 @@ func NewIDWithChain(chain Blockchain) ID {
90100
chain.Config(),
91101
chain.Genesis().Hash(),
92102
chain.CurrentHeader().Number.Uint64(),
103+
chain.CurrentHeader().Time,
93104
)
94105
}
95106

@@ -117,9 +128,10 @@ func NewStaticFilter(config *params.ChainConfig, genesis common.Hash) Filter {
117128
func newFilter(config *params.ChainConfig, genesis common.Hash, headfn func() uint64) Filter {
118129
// Calculate the all the valid fork hash and fork next combos
119130
var (
120-
forks = gatherForks(config)
121-
sums = make([][4]byte, len(forks)+1) // 0th is the genesis
131+
forks, forksByTime = gatherForks(config)
132+
sums = make([][4]byte, len(forks)+len(forksByTime)+1) // 0th is the genesis
122133
)
134+
forks = append(forks, forksByTime...)
123135
hash := crc32.ChecksumIEEE(genesis[:])
124136
sums[0] = checksumToBytes(hash)
125137
for i, fork := range forks {
@@ -212,45 +224,59 @@ func checksumToBytes(hash uint32) [4]byte {
212224
}
213225

214226
// gatherForks gathers all the known forks and creates a sorted list out of them.
215-
func gatherForks(config *params.ChainConfig) []uint64 {
227+
func gatherForks(config *params.ChainConfig) ([]uint64, []uint64) {
216228
// Gather all the fork block numbers via reflection
217229
kind := reflect.TypeOf(params.ChainConfig{})
218230
conf := reflect.ValueOf(config).Elem()
219231

220232
var forks []uint64
233+
var forksByTime []uint64
221234
for i := 0; i < kind.NumField(); i++ {
222235
// Fetch the next field and skip non-fork rules
223236
field := kind.Field(i)
237+
time := false
224238
if !strings.HasSuffix(field.Name, "Block") {
225-
continue
239+
if !strings.HasSuffix(field.Name, "Time") {
240+
continue
241+
}
242+
time = true
226243
}
227244
if field.Type != reflect.TypeOf(new(big.Int)) {
228245
continue
229246
}
230247
// Extract the fork rule block number and aggregate it
231248
rule := conf.Field(i).Interface().(*big.Int)
232249
if rule != nil {
233-
forks = append(forks, rule.Uint64())
234-
}
235-
}
236-
// Sort the fork block numbers to permit chronological XOR
237-
for i := 0; i < len(forks); i++ {
238-
for j := i + 1; j < len(forks); j++ {
239-
if forks[i] > forks[j] {
240-
forks[i], forks[j] = forks[j], forks[i]
250+
if time {
251+
forksByTime = append(forksByTime, rule.Uint64())
252+
} else {
253+
forks = append(forks, rule.Uint64())
241254
}
242255
}
243256
}
257+
258+
sort.Slice(forks, func(i, j int) bool { return forks[i] < forks[j] })
259+
sort.Slice(forksByTime, func(i, j int) bool { return forksByTime[i] < forksByTime[j] })
260+
244261
// Deduplicate block numbers applying multiple forks
245262
for i := 1; i < len(forks); i++ {
246263
if forks[i] == forks[i-1] {
247264
forks = append(forks[:i], forks[i+1:]...)
248265
i--
249266
}
250267
}
268+
for i := 1; i < len(forksByTime); i++ {
269+
if forksByTime[i] == forksByTime[i-1] {
270+
forksByTime = append(forksByTime[:i], forksByTime[i+1:]...)
271+
i--
272+
}
273+
}
251274
// Skip any forks in block 0, that's the genesis ruleset
252275
if len(forks) > 0 && forks[0] == 0 {
253276
forks = forks[1:]
254277
}
255-
return forks
278+
if len(forksByTime) > 0 && forksByTime[0] == 0 {
279+
forksByTime = forksByTime[1:]
280+
}
281+
return forks, forksByTime
256282
}

core/forkid/forkid_test.go

Lines changed: 99 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,105 @@ func TestCreation(t *testing.T) {
185185
}
186186
for i, tt := range tests {
187187
for j, ttt := range tt.cases {
188-
if have := NewID(tt.config, tt.genesis, ttt.head); have != ttt.want {
188+
if have := NewID(tt.config, tt.genesis, ttt.head, 0); have != ttt.want {
189+
t.Errorf("test %d, case %d: fork ID mismatch: have %x, want %x", i, j, have, ttt.want)
190+
}
191+
}
192+
}
193+
}
194+
195+
// TestCreationWithTimestamps tests that different genesis and fork rule combinations result in
196+
// the correct fork ID even for time based forks.
197+
func TestCreationWithTimestamps(t *testing.T) {
198+
mergeConfig := *params.MainnetChainConfig
199+
mergeConfig.MergeNetsplitBlock = big.NewInt(18000000)
200+
201+
withdrawalConfig := *params.MainnetChainConfig
202+
withdrawalConfig.MergeNetsplitBlock = big.NewInt(18000000)
203+
withdrawalConfig.ShanghaiTime = big.NewInt(1668000000)
204+
type testcase struct {
205+
head uint64
206+
time uint64
207+
want ID
208+
}
209+
tests := []struct {
210+
config *params.ChainConfig
211+
genesis common.Hash
212+
cases []testcase
213+
}{
214+
// Mainnet test cases
215+
{
216+
params.MainnetChainConfig,
217+
params.MainnetGenesisHash,
218+
[]testcase{
219+
{0, 0, ID{Hash: checksumToBytes(0xfc64ec04), Next: 1150000}}, // Unsynced
220+
{1149999, 0, ID{Hash: checksumToBytes(0xfc64ec04), Next: 1150000}}, // Last Frontier block
221+
{1150000, 0, ID{Hash: checksumToBytes(0x97c2c34c), Next: 1920000}}, // First Homestead block
222+
{1919999, 0, ID{Hash: checksumToBytes(0x97c2c34c), Next: 1920000}}, // Last Homestead block
223+
{1920000, 0, ID{Hash: checksumToBytes(0x91d1f948), Next: 2463000}}, // First DAO block
224+
{2462999, 0, ID{Hash: checksumToBytes(0x91d1f948), Next: 2463000}}, // Last DAO block
225+
{2463000, 0, ID{Hash: checksumToBytes(0x7a64da13), Next: 2675000}}, // First Tangerine block
226+
{2674999, 0, ID{Hash: checksumToBytes(0x7a64da13), Next: 2675000}}, // Last Tangerine block
227+
{2675000, 0, ID{Hash: checksumToBytes(0x3edd5b10), Next: 4370000}}, // First Spurious block
228+
{4369999, 0, ID{Hash: checksumToBytes(0x3edd5b10), Next: 4370000}}, // Last Spurious block
229+
{4370000, 0, ID{Hash: checksumToBytes(0xa00bc324), Next: 7280000}}, // First Byzantium block
230+
{7279999, 0, ID{Hash: checksumToBytes(0xa00bc324), Next: 7280000}}, // Last Byzantium block
231+
{7280000, 0, ID{Hash: checksumToBytes(0x668db0af), Next: 9069000}}, // First and last Constantinople, first Petersburg block
232+
{9068999, 0, ID{Hash: checksumToBytes(0x668db0af), Next: 9069000}}, // Last Petersburg block
233+
{9069000, 0, ID{Hash: checksumToBytes(0x879d6e30), Next: 9200000}}, // First Istanbul and first Muir Glacier block
234+
{9199999, 0, ID{Hash: checksumToBytes(0x879d6e30), Next: 9200000}}, // Last Istanbul and first Muir Glacier block
235+
{9200000, 0, ID{Hash: checksumToBytes(0xe029e991), Next: 12244000}}, // First Muir Glacier block
236+
{12243999, 0, ID{Hash: checksumToBytes(0xe029e991), Next: 12244000}}, // Last Muir Glacier block
237+
{12244000, 0, ID{Hash: checksumToBytes(0x0eb440f6), Next: 12965000}}, // First Berlin block
238+
{12964999, 0, ID{Hash: checksumToBytes(0x0eb440f6), Next: 12965000}}, // Last Berlin block
239+
{12965000, 0, ID{Hash: checksumToBytes(0xb715077d), Next: 13773000}}, // First London block
240+
{13772999, 0, ID{Hash: checksumToBytes(0xb715077d), Next: 13773000}}, // Last London block
241+
{13773000, 0, ID{Hash: checksumToBytes(0x20c327fc), Next: 15050000}}, // First Arrow Glacier block
242+
{15049999, 0, ID{Hash: checksumToBytes(0x20c327fc), Next: 15050000}}, // Last Arrow Glacier block
243+
{15050000, 0, ID{Hash: checksumToBytes(0xf0afd0e3), Next: 0}}, // First Gray Glacier block
244+
{20000000, 0, ID{Hash: checksumToBytes(0xf0afd0e3), Next: 0}}, // Future Gray Glacier block
245+
},
246+
},
247+
// Withdrawal test cases
248+
{
249+
&withdrawalConfig,
250+
params.MainnetGenesisHash,
251+
[]testcase{
252+
{0, 0, ID{Hash: checksumToBytes(0xfc64ec04), Next: 1150000}}, // Unsynced
253+
{1149999, 0, ID{Hash: checksumToBytes(0xfc64ec04), Next: 1150000}}, // Last Frontier block
254+
{1150000, 0, ID{Hash: checksumToBytes(0x97c2c34c), Next: 1920000}}, // First Homestead block
255+
{1919999, 0, ID{Hash: checksumToBytes(0x97c2c34c), Next: 1920000}}, // Last Homestead block
256+
{1920000, 0, ID{Hash: checksumToBytes(0x91d1f948), Next: 2463000}}, // First DAO block
257+
{2462999, 0, ID{Hash: checksumToBytes(0x91d1f948), Next: 2463000}}, // Last DAO block
258+
{2463000, 0, ID{Hash: checksumToBytes(0x7a64da13), Next: 2675000}}, // First Tangerine block
259+
{2674999, 0, ID{Hash: checksumToBytes(0x7a64da13), Next: 2675000}}, // Last Tangerine block
260+
{2675000, 0, ID{Hash: checksumToBytes(0x3edd5b10), Next: 4370000}}, // First Spurious block
261+
{4369999, 0, ID{Hash: checksumToBytes(0x3edd5b10), Next: 4370000}}, // Last Spurious block
262+
{4370000, 0, ID{Hash: checksumToBytes(0xa00bc324), Next: 7280000}}, // First Byzantium block
263+
{7279999, 0, ID{Hash: checksumToBytes(0xa00bc324), Next: 7280000}}, // Last Byzantium block
264+
{7280000, 0, ID{Hash: checksumToBytes(0x668db0af), Next: 9069000}}, // First and last Constantinople, first Petersburg block
265+
{9068999, 0, ID{Hash: checksumToBytes(0x668db0af), Next: 9069000}}, // Last Petersburg block
266+
{9069000, 0, ID{Hash: checksumToBytes(0x879d6e30), Next: 9200000}}, // First Istanbul and first Muir Glacier block
267+
{9199999, 0, ID{Hash: checksumToBytes(0x879d6e30), Next: 9200000}}, // Last Istanbul and first Muir Glacier block
268+
{9200000, 0, ID{Hash: checksumToBytes(0xe029e991), Next: 12244000}}, // First Muir Glacier block
269+
{12243999, 0, ID{Hash: checksumToBytes(0xe029e991), Next: 12244000}}, // Last Muir Glacier block
270+
{12244000, 0, ID{Hash: checksumToBytes(0x0eb440f6), Next: 12965000}}, // First Berlin block
271+
{12964999, 0, ID{Hash: checksumToBytes(0x0eb440f6), Next: 12965000}}, // Last Berlin block
272+
{12965000, 0, ID{Hash: checksumToBytes(0xb715077d), Next: 13773000}}, // First London block
273+
{13772999, 0, ID{Hash: checksumToBytes(0xb715077d), Next: 13773000}}, // Last London block
274+
{13773000, 0, ID{Hash: checksumToBytes(0x20c327fc), Next: 15050000}}, // First Arrow Glacier block
275+
{15049999, 0, ID{Hash: checksumToBytes(0x20c327fc), Next: 15050000}}, // Last Arrow Glacier block
276+
{15050000, 0, ID{Hash: checksumToBytes(0xf0afd0e3), Next: 18000000}}, // First Gray Glacier block
277+
{18000000, 0, ID{Hash: checksumToBytes(0x4fb8a872), Next: 1668000000}}, // First Merge Start block
278+
{20000000, 0, ID{Hash: checksumToBytes(0x4fb8a872), Next: 1668000000}}, // Last Merge Start block
279+
{20000000, 1668000000, ID{Hash: checksumToBytes(0xc1fdf181), Next: 0}}, // First Merge Start block
280+
{20000000, 2668000000, ID{Hash: checksumToBytes(0xc1fdf181), Next: 0}}, // Future Merge Start block
281+
},
282+
},
283+
}
284+
for i, tt := range tests {
285+
for j, ttt := range tt.cases {
286+
if have := NewID(tt.config, tt.genesis, ttt.head, ttt.time); have != ttt.want {
189287
t.Errorf("test %d, case %d: fork ID mismatch: have %x, want %x", i, j, have, ttt.want)
190288
}
191289
}

eth/handler.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ func (h *handler) runEthPeer(peer *eth.Peer, handler eth.Handler) error {
331331
number = head.Number.Uint64()
332332
td = h.chain.GetTd(hash, number)
333333
)
334-
forkID := forkid.NewID(h.chain.Config(), h.chain.Genesis().Hash(), h.chain.CurrentHeader().Number.Uint64())
334+
forkID := forkid.NewID(h.chain.Config(), h.chain.Genesis().Hash(), h.chain.CurrentHeader().Number.Uint64(), h.chain.CurrentHeader().Time)
335335
if err := peer.Handshake(h.networkID, td, hash, genesis.Hash(), forkID, h.forkFilter); err != nil {
336336
peer.Log().Debug("Ethereum handshake failed", "err", err)
337337
return err

eth/protocols/eth/discovery.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,6 @@ func StartENRUpdater(chain *core.BlockChain, ln *enode.LocalNode) {
6060
// currentENREntry constructs an `eth` ENR entry based on the current state of the chain.
6161
func currentENREntry(chain *core.BlockChain) *enrEntry {
6262
return &enrEntry{
63-
ForkID: forkid.NewID(chain.Config(), chain.Genesis().Hash(), chain.CurrentHeader().Number.Uint64()),
63+
ForkID: forkid.NewID(chain.Config(), chain.Genesis().Hash(), chain.CurrentHeader().Number.Uint64(), chain.CurrentHeader().Time),
6464
}
6565
}

eth/protocols/eth/handshake_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ func testHandshake(t *testing.T, protocol uint) {
4040
genesis = backend.chain.Genesis()
4141
head = backend.chain.CurrentBlock()
4242
td = backend.chain.GetTd(head.Hash(), head.NumberU64())
43-
forkID = forkid.NewID(backend.chain.Config(), backend.chain.Genesis().Hash(), backend.chain.CurrentHeader().Number.Uint64())
43+
forkID = forkid.NewID(backend.chain.Config(), backend.chain.Genesis().Hash(), backend.chain.CurrentHeader().Number.Uint64(), backend.chain.CurrentHeader().Time)
4444
)
4545
tests := []struct {
4646
code uint64

les/client_handler.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ func (h *clientHandler) handle(p *serverPeer, noInitAnnounce bool) error {
111111
p.Log().Debug("Light Ethereum peer connected", "name", p.Name())
112112

113113
// Execute the LES handshake
114-
forkid := forkid.NewID(h.backend.blockchain.Config(), h.backend.genesis, h.backend.blockchain.CurrentHeader().Number.Uint64())
114+
forkid := forkid.NewID(h.backend.blockchain.Config(), h.backend.genesis, h.backend.blockchain.CurrentHeader().Number.Uint64(), h.backend.blockchain.CurrentHeader().Time)
115115
if err := p.Handshake(h.backend.blockchain.Genesis().Hash(), forkid, h.forkFilter); err != nil {
116116
p.Log().Debug("Light Ethereum handshake failed", "err", err)
117117
return err

les/peer_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,8 @@ func TestHandshake(t *testing.T) {
124124
genesis = common.HexToHash("cafebabe")
125125

126126
chain1, chain2 = &fakeChain{}, &fakeChain{}
127-
forkID1 = forkid.NewID(chain1.Config(), chain1.Genesis().Hash(), chain1.CurrentHeader().Number.Uint64())
128-
forkID2 = forkid.NewID(chain2.Config(), chain2.Genesis().Hash(), chain2.CurrentHeader().Number.Uint64())
127+
forkID1 = forkid.NewID(chain1.Config(), chain1.Genesis().Hash(), chain1.CurrentHeader().Number.Uint64(), chain1.CurrentHeader().Time)
128+
forkID2 = forkid.NewID(chain2.Config(), chain2.Genesis().Hash(), chain2.CurrentHeader().Number.Uint64(), chain2.CurrentHeader().Time)
129129
filter1, filter2 = forkid.NewFilter(chain1), forkid.NewFilter(chain2)
130130
)
131131

les/server_handler.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ func (h *serverHandler) handle(p *clientPeer) error {
117117
hash = head.Hash()
118118
number = head.Number.Uint64()
119119
td = h.blockchain.GetTd(hash, number)
120-
forkID = forkid.NewID(h.blockchain.Config(), h.blockchain.Genesis().Hash(), h.blockchain.CurrentBlock().NumberU64())
120+
forkID = forkid.NewID(h.blockchain.Config(), h.blockchain.Genesis().Hash(), h.blockchain.CurrentBlock().NumberU64(), h.blockchain.CurrentBlock().Time())
121121
)
122122
if err := p.Handshake(td, hash, number, h.blockchain.Genesis().Hash(), forkID, h.forkFilter, h.server); err != nil {
123123
p.Log().Debug("Light Ethereum handshake failed", "err", err)

les/test_helper.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -489,7 +489,7 @@ func (client *testClient) newRawPeer(t *testing.T, name string, version int, rec
489489
head = client.handler.backend.blockchain.CurrentHeader()
490490
td = client.handler.backend.blockchain.GetTd(head.Hash(), head.Number.Uint64())
491491
)
492-
forkID := forkid.NewID(client.handler.backend.blockchain.Config(), genesis.Hash(), head.Number.Uint64())
492+
forkID := forkid.NewID(client.handler.backend.blockchain.Config(), genesis.Hash(), head.Number.Uint64(), head.Time)
493493
tp.handshakeWithClient(t, td, head.Hash(), head.Number.Uint64(), genesis.Hash(), forkID, testCostList(0), recentTxLookup) // disable flow control by default
494494

495495
// Ensure the connection is established or exits when any error occurs
@@ -553,7 +553,7 @@ func (server *testServer) newRawPeer(t *testing.T, name string, version int) (*t
553553
head = server.handler.blockchain.CurrentHeader()
554554
td = server.handler.blockchain.GetTd(head.Hash(), head.Number.Uint64())
555555
)
556-
forkID := forkid.NewID(server.handler.blockchain.Config(), genesis.Hash(), head.Number.Uint64())
556+
forkID := forkid.NewID(server.handler.blockchain.Config(), genesis.Hash(), head.Number.Uint64(), head.Time)
557557
tp.handshakeWithServer(t, td, head.Hash(), head.Number.Uint64(), genesis.Hash(), forkID)
558558

559559
// Ensure the connection is established or exits when any error occurs

0 commit comments

Comments
 (0)