Fixed PeerPool error when no bootnodes given

This commit is contained in:
Yuriy Glukhov 2019-02-26 14:47:26 +02:00 committed by zah
parent daafd991d5
commit 07a0c5443f
2 changed files with 13 additions and 12 deletions

View File

@ -77,7 +77,7 @@ proc `$`*(n: Node): string =
"Node[" & $n.node.address.ip & ":" & $n.node.address.udpPort & "]"
proc hash*(n: Node): hashes.Hash = hash(n.node.pubkey.data)
proc `==`*(a, b: Node): bool = a.node.pubkey == b.node.pubkey
proc `==`*(a, b: Node): bool = (a.isNil and b.isNil) or (not a.isNil and not b.isNil and a.node.pubkey == b.node.pubkey)
proc newKBucket(istart, iend: NodeId): KBucket =
result.new()
@ -322,13 +322,13 @@ proc bond(k: KademliaProtocol, n: Node): Future[bool] {.async.} =
##
## Bonding consists of pinging the node, waiting for a pong and maybe a ping as well.
## It is necessary to do this at least once before we send findNode requests to a node.
info "Bonding to peer", n
trace "Bonding to peer", n
if n in k.routing:
return true
let pid = pingId(n, k.ping(n))
if pid in k.pongFutures:
debug "Binding failed, already waiting for pong", n
debug "Bonding failed, already waiting for pong", n
return false
let gotPong = await k.waitPong(n, pid)
@ -349,7 +349,7 @@ proc bond(k: KademliaProtocol, n: Node): Future[bool] {.async.} =
discard await k.waitPing(n)
debug "Bonding completed successfully", n
trace "Bonding completed successfully", n
k.updateRoutingTable(n)
return true
@ -424,14 +424,14 @@ proc bootstrap*(k: KademliaProtocol, bootstrapNodes: seq[Node]) {.async.} =
discard await k.lookupRandom()
proc recvPong*(k: KademliaProtocol, n: Node, token: seq[byte]) =
debug "<<< pong from ", n
trace "<<< pong from ", n
let pingid = token & @(n.node.pubkey.data)
var future: Future[bool]
if k.pongFutures.take(pingid, future):
future.complete(true)
proc recvPing*(k: KademliaProtocol, n: Node, msgHash: any) =
debug "<<< ping from ", n
trace "<<< ping from ", n
k.updateRoutingTable(n)
k.wire.sendPong(n, msgHash)
@ -446,7 +446,7 @@ proc recvNeighbours*(k: KademliaProtocol, remote: Node, neighbours: seq[Node]) =
## done as part of node lookup, so the actual processing is left to the callback from
## neighbours_callbacks, which is added (and removed after it's done or timed out) in
## wait_neighbours().
debug "Received neighbours", remote, neighbours
trace "Received neighbours", remote, neighbours
let cb = k.neighboursCallbacks.getOrDefault(remote)
if not cb.isNil:
cb(neighbours)

View File

@ -2,7 +2,7 @@
# on the given network.
import
os, tables, times, random, sequtils,
os, tables, times, random, sequtils, options,
chronos, chronicles, eth/[rlp, keys],
private/p2p_types, discovery, kademlia, rlpx
@ -99,8 +99,9 @@ proc lookupRandomNode(p: PeerPool) {.async.} =
discard
p.lastLookupTime = epochTime()
proc getRandomBootnode(p: PeerPool): Node =
p.discovery.bootstrapNodes.rand()
proc getRandomBootnode(p: PeerPool): Option[Node] =
if p.discovery.bootstrapNodes.len != 0:
result = option(p.discovery.bootstrapNodes.rand())
proc addPeer*(pool: PeerPool, peer: Peer): bool =
if peer.remote notin pool.connectedNodes:
@ -157,8 +158,8 @@ proc maybeConnectToMorePeers(p: PeerPool) {.async.} =
# In some cases (e.g ROPSTEN or private testnets), the discovery table might
# be full of bad peers, so if we can't connect to any peers we try a random
# bootstrap node as well.
if p.connectedNodes.len == 0:
await p.connectToNode(p.getRandomBootnode())
if p.connectedNodes.len == 0 and (let n = p.getRandomBootnode(); n.isSome):
await p.connectToNode(n.get())
proc run(p: PeerPool) {.async.} =
trace "Running PeerPool..."