Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

p2p/enode: add quic ENR entry #30283

Merged
merged 4 commits into from
Sep 13, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion p2p/enode/localnode.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ func (ln *LocalNode) sign() {
panic(fmt.Errorf("enode: can't verify local record: %v", err))
}
ln.cur.Store(n)
log.Info("New local node record", "seq", ln.seq, "id", n.ID(), "ip", n.IPAddr(), "udp", n.UDP(), "tcp", n.TCP())
log.Info("New local node record", "seq", ln.seq, "id", n.ID(), "ip", n.IPAddr(), "udp", n.UDP(), "tcp", n.TCP(), "quic", n.QUIC())
}

func (ln *LocalNode) bumpSeq() {
Expand Down
24 changes: 21 additions & 3 deletions p2p/enode/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,10 @@ type Node struct {
r enr.Record
id ID
// endpoint information
ip netip.Addr
udp uint16
tcp uint16
ip netip.Addr
udp uint16
tcp uint16
quic uint16
}

// New wraps a node record. The record must be valid according to the given
Expand Down Expand Up @@ -105,6 +106,7 @@ func (n *Node) setIP4(ip netip.Addr) {
n.ip = ip
n.Load((*enr.UDP)(&n.udp))
n.Load((*enr.TCP)(&n.tcp))
n.Load((*enr.QUIC)(&n.quic))
}

func (n *Node) setIP6(ip netip.Addr) {
Expand All @@ -119,6 +121,9 @@ func (n *Node) setIP6(ip netip.Addr) {
if err := n.Load((*enr.TCP6)(&n.tcp)); err != nil {
n.Load((*enr.TCP)(&n.tcp))
}
if err := n.Load((*enr.QUIC6)(&n.quic)); err != nil {
n.Load((*enr.QUIC)(&n.quic))
}
}

// MustParse parses a node record or enode:// URL. It panics if the input is invalid.
Expand Down Expand Up @@ -184,6 +189,11 @@ func (n *Node) TCP() int {
return int(n.tcp)
}

// QUIC returns the QUIC port of the node.
func (n *Node) QUIC() int {
guillaumemichel marked this conversation as resolved.
Show resolved Hide resolved
return int(n.quic)
}

// UDPEndpoint returns the announced UDP endpoint.
func (n *Node) UDPEndpoint() (netip.AddrPort, bool) {
if !n.ip.IsValid() || n.ip.IsUnspecified() || n.udp == 0 {
Expand All @@ -200,6 +210,14 @@ func (n *Node) TCPEndpoint() (netip.AddrPort, bool) {
return netip.AddrPortFrom(n.ip, n.tcp), true
}

// QUICEndpoint returns the announced QUIC endpoint.
func (n *Node) QUICEndpoint() (netip.AddrPort, bool) {
if !n.ip.IsValid() || n.ip.IsUnspecified() || n.quic == 0 {
return netip.AddrPort{}, false
}
return netip.AddrPortFrom(n.ip, n.quic), true
}

// Pubkey returns the secp256k1 public key of the node, if present.
func (n *Node) Pubkey() *ecdsa.PublicKey {
var key ecdsa.PublicKey
Expand Down
22 changes: 17 additions & 5 deletions p2p/enode/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,12 @@ func TestPythonInterop(t *testing.T) {
func TestNodeEndpoints(t *testing.T) {
id := HexID("00000000000000806ad9b61fa5ae014307ebdc964253adcd9f2c0a392aa11abc")
type endpointTest struct {
name string
node *Node
wantIP netip.Addr
wantUDP int
wantTCP int
name string
node *Node
wantIP netip.Addr
wantUDP int
wantTCP int
wantQUIC int
}
tests := []endpointTest{
{
Expand All @@ -98,6 +99,14 @@ func TestNodeEndpoints(t *testing.T) {
return SignNull(&r, id)
}(),
},
{
name: "quic-only",
node: func() *Node {
var r enr.Record
r.Set(enr.QUIC(9000))
return SignNull(&r, id)
}(),
},
{
name: "ipv4-only-loopback",
node: func() *Node {
Expand Down Expand Up @@ -222,6 +231,9 @@ func TestNodeEndpoints(t *testing.T) {
if test.wantTCP != test.node.TCP() {
t.Errorf("node has wrong TCP port %d, want %d", test.node.TCP(), test.wantTCP)
}
if test.wantQUIC != test.node.QUIC() {
t.Errorf("node has wrong QUIC port %d, want %d", test.node.QUIC(), test.wantQUIC)
}
})
}
}
Expand Down
10 changes: 10 additions & 0 deletions p2p/enr/entries.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,16 @@ type UDP6 uint16

func (v UDP6) ENRKey() string { return "udp6" }

// QUIC is the "quic" key, which holds the QUIC port of the node.
type QUIC uint16

func (v QUIC) ENRKey() string { return "quic" }

// QUIC6 is the "quic6" key, which holds the IPv6-specific quic6 port of the node.
type QUIC6 uint16

func (v QUIC6) ENRKey() string { return "quic6" }

// ID is the "id" key, which holds the name of the identity scheme.
type ID string

Expand Down