mirror of
https://github.com/status-im/nimbus-eth2.git
synced 2025-01-18 10:31:04 +00:00
handoff useless peers from sync manager directly into branch discovery
This commit is contained in:
parent
08b87e2506
commit
74606c6e1b
@ -445,12 +445,17 @@ proc initFullNode(
|
|||||||
blockProcessor, node.validatorMonitor, dag, attestationPool,
|
blockProcessor, node.validatorMonitor, dag, attestationPool,
|
||||||
validatorChangePool, node.attachedValidators, syncCommitteeMsgPool,
|
validatorChangePool, node.attachedValidators, syncCommitteeMsgPool,
|
||||||
lightClientPool, quarantine, blobQuarantine, rng, getBeaconTime, taskpool)
|
lightClientPool, quarantine, blobQuarantine, rng, getBeaconTime, taskpool)
|
||||||
|
branchDiscovery = BranchDiscovery.new(
|
||||||
|
node.network, getFirstSlotAtFinalizedEpoch, isBlockKnown,
|
||||||
|
branchDiscoveryBlockVerifier)
|
||||||
|
fallbackSyncer = proc(peer: Peer) =
|
||||||
|
branchDiscovery.transferOwnership(peer)
|
||||||
syncManager = newSyncManager[Peer, PeerId](
|
syncManager = newSyncManager[Peer, PeerId](
|
||||||
node.network.peerPool,
|
node.network.peerPool,
|
||||||
dag.cfg.DENEB_FORK_EPOCH, dag.cfg.MIN_EPOCHS_FOR_BLOB_SIDECARS_REQUESTS,
|
dag.cfg.DENEB_FORK_EPOCH, dag.cfg.MIN_EPOCHS_FOR_BLOB_SIDECARS_REQUESTS,
|
||||||
SyncQueueKind.Forward, getLocalHeadSlot,
|
SyncQueueKind.Forward, getLocalHeadSlot,
|
||||||
getLocalWallSlot, getFirstSlotAtFinalizedEpoch, getBackfillSlot,
|
getLocalWallSlot, getFirstSlotAtFinalizedEpoch, getBackfillSlot,
|
||||||
getFrontfillSlot, dag.tail.slot, blockVerifier)
|
getFrontfillSlot, dag.tail.slot, blockVerifier, fallbackSyncer)
|
||||||
backfiller = newSyncManager[Peer, PeerId](
|
backfiller = newSyncManager[Peer, PeerId](
|
||||||
node.network.peerPool,
|
node.network.peerPool,
|
||||||
dag.cfg.DENEB_FORK_EPOCH, dag.cfg.MIN_EPOCHS_FOR_BLOB_SIDECARS_REQUESTS,
|
dag.cfg.DENEB_FORK_EPOCH, dag.cfg.MIN_EPOCHS_FOR_BLOB_SIDECARS_REQUESTS,
|
||||||
@ -458,9 +463,6 @@ proc initFullNode(
|
|||||||
getLocalWallSlot, getFirstSlotAtFinalizedEpoch, getBackfillSlot,
|
getLocalWallSlot, getFirstSlotAtFinalizedEpoch, getBackfillSlot,
|
||||||
getFrontfillSlot, dag.backfill.slot, blockVerifier,
|
getFrontfillSlot, dag.backfill.slot, blockVerifier,
|
||||||
maxHeadAge = 0)
|
maxHeadAge = 0)
|
||||||
branchDiscovery = BranchDiscovery.new(
|
|
||||||
node.network, getFirstSlotAtFinalizedEpoch, isBlockKnown,
|
|
||||||
branchDiscoveryBlockVerifier)
|
|
||||||
router = (ref MessageRouter)(
|
router = (ref MessageRouter)(
|
||||||
processor: processor,
|
processor: processor,
|
||||||
network: node.network)
|
network: node.network)
|
||||||
|
@ -29,7 +29,7 @@
|
|||||||
# with lower validator support while the canonical chain was not visible.
|
# with lower validator support while the canonical chain was not visible.
|
||||||
|
|
||||||
import
|
import
|
||||||
std/algorithm,
|
std/[algorithm, deques],
|
||||||
chronos, chronicles, metrics, results,
|
chronos, chronicles, metrics, results,
|
||||||
../spec/[forks, network],
|
../spec/[forks, network],
|
||||||
../consensus_object_pools/block_pools_types,
|
../consensus_object_pools/block_pools_types,
|
||||||
@ -69,6 +69,7 @@ type
|
|||||||
blockVerifier: BlockVerifierCallback
|
blockVerifier: BlockVerifierCallback
|
||||||
isActive: AsyncEvent
|
isActive: AsyncEvent
|
||||||
loopFuture: Future[void].Raising([])
|
loopFuture: Future[void].Raising([])
|
||||||
|
peerQueue: Deque[Peer]
|
||||||
|
|
||||||
proc new*(
|
proc new*(
|
||||||
T: type BranchDiscovery,
|
T: type BranchDiscovery,
|
||||||
@ -245,11 +246,14 @@ proc loop(self: ref BranchDiscovery) {.async: (raises: []).} =
|
|||||||
await sleepAsync(RESP_TIMEOUT_DUR)
|
await sleepAsync(RESP_TIMEOUT_DUR)
|
||||||
|
|
||||||
let peer =
|
let peer =
|
||||||
try:
|
if self[].peerQueue.len > 0:
|
||||||
self[].network.peerPool.acquireNoWait()
|
self[].peerQueue.popFirst()
|
||||||
except PeerPoolError as exc:
|
else:
|
||||||
debug "Failed to acquire peer", exc = exc.msg
|
try:
|
||||||
continue
|
self[].network.peerPool.acquireNoWait()
|
||||||
|
except PeerPoolError as exc:
|
||||||
|
debug "Failed to acquire peer", exc = exc.msg
|
||||||
|
continue
|
||||||
defer: self[].network.peerPool.release(peer)
|
defer: self[].network.peerPool.release(peer)
|
||||||
|
|
||||||
await self[].discoverBranch(peer)
|
await self[].discoverBranch(peer)
|
||||||
@ -264,6 +268,11 @@ func state*(self: ref BranchDiscovery): BranchDiscoveryState =
|
|||||||
else:
|
else:
|
||||||
BranchDiscoveryState.Active
|
BranchDiscoveryState.Active
|
||||||
|
|
||||||
|
proc clearPeerQueue(self: ref BranchDiscovery) =
|
||||||
|
while self[].peerQueue.len > 0:
|
||||||
|
let peer = self[].peerQueue.popLast()
|
||||||
|
self[].network.peerPool.release(peer)
|
||||||
|
|
||||||
proc start*(self: ref BranchDiscovery) =
|
proc start*(self: ref BranchDiscovery) =
|
||||||
doAssert self[].loopFuture == nil
|
doAssert self[].loopFuture == nil
|
||||||
info "Starting discovery of new branches"
|
info "Starting discovery of new branches"
|
||||||
@ -276,11 +285,23 @@ proc stop*(self: ref BranchDiscovery) {.async: (raises: []).} =
|
|||||||
await self[].loopFuture.cancelAndWait()
|
await self[].loopFuture.cancelAndWait()
|
||||||
self[].loopFuture = nil
|
self[].loopFuture = nil
|
||||||
beacon_sync_branchdiscovery_state.set(self.state.ord().int64)
|
beacon_sync_branchdiscovery_state.set(self.state.ord().int64)
|
||||||
|
self.clearPeerQueue()
|
||||||
|
|
||||||
proc suspend*(self: ref BranchDiscovery) =
|
proc suspend*(self: ref BranchDiscovery) =
|
||||||
self[].isActive.clear()
|
self[].isActive.clear()
|
||||||
beacon_sync_branchdiscovery_state.set(self.state.ord().int64)
|
beacon_sync_branchdiscovery_state.set(self.state.ord().int64)
|
||||||
|
self.clearPeerQueue()
|
||||||
|
|
||||||
proc resume*(self: ref BranchDiscovery) =
|
proc resume*(self: ref BranchDiscovery) =
|
||||||
self[].isActive.fire()
|
self[].isActive.fire()
|
||||||
beacon_sync_branchdiscovery_state.set(self.state.ord().int64)
|
beacon_sync_branchdiscovery_state.set(self.state.ord().int64)
|
||||||
|
|
||||||
|
proc transferOwnership*(self: ref BranchDiscovery, peer: Peer) =
|
||||||
|
const maxPeersInQueue = 10
|
||||||
|
if self.state != BranchDiscoveryState.Active or
|
||||||
|
self[].peerQueue.len >= maxPeersInQueue:
|
||||||
|
self[].network.peerPool.release(peer)
|
||||||
|
return
|
||||||
|
debug "Peer transferred to branch discovery",
|
||||||
|
peer, peer_score = peer.getScore()
|
||||||
|
self[].peerQueue.addLast(peer)
|
||||||
|
@ -35,6 +35,8 @@ const
|
|||||||
## Time time it takes for the peer's status information to expire.
|
## Time time it takes for the peer's status information to expire.
|
||||||
|
|
||||||
type
|
type
|
||||||
|
PeerSyncer*[T] = proc(peer: T) {.gcsafe, raises: [].}
|
||||||
|
|
||||||
SyncWorkerStatus* {.pure.} = enum
|
SyncWorkerStatus* {.pure.} = enum
|
||||||
Sleeping, WaitingPeer, UpdatingStatus, Requesting, Downloading, Queueing,
|
Sleeping, WaitingPeer, UpdatingStatus, Requesting, Downloading, Queueing,
|
||||||
Processing
|
Processing
|
||||||
@ -65,6 +67,7 @@ type
|
|||||||
queue: SyncQueue[A]
|
queue: SyncQueue[A]
|
||||||
syncFut: Future[void]
|
syncFut: Future[void]
|
||||||
blockVerifier: BlockVerifier
|
blockVerifier: BlockVerifier
|
||||||
|
fallbackSyncer: PeerSyncer[A]
|
||||||
inProgress*: bool
|
inProgress*: bool
|
||||||
insSyncSpeed*: float
|
insSyncSpeed*: float
|
||||||
avgSyncSpeed*: float
|
avgSyncSpeed*: float
|
||||||
@ -126,6 +129,7 @@ proc newSyncManager*[A, B](pool: PeerPool[A, B],
|
|||||||
getFrontfillSlotCb: GetSlotCallback,
|
getFrontfillSlotCb: GetSlotCallback,
|
||||||
progressPivot: Slot,
|
progressPivot: Slot,
|
||||||
blockVerifier: BlockVerifier,
|
blockVerifier: BlockVerifier,
|
||||||
|
fallbackSyncer: PeerSyncer[A] = nil,
|
||||||
maxHeadAge = uint64(SLOTS_PER_EPOCH * 1),
|
maxHeadAge = uint64(SLOTS_PER_EPOCH * 1),
|
||||||
chunkSize = uint64(SLOTS_PER_EPOCH),
|
chunkSize = uint64(SLOTS_PER_EPOCH),
|
||||||
flags: set[SyncManagerFlag] = {},
|
flags: set[SyncManagerFlag] = {},
|
||||||
@ -150,6 +154,7 @@ proc newSyncManager*[A, B](pool: PeerPool[A, B],
|
|||||||
maxHeadAge: maxHeadAge,
|
maxHeadAge: maxHeadAge,
|
||||||
chunkSize: chunkSize,
|
chunkSize: chunkSize,
|
||||||
blockVerifier: blockVerifier,
|
blockVerifier: blockVerifier,
|
||||||
|
fallbackSyncer: fallbackSyncer,
|
||||||
notInSyncEvent: newAsyncEvent(),
|
notInSyncEvent: newAsyncEvent(),
|
||||||
direction: direction,
|
direction: direction,
|
||||||
ident: ident,
|
ident: ident,
|
||||||
@ -520,7 +525,13 @@ proc syncWorker[A, B](man: SyncManager[A, B], index: int) {.async: (raises: [Can
|
|||||||
man.workers[index].status = SyncWorkerStatus.WaitingPeer
|
man.workers[index].status = SyncWorkerStatus.WaitingPeer
|
||||||
peer = await man.pool.acquire()
|
peer = await man.pool.acquire()
|
||||||
await man.syncStep(index, peer)
|
await man.syncStep(index, peer)
|
||||||
man.pool.release(peer)
|
if man.workers[index].status < SyncWorkerStatus.Downloading and
|
||||||
|
man.fallbackSyncer != nil:
|
||||||
|
# The peer was not useful for us, hand it over to the fallback syncer.
|
||||||
|
# It is the responsibility of the fallback syncer to release the peer
|
||||||
|
man.fallbackSyncer(peer)
|
||||||
|
else:
|
||||||
|
man.pool.release(peer)
|
||||||
peer = nil
|
peer = nil
|
||||||
finally:
|
finally:
|
||||||
if not(isNil(peer)):
|
if not(isNil(peer)):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user