make some comment with discv5 modification

Signed-off-by: Chen Kai <281165273grape@gmail.com>
This commit is contained in:
Chen Kai 2024-12-09 16:47:18 +08:00
parent db3778434a
commit 6b4dc095e7
2 changed files with 30 additions and 48 deletions

View file

@ -174,24 +174,6 @@ func (tab *Table) Nodes() [][]BucketNode {
return nodes 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 { func (tab *Table) self() *enode.Node {
return tab.net.Self() return tab.net.Self()
} }
@ -525,13 +507,11 @@ func (tab *Table) removeIP(b *bucket, ip netip.Addr) {
// The caller must hold tab.mutex. // The caller must hold tab.mutex.
func (tab *Table) handleAddNode(req addNodeOp) bool { func (tab *Table) handleAddNode(req addNodeOp) bool {
if req.node.ID() == tab.self().ID() { if req.node.ID() == tab.self().ID() {
tab.log.Debug("this node is already in table", "id", req.node.ID())
return false return false
} }
// For nodes from inbound contact, there is an additional safety measure: if the table // For nodes from inbound contact, there is an additional safety measure: if the table
// is still initializing the node is not added. // is still initializing the node is not added.
if req.isInbound && !tab.isInitDone() { if req.isInbound && !tab.isInitDone() {
tab.log.Debug("table is not ready")
return false return false
} }
@ -539,18 +519,15 @@ func (tab *Table) handleAddNode(req addNodeOp) bool {
n, _ := tab.bumpInBucket(b, req.node, req.isInbound) n, _ := tab.bumpInBucket(b, req.node, req.isInbound)
if n != nil { if n != nil {
// Already in bucket. // Already in bucket.
tab.log.Debug("the node is already in table", "id", req.node.ID())
return false return false
} }
if len(b.entries) >= BucketSize { if len(b.entries) >= BucketSize {
// Bucket full, maybe add as replacement. // 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) tab.addReplacement(b, req.node)
return false return false
} }
if !tab.addIP(b, req.node.IPAddr()) { if !tab.addIP(b, req.node.IPAddr()) {
// Can't add: IP limit reached. // Can't add: IP limit reached.
tab.log.Debug("IP limit reached", "id", req.node.ID())
return false return false
} }
@ -718,10 +695,12 @@ func pushNode(list []*tableNode, n *tableNode, max int) ([]*tableNode, *tableNod
return list, removed return list, removed
} }
// WaitInit waits until the table is initialized.
func (tab *Table) WaitInit() { func (tab *Table) WaitInit() {
<-tab.initDone <-tab.initDone
} }
// NodeIds returns the node IDs in the table.
func (tab *Table) NodeIds() [][]string { func (tab *Table) NodeIds() [][]string {
tab.mutex.Lock() tab.mutex.Lock()
defer tab.mutex.Unlock() defer tab.mutex.Unlock()
@ -736,13 +715,33 @@ func (tab *Table) NodeIds() [][]string {
return nodes return nodes
} }
// Config returns the table's configuration.
func (tab *Table) Config() Config { func (tab *Table) Config() Config {
return tab.cfg return tab.cfg
} }
// DeleteNode removes a node from the table.
func (tab *Table) DeleteNode(n *enode.Node) { func (tab *Table) DeleteNode(n *enode.Node) {
tab.mutex.Lock() tab.mutex.Lock()
defer tab.mutex.Unlock() defer tab.mutex.Unlock()
b := tab.bucket(n.ID()) b := tab.bucket(n.ID())
tab.deleteInBucket(b, 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
}

View file

@ -43,6 +43,7 @@ const (
findnodeResultLimit = 16 // applies in FINDNODE handler findnodeResultLimit = 16 // applies in FINDNODE handler
totalNodesResponseLimit = 5 // applies in waitForNodes totalNodesResponseLimit = 5 // applies in waitForNodes
// change to a configurable value
respTimeoutV5 = 3 * time.Second respTimeoutV5 = 3 * time.Second
) )
@ -62,9 +63,10 @@ type codecV5 interface {
// UDPv5 is the implementation of protocol version 5. // UDPv5 is the implementation of protocol version 5.
type UDPv5 struct { type UDPv5 struct {
// static fields // static fields
conn UDPConn conn UDPConn
tab *Table tab *Table
nodeMu sync.Mutex 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 cachedAddrNode map[string]*enode.Node
netrestrict *netutil.Netlist netrestrict *netutil.Netlist
priv *ecdsa.PrivateKey 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. // AllNodes returns all the nodes stored in the local table.
func (t *UDPv5) AllNodes() []*enode.Node { func (t *UDPv5) AllNodes() []*enode.Node {
t.tab.mutex.Lock() return t.tab.NodeList()
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
} }
func (t *UDPv5) RoutingTableInfo() [][]string { func (t *UDPv5) RoutingTableInfo() [][]string {
t.tab.mutex.Lock() return t.tab.NodeIds()
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
} }
// LocalNode returns the current local Node running the // LocalNode returns the current local Node running the
@ -602,6 +585,7 @@ func (t *UDPv5) dispatch() {
t.sendNextCall(c.id) t.sendNextCall(c.id)
case r := <-t.sendCh: 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 := &callV5{id: r.destID, addr: r.destAddr}
c.node = r.destNode c.node = r.destNode
c.packet = r.msg c.packet = r.msg
@ -619,7 +603,6 @@ func (t *UDPv5) dispatch() {
c.nonce = nonce c.nonce = nonce
t.activeCallByAuth[nonce] = c t.activeCallByAuth[nonce] = c
t.startResponseTimeout(c) t.startResponseTimeout(c)
//t.send(r.destID, r.destAddr, r.msg, nil)
case p := <-t.packetInCh: case p := <-t.packetInCh:
t.handlePacket(p.Data, p.Addr) t.handlePacket(p.Data, p.Addr)