2021-07-14 15:13:27 +00:00
|
|
|
# Nimbus
|
|
|
|
# Copyright (c) 2018 Status Research & Development GmbH
|
|
|
|
# Licensed under either of
|
|
|
|
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0)
|
|
|
|
# * MIT license ([LICENSE-MIT](LICENSE-MIT) or
|
|
|
|
# http://opensource.org/licenses/MIT)
|
|
|
|
# at your option. This file may not be copied, modified, or distributed except
|
|
|
|
# according to those terms.
|
|
|
|
|
|
|
|
##
|
|
|
|
## Snapshot for Clique PoA Consensus Protocol
|
|
|
|
## ==========================================
|
|
|
|
##
|
|
|
|
## For details see
|
|
|
|
## `EIP-225 <https://github.com/ethereum/EIPs/blob/master/EIPS/eip-225.md>`_
|
|
|
|
## and
|
|
|
|
## `go-ethereum <https://github.com/ethereum/EIPs/blob/master/EIPS/eip-225.md>`_
|
|
|
|
##
|
|
|
|
|
|
|
|
import
|
2022-07-21 18:16:28 +00:00
|
|
|
std/[sequtils, strutils],
|
2021-07-14 15:13:27 +00:00
|
|
|
chronicles,
|
|
|
|
eth/[common, keys],
|
|
|
|
nimcrypto,
|
2022-07-21 18:16:28 +00:00
|
|
|
stew/[keyed_queue, results],
|
|
|
|
stint,
|
|
|
|
"../.."/[constants, db/db_chain, utils/prettify],
|
|
|
|
"."/[clique_cfg, clique_defs, clique_desc],
|
|
|
|
./snapshot/[snapshot_apply, snapshot_desc]
|
2021-07-14 15:13:27 +00:00
|
|
|
|
|
|
|
type
|
|
|
|
# Internal sub-descriptor for `LocalSnapsDesc`
|
|
|
|
LocalPivot = object
|
|
|
|
header: BlockHeader
|
|
|
|
hash: Hash256
|
|
|
|
|
|
|
|
# Internal sub-descriptor for `LocalSnapsDesc`
|
|
|
|
LocalPath = object
|
|
|
|
snaps: Snapshot ## snapshot for given hash
|
2021-07-21 13:31:52 +00:00
|
|
|
chain: seq[BlockHeader] ## header chain towards snapshot
|
2021-07-14 15:13:27 +00:00
|
|
|
error: CliqueError ## error message
|
|
|
|
|
2021-07-21 13:31:52 +00:00
|
|
|
# Internal sub-descriptor for `LocalSnapsDesc`
|
|
|
|
LocalSubChain = object
|
|
|
|
first: int ## first chain[] element to be used
|
|
|
|
top: int ## length of chain starting at position 0
|
|
|
|
|
2021-07-14 15:13:27 +00:00
|
|
|
LocalSnaps = object
|
|
|
|
c: Clique
|
|
|
|
start: LocalPivot ## start here searching for checkpoints
|
2021-07-21 13:31:52 +00:00
|
|
|
trail: LocalPath ## snapshot location
|
|
|
|
subChn: LocalSubChain ## chain[] sub-range
|
2021-07-14 15:13:27 +00:00
|
|
|
parents: seq[BlockHeader] ## explicit parents
|
|
|
|
|
2021-07-30 14:06:51 +00:00
|
|
|
|
2021-07-14 15:13:27 +00:00
|
|
|
{.push raises: [Defect].}
|
|
|
|
|
|
|
|
logScope:
|
|
|
|
topics = "clique PoA snapshot"
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
2021-07-21 13:31:52 +00:00
|
|
|
# Private debugging functions, pretty printing
|
2021-07-14 15:13:27 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2022-01-12 15:55:36 +00:00
|
|
|
template say(d: var LocalSnaps; v: varargs[untyped]): untyped =
|
2021-07-14 15:13:27 +00:00
|
|
|
discard
|
2022-01-12 15:55:36 +00:00
|
|
|
# uncomment body to enable, note that say() prints on <stderr>
|
|
|
|
# d.c.cfg.say v
|
|
|
|
|
|
|
|
proc pp(a: Hash256): string =
|
|
|
|
if a == BLANK_ROOT_HASH:
|
|
|
|
"*blank-root*"
|
|
|
|
elif a == EMPTY_SHA3:
|
|
|
|
"*empty-sha3*"
|
|
|
|
else:
|
|
|
|
a.data.mapIt(it.toHex(2)).join[56 .. 63].toLowerAscii
|
2021-07-21 13:31:52 +00:00
|
|
|
|
2022-01-12 15:55:36 +00:00
|
|
|
proc pp(q: openArray[BlockHeader]; n: int): string =
|
2021-07-30 14:06:51 +00:00
|
|
|
result = "["
|
|
|
|
if 5 < n:
|
|
|
|
result &= toSeq(q[0 .. 2]).mapIt("#" & $it.blockNumber).join(", ")
|
|
|
|
result &= " .." & $n & ".. #" & $q[n-1].blockNumber
|
|
|
|
else:
|
|
|
|
result &= toSeq(q[0 ..< n]).mapIt("#" & $it.blockNumber).join(", ")
|
|
|
|
result &= "]"
|
2021-07-21 13:31:52 +00:00
|
|
|
|
2022-01-12 15:55:36 +00:00
|
|
|
proc pp(b: BlockNumber, q: openArray[BlockHeader]; n: int): string =
|
2021-07-21 13:31:52 +00:00
|
|
|
"#" & $b & " + " & q.pp(n)
|
|
|
|
|
|
|
|
|
2022-01-12 15:55:36 +00:00
|
|
|
proc pp(q: openArray[BlockHeader]): string =
|
2021-07-21 13:31:52 +00:00
|
|
|
q.pp(q.len)
|
|
|
|
|
2022-01-12 15:55:36 +00:00
|
|
|
proc pp(b: BlockNumber, q: openArray[BlockHeader]): string =
|
2021-07-21 13:31:52 +00:00
|
|
|
b.pp(q, q.len)
|
|
|
|
|
|
|
|
|
2022-01-12 15:55:36 +00:00
|
|
|
proc pp(h: BlockHeader, q: openArray[BlockHeader]; n: int): string =
|
2021-07-21 13:31:52 +00:00
|
|
|
"headers=(" & h.blockNumber.pp(q,n) & ")"
|
|
|
|
|
2022-01-12 15:55:36 +00:00
|
|
|
proc pp(h: BlockHeader, q: openArray[BlockHeader]): string =
|
2021-07-21 13:31:52 +00:00
|
|
|
h.pp(q,q.len)
|
|
|
|
|
2022-01-12 15:55:36 +00:00
|
|
|
proc pp(t: var LocalPath; w: var LocalSubChain): string =
|
2021-07-21 13:31:52 +00:00
|
|
|
var (a, b) = (w.first, w.top)
|
|
|
|
if a == 0 and b == 0: b = t.chain.len
|
|
|
|
"trail=(#" & $t.snaps.blockNumber & " + " & t.chain[a ..< b].pp & ")"
|
|
|
|
|
2022-01-12 15:55:36 +00:00
|
|
|
proc pp(t: var LocalPath): string =
|
2021-07-21 13:31:52 +00:00
|
|
|
var w = LocalSubChain()
|
|
|
|
t.pp(w)
|
|
|
|
|
2022-04-08 04:54:11 +00:00
|
|
|
proc pp(err: CliqueError): string =
|
2022-01-12 15:55:36 +00:00
|
|
|
"(" & $err[0] & "," & err[1] & ")"
|
|
|
|
|
2021-07-14 15:13:27 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Private helpers
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2022-01-12 15:55:36 +00:00
|
|
|
proc maxCheckPointLe(d: var LocalSnaps; number: BlockNumber): BlockNumber =
|
2021-07-14 15:13:27 +00:00
|
|
|
let epc = number mod d.c.cfg.ckpInterval
|
|
|
|
if epc < number:
|
|
|
|
number - epc
|
|
|
|
else:
|
|
|
|
# epc == number => number < ckpInterval
|
|
|
|
0.u256
|
|
|
|
|
2022-01-12 15:55:36 +00:00
|
|
|
proc isCheckPoint(d: var LocalSnaps; number: BlockNumber): bool =
|
2021-07-14 15:13:27 +00:00
|
|
|
(number mod d.c.cfg.ckpInterval) == 0
|
|
|
|
|
2022-01-12 15:55:36 +00:00
|
|
|
proc isEpoch(d: var LocalSnaps; number: BlockNumber): bool =
|
2021-07-14 15:13:27 +00:00
|
|
|
(number mod d.c.cfg.epoch) == 0
|
|
|
|
|
2022-01-12 15:55:36 +00:00
|
|
|
proc isSnapshotPosition(d: var LocalSnaps; number: BlockNumber): bool =
|
2021-07-14 15:13:27 +00:00
|
|
|
# clique/clique.go(394): if number == 0 || (number%c.config.Epoch [..]
|
2021-07-30 14:06:51 +00:00
|
|
|
if d.isEpoch(number):
|
|
|
|
if number.isZero:
|
|
|
|
# At the genesis => snapshot the initial state.
|
|
|
|
return true
|
|
|
|
if not d.c.applySnapsMinBacklog:
|
|
|
|
return true
|
|
|
|
if d.c.cfg.roThreshold < d.trail.chain.len:
|
|
|
|
# We have piled up more headers than allowed to be re-orged (chain
|
|
|
|
# reinit from a freezer), regard checkpoint trusted and snapshot it.
|
|
|
|
return true
|
2021-07-14 15:13:27 +00:00
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Private functions
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
proc findSnapshot(d: var LocalSnaps): bool
|
2022-01-12 15:55:36 +00:00
|
|
|
{.gcsafe, raises: [Defect,CatchableError].} =
|
2021-07-14 15:13:27 +00:00
|
|
|
## Search for a snapshot starting at current header starting at the pivot
|
2021-07-21 13:31:52 +00:00
|
|
|
## value `d.start`. The snapshot returned in `trail` is a clone of the
|
|
|
|
## cached snapshot and can be modified later.
|
|
|
|
|
|
|
|
var
|
|
|
|
(header, hash) = (d.start.header, d.start.hash)
|
|
|
|
parentsLen = d.parents.len
|
2021-07-14 15:13:27 +00:00
|
|
|
|
2021-07-21 13:31:52 +00:00
|
|
|
# For convenience, ignore the current header as top parents list entry
|
|
|
|
if 0 < parentsLen and d.parents[^1] == header:
|
|
|
|
parentsLen.dec
|
2021-07-14 15:13:27 +00:00
|
|
|
|
|
|
|
while true:
|
2021-07-30 14:06:51 +00:00
|
|
|
#d.say "findSnapshot ", header.pp(d.parents, parentsLen),
|
|
|
|
# " trail=", d.trail.chain.pp
|
|
|
|
|
2021-07-14 15:13:27 +00:00
|
|
|
let number = header.blockNumber
|
|
|
|
|
2022-01-12 15:55:36 +00:00
|
|
|
# Check whether the snapshot was recently visited and cached
|
2022-07-21 18:16:28 +00:00
|
|
|
block:
|
|
|
|
let rc = d.c.recents.lruFetch(hash.data)
|
2022-04-08 04:54:11 +00:00
|
|
|
if rc.isOk:
|
2021-07-21 13:31:52 +00:00
|
|
|
d.trail.snaps = rc.value.cloneSnapshot
|
|
|
|
# d.say "findSnapshot cached ", d.trail.pp
|
2022-03-28 08:35:26 +00:00
|
|
|
trace "Found recently cached voting snapshot",
|
2021-07-14 15:13:27 +00:00
|
|
|
blockNumber = number,
|
|
|
|
blockHash = hash
|
|
|
|
return true
|
|
|
|
|
|
|
|
# If an on-disk checkpoint snapshot can be found, use that
|
2021-07-21 13:31:52 +00:00
|
|
|
if d.isCheckPoint(number):
|
|
|
|
let rc = d.c.cfg.loadSnapshot(hash)
|
|
|
|
if rc.isOk:
|
|
|
|
d.trail.snaps = rc.value.cloneSnapshot
|
2022-01-12 15:55:36 +00:00
|
|
|
d.say "findSnapshot on disk ", d.trail.pp
|
2021-07-21 13:31:52 +00:00
|
|
|
trace "Loaded voting snapshot from disk",
|
|
|
|
blockNumber = number,
|
|
|
|
blockHash = hash
|
|
|
|
# clique/clique.go(386): snap = s
|
|
|
|
return true
|
2021-07-14 15:13:27 +00:00
|
|
|
|
|
|
|
# Note that epoch is a restart and sync point. Eip-225 requires that the
|
|
|
|
# epoch header contains the full list of currently authorised signers.
|
|
|
|
if d.isSnapshotPosition(number):
|
|
|
|
# clique/clique.go(395): checkpoint := chain.GetHeaderByNumber [..]
|
2021-07-21 13:31:52 +00:00
|
|
|
d.trail.snaps = d.c.cfg.newSnapshot(header)
|
2022-07-21 18:16:28 +00:00
|
|
|
let rc = d.c.cfg.storeSnapshot(d.trail.snaps)
|
2022-04-08 04:54:11 +00:00
|
|
|
if rc.isOk:
|
2021-07-21 13:31:52 +00:00
|
|
|
d.say "findSnapshot <epoch> ", d.trail.pp
|
2022-07-21 18:16:28 +00:00
|
|
|
trace "Stored voting snapshot to disk",
|
2021-07-14 15:13:27 +00:00
|
|
|
blockNumber = number,
|
2022-07-21 18:16:28 +00:00
|
|
|
blockHash = hash,
|
|
|
|
nSnaps = d.c.cfg.nSnaps,
|
|
|
|
snapsTotal = d.c.cfg.snapsData.toSI
|
2021-07-14 15:13:27 +00:00
|
|
|
return true
|
|
|
|
|
2021-07-21 13:31:52 +00:00
|
|
|
# No snapshot for this header, get the parent header and move backward
|
|
|
|
hash = header.parentHash
|
|
|
|
# Add to batch (reversed list order, biggest block number comes first)
|
|
|
|
d.trail.chain.add header
|
|
|
|
|
|
|
|
# Assign parent header
|
2022-04-08 04:54:11 +00:00
|
|
|
if 0 < parentsLen:
|
2021-07-21 13:31:52 +00:00
|
|
|
# If we have explicit parents, pop it from the parents list
|
|
|
|
parentsLen.dec
|
|
|
|
header = d.parents[parentsLen]
|
2021-07-14 15:13:27 +00:00
|
|
|
# clique/clique.go(416): if header.Hash() != hash [..]
|
2021-07-30 14:06:51 +00:00
|
|
|
if header.blockHash != hash:
|
2021-07-21 13:31:52 +00:00
|
|
|
d.trail.error = (errUnknownAncestor,"")
|
2021-07-14 15:13:27 +00:00
|
|
|
return false
|
|
|
|
|
2021-07-21 13:31:52 +00:00
|
|
|
# No explicit parents (or no more parents left), reach out to the database
|
|
|
|
elif not d.c.cfg.db.getBlockHeader(hash, header):
|
|
|
|
d.trail.error = (errUnknownAncestor,"")
|
2021-07-14 15:13:27 +00:00
|
|
|
return false
|
|
|
|
|
|
|
|
# => while loop
|
|
|
|
|
|
|
|
# notreached
|
|
|
|
raiseAssert "findSnapshot(): wrong exit from forever-loop"
|
|
|
|
|
|
|
|
|
2021-07-21 13:31:52 +00:00
|
|
|
proc applyTrail(d: var LocalSnaps): CliqueOkResult
|
2022-01-12 15:55:36 +00:00
|
|
|
{.gcsafe, raises: [Defect,CatchableError].} =
|
2021-07-14 15:13:27 +00:00
|
|
|
## Apply any `trail` headers on top of the snapshot `snap`
|
2021-07-21 13:31:52 +00:00
|
|
|
if d.subChn.first < d.subChn.top:
|
|
|
|
block:
|
|
|
|
# clique/clique.go(434): snap, err := snap.apply(headers)
|
|
|
|
d.say "applyTrail ", d.trail.pp(d.subChn)
|
|
|
|
let rc = d.trail.snaps.snapshotApplySeq(
|
|
|
|
d.trail.chain, d.subChn.top-1, d.subChn.first)
|
|
|
|
if rc.isErr:
|
2022-01-12 15:55:36 +00:00
|
|
|
d.say "applyTrail snaps=#", d.trail.snaps.blockNumber,
|
|
|
|
" err=", rc.error.pp
|
2021-07-21 13:31:52 +00:00
|
|
|
return err(rc.error)
|
2021-07-30 14:06:51 +00:00
|
|
|
d.say "applyTrail snaps=#", d.trail.snaps.blockNumber
|
2021-07-14 15:13:27 +00:00
|
|
|
|
2021-07-21 13:31:52 +00:00
|
|
|
# If we've generated a new checkpoint snapshot, save to disk
|
|
|
|
if d.isCheckPoint(d.trail.snaps.blockNumber):
|
2021-07-14 15:13:27 +00:00
|
|
|
|
2022-07-21 18:16:28 +00:00
|
|
|
var rc = d.c.cfg.storeSnapshot(d.trail.snaps)
|
2021-07-21 13:31:52 +00:00
|
|
|
if rc.isErr:
|
|
|
|
return err(rc.error)
|
2021-07-14 15:13:27 +00:00
|
|
|
|
2022-01-12 15:55:36 +00:00
|
|
|
d.say "applyTrail <disk> chechkpoint #", d.trail.snaps.blockNumber
|
2021-07-21 13:31:52 +00:00
|
|
|
trace "Stored voting snapshot to disk",
|
|
|
|
blockNumber = d.trail.snaps.blockNumber,
|
2022-07-21 18:16:28 +00:00
|
|
|
blockHash = d.trail.snaps.blockHash,
|
|
|
|
nSnaps = d.c.cfg.nSnaps,
|
|
|
|
snapsTotal = d.c.cfg.snapsData.toSI
|
2021-07-21 13:31:52 +00:00
|
|
|
ok()
|
2021-07-14 15:13:27 +00:00
|
|
|
|
|
|
|
|
2021-07-21 13:31:52 +00:00
|
|
|
proc updateSnapshot(d: var LocalSnaps): SnapshotResult
|
2022-01-12 15:55:36 +00:00
|
|
|
{.gcsafe, raises: [Defect,CatchableError].} =
|
2021-07-21 13:31:52 +00:00
|
|
|
## Find snapshot for header `d.start.header` and assign it to the LRU cache.
|
|
|
|
## This function was expects thet the LRU cache already has a slot allocated
|
|
|
|
## for the snapshot having run `getLruSnaps()`.
|
2021-07-14 15:13:27 +00:00
|
|
|
|
2021-07-30 14:06:51 +00:00
|
|
|
d.say "updateSnapshot begin ", d.start.header.blockNumber.pp(d.parents)
|
2021-07-14 15:13:27 +00:00
|
|
|
|
|
|
|
# Search for previous snapshots
|
|
|
|
if not d.findSnapshot:
|
2021-07-21 13:31:52 +00:00
|
|
|
return err(d.trail.error)
|
|
|
|
|
|
|
|
# Initialise range for header chain[] to be applied to `d.trail.snaps`
|
|
|
|
d.subChn.top = d.trail.chain.len
|
2021-07-14 15:13:27 +00:00
|
|
|
|
|
|
|
# Previous snapshot found, apply any pending trail headers on top of it
|
2021-07-21 13:31:52 +00:00
|
|
|
if 0 < d.subChn.top:
|
2021-07-14 15:13:27 +00:00
|
|
|
let
|
2021-07-21 13:31:52 +00:00
|
|
|
first = d.trail.chain[^1].blockNumber
|
|
|
|
last = d.trail.chain[0].blockNumber
|
2021-07-14 15:13:27 +00:00
|
|
|
ckpt = d.maxCheckPointLe(last)
|
|
|
|
|
|
|
|
# If there is at least one checkpoint part of the trail sequence, make sure
|
|
|
|
# that we can store the latest one. This will be done by the `applyTrail()`
|
|
|
|
# handler for the largest block number in the sequence (note that the trail
|
|
|
|
# block numbers are in reverse order.)
|
|
|
|
if first <= ckpt and ckpt < last:
|
|
|
|
# Split the trail sequence so that the first one has the checkpoint
|
|
|
|
# entry with largest block number.
|
2021-07-21 13:31:52 +00:00
|
|
|
let inx = (last - ckpt).truncate(int)
|
2021-07-14 15:13:27 +00:00
|
|
|
|
2021-07-21 13:31:52 +00:00
|
|
|
# First part (note reverse block numbers.)
|
|
|
|
d.subChn.first = inx
|
|
|
|
let rc = d.applyTrail
|
2021-07-14 15:13:27 +00:00
|
|
|
if rc.isErr:
|
|
|
|
return err(rc.error)
|
|
|
|
|
2021-07-21 13:31:52 +00:00
|
|
|
# Second part (note reverse block numbers.)
|
|
|
|
d.subChn.first = 0
|
|
|
|
d.subChn.top = inx
|
|
|
|
|
|
|
|
var rc = d.applyTrail
|
|
|
|
if rc.isErr:
|
|
|
|
return err(rc.error)
|
2021-07-14 15:13:27 +00:00
|
|
|
|
|
|
|
# clique/clique.go(438): c.recents.Add(snap.Hash, snap)
|
2022-07-21 18:16:28 +00:00
|
|
|
discard d.c.recents.lruAppend(
|
|
|
|
d.trail.snaps.blockHash.data, d.trail.snaps, INMEMORY_SNAPSHOTS)
|
2021-07-14 15:13:27 +00:00
|
|
|
|
2021-07-30 14:06:51 +00:00
|
|
|
if 1 < d.trail.chain.len:
|
|
|
|
d.say "updateSnapshot ok #", d.trail.snaps.blockNumber,
|
|
|
|
" trail.len=", d.trail.chain.len
|
|
|
|
|
2021-07-21 13:31:52 +00:00
|
|
|
ok(d.trail.snaps)
|
2021-07-14 15:13:27 +00:00
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public functions
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2022-04-08 04:54:11 +00:00
|
|
|
proc cliqueSnapshotSeq*(c: Clique; header: BlockHeader;
|
|
|
|
parents: var seq[BlockHeader]): SnapshotResult
|
2021-07-21 13:31:52 +00:00
|
|
|
{.gcsafe, raises: [Defect,CatchableError].} =
|
2021-07-14 15:13:27 +00:00
|
|
|
## Create authorisation state snapshot of a given point in the block chain
|
|
|
|
## and store it in the `Clique` descriptor to be retrievable as `c.snapshot`
|
|
|
|
## if successful.
|
|
|
|
##
|
2021-07-21 13:31:52 +00:00
|
|
|
## If the `parents[]` argument list top element (if any) is the same as the
|
|
|
|
## `header` argument, this top element is silently ignored.
|
|
|
|
##
|
|
|
|
## If this function is successful, the compiled `Snapshot` will also be
|
|
|
|
## stored in the `Clique` descriptor which can be retrieved later
|
|
|
|
## via `c.snapshot`.
|
2022-07-21 18:16:28 +00:00
|
|
|
block:
|
|
|
|
let rc = c.recents.lruFetch(header.blockHash.data)
|
|
|
|
if rc.isOk:
|
|
|
|
c.snapshot = rc.value
|
|
|
|
return ok(rc.value)
|
2021-07-21 13:31:52 +00:00
|
|
|
|
|
|
|
# Avoid deep copy, sequence will not be changed by `updateSnapshot()`
|
|
|
|
parents.shallow
|
|
|
|
|
|
|
|
var snaps = LocalSnaps(
|
|
|
|
c: c,
|
|
|
|
parents: parents,
|
|
|
|
start: LocalPivot(
|
|
|
|
header: header,
|
2021-07-30 14:06:51 +00:00
|
|
|
hash: header.blockHash))
|
2021-07-14 15:13:27 +00:00
|
|
|
|
2022-07-21 18:16:28 +00:00
|
|
|
let rc = snaps.updateSnapshot
|
|
|
|
if rc.isOk:
|
|
|
|
c.snapshot = rc.value
|
2021-07-14 15:13:27 +00:00
|
|
|
|
2022-07-21 18:16:28 +00:00
|
|
|
rc
|
2021-07-14 15:13:27 +00:00
|
|
|
|
|
|
|
|
2021-07-21 13:31:52 +00:00
|
|
|
proc cliqueSnapshotSeq*(c: Clique; hash: Hash256;
|
2022-04-08 04:54:11 +00:00
|
|
|
parents: var seq[BlockHeader]): SnapshotResult
|
2021-07-21 13:31:52 +00:00
|
|
|
{.gcsafe,raises: [Defect,CatchableError].} =
|
2021-07-14 15:13:27 +00:00
|
|
|
## Create authorisation state snapshot of a given point in the block chain
|
|
|
|
## and store it in the `Clique` descriptor to be retrievable as `c.snapshot`
|
|
|
|
## if successful.
|
|
|
|
##
|
2021-07-21 13:31:52 +00:00
|
|
|
## If the `parents[]` argument list top element (if any) is the same as the
|
|
|
|
## `header` argument, this top element is silently ignored.
|
|
|
|
##
|
|
|
|
## If this function is successful, the compiled `Snapshot` will also be
|
|
|
|
## stored in the `Clique` descriptor which can be retrieved later
|
|
|
|
## via `c.snapshot`.
|
2022-07-21 18:16:28 +00:00
|
|
|
block:
|
|
|
|
let rc = c.recents.lruFetch(hash.data)
|
|
|
|
if rc.isOk:
|
|
|
|
c.snapshot = rc.value
|
|
|
|
return ok(rc.value)
|
2021-07-14 15:13:27 +00:00
|
|
|
|
|
|
|
var header: BlockHeader
|
|
|
|
if not c.cfg.db.getBlockHeader(hash, header):
|
2021-07-21 13:31:52 +00:00
|
|
|
return err((errUnknownHash,""))
|
2021-07-14 15:13:27 +00:00
|
|
|
|
2021-07-21 13:31:52 +00:00
|
|
|
# Avoid deep copy, sequence will not be changed by `updateSnapshot()`
|
|
|
|
parents.shallow
|
|
|
|
|
|
|
|
var snaps = LocalSnaps(
|
|
|
|
c: c,
|
|
|
|
parents: parents,
|
|
|
|
start: LocalPivot(
|
|
|
|
header: header,
|
|
|
|
hash: hash))
|
|
|
|
|
2022-07-21 18:16:28 +00:00
|
|
|
let rc = snaps.updateSnapshot
|
|
|
|
if rc.isOk:
|
|
|
|
c.snapshot = rc.value
|
2021-07-21 13:31:52 +00:00
|
|
|
|
2022-07-21 18:16:28 +00:00
|
|
|
rc
|
2021-07-14 15:13:27 +00:00
|
|
|
|
|
|
|
|
2021-07-21 13:31:52 +00:00
|
|
|
# clique/clique.go(369): func (c *Clique) snapshot(chain [..]
|
2022-04-08 04:54:11 +00:00
|
|
|
proc cliqueSnapshot*(c: Clique; header: BlockHeader;
|
|
|
|
parents: var seq[BlockHeader]): SnapshotResult
|
2021-07-21 13:31:52 +00:00
|
|
|
{.gcsafe, raises: [Defect,CatchableError].} =
|
|
|
|
var list = toSeq(parents)
|
|
|
|
c.cliqueSnapshotSeq(header,list)
|
|
|
|
|
|
|
|
proc cliqueSnapshot*(c: Clique;hash: Hash256;
|
2022-04-08 04:54:11 +00:00
|
|
|
parents: openArray[BlockHeader]): SnapshotResult
|
2021-07-21 13:31:52 +00:00
|
|
|
{.gcsafe, raises: [Defect,CatchableError].} =
|
|
|
|
var list = toSeq(parents)
|
|
|
|
c.cliqueSnapshotSeq(hash,list)
|
|
|
|
|
2022-04-08 04:54:11 +00:00
|
|
|
proc cliqueSnapshot*(c: Clique; header: BlockHeader): SnapshotResult
|
2022-01-12 15:55:36 +00:00
|
|
|
{.gcsafe,raises: [Defect,CatchableError].} =
|
2021-07-21 13:31:52 +00:00
|
|
|
## Short for `cliqueSnapshot(c,header,@[])`
|
2022-04-08 04:54:11 +00:00
|
|
|
var blind: seq[BlockHeader]
|
2021-07-21 13:31:52 +00:00
|
|
|
c.cliqueSnapshotSeq(header, blind)
|
2021-07-14 15:13:27 +00:00
|
|
|
|
2021-07-21 13:31:52 +00:00
|
|
|
proc cliqueSnapshot*(c: Clique; hash: Hash256): SnapshotResult
|
2021-07-14 15:13:27 +00:00
|
|
|
{.gcsafe,raises: [Defect,CatchableError].} =
|
|
|
|
## Short for `cliqueSnapshot(c,hash,@[])`
|
2022-04-08 04:54:11 +00:00
|
|
|
var blind: seq[BlockHeader]
|
2021-07-21 13:31:52 +00:00
|
|
|
c.cliqueSnapshot(hash, blind)
|
2021-07-14 15:13:27 +00:00
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# End
|
|
|
|
# ------------------------------------------------------------------------------
|