Jordan Hrycaj 71e466d173
Block header download beacon to era1 (#2601)
* Block header download starting at Beacon down to Era1

details:
  The header download implementation is intended to be completed to a
  full sync facility.

  Downloaded block headers are stored in a `CoreDb` table. Later on they
  should be fetched, complemented by a block body, executed/imported,
  and deleted from the table.

  The Era1 repository may be partial or missing. Era1 headers are neither
  downloaded nor stored on the `CoreDb` table.

  Headers are downloaded top down (largest block number first) using the
  hash of the block header by one peer. Other peers fetch headers
  opportunistically using block numbers

  Observed download times for 14m `MainNet` headers varies between 30min
  and 1h (Era1 size truncated to 66m blocks.), full download 52min
  (anectdotal.) The number of peers downloading concurrently is crucial
  here.

* Activate `flare` by command line option

* Fix copyright year
2024-09-09 09:12:56 +00:00

106 lines
3.1 KiB
Nim

# Nimbus
# Copyright (c) 2023-2024 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:[].}
import
pkg/eth/p2p,
pkg/results,
pkg/stew/interval_set,
../worker_desc
# ------------------------------------------------------------------------------
# Public functions
# ------------------------------------------------------------------------------
proc unprocFetch*(
ctx: FlareCtxRef;
maxLen: uint64;
): Result[BnRange,void] =
## Fetch interval from block ranges with maximal size `maxLen`, where
## `0` is interpreted as `2^64`.
##
let
q = ctx.lhc.unprocessed
# Fetch top right interval with largest block numbers
jv = q.le().valueOr:
return err()
# Curb interval to maximal length `maxLen`
iv = block:
if maxLen == 0 or (0 < jv.len and jv.len <= maxLen):
jv
else:
# Curb interval `jv` to length `maxLen`
#
# Note that either (fringe case):
# (`jv.len`==0) => (`jv`==`[0,high(u64)]`) => `jv.maxPt`==`high(u64)`
# or (in the non-fringe case)
# (`maxLen` < `jv.len`) => (`jv.maxPt` - `maxLen` + 1 <= `jv.maxPt`)
#
BnRange.new(jv.maxPt - maxLen + 1, jv.maxPt)
discard q.reduce(iv)
ok(iv)
proc unprocMerge*(ctx: FlareCtxRef; iv: BnRange) =
## Merge back unprocessed range
discard ctx.lhc.unprocessed.merge(iv)
proc unprocMerge*(ctx: FlareCtxRef; minPt, maxPt: BlockNumber) =
## Ditto
discard ctx.lhc.unprocessed.merge(minPt, maxPt)
proc unprocReduce*(ctx: FlareCtxRef; minPt, maxPt: BlockNumber) =
## Merge back unprocessed range
discard ctx.lhc.unprocessed.reduce(minPt, maxPt)
proc unprocFullyCovered*(
ctx: FlareCtxRef; minPt, maxPt: BlockNumber): bool =
## Check whether range is fully contained
ctx.lhc.unprocessed.covered(minPt, maxPt) == maxPt - minPt + 1
proc unprocCovered*(ctx: FlareCtxRef; minPt, maxPt: BlockNumber): uint64 =
## Check whether range is fully contained
ctx.lhc.unprocessed.covered(minPt, maxPt)
proc unprocCovered*(ctx: FlareCtxRef; pt: BlockNumber): bool =
## Check whether point is contained
ctx.lhc.unprocessed.covered(pt, pt) == 1
proc unprocClear*(ctx: FlareCtxRef) =
ctx.lhc.unprocessed.clear()
proc unprocTop*(ctx: FlareCtxRef): BlockNumber =
let iv = ctx.lhc.unprocessed.le().valueOr:
return BlockNumber(0)
iv.maxPt
proc unprocTotal*(ctx: FlareCtxRef): uint64 =
ctx.lhc.unprocessed.total()
proc unprocChunks*(ctx: FlareCtxRef): int =
ctx.lhc.unprocessed.chunks()
# ------------
proc unprocInit*(ctx: FlareCtxRef) =
## Constructor
ctx.lhc.unprocessed = BnRangeSet.init()
# ------------------------------------------------------------------------------
# End
# ------------------------------------------------------------------------------