2021-07-06 13:14:45 +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.
|
|
|
|
|
|
|
|
##
|
|
|
|
## Descriptor Objects 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
|
|
|
|
std/[tables],
|
|
|
|
../../db/db_chain,
|
|
|
|
../../constants,
|
|
|
|
./clique_cfg,
|
|
|
|
./clique_defs,
|
2021-07-14 15:13:27 +00:00
|
|
|
./snapshot/[lru_snaps, snapshot_desc],
|
2021-07-21 13:31:52 +00:00
|
|
|
chronicles,
|
|
|
|
eth/[common, keys, rlp],
|
|
|
|
stew/results
|
2021-07-06 13:14:45 +00:00
|
|
|
|
2021-07-30 14:06:51 +00:00
|
|
|
const
|
|
|
|
enableCliqueAsyncLock* = ##\
|
|
|
|
## Async locks are currently unused by `Clique` but were part of the Go
|
|
|
|
## reference implementation. The unused code fragment from the reference
|
|
|
|
## implementation are buried in the file `clique_unused.nim` and not used
|
|
|
|
## otherwise.
|
|
|
|
defined(clique_async_lock)
|
|
|
|
|
|
|
|
when enableCliqueAsyncLock:
|
|
|
|
import chronos
|
|
|
|
|
2021-07-06 13:14:45 +00:00
|
|
|
type
|
|
|
|
# clique/clique.go(142): type SignerFn func(signer [..]
|
2021-07-14 15:13:27 +00:00
|
|
|
CliqueSignerFn* = ## Hashes and signs the data to be signed by
|
|
|
|
## a backing account
|
2021-07-06 13:14:45 +00:00
|
|
|
proc(signer: EthAddress;
|
|
|
|
message: openArray[byte]): Result[Hash256,cstring] {.gcsafe.}
|
|
|
|
|
|
|
|
Proposals = Table[EthAddress,bool]
|
|
|
|
|
2021-07-21 13:31:52 +00:00
|
|
|
CliqueFailed* = ##\
|
|
|
|
## Last failed state: block hash and error result
|
|
|
|
(Hash256, CliqueError)
|
|
|
|
|
2021-07-06 13:14:45 +00:00
|
|
|
# clique/clique.go(172): type Clique struct { [..]
|
2021-07-21 13:31:52 +00:00
|
|
|
Clique* = ref object ##\
|
|
|
|
## Clique is the proof-of-authority consensus engine proposed to support
|
|
|
|
## the Ethereum testnet following the Ropsten attacks.
|
|
|
|
|
2021-07-14 15:13:27 +00:00
|
|
|
signer*: EthAddress ##\
|
|
|
|
## Ethereum address of the current signing key
|
2021-07-06 13:14:45 +00:00
|
|
|
|
2021-07-14 15:13:27 +00:00
|
|
|
signFn*: CliqueSignerFn ## Signer function to authorize hashes with
|
|
|
|
stopSealReq*: bool ## Stop running `seal()` function
|
|
|
|
stopVHeaderReq*: bool ## Stop running `verifyHeader()` function
|
2021-07-06 13:14:45 +00:00
|
|
|
# signatures => see CliqueCfg
|
|
|
|
|
2021-07-14 15:13:27 +00:00
|
|
|
cfg: CliqueCfg ##\
|
|
|
|
## Common engine parameters to fine tune behaviour
|
2021-07-06 13:14:45 +00:00
|
|
|
|
2021-07-14 15:13:27 +00:00
|
|
|
recents: LruSnaps ##\
|
|
|
|
## Snapshots cache for recent block search
|
2021-07-06 13:14:45 +00:00
|
|
|
|
2021-07-14 15:13:27 +00:00
|
|
|
snapshot: Snapshot ##\
|
2021-07-21 13:31:52 +00:00
|
|
|
## Last successful snapshot
|
2021-07-14 15:13:27 +00:00
|
|
|
|
2021-07-21 13:31:52 +00:00
|
|
|
failed: CliqueFailed ##\
|
|
|
|
## Last verification error (if any)
|
2021-07-14 15:13:27 +00:00
|
|
|
|
|
|
|
proposals: Proposals ##\
|
|
|
|
## Cu1rrent list of proposals we are pushing
|
|
|
|
|
2021-07-30 14:06:51 +00:00
|
|
|
applySnapsMinBacklog: bool ##\
|
|
|
|
## Epoch is a restart and sync point. Eip-225 requires that the epoch
|
|
|
|
## header contains the full list of currently authorised signers.
|
|
|
|
##
|
|
|
|
## If this flag is set `true`, then the `cliqueSnapshot()` function will
|
|
|
|
## walk back to the `epoch` header with at least `cfg.roThreshold` blocks
|
|
|
|
## apart from the current header. This is how it is done in the reference
|
|
|
|
## implementation.
|
|
|
|
##
|
|
|
|
## Leving the flag `false`, the assumption is that all the checkponts
|
|
|
|
## before have been vetted already regardless of the current branch. So
|
|
|
|
## the nearest `epoch` header is used.
|
|
|
|
|
|
|
|
when enableCliqueAsyncLock:
|
|
|
|
asyncLock: AsyncLock ##\
|
|
|
|
## Protects the signer fields
|
2021-07-06 13:14:45 +00:00
|
|
|
|
|
|
|
{.push raises: [Defect].}
|
|
|
|
|
2021-07-21 13:31:52 +00:00
|
|
|
logScope:
|
|
|
|
topics = "clique PoA constructor"
|
|
|
|
|
2021-07-06 13:14:45 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public constructor
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
# clique/clique.go(191): func New(config [..]
|
|
|
|
proc newClique*(cfg: CliqueCfg): Clique =
|
|
|
|
## Initialiser for Clique proof-of-authority consensus engine with the
|
|
|
|
## initial signers set to the ones provided by the user.
|
2021-07-30 14:06:51 +00:00
|
|
|
result = Clique(cfg: cfg,
|
|
|
|
recents: cfg.initLruSnaps,
|
|
|
|
snapshot: cfg.newSnapshot(BlockHeader()),
|
|
|
|
proposals: initTable[EthAddress,bool]())
|
|
|
|
when enableCliqueAsyncLock:
|
|
|
|
result.asyncLock = newAsyncLock()
|
2021-07-06 13:14:45 +00:00
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
2021-07-21 13:31:52 +00:00
|
|
|
# Public /pretty print
|
2021-07-06 13:14:45 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2021-07-21 13:31:52 +00:00
|
|
|
# Debugging only
|
2021-07-14 15:13:27 +00:00
|
|
|
proc getPrettyPrinters*(c: Clique): var PrettyPrinters =
|
|
|
|
## Mixin for pretty printers, see `clique/clique_cfg.pp()`
|
|
|
|
c.cfg.prettyPrint
|
2021-07-06 13:14:45 +00:00
|
|
|
|
2021-07-21 13:31:52 +00:00
|
|
|
proc `$`*(e: CliqueError): string =
|
|
|
|
## Join text fragments
|
|
|
|
result = $e[0]
|
|
|
|
if e[1] != "":
|
|
|
|
result &= ": " & e[1]
|
|
|
|
|
2021-07-06 13:14:45 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public getters
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2021-07-14 15:13:27 +00:00
|
|
|
proc recents*(c: Clique): var LruSnaps {.inline.} =
|
2021-07-06 13:14:45 +00:00
|
|
|
## Getter
|
2021-07-14 15:13:27 +00:00
|
|
|
c.recents
|
2021-07-06 13:14:45 +00:00
|
|
|
|
2021-07-14 15:13:27 +00:00
|
|
|
proc proposals*(c: Clique): var Proposals {.inline.} =
|
2021-07-06 13:14:45 +00:00
|
|
|
## Getter
|
2021-07-14 15:13:27 +00:00
|
|
|
c.proposals
|
2021-07-06 13:14:45 +00:00
|
|
|
|
2021-07-21 13:31:52 +00:00
|
|
|
proc snapshot*(c: Clique): auto {.inline.} =
|
|
|
|
## Getter, last successfully processed snapshot.
|
2021-07-14 15:13:27 +00:00
|
|
|
c.snapshot
|
2021-07-06 13:14:45 +00:00
|
|
|
|
2021-07-21 13:31:52 +00:00
|
|
|
proc failed*(c: Clique): auto {.inline.} =
|
|
|
|
## Getter, last snapshot error.
|
|
|
|
c.failed
|
2021-07-14 15:13:27 +00:00
|
|
|
|
|
|
|
proc cfg*(c: Clique): auto {.inline.} =
|
2021-07-06 13:14:45 +00:00
|
|
|
## Getter
|
2021-07-14 15:13:27 +00:00
|
|
|
c.cfg
|
2021-07-06 13:14:45 +00:00
|
|
|
|
2021-07-14 15:13:27 +00:00
|
|
|
proc db*(c: Clique): auto {.inline.} =
|
2021-07-06 13:14:45 +00:00
|
|
|
## Getter
|
2021-07-14 15:13:27 +00:00
|
|
|
c.cfg.db
|
2021-07-06 13:14:45 +00:00
|
|
|
|
2021-07-30 14:06:51 +00:00
|
|
|
proc applySnapsMinBacklog*(c: Clique): auto {.inline.} =
|
|
|
|
## Getter.
|
|
|
|
##
|
|
|
|
## If this flag is set `true`, then the `cliqueSnapshot()` function will
|
|
|
|
## walk back to the `epoch` header with at least `cfg.roThreshold` blocks
|
|
|
|
## apart from the current header. This is how it is done in the reference
|
|
|
|
## implementation.
|
|
|
|
##
|
|
|
|
## Setting the flag `false` which is the default, the assumption is that all
|
|
|
|
## the checkponts before have been vetted already regardless of the current
|
|
|
|
## branch. So the nearest `epoch` header is used.
|
|
|
|
c.applySnapsMinBacklog
|
2021-07-06 13:14:45 +00:00
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public setters
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
proc `db=`*(c: Clique; db: BaseChainDB) {.inline.} =
|
|
|
|
## Setter, re-set database
|
2021-07-14 15:13:27 +00:00
|
|
|
c.cfg.db = db
|
|
|
|
c.proposals = initTable[EthAddress,bool]()
|
|
|
|
c.recents = c.cfg.initLruSnaps
|
|
|
|
|
2021-07-21 13:31:52 +00:00
|
|
|
proc `snapshot=`*(c: Clique; snaps: Snapshot) =
|
2021-07-14 15:13:27 +00:00
|
|
|
## Setter
|
2021-07-21 13:31:52 +00:00
|
|
|
c.snapshot = snaps
|
2021-07-14 15:13:27 +00:00
|
|
|
|
2021-07-21 13:31:52 +00:00
|
|
|
proc `failed=`*(c: Clique; failure: CliqueFailed) =
|
2021-07-14 15:13:27 +00:00
|
|
|
## Setter
|
2021-07-21 13:31:52 +00:00
|
|
|
c.failed = failure
|
2021-07-06 13:14:45 +00:00
|
|
|
|
2021-07-30 14:06:51 +00:00
|
|
|
proc `applySnapsMinBacklog=`*(c: Clique; value: bool) {.inline.} =
|
|
|
|
## Setter
|
|
|
|
c.applySnapsMinBacklog = value
|
|
|
|
|
2021-07-06 13:14:45 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public lock/unlock
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2021-07-30 14:06:51 +00:00
|
|
|
when enableCliqueAsyncLock:
|
|
|
|
proc lock*(c: Clique) {.inline, raises: [Defect,CatchableError].} =
|
|
|
|
## Lock descriptor
|
|
|
|
waitFor c.asyncLock.acquire
|
2021-07-06 13:14:45 +00:00
|
|
|
|
2021-07-30 14:06:51 +00:00
|
|
|
proc unLock*(c: Clique) {.inline, raises: [Defect,AsyncLockError].} =
|
|
|
|
## Unlock descriptor
|
|
|
|
c.asyncLock.release
|
2021-07-06 13:14:45 +00:00
|
|
|
|
2021-07-30 14:06:51 +00:00
|
|
|
template doExclusively*(c: Clique; action: untyped) =
|
|
|
|
## Handy helper
|
|
|
|
c.lock
|
|
|
|
action
|
|
|
|
c.unlock
|
2021-07-06 13:14:45 +00:00
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# End
|
|
|
|
# ------------------------------------------------------------------------------
|