This commit is contained in:
thinkAfCod 2024-07-16 13:19:48 +08:00 committed by Chen Kai
parent 3816cc97e3
commit 8de928c004
2 changed files with 12 additions and 1 deletions

View file

@ -290,7 +290,11 @@ func (p *PortalProtocol) AddEnr(n *enode.Node) {
// immediately add the node to the routing table
p.table.mutex.Lock()
defer p.table.mutex.Unlock()
p.table.handleAddNode(addNodeOp{node: n, isInbound: true, forceSetLive: true})
added := p.table.handleAddNode(addNodeOp{node: n, isInbound: true, forceSetLive: true})
if !added {
p.Log.Warn("add node failed", "id", n.ID())
return
}
id := n.ID().String()
p.radiusCache.Set([]byte(id), MaxDistance)
}
@ -1433,11 +1437,13 @@ func (p *PortalProtocol) Resolve(n *enode.Node) *enode.Node {
// It returns nil if the nodeId could not be resolved.
func (p *PortalProtocol) ResolveNodeId(id enode.ID) *enode.Node {
if id == p.Self().ID() {
p.Log.Debug("Resolve Self Id", "id", id.String())
return p.Self()
}
n := p.table.getNode(id)
if n != nil {
p.Log.Debug("found Id in table and will request enr from the node", "id", id.String())
// Try asking directly. This works if the Node is still responding on the endpoint we have.
if resp, err := p.RequestENR(n); err == nil {
return resp

View file

@ -525,11 +525,13 @@ 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
}
@ -537,15 +539,18 @@ 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
}