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 Structure 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/[tables],
|
2021-07-14 15:13:27 +00:00
|
|
|
../../../db/storage_types,
|
|
|
|
../clique_cfg,
|
|
|
|
../clique_defs,
|
2021-07-21 13:31:52 +00:00
|
|
|
../clique_helpers,
|
2021-07-14 15:13:27 +00:00
|
|
|
./ballot,
|
|
|
|
chronicles,
|
2022-12-02 04:35:41 +00:00
|
|
|
eth/[rlp],
|
2021-07-14 15:13:27 +00:00
|
|
|
stew/results
|
|
|
|
|
2022-07-28 19:34:38 +00:00
|
|
|
export tables
|
|
|
|
|
2021-07-14 15:13:27 +00:00
|
|
|
type
|
2021-07-21 13:31:52 +00:00
|
|
|
SnapshotResult* = ##\
|
|
|
|
## Snapshot/error result type
|
|
|
|
Result[Snapshot,CliqueError]
|
|
|
|
|
2021-07-30 14:06:51 +00:00
|
|
|
AddressHistory* = Table[BlockNumber,EthAddress]
|
2021-07-14 15:13:27 +00:00
|
|
|
|
|
|
|
SnapshotData* = object
|
|
|
|
blockNumber: BlockNumber ## block number where snapshot was created on
|
|
|
|
blockHash: Hash256 ## block hash where snapshot was created on
|
|
|
|
recents: AddressHistory ## recent signers for spam protections
|
|
|
|
|
|
|
|
# clique/snapshot.go(58): Recents map[uint64]common.Address [..]
|
|
|
|
ballot: Ballot ## Votes => authorised signers
|
|
|
|
|
|
|
|
# clique/snapshot.go(50): type Snapshot struct [..]
|
2021-07-21 13:31:52 +00:00
|
|
|
Snapshot* = ref object ## Snapshot is the state of the authorization
|
2021-07-14 15:13:27 +00:00
|
|
|
## voting at a given point in time.
|
|
|
|
cfg: CliqueCfg ## parameters to fine tune behavior
|
|
|
|
data*: SnapshotData ## real snapshot
|
|
|
|
|
2023-01-30 22:10:23 +00:00
|
|
|
{.push raises: [].}
|
2021-07-14 15:13:27 +00:00
|
|
|
|
|
|
|
logScope:
|
|
|
|
topics = "clique PoA snapshot"
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Private functions needed to support RLP conversion
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2022-01-12 15:55:36 +00:00
|
|
|
proc append[K,V](rw: var RlpWriter; tab: Table[K,V]) =
|
2021-07-14 15:13:27 +00:00
|
|
|
rw.startList(tab.len)
|
|
|
|
for key,value in tab.pairs:
|
|
|
|
rw.append((key,value))
|
|
|
|
|
|
|
|
proc read[K,V](rlp: var Rlp;
|
2023-01-30 22:10:23 +00:00
|
|
|
Q: type Table[K,V]): Q {.gcsafe, raises: [CatchableError].} =
|
2021-07-14 15:13:27 +00:00
|
|
|
for w in rlp.items:
|
|
|
|
let (key,value) = w.read((K,V))
|
|
|
|
result[key] = value
|
|
|
|
|
2021-07-21 13:31:52 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Private constructor helper
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
# clique/snapshot.go(72): func newSnapshot(config [..]
|
|
|
|
proc initSnapshot(s: Snapshot; cfg: CliqueCfg;
|
|
|
|
number: BlockNumber; hash: Hash256; signers: openArray[EthAddress]) =
|
|
|
|
## Initalise a new snapshot.
|
|
|
|
s.cfg = cfg
|
|
|
|
s.data.blockNumber = number
|
|
|
|
s.data.blockHash = hash
|
|
|
|
s.data.recents = initTable[BlockNumber,EthAddress]()
|
|
|
|
s.data.ballot.initBallot(signers)
|
2021-07-14 15:13:27 +00:00
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public Constructor
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2021-07-21 13:31:52 +00:00
|
|
|
proc newSnapshot*(cfg: CliqueCfg; header: BlockHeader): Snapshot =
|
2021-07-14 15:13:27 +00:00
|
|
|
## Create a new snapshot for the given header. The header need not be on the
|
|
|
|
## block chain, yet. The trusted signer list is derived from the
|
|
|
|
## `extra data` field of the header.
|
2021-07-21 13:31:52 +00:00
|
|
|
new result
|
2021-07-14 15:13:27 +00:00
|
|
|
let signers = header.extraData.extraDataAddresses
|
2021-07-30 14:06:51 +00:00
|
|
|
result.initSnapshot(cfg, header.blockNumber, header.blockHash, signers)
|
2021-07-14 15:13:27 +00:00
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public getters
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2022-01-12 15:55:36 +00:00
|
|
|
proc cfg*(s: Snapshot): CliqueCfg =
|
2021-07-14 15:13:27 +00:00
|
|
|
## Getter
|
|
|
|
s.cfg
|
|
|
|
|
2022-01-12 15:55:36 +00:00
|
|
|
proc blockNumber*(s: Snapshot): BlockNumber =
|
2021-07-14 15:13:27 +00:00
|
|
|
## Getter
|
|
|
|
s.data.blockNumber
|
|
|
|
|
2022-01-12 15:55:36 +00:00
|
|
|
proc blockHash*(s: Snapshot): Hash256 =
|
2021-07-14 15:13:27 +00:00
|
|
|
## Getter
|
|
|
|
s.data.blockHash
|
|
|
|
|
2022-01-12 15:55:36 +00:00
|
|
|
proc recents*(s: Snapshot): var AddressHistory =
|
2021-07-14 15:13:27 +00:00
|
|
|
## Retrieves the list of recently added addresses
|
|
|
|
s.data.recents
|
|
|
|
|
2022-01-12 15:55:36 +00:00
|
|
|
proc ballot*(s: Snapshot): var Ballot =
|
2021-07-14 15:13:27 +00:00
|
|
|
## Retrieves the ballot box descriptor with the votes
|
|
|
|
s.data.ballot
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public setters
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2022-01-12 15:55:36 +00:00
|
|
|
proc `blockNumber=`*(s: Snapshot; number: BlockNumber) =
|
2021-07-14 15:13:27 +00:00
|
|
|
## Getter
|
|
|
|
s.data.blockNumber = number
|
|
|
|
|
2022-01-12 15:55:36 +00:00
|
|
|
proc `blockHash=`*(s: Snapshot; hash: Hash256) =
|
2021-07-14 15:13:27 +00:00
|
|
|
## Getter
|
|
|
|
s.data.blockHash = hash
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public load/store support
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
# clique/snapshot.go(88): func loadSnapshot(config [..]
|
2021-07-21 13:31:52 +00:00
|
|
|
proc loadSnapshot*(cfg: CliqueCfg; hash: Hash256):
|
2022-07-21 18:16:28 +00:00
|
|
|
Result[Snapshot,CliqueError] =
|
2021-07-14 15:13:27 +00:00
|
|
|
## Load an existing snapshot from the database.
|
2021-07-21 13:31:52 +00:00
|
|
|
var s = Snapshot(cfg: cfg)
|
2021-07-14 15:13:27 +00:00
|
|
|
try:
|
2023-08-04 11:10:09 +00:00
|
|
|
let db = s.cfg.db.kvt
|
2022-12-02 04:35:41 +00:00
|
|
|
let rlpData = db.get(hash.cliqueSnapshotKey.toOpenArray)
|
2022-01-12 15:55:36 +00:00
|
|
|
|
|
|
|
# The following check is only needed for Github/CI for 64bit Windows (not
|
|
|
|
# reproducible on my local Win7 -- jordan). What normally happens when
|
|
|
|
# `rlpData == @[]` holds is that the library function `eth.readImpl()`
|
|
|
|
# throws an `RlpTypeMismatch` error as the inner function `isList()` fails.
|
|
|
|
# On Github CI for 64bit Windows, the unit test crashes with a segmentation
|
|
|
|
# fault.
|
|
|
|
if rlpData.len == 0:
|
|
|
|
return err((errSnapshotLoad,""))
|
|
|
|
|
|
|
|
s.data = rlpData.decode(SnapshotData)
|
2021-07-14 15:13:27 +00:00
|
|
|
except CatchableError as e:
|
2022-01-12 15:55:36 +00:00
|
|
|
return err((errSnapshotLoad, $e.name & ": " & e.msg))
|
|
|
|
ok(s)
|
2021-07-14 15:13:27 +00:00
|
|
|
|
|
|
|
# clique/snapshot.go(104): func (s *Snapshot) store(db [..]
|
2022-07-21 18:16:28 +00:00
|
|
|
proc storeSnapshot*(cfg: CliqueCfg; s: Snapshot): CliqueOkResult =
|
2021-07-14 15:13:27 +00:00
|
|
|
## Insert the snapshot into the database.
|
|
|
|
try:
|
2022-07-21 18:16:28 +00:00
|
|
|
let
|
|
|
|
key = s.data.blockHash.cliqueSnapshotKey
|
|
|
|
val = rlp.encode(s.data)
|
2023-08-04 11:10:09 +00:00
|
|
|
db = s.cfg.db.kvt
|
2022-12-02 04:35:41 +00:00
|
|
|
db.put(key.toOpenArray, val)
|
2022-07-21 18:16:28 +00:00
|
|
|
|
|
|
|
cfg.nSnaps.inc
|
|
|
|
cfg.snapsData += val.len.uint
|
2021-07-14 15:13:27 +00:00
|
|
|
except CatchableError as e:
|
2022-01-12 15:55:36 +00:00
|
|
|
return err((errSnapshotStore, $e.name & ": " & e.msg))
|
2022-07-21 18:16:28 +00:00
|
|
|
|
2022-01-12 15:55:36 +00:00
|
|
|
ok()
|
2021-07-14 15:13:27 +00:00
|
|
|
|
2021-07-21 13:31:52 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public deep copy
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2022-01-12 15:55:36 +00:00
|
|
|
proc cloneSnapshot*(s: Snapshot): Snapshot =
|
2021-07-21 13:31:52 +00:00
|
|
|
## Clone the snapshot
|
|
|
|
Snapshot(
|
|
|
|
cfg: s.cfg, # copy ref
|
|
|
|
data: s.data) # copy data
|
|
|
|
|
2021-07-14 15:13:27 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# End
|
|
|
|
# ------------------------------------------------------------------------------
|