-
Notifications
You must be signed in to change notification settings - Fork 20.9k
p2p/discover: expose discv5 functions for portal JSON-RPC interface #31117
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
Signed-off-by: Chen Kai <[email protected]>
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -200,8 +200,8 @@ func (t *UDPv5) Close() { | |
}) | ||
} | ||
|
||
// Ping sends a ping message to the given node. | ||
func (t *UDPv5) Ping(n *enode.Node) error { | ||
// PingWithoutResp sends a ping message to the given node. | ||
func (t *UDPv5) PingWithoutResp(n *enode.Node) error { | ||
_, err := t.ping(n) | ||
return err | ||
} | ||
|
@@ -226,21 +226,62 @@ func (t *UDPv5) Resolve(n *enode.Node) *enode.Node { | |
return n | ||
} | ||
|
||
// AllNodes returns all the nodes stored in the local table. | ||
func (t *UDPv5) AllNodes() []*enode.Node { | ||
t.tab.mutex.Lock() | ||
defer t.tab.mutex.Unlock() | ||
nodes := make([]*enode.Node, 0) | ||
// ResolveNodeId searches for a specific Node with the given ID. | ||
// It returns nil if the nodeId could not be resolved. | ||
func (t *UDPv5) ResolveNodeId(id enode.ID) *enode.Node { | ||
if id == t.Self().ID() { | ||
return t.Self() | ||
} | ||
|
||
for _, b := range &t.tab.buckets { | ||
for _, n := range b.entries { | ||
nodes = append(nodes, n.Node) | ||
n := t.tab.getNode(id) | ||
if n != nil { | ||
// Try asking directly. This works if the Node is still responding on the endpoint we have. | ||
if resp, err := t.RequestENR(n); err == nil { | ||
return resp | ||
} | ||
} | ||
return nodes | ||
|
||
// Otherwise do a network lookup. | ||
result := t.Lookup(id) | ||
for _, rn := range result { | ||
if rn.ID() == id { | ||
if n != nil && rn.Seq() <= n.Seq() { | ||
return n | ||
} else { | ||
return rn | ||
} | ||
} | ||
} | ||
|
||
return n | ||
} | ||
|
||
// AllNodes returns all the nodes stored in the local table. | ||
func (t *UDPv5) AllNodes() []*enode.Node { | ||
return t.tab.nodeList() | ||
} | ||
|
||
// LocalNode returns the current local node running the | ||
// 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. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should have a comment that says it's only for testing. |
||
func (t *UDPv5) AddKnownNode(n *enode.Node) bool { | ||
return t.tab.addFoundNode(n, true) | ||
} | ||
|
||
// DeleteNode removes a node from the routing table. Used for Portal discv5 DeleteEnr API. | ||
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() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Better to add a config option (in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this change will be made in another PR. |
||
} | ||
|
||
// LocalNode returns the current local Node running the | ||
// protocol. | ||
func (t *UDPv5) LocalNode() *enode.LocalNode { | ||
return t.localNode | ||
|
@@ -328,7 +369,7 @@ func (t *UDPv5) lookupWorker(destNode *enode.Node, target enode.ID) ([]*enode.No | |
err error | ||
) | ||
var r []*enode.Node | ||
r, err = t.findnode(destNode, dists) | ||
r, err = t.Findnode(destNode, dists) | ||
if errors.Is(err, errClosed) { | ||
return nil, err | ||
} | ||
|
@@ -359,21 +400,31 @@ 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) | ||
if err != nil { | ||
return 0, err | ||
} | ||
|
||
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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can just change |
||
req := &v5wire.Ping{ENRSeq: t.localNode.Node().Seq()} | ||
resp := t.callToNode(n, v5wire.PongMsg, req) | ||
defer t.callDone(resp) | ||
|
||
select { | ||
case pong := <-resp.ch: | ||
return pong.(*v5wire.Pong).ENRSeq, nil | ||
return pong.(*v5wire.Pong), nil | ||
case err := <-resp.err: | ||
return 0, err | ||
return nil, err | ||
} | ||
} | ||
|
||
// RequestENR requests n's record. | ||
func (t *UDPv5) RequestENR(n *enode.Node) (*enode.Node, error) { | ||
nodes, err := t.findnode(n, []uint{0}) | ||
nodes, err := t.Findnode(n, []uint{0}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
@@ -383,8 +434,8 @@ func (t *UDPv5) RequestENR(n *enode.Node) (*enode.Node, error) { | |
return nodes[0], nil | ||
} | ||
|
||
// findnode calls FINDNODE on a node and waits for responses. | ||
func (t *UDPv5) findnode(n *enode.Node, distances []uint) ([]*enode.Node, error) { | ||
// Findnode calls FINDNODE on a node and waits for responses. | ||
func (t *UDPv5) Findnode(n *enode.Node, distances []uint) ([]*enode.Node, error) { | ||
resp := t.callToNode(n, v5wire.NodesMsg, &v5wire.Findnode{Distances: distances}) | ||
return t.waitForNodes(resp, distances) | ||
} | ||
|
@@ -736,8 +787,8 @@ func (t *UDPv5) handleCallResponse(fromID enode.ID, fromAddr netip.AddrPort, p v | |
return true | ||
} | ||
|
||
// getNode looks for a node record in table and database. | ||
func (t *UDPv5) getNode(id enode.ID) *enode.Node { | ||
// GetNode looks for a node record in table and database. | ||
func (t *UDPv5) GetNode(id enode.ID) *enode.Node { | ||
if n := t.tab.getNode(id); n != nil { | ||
return n | ||
} | ||
|
@@ -776,7 +827,7 @@ func (t *UDPv5) handle(p v5wire.Packet, fromID enode.ID, fromAddr netip.AddrPort | |
func (t *UDPv5) handleUnknown(p *v5wire.Unknown, fromID enode.ID, fromAddr netip.AddrPort) { | ||
challenge := &v5wire.Whoareyou{Nonce: p.Nonce} | ||
crand.Read(challenge.IDNonce[:]) | ||
if n := t.getNode(fromID); n != nil { | ||
if n := t.GetNode(fromID); n != nil { | ||
challenge.Node = n | ||
challenge.RecordSeq = n.Seq() | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should expose
Table.Nodes
method instead because it has more information.