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,53 +875,46 @@ 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()
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
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
debug "Discovery tick", wanted_peers = node.wantedPeers,
space = node.peerPool.shortLogSpace(),
acquired = node.peerPool.shortLogAcquired(),
available = node.peerPool.shortLogAvailable(),
current = node.peerPool.shortLogCurrent(),
length = len(node.peerPool),
discovered_nodes = len(discoveredNodes),
new_peers = newPeers
trace "Discovery tick", wanted_peers = node.wantedPeers,
space = node.peerPool.shortLogSpace(),
acquired = node.peerPool.shortLogAcquired(),
available = node.peerPool.shortLogAvailable(),
current = node.peerPool.shortLogCurrent(),
length = len(node.peerPool),
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)
if newPeers == 0:
let currentPeers = node.peerPool.lenCurrent()
if currentPeers <= node.wantedPeers shr 2: # 25%
warn "Peer count low, no new peers discovered",
discovered_nodes = len(discoveredNodes), new_peers = newPeers,
current_peers = currentPeers, wanted_peers = node.wantedPeers
else:
await sleepAsync(1.seconds)

2
vendor/nim-eth vendored

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