Use async queryRandom instead of inefficient randomNodes to discover nodes (#2211)
This commit is contained in:
parent
67cce36f06
commit
66d8f317cd
|
@ -14,7 +14,7 @@ type
|
||||||
PublicKey = keys.PublicKey
|
PublicKey = keys.PublicKey
|
||||||
|
|
||||||
export
|
export
|
||||||
Eth2DiscoveryProtocol, open, start, close, closeWait, randomNodes,
|
Eth2DiscoveryProtocol, open, start, close, closeWait, queryRandom,
|
||||||
updateRecord, results
|
updateRecord, results
|
||||||
|
|
||||||
proc parseBootstrapAddress*(address: TaintedString):
|
proc parseBootstrapAddress*(address: TaintedString):
|
||||||
|
|
|
@ -875,53 +875,46 @@ proc runDiscoveryLoop*(node: Eth2Node) {.async.} =
|
||||||
let enrField = ("eth2", SSZ.encode(node.forkId))
|
let enrField = ("eth2", SSZ.encode(node.forkId))
|
||||||
|
|
||||||
while true:
|
while true:
|
||||||
# We always request constant number of peers to avoid problem with
|
if node.peerPool.lenSpace({PeerType.Outgoing}) > 0:
|
||||||
# low amount of returned peers.
|
var discoveredNodes = await node.discovery.queryRandom(enrField)
|
||||||
let discoveredNodes = node.discovery.randomNodes(node.wantedPeers, enrField)
|
var newPeers = 0
|
||||||
|
for discNode in discoveredNodes:
|
||||||
|
let res = discNode.toPeerAddr()
|
||||||
|
if res.isOk():
|
||||||
|
let peerAddr = res.get()
|
||||||
|
# Waiting for an empty space in PeerPool.
|
||||||
|
while true:
|
||||||
|
if node.peerPool.lenSpace({PeerType.Outgoing}) == 0:
|
||||||
|
await node.peerPool.waitForEmptySpace(PeerType.Outgoing)
|
||||||
|
else:
|
||||||
|
break
|
||||||
|
# Check if peer present in SeenTable or PeerPool.
|
||||||
|
if node.checkPeer(peerAddr):
|
||||||
|
if peerAddr.peerId notin node.connTable:
|
||||||
|
# We adding to pending connections table here, but going
|
||||||
|
# to remove it only in `connectWorker`.
|
||||||
|
node.connTable.incl(peerAddr.peerId)
|
||||||
|
await node.connQueue.addLast(peerAddr)
|
||||||
|
inc(newPeers)
|
||||||
|
else:
|
||||||
|
debug "Failed to decode discovery's node address",
|
||||||
|
node = discnode, errMsg = res.error
|
||||||
|
|
||||||
var newPeers = 0
|
debug "Discovery tick", wanted_peers = node.wantedPeers,
|
||||||
for discNode in discoveredNodes:
|
space = node.peerPool.shortLogSpace(),
|
||||||
let res = discNode.toPeerAddr()
|
acquired = node.peerPool.shortLogAcquired(),
|
||||||
if res.isOk():
|
available = node.peerPool.shortLogAvailable(),
|
||||||
let peerAddr = res.get()
|
current = node.peerPool.shortLogCurrent(),
|
||||||
# Waiting for an empty space in PeerPool.
|
length = len(node.peerPool),
|
||||||
while true:
|
discovered_nodes = len(discoveredNodes),
|
||||||
if node.peerPool.lenSpace({PeerType.Outgoing}) == 0:
|
new_peers = newPeers
|
||||||
await node.peerPool.waitForEmptySpace(PeerType.Outgoing)
|
|
||||||
else:
|
|
||||||
break
|
|
||||||
# Check if peer present in SeenTable or PeerPool.
|
|
||||||
if node.checkPeer(peerAddr):
|
|
||||||
if peerAddr.peerId notin node.connTable:
|
|
||||||
# We adding to pending connections table here, but going
|
|
||||||
# to remove it only in `connectWorker`.
|
|
||||||
node.connTable.incl(peerAddr.peerId)
|
|
||||||
await node.connQueue.addLast(peerAddr)
|
|
||||||
inc(newPeers)
|
|
||||||
else:
|
|
||||||
debug "Failed to decode discovery's node address",
|
|
||||||
node = discnode, errMsg = res.error
|
|
||||||
|
|
||||||
trace "Discovery tick", wanted_peers = node.wantedPeers,
|
if newPeers == 0:
|
||||||
space = node.peerPool.shortLogSpace(),
|
let currentPeers = node.peerPool.lenCurrent()
|
||||||
acquired = node.peerPool.shortLogAcquired(),
|
if currentPeers <= node.wantedPeers shr 2: # 25%
|
||||||
available = node.peerPool.shortLogAvailable(),
|
warn "Peer count low, no new peers discovered",
|
||||||
current = node.peerPool.shortLogCurrent(),
|
discovered_nodes = len(discoveredNodes), new_peers = newPeers,
|
||||||
length = len(node.peerPool),
|
current_peers = currentPeers, wanted_peers = node.wantedPeers
|
||||||
discovered_nodes = len(discoveredNodes),
|
|
||||||
new_peers = newPeers
|
|
||||||
|
|
||||||
if newPeers == 0:
|
|
||||||
let currentPeers = node.peerPool.lenCurrent()
|
|
||||||
if currentPeers <= node.wantedPeers shr 2: # 25%
|
|
||||||
notice "Peer count low, no new peers discovered",
|
|
||||||
discovered = len(discoveredNodes), new_peers = newPeers,
|
|
||||||
current_peers = currentPeers, wanted_peers = node.wantedPeers
|
|
||||||
elif currentPeers <= node.wantedPeers shr 3: # 12.5 %
|
|
||||||
warn "Peer count low, no new peers discovered",
|
|
||||||
discovered = len(discoveredNodes), new_peers = newPeers,
|
|
||||||
current_peers = currentPeers, wanted_peers = node.wantedPeers
|
|
||||||
await sleepAsync(5.seconds)
|
|
||||||
else:
|
else:
|
||||||
await sleepAsync(1.seconds)
|
await sleepAsync(1.seconds)
|
||||||
|
|
||||||
|
|
|
@ -1 +1 @@
|
||||||
Subproject commit 8de6bd4f48ee6a0bca8dc3905cf68b4cc29a96d6
|
Subproject commit e25500c9f0597e91979a4c714989abd6e01f70d9
|
Loading…
Reference in New Issue