2021-05-28 17:39:55 +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.
|
|
|
|
|
|
|
|
##
|
|
|
|
## Clique PoA Conmmon Config
|
|
|
|
## =========================
|
|
|
|
##
|
|
|
|
## Constants used by Clique proof-of-authority consensus protocol, 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/[random, times],
|
2021-06-04 17:20:37 +00:00
|
|
|
eth/common,
|
|
|
|
ethash,
|
2021-06-11 17:26:08 +00:00
|
|
|
stew/results,
|
2022-07-21 18:16:28 +00:00
|
|
|
stint,
|
|
|
|
../../db/db_chain,
|
|
|
|
../../utils/ec_recover,
|
|
|
|
./clique_defs
|
2021-05-28 17:39:55 +00:00
|
|
|
|
|
|
|
const
|
|
|
|
prngSeed = 42
|
|
|
|
|
|
|
|
type
|
2021-06-04 17:20:37 +00:00
|
|
|
SimpleTypePP = BlockNonce|EthAddress|Blob|BlockHeader
|
|
|
|
SeqTypePP = EthAddress|BlockHeader
|
|
|
|
|
2021-07-14 15:13:27 +00:00
|
|
|
CliqueCfg* = ref object of RootRef
|
|
|
|
db*: BaseChainDB ##\
|
|
|
|
## All purpose (incl. blockchain) database.
|
|
|
|
|
2022-07-21 18:16:28 +00:00
|
|
|
nSnaps*: uint64 ##\
|
|
|
|
## Number of snapshots stored on disk (for logging troublesshoting)
|
|
|
|
|
|
|
|
snapsData*: uint64 ##\
|
|
|
|
## Raw payload stored on disk (for logging troublesshoting)
|
|
|
|
|
2021-07-14 15:13:27 +00:00
|
|
|
period: Duration ##\
|
|
|
|
## Time between blocks to enforce.
|
|
|
|
|
|
|
|
ckpInterval: int ##\
|
|
|
|
## Number of blocks after which to save the vote snapshot to the
|
2022-07-21 18:16:28 +00:00
|
|
|
## disk database.
|
2021-07-14 15:13:27 +00:00
|
|
|
|
|
|
|
roThreshold: int ##\
|
|
|
|
## Number of blocks after which a chain segment is considered immutable
|
|
|
|
## (ie. soft finality). It is used by the downloader as a hard limit
|
|
|
|
## against deep ancestors, by the blockchain against deep reorgs, by the
|
|
|
|
## freezer as the cutoff threshold and by clique as the snapshot trust
|
|
|
|
## limit.
|
|
|
|
|
|
|
|
prng: Rand ##\
|
|
|
|
## PRNG state for internal random generator. This PRNG is
|
|
|
|
## cryptographically insecure but with reproducible data stream.
|
|
|
|
|
|
|
|
signatures: EcRecover ##\
|
|
|
|
## Recent block signatures cached to speed up mining.
|
|
|
|
|
|
|
|
epoch: int ##\
|
|
|
|
## The number of blocks after which to checkpoint and reset the pending
|
|
|
|
## votes.Suggested 30000 for the testnet to remain analogous to the
|
|
|
|
## mainnet ethash epoch.
|
|
|
|
|
2021-07-21 13:31:52 +00:00
|
|
|
logInterval: Duration ##\
|
|
|
|
## Time interval after which the `snapshotApply()` function main loop
|
|
|
|
## produces logging entries.
|
|
|
|
|
2021-06-14 18:33:57 +00:00
|
|
|
{.push raises: [Defect].}
|
2021-05-28 17:39:55 +00:00
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
2021-07-14 15:13:27 +00:00
|
|
|
# Public constructor
|
2021-05-28 17:39:55 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2021-07-14 15:13:27 +00:00
|
|
|
proc newCliqueCfg*(db: BaseChainDB): CliqueCfg =
|
|
|
|
result = CliqueCfg(
|
2021-07-06 13:14:45 +00:00
|
|
|
db: db,
|
2021-07-14 15:13:27 +00:00
|
|
|
epoch: EPOCH_LENGTH,
|
|
|
|
period: BLOCK_PERIOD,
|
|
|
|
ckpInterval: CHECKPOINT_INTERVAL,
|
|
|
|
roThreshold: FULL_IMMUTABILITY_THRESHOLD,
|
2021-07-21 13:31:52 +00:00
|
|
|
logInterval: SNAPS_LOG_INTERVAL_MICSECS,
|
2022-01-18 13:05:00 +00:00
|
|
|
signatures: EcRecover.init(),
|
2022-07-21 18:16:28 +00:00
|
|
|
prng: initRand(prngSeed))
|
2021-06-04 17:20:37 +00:00
|
|
|
|
2021-07-14 15:13:27 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public helper funcion
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2021-07-21 13:31:52 +00:00
|
|
|
# clique/clique.go(145): func ecrecover(header [..]
|
2021-07-14 15:13:27 +00:00
|
|
|
proc ecRecover*(cfg: CliqueCfg; header: BlockHeader): auto
|
2021-07-21 13:31:52 +00:00
|
|
|
{.gcsafe, raises: [Defect,CatchableError].} =
|
2021-07-27 11:28:05 +00:00
|
|
|
cfg.signatures.ecRecover(header)
|
2021-06-14 18:33:57 +00:00
|
|
|
|
2021-07-14 15:13:27 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public setters
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2022-07-21 18:16:28 +00:00
|
|
|
proc `epoch=`*(cfg: CliqueCfg; epoch: SomeInteger) =
|
2021-07-14 15:13:27 +00:00
|
|
|
## Setter
|
|
|
|
cfg.epoch = if 0 < epoch: epoch
|
|
|
|
else: EPOCH_LENGTH
|
|
|
|
|
2022-07-21 18:16:28 +00:00
|
|
|
proc `period=`*(cfg: CliqueCfg; period: Duration) =
|
2021-06-14 18:33:57 +00:00
|
|
|
## Setter
|
2021-07-14 15:13:27 +00:00
|
|
|
cfg.period = if period != Duration(): period
|
|
|
|
else: BLOCK_PERIOD
|
2021-06-14 18:33:57 +00:00
|
|
|
|
2022-07-21 18:16:28 +00:00
|
|
|
proc `ckpInterval=`*(cfg: CliqueCfg; numBlocks: SomeInteger) =
|
2021-06-14 18:33:57 +00:00
|
|
|
## Setter
|
2021-07-14 15:13:27 +00:00
|
|
|
cfg.ckpInterval = if 0 < numBlocks: numBlocks
|
|
|
|
else: CHECKPOINT_INTERVAL
|
|
|
|
|
2022-07-21 18:16:28 +00:00
|
|
|
proc `roThreshold=`*(cfg: CliqueCfg; numBlocks: SomeInteger) =
|
2021-07-14 15:13:27 +00:00
|
|
|
## Setter
|
|
|
|
cfg.roThreshold = if 0 < numBlocks: numBlocks
|
|
|
|
else: FULL_IMMUTABILITY_THRESHOLD
|
|
|
|
|
2022-07-21 18:16:28 +00:00
|
|
|
proc `logInterval=`*(cfg: CliqueCfg; duration: Duration) =
|
2021-07-21 13:31:52 +00:00
|
|
|
## Setter
|
|
|
|
cfg.logInterval = if duration != Duration(): duration
|
|
|
|
else: SNAPS_LOG_INTERVAL_MICSECS
|
|
|
|
|
2021-07-14 15:13:27 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public PRNG, may be overloaded
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
method rand*(cfg: CliqueCfg; max: Natural): int {.gcsafe,base.} =
|
|
|
|
## The method returns a random number base on an internal PRNG providing a
|
|
|
|
## reproducible stream of random data. This function is supposed to be used
|
|
|
|
## exactly when repeatability comes in handy. Never to be used for crypto key
|
|
|
|
## generation or like (except testing.)
|
|
|
|
cfg.prng.rand(max)
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public getter
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2022-07-21 18:16:28 +00:00
|
|
|
proc epoch*(cfg: CliqueCfg): BlockNumber =
|
2021-07-14 15:13:27 +00:00
|
|
|
## Getter
|
|
|
|
cfg.epoch.u256
|
|
|
|
|
2022-07-21 18:16:28 +00:00
|
|
|
proc period*(cfg: CliqueCfg): Duration =
|
2021-07-14 15:13:27 +00:00
|
|
|
## Getter
|
|
|
|
cfg.period
|
|
|
|
|
2022-07-21 18:16:28 +00:00
|
|
|
proc ckpInterval*(cfg: CliqueCfg): BlockNumber =
|
2021-07-14 15:13:27 +00:00
|
|
|
## Getter
|
|
|
|
cfg.ckpInterval.u256
|
|
|
|
|
2022-07-21 18:16:28 +00:00
|
|
|
proc roThreshold*(cfg: CliqueCfg): int =
|
2021-07-14 15:13:27 +00:00
|
|
|
## Getter
|
|
|
|
cfg.roThreshold
|
2021-06-14 18:33:57 +00:00
|
|
|
|
2022-07-21 18:16:28 +00:00
|
|
|
proc logInterval*(cfg: CliqueCfg): Duration =
|
2021-07-21 13:31:52 +00:00
|
|
|
## Getter
|
|
|
|
cfg.logInterval
|
|
|
|
|
2021-05-28 17:39:55 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# End
|
|
|
|
# ------------------------------------------------------------------------------
|