Use async queryRandom instead of inefficient randomNodes to discover nodes (#2211)

This commit is contained in:
Kim De Mey 2021-01-14 08:58:13 +01:00 committed by GitHub
parent 67cce36f06
commit 66d8f317cd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 40 additions and 47 deletions

View File

@ -14,7 +14,7 @@ type
PublicKey = keys.PublicKey
export
Eth2DiscoveryProtocol, open, start, close, closeWait, randomNodes,
Eth2DiscoveryProtocol, open, start, close, closeWait, queryRandom,
updateRecord, results
proc parseBootstrapAddress*(address: TaintedString):

View File

@ -875,10 +875,8 @@ proc runDiscoveryLoop*(node: Eth2Node) {.async.} =
let enrField = ("eth2", SSZ.encode(node.forkId))
while true:
# We always request constant number of peers to avoid problem with
# low amount of returned peers.
let discoveredNodes = node.discovery.randomNodes(node.wantedPeers, enrField)
if node.peerPool.lenSpace({PeerType.Outgoing}) > 0:
var discoveredNodes = await node.discovery.queryRandom(enrField)
var newPeers = 0
for discNode in discoveredNodes:
let res = discNode.toPeerAddr()
@ -902,7 +900,7 @@ proc runDiscoveryLoop*(node: Eth2Node) {.async.} =
debug "Failed to decode discovery's node address",
node = discnode, errMsg = res.error
trace "Discovery tick", wanted_peers = node.wantedPeers,
debug "Discovery tick", wanted_peers = node.wantedPeers,
space = node.peerPool.shortLogSpace(),
acquired = node.peerPool.shortLogAcquired(),
available = node.peerPool.shortLogAvailable(),
@ -914,14 +912,9 @@ proc runDiscoveryLoop*(node: Eth2Node) {.async.} =
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,
discovered_nodes = len(discoveredNodes), new_peers = newPeers,
current_peers = currentPeers, wanted_peers = node.wantedPeers
await sleepAsync(5.seconds)
else:
await sleepAsync(1.seconds)

2
vendor/nim-eth vendored

@ -1 +1 @@
Subproject commit 8de6bd4f48ee6a0bca8dc3905cf68b4cc29a96d6
Subproject commit e25500c9f0597e91979a4c714989abd6e01f70d9