eth/protocols/eth: update eth/69 for status changes and BlockRangeUpdate

This commit is contained in:
Felix Lange 2025-04-11 13:12:48 +02:00
parent ff3cb690b5
commit c3960e0a07
8 changed files with 171 additions and 71 deletions

View file

@ -26,7 +26,6 @@ import (
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core" "github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/core/forkid"
"github.com/ethereum/go-ethereum/core/txpool" "github.com/ethereum/go-ethereum/core/txpool"
"github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/crypto"
@ -103,9 +102,8 @@ type handlerConfig struct {
} }
type handler struct { type handler struct {
nodeID enode.ID nodeID enode.ID
networkID uint64 networkID uint64
forkFilter forkid.Filter // Fork ID filter, constant across the lifetime of the node
snapSync atomic.Bool // Flag whether snap sync is enabled (gets disabled if we already have blocks) snapSync atomic.Bool // Flag whether snap sync is enabled (gets disabled if we already have blocks)
synced atomic.Bool // Flag whether we're considered synchronised (enables transaction processing) synced atomic.Bool // Flag whether we're considered synchronised (enables transaction processing)
@ -143,7 +141,6 @@ func newHandler(config *handlerConfig) (*handler, error) {
h := &handler{ h := &handler{
nodeID: config.NodeID, nodeID: config.NodeID,
networkID: config.Network, networkID: config.Network,
forkFilter: forkid.NewFilter(config.Chain),
eventMux: config.EventMux, eventMux: config.EventMux,
database: config.Database, database: config.Database,
txpool: config.TxPool, txpool: config.TxPool,
@ -256,14 +253,7 @@ func (h *handler) runEthPeer(peer *eth.Peer, handler eth.Handler) error {
} }
// Execute the Ethereum handshake // Execute the Ethereum handshake
var ( if err := peer.Handshake(h.networkID, h.chain); err != nil {
genesis = h.chain.Genesis()
head = h.chain.CurrentHeader()
hash = head.Hash()
number = head.Number.Uint64()
)
forkID := forkid.NewID(h.chain.Config(), genesis, number, head.Time)
if err := peer.Handshake(h.networkID, hash, genesis.Hash(), forkID, h.forkFilter); err != nil {
peer.Log().Debug("Ethereum handshake failed", "err", err) peer.Log().Debug("Ethereum handshake failed", "err", err)
return err return err
} }

View file

@ -25,7 +25,6 @@ import (
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/consensus/ethash" "github.com/ethereum/go-ethereum/consensus/ethash"
"github.com/ethereum/go-ethereum/core" "github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/core/forkid"
"github.com/ethereum/go-ethereum/core/rawdb" "github.com/ethereum/go-ethereum/core/rawdb"
"github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/core/vm" "github.com/ethereum/go-ethereum/core/vm"
@ -257,11 +256,7 @@ func testRecvTransactions(t *testing.T, protocol uint) {
return eth.Handle((*ethHandler)(handler.handler), peer) return eth.Handle((*ethHandler)(handler.handler), peer)
}) })
// Run the handshake locally to avoid spinning up a source handler // Run the handshake locally to avoid spinning up a source handler
var ( if err := src.Handshake(1, handler.chain); err != nil {
genesis = handler.chain.Genesis()
head = handler.chain.CurrentBlock()
)
if err := src.Handshake(1, head.Hash(), genesis.Hash(), forkid.NewIDWithChain(handler.chain), forkid.NewFilter(handler.chain)); err != nil {
t.Fatalf("failed to run protocol handshake") t.Fatalf("failed to run protocol handshake")
} }
// Send the transaction to the sink and verify that it's added to the tx pool // Send the transaction to the sink and verify that it's added to the tx pool
@ -316,11 +311,7 @@ func testSendTransactions(t *testing.T, protocol uint) {
return eth.Handle((*ethHandler)(handler.handler), peer) return eth.Handle((*ethHandler)(handler.handler), peer)
}) })
// Run the handshake locally to avoid spinning up a source handler // Run the handshake locally to avoid spinning up a source handler
var ( if err := sink.Handshake(1, handler.chain); err != nil {
genesis = handler.chain.Genesis()
head = handler.chain.CurrentBlock()
)
if err := sink.Handshake(1, head.Hash(), genesis.Hash(), forkid.NewIDWithChain(handler.chain), forkid.NewFilter(handler.chain)); err != nil {
t.Fatalf("failed to run protocol handshake") t.Fatalf("failed to run protocol handshake")
} }
// After the handshake completes, the source handler should stream the sink // After the handshake completes, the source handler should stream the sink

