diff --git a/cmd/devp2p/discv4cmd.go b/cmd/devp2p/discv4cmd.go index 0c832262a6..84c7ef0c44 100644 --- a/cmd/devp2p/discv4cmd.go +++ b/cmd/devp2p/discv4cmd.go @@ -163,7 +163,7 @@ func discv4Ping(ctx *cli.Context) error { defer disc.Close() start := time.Now() - if err := disc.PingWithoutResp(n); err != nil { + if _, err := disc.Ping(n); err != nil { return fmt.Errorf("node didn't respond: %v", err) } fmt.Printf("node responded to ping (RTT %v).\n", time.Since(start)) diff --git a/cmd/devp2p/discv5cmd.go b/cmd/devp2p/discv5cmd.go index b8a02b560a..dd253dd082 100644 --- a/cmd/devp2p/discv5cmd.go +++ b/cmd/devp2p/discv5cmd.go @@ -84,7 +84,8 @@ func discv5Ping(ctx *cli.Context) error { disc, _ := startV5(ctx) defer disc.Close() - fmt.Println(disc.PingWithoutResp(n)) + _, err := disc.Ping(n) + fmt.Println(err) return nil } diff --git a/p2p/discover/table.go b/p2p/discover/table.go index b01026c89f..6a3d1af3af 100644 --- a/p2p/discover/table.go +++ b/p2p/discover/table.go @@ -695,44 +695,7 @@ func pushNode(list []*tableNode, n *tableNode, max int) ([]*tableNode, *tableNod return list, removed } -// waitInit waits until the table is initialized. -func (tab *Table) waitInit() { - <-tab.initDone -} - -// nodeList returns all nodes contained in the table. -func (tab *Table) nodeList() []*enode.Node { - if !tab.isInitDone() { - return nil - } - - tab.mutex.Lock() - defer tab.mutex.Unlock() - - var nodes []*enode.Node - for _, b := range &tab.buckets { - for _, n := range b.entries { - nodes = append(nodes, n.Node) - } - } - return nodes -} - -// nodeIds returns the node IDs in the table. -func (tab *Table) nodeIds() [][]string { - tab.mutex.Lock() - defer tab.mutex.Unlock() - nodes := make([][]string, 0) - for _, b := range &tab.buckets { - bucketNodes := make([]string, 0) - for _, n := range b.entries { - bucketNodes = append(bucketNodes, "0x"+n.ID().String()) - } - nodes = append(nodes, bucketNodes) - } - return nodes -} - +// deleteNode removes a node from the table. func (tab *Table) deleteNode(n *enode.Node) { tab.mutex.Lock() defer tab.mutex.Unlock() diff --git a/p2p/discover/v4_udp.go b/p2p/discover/v4_udp.go index 717fd00a03..dd3f363f7e 100644 --- a/p2p/discover/v4_udp.go +++ b/p2p/discover/v4_udp.go @@ -210,12 +210,6 @@ func (t *UDPv4) ourEndpoint() v4wire.Endpoint { return v4wire.NewEndpoint(addr, uint16(node.TCP())) } -// PingWithoutResp sends a ping message to the given node. -func (t *UDPv4) PingWithoutResp(n *enode.Node) error { - _, err := t.ping(n) - return err -} - // ping sends a ping message to the given node and waits for a reply. func (t *UDPv4) ping(n *enode.Node) (seq uint64, err error) { addr, ok := n.UDPEndpoint() @@ -229,6 +223,19 @@ func (t *UDPv4) ping(n *enode.Node) (seq uint64, err error) { return seq, err } +// Ping calls PING on a node and waits for a PONG response. +func (t *UDPv4) Ping(n *enode.Node) (pong *v4wire.Pong, err error) { + addr, ok := n.UDPEndpoint() + if !ok { + return nil, errNoUDPEndpoint + } + rm := t.sendPing(n.ID(), addr, nil) + if err = <-rm.errc; err == nil { + pong = rm.reply.(*v4wire.Pong) + } + return pong, err +} + // sendPing sends a ping message to the given node and invokes the callback // when the reply arrives. func (t *UDPv4) sendPing(toid enode.ID, toaddr netip.AddrPort, callback func()) *replyMatcher { diff --git a/p2p/discover/v5_udp.go b/p2p/discover/v5_udp.go index f840c983f1..c82aadb26d 100644 --- a/p2p/discover/v5_udp.go +++ b/p2p/discover/v5_udp.go @@ -204,12 +204,6 @@ func (t *UDPv5) Close() { }) } -// PingWithoutResp sends a ping message to the given node. -func (t *UDPv5) PingWithoutResp(n *enode.Node) error { - _, err := t.ping(n) - return err -} - // 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. func (t *UDPv5) Resolve(n *enode.Node) *enode.Node { @@ -246,7 +240,7 @@ func (t *UDPv5) ResolveNodeId(id enode.ID) *enode.Node { } // Otherwise do a network lookup. - result := t.Lookup(n.ID()) + result := t.Lookup(id) for _, rn := range result { if rn.ID() == id { if n != nil && rn.Seq() <= n.Seq() { @@ -262,15 +256,20 @@ func (t *UDPv5) ResolveNodeId(id enode.ID) *enode.Node { // AllNodes returns all the nodes stored in the local table. func (t *UDPv5) AllNodes() []*enode.Node { - return t.tab.nodeList() + t.tab.mutex.Lock() + defer t.tab.mutex.Unlock() + nodes := make([]*enode.Node, 0) + + for _, b := range &t.tab.buckets { + for _, n := range b.entries { + nodes = append(nodes, n.Node) + } + } + return nodes } -// RoutingTableInfo returns the routing table information. Used for Portal discv5 RoutingTableInfo API. -func (t *UDPv5) RoutingTableInfo() [][]string { - return t.tab.nodeIds() -} - -// AddKnownNode adds a node to the routing table. Used for Portal discv5 AddEnr API. +// AddKnownNode adds a node to the routing table. +// The function should be used for testing only. func (t *UDPv5) AddKnownNode(n *enode.Node) bool { return t.tab.addFoundNode(n, true) } @@ -280,11 +279,6 @@ func (t *UDPv5) DeleteNode(n *enode.Node) { t.tab.deleteNode(n) } -// WaitInit waits for the routing table to be initialized. -func (t *UDPv5) WaitInit() { - t.tab.waitInit() -} - // LocalNode returns the current local Node running the // protocol. func (t *UDPv5) LocalNode() *enode.LocalNode { @@ -404,7 +398,7 @@ func lookupDistances(target, dest enode.ID) (dists []uint) { // ping calls PING on a node and waits for a PONG response. func (t *UDPv5) ping(n *enode.Node) (uint64, error) { - pong, err := t.PingWithResp(n) + pong, err := t.Ping(n) if err != nil { return 0, err } @@ -412,8 +406,8 @@ func (t *UDPv5) ping(n *enode.Node) (uint64, error) { return pong.ENRSeq, nil } -// PingWithResp calls PING on a node and waits for a PONG response. -func (t *UDPv5) PingWithResp(n *enode.Node) (*v5wire.Pong, error) { +// Ping calls PING on a node and waits for a PONG response. +func (t *UDPv5) Ping(n *enode.Node) (*v5wire.Pong, error) { req := &v5wire.Ping{ENRSeq: t.localNode.Node().Seq()} resp := t.callToNode(n, v5wire.PongMsg, req) defer t.callDone(resp) @@ -837,6 +831,11 @@ func (t *UDPv5) GetNode(id enode.ID) *enode.Node { return nil } +// Nodes returns the nodes in the routing table. +func (t *UDPv5) Nodes() [][]BucketNode { + return t.tab.Nodes() +} + // handle processes incoming packets according to their message type. func (t *UDPv5) handle(p v5wire.Packet, fromID enode.ID, fromAddr netip.AddrPort) { switch p := p.(type) {