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.
|
|
|
|
|
|
|
|
##
|
|
|
|
## Mining Support for Clique PoA Consensus Protocol
|
2022-07-21 18:16:28 +00:00
|
|
|
## ===================!=============================
|
2021-07-06 13:14:45 +00:00
|
|
|
##
|
|
|
|
## 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>`_
|
|
|
|
##
|
|
|
|
|
2023-05-10 16:04:35 +00:00
|
|
|
{.push raises: [].}
|
2023-02-14 20:27:17 +00:00
|
|
|
|
2021-07-06 13:14:45 +00:00
|
|
|
import
|
2022-07-21 18:16:28 +00:00
|
|
|
std/[sequtils, times],
|
|
|
|
chronicles,
|
|
|
|
chronos,
|
2023-01-30 22:10:23 +00:00
|
|
|
eth/keys,
|
2022-07-21 18:16:28 +00:00
|
|
|
"../.."/[constants, utils/ec_recover],
|
2022-12-05 11:25:44 +00:00
|
|
|
../../common/common,
|
2021-07-06 13:14:45 +00:00
|
|
|
./clique_cfg,
|
|
|
|
./clique_defs,
|
|
|
|
./clique_desc,
|
2021-07-21 13:31:52 +00:00
|
|
|
./clique_helpers,
|
2021-07-14 15:13:27 +00:00
|
|
|
./clique_snapshot,
|
2021-07-21 13:31:52 +00:00
|
|
|
./clique_verify,
|
2022-07-21 18:16:28 +00:00
|
|
|
./snapshot/[ballot, snapshot_desc]
|
2021-07-30 14:06:51 +00:00
|
|
|
|
2021-07-06 13:14:45 +00:00
|
|
|
logScope:
|
|
|
|
topics = "clique PoA Mining"
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Private Helpers
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2023-01-30 22:10:23 +00:00
|
|
|
proc isValidVote(s: Snapshot; a: EthAddress; authorize: bool): bool {.gcsafe, raises: [].} =
|
2021-07-30 14:06:51 +00:00
|
|
|
s.ballot.isValidVote(a, authorize)
|
|
|
|
|
2023-01-30 22:10:23 +00:00
|
|
|
proc isSigner*(s: Snapshot; address: EthAddress): bool {.gcsafe, raises: [].} =
|
2021-07-30 14:06:51 +00:00
|
|
|
## See `clique_verify.isSigner()`
|
|
|
|
s.ballot.isAuthSigner(address)
|
|
|
|
|
|
|
|
# clique/snapshot.go(319): func (s *Snapshot) inturn(number [..]
|
2023-01-30 22:10:23 +00:00
|
|
|
proc inTurn*(s: Snapshot; number: BlockNumber, signer: EthAddress): bool {.gcsafe, raises: [].} =
|
2021-07-30 14:06:51 +00:00
|
|
|
## See `clique_verify.inTurn()`
|
|
|
|
let ascSignersList = s.ballot.authSigners
|
|
|
|
for offset in 0 ..< ascSignersList.len:
|
|
|
|
if ascSignersList[offset] == signer:
|
|
|
|
return (number mod ascSignersList.len.u256) == offset.u256
|
|
|
|
|
2021-07-06 13:14:45 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Private functions
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
# clique/clique.go(681): func calcDifficulty(snap [..]
|
2023-01-30 22:10:23 +00:00
|
|
|
proc calcDifficulty(s: Snapshot; signer: EthAddress): DifficultyInt {.gcsafe, raises: [].} =
|
2021-07-30 14:06:51 +00:00
|
|
|
if s.inTurn(s.blockNumber + 1, signer):
|
2021-07-06 13:14:45 +00:00
|
|
|
DIFF_INTURN
|
|
|
|
else:
|
|
|
|
DIFF_NOTURN
|
|
|
|
|
2021-07-30 14:06:51 +00:00
|
|
|
proc recentBlockNumber*(s: Snapshot;
|
2023-01-30 22:10:23 +00:00
|
|
|
a: EthAddress): Result[BlockNumber,void] {.gcsafe, raises: [].} =
|
2021-07-30 14:06:51 +00:00
|
|
|
## Return `BlockNumber` for `address` argument (if any)
|
|
|
|
for (number,recent) in s.recents.pairs:
|
|
|
|
if recent == a:
|
|
|
|
return ok(number)
|
|
|
|
return err()
|
|
|
|
|
2021-07-06 13:14:45 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public functions
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
# clique/clique.go(506): func (c *Clique) Prepare(chain [..]
|
2021-08-24 07:34:58 +00:00
|
|
|
proc prepare*(c: Clique; parent: BlockHeader, header: var BlockHeader): CliqueOkResult
|
2023-01-30 22:10:23 +00:00
|
|
|
{.gcsafe, raises: [CatchableError].} =
|
2021-07-06 13:14:45 +00:00
|
|
|
## For the Consensus Engine, `prepare()` initializes the consensus fields
|
2022-07-21 18:16:28 +00:00
|
|
|
## of a block header according to the rules of a particular engine.
|
2021-07-06 13:14:45 +00:00
|
|
|
##
|
|
|
|
## This implementation prepares all the consensus fields of the header for
|
|
|
|
## running the transactions on top.
|
|
|
|
|
|
|
|
# Assemble the voting snapshot to check which votes make sense
|
2022-12-06 05:55:40 +00:00
|
|
|
let rc = c.cliqueSnapshot(parent.blockHash, @[])
|
2021-07-14 15:13:27 +00:00
|
|
|
if rc.isErr:
|
|
|
|
return err(rc.error)
|
2021-07-06 13:14:45 +00:00
|
|
|
|
2022-05-29 03:26:11 +00:00
|
|
|
# if we are not voting, coinbase should be filled with zero
|
|
|
|
# because other subsystem e.g txpool can produce block header
|
|
|
|
# with non zero coinbase. if that coinbase is one of the signer
|
|
|
|
# and the nonce is zero, that signer will be vote out from
|
|
|
|
# signer list
|
|
|
|
header.coinbase.reset
|
|
|
|
|
2022-12-06 05:55:40 +00:00
|
|
|
let modEpoch = (parent.blockNumber+1) mod c.cfg.epoch
|
|
|
|
if modEpoch != 0:
|
2022-12-09 04:25:47 +00:00
|
|
|
# Gather all the proposals that make sense voting on
|
|
|
|
var addresses: seq[EthAddress]
|
|
|
|
for (address,authorize) in c.proposals.pairs:
|
|
|
|
if c.snapshot.isValidVote(address, authorize):
|
|
|
|
addresses.add address
|
|
|
|
|
|
|
|
# If there's pending proposals, cast a vote on them
|
|
|
|
if 0 < addresses.len:
|
|
|
|
header.coinbase = addresses[c.cfg.rand(addresses.len-1)]
|
|
|
|
header.nonce = if header.coinbase in c.proposals: NONCE_AUTH
|
|
|
|
else: NONCE_DROP
|
2021-07-06 13:14:45 +00:00
|
|
|
|
|
|
|
# Set the correct difficulty
|
2021-07-14 15:13:27 +00:00
|
|
|
header.difficulty = c.snapshot.calcDifficulty(c.signer)
|
2021-07-06 13:14:45 +00:00
|
|
|
|
|
|
|
# Ensure the extra data has all its components
|
|
|
|
header.extraData.setLen(EXTRA_VANITY)
|
2022-12-06 05:55:40 +00:00
|
|
|
if modEpoch == 0:
|
2021-07-30 14:06:51 +00:00
|
|
|
header.extraData.add c.snapshot.ballot.authSigners.mapIt(toSeq(it)).concat
|
2021-07-06 13:14:45 +00:00
|
|
|
header.extraData.add 0.byte.repeat(EXTRA_SEAL)
|
|
|
|
|
|
|
|
# Mix digest is reserved for now, set to empty
|
|
|
|
header.mixDigest.reset
|
|
|
|
|
|
|
|
# Ensure the timestamp has the correct delay
|
2021-07-14 15:13:27 +00:00
|
|
|
header.timestamp = parent.timestamp + c.cfg.period
|
2021-07-06 13:14:45 +00:00
|
|
|
if header.timestamp < getTime():
|
|
|
|
header.timestamp = getTime()
|
|
|
|
|
2021-08-24 07:34:58 +00:00
|
|
|
ok()
|
2021-07-06 13:14:45 +00:00
|
|
|
|
2023-01-30 22:10:23 +00:00
|
|
|
proc prepareForSeal*(c: Clique; prepHeader: BlockHeader; header: var BlockHeader) {.gcsafe, raises: [].} =
|
2022-12-06 05:55:40 +00:00
|
|
|
# TODO: use system.move?
|
|
|
|
header.nonce = prepHeader.nonce
|
|
|
|
header.extraData = prepHeader.extraData
|
|
|
|
header.mixDigest = prepHeader.mixDigest
|
|
|
|
|
2021-07-06 13:14:45 +00:00
|
|
|
# clique/clique.go(589): func (c *Clique) Authorize(signer [..]
|
2023-01-30 22:10:23 +00:00
|
|
|
proc authorize*(c: Clique; signer: EthAddress; signFn: CliqueSignerFn) {.gcsafe, raises: [].} =
|
2021-07-06 13:14:45 +00:00
|
|
|
## Injects private key into the consensus engine to mint new blocks with.
|
2022-12-09 04:25:47 +00:00
|
|
|
c.signer = signer
|
|
|
|
c.signFn = signFn
|
2021-07-06 13:14:45 +00:00
|
|
|
|
|
|
|
# clique/clique.go(724): func CliqueRLP(header [..]
|
2023-01-30 22:10:23 +00:00
|
|
|
proc cliqueRlp*(header: BlockHeader): seq[byte] {.gcsafe, raises: [].} =
|
2021-07-06 13:14:45 +00:00
|
|
|
## Returns the rlp bytes which needs to be signed for the proof-of-authority
|
|
|
|
## sealing. The RLP to sign consists of the entire header apart from the 65
|
|
|
|
## byte signature contained at the end of the extra data.
|
|
|
|
##
|
|
|
|
## Note, the method requires the extra data to be at least 65 bytes,
|
|
|
|
## otherwise it panics. This is done to avoid accidentally using both forms
|
|
|
|
## (signature present or not), which could be abused to produce different
|
|
|
|
##hashes for the same header.
|
|
|
|
header.encodeSealHeader
|
|
|
|
|
|
|
|
# clique/clique.go(688): func SealHash(header *types.Header) common.Hash {
|
2023-01-30 22:10:23 +00:00
|
|
|
proc sealHash*(header: BlockHeader): Hash256 {.gcsafe, raises: [].} =
|
2021-07-06 13:14:45 +00:00
|
|
|
## For the Consensus Engine, `sealHash()` returns the hash of a block prior
|
|
|
|
## to it being sealed.
|
|
|
|
##
|
|
|
|
## This implementation returns the hash of a block prior to it being sealed.
|
|
|
|
header.hashSealHeader
|
|
|
|
|
|
|
|
|
|
|
|
# clique/clique.go(599): func (c *Clique) Seal(chain [..]
|
2021-08-24 07:34:58 +00:00
|
|
|
proc seal*(c: Clique; ethBlock: var EthBlock):
|
|
|
|
Result[void,CliqueError] {.gcsafe,
|
2023-01-30 22:10:23 +00:00
|
|
|
raises: [CatchableError].} =
|
2021-07-06 13:14:45 +00:00
|
|
|
## This implementation attempts to create a sealed block using the local
|
2021-08-24 09:14:17 +00:00
|
|
|
## signing credentials.
|
2021-08-24 07:34:58 +00:00
|
|
|
|
2021-07-06 13:14:45 +00:00
|
|
|
var header = ethBlock.header
|
|
|
|
|
|
|
|
# Sealing the genesis block is not supported
|
|
|
|
if header.blockNumber.isZero:
|
2021-08-24 07:34:58 +00:00
|
|
|
return err((errUnknownBlock, ""))
|
2021-07-06 13:14:45 +00:00
|
|
|
|
|
|
|
# For 0-period chains, refuse to seal empty blocks (no reward but would spin
|
|
|
|
# sealing)
|
|
|
|
if c.cfg.period.isZero and ethBlock.txs.len == 0:
|
|
|
|
info $nilCliqueSealNoBlockYet
|
2021-08-24 07:34:58 +00:00
|
|
|
return err((nilCliqueSealNoBlockYet, ""))
|
2021-07-06 13:14:45 +00:00
|
|
|
|
|
|
|
# Don't hold the signer fields for the entire sealing procedure
|
2022-12-09 04:25:47 +00:00
|
|
|
let
|
|
|
|
signer = c.signer
|
|
|
|
signFn = c.signFn
|
2021-07-06 13:14:45 +00:00
|
|
|
|
|
|
|
# Bail out if we're unauthorized to sign a block
|
2021-07-14 15:13:27 +00:00
|
|
|
let rc = c.cliqueSnapshot(header.parentHash)
|
|
|
|
if rc.isErr:
|
|
|
|
return err(rc.error)
|
|
|
|
if not c.snapshot.isSigner(signer):
|
2021-08-24 07:34:58 +00:00
|
|
|
return err((errUnauthorizedSigner, ""))
|
2021-07-06 13:14:45 +00:00
|
|
|
|
|
|
|
# If we're amongst the recent signers, wait for the next block
|
2021-07-30 14:06:51 +00:00
|
|
|
let seen = c.snapshot.recentBlockNumber(signer)
|
2021-07-06 13:14:45 +00:00
|
|
|
if seen.isOk:
|
|
|
|
# Signer is among recents, only wait if the current block does not
|
|
|
|
# shift it out
|
2021-07-14 15:13:27 +00:00
|
|
|
if header.blockNumber < seen.value + c.snapshot.signersThreshold.u256:
|
2021-07-06 13:14:45 +00:00
|
|
|
info $nilCliqueSealSignedRecently
|
2021-08-24 07:34:58 +00:00
|
|
|
return err((nilCliqueSealSignedRecently, ""))
|
2021-07-06 13:14:45 +00:00
|
|
|
|
|
|
|
# Sweet, the protocol permits us to sign the block, wait for our time
|
|
|
|
var delay = header.timestamp - getTime()
|
|
|
|
if header.difficulty == DIFF_NOTURN:
|
|
|
|
# It's not our turn explicitly to sign, delay it a bit
|
2021-07-14 15:13:27 +00:00
|
|
|
let wiggle = c.snapshot.signersThreshold.int64 * WIGGLE_TIME
|
2021-07-06 13:14:45 +00:00
|
|
|
# Kludge for limited rand() argument range
|
|
|
|
if wiggle.inSeconds < (int.high div 1000).int64:
|
2022-04-08 04:54:11 +00:00
|
|
|
let rndWiggleMs = c.cfg.rand(wiggle.inMilliseconds.int)
|
2021-07-06 13:14:45 +00:00
|
|
|
delay += initDuration(milliseconds = rndWiggleMs)
|
|
|
|
else:
|
2021-07-14 15:13:27 +00:00
|
|
|
let rndWiggleSec = c.cfg.rand((wiggle.inSeconds and int.high).int)
|
2021-07-06 13:14:45 +00:00
|
|
|
delay += initDuration(seconds = rndWiggleSec)
|
|
|
|
|
|
|
|
trace "Out-of-turn signing requested",
|
|
|
|
wiggle = $wiggle
|
|
|
|
|
|
|
|
# Sign all the things!
|
2021-08-24 07:34:58 +00:00
|
|
|
try:
|
|
|
|
let signature = signFn(signer,header.cliqueRlp)
|
|
|
|
if signature.isErr:
|
|
|
|
return err((errCliqueSealSigFn,$signature.error))
|
|
|
|
let extraLen = header.extraData.len
|
|
|
|
if EXTRA_SEAL < extraLen:
|
|
|
|
header.extraData.setLen(extraLen - EXTRA_SEAL)
|
|
|
|
header.extraData.add signature.value
|
2023-02-14 20:27:17 +00:00
|
|
|
except CatchableError as exc:
|
2022-12-06 05:55:40 +00:00
|
|
|
return err((errCliqueSealSigFn,
|
|
|
|
"Error when signing block header: " & exc.msg))
|
2021-08-24 07:34:58 +00:00
|
|
|
|
|
|
|
ethBlock = ethBlock.withHeader(header)
|
|
|
|
ok()
|
2021-07-06 13:14:45 +00:00
|
|
|
|
|
|
|
# clique/clique.go(673): func (c *Clique) CalcDifficulty(chain [..]
|
2022-06-14 06:01:51 +00:00
|
|
|
proc calcDifficulty*(c: Clique;
|
2021-07-06 13:14:45 +00:00
|
|
|
parent: BlockHeader): Result[DifficultyInt,CliqueError]
|
2023-01-30 22:10:23 +00:00
|
|
|
{.gcsafe, raises: [CatchableError].} =
|
2021-07-06 13:14:45 +00:00
|
|
|
## For the Consensus Engine, `calcDifficulty()` is the difficulty adjustment
|
|
|
|
## algorithm. It returns the difficulty that a new block should have.
|
|
|
|
##
|
|
|
|
## This implementation returns the difficulty that a new block should have:
|
|
|
|
## * DIFF_NOTURN(2) if BLOCK_NUMBER % SIGNER_COUNT != SIGNER_INDEX
|
|
|
|
## * DIFF_INTURN(1) if BLOCK_NUMBER % SIGNER_COUNT == SIGNER_INDEX
|
2021-07-14 15:13:27 +00:00
|
|
|
let rc = c.cliqueSnapshot(parent)
|
|
|
|
if rc.isErr:
|
|
|
|
return err(rc.error)
|
|
|
|
return ok(c.snapshot.calcDifficulty(c.signer))
|
2021-07-06 13:14:45 +00:00
|
|
|
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# End
|
|
|
|
# ------------------------------------------------------------------------------
|