2021-03-26 06:52:01 +00:00
|
|
|
# beacon_chain
|
2023-01-20 14:14:37 +00:00
|
|
|
# Copyright (c) 2018-2023 Status Research & Development GmbH
|
2021-03-26 06:52:01 +00:00
|
|
|
# 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.
|
|
|
|
|
2023-01-20 14:14:37 +00:00
|
|
|
{.push raises: [].}
|
2021-03-26 06:52:01 +00:00
|
|
|
|
2022-01-26 12:20:08 +00:00
|
|
|
import std/[sequtils, strutils]
|
2020-06-18 10:03:36 +00:00
|
|
|
import chronos, chronicles
|
2021-08-12 13:08:20 +00:00
|
|
|
import
|
2022-01-26 12:20:08 +00:00
|
|
|
../spec/datatypes/[phase0],
|
2021-08-12 13:08:20 +00:00
|
|
|
../spec/forks,
|
|
|
|
../networking/eth2_network,
|
2022-01-26 12:20:08 +00:00
|
|
|
../consensus_object_pools/block_quarantine,
|
2021-08-12 13:08:20 +00:00
|
|
|
"."/sync_protocol, "."/sync_manager
|
2023-01-31 23:25:08 +00:00
|
|
|
from ../beacon_clock import GetBeaconTimeFn
|
2022-06-10 14:16:37 +00:00
|
|
|
export block_quarantine, sync_manager
|
2020-06-18 10:03:36 +00:00
|
|
|
|
|
|
|
logScope:
|
|
|
|
topics = "requman"
|
|
|
|
|
|
|
|
const
|
2020-08-12 09:29:11 +00:00
|
|
|
SYNC_MAX_REQUESTED_BLOCKS* = 32 # Spec allows up to MAX_REQUEST_BLOCKS.
|
|
|
|
## Maximum number of blocks which will be requested in each
|
|
|
|
## `beaconBlocksByRoot` invocation.
|
2020-06-18 10:03:36 +00:00
|
|
|
PARALLEL_REQUESTS* = 2
|
|
|
|
## Number of peers we using to resolve our request.
|
2019-03-28 14:03:19 +00:00
|
|
|
|
2019-11-25 14:36:25 +00:00
|
|
|
type
|
2021-12-16 14:57:16 +00:00
|
|
|
BlockVerifier* =
|
|
|
|
proc(signedBlock: ForkedSignedBeaconBlock):
|
2022-11-10 17:40:27 +00:00
|
|
|
Future[Result[void, VerifierError]] {.gcsafe, raises: [Defect].}
|
2023-01-31 23:25:08 +00:00
|
|
|
BlockBlobsVerifier* =
|
|
|
|
proc(signedBlock: ForkedSignedBeaconBlock, blobs: eip4844.BlobsSidecar):
|
|
|
|
Future[Result[void, VerifierError]] {.gcsafe, raises: [Defect].}
|
2021-12-16 14:57:16 +00:00
|
|
|
|
2019-11-25 14:36:25 +00:00
|
|
|
RequestManager* = object
|
|
|
|
network*: Eth2Node
|
2020-08-12 09:29:11 +00:00
|
|
|
inpQueue*: AsyncQueue[FetchRecord]
|
2023-01-31 23:25:08 +00:00
|
|
|
cfg: RuntimeConfig
|
|
|
|
getBeaconTime: GetBeaconTimeFn
|
2021-12-16 14:57:16 +00:00
|
|
|
blockVerifier: BlockVerifier
|
2023-01-31 23:25:08 +00:00
|
|
|
blockBlobsVerifier: BlockBlobsVerifier
|
2020-06-18 10:03:36 +00:00
|
|
|
loopFuture: Future[void]
|
2019-11-25 14:36:25 +00:00
|
|
|
|
2020-06-18 10:03:36 +00:00
|
|
|
func shortLog*(x: seq[Eth2Digest]): string =
|
|
|
|
"[" & x.mapIt(shortLog(it)).join(", ") & "]"
|
|
|
|
|
|
|
|
func shortLog*(x: seq[FetchRecord]): string =
|
|
|
|
"[" & x.mapIt(shortLog(it.root)).join(", ") & "]"
|
|
|
|
|
|
|
|
proc init*(T: type RequestManager, network: Eth2Node,
|
2023-01-31 23:25:08 +00:00
|
|
|
cfg: RuntimeConfig,
|
|
|
|
getBeaconTime: GetBeaconTimeFn,
|
|
|
|
blockVerifier: BlockVerifier,
|
|
|
|
blockBlobsVerifier: BlockBlobsVerifier): RequestManager =
|
2020-08-12 09:29:11 +00:00
|
|
|
RequestManager(
|
|
|
|
network: network,
|
|
|
|
inpQueue: newAsyncQueue[FetchRecord](),
|
2023-01-31 23:25:08 +00:00
|
|
|
cfg: cfg,
|
|
|
|
getBeaconTime: getBeaconTime,
|
|
|
|
blockVerifier: blockVerifier,
|
|
|
|
blockBlobsVerifier: blockBlobsVerifier,
|
2020-06-18 10:03:36 +00:00
|
|
|
)
|
|
|
|
|
2023-01-31 23:25:08 +00:00
|
|
|
proc checkResponse(roots: openArray[Eth2Digest],
|
|
|
|
blocks: openArray[ref SignedBeaconBlockAndBlobsSidecar]): bool =
|
|
|
|
## This procedure checks peer's response.
|
|
|
|
var checks = @roots
|
|
|
|
if len(blocks) > len(roots):
|
|
|
|
return false
|
|
|
|
for item in blocks:
|
|
|
|
let res = checks.find(item[].beacon_block.root)
|
|
|
|
if res == -1:
|
|
|
|
return false
|
|
|
|
else:
|
|
|
|
checks.del(res)
|
|
|
|
true
|
|
|
|
|
2020-06-18 10:03:36 +00:00
|
|
|
proc checkResponse(roots: openArray[Eth2Digest],
|
2022-02-07 17:20:10 +00:00
|
|
|
blocks: openArray[ref ForkedSignedBeaconBlock]): bool =
|
2020-06-18 10:03:36 +00:00
|
|
|
## This procedure checks peer's response.
|
|
|
|
var checks = @roots
|
|
|
|
if len(blocks) > len(roots):
|
|
|
|
return false
|
|
|
|
for blk in blocks:
|
2022-02-07 17:20:10 +00:00
|
|
|
let res = checks.find(blk[].root)
|
2020-06-18 10:03:36 +00:00
|
|
|
if res == -1:
|
|
|
|
return false
|
|
|
|
else:
|
|
|
|
checks.del(res)
|
|
|
|
return true
|
|
|
|
|
|
|
|
proc fetchAncestorBlocksFromNetwork(rman: RequestManager,
|
|
|
|
items: seq[Eth2Digest]) {.async.} =
|
2020-03-22 20:54:47 +00:00
|
|
|
var peer: Peer
|
|
|
|
try:
|
2020-06-18 10:03:36 +00:00
|
|
|
peer = await rman.network.peerPool.acquire()
|
|
|
|
debug "Requesting blocks by root", peer = peer, blocks = shortLog(items),
|
|
|
|
peer_score = peer.getScore()
|
|
|
|
|
2022-10-27 16:51:43 +00:00
|
|
|
let blocks = (await beaconBlocksByRoot_v2(peer, BlockRootsList items))
|
2021-07-15 19:01:07 +00:00
|
|
|
|
2020-05-12 22:37:07 +00:00
|
|
|
if blocks.isOk:
|
2020-06-18 10:03:36 +00:00
|
|
|
let ublocks = blocks.get()
|
2022-10-27 16:51:43 +00:00
|
|
|
if checkResponse(items, ublocks.asSeq()):
|
2022-01-26 12:20:08 +00:00
|
|
|
var
|
|
|
|
gotGoodBlock = false
|
|
|
|
gotUnviableBlock = false
|
|
|
|
|
|
|
|
for b in ublocks:
|
2022-02-07 17:20:10 +00:00
|
|
|
let ver = await rman.blockVerifier(b[])
|
2022-01-26 12:20:08 +00:00
|
|
|
if ver.isErr():
|
|
|
|
case ver.error()
|
2022-11-10 17:40:27 +00:00
|
|
|
of VerifierError.MissingParent:
|
2022-01-26 12:20:08 +00:00
|
|
|
# Ignoring because the order of the blocks that
|
|
|
|
# we requested may be different from the order in which we need
|
|
|
|
# these blocks to apply.
|
|
|
|
discard
|
2022-11-10 17:40:27 +00:00
|
|
|
of VerifierError.Duplicate:
|
2022-01-26 12:20:08 +00:00
|
|
|
# Ignoring because these errors could occur due to the
|
|
|
|
# concurrent/parallel requests we made.
|
|
|
|
discard
|
2022-11-10 17:40:27 +00:00
|
|
|
of VerifierError.UnviableFork:
|
2022-01-26 12:20:08 +00:00
|
|
|
# If they're working a different fork, we'll want to descore them
|
|
|
|
# but also process the other blocks (in case we can register the
|
|
|
|
# other blocks as unviable)
|
|
|
|
gotUnviableBlock = true
|
2022-11-10 17:40:27 +00:00
|
|
|
of VerifierError.Invalid:
|
2022-01-26 12:20:08 +00:00
|
|
|
# We stop processing blocks because peer is either sending us
|
|
|
|
# junk or working a different fork
|
2022-07-06 10:34:12 +00:00
|
|
|
notice "Received invalid block",
|
2022-01-26 12:20:08 +00:00
|
|
|
peer = peer, blocks = shortLog(items),
|
|
|
|
peer_score = peer.getScore()
|
2022-11-11 11:34:28 +00:00
|
|
|
peer.updateScore(PeerScoreBadValues)
|
2022-01-26 12:20:08 +00:00
|
|
|
|
|
|
|
return # Stop processing this junk...
|
|
|
|
else:
|
|
|
|
gotGoodBlock = true
|
|
|
|
|
|
|
|
if gotUnviableBlock:
|
|
|
|
notice "Received blocks from an unviable fork",
|
|
|
|
peer = peer, blocks = shortLog(items),
|
|
|
|
peer_score = peer.getScore()
|
|
|
|
peer.updateScore(PeerScoreUnviableFork)
|
|
|
|
elif gotGoodBlock:
|
|
|
|
# We reward peer only if it returns something.
|
2022-11-11 11:34:28 +00:00
|
|
|
peer.updateScore(PeerScoreGoodValues)
|
2022-01-26 12:20:08 +00:00
|
|
|
|
2020-06-18 10:03:36 +00:00
|
|
|
else:
|
|
|
|
peer.updateScore(PeerScoreBadResponse)
|
|
|
|
else:
|
2022-11-11 11:34:28 +00:00
|
|
|
peer.updateScore(PeerScoreNoValues)
|
2020-06-18 10:03:36 +00:00
|
|
|
|
|
|
|
except CancelledError as exc:
|
|
|
|
raise exc
|
|
|
|
except CatchableError as exc:
|
2022-11-11 11:34:28 +00:00
|
|
|
peer.updateScore(PeerScoreNoValues)
|
2020-06-18 10:03:36 +00:00
|
|
|
debug "Error while fetching ancestor blocks", exc = exc.msg,
|
|
|
|
items = shortLog(items), peer = peer, peer_score = peer.getScore()
|
|
|
|
raise exc
|
2020-03-22 20:54:47 +00:00
|
|
|
finally:
|
|
|
|
if not(isNil(peer)):
|
2020-06-18 10:03:36 +00:00
|
|
|
rman.network.peerPool.release(peer)
|
|
|
|
|
2023-01-31 23:25:08 +00:00
|
|
|
proc fetchAncestorBlocksAndBlobsFromNetwork(rman: RequestManager,
|
|
|
|
items: seq[Eth2Digest]) {.async.} =
|
|
|
|
var peer: Peer
|
|
|
|
try:
|
|
|
|
peer = await rman.network.peerPool.acquire()
|
2023-02-01 14:14:50 +00:00
|
|
|
debug "Requesting blocks and sidecars by root",
|
|
|
|
peer = peer, blocks = shortLog(items), peer_score = peer.getScore()
|
2023-01-31 23:25:08 +00:00
|
|
|
|
|
|
|
let blocks = (await beaconBlockAndBlobsSidecarByRoot_v1(peer, BlockRootsList items))
|
|
|
|
|
|
|
|
if blocks.isOk:
|
|
|
|
let ublocks = blocks.get()
|
|
|
|
if checkResponse(items, ublocks.asSeq()):
|
|
|
|
var
|
|
|
|
gotGoodBlock = false
|
|
|
|
gotUnviableBlock = false
|
|
|
|
|
|
|
|
for b in ublocks:
|
|
|
|
let ver = await rman.blockBlobsVerifier(ForkedSignedBeaconBlock.init(b[].beacon_block), b[].blobs_sidecar)
|
|
|
|
if ver.isErr():
|
|
|
|
case ver.error()
|
|
|
|
of VerifierError.MissingParent:
|
|
|
|
# Ignoring because the order of the blocks that
|
|
|
|
# we requested may be different from the order in which we need
|
|
|
|
# these blocks to apply.
|
|
|
|
discard
|
|
|
|
of VerifierError.Duplicate:
|
|
|
|
# Ignoring because these errors could occur due to the
|
|
|
|
# concurrent/parallel requests we made.
|
|
|
|
discard
|
|
|
|
of VerifierError.UnviableFork:
|
|
|
|
# If they're working a different fork, we'll want to descore them
|
|
|
|
# but also process the other blocks (in case we can register the
|
|
|
|
# other blocks as unviable)
|
|
|
|
gotUnviableBlock = true
|
|
|
|
of VerifierError.Invalid:
|
|
|
|
# We stop processing blocks because peer is either sending us
|
|
|
|
# junk or working a different fork
|
2023-02-01 14:14:50 +00:00
|
|
|
notice "Received invalid block and blobs",
|
2023-01-31 23:25:08 +00:00
|
|
|
peer = peer, blocks = shortLog(items),
|
|
|
|
peer_score = peer.getScore()
|
|
|
|
peer.updateScore(PeerScoreBadValues)
|
|
|
|
|
|
|
|
return # Stop processing this junk...
|
|
|
|
else:
|
|
|
|
gotGoodBlock = true
|
|
|
|
|
|
|
|
if gotUnviableBlock:
|
2023-02-01 14:14:50 +00:00
|
|
|
notice "Received blocks and blobs from an unviable fork",
|
2023-01-31 23:25:08 +00:00
|
|
|
peer = peer, blocks = shortLog(items),
|
|
|
|
peer_score = peer.getScore()
|
|
|
|
peer.updateScore(PeerScoreUnviableFork)
|
|
|
|
elif gotGoodBlock:
|
|
|
|
# We reward peer only if it returns something.
|
|
|
|
peer.updateScore(PeerScoreGoodValues)
|
|
|
|
else:
|
|
|
|
let err = blocks.error()
|
|
|
|
case err.kind
|
|
|
|
of ReceivedErrorResponse:
|
|
|
|
if err.responseCode == ResourceUnavailable:
|
|
|
|
if not(isNil(peer)):
|
|
|
|
rman.network.peerPool.release(peer)
|
|
|
|
await rman.fetchAncestorBlocksFromNetwork(items)
|
|
|
|
return
|
|
|
|
else:
|
|
|
|
peer.updateScore(PeerScoreBadResponse)
|
|
|
|
else:
|
|
|
|
discard
|
|
|
|
else:
|
|
|
|
peer.updateScore(PeerScoreNoValues)
|
|
|
|
|
|
|
|
except CancelledError as exc:
|
|
|
|
raise exc
|
|
|
|
except CatchableError as exc:
|
|
|
|
peer.updateScore(PeerScoreNoValues)
|
2023-02-01 14:14:50 +00:00
|
|
|
debug "Error while fetching ancestor blocks and blobs", exc = exc.msg,
|
2023-01-31 23:25:08 +00:00
|
|
|
items = shortLog(items), peer = peer, peer_score = peer.getScore()
|
|
|
|
raise exc
|
|
|
|
finally:
|
|
|
|
if not(isNil(peer)):
|
|
|
|
rman.network.peerPool.release(peer)
|
|
|
|
|
|
|
|
|
|
|
|
proc isBlobsTime(rman: RequestManager): bool =
|
|
|
|
rman.getBeaconTime().slotOrZero.epoch >= rman.cfg.EIP4844_FORK_EPOCH
|
|
|
|
|
2020-06-18 10:03:36 +00:00
|
|
|
proc requestManagerLoop(rman: RequestManager) {.async.} =
|
|
|
|
var rootList = newSeq[Eth2Digest]()
|
|
|
|
var workers = newSeq[Future[void]](PARALLEL_REQUESTS)
|
|
|
|
while true:
|
|
|
|
try:
|
|
|
|
rootList.setLen(0)
|
2020-08-12 09:29:11 +00:00
|
|
|
let req = await rman.inpQueue.popFirst()
|
2020-06-18 10:03:36 +00:00
|
|
|
rootList.add(req.root)
|
|
|
|
|
2020-08-12 09:29:11 +00:00
|
|
|
var count = min(SYNC_MAX_REQUESTED_BLOCKS - 1, len(rman.inpQueue))
|
2020-06-18 10:03:36 +00:00
|
|
|
while count > 0:
|
2020-08-12 09:29:11 +00:00
|
|
|
rootList.add(rman.inpQueue.popFirstNoWait().root)
|
2020-06-18 10:03:36 +00:00
|
|
|
dec(count)
|
|
|
|
|
2021-12-16 14:57:16 +00:00
|
|
|
let start = SyncMoment.now(0)
|
2020-06-18 10:03:36 +00:00
|
|
|
|
2023-01-31 23:25:08 +00:00
|
|
|
# As soon as EIP4844_FORK_EPOCH comes around in wall time, we
|
|
|
|
# switch to requesting blocks and blobs. In the vicinity of the
|
|
|
|
# transition, that means that we *may* request blobs for a
|
|
|
|
# pre-eip4844. In that case, we get ResourceUnavailable from the
|
|
|
|
# peer and fall back to requesting blocks only.
|
|
|
|
let getBlobs = rman.isBlobsTime()
|
2020-06-18 10:03:36 +00:00
|
|
|
for i in 0 ..< PARALLEL_REQUESTS:
|
2023-01-31 23:25:08 +00:00
|
|
|
workers[i] = if getBlobs:
|
|
|
|
rman.fetchAncestorBlocksAndBlobsFromNetwork(rootList)
|
|
|
|
else:
|
|
|
|
rman.fetchAncestorBlocksFromNetwork(rootList)
|
2020-06-18 10:03:36 +00:00
|
|
|
|
|
|
|
# We do not care about
|
|
|
|
await allFutures(workers)
|
|
|
|
|
2021-12-16 14:57:16 +00:00
|
|
|
let finish = SyncMoment.now(uint64(len(rootList)))
|
2020-06-18 10:03:36 +00:00
|
|
|
|
|
|
|
var succeed = 0
|
|
|
|
for worker in workers:
|
|
|
|
if worker.finished() and not(worker.failed()):
|
|
|
|
inc(succeed)
|
|
|
|
|
|
|
|
debug "Request manager tick", blocks_count = len(rootList),
|
|
|
|
succeed = succeed,
|
|
|
|
failed = (len(workers) - succeed),
|
2020-08-12 09:29:11 +00:00
|
|
|
queue_size = len(rman.inpQueue),
|
2020-06-18 10:03:36 +00:00
|
|
|
sync_speed = speed(start, finish)
|
|
|
|
|
|
|
|
except CatchableError as exc:
|
|
|
|
debug "Got a problem in request manager", exc = exc.msg
|
|
|
|
|
|
|
|
proc start*(rman: var RequestManager) =
|
|
|
|
## Start Request Manager's loop.
|
2020-08-12 09:29:11 +00:00
|
|
|
rman.loopFuture = rman.requestManagerLoop()
|
2020-06-18 10:03:36 +00:00
|
|
|
|
|
|
|
proc stop*(rman: RequestManager) =
|
|
|
|
## Stop Request Manager's loop.
|
|
|
|
if not(isNil(rman.loopFuture)):
|
|
|
|
rman.loopFuture.cancel()
|
2020-02-26 13:31:24 +00:00
|
|
|
|
2020-06-18 10:03:36 +00:00
|
|
|
proc fetchAncestorBlocks*(rman: RequestManager, roots: seq[FetchRecord]) =
|
|
|
|
## Enqueue list missing blocks roots ``roots`` for download by
|
|
|
|
## Request Manager ``rman``.
|
|
|
|
for item in roots:
|
2021-03-26 06:52:01 +00:00
|
|
|
try:
|
|
|
|
rman.inpQueue.addLastNoWait(item)
|
|
|
|
except AsyncQueueFullError: raiseAssert "unbounded queue"
|