Skip to content

Commit 64571f9

Browse files
committed
eth: eth/64 - extend handshake packet with fork id
1 parent e306304 commit 64571f9

7 files changed

Lines changed: 304 additions & 58 deletions

File tree

‎core/forkid/forkid.go‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ type ID struct {
5050
Next uint64 // Block number of the next upcoming fork, or 0 if no forks are known
5151
}
5252

53+
// Filter is a fork id filter to validate a remotely advertised ID.
54+
type Filter func(id ID) error
55+
5356
// NewID calculates the Ethereum fork ID from the chain config and head.
5457
func NewID(chain *core.BlockChain) ID {
5558
return newID(
@@ -82,7 +85,7 @@ func newID(config *params.ChainConfig, genesis common.Hash, head uint64) ID {
8285

8386
// NewFilter creates a filter that returns if a fork ID should be rejected or not
8487
// based on the local chain's status.
85-
func NewFilter(chain *core.BlockChain) func(id ID) error {
88+
func NewFilter(chain *core.BlockChain) Filter {
8689
return newFilter(
8790
chain.Config(),
8891
chain.Genesis().Hash(),

‎eth/handler.go‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import (
2929
"github.com/ethereum/go-ethereum/common"
3030
"github.com/ethereum/go-ethereum/consensus"
3131
"github.com/ethereum/go-ethereum/core"
32+
"github.com/ethereum/go-ethereum/core/forkid"
3233
"github.com/ethereum/go-ethereum/core/types"
3334
"github.com/ethereum/go-ethereum/eth/downloader"
3435
"github.com/ethereum/go-ethereum/eth/fetcher"
@@ -63,7 +64,8 @@ func errResp(code errCode, format string, v ...interface{}) error {
6364
}
6465

6566
type ProtocolManager struct {
66-
networkID uint64
67+
networkID uint64
68+
forkFilter forkid.Filter // Fork ID filter, constant across the lifetime of the node
6769

6870
fastSync uint32 // Flag whether fast sync is enabled (gets disabled if we already have blocks)
6971
acceptTxs uint32 // Flag whether we're considered synchronised (enables transaction processing)
@@ -103,6 +105,7 @@ func NewProtocolManager(config *params.ChainConfig, checkpoint *params.TrustedCh
103105
// Create the protocol manager with the base fields
104106
manager := &ProtocolManager{
105107
networkID: networkID,
108+
forkFilter: forkid.NewFilter(blockchain),
106109
eventMux: mux,
107110
txpool: txpool,
108111
blockchain: blockchain,
@@ -304,7 +307,7 @@ func (pm *ProtocolManager) handle(p *peer) error {
304307
number = head.Number.Uint64()
305308
td = pm.blockchain.GetTd(hash, number)
306309
)
307-
if err := p.Handshake(pm.networkID, td, hash, genesis.Hash()); err != nil {
310+
if err := p.Handshake(pm.networkID, td, hash, genesis.Hash(), forkid.NewID(pm.blockchain), pm.forkFilter); err != nil {
308311
p.Log().Debug("Ethereum handshake failed", "err", err)
309312
return err
310313
}

‎eth/handler_test.go‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ import (
3939
)
4040

4141
// Tests that block headers can be retrieved from a remote chain based on user queries.
42-
func TestGetBlockHeaders62(t *testing.T) { testGetBlockHeaders(t, 62) }
4342
func TestGetBlockHeaders63(t *testing.T) { testGetBlockHeaders(t, 63) }
43+
func TestGetBlockHeaders64(t *testing.T) { testGetBlockHeaders(t, 64) }
4444

4545
func testGetBlockHeaders(t *testing.T, protocol int) {
4646
pm, _ := newTestProtocolManagerMust(t, downloader.FullSync, downloader.MaxHashFetch+15, nil, nil)
@@ -198,8 +198,8 @@ func testGetBlockHeaders(t *testing.T, protocol int) {
198198
}
199199

200200
// Tests that block contents can be retrieved from a remote chain based on their hashes.
201-
func TestGetBlockBodies62(t *testing.T) { testGetBlockBodies(t, 62) }
202201
func TestGetBlockBodies63(t *testing.T) { testGetBlockBodies(t, 63) }
202+
func TestGetBlockBodies64(t *testing.T) { testGetBlockBodies(t, 64) }
203203

204204
func testGetBlockBodies(t *testing.T, protocol int) {
205205
pm, _ := newTestProtocolManagerMust(t, downloader.FullSync, downloader.MaxBlockFetch+15, nil, nil)
@@ -271,6 +271,7 @@ func testGetBlockBodies(t *testing.T, protocol int) {
271271

272272
// Tests that the node state database can be retrieved based on hashes.
273273
func TestGetNodeData63(t *testing.T) { testGetNodeData(t, 63) }
274+
func TestGetNodeData64(t *testing.T) { testGetNodeData(t, 64) }
274275

275276
func testGetNodeData(t *testing.T, protocol int) {
276277
// Define three accounts to simulate transactions with
@@ -367,6 +368,7 @@ func testGetNodeData(t *testing.T, protocol int) {
367368

368369
// Tests that the transaction receipts can be retrieved based on hashes.
369370
func TestGetReceipt63(t *testing.T) { testGetReceipt(t, 63) }
371+
func TestGetReceipt64(t *testing.T) { testGetReceipt(t, 64) }
370372

371373
func testGetReceipt(t *testing.T, protocol int) {
372374
// Define three accounts to simulate transactions with

‎eth/helper_test.go‎

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ package eth
2222
import (
2323
"crypto/ecdsa"
2424
"crypto/rand"
25+
"fmt"
2526
"math/big"
2627
"sort"
2728
"sync"
@@ -30,6 +31,7 @@ import (
3031
"github.com/ethereum/go-ethereum/common"
3132
"github.com/ethereum/go-ethereum/consensus/ethash"
3233
"github.com/ethereum/go-ethereum/core"
34+
"github.com/ethereum/go-ethereum/core/forkid"
3335
"github.com/ethereum/go-ethereum/core/rawdb"
3436
"github.com/ethereum/go-ethereum/core/types"
3537
"github.com/ethereum/go-ethereum/core/vm"
@@ -171,20 +173,35 @@ func newTestPeer(name string, version int, pm *ProtocolManager, shake bool) (*te
171173
head = pm.blockchain.CurrentHeader()
172174
td = pm.blockchain.GetTd(head.Hash(), head.Number.Uint64())
173175
)
174-
tp.handshake(nil, td, head.Hash(), genesis.Hash())
176+
tp.handshake(nil, td, head.Hash(), genesis.Hash(), forkid.NewID(pm.blockchain), forkid.NewFilter(pm.blockchain))
175177
}
176178
return tp, errc
177179
}
178180

179181
// handshake simulates a trivial handshake that expects the same state from the
180182
// remote side as we are simulating locally.
181-
func (p *testPeer) handshake(t *testing.T, td *big.Int, head common.Hash, genesis common.Hash) {
182-
msg := &statusData{
183-
ProtocolVersion: uint32(p.version),
184-
NetworkId: DefaultConfig.NetworkId,
185-
TD: td,
186-
CurrentBlock: head,
187-
GenesisBlock: genesis,
183+
func (p *testPeer) handshake(t *testing.T, td *big.Int, head common.Hash, genesis common.Hash, forkID forkid.ID, forkFilter forkid.Filter) {
184+
var msg interface{}
185+
switch {
186+
case p.version == eth63:
187+
msg = &statusData63{
188+
ProtocolVersion: uint32(p.version),
189+
NetworkId: DefaultConfig.NetworkId,
190+
TD: td,
191+
CurrentBlock: head,
192+
GenesisBlock: genesis,
193+
}
194+
case p.version == eth64:
195+
msg = &statusData{
196+
ProtocolVersion: uint32(p.version),
197+
NetworkID: DefaultConfig.NetworkId,
198+
TD: td,
199+
Head: head,
200+
Genesis: genesis,
201+
ForkID: forkID,
202+
}
203+
default:
204+
panic(fmt.Sprintf("unsupported eth protocol version: %d", p.version))
188205
}
189206
if err := p2p.ExpectMsg(p.app, StatusMsg, msg); err != nil {
190207
t.Fatalf("status recv: %v", err)

‎eth/peer.go‎

Lines changed: 76 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525

2626
mapset "github.com/deckarep/golang-set"
2727
"github.com/ethereum/go-ethereum/common"
28+
"github.com/ethereum/go-ethereum/core/forkid"
2829
"github.com/ethereum/go-ethereum/core/types"
2930
"github.com/ethereum/go-ethereum/p2p"
3031
"github.com/ethereum/go-ethereum/rlp"
@@ -353,22 +354,46 @@ func (p *peer) RequestReceipts(hashes []common.Hash) error {
353354

354355
// Handshake executes the eth protocol handshake, negotiating version number,
355356
// network IDs, difficulties, head and genesis blocks.
356-
func (p *peer) Handshake(network uint64, td *big.Int, head common.Hash, genesis common.Hash) error {
357+
func (p *peer) Handshake(network uint64, td *big.Int, head common.Hash, genesis common.Hash, forkID forkid.ID, forkFilter forkid.Filter) error {
357358
// Send out own handshake in a new thread
358359
errc := make(chan error, 2)
359-
var status statusData // safe to read after two values have been received from errc
360360

361+
var (
362+
status63 statusData63 // safe to read after two values have been received from errc
363+
status statusData // safe to read after two values have been received from errc
364+
)
361365
go func() {
362-
errc <- p2p.Send(p.rw, StatusMsg, &statusData{
363-
ProtocolVersion: uint32(p.version),
364-
NetworkId: network,
365-
TD: td,
366-
CurrentBlock: head,
367-
GenesisBlock: genesis,
368-
})
366+
switch {
367+
case p.version == eth63:
368+
errc <- p2p.Send(p.rw, StatusMsg, &statusData63{
369+
ProtocolVersion: uint32(p.version),
370+
NetworkId: network,
371+
TD: td,
372+
CurrentBlock: head,
373+
GenesisBlock: genesis,
374+
})
375+
case p.version == eth64:
376+
errc <- p2p.Send(p.rw, StatusMsg, &statusData{
377+
ProtocolVersion: uint32(p.version),
378+
NetworkID: network,
379+
TD: td,
380+
Head: head,
381+
Genesis: genesis,
382+
ForkID: forkID,
383+
})
384+
default:
385+
panic(fmt.Sprintf("unsupported eth protocol version: %d", p.version))
386+
}
369387
}()
370388
go func() {
371-
errc <- p.readStatus(network, &status, genesis)
389+
switch {
390+
case p.version == eth63:
391+
errc <- p.readStatusLegacy(network, &status63, genesis)
392+
case p.version == eth64:
393+
errc <- p.readStatus(network, &status, genesis, forkFilter)
394+
default:
395+
panic(fmt.Sprintf("unsupported eth protocol version: %d", p.version))
396+
}
372397
}()
373398
timeout := time.NewTimer(handshakeTimeout)
374399
defer timeout.Stop()
@@ -382,11 +407,18 @@ func (p *peer) Handshake(network uint64, td *big.Int, head common.Hash, genesis
382407
return p2p.DiscReadTimeout
383408
}
384409
}
385-
p.td, p.head = status.TD, status.CurrentBlock
410+
switch {
411+
case p.version == eth63:
412+
p.td, p.head = status63.TD, status63.CurrentBlock
413+
case p.version == eth64:
414+
p.td, p.head = status.TD, status.Head
415+
default:
416+
panic(fmt.Sprintf("unsupported eth protocol version: %d", p.version))
417+
}
386418
return nil
387419
}
388420

389-
func (p *peer) readStatus(network uint64, status *statusData, genesis common.Hash) (err error) {
421+
func (p *peer) readStatusLegacy(network uint64, status *statusData63, genesis common.Hash) error {
390422
msg, err := p.rw.ReadMsg()
391423
if err != nil {
392424
return err
@@ -402,17 +434,47 @@ func (p *peer) readStatus(network uint64, status *statusData, genesis common.Has
402434
return errResp(ErrDecode, "msg %v: %v", msg, err)
403435
}
404436
if status.GenesisBlock != genesis {
405-
return errResp(ErrGenesisBlockMismatch, "%x (!= %x)", status.GenesisBlock[:8], genesis[:8])
437+
return errResp(ErrGenesisMismatch, "%x (!= %x)", status.GenesisBlock[:8], genesis[:8])
406438
}
407439
if status.NetworkId != network {
408-
return errResp(ErrNetworkIdMismatch, "%d (!= %d)", status.NetworkId, network)
440+
return errResp(ErrNetworkIDMismatch, "%d (!= %d)", status.NetworkId, network)
409441
}
410442
if int(status.ProtocolVersion) != p.version {
411443
return errResp(ErrProtocolVersionMismatch, "%d (!= %d)", status.ProtocolVersion, p.version)
412444
}
413445
return nil
414446
}
415447

448+
func (p *peer) readStatus(network uint64, status *statusData, genesis common.Hash, forkFilter forkid.Filter) error {
449+
msg, err := p.rw.ReadMsg()
450+
if err != nil {
451+
return err
452+
}
453+
if msg.Code != StatusMsg {
454+
return errResp(ErrNoStatusMsg, "first msg has code %x (!= %x)", msg.Code, StatusMsg)
455+
}
456+
if msg.Size > protocolMaxMsgSize {
457+
return errResp(ErrMsgTooLarge, "%v > %v", msg.Size, protocolMaxMsgSize)
458+
}
459+
// Decode the handshake and make sure everything matches
460+
if err := msg.Decode(&status); err != nil {
461+
return errResp(ErrDecode, "msg %v: %v", msg, err)
462+
}
463+
if status.NetworkID != network {
464+
return errResp(ErrNetworkIDMismatch, "%d (!= %d)", status.NetworkID, network)
465+
}
466+
if int(status.ProtocolVersion) != p.version {
467+
return errResp(ErrProtocolVersionMismatch, "%d (!= %d)", status.ProtocolVersion, p.version)
468+
}
469+
if status.Genesis != genesis {
470+
return errResp(ErrGenesisMismatch, "%x (!= %x)", status.Genesis, genesis)
471+
}
472+
if err := forkFilter(status.ForkID); err != nil {
473+
return errResp(ErrForkIDRejected, "%v", err)
474+
}
475+
return nil
476+
}
477+
416478
// String implements fmt.Stringer.
417479
func (p *peer) String() string {
418480
return fmt.Sprintf("Peer %s [%s]", p.id,

‎eth/protocol.go‎

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -23,31 +23,31 @@ import (
2323

2424
"github.com/ethereum/go-ethereum/common"
2525
"github.com/ethereum/go-ethereum/core"
26+
"github.com/ethereum/go-ethereum/core/forkid"
2627
"github.com/ethereum/go-ethereum/core/types"
2728
"github.com/ethereum/go-ethereum/event"
2829
"github.com/ethereum/go-ethereum/rlp"
2930
)
3031

3132
// Constants to match up protocol versions and messages
3233
const (
33-
eth62 = 62
3434
eth63 = 63
35+
eth64 = 64
3536
)
3637

3738
// protocolName is the official short name of the protocol used during capability negotiation.
3839
const protocolName = "eth"
3940

4041
// ProtocolVersions are the supported versions of the eth protocol (first is primary).
41-
var ProtocolVersions = []uint{eth63}
42+
var ProtocolVersions = []uint{eth64, eth63}
4243

4344
// protocolLengths are the number of implemented message corresponding to different protocol versions.
44-
var protocolLengths = map[uint]uint64{eth63: 17, eth62: 8}
45+
var protocolLengths = map[uint]uint64{eth64: 17, eth63: 17}
4546

4647
const protocolMaxMsgSize = 10 * 1024 * 1024 // Maximum cap on the size of a protocol message
4748

4849
// eth protocol message codes
4950
const (
50-
// Protocol messages belonging to eth/62
5151
StatusMsg = 0x00
5252
NewBlockHashesMsg = 0x01
5353
TxMsg = 0x02
@@ -56,12 +56,10 @@ const (
5656
GetBlockBodiesMsg = 0x05
5757
BlockBodiesMsg = 0x06
5858
NewBlockMsg = 0x07
59-
60-
// Protocol messages belonging to eth/63
61-
GetNodeDataMsg = 0x0d
62-
NodeDataMsg = 0x0e
63-
GetReceiptsMsg = 0x0f
64-
ReceiptsMsg = 0x10
59+
GetNodeDataMsg = 0x0d
60+
NodeDataMsg = 0x0e
61+
GetReceiptsMsg = 0x0f
62+
ReceiptsMsg = 0x10
6563
)
6664

6765
type errCode int
@@ -71,11 +69,11 @@ const (
7169
ErrDecode
7270
ErrInvalidMsgCode
7371
ErrProtocolVersionMismatch
74-
ErrNetworkIdMismatch
75-
ErrGenesisBlockMismatch
72+
ErrNetworkIDMismatch
73+
ErrGenesisMismatch
74+
ErrForkIDRejected
7675
ErrNoStatusMsg
7776
ErrExtraStatusMsg
78-
ErrSuspendedPeer
7977
)
8078

8179
func (e errCode) String() string {
@@ -88,11 +86,11 @@ var errorToString = map[int]string{
8886
ErrDecode: "Invalid message",
8987
ErrInvalidMsgCode: "Invalid message code",
9088
ErrProtocolVersionMismatch: "Protocol version mismatch",
91-
ErrNetworkIdMismatch: "NetworkId mismatch",
92-
ErrGenesisBlockMismatch: "Genesis block mismatch",
89+
ErrNetworkIDMismatch: "Network ID mismatch",
90+
ErrGenesisMismatch: "Genesis mismatch",
91+
ErrForkIDRejected: "Fork ID rejected",
9392
ErrNoStatusMsg: "No status message",
9493
ErrExtraStatusMsg: "Extra status message",
95-
ErrSuspendedPeer: "Suspended peer",
9694
}
9795

9896
type txPool interface {
@@ -108,15 +106,25 @@ type txPool interface {
108106
SubscribeNewTxsEvent(chan<- core.NewTxsEvent) event.Subscription
109107
}
110108

111-
// statusData is the network packet for the status message.
112-
type statusData struct {
109+
// statusData63 is the network packet for the status message for eth/63.
110+
type statusData63 struct {
113111
ProtocolVersion uint32
114112
NetworkId uint64
115113
TD *big.Int
116114
CurrentBlock common.Hash
117115
GenesisBlock common.Hash
118116
}
119117

118+
// statusData is the network packet for the status message for eth/64 and later.
119+
type statusData struct {
120+
ProtocolVersion uint32
121+
NetworkID uint64
122+
TD *big.Int
123+
Head common.Hash
124+
Genesis common.Hash
125+
ForkID forkid.ID
126+
}
127+
120128
// newBlockHashesData is the network packet for the block announcements.
121129
type newBlockHashesData []struct {
122130
Hash common.Hash // Hash of one particular block being announced

0 commit comments

Comments
 (0)