Slightly change the static peer manager lookup behaviour (#1484)

why:
  The peer manager runs concurrently to the discovery scheme. So the p2p
  peer observer will also present `peer` non-static entries. Previously,
  this peer manager throw an assert defect when this happened.
This commit is contained in:
Jordan Hrycaj 2023-03-06 09:22:07 +00:00 committed by GitHub
parent 10ad7867e4
commit fe04b50fef
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 19 additions and 10 deletions

View File

@ -7,7 +7,10 @@
# This file may not be copied, modified, or distributed except according to
# those terms.
{.push raises: [].}
import
std/[hashes, tables],
chronicles,
chronos,
eth/p2p,
@ -32,27 +35,33 @@ type
pool: PeerPool
maxRetryCount: int # zero == infinite
retryInterval: int # in seconds
reconnectStates: seq[ReconnectState]
reconnectStates: Table[Node,ReconnectState]
reconnectFut: Future[void]
logScope:
topics = "PeerManagerRef"
proc setConnected(pm: PeerManagerRef, peer: Peer, connected: bool) =
for n in pm.reconnectStates:
if peer.remote.id == n.node.id:
n.connected = connected
return
template noKeyError(info: static[string]; code: untyped) =
try:
code
except KeyError as e:
raiseAssert "Not possible (" & info & "): " & e.msg
doAssert(false, "unreachable code")
proc setConnected(pm: PeerManagerRef, peer: Peer, connected: bool) =
if pm.reconnectStates.hasKey(peer.remote):
noKeyError("setConnected"):
pm.reconnectStates[peer.remote].connected = connected
else:
# Peer was not registered a static, so ignore it
trace "Could not update non-static peer", peer, connected
proc needReconnect(pm: PeerManagerRef): bool =
for n in pm.reconnectStates:
for n in pm.reconnectStates.values:
if not n.connected:
return true
proc reconnect(pm: PeerManagerRef) {.async, gcsafe.} =
for n in pm.reconnectStates:
for n in pm.reconnectStates.values:
if not n.connected and pm.state == Running:
if n.retryCount < pm.maxRetryCount or pm.maxRetryCount == 0:
trace "Reconnecting to", remote=n.node.node
@ -93,7 +102,7 @@ proc setupManager(pm: PeerManagerRef, enodes: openArray[ENode]) =
retryCount: 0,
connected: false
)
pm.reconnectStates.add(state)
pm.reconnectStates[state.node] = state
proc new*(_: type PeerManagerRef,
pool: PeerPool,