p2p/discover: add debug API

This commit is contained in:
Felix Lange 2024-03-26 19:35:48 +01:00
parent bc62df2223
commit 8f4c72c579
3 changed files with 20 additions and 9 deletions

View file

@ -29,6 +29,12 @@ import (
"github.com/ethereum/go-ethereum/p2p/enode"
)
type BucketNode struct {
Node *enode.Node `json:"node"`
AddedAt time.Time `json:"added"`
Checks int `json:"checks"`
}
// node represents a host on the network.
// The fields of Node may not be modified.
type node struct {

View file

@ -150,18 +150,19 @@ func newMeteredTable(t transport, db *enode.DB, cfg Config) (*Table, error) {
}
// Nodes returns all nodes contained in the table.
func (tab *Table) Nodes() []*enode.Node {
if !tab.isInitDone() {
return nil
}
func (tab *Table) Nodes() [][]BucketNode {
tab.mutex.Lock()
defer tab.mutex.Unlock()
var nodes []*enode.Node
for _, b := range &tab.buckets {
for _, n := range b.entries {
nodes = append(nodes, unwrapNode(n))
nodes := make([][]BucketNode, len(tab.buckets))
for i, b := range &tab.buckets {
nodes[i] = make([]BucketNode, len(b.entries))
for j, n := range b.entries {
nodes[i][j] = BucketNode{
Node: &n.Node,
Checks: int(n.livenessChecks),
AddedAt: n.addedAt,
}
}
}
return nodes

View file

@ -375,6 +375,10 @@ func (t *UDPv4) RequestENR(n *enode.Node) (*enode.Node, error) {
return respN, nil
}
func (t *UDPv4) TableBuckets() [][]BucketNode {
return t.tab.Nodes()
}
// pending adds a reply matcher to the pending reply queue.
// see the documentation of type replyMatcher for a detailed explanation.
func (t *UDPv4) pending(id enode.ID, ip net.IP, ptype byte, callback replyMatchFunc) *replyMatcher {