Skip to content

Commit 8f1f4ef

Browse files
committed
Merge branch 'gethmaster' into gethintegration
2 parents ec6f511 + 7d99f7d commit 8f1f4ef

5 files changed

Lines changed: 37 additions & 67 deletions

File tree

cmd/devp2p/discv4cmd.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ func discv4Ping(ctx *cli.Context) error {
163163
defer disc.Close()
164164

165165
start := time.Now()
166-
if err := disc.PingWithoutResp(n); err != nil {
166+
if _, err := disc.Ping(n); err != nil {
167167
return fmt.Errorf("node didn't respond: %v", err)
168168
}
169169
fmt.Printf("node responded to ping (RTT %v).\n", time.Since(start))

cmd/devp2p/discv5cmd.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,8 @@ func discv5Ping(ctx *cli.Context) error {
8484
disc, _ := startV5(ctx)
8585
defer disc.Close()
8686

87-
fmt.Println(disc.PingWithoutResp(n))
87+
_, err := disc.Ping(n)
88+
fmt.Println(err)
8889
return nil
8990
}
9091

p2p/discover/table.go

Lines changed: 1 addition & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -695,44 +695,7 @@ func pushNode(list []*tableNode, n *tableNode, max int) ([]*tableNode, *tableNod
695695
return list, removed
696696
}
697697

698-
// waitInit waits until the table is initialized.
699-
func (tab *Table) waitInit() {
700-
<-tab.initDone
701-
}
702-
703-
// nodeList returns all nodes contained in the table.
704-
func (tab *Table) nodeList() []*enode.Node {
705-
if !tab.isInitDone() {
706-
return nil
707-
}
708-
709-
tab.mutex.Lock()
710-
defer tab.mutex.Unlock()
711-
712-
var nodes []*enode.Node
713-
for _, b := range &tab.buckets {
714-
for _, n := range b.entries {
715-
nodes = append(nodes, n.Node)
716-
}
717-
}
718-
return nodes
719-
}
720-
721-
// nodeIds returns the node IDs in the table.
722-
func (tab *Table) nodeIds() [][]string {
723-
tab.mutex.Lock()
724-
defer tab.mutex.Unlock()
725-
nodes := make([][]string, 0)
726-
for _, b := range &tab.buckets {
727-
bucketNodes := make([]string, 0)
728-
for _, n := range b.entries {
729-
bucketNodes = append(bucketNodes, "0x"+n.ID().String())
730-
}
731-
nodes = append(nodes, bucketNodes)
732-
}
733-
return nodes
734-
}
735-
698+
// deleteNode removes a node from the table.
736699
func (tab *Table) deleteNode(n *enode.Node) {
737700
tab.mutex.Lock()
738701
defer tab.mutex.Unlock()

p2p/discover/v4_udp.go

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -210,12 +210,6 @@ func (t *UDPv4) ourEndpoint() v4wire.Endpoint {
210210
return v4wire.NewEndpoint(addr, uint16(node.TCP()))
211211
}
212212

213-
// PingWithoutResp sends a ping message to the given node.
214-
func (t *UDPv4) PingWithoutResp(n *enode.Node) error {
215-
_, err := t.ping(n)
216-
return err
217-
}
218-
219213
// ping sends a ping message to the given node and waits for a reply.
220214
func (t *UDPv4) ping(n *enode.Node) (seq uint64, err error) {
221215
addr, ok := n.UDPEndpoint()
@@ -229,6 +223,19 @@ func (t *UDPv4) ping(n *enode.Node) (seq uint64, err error) {
229223
return seq, err
230224
}
231225

226+
// Ping calls PING on a node and waits for a PONG response.
227+
func (t *UDPv4) Ping(n *enode.Node) (pong *v4wire.Pong, err error) {
228+
addr, ok := n.UDPEndpoint()
229+
if !ok {
230+
return nil, errNoUDPEndpoint
231+
}
232+
rm := t.sendPing(n.ID(), addr, nil)
233+
if err = <-rm.errc; err == nil {
234+
pong = rm.reply.(*v4wire.Pong)
235+
}
236+
return pong, err
237+
}
238+
232239
// sendPing sends a ping message to the given node and invokes the callback
233240
// when the reply arrives.
234241
func (t *UDPv4) sendPing(toid enode.ID, toaddr netip.AddrPort, callback func()) *replyMatcher {

p2p/discover/v5_udp.go

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -204,12 +204,6 @@ func (t *UDPv5) Close() {
204204
})
205205
}
206206

207-
// PingWithoutResp sends a ping message to the given node.
208-
func (t *UDPv5) PingWithoutResp(n *enode.Node) error {
209-
_, err := t.ping(n)
210-
return err
211-
}
212-
213207
// Resolve searches for a specific node with the given ID and tries to get the most recent
214208
// version of the node record for it. It returns n if the node could not be resolved.
215209
func (t *UDPv5) Resolve(n *enode.Node) *enode.Node {
@@ -246,7 +240,7 @@ func (t *UDPv5) ResolveNodeId(id enode.ID) *enode.Node {
246240
}
247241

248242
// Otherwise do a network lookup.
249-
result := t.Lookup(n.ID())
243+
result := t.Lookup(id)
250244
for _, rn := range result {
251245
if rn.ID() == id {
252246
if n != nil && rn.Seq() <= n.Seq() {
@@ -262,15 +256,20 @@ func (t *UDPv5) ResolveNodeId(id enode.ID) *enode.Node {
262256

263257
// AllNodes returns all the nodes stored in the local table.
264258
func (t *UDPv5) AllNodes() []*enode.Node {
265-
return t.tab.nodeList()
266-
}
259+
t.tab.mutex.Lock()
260+
defer t.tab.mutex.Unlock()
261+
nodes := make([]*enode.Node, 0)
267262

268-
// RoutingTableInfo returns the routing table information. Used for Portal discv5 RoutingTableInfo API.
269-
func (t *UDPv5) RoutingTableInfo() [][]string {
270-
return t.tab.nodeIds()
263+
for _, b := range &t.tab.buckets {
264+
for _, n := range b.entries {
265+
nodes = append(nodes, n.Node)
266+
}
267+
}
268+
return nodes
271269
}
272270

273-
// AddKnownNode adds a node to the routing table. Used for Portal discv5 AddEnr API.
271+
// AddKnownNode adds a node to the routing table.
272+
// The function should be used for testing only.
274273
func (t *UDPv5) AddKnownNode(n *enode.Node) bool {
275274
return t.tab.addFoundNode(n, true)
276275
}
@@ -280,11 +279,6 @@ func (t *UDPv5) DeleteNode(n *enode.Node) {
280279
t.tab.deleteNode(n)
281280
}
282281

283-
// WaitInit waits for the routing table to be initialized.
284-
func (t *UDPv5) WaitInit() {
285-
t.tab.waitInit()
286-
}
287-
288282
// LocalNode returns the current local Node running the
289283
// protocol.
290284
func (t *UDPv5) LocalNode() *enode.LocalNode {
@@ -404,16 +398,16 @@ func lookupDistances(target, dest enode.ID) (dists []uint) {
404398

405399
// ping calls PING on a node and waits for a PONG response.
406400
func (t *UDPv5) ping(n *enode.Node) (uint64, error) {
407-
pong, err := t.PingWithResp(n)
401+
pong, err := t.Ping(n)
408402
if err != nil {
409403
return 0, err
410404
}
411405

412406
return pong.ENRSeq, nil
413407
}
414408

415-
// PingWithResp calls PING on a node and waits for a PONG response.
416-
func (t *UDPv5) PingWithResp(n *enode.Node) (*v5wire.Pong, error) {
409+
// Ping calls PING on a node and waits for a PONG response.
410+
func (t *UDPv5) Ping(n *enode.Node) (*v5wire.Pong, error) {
417411
req := &v5wire.Ping{ENRSeq: t.localNode.Node().Seq()}
418412
resp := t.callToNode(n, v5wire.PongMsg, req)
419413
defer t.callDone(resp)
@@ -837,6 +831,11 @@ func (t *UDPv5) GetNode(id enode.ID) *enode.Node {
837831
return nil
838832
}
839833

834+
// Nodes returns the nodes in the routing table.
835+
func (t *UDPv5) Nodes() [][]BucketNode {
836+
return t.tab.Nodes()
837+
}
838+
840839
// handle processes incoming packets according to their message type.
841840
func (t *UDPv5) handle(p v5wire.Packet, fromID enode.ID, fromAddr netip.AddrPort) {
842841
switch p := p.(type) {

0 commit comments

Comments
 (0)