Skip to content
Open
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
7 changes: 6 additions & 1 deletion cmd/devp2p/discv4cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,12 @@ func discv4Resolve(ctx *cli.Context) error {
disc := startV4(ctx)
defer disc.Close()

fmt.Println(disc.Resolve(n).String())
resolved := disc.Resolve(n)
if resolved == nil {
fmt.Println("unresolved")
return fmt.Errorf("could not resolve node %s", n.ID())
}
fmt.Println(resolved.String())
return nil
}

Expand Down
6 changes: 3 additions & 3 deletions p2p/discover/v4_udp.go
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ func (t *UDPv4) Close() {
}

// Resolve searches for a specific node with the given ID and tries to get the most recent
// version of the node record for it. It returns n if the node could not be resolved.
// version of the node record for it. It returns nil if the node could not be resolved.
func (t *UDPv4) Resolve(n *enode.Node) *enode.Node {
Comment thread
gzliudan marked this conversation as resolved.
// Try asking directly. This works if the node is still responding on the endpoint we have.
if rn, err := t.RequestENR(n); err == nil {
Expand All @@ -324,7 +324,7 @@ func (t *UDPv4) Resolve(n *enode.Node) *enode.Node {
// Otherwise perform a network lookup.
var key enode.Secp256k1
if n.Load(&key) != nil {
return n // no secp256k1 key
return nil // no secp256k1 key
}
result := t.LookupPubkey((*ecdsa.PublicKey)(&key))
for _, rn := range result {
Expand All @@ -334,7 +334,7 @@ func (t *UDPv4) Resolve(n *enode.Node) *enode.Node {
}
}
}
return n
return nil
}

func (t *UDPv4) ourEndpoint() rpcEndpoint {
Expand Down
35 changes: 35 additions & 0 deletions p2p/discover/v4_udp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,41 @@ func TestUDPv4_pingTimeout(t *testing.T) {
}
}

func TestUDPv4_resolveReturnsNilWhenUnresolved(t *testing.T) {
test := newUDPTest(t)
defer test.close()

Comment thread
gzliudan marked this conversation as resolved.
var r enr.Record
ip := net.IP{127, 0, 0, 1}
id := enode.ID{9}
r.Set(enr.IP(ip))
r.Set(enr.UDP(30303))
n := enode.SignNull(&r, id)

// Avoid triggering the extra bonding ping path in requestENR.
test.udp.db.UpdateLastPingReceived(id, ip, time.Now())

if resolved := test.udp.Resolve(n); resolved != nil {
t.Fatalf("expected nil for unresolved node, got: %v", resolved)
}
}

func TestUDPv4_resolveReturnsNilWhenLookupMisses(t *testing.T) {
test := newUDPTest(t)
defer test.close()

key := newkey()
ip := net.IP{127, 0, 0, 1}
n := enode.NewV4(&key.PublicKey, ip, 30303, 30303)

// Avoid triggering the extra bonding ping path in requestENR.
test.udp.db.UpdateLastPingReceived(n.ID(), ip, time.Now())

if resolved := test.udp.Resolve(n); resolved != nil {
t.Fatalf("expected nil for unresolved node with secp256k1 key, got: %v", resolved)
}
}

type testPacket byte

func (req testPacket) kind() byte { return byte(req) }
Expand Down
Loading