View file

@ -192,6 +192,7 @@ var eth69 = map[uint64]msgHandler{
ReceiptsMsg: handleReceipts69, ReceiptsMsg: handleReceipts69,
GetPooledTransactionsMsg: handleGetPooledTransactions, GetPooledTransactionsMsg: handleGetPooledTransactions,
PooledTransactionsMsg: handlePooledTransactions, PooledTransactionsMsg: handlePooledTransactions,
BlockRangeUpdateMsg: handleBlockRangeUpdate,
} }
// handleMessage is invoked whenever an inbound message is received from a remote // handleMessage is invoked whenever an inbound message is received from a remote

View file

@ -532,3 +532,12 @@ func handlePooledTransactions(backend Backend, msg Decoder, peer *Peer) error {
return backend.Handle(peer, &txs.PooledTransactionsResponse) return backend.Handle(peer, &txs.PooledTransactionsResponse)
} }
func handleBlockRangeUpdate(backend Backend, msg Decoder, peer *Peer) error {
var update BlockRangeUpdatePacket
if err := msg.Decode(&update); err != nil {
return fmt.Errorf("%w: message %v: %v", errDecode, msg, err)
}
// We don't do anything with these messages for now.
return nil
}

View file

@ -22,6 +22,7 @@ import (
"time" "time"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/core/forkid" "github.com/ethereum/go-ethereum/core/forkid"
"github.com/ethereum/go-ethereum/metrics" "github.com/ethereum/go-ethereum/metrics"
"github.com/ethereum/go-ethereum/p2p" "github.com/ethereum/go-ethereum/p2p"
@ -35,44 +36,112 @@ const (
// Handshake executes the eth protocol handshake, negotiating version number, // Handshake executes the eth protocol handshake, negotiating version number,
// network IDs, difficulties, head and genesis blocks. // network IDs, difficulties, head and genesis blocks.
func (p *Peer) Handshake(network uint64, head common.Hash, genesis common.Hash, forkID forkid.ID, forkFilter forkid.Filter) error { func (p *Peer) Handshake(networkID uint64, chain *core.BlockChain) error {
// Send out own handshake in a new thread switch p.version {
case ETH69:
return p.handshake69(networkID, chain)
case ETH68:
return p.handshake68(networkID, chain)
default:
return errors.New("unsupported protocol version")
}
}
func (p *Peer) handshake68(networkID uint64, chain *core.BlockChain) error {
var (
genesis = chain.Genesis()
latest = chain.CurrentBlock()
forkID = forkid.NewID(chain.Config(), genesis, latest.Number.Uint64(), latest.Time)
forkFilter = forkid.NewFilter(chain)
)
errc := make(chan error, 2) errc := make(chan error, 2)
var status StatusPacket // safe to read after two values have been received from errc
go func() { go func() {
pkt := &StatusPacket{ pkt := &StatusPacket68{
ProtocolVersion: uint32(p.version), ProtocolVersion: uint32(p.version),
NetworkID: network, NetworkID: networkID,
Head: head, Head: latest.Hash(),
Genesis: genesis, Genesis: genesis.Hash(),
ForkID: forkID, ForkID: forkID,
} }
errc <- p2p.Send(p.rw, StatusMsg, pkt) errc <- p2p.Send(p.rw, StatusMsg, pkt)
}() }()
var status StatusPacket68 // safe to read after two values have been received from errc
go func() { go func() {
errc <- p.readStatus(network, &status, genesis, forkFilter) errc <- p.readStatus68(networkID, &status, genesis.Hash(), forkFilter)
}() }()
timeout := time.NewTimer(handshakeTimeout)
defer timeout.Stop() return waitForHandshake(errc, p)
for i := 0; i < 2; i++ { }
select {
case err := <-errc: func (p *Peer) readStatus68(networkID uint64, status *StatusPacket68, genesis common.Hash, forkFilter forkid.Filter) error {
if err != nil { if err := p.readStatusMsg(status); err != nil {
markError(p, err) return err
return err }
} if status.NetworkID != networkID {
case <-timeout.C: return fmt.Errorf("%w: %d (!= %d)", errNetworkIDMismatch, status.NetworkID, networkID)
markError(p, p2p.DiscReadTimeout) }
return p2p.DiscReadTimeout if uint(status.ProtocolVersion) != p.version {
} return fmt.Errorf("%w: %d (!= %d)", errProtocolVersionMismatch, status.ProtocolVersion, p.version)
}
if status.Genesis != genesis {
return fmt.Errorf("%w: %x (!= %x)", errGenesisMismatch, status.Genesis, genesis)
}
if err := forkFilter(status.ForkID); err != nil {
return fmt.Errorf("%w: %v", errForkIDRejected, err)
} }
return nil return nil
} }
// readStatus reads the remote handshake message. func (p *Peer) handshake69(networkID uint64, chain *core.BlockChain) error {
func (p *Peer) readStatus(network uint64, status *StatusPacket, genesis common.Hash, forkFilter forkid.Filter) error { var (
genesis = chain.Genesis()
latest = chain.CurrentBlock()
forkID = forkid.NewID(chain.Config(), genesis, latest.Number.Uint64(), latest.Time)
forkFilter = forkid.NewFilter(chain)
earliest, _ = chain.HistoryPruningCutoff()
)
errc := make(chan error, 2)
go func() {
pkt := &StatusPacket69{
ProtocolVersion: uint32(p.version),
NetworkID: networkID,
Genesis: genesis.Hash(),
ForkID: forkID,
EarliestBlock: earliest,
LatestBlock: latest.Number.Uint64(),
LatestBlockHash: latest.Hash(),
}
errc <- p2p.Send(p.rw, StatusMsg, pkt)
}()
var status StatusPacket69 // safe to read after two values have been received from errc
go func() {
errc <- p.readStatus69(networkID, &status, genesis.Hash(), forkFilter)
}()
return waitForHandshake(errc, p)
}
func (p *Peer) readStatus69(networkID uint64, status *StatusPacket69, genesis common.Hash, forkFilter forkid.Filter) error {
if err := p.readStatusMsg(status); err != nil {
return err
}
if status.NetworkID != networkID {
return fmt.Errorf("%w: %d (!= %d)", errNetworkIDMismatch, status.NetworkID, networkID)
}
if uint(status.ProtocolVersion) != p.version {
return fmt.Errorf("%w: %d (!= %d)", errProtocolVersionMismatch, status.ProtocolVersion, p.version)
}
if status.Genesis != genesis {
return fmt.Errorf("%w: %x (!= %x)", errGenesisMismatch, status.Genesis, genesis)
}
if err := forkFilter(status.ForkID); err != nil {
return fmt.Errorf("%w: %v", errForkIDRejected, err)
}
return nil
}
// readStatusMsg reads the first message on the connection.
func (p *Peer) readStatusMsg(dst any) error {
msg, err := p.rw.ReadMsg() msg, err := p.rw.ReadMsg()
if err != nil { if err != nil {
return err return err
@ -83,21 +152,26 @@ func (p *Peer) readStatus(network uint64, status *StatusPacket, genesis common.H
if msg.Size > maxMessageSize { if msg.Size > maxMessageSize {
return fmt.Errorf("%w: %v > %v", errMsgTooLarge, msg.Size, maxMessageSize) return fmt.Errorf("%w: %v > %v", errMsgTooLarge, msg.Size, maxMessageSize)
} }
// Decode the handshake and make sure everything matches if err := msg.Decode(dst); err != nil {
if err := msg.Decode(&status); err != nil {
return fmt.Errorf("%w: message %v: %v", errDecode, msg, err) return fmt.Errorf("%w: message %v: %v", errDecode, msg, err)
} }
if status.NetworkID != network { return nil
return fmt.Errorf("%w: %d (!= %d)", errNetworkIDMismatch, status.NetworkID, network) }
}
if uint(status.ProtocolVersion) != p.version { func waitForHandshake(errc <-chan error, p *Peer) error {
return fmt.Errorf("%w: %d (!= %d)", errProtocolVersionMismatch, status.ProtocolVersion, p.version) timeout := time.NewTimer(handshakeTimeout)
} defer timeout.Stop()
if status.Genesis != genesis { for range 2 {
return fmt.Errorf("%w: %x (!= %x)", errGenesisMismatch, status.Genesis, genesis) select {
} case err := <-errc:
if err := forkFilter(status.ForkID); err != nil { if err != nil {
return fmt.Errorf("%w: %v", errForkIDRejected, err) markError(p, err)
return err
}
case <-timeout.C:
markError(p, p2p.DiscReadTimeout)
return p2p.DiscReadTimeout
}
} }
return nil return nil
} }

View file

@ -52,19 +52,19 @@ func testHandshake(t *testing.T, protocol uint) {
want: errNoStatusMsg, want: errNoStatusMsg,
}, },
{ {
code: StatusMsg, data: StatusPacket{10, 1, new(big.Int), head.Hash(), genesis.Hash(), forkID}, code: StatusMsg, data: StatusPacket68{10, 1, new(big.Int), head.Hash(), genesis.Hash(), forkID},
want: errProtocolVersionMismatch, want: errProtocolVersionMismatch,
}, },
{ {
code: StatusMsg, data: StatusPacket{uint32(protocol), 999, new(big.Int), head.Hash(), genesis.Hash(), forkID}, code: StatusMsg, data: StatusPacket68{uint32(protocol), 999, new(big.Int), head.Hash(), genesis.Hash(), forkID},
want: errNetworkIDMismatch, want: errNetworkIDMismatch,
}, },
{ {
code: StatusMsg, data: StatusPacket{uint32(protocol), 1, new(big.Int), head.Hash(), common.Hash{3}, forkID}, code: StatusMsg, data: StatusPacket68{uint32(protocol), 1, new(big.Int), head.Hash(), common.Hash{3}, forkID},
want: errGenesisMismatch, want: errGenesisMismatch,
}, },
{ {
code: StatusMsg, data: StatusPacket{uint32(protocol), 1, new(big.Int), head.Hash(), genesis.Hash(), forkid.ID{Hash: [4]byte{0x00, 0x01, 0x02, 0x03}}}, code: StatusMsg, data: StatusPacket68{uint32(protocol), 1, new(big.Int), head.Hash(), genesis.Hash(), forkid.ID{Hash: [4]byte{0x00, 0x01, 0x02, 0x03}}},
want: errForkIDRejected, want: errForkIDRejected,
}, },
} }
@ -80,7 +80,7 @@ func testHandshake(t *testing.T, protocol uint) {
// Send the junk test with one peer, check the handshake failure // Send the junk test with one peer, check the handshake failure
go p2p.Send(app, test.code, test.data) go p2p.Send(app, test.code, test.data)
err := peer.Handshake(1, head.Hash(), genesis.Hash(), forkID, forkid.NewFilter(backend.chain)) err := peer.Handshake(1, backend.chain)
if err == nil { if err == nil {
t.Errorf("test %d: protocol returned nil error, want %q", i, test.want) t.Errorf("test %d: protocol returned nil error, want %q", i, test.want)
} else if !errors.Is(err, test.want) { } else if !errors.Is(err, test.want) {

View file

@ -343,6 +343,15 @@ func (p *Peer) RequestTxs(hashes []common.Hash) error {
}) })
} }
// SendBlockRangeUpdate sends a notification about our available block range to the peer.
func (p *Peer) SendBlockRangeUpdate(earliestBlock, latestBlock uint64, latestBlockHash common.Hash) error {
return p2p.Send(p.rw, BlockRangeUpdateMsg, &BlockRangeUpdatePacket{
EarliestBlock: earliestBlock,
LatestBlock: latestBlock,
LatestBlockHash: latestBlockHash,
})
}
// knownCache is a cache for known hashes. // knownCache is a cache for known hashes.
type knownCache struct { type knownCache struct {
hashes mapset.Set[common.Hash] hashes mapset.Set[common.Hash]

View file

@ -44,7 +44,7 @@ var ProtocolVersions = []uint{ETH69, ETH68}
// protocolLengths are the number of implemented message corresponding to // protocolLengths are the number of implemented message corresponding to
// different protocol versions. // different protocol versions.
var protocolLengths = map[uint]uint64{ETH68: 17, ETH69: 17} var protocolLengths = map[uint]uint64{ETH68: 17, ETH69: 18}
// maxMessageSize is the maximum cap on the size of a protocol message. // maxMessageSize is the maximum cap on the size of a protocol message.
const maxMessageSize = 10 * 1024 * 1024 const maxMessageSize = 10 * 1024 * 1024
@ -63,6 +63,7 @@ const (
PooledTransactionsMsg = 0x0a PooledTransactionsMsg = 0x0a
GetReceiptsMsg = 0x0f GetReceiptsMsg = 0x0f
ReceiptsMsg = 0x10 ReceiptsMsg = 0x10
BlockRangeUpdateMsg = 0x11
) )
var ( var (
@ -83,7 +84,7 @@ type Packet interface {
} }
// StatusPacket is the network packet for the status message. // StatusPacket is the network packet for the status message.
type StatusPacket struct { type StatusPacket68 struct {
ProtocolVersion uint32 ProtocolVersion uint32
NetworkID uint64 NetworkID uint64
TD *big.Int TD *big.Int
@ -92,6 +93,18 @@ type StatusPacket struct {
ForkID forkid.ID ForkID forkid.ID
} }
// StatusPacket69 is the network packet for the status message.
type StatusPacket69 struct {
ProtocolVersion uint32
NetworkID uint64
Genesis common.Hash
ForkID forkid.ID
// initial available block range
EarliestBlock uint64
LatestBlock uint64
LatestBlockHash common.Hash
}
// NewBlockHashesPacket is the network packet for the block announcements. // NewBlockHashesPacket is the network packet for the block announcements.
type NewBlockHashesPacket []struct { type NewBlockHashesPacket []struct {
Hash common.Hash // Hash of one particular block being announced Hash common.Hash // Hash of one particular block being announced
@ -312,8 +325,18 @@ type PooledTransactionsRLPPacket struct {
PooledTransactionsRLPResponse PooledTransactionsRLPResponse
} }
func (*StatusPacket) Name() string { return "Status" } // BlockRangeUpdatePacket is an announcement of the node's available block range.
func (*StatusPacket) Kind() byte { return StatusMsg } type BlockRangeUpdatePacket struct {
EarliestBlock uint64
LatestBlock uint64
LatestBlockHash common.Hash
}
func (*StatusPacket68) Name() string { return "Status" }
func (*StatusPacket68) Kind() byte { return StatusMsg }
func (*StatusPacket69) Name() string { return "Status" }
func (*StatusPacket69) Kind() byte { return StatusMsg }
func (*NewBlockHashesPacket) Name() string { return "NewBlockHashes" } func (*NewBlockHashesPacket) Name() string { return "NewBlockHashes" }
func (*NewBlockHashesPacket) Kind() byte { return NewBlockHashesMsg } func (*NewBlockHashesPacket) Kind() byte { return NewBlockHashesMsg }
@ -353,3 +376,6 @@ func (*ReceiptsResponse) Kind() byte { return ReceiptsMsg }
func (*ReceiptsRLPResponse) Name() string { return "Receipts" } func (*ReceiptsRLPResponse) Name() string { return "Receipts" }
func (*ReceiptsRLPResponse) Kind() byte { return ReceiptsMsg } func (*ReceiptsRLPResponse) Kind() byte { return ReceiptsMsg }
func (*BlockRangeUpdatePacket) Name() string { return "BlockRangeUpdate" }
func (*BlockRangeUpdatePacket) Kind() byte { return BlockRangeUpdateMsg }