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: mock DNS resolver in URL parsing test #20252

Merged
merged 3 commits into from
Nov 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
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
17 changes: 11 additions & 6 deletions p2p/enode/urlv4.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ import (
"github.com/ethereum/go-ethereum/p2p/enr"
)

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

// MustParseV4 parses a node URL. It panics if the URL is not valid.
func MustParseV4(rawurl string) *Node {
Expand Down Expand Up @@ -107,7 +110,6 @@ func isNewV4(n *Node) bool {
func parseComplete(rawurl string) (*Node, error) {
var (
id *ecdsa.PublicKey
ip net.IP
tcpPort, udpPort uint64
)
u, err := url.Parse(rawurl)
Expand All @@ -125,11 +127,14 @@ func parseComplete(rawurl string) (*Node, error) {
return nil, fmt.Errorf("invalid public key (%v)", err)
}
// Parse the IP address.
ips, err := net.LookupIP(u.Hostname())
if err != nil {
return nil, err
ip := net.ParseIP(u.Hostname())
if ip == nil {
ips, err := lookupIPFunc(u.Hostname())
if err != nil {
return nil, err
}
ip = ips[0]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should make sure that there is at least one ip in that list. In theory, not finding an IP address should return an error, but since this change is about switching resolvers, there is no guarantee that each resolver will respect this.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nvm, they'll have to make sure this doesn't happen and it should make a stack trace to point the way here.

}
ip = ips[0]
// Ensure the IP is 4 bytes long for IPv4 addresses.
if ipv4 := ip.To4(); ipv4 != nil {
ip = ipv4
Expand Down
10 changes: 10 additions & 0 deletions p2p/enode/urlv4_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package enode

import (
"crypto/ecdsa"
"errors"
"net"
"reflect"
"strings"
Expand All @@ -27,6 +28,15 @@ import (
"github.com/ethereum/go-ethereum/p2p/enr"
)

func init() {
lookupIPFunc = func(name string) ([]net.IP, error) {
if name == "node.example.org" {
return []net.IP{{33, 44, 55, 66}}, nil
}
return nil, errors.New("no such host")
}
}

var parseNodeTests = []struct {
input string
wantError string
Expand Down