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
|
2021-06-14 18:33:57 +00:00
|
|
|
std/[random, sequtils, strutils, times],
|
2021-05-28 17:39:55 +00:00
|
|
|
../../db/db_chain,
|
2021-07-27 11:28:05 +00:00
|
|
|
../../utils/ec_recover,
|
2021-05-28 17:39:55 +00:00
|
|
|
./clique_defs,
|
2021-06-04 17:20:37 +00:00
|
|
|
eth/common,
|
|
|
|
ethash,
|
2021-06-11 17:26:08 +00:00
|
|
|
stew/results,
|
2021-06-14 18:33:57 +00:00
|
|
|
stint
|
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-06-14 18:33:57 +00:00
|
|
|
PrettyPrintDefect* = object of Defect
|
|
|
|
## Defect raised with `pp()` problems, should be used for debugging only
|
|
|
|
|
|
|
|
PrettyPrinters* = object ## Set of pretty printers for debugging
|
2021-06-04 17:20:37 +00:00
|
|
|
nonce*: proc(v: BlockNonce):
|
|
|
|
string {.gcsafe,raises: [Defect,CatchableError].}
|
|
|
|
address*: proc(v: EthAddress):
|
|
|
|
string {.gcsafe,raises: [Defect,CatchableError].}
|
|
|
|
extraData*: proc(v: Blob):
|
|
|
|
string {.gcsafe,raises: [Defect,CatchableError].}
|
|
|
|
blockHeader*: proc(v: BlockHeader; delim: string):
|
|
|
|
string {.gcsafe,raises: [Defect,CatchableError].}
|
|
|
|
|
2021-07-14 15:13:27 +00:00
|
|
|
CliqueCfg* = ref object of RootRef
|
|
|
|
db*: BaseChainDB ##\
|
|
|
|
## All purpose (incl. blockchain) database.
|
|
|
|
|
|
|
|
period: Duration ##\
|
|
|
|
## Time between blocks to enforce.
|
|
|
|
|
|
|
|
ckpInterval: int ##\
|
|
|
|
## Number of blocks after which to save the vote snapshot to the
|
|
|
|
## database.
|
|
|
|
|
|
|
|
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-07-14 15:13:27 +00:00
|
|
|
debug*: bool ##\
|
|
|
|
## Debug mode flag
|
|
|
|
|
|
|
|
prettyPrint*: PrettyPrinters ##\
|
|
|
|
## debugging support
|
2021-05-28 17:39:55 +00:00
|
|
|
|
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(),
|
2021-06-04 17:20:37 +00:00
|
|
|
prng: initRand(prngSeed),
|
|
|
|
prettyPrint: PrettyPrinters(
|
|
|
|
nonce: proc(v:BlockNonce): string = $v,
|
|
|
|
address: proc(v:EthAddress): string = $v,
|
|
|
|
extraData: proc(v:Blob): string = $v,
|
|
|
|
blockHeader: proc(v:BlockHeader; delim:string): string = $v))
|
|
|
|
|
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
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
proc `epoch=`*(cfg: CliqueCfg; epoch: SomeInteger) {.inline.} =
|
|
|
|
## Setter
|
|
|
|
cfg.epoch = if 0 < epoch: epoch
|
|
|
|
else: EPOCH_LENGTH
|
|
|
|
|
|
|
|
proc `period=`*(cfg: CliqueCfg; period: Duration) {.inline.} =
|
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
|
|
|
|
2021-07-14 15:13:27 +00:00
|
|
|
proc `ckpInterval=`*(cfg: CliqueCfg; numBlocks: SomeInteger) {.inline.} =
|
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
|
|
|
|
|
|
|
|
proc `roThreshold=`*(cfg: CliqueCfg; numBlocks: SomeInteger) {.inline.} =
|
|
|
|
## Setter
|
|
|
|
cfg.roThreshold = if 0 < numBlocks: numBlocks
|
|
|
|
else: FULL_IMMUTABILITY_THRESHOLD
|
|
|
|
|
2021-07-21 13:31:52 +00:00
|
|
|
proc `logInterval=`*(cfg: CliqueCfg; duration: Duration) {.inline.} =
|
|
|
|
## 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
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
proc epoch*(cfg: CliqueCfg): auto {.inline.} =
|
|
|
|
## Getter
|
|
|
|
cfg.epoch.u256
|
|
|
|
|
|
|
|
proc period*(cfg: CliqueCfg): auto {.inline.} =
|
|
|
|
## Getter
|
|
|
|
cfg.period
|
|
|
|
|
|
|
|
proc ckpInterval*(cfg: CliqueCfg): auto {.inline.} =
|
|
|
|
## Getter
|
|
|
|
cfg.ckpInterval.u256
|
|
|
|
|
|
|
|
proc roThreshold*(cfg: CliqueCfg): auto {.inline.} =
|
|
|
|
## Getter
|
|
|
|
cfg.roThreshold
|
2021-06-14 18:33:57 +00:00
|
|
|
|
2021-07-21 13:31:52 +00:00
|
|
|
proc logInterval*(cfg: CliqueCfg): auto {.inline.} =
|
|
|
|
## Getter
|
|
|
|
cfg.logInterval
|
|
|
|
|
2021-06-04 17:20:37 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Debugging
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2021-06-14 18:33:57 +00:00
|
|
|
template ppExceptionWrap*(body: untyped) =
|
|
|
|
## Exception relay to `PrettyPrintDefect`, intended to be used with `pp()`
|
|
|
|
## related functions.
|
|
|
|
try:
|
|
|
|
body
|
|
|
|
except:
|
|
|
|
raise (ref PrettyPrintDefect)(msg: getCurrentException().msg)
|
|
|
|
|
2021-07-14 15:13:27 +00:00
|
|
|
proc say*(cfg: CliqueCfg; v: varargs[string,`$`]) {.inline.} =
|
|
|
|
## Debugging output
|
|
|
|
ppExceptionWrap:
|
|
|
|
if cfg.debug: stderr.write "*** " & v.join & "\n"
|
|
|
|
|
2021-06-14 18:33:57 +00:00
|
|
|
|
2021-06-11 17:26:08 +00:00
|
|
|
proc pp*(v: CliqueError): string =
|
|
|
|
## Pretty print error
|
|
|
|
result = $v[0]
|
|
|
|
if v[1] != "":
|
|
|
|
result &= " => " & v[1]
|
|
|
|
|
2021-07-14 15:13:27 +00:00
|
|
|
proc pp*(v: CliqueOkResult): string =
|
2021-06-11 17:26:08 +00:00
|
|
|
## Pretty print result
|
|
|
|
if v.isOk:
|
|
|
|
"OK"
|
|
|
|
else:
|
|
|
|
v.error.pp
|
|
|
|
|
2021-06-14 18:33:57 +00:00
|
|
|
|
2021-06-04 17:20:37 +00:00
|
|
|
proc pp*(p: var PrettyPrinters; v: BlockNonce): string =
|
2021-06-14 18:33:57 +00:00
|
|
|
## Pretty print nonce (for debugging)
|
|
|
|
ppExceptionWrap: p.nonce(v)
|
2021-06-04 17:20:37 +00:00
|
|
|
|
|
|
|
proc pp*(p: var PrettyPrinters; v: EthAddress): string =
|
2021-06-14 18:33:57 +00:00
|
|
|
## Pretty print address (for debugging)
|
|
|
|
ppExceptionWrap: p.address(v)
|
2021-06-04 17:20:37 +00:00
|
|
|
|
|
|
|
proc pp*(p: var PrettyPrinters; v: openArray[EthAddress]): seq[string] =
|
|
|
|
## Pretty print address list
|
|
|
|
toSeq(v).mapIt(p.pp(it))
|
|
|
|
|
|
|
|
proc pp*(p: var PrettyPrinters; v: Blob): string =
|
|
|
|
## Visualise `extraData` field
|
2021-06-14 18:33:57 +00:00
|
|
|
ppExceptionWrap: p.extraData(v)
|
2021-06-04 17:20:37 +00:00
|
|
|
|
|
|
|
proc pp*(p: var PrettyPrinters; v: BlockHeader; delim: string): string =
|
|
|
|
## Pretty print block header
|
2021-06-14 18:33:57 +00:00
|
|
|
ppExceptionWrap: p.blockHeader(v, delim)
|
2021-06-04 17:20:37 +00:00
|
|
|
|
|
|
|
proc pp*(p: var PrettyPrinters; v: BlockHeader; indent = 3): string =
|
|
|
|
## Pretty print block header, NL delimited, indented fields
|
|
|
|
let delim = if 0 < indent: "\n" & ' '.repeat(indent) else: " "
|
|
|
|
p.pp(v,delim)
|
|
|
|
|
|
|
|
proc pp*(p: var PrettyPrinters; v: openArray[BlockHeader]): seq[string] =
|
|
|
|
## Pretty print list of block headers
|
|
|
|
toSeq(v).mapIt(p.pp(it,","))
|
|
|
|
|
|
|
|
|
|
|
|
proc pp*[T;V: SimpleTypePP](t: T; v: V): string =
|
2021-06-14 18:33:57 +00:00
|
|
|
## Generic pretty printer, requires `getPrettyPrinters()` function:
|
2021-06-04 17:20:37 +00:00
|
|
|
## ::
|
|
|
|
## proc getPrettyPrinters(t: SomeLocalType): var PrettyPrinters
|
|
|
|
##
|
|
|
|
mixin getPrettyPrinters
|
2021-06-14 18:33:57 +00:00
|
|
|
ppExceptionWrap: t.getPrettyPrinters.pp(v)
|
2021-06-04 17:20:37 +00:00
|
|
|
|
|
|
|
proc pp*[T;V: var SimpleTypePP](t: var T; v: V): string =
|
2021-06-14 18:33:57 +00:00
|
|
|
## Generic pretty printer, requires `getPrettyPrinters()` function:
|
2021-06-04 17:20:37 +00:00
|
|
|
## ::
|
|
|
|
## proc getPrettyPrinters(t: var SomeLocalType): var PrettyPrinters
|
|
|
|
##
|
|
|
|
mixin getPrettyPrinters
|
2021-06-14 18:33:57 +00:00
|
|
|
ppExceptionWrap: t.getPrettyPrinters.pp(v)
|
2021-06-04 17:20:37 +00:00
|
|
|
|
|
|
|
|
|
|
|
proc pp*[T;V: SeqTypePP](t: T; v: openArray[V]): seq[string] =
|
2021-06-14 18:33:57 +00:00
|
|
|
## Generic pretty printer, requires `getPrettyPrinters()` function:
|
2021-06-04 17:20:37 +00:00
|
|
|
## ::
|
|
|
|
## proc getPrettyPrinters(t: SomeLocalType): var PrettyPrinters
|
|
|
|
##
|
|
|
|
mixin getPrettyPrinters
|
2021-06-14 18:33:57 +00:00
|
|
|
ppExceptionWrap: t.getPrettyPrinters.pp(v)
|
2021-06-04 17:20:37 +00:00
|
|
|
|
|
|
|
proc pp*[T;V: SeqTypePP](t: var T; v: openArray[V]): seq[string] =
|
2021-06-14 18:33:57 +00:00
|
|
|
## Generic pretty printer, requires `getPrettyPrinters()` function:
|
2021-06-04 17:20:37 +00:00
|
|
|
## ::
|
|
|
|
## proc getPrettyPrinters(t: var SomeLocalType): var PrettyPrinters
|
|
|
|
##
|
|
|
|
mixin getPrettyPrinters
|
2021-06-14 18:33:57 +00:00
|
|
|
ppExceptionWrap: t.getPrettyPrinters.pp(v)
|
2021-06-04 17:20:37 +00:00
|
|
|
|
|
|
|
|
|
|
|
proc pp*[T;X: int|string](t: T; v: BlockHeader; sep: X): string =
|
2021-06-14 18:33:57 +00:00
|
|
|
## Generic pretty printer, requires `getPrettyPrinters()` function:
|
2021-06-04 17:20:37 +00:00
|
|
|
## ::
|
|
|
|
## proc getPrettyPrinters(t: SomeLocalType): var PrettyPrinters
|
|
|
|
##
|
|
|
|
mixin getPrettyPrinters
|
2021-06-14 18:33:57 +00:00
|
|
|
ppExceptionWrap: t.getPrettyPrinters.pp(v,sep)
|
2021-06-04 17:20:37 +00:00
|
|
|
|
|
|
|
proc pp*[T;X: int|string](t: var T; v: BlockHeader; sep: X): string =
|
2021-06-14 18:33:57 +00:00
|
|
|
## Generic pretty printer, requires `getPrettyPrinters()` function:
|
2021-06-04 17:20:37 +00:00
|
|
|
## ::
|
|
|
|
## proc getPrettyPrinters(t: var SomeLocalType): var PrettyPrinters
|
|
|
|
##
|
|
|
|
mixin getPrettyPrinters
|
2021-06-14 18:33:57 +00:00
|
|
|
ppExceptionWrap: t.getPrettyPrinters.pp(v,sep)
|
2021-05-28 17:39:55 +00:00
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# End
|
|
|
|
# ------------------------------------------------------------------------------
|