2021-03-26 06:52:01 +00:00
|
|
|
|
# beacon_chain
|
|
|
|
|
# Copyright (c) 2018-2021 Status Research & Development GmbH
|
|
|
|
|
# Licensed and distributed under either of
|
|
|
|
|
# * MIT license (license terms in the root directory or at https://opensource.org/licenses/MIT).
|
|
|
|
|
# * Apache v2 license (license terms in the root directory or at https://www.apache.org/licenses/LICENSE-2.0).
|
|
|
|
|
# at your option. This file may not be copied, modified, or distributed except according to those terms.
|
|
|
|
|
|
|
|
|
|
{.push raises: [Defect].}
|
|
|
|
|
|
2020-01-21 18:30:21 +00:00
|
|
|
|
import chronicles
|
2020-06-03 08:46:29 +00:00
|
|
|
|
import options, deques, heapqueue, tables, strutils, sequtils, math, algorithm
|
2020-06-03 13:52:02 +00:00
|
|
|
|
import stew/results, chronos, chronicles
|
2021-07-15 19:01:07 +00:00
|
|
|
|
import ../spec/[datatypes/phase0, datatypes/altair, digest, helpers, eth2_apis/callsigs_types, forkedbeaconstate_helpers],
|
2021-03-05 13:12:00 +00:00
|
|
|
|
../networking/[peer_pool, eth2_network]
|
2020-05-28 05:02:28 +00:00
|
|
|
|
|
2021-05-28 16:34:00 +00:00
|
|
|
|
import ../gossip_processing/block_processor
|
2021-03-04 09:13:44 +00:00
|
|
|
|
import ../consensus_object_pools/block_pools_types
|
2021-07-15 19:01:07 +00:00
|
|
|
|
export phase0, altair, digest, chronos, chronicles, results, block_pools_types,
|
2021-05-18 10:25:14 +00:00
|
|
|
|
helpers
|
2020-01-21 18:30:21 +00:00
|
|
|
|
|
2020-01-22 12:47:55 +00:00
|
|
|
|
logScope:
|
|
|
|
|
topics = "syncman"
|
2020-01-21 18:30:21 +00:00
|
|
|
|
|
2020-04-23 15:31:00 +00:00
|
|
|
|
const
|
|
|
|
|
PeerScoreNoStatus* = -100
|
|
|
|
|
## Peer did not answer `status` request.
|
|
|
|
|
PeerScoreStaleStatus* = -50
|
|
|
|
|
## Peer's `status` answer do not progress in time.
|
2020-10-30 12:33:52 +00:00
|
|
|
|
PeerScoreUseless* = -10
|
|
|
|
|
## Peer's latest head is lower then ours.
|
2020-04-23 15:31:00 +00:00
|
|
|
|
PeerScoreGoodStatus* = 50
|
|
|
|
|
## Peer's `status` answer is fine.
|
|
|
|
|
PeerScoreNoBlocks* = -100
|
|
|
|
|
## Peer did not respond in time on `blocksByRange` request.
|
|
|
|
|
PeerScoreGoodBlocks* = 100
|
2020-06-03 08:46:29 +00:00
|
|
|
|
## Peer's `blocksByRange` answer is fine.
|
2020-05-19 12:08:50 +00:00
|
|
|
|
PeerScoreBadBlocks* = -1000
|
2020-06-03 08:46:29 +00:00
|
|
|
|
## Peer's response contains incorrect blocks.
|
|
|
|
|
PeerScoreBadResponse* = -1000
|
|
|
|
|
## Peer's response is not in requested range.
|
2020-06-15 19:41:26 +00:00
|
|
|
|
PeerScoreMissingBlocks* = -200
|
2020-06-03 08:46:29 +00:00
|
|
|
|
## Peer response contains too many empty blocks.
|
2020-04-23 15:31:00 +00:00
|
|
|
|
|
2020-11-26 08:19:27 +00:00
|
|
|
|
SyncWorkersCount* = 10
|
2020-09-11 12:46:01 +00:00
|
|
|
|
## Number of sync workers to spawn
|
|
|
|
|
|
2020-09-23 15:58:02 +00:00
|
|
|
|
StatusUpdateInterval* = chronos.minutes(1)
|
|
|
|
|
## Minimum time between two subsequent calls to update peer's status
|
|
|
|
|
|
2020-10-08 12:50:48 +00:00
|
|
|
|
StatusExpirationTime* = chronos.minutes(2)
|
|
|
|
|
## Time time it takes for the peer's status information to expire.
|
|
|
|
|
|
2020-01-21 18:30:21 +00:00
|
|
|
|
type
|
2020-05-28 05:02:28 +00:00
|
|
|
|
SyncFailureKind* = enum
|
|
|
|
|
StatusInvalid,
|
|
|
|
|
StatusDownload,
|
|
|
|
|
StatusStale,
|
|
|
|
|
EmptyProblem,
|
2020-06-03 08:46:29 +00:00
|
|
|
|
BlockDownload,
|
|
|
|
|
BadResponse
|
2020-05-28 05:02:28 +00:00
|
|
|
|
|
2020-04-20 14:59:18 +00:00
|
|
|
|
GetSlotCallback* = proc(): Slot {.gcsafe, raises: [Defect].}
|
2020-01-21 18:30:21 +00:00
|
|
|
|
|
2020-05-19 12:08:50 +00:00
|
|
|
|
SyncRequest*[T] = object
|
2020-05-28 05:02:28 +00:00
|
|
|
|
index*: uint64
|
2020-01-21 18:30:21 +00:00
|
|
|
|
slot*: Slot
|
|
|
|
|
count*: uint64
|
|
|
|
|
step*: uint64
|
2020-05-19 12:08:50 +00:00
|
|
|
|
item*: T
|
2020-01-21 18:30:21 +00:00
|
|
|
|
|
2020-05-19 12:08:50 +00:00
|
|
|
|
SyncResult*[T] = object
|
|
|
|
|
request*: SyncRequest[T]
|
2021-07-15 19:01:07 +00:00
|
|
|
|
data*: seq[ForkedSignedBeaconBlock]
|
2020-01-21 18:30:21 +00:00
|
|
|
|
|
2020-06-03 08:46:29 +00:00
|
|
|
|
SyncWaiter*[T] = object
|
|
|
|
|
future: Future[bool]
|
|
|
|
|
request: SyncRequest[T]
|
|
|
|
|
|
2020-10-30 12:33:52 +00:00
|
|
|
|
RewindPoint = object
|
|
|
|
|
failSlot: Slot
|
|
|
|
|
epochCount: uint64
|
|
|
|
|
|
2020-05-19 12:08:50 +00:00
|
|
|
|
SyncQueue*[T] = ref object
|
2020-01-21 18:30:21 +00:00
|
|
|
|
inpSlot*: Slot
|
|
|
|
|
outSlot*: Slot
|
|
|
|
|
startSlot*: Slot
|
|
|
|
|
lastSlot: Slot
|
|
|
|
|
chunkSize*: uint64
|
|
|
|
|
queueSize*: int
|
2020-05-28 05:02:28 +00:00
|
|
|
|
counter*: uint64
|
2020-10-30 12:33:52 +00:00
|
|
|
|
opcounter*: uint64
|
2020-06-03 08:46:29 +00:00
|
|
|
|
pending*: Table[uint64, SyncRequest[T]]
|
|
|
|
|
waiters: seq[SyncWaiter[T]]
|
2020-07-06 12:53:48 +00:00
|
|
|
|
getFinalizedSlot*: GetSlotCallback
|
2020-05-19 12:08:50 +00:00
|
|
|
|
debtsQueue: HeapQueue[SyncRequest[T]]
|
2020-01-21 18:30:21 +00:00
|
|
|
|
debtsCount: uint64
|
2020-05-19 12:08:50 +00:00
|
|
|
|
readyQueue: HeapQueue[SyncResult[T]]
|
2020-10-30 12:33:52 +00:00
|
|
|
|
rewind: Option[RewindPoint]
|
2021-05-28 16:34:00 +00:00
|
|
|
|
blockProcessor: ref BlockProcessor
|
2020-04-20 14:59:18 +00:00
|
|
|
|
|
2020-09-11 12:46:01 +00:00
|
|
|
|
SyncWorkerStatus* {.pure.} = enum
|
|
|
|
|
Sleeping, WaitingPeer, UpdatingStatus, Requesting, Downloading, Processing
|
|
|
|
|
|
|
|
|
|
SyncWorker*[A, B] = object
|
|
|
|
|
future: Future[void]
|
|
|
|
|
status: SyncWorkerStatus
|
|
|
|
|
|
2020-04-20 14:59:18 +00:00
|
|
|
|
SyncManager*[A, B] = ref object
|
|
|
|
|
pool: PeerPool[A, B]
|
|
|
|
|
responseTimeout: chronos.Duration
|
|
|
|
|
sleepTime: chronos.Duration
|
|
|
|
|
maxStatusAge: uint64
|
|
|
|
|
maxHeadAge: uint64
|
|
|
|
|
toleranceValue: uint64
|
|
|
|
|
getLocalHeadSlot: GetSlotCallback
|
|
|
|
|
getLocalWallSlot: GetSlotCallback
|
2020-07-06 12:53:48 +00:00
|
|
|
|
getFinalizedSlot: GetSlotCallback
|
2020-09-11 12:46:01 +00:00
|
|
|
|
workers: array[SyncWorkersCount, SyncWorker[A, B]]
|
|
|
|
|
notInSyncEvent: AsyncEvent
|
|
|
|
|
rangeAge: uint64
|
|
|
|
|
inRangeEvent*: AsyncEvent
|
|
|
|
|
notInRangeEvent*: AsyncEvent
|
2020-04-23 15:31:00 +00:00
|
|
|
|
chunkSize: uint64
|
2020-05-19 12:08:50 +00:00
|
|
|
|
queue: SyncQueue[A]
|
2020-08-10 07:15:50 +00:00
|
|
|
|
syncFut: Future[void]
|
2021-05-28 16:34:00 +00:00
|
|
|
|
blockProcessor: ref BlockProcessor
|
2020-06-03 08:46:29 +00:00
|
|
|
|
inProgress*: bool
|
2020-10-27 09:25:28 +00:00
|
|
|
|
insSyncSpeed*: float
|
|
|
|
|
avgSyncSpeed*: float
|
2020-11-12 11:16:51 +00:00
|
|
|
|
timeLeft*: Duration
|
2020-10-27 09:25:28 +00:00
|
|
|
|
syncCount*: uint64
|
2020-09-11 12:46:01 +00:00
|
|
|
|
syncStatus*: string
|
2020-05-19 12:08:50 +00:00
|
|
|
|
|
|
|
|
|
SyncMoment* = object
|
|
|
|
|
stamp*: chronos.Moment
|
|
|
|
|
slot*: Slot
|
2020-01-21 18:30:21 +00:00
|
|
|
|
|
2020-05-28 05:02:28 +00:00
|
|
|
|
SyncFailure*[T] = object
|
|
|
|
|
kind*: SyncFailureKind
|
|
|
|
|
peer*: T
|
|
|
|
|
stamp*: chronos.Moment
|
|
|
|
|
|
2020-01-21 18:30:21 +00:00
|
|
|
|
SyncManagerError* = object of CatchableError
|
2021-07-15 19:01:07 +00:00
|
|
|
|
BeaconBlocksRes* = NetRes[seq[ForkedSignedBeaconBlock]]
|
2020-04-20 14:59:18 +00:00
|
|
|
|
|
2020-08-10 07:15:50 +00:00
|
|
|
|
proc validate*[T](sq: SyncQueue[T],
|
2021-07-15 19:01:07 +00:00
|
|
|
|
blk: ForkedSignedBeaconBlock): Future[Result[void, BlockError]] =
|
2021-05-28 16:34:00 +00:00
|
|
|
|
let resfut = newFuture[Result[void, BlockError]]("sync.manager.validate")
|
|
|
|
|
sq.blockProcessor[].addBlock(blk, resfut)
|
|
|
|
|
resfut
|
2020-08-10 07:15:50 +00:00
|
|
|
|
|
2020-05-19 12:08:50 +00:00
|
|
|
|
proc getShortMap*[T](req: SyncRequest[T],
|
2021-07-15 19:01:07 +00:00
|
|
|
|
data: openArray[ForkedSignedBeaconBlock]): string =
|
2020-05-13 06:37:58 +00:00
|
|
|
|
## Returns all slot numbers in ``data`` as placement map.
|
|
|
|
|
var res = newStringOfCap(req.count)
|
|
|
|
|
var slider = req.slot
|
|
|
|
|
var last = 0
|
|
|
|
|
for i in 0 ..< req.count:
|
|
|
|
|
if last < len(data):
|
|
|
|
|
for k in last ..< len(data):
|
2021-07-15 19:01:07 +00:00
|
|
|
|
if slider == data[k].slot:
|
2020-05-13 06:37:58 +00:00
|
|
|
|
res.add('x')
|
|
|
|
|
last = k + 1
|
|
|
|
|
break
|
2021-07-15 19:01:07 +00:00
|
|
|
|
elif slider < data[k].slot:
|
2020-05-13 06:37:58 +00:00
|
|
|
|
res.add('.')
|
|
|
|
|
break
|
|
|
|
|
else:
|
|
|
|
|
res.add('.')
|
|
|
|
|
slider = slider + req.step
|
|
|
|
|
result = res
|
|
|
|
|
|
2020-06-03 08:46:29 +00:00
|
|
|
|
proc contains*[T](req: SyncRequest[T], slot: Slot): bool {.inline.} =
|
|
|
|
|
slot >= req.slot and slot < req.slot + req.count * req.step and
|
|
|
|
|
((slot - req.slot) mod req.step == 0)
|
|
|
|
|
|
|
|
|
|
proc cmp*[T](a, b: SyncRequest[T]): int =
|
|
|
|
|
result = cmp(uint64(a.slot), uint64(b.slot))
|
|
|
|
|
|
|
|
|
|
proc checkResponse*[T](req: SyncRequest[T],
|
2021-07-15 19:01:07 +00:00
|
|
|
|
data: openArray[ForkedSignedBeaconBlock]): bool =
|
2020-06-03 08:46:29 +00:00
|
|
|
|
if len(data) == 0:
|
|
|
|
|
# Impossible to verify empty response.
|
|
|
|
|
return true
|
|
|
|
|
|
|
|
|
|
if uint64(len(data)) > req.count:
|
|
|
|
|
# Number of blocks in response should be less or equal to number of
|
|
|
|
|
# requested blocks.
|
|
|
|
|
return false
|
|
|
|
|
|
|
|
|
|
var slot = req.slot
|
|
|
|
|
var rindex = 0'u64
|
|
|
|
|
var dindex = 0
|
|
|
|
|
|
|
|
|
|
while (rindex < req.count) and (dindex < len(data)):
|
2021-07-15 19:01:07 +00:00
|
|
|
|
if slot < data[dindex].slot:
|
2020-06-03 08:46:29 +00:00
|
|
|
|
discard
|
2021-07-15 19:01:07 +00:00
|
|
|
|
elif slot == data[dindex].slot:
|
2020-06-03 08:46:29 +00:00
|
|
|
|
inc(dindex)
|
|
|
|
|
else:
|
|
|
|
|
return false
|
|
|
|
|
slot = slot + req.step
|
|
|
|
|
rindex = rindex + 1'u64
|
|
|
|
|
|
|
|
|
|
if dindex == len(data):
|
|
|
|
|
return true
|
|
|
|
|
else:
|
|
|
|
|
return false
|
|
|
|
|
|
2020-05-19 12:08:50 +00:00
|
|
|
|
proc getFullMap*[T](req: SyncRequest[T],
|
2021-07-15 19:01:07 +00:00
|
|
|
|
data: openArray[ForkedSignedBeaconBlock]): string =
|
2020-05-13 06:37:58 +00:00
|
|
|
|
# Returns all slot numbers in ``data`` as comma-delimeted string.
|
|
|
|
|
result = mapIt(data, $it.message.slot).join(", ")
|
|
|
|
|
|
2020-05-19 12:08:50 +00:00
|
|
|
|
proc init*[T](t1: typedesc[SyncRequest], t2: typedesc[T], slot: Slot,
|
2020-11-20 10:00:22 +00:00
|
|
|
|
count: uint64): SyncRequest[T] =
|
2020-05-19 12:08:50 +00:00
|
|
|
|
result = SyncRequest[T](slot: slot, count: count, step: 1'u64)
|
|
|
|
|
|
|
|
|
|
proc init*[T](t1: typedesc[SyncRequest], t2: typedesc[T], start: Slot,
|
2020-11-20 10:00:22 +00:00
|
|
|
|
finish: Slot): SyncRequest[T] =
|
2020-05-19 12:08:50 +00:00
|
|
|
|
let count = finish - start + 1'u64
|
|
|
|
|
result = SyncRequest[T](slot: start, count: count, step: 1'u64)
|
|
|
|
|
|
|
|
|
|
proc init*[T](t1: typedesc[SyncRequest], t2: typedesc[T], slot: Slot,
|
2020-11-20 10:00:22 +00:00
|
|
|
|
count: uint64, item: T): SyncRequest[T] =
|
2020-05-19 12:08:50 +00:00
|
|
|
|
result = SyncRequest[T](slot: slot, count: count, item: item, step: 1'u64)
|
2020-04-20 14:59:18 +00:00
|
|
|
|
|
2020-05-19 12:08:50 +00:00
|
|
|
|
proc init*[T](t1: typedesc[SyncRequest], t2: typedesc[T], start: Slot,
|
2020-11-20 10:00:22 +00:00
|
|
|
|
finish: Slot, item: T): SyncRequest[T] =
|
2020-04-20 14:59:18 +00:00
|
|
|
|
let count = finish - start + 1'u64
|
2020-05-19 12:08:50 +00:00
|
|
|
|
result = SyncRequest[T](slot: start, count: count, step: 1'u64, item: item)
|
|
|
|
|
|
2020-05-28 05:02:28 +00:00
|
|
|
|
proc init*[T](t1: typedesc[SyncFailure], kind: SyncFailureKind,
|
2020-11-20 10:00:22 +00:00
|
|
|
|
peer: T): SyncFailure[T] =
|
2020-05-28 05:02:28 +00:00
|
|
|
|
result = SyncFailure[T](kind: kind, peer: peer, stamp: now(chronos.Moment))
|
|
|
|
|
|
2020-05-19 12:08:50 +00:00
|
|
|
|
proc empty*[T](t: typedesc[SyncRequest],
|
|
|
|
|
t2: typedesc[T]): SyncRequest[T] {.inline.} =
|
|
|
|
|
result = SyncRequest[T](step: 0'u64, count: 0'u64)
|
2020-04-20 14:59:18 +00:00
|
|
|
|
|
2020-05-19 12:08:50 +00:00
|
|
|
|
proc setItem*[T](sr: var SyncRequest[T], item: T) =
|
|
|
|
|
sr.item = item
|
2020-04-20 14:59:18 +00:00
|
|
|
|
|
2020-05-19 12:08:50 +00:00
|
|
|
|
proc isEmpty*[T](sr: SyncRequest[T]): bool {.inline.} =
|
2020-04-20 14:59:18 +00:00
|
|
|
|
result = (sr.step == 0'u64) and (sr.count == 0'u64)
|
2020-01-21 18:30:21 +00:00
|
|
|
|
|
2020-05-19 12:08:50 +00:00
|
|
|
|
proc init*[T](t1: typedesc[SyncQueue], t2: typedesc[T],
|
|
|
|
|
start, last: Slot, chunkSize: uint64,
|
2020-07-06 12:53:48 +00:00
|
|
|
|
getFinalizedSlotCb: GetSlotCallback,
|
2021-05-28 16:34:00 +00:00
|
|
|
|
blockProcessor: ref BlockProcessor,
|
2021-03-11 10:10:57 +00:00
|
|
|
|
syncQueueSize: int = -1): SyncQueue[T] =
|
2020-01-21 18:30:21 +00:00
|
|
|
|
## Create new synchronization queue with parameters
|
|
|
|
|
##
|
|
|
|
|
## ``start`` and ``last`` are starting and finishing Slots.
|
|
|
|
|
##
|
|
|
|
|
## ``chunkSize`` maximum number of slots in one request.
|
|
|
|
|
##
|
2021-03-11 10:10:57 +00:00
|
|
|
|
## ``syncQueueSize`` maximum queue size for incoming data. If ``syncQueueSize > 0``
|
|
|
|
|
## queue will help to keep backpressure under control. If ``syncQueueSize <= 0``
|
2020-01-21 18:30:21 +00:00
|
|
|
|
## then queue size is unlimited (default).
|
|
|
|
|
##
|
|
|
|
|
## ``updateCb`` procedure which will be used to send downloaded blocks to
|
2020-04-20 14:59:18 +00:00
|
|
|
|
## consumer. Procedure should return ``false`` only when it receives
|
|
|
|
|
## incorrect blocks, and ``true`` if sequence of blocks is correct.
|
2020-05-19 12:08:50 +00:00
|
|
|
|
|
|
|
|
|
# SyncQueue is the core of sync manager, this data structure distributes
|
|
|
|
|
# requests to peers and manages responses from peers.
|
|
|
|
|
#
|
|
|
|
|
# Because SyncQueue is async data structure it manages backpressure and
|
|
|
|
|
# order of incoming responses and it also resolves "joker's" problem.
|
|
|
|
|
#
|
|
|
|
|
# Joker's problem
|
|
|
|
|
#
|
|
|
|
|
# According to current Ethereum2 network specification
|
|
|
|
|
# > Clients MUST respond with at least one block, if they have it and it
|
|
|
|
|
# > exists in the range. Clients MAY limit the number of blocks in the
|
|
|
|
|
# > response.
|
|
|
|
|
#
|
|
|
|
|
# Such rule can lead to very uncertain responses, for example let slots from
|
|
|
|
|
# 10 to 12 will be not empty. Client which follows specification can answer
|
|
|
|
|
# with any response from this list (X - block, `-` empty space):
|
|
|
|
|
#
|
|
|
|
|
# 1. X X X
|
|
|
|
|
# 2. - - X
|
|
|
|
|
# 3. - X -
|
|
|
|
|
# 4. - X X
|
|
|
|
|
# 5. X - -
|
|
|
|
|
# 6. X - X
|
|
|
|
|
# 7. X X -
|
|
|
|
|
#
|
|
|
|
|
# If peer answers with `1` everything will be fine and `block_pool` will be
|
|
|
|
|
# able to process all 3 blocks. In case of `2`, `3`, `4`, `6` - `block_pool`
|
|
|
|
|
# will fail immediately with chunk and report "parent is missing" error.
|
|
|
|
|
# But in case of `5` and `7` blocks will be processed by `block_pool` without
|
|
|
|
|
# any problems, however it will start producing problems right from this
|
|
|
|
|
# uncertain last slot. SyncQueue will start producing requests for next
|
|
|
|
|
# blocks, but all the responses from this point will fail with "parent is
|
|
|
|
|
# missing" error. Lets call such peers "jokers", because they are joking
|
|
|
|
|
# with responses.
|
|
|
|
|
#
|
2020-06-15 19:41:26 +00:00
|
|
|
|
# To fix "joker" problem we going to perform rollback to the latest finalized
|
|
|
|
|
# epoch's first slot.
|
2020-01-21 18:30:21 +00:00
|
|
|
|
doAssert(chunkSize > 0'u64, "Chunk size should not be zero")
|
2020-05-19 12:08:50 +00:00
|
|
|
|
result = SyncQueue[T](
|
2020-04-20 14:59:18 +00:00
|
|
|
|
startSlot: start,
|
|
|
|
|
lastSlot: last,
|
|
|
|
|
chunkSize: chunkSize,
|
2021-03-11 10:10:57 +00:00
|
|
|
|
queueSize: syncQueueSize,
|
2020-07-06 12:53:48 +00:00
|
|
|
|
getFinalizedSlot: getFinalizedSlotCb,
|
2020-06-03 08:46:29 +00:00
|
|
|
|
waiters: newSeq[SyncWaiter[T]](),
|
2020-05-28 05:02:28 +00:00
|
|
|
|
counter: 1'u64,
|
2020-06-03 08:46:29 +00:00
|
|
|
|
pending: initTable[uint64, SyncRequest[T]](),
|
2020-05-19 12:08:50 +00:00
|
|
|
|
debtsQueue: initHeapQueue[SyncRequest[T]](),
|
2020-04-20 14:59:18 +00:00
|
|
|
|
inpSlot: start,
|
2020-08-10 07:15:50 +00:00
|
|
|
|
outSlot: start,
|
2021-05-28 16:34:00 +00:00
|
|
|
|
blockProcessor: blockProcessor
|
2020-04-20 14:59:18 +00:00
|
|
|
|
)
|
2020-01-21 18:30:21 +00:00
|
|
|
|
|
2020-05-19 12:08:50 +00:00
|
|
|
|
proc `<`*[T](a, b: SyncRequest[T]): bool {.inline.} =
|
2020-01-21 18:30:21 +00:00
|
|
|
|
result = (a.slot < b.slot)
|
|
|
|
|
|
2020-05-19 12:08:50 +00:00
|
|
|
|
proc `<`*[T](a, b: SyncResult[T]): bool {.inline.} =
|
2020-01-21 18:30:21 +00:00
|
|
|
|
result = (a.request.slot < b.request.slot)
|
|
|
|
|
|
2020-05-19 12:08:50 +00:00
|
|
|
|
proc `==`*[T](a, b: SyncRequest[T]): bool {.inline.} =
|
2020-01-21 18:30:21 +00:00
|
|
|
|
result = ((a.slot == b.slot) and (a.count == b.count) and
|
|
|
|
|
(a.step == b.step))
|
|
|
|
|
|
2020-05-19 12:08:50 +00:00
|
|
|
|
proc lastSlot*[T](req: SyncRequest[T]): Slot {.inline.} =
|
2020-01-21 18:30:21 +00:00
|
|
|
|
## Returns last slot for request ``req``.
|
|
|
|
|
result = req.slot + req.count - 1'u64
|
|
|
|
|
|
2020-05-28 05:02:28 +00:00
|
|
|
|
proc makePending*[T](sq: SyncQueue[T], req: var SyncRequest[T]) =
|
|
|
|
|
req.index = sq.counter
|
|
|
|
|
sq.counter = sq.counter + 1'u64
|
2020-06-03 08:46:29 +00:00
|
|
|
|
sq.pending[req.index] = req
|
2020-05-28 05:02:28 +00:00
|
|
|
|
|
2020-05-19 12:08:50 +00:00
|
|
|
|
proc updateLastSlot*[T](sq: SyncQueue[T], last: Slot) {.inline.} =
|
2020-01-21 18:30:21 +00:00
|
|
|
|
## Update last slot stored in queue ``sq`` with value ``last``.
|
2020-04-20 14:59:18 +00:00
|
|
|
|
doAssert(sq.lastSlot <= last,
|
|
|
|
|
"Last slot could not be lower then stored one " &
|
|
|
|
|
$sq.lastSlot & " <= " & $last)
|
2020-01-21 18:30:21 +00:00
|
|
|
|
sq.lastSlot = last
|
|
|
|
|
|
2020-11-20 10:00:22 +00:00
|
|
|
|
proc wakeupWaiters[T](sq: SyncQueue[T], flag = true) =
|
2020-05-28 05:02:28 +00:00
|
|
|
|
## Wakeup one or all blocked waiters.
|
2020-06-03 08:46:29 +00:00
|
|
|
|
for item in sq.waiters:
|
|
|
|
|
if not(item.future.finished()):
|
|
|
|
|
item.future.complete(flag)
|
2020-05-28 05:02:28 +00:00
|
|
|
|
|
2020-06-03 08:46:29 +00:00
|
|
|
|
proc waitForChanges[T](sq: SyncQueue[T],
|
|
|
|
|
req: SyncRequest[T]): Future[bool] {.async.} =
|
2020-05-28 05:02:28 +00:00
|
|
|
|
## Create new waiter and wait for completion from `wakeupWaiters()`.
|
2020-06-03 08:46:29 +00:00
|
|
|
|
var waitfut = newFuture[bool]("SyncQueue.waitForChanges")
|
|
|
|
|
let waititem = SyncWaiter[T](future: waitfut, request: req)
|
|
|
|
|
sq.waiters.add(waititem)
|
2020-05-28 05:02:28 +00:00
|
|
|
|
try:
|
2020-06-03 08:46:29 +00:00
|
|
|
|
result = await waitfut
|
2020-05-28 05:02:28 +00:00
|
|
|
|
finally:
|
2020-06-03 08:46:29 +00:00
|
|
|
|
sq.waiters.delete(sq.waiters.find(waititem))
|
2020-05-28 05:02:28 +00:00
|
|
|
|
|
|
|
|
|
proc wakeupAndWaitWaiters[T](sq: SyncQueue[T]) {.async.} =
|
|
|
|
|
## This procedure will perform wakeupWaiters(false) and blocks until last
|
|
|
|
|
## waiter will be awakened.
|
2020-06-03 08:46:29 +00:00
|
|
|
|
var waitChanges = sq.waitForChanges(SyncRequest.empty(T))
|
2020-05-28 05:02:28 +00:00
|
|
|
|
sq.wakeupWaiters(false)
|
|
|
|
|
discard await waitChanges
|
|
|
|
|
|
|
|
|
|
proc resetWait*[T](sq: SyncQueue[T], toSlot: Option[Slot]) {.async.} =
|
|
|
|
|
## Perform reset of all the blocked waiters in SyncQueue.
|
|
|
|
|
##
|
|
|
|
|
## We adding one more waiter to the waiters sequence and
|
|
|
|
|
## call wakeupWaiters(false). Because our waiter is last in sequence of
|
|
|
|
|
## waiters it will be resumed only after all waiters will be awakened and
|
|
|
|
|
## finished.
|
|
|
|
|
|
|
|
|
|
# We are clearing pending list, so that all requests that are still running
|
|
|
|
|
# around (still downloading, but not yet pushed to the SyncQueue) will be
|
|
|
|
|
# expired. Its important to perform this call first (before await), otherwise
|
|
|
|
|
# you can introduce race problem.
|
|
|
|
|
sq.pending.clear()
|
|
|
|
|
|
|
|
|
|
# We calculating minimal slot number to which we will be able to reset,
|
|
|
|
|
# without missing any blocks. There 3 sources:
|
|
|
|
|
# 1. Debts queue.
|
|
|
|
|
# 2. Processing queue (`inpSlot`, `outSlot`).
|
2020-06-15 19:41:26 +00:00
|
|
|
|
# 3. Requested slot `toSlot`.
|
2020-05-28 05:02:28 +00:00
|
|
|
|
#
|
|
|
|
|
# Queue's `outSlot` is the lowest slot we added to `block_pool`, but
|
2020-06-15 19:41:26 +00:00
|
|
|
|
# `toSlot` slot can be less then `outSlot`. `debtsQueue` holds only not
|
2020-05-28 05:02:28 +00:00
|
|
|
|
# added slot requests, so it can't be bigger then `outSlot` value.
|
|
|
|
|
var minSlot = sq.outSlot
|
|
|
|
|
if toSlot.isSome():
|
|
|
|
|
minSlot = min(toSlot.get(), sq.outSlot)
|
|
|
|
|
sq.debtsQueue.clear()
|
|
|
|
|
sq.debtsCount = 0
|
|
|
|
|
sq.readyQueue.clear()
|
|
|
|
|
sq.inpSlot = minSlot
|
|
|
|
|
sq.outSlot = minSlot
|
|
|
|
|
|
|
|
|
|
# We are going to wakeup all the waiters and wait for last one.
|
|
|
|
|
await sq.wakeupAndWaitWaiters()
|
|
|
|
|
|
2020-05-19 12:08:50 +00:00
|
|
|
|
proc isEmpty*[T](sr: SyncResult[T]): bool {.inline.} =
|
2020-05-28 05:02:28 +00:00
|
|
|
|
## Returns ``true`` if response chain of blocks is empty (has only empty
|
|
|
|
|
## slots).
|
2020-05-19 12:08:50 +00:00
|
|
|
|
len(sr.data) == 0
|
|
|
|
|
|
|
|
|
|
proc hasEndGap*[T](sr: SyncResult[T]): bool {.inline.} =
|
2020-05-28 05:02:28 +00:00
|
|
|
|
## Returns ``true`` if response chain of blocks has gap at the end.
|
2020-05-19 12:08:50 +00:00
|
|
|
|
let lastslot = sr.request.slot + sr.request.count - 1'u64
|
|
|
|
|
if len(sr.data) == 0:
|
|
|
|
|
return true
|
2021-07-15 19:01:07 +00:00
|
|
|
|
if sr.data[^1].slot != lastslot:
|
2020-05-19 12:08:50 +00:00
|
|
|
|
return true
|
|
|
|
|
return false
|
|
|
|
|
|
|
|
|
|
proc getLastNonEmptySlot*[T](sr: SyncResult[T]): Slot {.inline.} =
|
|
|
|
|
## Returns last non-empty slot from result ``sr``. If response has only
|
|
|
|
|
## empty slots, original request slot will be returned.
|
|
|
|
|
if len(sr.data) == 0:
|
|
|
|
|
# If response has only empty slots we going to use original request slot
|
|
|
|
|
sr.request.slot
|
|
|
|
|
else:
|
2021-07-15 19:01:07 +00:00
|
|
|
|
sr.data[^1].slot
|
2020-05-19 12:08:50 +00:00
|
|
|
|
|
2020-11-20 10:00:22 +00:00
|
|
|
|
proc toDebtsQueue[T](sq: SyncQueue[T], sr: SyncRequest[T]) =
|
2020-05-28 05:02:28 +00:00
|
|
|
|
sq.debtsQueue.push(sr)
|
|
|
|
|
sq.debtsCount = sq.debtsCount + sr.count
|
|
|
|
|
|
2020-10-30 12:33:52 +00:00
|
|
|
|
proc getRewindPoint*[T](sq: SyncQueue[T], failSlot: Slot,
|
|
|
|
|
finalizedSlot: Slot): Slot =
|
2021-05-18 10:25:14 +00:00
|
|
|
|
# Calculate the latest finalized epoch.
|
|
|
|
|
let finalizedEpoch = compute_epoch_at_slot(finalizedSlot)
|
|
|
|
|
|
|
|
|
|
# Calculate failure epoch.
|
|
|
|
|
let failEpoch = compute_epoch_at_slot(failSlot)
|
|
|
|
|
|
2020-10-30 12:33:52 +00:00
|
|
|
|
# Calculate exponential rewind point in number of epochs.
|
|
|
|
|
let epochCount =
|
|
|
|
|
if sq.rewind.isSome():
|
|
|
|
|
let rewind = sq.rewind.get()
|
|
|
|
|
if failSlot == rewind.failSlot:
|
|
|
|
|
# `MissingParent` happened at same slot so we increase rewind point by
|
|
|
|
|
# factor of 2.
|
2021-05-18 10:25:14 +00:00
|
|
|
|
if failEpoch > finalizedEpoch:
|
|
|
|
|
let rewindPoint = rewind.epochCount shl 1
|
|
|
|
|
if rewindPoint < rewind.epochCount:
|
|
|
|
|
# If exponential rewind point produces `uint64` overflow we will
|
|
|
|
|
# make rewind to latest finalized epoch.
|
|
|
|
|
failEpoch - finalizedEpoch
|
|
|
|
|
else:
|
|
|
|
|
if (failEpoch < rewindPoint) or
|
|
|
|
|
(failEpoch - rewindPoint < finalizedEpoch):
|
|
|
|
|
# If exponential rewind point points to position which is far
|
|
|
|
|
# behind latest finalized epoch.
|
|
|
|
|
failEpoch - finalizedEpoch
|
|
|
|
|
else:
|
|
|
|
|
rewindPoint
|
|
|
|
|
else:
|
|
|
|
|
warn "Trying to rewind over the last finalized epoch",
|
|
|
|
|
finalized_slot = finalizedSlot, fail_slot = failSlot,
|
|
|
|
|
finalized_epoch = finalizedEpoch, fail_epoch = failEpoch,
|
|
|
|
|
rewind_epoch_count = rewind.epochCount,
|
|
|
|
|
finalized_epoch = finalizedEpoch
|
|
|
|
|
0'u64
|
2020-10-30 12:33:52 +00:00
|
|
|
|
else:
|
|
|
|
|
# `MissingParent` happened at different slot so we going to rewind for
|
|
|
|
|
# 1 epoch only.
|
2021-05-18 10:25:14 +00:00
|
|
|
|
if (failEpoch < 1'u64) or (failEpoch - 1'u64 < finalizedEpoch):
|
|
|
|
|
warn "Сould not rewind further than the last finalized epoch",
|
|
|
|
|
finalized_slot = finalizedSlot, fail_slot = failSlot,
|
|
|
|
|
finalized_epoch = finalizedEpoch, fail_epoch = failEpoch,
|
|
|
|
|
rewind_epoch_count = rewind.epochCount,
|
|
|
|
|
finalized_epoch = finalizedEpoch
|
|
|
|
|
0'u64
|
|
|
|
|
else:
|
|
|
|
|
1'u64
|
2020-10-30 12:33:52 +00:00
|
|
|
|
else:
|
|
|
|
|
# `MissingParent` happened first time.
|
2021-05-18 10:25:14 +00:00
|
|
|
|
if (failEpoch < 1'u64) or (failEpoch - 1'u64 < finalizedEpoch):
|
|
|
|
|
warn "Сould not rewind further than the last finalized epoch",
|
|
|
|
|
finalized_slot = finalizedSlot, fail_slot = failSlot,
|
|
|
|
|
finalized_epoch = finalizedEpoch, fail_epoch = failEpoch,
|
|
|
|
|
finalized_epoch = finalizedEpoch
|
|
|
|
|
0'u64
|
|
|
|
|
else:
|
|
|
|
|
1'u64
|
2020-10-30 12:33:52 +00:00
|
|
|
|
|
2021-05-18 10:25:14 +00:00
|
|
|
|
# echo "epochCount = ", epochCount
|
|
|
|
|
|
|
|
|
|
if epochCount == 0'u64:
|
|
|
|
|
warn "Unable to continue syncing, please restart the node",
|
|
|
|
|
finalized_slot = finalizedSlot, fail_slot = failSlot,
|
|
|
|
|
finalized_epoch = finalizedEpoch, fail_epoch = failEpoch,
|
|
|
|
|
finalized_epoch = finalizedEpoch
|
|
|
|
|
# Calculate the rewind epoch, which will be equal to last rewind point or
|
|
|
|
|
# finalizedEpoch
|
|
|
|
|
let rewindEpoch =
|
|
|
|
|
if sq.rewind.isNone():
|
2020-10-30 12:33:52 +00:00
|
|
|
|
finalizedEpoch
|
|
|
|
|
else:
|
2021-05-18 10:25:14 +00:00
|
|
|
|
compute_epoch_at_slot(sq.rewind.get().failSlot) -
|
|
|
|
|
sq.rewind.get().epochCount
|
|
|
|
|
compute_start_slot_at_epoch(rewindEpoch)
|
|
|
|
|
else:
|
|
|
|
|
# Calculate the rewind epoch, which should not be less than the latest
|
|
|
|
|
# finalized epoch.
|
|
|
|
|
let rewindEpoch = failEpoch - epochCount
|
|
|
|
|
# Update and save new rewind point in SyncQueue.
|
|
|
|
|
sq.rewind = some(RewindPoint(failSlot: failSlot, epochCount: epochCount))
|
|
|
|
|
compute_start_slot_at_epoch(rewindEpoch)
|
2020-10-30 12:33:52 +00:00
|
|
|
|
|
2020-05-19 12:08:50 +00:00
|
|
|
|
proc push*[T](sq: SyncQueue[T], sr: SyncRequest[T],
|
2021-07-15 19:01:07 +00:00
|
|
|
|
data: seq[ForkedSignedBeaconBlock]) {.async, gcsafe.} =
|
2020-01-21 18:30:21 +00:00
|
|
|
|
## Push successfull result to queue ``sq``.
|
2020-05-19 12:08:50 +00:00
|
|
|
|
mixin updateScore
|
|
|
|
|
|
2020-05-28 05:02:28 +00:00
|
|
|
|
if sr.index notin sq.pending:
|
|
|
|
|
# If request `sr` not in our pending list, it only means that
|
|
|
|
|
# SyncQueue.resetWait() happens and all pending requests are expired, so
|
|
|
|
|
# we swallow `old` requests, and in such way sync-workers are able to get
|
|
|
|
|
# proper new requests from SyncQueue.
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
sq.pending.del(sr.index)
|
|
|
|
|
|
|
|
|
|
# This is backpressure handling algorithm, this algorithm is blocking
|
|
|
|
|
# all pending `push` requests if `request.slot` not in range:
|
|
|
|
|
# [current_queue_slot, current_queue_slot + sq.queueSize * sq.chunkSize].
|
|
|
|
|
var exitNow = false
|
2020-01-21 18:30:21 +00:00
|
|
|
|
while true:
|
2020-04-20 14:59:18 +00:00
|
|
|
|
if (sq.queueSize > 0) and
|
|
|
|
|
(sr.slot >= sq.outSlot + uint64(sq.queueSize) * sq.chunkSize):
|
2020-06-03 08:46:29 +00:00
|
|
|
|
let res = await sq.waitForChanges(sr)
|
2020-05-28 05:02:28 +00:00
|
|
|
|
if res:
|
|
|
|
|
continue
|
|
|
|
|
else:
|
2020-06-15 19:41:26 +00:00
|
|
|
|
# SyncQueue reset happens. We are exiting to wake up sync-worker.
|
2020-05-28 05:02:28 +00:00
|
|
|
|
exitNow = true
|
|
|
|
|
break
|
|
|
|
|
let syncres = SyncResult[T](request: sr, data: data)
|
|
|
|
|
sq.readyQueue.push(syncres)
|
|
|
|
|
exitNow = false
|
2020-01-21 18:30:21 +00:00
|
|
|
|
break
|
|
|
|
|
|
2020-05-28 05:02:28 +00:00
|
|
|
|
if exitNow:
|
|
|
|
|
return
|
|
|
|
|
|
2020-01-21 18:30:21 +00:00
|
|
|
|
while len(sq.readyQueue) > 0:
|
|
|
|
|
let minSlot = sq.readyQueue[0].request.slot
|
|
|
|
|
if sq.outSlot != minSlot:
|
|
|
|
|
break
|
|
|
|
|
let item = sq.readyQueue.pop()
|
2020-08-10 07:15:50 +00:00
|
|
|
|
|
|
|
|
|
# Validating received blocks one by one
|
|
|
|
|
var res: Result[void, BlockError]
|
2020-10-30 12:33:52 +00:00
|
|
|
|
var failSlot: Option[Slot]
|
2020-08-10 07:15:50 +00:00
|
|
|
|
if len(item.data) > 0:
|
|
|
|
|
for blk in item.data:
|
2020-08-12 09:29:11 +00:00
|
|
|
|
trace "Pushing block", block_root = blk.root,
|
2021-07-15 19:01:07 +00:00
|
|
|
|
block_slot = blk.slot
|
2020-08-10 07:15:50 +00:00
|
|
|
|
res = await sq.validate(blk)
|
|
|
|
|
if not(res.isOk):
|
2021-07-15 19:01:07 +00:00
|
|
|
|
failSlot = some(blk.slot)
|
2020-08-10 07:15:50 +00:00
|
|
|
|
break
|
|
|
|
|
else:
|
|
|
|
|
res = Result[void, BlockError].ok()
|
|
|
|
|
|
2020-10-30 12:33:52 +00:00
|
|
|
|
# Increase progress counter, so watch task will be able to know that we are
|
|
|
|
|
# not stuck.
|
|
|
|
|
inc(sq.opcounter)
|
|
|
|
|
|
2020-05-28 05:02:28 +00:00
|
|
|
|
if res.isOk:
|
2020-05-19 12:08:50 +00:00
|
|
|
|
sq.outSlot = sq.outSlot + item.request.count
|
2020-10-08 12:50:48 +00:00
|
|
|
|
if len(item.data) > 0:
|
|
|
|
|
# If there no error and response was not empty we should reward peer
|
|
|
|
|
# with some bonus score.
|
|
|
|
|
item.request.item.updateScore(PeerScoreGoodBlocks)
|
2020-05-28 05:02:28 +00:00
|
|
|
|
sq.wakeupWaiters()
|
2020-05-19 12:08:50 +00:00
|
|
|
|
else:
|
|
|
|
|
debug "Block pool rejected peer's response", peer = item.request.item,
|
|
|
|
|
request_slot = item.request.slot,
|
|
|
|
|
request_count = item.request.count,
|
|
|
|
|
request_step = item.request.step,
|
|
|
|
|
blocks_map = getShortMap(item.request, item.data),
|
2020-07-10 08:25:58 +00:00
|
|
|
|
blocks_count = len(item.data), errCode = res.error,
|
|
|
|
|
topics = "syncman"
|
2020-05-28 05:02:28 +00:00
|
|
|
|
|
|
|
|
|
var resetSlot: Option[Slot]
|
|
|
|
|
|
|
|
|
|
if res.error == BlockError.MissingParent:
|
2020-06-15 19:41:26 +00:00
|
|
|
|
# If we got `BlockError.MissingParent` it means that peer returns chain
|
|
|
|
|
# of blocks with holes or `block_pool` is in incomplete state. We going
|
|
|
|
|
# to rewind to the first slot at latest finalized epoch.
|
|
|
|
|
let req = item.request
|
2020-07-06 12:53:48 +00:00
|
|
|
|
let finalizedSlot = sq.getFinalizedSlot()
|
2020-06-15 19:41:26 +00:00
|
|
|
|
if finalizedSlot < req.slot:
|
2020-10-30 12:33:52 +00:00
|
|
|
|
let rewindSlot = sq.getRewindPoint(failSlot.get(), finalizedSlot)
|
2020-06-15 19:41:26 +00:00
|
|
|
|
warn "Unexpected missing parent, rewind happens",
|
2020-10-30 12:33:52 +00:00
|
|
|
|
peer = req.item, rewind_to_slot = rewindSlot,
|
|
|
|
|
rewind_epoch_count = sq.rewind.get().epochCount,
|
|
|
|
|
rewind_fail_slot = failSlot.get(),
|
|
|
|
|
finalized_slot = finalized_slot,
|
2020-06-15 19:41:26 +00:00
|
|
|
|
request_slot = req.slot, request_count = req.count,
|
|
|
|
|
request_step = req.step, blocks_count = len(item.data),
|
2020-07-10 08:25:58 +00:00
|
|
|
|
blocks_map = getShortMap(req, item.data), topics = "syncman"
|
2020-10-30 12:33:52 +00:00
|
|
|
|
resetSlot = some(rewindSlot)
|
2020-06-15 19:41:26 +00:00
|
|
|
|
req.item.updateScore(PeerScoreMissingBlocks)
|
2020-05-28 05:02:28 +00:00
|
|
|
|
else:
|
2020-06-15 19:41:26 +00:00
|
|
|
|
error "Unexpected missing parent at finalized epoch slot",
|
|
|
|
|
peer = req.item, to_slot = finalizedSlot,
|
|
|
|
|
request_slot = req.slot, request_count = req.count,
|
|
|
|
|
request_step = req.step, blocks_count = len(item.data),
|
2020-07-10 08:25:58 +00:00
|
|
|
|
blocks_map = getShortMap(req, item.data), topics = "syncman"
|
2020-06-15 19:41:26 +00:00
|
|
|
|
req.item.updateScore(PeerScoreBadBlocks)
|
2020-05-28 05:02:28 +00:00
|
|
|
|
elif res.error == BlockError.Invalid:
|
|
|
|
|
let req = item.request
|
|
|
|
|
warn "Received invalid sequence of blocks", peer = req.item,
|
|
|
|
|
request_slot = req.slot, request_count = req.count,
|
|
|
|
|
request_step = req.step, blocks_count = len(item.data),
|
2020-07-10 08:25:58 +00:00
|
|
|
|
blocks_map = getShortMap(req, item.data), topics = "syncman"
|
2020-05-28 05:02:28 +00:00
|
|
|
|
req.item.updateScore(PeerScoreBadBlocks)
|
|
|
|
|
else:
|
|
|
|
|
let req = item.request
|
|
|
|
|
warn "Received unexpected response from block_pool", peer = req.item,
|
|
|
|
|
request_slot = req.slot, request_count = req.count,
|
|
|
|
|
request_step = req.step, blocks_count = len(item.data),
|
2020-07-10 08:25:58 +00:00
|
|
|
|
blocks_map = getShortMap(req, item.data), errorCode = res.error,
|
|
|
|
|
topics = "syncman"
|
2020-05-28 05:02:28 +00:00
|
|
|
|
req.item.updateScore(PeerScoreBadBlocks)
|
2020-05-19 12:08:50 +00:00
|
|
|
|
|
|
|
|
|
# We need to move failed response to the debts queue.
|
2020-05-28 05:02:28 +00:00
|
|
|
|
sq.toDebtsQueue(item.request)
|
|
|
|
|
if resetSlot.isSome():
|
|
|
|
|
await sq.resetWait(resetSlot)
|
2020-06-15 19:41:26 +00:00
|
|
|
|
debug "Rewind to slot was happened", reset_slot = reset_slot.get(),
|
2020-10-30 12:33:52 +00:00
|
|
|
|
queue_input_slot = sq.inpSlot, queue_output_slot = sq.outSlot,
|
|
|
|
|
rewind_epoch_count = sq.rewind.get().epochCount,
|
|
|
|
|
rewind_fail_slot = sq.rewind.get().failSlot,
|
|
|
|
|
reset_slot = resetSlot, topics = "syncman"
|
2020-01-21 18:30:21 +00:00
|
|
|
|
break
|
|
|
|
|
|
2020-05-19 12:08:50 +00:00
|
|
|
|
proc push*[T](sq: SyncQueue[T], sr: SyncRequest[T]) =
|
2020-01-21 18:30:21 +00:00
|
|
|
|
## Push failed request back to queue.
|
2020-05-28 05:02:28 +00:00
|
|
|
|
if sr.index notin sq.pending:
|
|
|
|
|
# If request `sr` not in our pending list, it only means that
|
|
|
|
|
# SyncQueue.resetWait() happens and all pending requests are expired, so
|
|
|
|
|
# we swallow `old` requests, and in such way sync-workers are able to get
|
|
|
|
|
# proper new requests from SyncQueue.
|
|
|
|
|
return
|
|
|
|
|
sq.pending.del(sr.index)
|
|
|
|
|
sq.toDebtsQueue(sr)
|
2020-01-21 18:30:21 +00:00
|
|
|
|
|
2020-05-19 12:08:50 +00:00
|
|
|
|
proc pop*[T](sq: SyncQueue[T], maxslot: Slot, item: T): SyncRequest[T] =
|
2020-01-21 18:30:21 +00:00
|
|
|
|
if len(sq.debtsQueue) > 0:
|
2020-04-20 14:59:18 +00:00
|
|
|
|
if maxSlot < sq.debtsQueue[0].slot:
|
2020-05-19 12:08:50 +00:00
|
|
|
|
return SyncRequest.empty(T)
|
2020-04-20 14:59:18 +00:00
|
|
|
|
|
2020-05-19 12:08:50 +00:00
|
|
|
|
var sr = sq.debtsQueue.pop()
|
2020-04-20 14:59:18 +00:00
|
|
|
|
if sr.lastSlot() <= maxSlot:
|
|
|
|
|
sq.debtsCount = sq.debtsCount - sr.count
|
2020-05-19 12:08:50 +00:00
|
|
|
|
sr.setItem(item)
|
2020-05-28 05:02:28 +00:00
|
|
|
|
sq.makePending(sr)
|
2020-04-20 14:59:18 +00:00
|
|
|
|
return sr
|
|
|
|
|
|
2020-05-28 05:02:28 +00:00
|
|
|
|
var sr1 = SyncRequest.init(T, sr.slot, maxslot, item)
|
2020-05-19 12:08:50 +00:00
|
|
|
|
let sr2 = SyncRequest.init(T, maxslot + 1'u64, sr.lastSlot())
|
2020-04-20 14:59:18 +00:00
|
|
|
|
sq.debtsQueue.push(sr2)
|
|
|
|
|
sq.debtsCount = sq.debtsCount - sr1.count
|
2020-05-28 05:02:28 +00:00
|
|
|
|
sq.makePending(sr1)
|
2020-04-20 14:59:18 +00:00
|
|
|
|
return sr1
|
2020-01-21 18:30:21 +00:00
|
|
|
|
else:
|
2020-04-20 14:59:18 +00:00
|
|
|
|
if maxSlot < sq.inpSlot:
|
2020-05-19 12:08:50 +00:00
|
|
|
|
return SyncRequest.empty(T)
|
2020-04-20 14:59:18 +00:00
|
|
|
|
|
|
|
|
|
if sq.inpSlot > sq.lastSlot:
|
2020-05-19 12:08:50 +00:00
|
|
|
|
return SyncRequest.empty(T)
|
2020-04-20 14:59:18 +00:00
|
|
|
|
|
|
|
|
|
let lastSlot = min(maxslot, sq.lastSlot)
|
|
|
|
|
let count = min(sq.chunkSize, lastSlot + 1'u64 - sq.inpSlot)
|
2020-05-28 05:02:28 +00:00
|
|
|
|
var sr = SyncRequest.init(T, sq.inpSlot, count, item)
|
2020-04-20 14:59:18 +00:00
|
|
|
|
sq.inpSlot = sq.inpSlot + count
|
2020-05-28 05:02:28 +00:00
|
|
|
|
sq.makePending(sr)
|
2020-04-20 14:59:18 +00:00
|
|
|
|
return sr
|
2020-01-21 18:30:21 +00:00
|
|
|
|
|
2020-05-19 12:08:50 +00:00
|
|
|
|
proc len*[T](sq: SyncQueue[T]): uint64 {.inline.} =
|
2020-01-21 18:30:21 +00:00
|
|
|
|
## Returns number of slots left in queue ``sq``.
|
|
|
|
|
if sq.inpSlot > sq.lastSlot:
|
|
|
|
|
result = sq.debtsCount
|
|
|
|
|
else:
|
2020-05-28 05:02:28 +00:00
|
|
|
|
result = sq.lastSlot - sq.inpSlot + 1'u64 - sq.debtsCount
|
2020-01-21 18:30:21 +00:00
|
|
|
|
|
2020-05-19 12:08:50 +00:00
|
|
|
|
proc total*[T](sq: SyncQueue[T]): uint64 {.inline.} =
|
2020-01-21 18:30:21 +00:00
|
|
|
|
## Returns total number of slots in queue ``sq``.
|
|
|
|
|
result = sq.lastSlot - sq.startSlot + 1'u64
|
|
|
|
|
|
2020-05-19 12:08:50 +00:00
|
|
|
|
proc progress*[T](sq: SyncQueue[T]): uint64 =
|
2020-01-21 18:30:21 +00:00
|
|
|
|
## Returns queue's ``sq`` progress string.
|
2020-01-22 12:47:55 +00:00
|
|
|
|
let curSlot = sq.outSlot - sq.startSlot
|
2020-04-23 15:31:00 +00:00
|
|
|
|
result = (curSlot * 100'u64) div sq.total()
|
2020-01-21 18:30:21 +00:00
|
|
|
|
|
2020-05-19 12:08:50 +00:00
|
|
|
|
proc now*(sm: typedesc[SyncMoment], slot: Slot): SyncMoment {.inline.} =
|
|
|
|
|
result = SyncMoment(stamp: now(chronos.Moment), slot: slot)
|
|
|
|
|
|
|
|
|
|
proc speed*(start, finish: SyncMoment): float {.inline.} =
|
|
|
|
|
## Returns number of slots per second.
|
|
|
|
|
let slots = finish.slot - start.slot
|
|
|
|
|
let dur = finish.stamp - start.stamp
|
|
|
|
|
let secs = float(chronos.seconds(1).nanoseconds)
|
|
|
|
|
if isZero(dur):
|
|
|
|
|
result = 0.0
|
|
|
|
|
else:
|
|
|
|
|
let v = float(slots) * (secs / float(dur.nanoseconds))
|
|
|
|
|
# We doing round manually because stdlib.round is deprecated
|
|
|
|
|
result = round(v * 10000) / 10000
|
|
|
|
|
|
2020-01-21 18:30:21 +00:00
|
|
|
|
proc newSyncManager*[A, B](pool: PeerPool[A, B],
|
2020-04-20 14:59:18 +00:00
|
|
|
|
getLocalHeadSlotCb: GetSlotCallback,
|
|
|
|
|
getLocalWallSlotCb: GetSlotCallback,
|
2020-07-06 12:53:48 +00:00
|
|
|
|
getFinalizedSlotCb: GetSlotCallback,
|
2021-05-28 16:34:00 +00:00
|
|
|
|
blockProcessor: ref BlockProcessor,
|
2020-04-20 14:59:18 +00:00
|
|
|
|
maxStatusAge = uint64(SLOTS_PER_EPOCH * 4),
|
2020-06-07 15:36:24 +00:00
|
|
|
|
maxHeadAge = uint64(SLOTS_PER_EPOCH * 1),
|
2020-04-20 14:59:18 +00:00
|
|
|
|
sleepTime = (int(SLOTS_PER_EPOCH) *
|
|
|
|
|
int(SECONDS_PER_SLOT)).seconds,
|
|
|
|
|
chunkSize = uint64(SLOTS_PER_EPOCH),
|
2020-05-28 05:02:28 +00:00
|
|
|
|
toleranceValue = uint64(1),
|
2020-09-11 12:46:01 +00:00
|
|
|
|
rangeAge = uint64(SLOTS_PER_EPOCH * 4)
|
2020-04-20 14:59:18 +00:00
|
|
|
|
): SyncManager[A, B] =
|
2020-05-19 12:08:50 +00:00
|
|
|
|
|
2020-09-11 12:46:01 +00:00
|
|
|
|
let queue = SyncQueue.init(A, getLocalHeadSlotCb(), getLocalWallSlotCb(),
|
2021-05-28 16:34:00 +00:00
|
|
|
|
chunkSize, getFinalizedSlotCb, blockProcessor, 1)
|
2020-05-19 12:08:50 +00:00
|
|
|
|
|
2020-04-20 14:59:18 +00:00
|
|
|
|
result = SyncManager[A, B](
|
|
|
|
|
pool: pool,
|
|
|
|
|
maxStatusAge: maxStatusAge,
|
|
|
|
|
getLocalHeadSlot: getLocalHeadSlotCb,
|
|
|
|
|
getLocalWallSlot: getLocalWallSlotCb,
|
2020-07-06 12:53:48 +00:00
|
|
|
|
getFinalizedSlot: getFinalizedSlotCb,
|
2020-04-20 14:59:18 +00:00
|
|
|
|
maxHeadAge: maxHeadAge,
|
|
|
|
|
sleepTime: sleepTime,
|
2020-04-23 15:31:00 +00:00
|
|
|
|
chunkSize: chunkSize,
|
2020-08-10 07:15:50 +00:00
|
|
|
|
queue: queue,
|
2021-05-28 16:34:00 +00:00
|
|
|
|
blockProcessor: blockProcessor,
|
2020-09-11 12:46:01 +00:00
|
|
|
|
notInSyncEvent: newAsyncEvent(),
|
|
|
|
|
inRangeEvent: newAsyncEvent(),
|
|
|
|
|
notInRangeEvent: newAsyncEvent(),
|
|
|
|
|
rangeAge: rangeAge
|
2020-04-20 14:59:18 +00:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
proc getBlocks*[A, B](man: SyncManager[A, B], peer: A,
|
2020-05-12 22:37:07 +00:00
|
|
|
|
req: SyncRequest): Future[BeaconBlocksRes] {.async.} =
|
2020-04-20 14:59:18 +00:00
|
|
|
|
mixin beaconBlocksByRange, getScore, `==`
|
|
|
|
|
doAssert(not(req.isEmpty()), "Request must not be empty!")
|
|
|
|
|
debug "Requesting blocks from peer", peer = peer,
|
|
|
|
|
slot = req.slot, slot_count = req.count, step = req.step,
|
2020-07-25 06:12:23 +00:00
|
|
|
|
peer_score = peer.getScore(), peer_speed = peer.netKbps(),
|
|
|
|
|
topics = "syncman"
|
2021-07-15 19:01:07 +00:00
|
|
|
|
if peer.useSyncV2():
|
|
|
|
|
var workFut = awaitne beaconBlocksByRange_v2(peer, req.slot, req.count, req.step)
|
|
|
|
|
if workFut.failed():
|
|
|
|
|
debug "Error, while waiting getBlocks response", peer = peer,
|
|
|
|
|
slot = req.slot, slot_count = req.count, step = req.step,
|
|
|
|
|
errMsg = workFut.readError().msg, peer_speed = peer.netKbps(),
|
|
|
|
|
topics = "syncman"
|
|
|
|
|
else:
|
|
|
|
|
let res = workFut.read()
|
|
|
|
|
if res.isErr:
|
|
|
|
|
debug "Error, while reading getBlocks response",
|
|
|
|
|
peer = peer, slot = req.slot, count = req.count,
|
|
|
|
|
step = req.step, peer_speed = peer.netKbps(),
|
|
|
|
|
topics = "syncman", error = $res.error()
|
|
|
|
|
result = res
|
2020-01-21 18:30:21 +00:00
|
|
|
|
else:
|
2021-07-15 19:01:07 +00:00
|
|
|
|
var workFut = awaitne beaconBlocksByRange(peer, req.slot, req.count, req.step)
|
|
|
|
|
if workFut.failed():
|
|
|
|
|
debug "Error, while waiting getBlocks response", peer = peer,
|
|
|
|
|
slot = req.slot, slot_count = req.count, step = req.step,
|
|
|
|
|
errMsg = workFut.readError().msg, peer_speed = peer.netKbps(),
|
|
|
|
|
topics = "syncman"
|
|
|
|
|
else:
|
|
|
|
|
let res = workFut.read()
|
|
|
|
|
if res.isErr:
|
|
|
|
|
debug "Error, while reading getBlocks response",
|
|
|
|
|
peer = peer, slot = req.slot, count = req.count,
|
|
|
|
|
step = req.step, peer_speed = peer.netKbps(),
|
|
|
|
|
topics = "syncman", error = $res.error()
|
|
|
|
|
result = res.map() do (blcks: seq[phase0.SignedBeaconBlock]) -> auto: blcks.mapIt(ForkedSignedBeaconBlock.init(it))
|
2020-04-20 14:59:18 +00:00
|
|
|
|
|
|
|
|
|
template headAge(): uint64 =
|
|
|
|
|
wallSlot - headSlot
|
|
|
|
|
|
2020-09-11 12:46:01 +00:00
|
|
|
|
template queueAge(): uint64 =
|
|
|
|
|
wallSlot - man.queue.outSlot
|
|
|
|
|
|
2020-09-23 15:58:02 +00:00
|
|
|
|
template peerStatusAge(): Duration =
|
|
|
|
|
Moment.now() - peer.state(BeaconSync).statusLastTime
|
|
|
|
|
|
2020-08-21 20:47:34 +00:00
|
|
|
|
func syncQueueLen*[A, B](man: SyncManager[A, B]): uint64 =
|
|
|
|
|
man.queue.len
|
|
|
|
|
|
2020-09-21 16:02:27 +00:00
|
|
|
|
proc syncStep[A, B](man: SyncManager[A, B], index: int, peer: A) {.async.} =
|
|
|
|
|
let wallSlot = man.getLocalWallSlot()
|
|
|
|
|
let headSlot = man.getLocalHeadSlot()
|
|
|
|
|
var peerSlot = peer.getHeadSlot()
|
|
|
|
|
|
|
|
|
|
# We updating SyncQueue's last slot all the time
|
|
|
|
|
man.queue.updateLastSlot(wallSlot)
|
|
|
|
|
|
|
|
|
|
debug "Peer's syncing status", wall_clock_slot = wallSlot,
|
|
|
|
|
remote_head_slot = peerSlot, local_head_slot = headSlot,
|
|
|
|
|
peer_score = peer.getScore(), peer = peer, index = index,
|
|
|
|
|
peer_speed = peer.netKbps(), topics = "syncman"
|
|
|
|
|
|
|
|
|
|
# Check if peer's head slot is bigger than our wall clock slot.
|
|
|
|
|
if peerSlot > wallSlot + man.toleranceValue:
|
|
|
|
|
warn "Local timer is broken or peer's status information is invalid",
|
|
|
|
|
wall_clock_slot = wallSlot, remote_head_slot = peerSlot,
|
|
|
|
|
local_head_slot = headSlot, peer = peer, index = index,
|
|
|
|
|
tolerance_value = man.toleranceValue, peer_speed = peer.netKbps(),
|
|
|
|
|
peer_score = peer.getScore(), topics = "syncman"
|
2020-11-17 10:14:53 +00:00
|
|
|
|
discard SyncFailure.init(SyncFailureKind.StatusInvalid, peer)
|
2020-09-21 16:02:27 +00:00
|
|
|
|
return
|
2020-09-11 12:46:01 +00:00
|
|
|
|
|
2020-09-21 16:02:27 +00:00
|
|
|
|
# Check if we need to update peer's status information
|
2020-10-08 12:50:48 +00:00
|
|
|
|
if peerStatusAge >= StatusExpirationTime:
|
2020-09-21 16:02:27 +00:00
|
|
|
|
# Peer's status information is very old, its time to update it
|
|
|
|
|
man.workers[index].status = SyncWorkerStatus.UpdatingStatus
|
2020-10-01 18:56:42 +00:00
|
|
|
|
trace "Updating peer's status information", wall_clock_slot = wallSlot,
|
2020-09-21 16:02:27 +00:00
|
|
|
|
remote_head_slot = peerSlot, local_head_slot = headSlot,
|
|
|
|
|
peer = peer, peer_score = peer.getScore(), index = index,
|
|
|
|
|
peer_speed = peer.netKbps(), topics = "syncman"
|
2020-09-11 12:46:01 +00:00
|
|
|
|
|
|
|
|
|
try:
|
2020-09-21 16:02:27 +00:00
|
|
|
|
let res = await peer.updateStatus()
|
|
|
|
|
if not(res):
|
|
|
|
|
peer.updateScore(PeerScoreNoStatus)
|
|
|
|
|
debug "Failed to get remote peer's status, exiting", peer = peer,
|
|
|
|
|
peer_score = peer.getScore(), peer_head_slot = peerSlot,
|
|
|
|
|
peer_speed = peer.netKbps(), index = index, topics = "syncman"
|
2020-11-17 10:14:53 +00:00
|
|
|
|
discard SyncFailure.init(SyncFailureKind.StatusDownload, peer)
|
2020-09-21 16:02:27 +00:00
|
|
|
|
return
|
|
|
|
|
except CatchableError as exc:
|
|
|
|
|
debug "Unexpected exception while updating peer's status",
|
|
|
|
|
peer = peer, peer_score = peer.getScore(),
|
|
|
|
|
peer_head_slot = peerSlot, peer_speed = peer.netKbps(),
|
|
|
|
|
index = index, errMsg = exc.msg, topics = "syncman"
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
let newPeerSlot = peer.getHeadSlot()
|
|
|
|
|
if peerSlot >= newPeerSlot:
|
|
|
|
|
peer.updateScore(PeerScoreStaleStatus)
|
|
|
|
|
debug "Peer's status information is stale",
|
|
|
|
|
wall_clock_slot = wallSlot, remote_old_head_slot = peerSlot,
|
|
|
|
|
local_head_slot = headSlot, remote_new_head_slot = newPeerSlot,
|
|
|
|
|
peer = peer, peer_score = peer.getScore(), index = index,
|
|
|
|
|
peer_speed = peer.netKbps(), topics = "syncman"
|
|
|
|
|
else:
|
|
|
|
|
debug "Peer's status information updated", wall_clock_slot = wallSlot,
|
|
|
|
|
remote_old_head_slot = peerSlot, local_head_slot = headSlot,
|
|
|
|
|
remote_new_head_slot = newPeerSlot, peer = peer,
|
|
|
|
|
peer_score = peer.getScore(), peer_speed = peer.netKbps(),
|
|
|
|
|
index = index, topics = "syncman"
|
|
|
|
|
peer.updateScore(PeerScoreGoodStatus)
|
|
|
|
|
peerSlot = newPeerSlot
|
|
|
|
|
|
2020-09-23 15:58:02 +00:00
|
|
|
|
if headAge <= man.maxHeadAge:
|
2020-10-01 18:56:42 +00:00
|
|
|
|
info "We are in sync with network", wall_clock_slot = wallSlot,
|
2020-09-21 16:02:27 +00:00
|
|
|
|
remote_head_slot = peerSlot, local_head_slot = headSlot,
|
|
|
|
|
peer = peer, peer_score = peer.getScore(), index = index,
|
|
|
|
|
peer_speed = peer.netKbps(), topics = "syncman"
|
2020-09-23 15:58:02 +00:00
|
|
|
|
# We clear SyncManager's `notInSyncEvent` so all the workers will become
|
|
|
|
|
# sleeping soon.
|
|
|
|
|
man.notInSyncEvent.clear()
|
|
|
|
|
return
|
|
|
|
|
|
2020-09-24 12:14:29 +00:00
|
|
|
|
if headSlot >= peerSlot - man.maxHeadAge:
|
2020-10-01 18:56:42 +00:00
|
|
|
|
debug "We are in sync with peer; refreshing peer's status information",
|
2020-09-23 15:58:02 +00:00
|
|
|
|
wall_clock_slot = wallSlot, remote_head_slot = peerSlot,
|
|
|
|
|
local_head_slot = headSlot, peer = peer, peer_score = peer.getScore(),
|
|
|
|
|
index = index, peer_speed = peer.netKbps(), topics = "syncman"
|
|
|
|
|
|
2020-09-24 12:14:29 +00:00
|
|
|
|
man.workers[index].status = SyncWorkerStatus.UpdatingStatus
|
|
|
|
|
|
2020-09-23 15:58:02 +00:00
|
|
|
|
if peerStatusAge <= StatusUpdateInterval:
|
|
|
|
|
await sleepAsync(StatusUpdateInterval - peerStatusAge)
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
let res = await peer.updateStatus()
|
|
|
|
|
if not(res):
|
|
|
|
|
peer.updateScore(PeerScoreNoStatus)
|
|
|
|
|
debug "Failed to get remote peer's status, exiting", peer = peer,
|
|
|
|
|
peer_score = peer.getScore(), peer_head_slot = peerSlot,
|
|
|
|
|
peer_speed = peer.netKbps(), index = index, topics = "syncman"
|
2020-11-17 10:14:53 +00:00
|
|
|
|
discard SyncFailure.init(SyncFailureKind.StatusDownload, peer)
|
2020-09-23 15:58:02 +00:00
|
|
|
|
return
|
|
|
|
|
except CatchableError as exc:
|
|
|
|
|
debug "Unexpected exception while updating peer's status",
|
|
|
|
|
peer = peer, peer_score = peer.getScore(),
|
|
|
|
|
peer_head_slot = peerSlot, peer_speed = peer.netKbps(),
|
|
|
|
|
index = index, errMsg = exc.msg, topics = "syncman"
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
let newPeerSlot = peer.getHeadSlot()
|
|
|
|
|
if peerSlot >= newPeerSlot:
|
|
|
|
|
peer.updateScore(PeerScoreStaleStatus)
|
|
|
|
|
debug "Peer's status information is stale",
|
|
|
|
|
wall_clock_slot = wallSlot, remote_old_head_slot = peerSlot,
|
|
|
|
|
local_head_slot = headSlot, remote_new_head_slot = newPeerSlot,
|
|
|
|
|
peer = peer, peer_score = peer.getScore(), index = index,
|
|
|
|
|
peer_speed = peer.netKbps(), topics = "syncman"
|
|
|
|
|
else:
|
2020-10-30 12:33:52 +00:00
|
|
|
|
# This is not very good solution because we should not discriminate and/or
|
|
|
|
|
# penalize peers which are in sync process too, but their latest head is
|
|
|
|
|
# lower then our latest head. We should keep connections with such peers
|
|
|
|
|
# (so this peers are able to get in sync using our data), but we should
|
|
|
|
|
# not use this peers for syncing because this peers are useless for us.
|
|
|
|
|
# Right now we decreasing peer's score a bit, so it will not be
|
|
|
|
|
# disconnected due to low peer's score, but new fresh peers could replace
|
|
|
|
|
# peers with low latest head.
|
|
|
|
|
if headSlot >= newPeerSlot - man.maxHeadAge:
|
|
|
|
|
# Peer's head slot is still lower then ours.
|
|
|
|
|
debug "Peer's head slot is lower then local head slot",
|
|
|
|
|
wall_clock_slot = wallSlot, remote_old_head_slot = peerSlot,
|
|
|
|
|
local_head_slot = headSlot, remote_new_head_slot = newPeerSlot,
|
|
|
|
|
peer = peer, peer_score = peer.getScore(),
|
|
|
|
|
peer_speed = peer.netKbps(), index = index, topics = "syncman"
|
|
|
|
|
peer.updateScore(PeerScoreUseless)
|
|
|
|
|
else:
|
|
|
|
|
debug "Peer's status information updated", wall_clock_slot = wallSlot,
|
|
|
|
|
remote_old_head_slot = peerSlot, local_head_slot = headSlot,
|
|
|
|
|
remote_new_head_slot = newPeerSlot, peer = peer,
|
|
|
|
|
peer_score = peer.getScore(), peer_speed = peer.netKbps(),
|
|
|
|
|
index = index, topics = "syncman"
|
|
|
|
|
peer.updateScore(PeerScoreGoodStatus)
|
|
|
|
|
peerSlot = newPeerSlot
|
2020-09-23 15:58:02 +00:00
|
|
|
|
|
2020-09-21 16:02:27 +00:00
|
|
|
|
return
|
2020-04-20 14:59:18 +00:00
|
|
|
|
|
2020-09-21 16:02:27 +00:00
|
|
|
|
man.workers[index].status = SyncWorkerStatus.Requesting
|
|
|
|
|
let req = man.queue.pop(peerSlot, peer)
|
|
|
|
|
if req.isEmpty():
|
|
|
|
|
# SyncQueue could return empty request in 2 cases:
|
|
|
|
|
# 1. There no more slots in SyncQueue to download (we are synced, but
|
|
|
|
|
# our ``notInSyncEvent`` is not yet cleared).
|
|
|
|
|
# 2. Current peer's known head slot is too low to satisfy request.
|
|
|
|
|
#
|
|
|
|
|
# To avoid endless loop we going to wait for RESP_TIMEOUT time here.
|
|
|
|
|
# This time is enough for all pending requests to finish and it is also
|
|
|
|
|
# enough for main sync loop to clear ``notInSyncEvent``.
|
|
|
|
|
debug "Empty request received from queue, exiting", peer = peer,
|
|
|
|
|
local_head_slot = headSlot, remote_head_slot = peerSlot,
|
|
|
|
|
queue_input_slot = man.queue.inpSlot,
|
|
|
|
|
queue_output_slot = man.queue.outSlot,
|
|
|
|
|
queue_last_slot = man.queue.lastSlot,
|
|
|
|
|
peer_speed = peer.netKbps(), peer_score = peer.getScore(),
|
|
|
|
|
index = index, topics = "syncman"
|
|
|
|
|
await sleepAsync(RESP_TIMEOUT)
|
|
|
|
|
return
|
2020-04-23 15:31:00 +00:00
|
|
|
|
|
2020-09-21 16:02:27 +00:00
|
|
|
|
debug "Creating new request for peer", wall_clock_slot = wallSlot,
|
|
|
|
|
remote_head_slot = peerSlot, local_head_slot = headSlot,
|
|
|
|
|
request_slot = req.slot, request_count = req.count,
|
|
|
|
|
request_step = req.step, peer = peer, peer_speed = peer.netKbps(),
|
|
|
|
|
peer_score = peer.getScore(), index = index, topics = "syncman"
|
|
|
|
|
|
|
|
|
|
man.workers[index].status = SyncWorkerStatus.Downloading
|
2020-04-20 14:59:18 +00:00
|
|
|
|
|
2020-09-21 16:02:27 +00:00
|
|
|
|
try:
|
|
|
|
|
let blocks = await man.getBlocks(peer, req)
|
|
|
|
|
if blocks.isOk:
|
|
|
|
|
let data = blocks.get()
|
|
|
|
|
let smap = getShortMap(req, data)
|
|
|
|
|
debug "Received blocks on request", blocks_count = len(data),
|
|
|
|
|
blocks_map = smap, request_slot = req.slot,
|
|
|
|
|
request_count = req.count, request_step = req.step,
|
|
|
|
|
peer = peer, peer_score = peer.getScore(),
|
|
|
|
|
peer_speed = peer.netKbps(), index = index, topics = "syncman"
|
|
|
|
|
|
|
|
|
|
if not(checkResponse(req, data)):
|
|
|
|
|
peer.updateScore(PeerScoreBadResponse)
|
|
|
|
|
warn "Received blocks sequence is not in requested range",
|
|
|
|
|
blocks_count = len(data), blocks_map = smap,
|
|
|
|
|
request_slot = req.slot, request_count = req.count,
|
|
|
|
|
request_step = req.step, peer = peer,
|
|
|
|
|
peer_score = peer.getScore(), peer_speed = peer.netKbps(),
|
|
|
|
|
index = index, topics = "syncman"
|
2020-11-17 10:14:53 +00:00
|
|
|
|
discard SyncFailure.init(SyncFailureKind.BadResponse, peer)
|
2020-09-21 16:02:27 +00:00
|
|
|
|
return
|
2020-01-21 18:30:21 +00:00
|
|
|
|
|
2020-09-21 16:02:27 +00:00
|
|
|
|
# Scoring will happen in `syncUpdate`.
|
|
|
|
|
man.workers[index].status = SyncWorkerStatus.Processing
|
|
|
|
|
await man.queue.push(req, data)
|
|
|
|
|
else:
|
|
|
|
|
peer.updateScore(PeerScoreNoBlocks)
|
|
|
|
|
man.queue.push(req)
|
|
|
|
|
debug "Failed to receive blocks on request",
|
|
|
|
|
request_slot = req.slot, request_count = req.count,
|
|
|
|
|
request_step = req.step, peer = peer, index = index,
|
|
|
|
|
peer_score = peer.getScore(), peer_speed = peer.netKbps(),
|
|
|
|
|
topics = "syncman"
|
2020-11-17 10:14:53 +00:00
|
|
|
|
discard SyncFailure.init(SyncFailureKind.BlockDownload, peer)
|
2020-09-21 16:02:27 +00:00
|
|
|
|
return
|
2020-01-21 18:30:21 +00:00
|
|
|
|
|
2020-09-21 16:02:27 +00:00
|
|
|
|
except CatchableError as exc:
|
|
|
|
|
debug "Unexpected exception while receiving blocks",
|
2020-04-20 14:59:18 +00:00
|
|
|
|
request_slot = req.slot, request_count = req.count,
|
2020-09-21 16:02:27 +00:00
|
|
|
|
request_step = req.step, peer = peer, index = index,
|
|
|
|
|
peer_score = peer.getScore(), peer_speed = peer.netKbps(),
|
|
|
|
|
errMsg = exc.msg, topics = "syncman"
|
|
|
|
|
return
|
2020-01-21 18:30:21 +00:00
|
|
|
|
|
2020-09-21 16:02:27 +00:00
|
|
|
|
proc syncWorker[A, B](man: SyncManager[A, B], index: int) {.async.} =
|
|
|
|
|
mixin getKey, getScore, getHeadSlot
|
|
|
|
|
|
|
|
|
|
debug "Starting syncing worker", index = index, topics = "syncman"
|
|
|
|
|
|
|
|
|
|
while true:
|
|
|
|
|
man.workers[index].status = SyncWorkerStatus.Sleeping
|
|
|
|
|
# This event is going to be set until we are not in sync with network
|
|
|
|
|
await man.notInSyncEvent.wait()
|
|
|
|
|
man.workers[index].status = SyncWorkerStatus.WaitingPeer
|
|
|
|
|
let peer = await man.pool.acquire()
|
|
|
|
|
await man.syncStep(index, peer)
|
|
|
|
|
man.pool.release(peer)
|
2020-09-11 12:46:01 +00:00
|
|
|
|
|
|
|
|
|
proc getWorkersStats[A, B](man: SyncManager[A, B]): tuple[map: string,
|
|
|
|
|
sleeping: int,
|
|
|
|
|
waiting: int,
|
|
|
|
|
pending: int] =
|
|
|
|
|
var map = newString(len(man.workers))
|
|
|
|
|
var sleeping, waiting, pending: int
|
|
|
|
|
for i in 0 ..< len(man.workers):
|
|
|
|
|
var ch: char
|
|
|
|
|
case man.workers[i].status
|
|
|
|
|
of SyncWorkerStatus.Sleeping:
|
|
|
|
|
ch = 's'
|
|
|
|
|
inc(sleeping)
|
|
|
|
|
of SyncWorkerStatus.WaitingPeer:
|
|
|
|
|
ch = 'w'
|
|
|
|
|
inc(waiting)
|
|
|
|
|
of SyncWorkerStatus.UpdatingStatus:
|
|
|
|
|
ch = 'U'
|
|
|
|
|
inc(pending)
|
|
|
|
|
of SyncWorkerStatus.Requesting:
|
|
|
|
|
ch = 'R'
|
|
|
|
|
inc(pending)
|
|
|
|
|
of SyncWorkerStatus.Downloading:
|
|
|
|
|
ch = 'D'
|
|
|
|
|
inc(pending)
|
|
|
|
|
of SyncWorkerStatus.Processing:
|
|
|
|
|
ch = 'P'
|
|
|
|
|
inc(pending)
|
|
|
|
|
map[i] = ch
|
|
|
|
|
(map, sleeping, waiting, pending)
|
2020-01-21 18:30:21 +00:00
|
|
|
|
|
2020-11-10 13:47:26 +00:00
|
|
|
|
proc guardTask[A, B](man: SyncManager[A, B]) {.async.} =
|
|
|
|
|
var pending: array[SyncWorkersCount, Future[void]]
|
|
|
|
|
|
|
|
|
|
# Starting all the synchronization workers.
|
|
|
|
|
for i in 0 ..< len(man.workers):
|
|
|
|
|
let future = syncWorker[A, B](man, i)
|
|
|
|
|
man.workers[i].future = future
|
|
|
|
|
pending[i] = future
|
|
|
|
|
|
|
|
|
|
# Wait for synchronization worker's failure and replace it with new one.
|
|
|
|
|
while true:
|
|
|
|
|
let failFuture = await one(pending)
|
|
|
|
|
let index = pending.find(failFuture)
|
|
|
|
|
if failFuture.failed():
|
|
|
|
|
warn "Synchronization worker stopped working unexpectedly with an error",
|
|
|
|
|
index = index, errMsg = failFuture.error.msg
|
|
|
|
|
else:
|
|
|
|
|
warn "Synchronization worker stopped working unexpectedly without error",
|
|
|
|
|
index = index
|
|
|
|
|
|
|
|
|
|
let future = syncWorker[A, B](man, index)
|
|
|
|
|
man.workers[index].future = future
|
|
|
|
|
pending[index] = future
|
|
|
|
|
|
2020-11-12 11:16:51 +00:00
|
|
|
|
proc toTimeLeftString(d: Duration): string =
|
|
|
|
|
var v = d
|
|
|
|
|
var res = ""
|
|
|
|
|
let ndays = chronos.days(v)
|
|
|
|
|
if ndays > 0:
|
|
|
|
|
res = res & (if ndays < 10: "0" & $ndays else: $ndays) & "d"
|
|
|
|
|
v = v - chronos.days(ndays)
|
|
|
|
|
|
|
|
|
|
let nhours = chronos.hours(v)
|
|
|
|
|
if nhours > 0:
|
|
|
|
|
res = res & (if nhours < 10: "0" & $nhours else: $nhours) & "h"
|
|
|
|
|
v = v - chronos.hours(nhours)
|
|
|
|
|
else:
|
|
|
|
|
res = res & "00h"
|
|
|
|
|
|
|
|
|
|
let nmins = chronos.minutes(v)
|
|
|
|
|
if nmins > 0:
|
|
|
|
|
res = res & (if nmins < 10: "0" & $nmins else: $nmins) & "m"
|
|
|
|
|
v = v - chronos.minutes(nmins)
|
|
|
|
|
else:
|
|
|
|
|
res = res & "00m"
|
|
|
|
|
res
|
|
|
|
|
|
2020-08-10 07:15:50 +00:00
|
|
|
|
proc syncLoop[A, B](man: SyncManager[A, B]) {.async.} =
|
2020-04-20 14:59:18 +00:00
|
|
|
|
mixin getKey, getScore
|
2020-10-27 09:25:28 +00:00
|
|
|
|
var pauseTime = 0
|
2020-01-21 18:30:21 +00:00
|
|
|
|
|
2020-11-10 13:47:26 +00:00
|
|
|
|
asyncSpawn man.guardTask()
|
2020-01-21 18:30:21 +00:00
|
|
|
|
|
2020-09-11 12:46:01 +00:00
|
|
|
|
debug "Synchronization loop started", topics = "syncman"
|
|
|
|
|
|
2020-10-27 09:25:28 +00:00
|
|
|
|
proc averageSpeedTask() {.async.} =
|
|
|
|
|
while true:
|
|
|
|
|
let wallSlot = man.getLocalWallSlot()
|
|
|
|
|
let headSlot = man.getLocalHeadSlot()
|
|
|
|
|
let lsm1 = SyncMoment.now(man.getLocalHeadSlot())
|
|
|
|
|
await sleepAsync(chronos.seconds(int(SECONDS_PER_SLOT)))
|
|
|
|
|
let lsm2 = SyncMoment.now(man.getLocalHeadSlot())
|
|
|
|
|
let bps =
|
|
|
|
|
if lsm2.slot - lsm1.slot == 0'u64:
|
|
|
|
|
0.0
|
|
|
|
|
else:
|
|
|
|
|
speed(lsm1, lsm2)
|
|
|
|
|
inc(man.syncCount)
|
|
|
|
|
man.insSyncSpeed = bps
|
|
|
|
|
man.avgSyncSpeed = man.avgSyncSpeed +
|
|
|
|
|
(bps - man.avgSyncSpeed) / float(man.syncCount)
|
2020-11-12 11:16:51 +00:00
|
|
|
|
let nsec = (float(wallSlot - headSlot) / man.avgSyncSpeed) *
|
|
|
|
|
1_000_000_000.0
|
|
|
|
|
man.timeLeft = chronos.nanoseconds(int64(nsec))
|
2020-10-27 09:25:28 +00:00
|
|
|
|
|
|
|
|
|
asyncSpawn averageSpeedTask()
|
2020-05-19 12:08:50 +00:00
|
|
|
|
|
2020-04-20 14:59:18 +00:00
|
|
|
|
while true:
|
2020-06-14 09:45:53 +00:00
|
|
|
|
let wallSlot = man.getLocalWallSlot()
|
|
|
|
|
let headSlot = man.getLocalHeadSlot()
|
2020-04-20 14:59:18 +00:00
|
|
|
|
|
2020-09-11 12:46:01 +00:00
|
|
|
|
let (map, sleeping, waiting, pending) = man.getWorkersStats()
|
2020-04-23 15:31:00 +00:00
|
|
|
|
|
2020-10-27 09:25:28 +00:00
|
|
|
|
debug "Current syncing state", workers_map = map,
|
2020-09-11 12:46:01 +00:00
|
|
|
|
sleeping_workers_count = sleeping,
|
|
|
|
|
waiting_workers_count = waiting,
|
|
|
|
|
pending_workers_count = pending,
|
|
|
|
|
wall_head_slot = wallSlot, local_head_slot = headSlot,
|
2020-10-27 09:25:28 +00:00
|
|
|
|
pause_time = $chronos.seconds(pauseTime),
|
|
|
|
|
avg_sync_speed = man.avgSyncSpeed, ins_sync_speed = man.insSyncSpeed,
|
2020-06-14 09:45:53 +00:00
|
|
|
|
topics = "syncman"
|
2020-04-20 14:59:18 +00:00
|
|
|
|
|
2020-09-11 12:46:01 +00:00
|
|
|
|
# Update status string
|
|
|
|
|
man.syncStatus = map & ":" & $pending & ":" &
|
2020-10-27 09:25:28 +00:00
|
|
|
|
man.insSyncSpeed.formatBiggestFloat(ffDecimal, 4) & ":" &
|
2020-11-12 11:16:51 +00:00
|
|
|
|
man.avgSyncSpeed.formatBiggestFloat(ffDecimal, 4) & ":" &
|
|
|
|
|
man.timeLeft.toTimeLeftString() &
|
2020-10-27 09:25:28 +00:00
|
|
|
|
" (" & $man.queue.outSlot & ")"
|
2020-05-19 12:08:50 +00:00
|
|
|
|
|
2020-06-14 09:45:53 +00:00
|
|
|
|
if headAge <= man.maxHeadAge:
|
2020-09-11 12:46:01 +00:00
|
|
|
|
man.notInSyncEvent.clear()
|
|
|
|
|
# We are marking SyncManager as not working only when we are in sync and
|
|
|
|
|
# all sync workers are in `Sleeping` state.
|
|
|
|
|
if pending > 0:
|
|
|
|
|
debug "Synchronization loop waits for workers completion",
|
|
|
|
|
wall_head_slot = wallSlot, local_head_slot = headSlot,
|
|
|
|
|
difference = (wallSlot - headSlot), max_head_age = man.maxHeadAge,
|
|
|
|
|
sleeping_workers_count = sleeping,
|
|
|
|
|
waiting_workers_count = waiting, pending_workers_count = pending,
|
|
|
|
|
topics = "syncman"
|
|
|
|
|
man.inProgress = true
|
|
|
|
|
else:
|
|
|
|
|
debug "Synchronization loop sleeping", wall_head_slot = wallSlot,
|
|
|
|
|
local_head_slot = headSlot, difference = (wallSlot - headSlot),
|
2020-06-14 09:45:53 +00:00
|
|
|
|
max_head_age = man.maxHeadAge, topics = "syncman"
|
|
|
|
|
man.inProgress = false
|
|
|
|
|
else:
|
2020-10-30 12:33:52 +00:00
|
|
|
|
if not(man.notInSyncEvent.isSet()):
|
|
|
|
|
# We get here only if we lost sync for more then `maxHeadAge` period.
|
|
|
|
|
if pending == 0:
|
|
|
|
|
man.queue = SyncQueue.init(A, man.getLocalHeadSlot(),
|
|
|
|
|
man.getLocalWallSlot(),
|
|
|
|
|
man.chunkSize, man.getFinalizedSlot,
|
2021-05-28 16:34:00 +00:00
|
|
|
|
man.blockProcessor, 1)
|
2020-10-30 12:33:52 +00:00
|
|
|
|
man.notInSyncEvent.fire()
|
|
|
|
|
man.inProgress = true
|
|
|
|
|
else:
|
|
|
|
|
man.notInSyncEvent.fire()
|
|
|
|
|
man.inProgress = true
|
2020-06-14 09:45:53 +00:00
|
|
|
|
|
2020-09-11 12:46:01 +00:00
|
|
|
|
if queueAge <= man.rangeAge:
|
|
|
|
|
# We are in requested range ``man.rangeAge``.
|
|
|
|
|
man.inRangeEvent.fire()
|
|
|
|
|
man.notInRangeEvent.clear()
|
|
|
|
|
else:
|
|
|
|
|
# We are not in requested range anymore ``man.rangeAge``.
|
|
|
|
|
man.inRangeEvent.clear()
|
|
|
|
|
man.notInRangeEvent.fire()
|
2020-06-14 09:45:53 +00:00
|
|
|
|
|
2020-09-11 12:46:01 +00:00
|
|
|
|
await sleepAsync(chronos.seconds(2))
|
|
|
|
|
|
2020-08-10 07:15:50 +00:00
|
|
|
|
proc start*[A, B](man: SyncManager[A, B]) =
|
|
|
|
|
## Starts SyncManager's main loop.
|
|
|
|
|
man.syncFut = man.syncLoop()
|
2020-11-26 19:23:45 +00:00
|
|
|
|
|
|
|
|
|
proc getInfo*[A, B](man: SyncManager[A, B]): SyncInfo =
|
|
|
|
|
## Returns current synchronization information for RPC call.
|
|
|
|
|
let wallSlot = man.getLocalWallSlot()
|
|
|
|
|
let headSlot = man.getLocalHeadSlot()
|
2021-03-04 09:13:23 +00:00
|
|
|
|
let sync_distance = wallSlot - headSlot
|
2021-03-17 20:42:55 +00:00
|
|
|
|
(
|
|
|
|
|
head_slot: headSlot,
|
|
|
|
|
sync_distance: sync_distance,
|
|
|
|
|
is_syncing: man.inProgress
|
|
|
|
|
)
|