eth/protocols/eth: store block range on peer

This commit is contained in:
Felix Lange 2025-04-16 10:49:04 +02:00
parent b89eb04fff
commit 3571ef3647
4 changed files with 33 additions and 4 deletions

View file

@ -17,6 +17,7 @@
package eth
import (
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/eth/protocols/eth"
"github.com/ethereum/go-ethereum/eth/protocols/snap"
)
@ -25,6 +26,13 @@ import (
// about a connected peer.
type ethPeerInfo struct {
Version uint `json:"version"` // Ethereum protocol version negotiated
BlockRange *peerBlockRange `json:"blockRange"`
}
type peerBlockRange struct {
Earliest uint64 `json:"earliestBlock"`
Latest uint64 `json:"latestBlock"`
LatestHash common.Hash `json:"latestBlockHash"`
}
// ethPeer is a wrapper around eth.Peer to maintain a few extra metadata.
@ -35,9 +43,15 @@ type ethPeer struct {
// info gathers and returns some `eth` protocol metadata known about a peer.
func (p *ethPeer) info() *ethPeerInfo {
return &ethPeerInfo{
Version: p.Version(),
info := &ethPeerInfo{Version: p.Version()}
if br := p.BlockRange(); br != nil {
info.BlockRange = &peerBlockRange{
Earliest: br.EarliestBlock,
Latest: br.LatestBlock,
LatestHash: br.LatestBlockHash,
}
}
return info
}
// snapPeerInfo represents a short summary of the `snap` sub-protocol metadata known

View file

@ -548,6 +548,7 @@ func handleBlockRangeUpdate(backend Backend, msg Decoder, peer *Peer) error {
if err := msg.Decode(&update); err != nil {
return err
}
// We don't do anything with these messages for now.
// We don't do anything with these messages for now, just store them on the peer.
peer.lastRange.Store(&update)
return nil
}

View file

@ -137,6 +137,12 @@ func (p *Peer) readStatus69(networkID uint64, status *StatusPacket69, genesis co
if err := forkFilter(status.ForkID); err != nil {
return fmt.Errorf("%w: %v", errForkIDRejected, err)
}
// Handle initial block range.
p.lastRange.Store(&BlockRangeUpdatePacket{
EarliestBlock: status.EarliestBlock,
LatestBlock: status.LatestBlock,
LatestBlockHash: status.LatestBlockHash,
})
return nil
}

View file

@ -18,6 +18,7 @@ package eth
import (
"math/rand"
"sync/atomic"
mapset "github.com/deckarep/golang-set/v2"
"github.com/ethereum/go-ethereum/common"
@ -47,6 +48,7 @@ type Peer struct {
*p2p.Peer // The embedded P2P package peer
rw p2p.MsgReadWriter // Input/output streams for snap
version uint // Protocol version negotiated
lastRange atomic.Pointer[BlockRangeUpdatePacket]
txpool TxPool // Transaction pool used by the broadcasters for liveness checks
knownTxs *knownCache // Set of transaction hashes known to be known by this peer
@ -102,6 +104,12 @@ func (p *Peer) Version() uint {
return p.version
}
// BlockRange returns the latest announced block range.
// This will be nil for peers below protocol version eth/69.
func (p *Peer) BlockRange() *BlockRangeUpdatePacket {
return p.lastRange.Load()
}
// KnownTransaction returns whether peer is known to already have a transaction.
func (p *Peer) KnownTransaction(hash common.Hash) bool {
return p.knownTxs.Contains(hash)