p2p/discover/v4wire: use optional RLP field for EIP-868 seq (#22842)

This changes the definitions of Ping and Pong, adding an optional field
for the sequence number. This field was previously encoded/decoded using
the "tail" struct tag, but using "optional" is much nicer.
This commit is contained in:
Felix Lange
2021-05-18 11:48:41 +02:00
committed by GitHub
parent 32c1ed8a9c
commit 3e6f46caec
4 changed files with 17 additions and 47 deletions

View File

@ -34,7 +34,6 @@ import (
"github.com/ethereum/go-ethereum/p2p/discover/v4wire"
"github.com/ethereum/go-ethereum/p2p/enode"
"github.com/ethereum/go-ethereum/p2p/netutil"
"github.com/ethereum/go-ethereum/rlp"
)
// Errors
@ -217,7 +216,7 @@ func (t *UDPv4) Ping(n *enode.Node) error {
func (t *UDPv4) ping(n *enode.Node) (seq uint64, err error) {
rm := t.sendPing(n.ID(), &net.UDPAddr{IP: n.IP(), Port: n.UDP()}, nil)
if err = <-rm.errc; err == nil {
seq = rm.reply.(*v4wire.Pong).ENRSeq()
seq = rm.reply.(*v4wire.Pong).ENRSeq
}
return seq, err
}
@ -248,13 +247,12 @@ func (t *UDPv4) sendPing(toid enode.ID, toaddr *net.UDPAddr, callback func()) *r
}
func (t *UDPv4) makePing(toaddr *net.UDPAddr) *v4wire.Ping {
seq, _ := rlp.EncodeToBytes(t.localNode.Node().Seq())
return &v4wire.Ping{
Version: 4,
From: t.ourEndpoint(),
To: v4wire.NewEndpoint(toaddr, 0),
Expiration: uint64(time.Now().Add(expiration).Unix()),
Rest: []rlp.RawValue{seq},
ENRSeq: t.localNode.Node().Seq(),
}
}
@ -660,12 +658,11 @@ func (t *UDPv4) handlePing(h *packetHandlerV4, from *net.UDPAddr, fromID enode.I
req := h.Packet.(*v4wire.Ping)
// Reply.
seq, _ := rlp.EncodeToBytes(t.localNode.Node().Seq())
t.send(from, fromID, &v4wire.Pong{
To: v4wire.NewEndpoint(from, req.From.TCP),
ReplyTok: mac,
Expiration: uint64(time.Now().Add(expiration).Unix()),
Rest: []rlp.RawValue{seq},
ENRSeq: t.localNode.Node().Seq(),
})
// Ping back if our last pong on file is too far in the past.