nimbus-eth1/nimbus/sync/beacon/worker/headers_unproc.nim
Jordan Hrycaj 05483d89bd
Rename flare as beacon (#2680)
* Remove `--sync-mode` option from nimbus config

why:
  Currently there is only one sync mode available.

* Rename `flare` -> `beacon`, but not base module folder and nim source

why:
  The name `flare` was used do designate an alternative `beacon` mode that.

  Leaving the base folder and source as-is for a moment, makes it easier
  to read change diffs.

* Rename `flare` base module folder and nim source: `flare` -> `beacon`
2024-10-02 11:31:33 +00:00

127 lines
3.7 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/[common, p2p],
pkg/results,
pkg/stew/interval_set,
../worker_desc
# ------------------------------------------------------------------------------
# Public functions
# ------------------------------------------------------------------------------
proc headersUnprocFetch*(
ctx: BeaconCtxRef;
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)
ctx.lhc.borrowed += iv.len
ok(iv)
proc headersUnprocCommit*(ctx: BeaconCtxRef; borrowed: uint) =
## Commit back all processed range
ctx.lhc.borrowed -= borrowed
proc headersUnprocCommit*(ctx: BeaconCtxRef; borrowed: uint; retuor: BnRange) =
## Merge back unprocessed range `retour`
ctx.headersUnprocCommit borrowed
doAssert ctx.lhc.unprocessed.merge(retuor) == retuor.len
proc headersUnprocCommit*(
ctx: BeaconCtxRef;
borrowed: uint;
rMinPt: BlockNumber;
rMaxPt: BlockNumber) =
## Variant of `headersUnprocCommit()`
ctx.headersUnprocCommit borrowed
doAssert ctx.lhc.unprocessed.merge(rMinPt, rMaxPt) == rMaxPt - rMinPt + 1
proc headersUnprocCovered*(ctx: BeaconCtxRef; minPt,maxPt: BlockNumber): uint64 =
## Check whether range is fully contained
ctx.lhc.unprocessed.covered(minPt, maxPt)
proc headersUnprocCovered*(ctx: BeaconCtxRef; pt: BlockNumber): bool =
## Check whether point is contained
ctx.lhc.unprocessed.covered(pt, pt) == 1
proc headersUnprocTop*(ctx: BeaconCtxRef): BlockNumber =
let iv = ctx.lhc.unprocessed.le().valueOr:
return BlockNumber(0)
iv.maxPt
proc headersUnprocTotal*(ctx: BeaconCtxRef): uint64 =
ctx.lhc.unprocessed.total()
proc headersUnprocBorrowed*(ctx: BeaconCtxRef): uint64 =
ctx.lhc.borrowed
proc headersUnprocChunks*(ctx: BeaconCtxRef): int =
ctx.lhc.unprocessed.chunks()
proc headersUnprocIsEmpty*(ctx: BeaconCtxRef): bool =
ctx.lhc.unprocessed.chunks() == 0
# ------------
proc headersUnprocInit*(ctx: BeaconCtxRef) =
## Constructor
ctx.lhc.unprocessed = BnRangeSet.init()
proc headersUnprocSet*(ctx: BeaconCtxRef) =
## Clear
ctx.lhc.unprocessed.clear()
ctx.lhc.borrowed = 0u
proc headersUnprocSet*(ctx: BeaconCtxRef; iv: BnRange) =
## Set up new unprocessed range
ctx.headersUnprocSet()
discard ctx.lhc.unprocessed.merge(iv)
proc headersUnprocSet*(ctx: BeaconCtxRef; minPt, maxPt: BlockNumber) =
## Set up new unprocessed range
ctx.headersUnprocSet()
discard ctx.lhc.unprocessed.merge(minPt, maxPt)
# ------------------------------------------------------------------------------
# End
# ------------------------------------------------------------------------------