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>`_
|
|
|
|
##
|
|
|
|
|
|
|
|
import
|
2022-07-21 18:16:28 +00:00
|
|
|
std/[sequtils, times],
|
|
|
|
chronicles,
|
|
|
|
chronos,
|
|
|
|
eth/[common, keys, rlp],
|
|
|
|
"../.."/[constants, utils/ec_recover],
|
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
|
|
|
{.push raises: [Defect].}
|
|
|
|
|
|
|
|
logScope:
|
|
|
|
topics = "clique PoA Mining"
|
|
|
|
|
|
|
|
type
|
|
|
|
CliqueSyncDefect* = object of Defect
|
|
|
|
## Defect raised with lock/unlock problem
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Private Helpers
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
template syncExceptionWrap(action: untyped) =
|
|
|
|
try:
|
|
|
|
action
|
|
|
|
except:
|
|
|
|
raise (ref CliqueSyncDefect)(msg: getCurrentException().msg)
|
|
|
|
|
2021-07-30 14:06:51 +00:00
|
|
|
|
|
|
|
# clique/clique.go(217): func (c *Clique) VerifyHeader(chain [..]
|
|
|
|
proc verifyHeader(c: Clique; header: BlockHeader): CliqueOkResult
|
|
|
|
{.gcsafe, raises: [Defect,CatchableError].} =
|
|
|
|
## See `clique.cliqueVerify()`
|
|
|
|
var blind: seq[BlockHeader]
|
|
|
|
c.cliqueVerifySeq(header, blind)
|
|
|
|
|
|
|
|
proc verifyHeader(c: Clique; header: BlockHeader;
|
|
|
|
parents: openArray[BlockHeader]): CliqueOkResult
|
|
|
|
{.gcsafe, raises: [Defect,CatchableError].} =
|
|
|
|
## See `clique.cliqueVerify()`
|
|
|
|
var list = toSeq(parents)
|
|
|
|
c.cliqueVerifySeq(header, list)
|
|
|
|
|
|
|
|
|
2022-07-21 18:16:28 +00:00
|
|
|
proc isValidVote(s: Snapshot; a: EthAddress; authorize: bool): bool =
|
2021-07-30 14:06:51 +00:00
|
|
|
s.ballot.isValidVote(a, authorize)
|
|
|
|
|
|
|
|
proc isSigner*(s: Snapshot; address: EthAddress): bool =
|
|
|
|
## See `clique_verify.isSigner()`
|
|
|
|
s.ballot.isAuthSigner(address)
|
|
|
|
|
|
|
|
# clique/snapshot.go(319): func (s *Snapshot) inturn(number [..]
|
|
|
|
proc inTurn*(s: Snapshot; number: BlockNumber, signer: EthAddress): bool =
|
|
|
|
## 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 [..]
|
2021-07-30 14:06:51 +00:00
|
|
|
proc calcDifficulty(s: Snapshot; signer: EthAddress): DifficultyInt =
|
|
|
|
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;
|
2022-07-21 18:16:28 +00:00
|
|
|
a: EthAddress): Result[BlockNumber,void] =
|
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(212): func (c *Clique) Author(header [..]
|
2021-08-24 07:34:58 +00:00
|
|
|
proc author*(c: Clique; header: BlockHeader): Result[EthAddress,UtilsError]
|
2021-07-06 13:14:45 +00:00
|
|
|
{.gcsafe, raises: [Defect,CatchableError].} =
|
|
|
|
## For the Consensus Engine, `author()` retrieves the Ethereum address of the
|
|
|
|
## account that minted the given block, which may be different from the
|
|
|
|
## header's coinbase if a consensus engine is based on signatures.
|
|
|
|
##
|
|
|
|
## This implementation returns the Ethereum address recovered from the
|
|
|
|
## signature in the header's extra-data section.
|
2021-07-21 13:31:52 +00:00
|
|
|
c.cfg.ecRecover(header)
|
2021-07-06 13:14:45 +00:00
|
|
|
|
|
|
|
|
|
|
|
# clique/clique.go(224): func (c *Clique) VerifyHeader(chain [..]
|
|
|
|
proc verifyHeaders*(c: Clique; headers: openArray[BlockHeader]):
|
2021-07-14 15:13:27 +00:00
|
|
|
Future[seq[CliqueOkResult]] {.async,gcsafe.} =
|
2021-07-06 13:14:45 +00:00
|
|
|
## For the Consensus Engine, `verifyHeader()` s similar to VerifyHeader, but
|
|
|
|
## verifies a batch of headers concurrently. This method is accompanied
|
|
|
|
## by a `stopVerifyHeader()` method that can abort the operations.
|
|
|
|
##
|
|
|
|
## This implementation checks whether a header conforms to the consensus
|
|
|
|
## rules. It verifies a batch of headers. If running in the background,
|
|
|
|
## the process can be stopped by calling the `stopVerifyHeader()` function.
|
|
|
|
syncExceptionWrap:
|
|
|
|
c.doExclusively:
|
|
|
|
c.stopVHeaderReq = false
|
|
|
|
for n in 0 ..< headers.len:
|
|
|
|
c.doExclusively:
|
|
|
|
let isStopRequest = c.stopVHeaderReq
|
|
|
|
if isStopRequest:
|
|
|
|
result.add cliqueResultErr((errCliqueStopped,""))
|
|
|
|
break
|
2021-07-30 14:06:51 +00:00
|
|
|
result.add c.verifyHeader(headers[n], headers[0 ..< n])
|
2021-07-06 13:14:45 +00:00
|
|
|
c.doExclusively:
|
|
|
|
c.stopVHeaderReq = false
|
|
|
|
|
|
|
|
proc stopVerifyHeader*(c: Clique): bool {.discardable.} =
|
|
|
|
## Activate the stop flag for running `verifyHeader()` function.
|
|
|
|
## Returns `true` if the stop flag could be activated.
|
|
|
|
syncExceptionWrap:
|
|
|
|
c.doExclusively:
|
|
|
|
if not c.stopVHeaderReq:
|
|
|
|
c.stopVHeaderReq = true
|
|
|
|
result = true
|
|
|
|
|
|
|
|
|
|
|
|
# clique/clique.go(450): func (c *Clique) VerifyUncles(chain [..]
|
2021-07-14 15:13:27 +00:00
|
|
|
proc verifyUncles*(c: Clique; ethBlock: EthBlock): CliqueOkResult =
|
2021-07-06 13:14:45 +00:00
|
|
|
## For the Consensus Engine, `verifyUncles()` verifies that the given
|
|
|
|
## block's uncles conform to the consensus rules of a given engine.
|
|
|
|
##
|
|
|
|
## This implementation always returns an error for existing uncles as this
|
|
|
|
## consensus mechanism doesn't permit uncles.
|
|
|
|
if 0 < ethBlock.uncles.len:
|
|
|
|
return err((errCliqueUnclesNotAllowed,""))
|
|
|
|
result = ok()
|
|
|
|
|
|
|
|
|
|
|
|
# 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
|
2021-10-05 23:31:35 +00:00
|
|
|
{.gcsafe, raises: [Defect, 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
|
2021-07-14 15:13:27 +00:00
|
|
|
let rc = c.cliqueSnapshot(header.parentHash, @[])
|
|
|
|
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
|
|
|
|
|
2021-07-06 13:14:45 +00:00
|
|
|
if (header.blockNumber mod c.cfg.epoch) != 0:
|
|
|
|
c.doExclusively:
|
|
|
|
# Gather all the proposals that make sense voting on
|
|
|
|
var addresses: seq[EthAddress]
|
|
|
|
for (address,authorize) in c.proposals.pairs:
|
2021-07-14 15:13:27 +00:00
|
|
|
if c.snapshot.isValidVote(address, authorize):
|
2021-07-06 13:14:45 +00:00
|
|
|
addresses.add address
|
|
|
|
|
|
|
|
# If there's pending proposals, cast a vote on them
|
|
|
|
if 0 < addresses.len:
|
2021-07-14 15:13:27 +00:00
|
|
|
header.coinbase = addresses[c.cfg.rand(addresses.len-1)]
|
2021-07-06 13:14:45 +00:00
|
|
|
header.nonce = if header.coinbase in c.proposals: NONCE_AUTH
|
|
|
|
else: NONCE_DROP
|
|
|
|
|
|
|
|
# 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)
|
|
|
|
if (header.blockNumber mod c.cfg.epoch) == 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
|
|
|
|
|
|
|
# clique/clique.go(589): func (c *Clique) Authorize(signer [..]
|
|
|
|
proc authorize*(c: Clique; signer: EthAddress; signFn: CliqueSignerFn) =
|
|
|
|
## Injects private key into the consensus engine to mint new blocks with.
|
|
|
|
syncExceptionWrap:
|
|
|
|
c.doExclusively:
|
|
|
|
c.signer = signer
|
|
|
|
c.signFn = signFn
|
|
|
|
|
|
|
|
# clique/clique.go(724): func CliqueRLP(header [..]
|
|
|
|
proc cliqueRlp*(header: BlockHeader): seq[byte] =
|
|
|
|
## 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 {
|
|
|
|
proc sealHash*(header: BlockHeader): Hash256 =
|
|
|
|
## 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,
|
|
|
|
raises: [Defect,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
|
|
|
|
c.doExclusively:
|
|
|
|
let
|
|
|
|
signer = c.signer
|
|
|
|
signFn = c.signFn
|
|
|
|
|
|
|
|
# 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
|
|
|
|
except Exception as exc:
|
|
|
|
return err((errCliqueSealSigFn, "Error when signing block header"))
|
|
|
|
|
|
|
|
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]
|
|
|
|
{.gcsafe, raises: [Defect,CatchableError].} =
|
|
|
|
## 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
|
|
|
|
# ------------------------------------------------------------------------------
|