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 & "]" "Node[" & $n.node.address.ip & ":" & $n.node.address.udpPort & "]"
proc hash*(n: Node): hashes.Hash = hash(n.node.pubkey.data) 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 = proc newKBucket(istart, iend: NodeId): KBucket =
result.new() 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. ## 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. ## 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: if n in k.routing:
return true return true
let pid = pingId(n, k.ping(n)) let pid = pingId(n, k.ping(n))
if pid in k.pongFutures: if pid in k.pongFutures:
debug "Binding failed, already waiting for pong", n debug "Bonding failed, already waiting for pong", n
return false return false
let gotPong = await k.waitPong(n, pid) 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) discard await k.waitPing(n)
debug "Bonding completed successfully", n trace "Bonding completed successfully", n
k.updateRoutingTable(n) k.updateRoutingTable(n)
return true return true
@ -424,14 +424,14 @@ proc bootstrap*(k: KademliaProtocol, bootstrapNodes: seq[Node]) {.async.} =
discard await k.lookupRandom() discard await k.lookupRandom()
proc recvPong*(k: KademliaProtocol, n: Node, token: seq[byte]) = proc recvPong*(k: KademliaProtocol, n: Node, token: seq[byte]) =
debug "<<< pong from ", n trace "<<< pong from ", n
let pingid = token & @(n.node.pubkey.data) let pingid = token & @(n.node.pubkey.data)
var future: Future[bool] var future: Future[bool]
if k.pongFutures.take(pingid, future): if k.pongFutures.take(pingid, future):
future.complete(true) future.complete(true)
proc recvPing*(k: KademliaProtocol, n: Node, msgHash: any) = proc recvPing*(k: KademliaProtocol, n: Node, msgHash: any) =
debug "<<< ping from ", n trace "<<< ping from ", n
k.updateRoutingTable(n) k.updateRoutingTable(n)
k.wire.sendPong(n, msgHash) 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 ## 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 ## neighbours_callbacks, which is added (and removed after it's done or timed out) in
## wait_neighbours(). ## wait_neighbours().
debug "Received neighbours", remote, neighbours trace "Received neighbours", remote, neighbours
let cb = k.neighboursCallbacks.getOrDefault(remote) let cb = k.neighboursCallbacks.getOrDefault(remote)
if not cb.isNil: if not cb.isNil:
cb(neighbours) cb(neighbours)

View File

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