Add own node checks before sending ping (#548)

Seems there are two occasions possible where we try to ping the
own local node which causes an assert

- One via the bounding
- One via a received ping, which is strange in the first place but
notice in a stack trace
This commit is contained in:
Kim De Mey 2022-11-07 14:25:11 +01:00 committed by GitHub
parent fef47331c3
commit 64b56d866c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 18 additions and 14 deletions

View File

@ -443,7 +443,8 @@ proc findNode*(k: KademliaProtocol, nodesSeen: ref HashSet[Node],
var bondedNodes: seq[Future[bool]] = @[] var bondedNodes: seq[Future[bool]] = @[]
for node in candidates: for node in candidates:
bondedNodes.add(k.bond(node)) if node != k.thisNode:
bondedNodes.add(k.bond(node))
await allFutures(bondedNodes) await allFutures(bondedNodes)
@ -602,22 +603,25 @@ proc recvPing*(k: KademliaProtocol, n: Node, msgHash: any)
k.wire.sendPong(n, msgHash) k.wire.sendPong(n, msgHash)
if getTime() - k.lastPongReceived(n) > BOND_EXPIRATION: if getTime() - k.lastPongReceived(n) > BOND_EXPIRATION:
let pingId = pingId(n, k.ping(n)) # TODO: It is strange that this would occur, as it means our own node would
# have pinged us which should have caused an assert in the first place.
if n != k.thisNode:
let pingId = pingId(n, k.ping(n))
let fut = if pingId in k.pongFutures: let fut = if pingId in k.pongFutures:
k.pongFutures[pingId] k.pongFutures[pingId]
else: else:
k.waitPong(n, pingId) k.waitPong(n, pingId)
let cb = proc(data: pointer) {.gcsafe.} = let cb = proc(data: pointer) {.gcsafe.} =
# fut.read == true if pingid exists # fut.read == true if pingid exists
try: try:
if fut.completed and fut.read: if fut.completed and fut.read:
k.updateRoutingTable(n) k.updateRoutingTable(n)
except CatchableError as ex: except CatchableError as ex:
error "recvPing:WaitPong exception", msg=ex.msg error "recvPing:WaitPong exception", msg=ex.msg
fut.addCallback cb fut.addCallback cb
else: else:
k.updateRoutingTable(n) k.updateRoutingTable(n)