Skip to content

Commit

Permalink
Revert "fix: removed kademlia and libp2p discrepancy kludge (#2429)"
Browse files Browse the repository at this point in the history
This reverts commit c778dad.
  • Loading branch information
acud committed Jan 31, 2022
1 parent 5b9637b commit d3d9367
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions pkg/topology/kademlia/kademlia.go
Original file line number Diff line number Diff line change
Expand Up @@ -1213,12 +1213,23 @@ func closestPeerFunc(closest *swarm.Address, addr swarm.Address, spf sanctionedP
}
}

func isIn(a swarm.Address, addresses []p2p.Peer) bool {
for _, v := range addresses {
if v.Address.Equal(a) {
return true
}
}
return false
}

// ClosestPeer returns the closest peer to a given address.
func (k *Kad) ClosestPeer(addr swarm.Address, includeSelf bool, filter topology.Filter, skipPeers ...swarm.Address) (swarm.Address, error) {
if k.connectedPeers.Length() == 0 {
return swarm.Address{}, topology.ErrNotFound
}

peers := k.p2p.Peers()
var peersToDisconnect []swarm.Address
closest := swarm.ZeroAddress

if includeSelf && k.reachability == p2p.ReachabilityStatusPublic {
Expand All @@ -1241,6 +1252,13 @@ func (k *Kad) ClosestPeer(addr swarm.Address, includeSelf bool, filter topology.
if closer, _ := peer.Closer(addr, closest); closer {
closest = peer
}
// kludge: hotfix for topology peer inconsistencies bug
if !isIn(peer, peers) {
a := swarm.NewAddress(peer.Bytes())
peersToDisconnect = append(peersToDisconnect, a)
return false, false, nil
}

return false, false, nil
}, filter)

Expand All @@ -1252,6 +1270,10 @@ func (k *Kad) ClosestPeer(addr swarm.Address, includeSelf bool, filter topology.
return swarm.Address{}, topology.ErrNotFound // only for light nodes
}

for _, v := range peersToDisconnect {
k.Disconnected(p2p.Peer{Address: v})
}

// check if self
if closest.Equal(k.base) {
return swarm.Address{}, topology.ErrWantSelf
Expand Down

0 comments on commit d3d9367

Please sign in to comment.