From 6b4dc095e75b2ce3da16d6a178f11b01cb3eec27 Mon Sep 17 00:00:00 2001 From: Chen Kai <281165273grape@gmail.com> Date: Mon, 9 Dec 2024 16:47:18 +0800 Subject: [PATCH] make some comment with discv5 modification Signed-off-by: Chen Kai <281165273grape@gmail.com> --- p2p/discover/table.go | 45 +++++++++++++++++++++--------------------- p2p/discover/v5_udp.go | 33 ++++++++----------------------- 2 files changed, 30 insertions(+), 48 deletions(-) diff --git a/p2p/discover/table.go b/p2p/discover/table.go index 547bf34409..d2c50a969c 100644 --- a/p2p/discover/table.go +++ b/p2p/discover/table.go @@ -174,24 +174,6 @@ func (tab *Table) Nodes() [][]BucketNode { return nodes } -// 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 -} - func (tab *Table) self() *enode.Node { return tab.net.Self() } @@ -525,13 +507,11 @@ func (tab *Table) removeIP(b *bucket, ip netip.Addr) { // The caller must hold tab.mutex. func (tab *Table) handleAddNode(req addNodeOp) bool { if req.node.ID() == tab.self().ID() { - tab.log.Debug("this node is already in table", "id", req.node.ID()) return false } // For nodes from inbound contact, there is an additional safety measure: if the table // is still initializing the node is not added. if req.isInbound && !tab.isInitDone() { - tab.log.Debug("table is not ready") return false } @@ -539,18 +519,15 @@ func (tab *Table) handleAddNode(req addNodeOp) bool { n, _ := tab.bumpInBucket(b, req.node, req.isInbound) if n != nil { // Already in bucket. - tab.log.Debug("the node is already in table", "id", req.node.ID()) return false } if len(b.entries) >= BucketSize { // Bucket full, maybe add as replacement. - tab.log.Debug("the bucket is full and will add in replacement", "id", req.node.ID()) tab.addReplacement(b, req.node) return false } if !tab.addIP(b, req.node.IPAddr()) { // Can't add: IP limit reached. - tab.log.Debug("IP limit reached", "id", req.node.ID()) return false } @@ -718,10 +695,12 @@ 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 } +// NodeIds returns the node IDs in the table. func (tab *Table) NodeIds() [][]string { tab.mutex.Lock() defer tab.mutex.Unlock() @@ -736,13 +715,33 @@ func (tab *Table) NodeIds() [][]string { return nodes } +// Config returns the table's configuration. func (tab *Table) Config() Config { return tab.cfg } +// DeleteNode removes a node from the table. func (tab *Table) DeleteNode(n *enode.Node) { tab.mutex.Lock() defer tab.mutex.Unlock() b := tab.bucket(n.ID()) tab.deleteInBucket(b, n.ID()) } + +// 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 +} diff --git a/p2p/discover/v5_udp.go b/p2p/discover/v5_udp.go index db66231b02..fdf41d5cfc 100644 --- a/p2p/discover/v5_udp.go +++ b/p2p/discover/v5_udp.go @@ -43,6 +43,7 @@ const ( findnodeResultLimit = 16 // applies in FINDNODE handler totalNodesResponseLimit = 5 // applies in waitForNodes + // change to a configurable value respTimeoutV5 = 3 * time.Second ) @@ -62,9 +63,10 @@ type codecV5 interface { // UDPv5 is the implementation of protocol version 5. type UDPv5 struct { // static fields - conn UDPConn - tab *Table - nodeMu sync.Mutex + conn UDPConn + tab *Table + nodeMu sync.Mutex + // addr -> node cache which cached the nodes we have sent packets to or received packets from recently. cachedAddrNode map[string]*enode.Node netrestrict *netutil.Netlist priv *ecdsa.PrivateKey @@ -262,30 +264,11 @@ 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 { - 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 + return t.tab.NodeList() } func (t *UDPv5) RoutingTableInfo() [][]string { - t.tab.mutex.Lock() - defer t.tab.mutex.Unlock() - nodes := make([][]string, 0) - for _, b := range &t.tab.buckets { - bucketNodes := make([]string, 0) - for _, n := range b.entries { - bucketNodes = append(bucketNodes, "0x"+n.ID().String()) - } - nodes = append(nodes, bucketNodes) - } - return nodes + return t.tab.NodeIds() } // LocalNode returns the current local Node running the @@ -602,6 +585,7 @@ func (t *UDPv5) dispatch() { t.sendNextCall(c.id) case r := <-t.sendCh: + // make send request by another thread also be cached to handle WHOAREYOU c := &callV5{id: r.destID, addr: r.destAddr} c.node = r.destNode c.packet = r.msg @@ -619,7 +603,6 @@ func (t *UDPv5) dispatch() { c.nonce = nonce t.activeCallByAuth[nonce] = c t.startResponseTimeout(c) - //t.send(r.destID, r.destAddr, r.msg, nil) case p := <-t.packetInCh: t.handlePacket(p.Data, p.Addr)