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.
|
|
|
|
|
|
|
|
##
|
|
|
|
## EIP-225 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-12-05 11:25:44 +00:00
|
|
|
std/[times],
|
|
|
|
./clique/[clique_cfg, clique_defs, clique_desc],
|
2021-07-21 13:31:52 +00:00
|
|
|
./clique/snapshot/[ballot, snapshot_desc],
|
|
|
|
stew/results
|
2021-06-14 18:33:57 +00:00
|
|
|
|
2023-01-30 22:10:23 +00:00
|
|
|
{.push raises: [].}
|
2021-05-28 17:39:55 +00:00
|
|
|
|
2021-07-30 14:06:51 +00:00
|
|
|
# Note that mining is unsupported. Unused code ported from the Go
|
|
|
|
# implementation is stashed into the `clique_unused` module.
|
2021-07-06 13:14:45 +00:00
|
|
|
export
|
|
|
|
clique_cfg,
|
|
|
|
clique_defs,
|
2021-07-30 14:06:51 +00:00
|
|
|
clique_desc.Clique
|
2021-07-21 13:31:52 +00:00
|
|
|
|
|
|
|
type
|
|
|
|
CliqueState* = ##\
|
|
|
|
## Descriptor state snapshot which can be used for implementing
|
|
|
|
## transaction trasnaction handling. Nore the the `Snapshot` type
|
|
|
|
## inside the `Result[]` is most probably opaque.
|
|
|
|
Result[Snapshot,void]
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2022-12-05 11:25:44 +00:00
|
|
|
proc newClique*(db: ChainDBRef, cliquePeriod, cliqueEpoch: int): Clique =
|
2021-07-30 14:06:51 +00:00
|
|
|
## Constructor for a new Clique proof-of-authority consensus engine. The
|
|
|
|
## initial state of the engine is `empty`, there are no authorised signers.
|
2021-08-10 12:31:11 +00:00
|
|
|
##
|
|
|
|
## If chain_config provides `Period` or `Epoch`, then `Period` or `Epoch`
|
|
|
|
## will be taken from chain_config. Otherwise, default value in `newCliqueCfg`
|
|
|
|
## will be used
|
2021-07-30 14:06:51 +00:00
|
|
|
|
2022-12-05 11:25:44 +00:00
|
|
|
let cfg = db.newCliqueCfg
|
|
|
|
if cliquePeriod > 0:
|
|
|
|
cfg.period = initDuration(seconds = cliquePeriod)
|
|
|
|
if cliqueEpoch > 0:
|
|
|
|
cfg.epoch = cliqueEpoch
|
2021-08-10 12:31:11 +00:00
|
|
|
cfg.newClique
|
2021-07-30 14:06:51 +00:00
|
|
|
|
2022-12-05 11:25:44 +00:00
|
|
|
proc cliqueSave*(c: Clique): CliqueState =
|
2021-07-30 14:06:51 +00:00
|
|
|
## Save current `Clique` state. This state snapshot saves the internal
|
|
|
|
## data that make up the list of authorised signers (see `cliqueSigners()`
|
|
|
|
## below.)
|
2021-07-21 13:31:52 +00:00
|
|
|
ok(c.snapshot)
|
|
|
|
|
2022-12-05 11:25:44 +00:00
|
|
|
proc cliqueRestore*(c: Clique; state: var CliqueState) =
|
2021-07-21 13:31:52 +00:00
|
|
|
## Restore current `Clique` state from a saved snapshot.
|
|
|
|
##
|
|
|
|
## For the particular `state` argument this fuction is disabled with
|
2021-07-30 14:06:51 +00:00
|
|
|
## `cliqueDispose()`. So it can be savely wrapped in a `defer:` statement.
|
|
|
|
## In transaction lingo, this would then be the rollback function.
|
2021-07-21 13:31:52 +00:00
|
|
|
if state.isOk:
|
|
|
|
c.snapshot = state.value
|
|
|
|
|
2022-12-05 11:25:44 +00:00
|
|
|
proc cliqueDispose*(c: Clique; state: var CliqueState) =
|
2021-07-21 13:31:52 +00:00
|
|
|
## Disable the function `cliqueDispose()` for the particular `state`
|
2021-07-30 14:06:51 +00:00
|
|
|
## argument.
|
|
|
|
##
|
|
|
|
## In transaction lingo, this would be the commit function if
|
|
|
|
## `cliqueRestore()` was wrapped in a `defer:` statement.
|
2021-07-21 13:31:52 +00:00
|
|
|
state = err(CliqueState)
|
|
|
|
|
2023-01-30 22:10:23 +00:00
|
|
|
proc cliqueSigners*(c: Clique): seq[EthAddress] =
|
2021-07-21 13:31:52 +00:00
|
|
|
## Retrieve the sorted list of authorized signers for the current state
|
|
|
|
## of the `Clique` descriptor.
|
|
|
|
##
|
2021-07-30 14:06:51 +00:00
|
|
|
## Note that the return argument list is sorted on-the-fly each time this
|
|
|
|
## function is invoked.
|
2021-07-21 13:31:52 +00:00
|
|
|
c.snapshot.ballot.authSigners
|
2021-06-11 17:26:08 +00:00
|
|
|
|
2023-01-30 22:10:23 +00:00
|
|
|
proc cliqueSignersLen*(c: Clique): int =
|
2021-07-30 14:06:51 +00:00
|
|
|
## Get the number of authorized signers for the current state of the
|
|
|
|
## `Clique` descriptor. The result is equivalent to `c.cliqueSigners.len`.
|
|
|
|
c.snapshot.ballot.authSignersLen
|
|
|
|
|
2021-05-28 17:39:55 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# End
|
|
|
|
# ------------------------------------------------------------------------------
|