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
|
2022-07-21 18:16:28 +00:00
|
|
|
std/tables,
|
2021-07-06 13:14:45 +00:00
|
|
|
../../db/db_chain,
|
|
|
|
../../constants,
|
|
|
|
./clique_cfg,
|
|
|
|
./clique_defs,
|
2022-07-21 18:16:28 +00:00
|
|
|
./snapshot/snapshot_desc,
|
2021-07-21 13:31:52 +00:00
|
|
|
chronicles,
|
|
|
|
eth/[common, keys, rlp],
|
2022-07-21 18:16:28 +00:00
|
|
|
stew/[keyed_queue, 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
|
2021-08-19 12:00:30 +00:00
|
|
|
RawSignature* = array[RawSignatureSize, byte]
|
|
|
|
|
2021-07-06 13:14:45 +00:00
|
|
|
# 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;
|
2021-08-19 12:00:30 +00:00
|
|
|
message: openArray[byte]): Result[RawSignature, cstring] {.gcsafe.}
|
2021-07-06 13:14:45 +00:00
|
|
|
|
|
|
|
Proposals = Table[EthAddress,bool]
|
|
|
|
|
2022-07-21 18:16:28 +00:00
|
|
|
CliqueSnapKey* = ##\
|
|
|
|
## Internal key used for the LRU cache (derived from Hash256).
|
|
|
|
array[32,byte]
|
|
|
|
|
|
|
|
CliqueSnapLru = ##\
|
|
|
|
## Snapshots cache
|
|
|
|
KeyedQueue[CliqueSnapKey,Snapshot]
|
|
|
|
|
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
|
|
|
|
2022-07-21 18:16:28 +00:00
|
|
|
recents: CliqueSnapLru ##\
|
2021-07-14 15:13:27 +00:00
|
|
|
## 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
|
2022-07-21 18:16:28 +00:00
|
|
|
## walk back to the1 `epoch` header with at least `cfg.roThreshold` blocks
|
2021-07-30 14:06:51 +00:00
|
|
|
## 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,
|
|
|
|
snapshot: cfg.newSnapshot(BlockHeader()),
|
|
|
|
proposals: initTable[EthAddress,bool]())
|
|
|
|
when enableCliqueAsyncLock:
|
|
|
|
result.asyncLock = newAsyncLock()
|
2021-07-06 13:14:45 +00:00
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
2022-07-21 18:16:28 +00:00
|
|
|
# Public debug/pretty print
|
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
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2022-07-21 18:16:28 +00:00
|
|
|
proc recents*(c: Clique): var KeyedQueue[CliqueSnapKey,Snapshot] =
|
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
|
|
|
|
2022-07-21 18:16:28 +00:00
|
|
|
proc proposals*(c: Clique): var Proposals =
|
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
|
|
|
|
2022-07-21 18:16:28 +00:00
|
|
|
proc snapshot*(c: Clique): Snapshot =
|
2021-07-21 13:31:52 +00:00
|
|
|
## Getter, last successfully processed snapshot.
|
2021-07-14 15:13:27 +00:00
|
|
|
c.snapshot
|
2021-07-06 13:14:45 +00:00
|
|
|
|
2022-07-21 18:16:28 +00:00
|
|
|
proc failed*(c: Clique): CliqueFailed =
|
2021-07-21 13:31:52 +00:00
|
|
|
## Getter, last snapshot error.
|
|
|
|
c.failed
|
2021-07-14 15:13:27 +00:00
|
|
|
|
2022-07-21 18:16:28 +00:00
|
|
|
proc cfg*(c: Clique): CliqueCfg =
|
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
|
|
|
|
2022-07-21 18:16:28 +00:00
|
|
|
proc db*(c: Clique): BaseChainDB =
|
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
|
|
|
|
2022-07-21 18:16:28 +00:00
|
|
|
proc applySnapsMinBacklog*(c: Clique): bool =
|
2021-07-30 14:06:51 +00:00
|
|
|
## 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
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2022-07-21 18:16:28 +00:00
|
|
|
proc `db=`*(c: Clique; db: BaseChainDB) =
|
2021-07-06 13:14:45 +00:00
|
|
|
## Setter, re-set database
|
2021-07-14 15:13:27 +00:00
|
|
|
c.cfg.db = db
|
|
|
|
c.proposals = initTable[EthAddress,bool]()
|
|
|
|
|
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
|
|
|
|
2022-07-21 18:16:28 +00:00
|
|
|
proc `applySnapsMinBacklog=`*(c: Clique; value: bool) =
|
2021-07-30 14:06:51 +00:00
|
|
|
## 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:
|
2022-07-21 18:16:28 +00:00
|
|
|
proc lock*(c: Clique) {.gcsafe, raises: [Defect,CatchableError].} =
|
2021-07-30 14:06:51 +00:00
|
|
|
## Lock descriptor
|
|
|
|
waitFor c.asyncLock.acquire
|
2021-07-06 13:14:45 +00:00
|
|
|
|
2022-07-21 18:16:28 +00:00
|
|
|
proc unLock*(c: Clique) {.gcsafe, raises: [Defect,AsyncLockError].} =
|
2021-07-30 14:06:51 +00:00
|
|
|
## 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
|
|
|
|
2021-08-24 07:34:58 +00:00
|
|
|
else:
|
|
|
|
template doExclusively*(c: Clique; action: untyped) =
|
|
|
|
action
|
|
|
|
|
2021-07-06 13:14:45 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# End
|
|
|
|
# ------------------------------------------------------------------------------
|