Skip to content

Commit f080af8

Browse files
committed
p2p/enode: add back DNS hostname resolution at parsing time
- removed in ethereum/go-ethereum#30822 in favor of on-demand runtime dialling - reported to have removed bootnodes DNS resolution at ethereum/go-ethereum#31208 - possibly broke DNS resolution for other methods of adding peers
1 parent e136827 commit f080af8

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

p2p/enode/urlv4.go

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import (
3333

3434
var (
3535
incompleteNodeURL = regexp.MustCompile("(?i)^(?:enode://)?([0-9a-f]+)$")
36+
lookupIPFunc = net.LookupIP
3637
)
3738

3839
// MustParseV4 parses a node URL. It panics if the URL is not valid.
@@ -128,6 +129,25 @@ func parseComplete(rawurl string) (*Node, error) {
128129

129130
// Parse the IP and ports.
130131
ip := net.ParseIP(u.Hostname())
132+
133+
// OP-Stack diff: add back DNS hostname resolution at parsing time
134+
// - removed in https://github.com/ethereum/go-ethereum/pull/30822 in favor of on-demand runtime dialling
135+
// - reported to have removed bootnodes DNS resolution at https://github.com/ethereum/go-ethereum/issues/31208
136+
// - possibly broke DNS resolution for other methods of adding peers
137+
var resolved bool
138+
if ip == nil {
139+
ips, err := lookupIPFunc(u.Hostname())
140+
if err != nil {
141+
return nil, err
142+
}
143+
resolved = true
144+
ip = ips[0]
145+
}
146+
// Ensure the IP is 4 bytes long for IPv4 addresses.
147+
if ipv4 := ip.To4(); ipv4 != nil {
148+
ip = ipv4
149+
}
150+
131151
if tcpPort, err = strconv.ParseUint(u.Port(), 10, 16); err != nil {
132152
return nil, errors.New("invalid port")
133153
}
@@ -142,7 +162,7 @@ func parseComplete(rawurl string) (*Node, error) {
142162

143163
// Create the node.
144164
node := NewV4(id, ip, int(tcpPort), int(udpPort))
145-
if ip == nil && u.Hostname() != "" {
165+
if resolved {
146166
node = node.WithHostname(u.Hostname())
147167
}
148168
return node, nil

p2p/enode/urlv4_test.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package enode
1818

1919
import (
2020
"crypto/ecdsa"
21+
"errors"
2122
"net"
2223
"reflect"
2324
"strings"
@@ -27,6 +28,15 @@ import (
2728
"github.com/ethereum/go-ethereum/p2p/enr"
2829
)
2930

31+
func init() {
32+
lookupIPFunc = func(name string) ([]net.IP, error) {
33+
if name == "valid." {
34+
return []net.IP{{33, 44, 55, 66}}, nil
35+
}
36+
return nil, errors.New("no such host")
37+
}
38+
}
39+
3040
var parseNodeTests = []struct {
3141
input string
3242
wantError string
@@ -81,7 +91,7 @@ var parseNodeTests = []struct {
8191
input: "enode://1dd9d65c4552b5eb43d5ad55a2ee3f56c6cbc1c64a5c8d659f51fcd51bace24351232b8d7821617d2b29b54b81cdefb9b3e9c37d7fd5f63270bcc9e1a6f6a439@valid.:3",
8292
wantResult: NewV4(
8393
hexPubkey("1dd9d65c4552b5eb43d5ad55a2ee3f56c6cbc1c64a5c8d659f51fcd51bace24351232b8d7821617d2b29b54b81cdefb9b3e9c37d7fd5f63270bcc9e1a6f6a439"),
84-
nil,
94+
net.IP{33, 44, 55, 66}, // OP-Stack adds back DNS resolution at parsing time
8595
3,
8696
3,
8797
).WithHostname("valid."),

0 commit comments

Comments
 (0)