mirror of
https://github.com/status-im/nimbus-eth1.git
synced 2025-02-05 16:54:00 +00:00
ca07c40a48
* Provide API details: API is bundled via clique.nim. * Set extraValidation as default for PoA chains why: This triggers consensus verification and an update of the list of authorised signers. These signers are integral part of the PoA block chain. todo: Option argument to control validation for the nimbus binary. * Fix snapshot state block number why: Using sub-sequence here, so the len() function was wrong. * Optional start where block verification begins why: Can speed up time building loading initial parts of block chain. For PoA, this allows to prove & test that authorised signers can be (correctly) calculated starting at any point on the block chain. todo: On Goerli around blocks #193537..#197568, processing time increases disproportionally -- needs to be understand * For Clique test, get old grouping back (7 transactions per log entry) why: Forgot to change back after troubleshooting * Fix field/function/module-name misunderstanding why: Make compilation work * Use eth_types.blockHash() rather than utils.hash() in Clique modules why: Prefer lib module * Dissolve snapshot_misc.nim details: .. into clique_verify.nim (the other source file clique_unused.nim is inactive) * Hide unused AsyncLock in Clique descriptor details: Unused here but was part of the Go reference implementation * Remove fakeDiff flag from Clique descriptor details: This flag was a kludge in the Go reference implementation used for the canonical tests. The tests have been adapted so there is no need for the fakeDiff flag and its implementation. * Not observing minimum distance from epoch sync point why: For compiling PoA state, the go implementation will walk back to the epoch header with at least 90000 blocks apart from the current header in the absence of other synchronisation points. Here just the nearest epoch header is used. The assumption is that all the checkpoints before have been vetted already regardless of the current branch. details: The behaviour of using the nearest vs the minimum distance epoch is controlled by a flag and can be changed at run time. * Analysing processing time (patch adds some debugging/visualisation support) why: At the first half million blocks of the Goerli replay, blocks on the interval #194854..#196224 take exceptionally long to process, but not due to PoA processing. details: It turns out that much time is spent in p2p/excecutor.processBlock() where the elapsed transaction execution time is significantly greater for many of these blocks. Between the 1371 blocks #194854..#196224 there are 223 blocks with more than 1/2 seconds execution time whereas there are only 4 such blocks before and 13 such after this range up to #504192. * fix debugging symbol in clique_desc (causes CI failing) * Fixing canonical reference tests why: Two errors were introduced earlier but ovelooked: 1. "Remove fakeDiff flag .." patch was incomplete 2. "Not observing minimum distance .." introduced problem w/tests 23/24 details: Fixing 2. needed to revert the behaviour by setting the applySnapsMinBacklog flag for the Clique descriptor. Also a new test was added to lock the new behaviour. * Remove cruft why: Clique/PoA processing was intended to take place somewhere in executor/process_block.processBlock() but was decided later to run from chain/persist_block.persistBlock() instead. * Update API comment * ditto
222 lines
7.5 KiB
Nim
222 lines
7.5 KiB
Nim
# 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.
|
|
|
|
##
|
|
## Snapshot Processor 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/[algorithm, sequtils, strutils, tables, times],
|
|
../clique_cfg,
|
|
../clique_defs,
|
|
./ballot,
|
|
./snapshot_desc,
|
|
chronicles,
|
|
eth/[common, rlp],
|
|
stew/results
|
|
|
|
{.push raises: [Defect].}
|
|
|
|
logScope:
|
|
topics = "clique PoA snapshot-apply"
|
|
|
|
# ------------------------------------------------------------------------------
|
|
# Private helpers, pretty printing
|
|
# ------------------------------------------------------------------------------
|
|
|
|
proc say(s: Snapshot; v: varargs[string,`$`]) {.inline.} =
|
|
discard
|
|
# uncomment body to enable
|
|
s.cfg.say v
|
|
|
|
proc pp(a: openArray[BlockHeader]; first, last: int): string {.inline.} =
|
|
result = "["
|
|
var
|
|
n = last - first
|
|
q = toSeq(a)
|
|
if last < first:
|
|
q = a.reversed(last, first)
|
|
n = q.len
|
|
if 5 < n:
|
|
result &= toSeq(q[0 .. 2]).mapIt("#" & $it.blockNumber).join(", ")
|
|
result &= " .." & $n & ".. #" & $q[n-1].blockNumber
|
|
else:
|
|
result &= toSeq(q[0 ..< n]).mapIt("#" & $it.blockNumber).join(", ")
|
|
result &= "]"
|
|
|
|
# ------------------------------------------------------------------------------
|
|
# Private functions
|
|
# ------------------------------------------------------------------------------
|
|
|
|
template pairWalkIj(first, last: int; offTop: Positive; code: untyped) =
|
|
if first <= last:
|
|
for n in first .. last - offTop:
|
|
let
|
|
i {.inject.} = n
|
|
j {.inject.} = n + 1
|
|
code
|
|
else:
|
|
for n in first.countdown(last + offTop):
|
|
let
|
|
i {.inject.} = n
|
|
j {.inject.} = n - 1
|
|
code
|
|
|
|
template doWalkIt(first, last: int; code: untyped) =
|
|
if first <= last:
|
|
for n in first .. last:
|
|
let it {.inject.} = n
|
|
code
|
|
else:
|
|
for n in first.countdown(last):
|
|
let it {.inject.} = n
|
|
code
|
|
|
|
# ------------------------------------------------------------------------------
|
|
# Public functions
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# clique/snapshot.go(185): func (s *Snapshot) apply(headers [..]
|
|
proc snapshotApplySeq*(s: Snapshot; headers: var seq[BlockHeader],
|
|
first, last: int): CliqueOkResult
|
|
{.gcsafe, raises: [Defect,CatchableError].} =
|
|
## Initialises an authorization snapshot `snap` by applying the `headers`
|
|
## to the argument snapshot desciptor `s`.
|
|
|
|
s.say "applySnapshot begin #", s.blockNumber, " + ", headers.pp(first, last)
|
|
|
|
# Sanity check that the headers can be applied
|
|
if headers[first].blockNumber != s.blockNumber + 1:
|
|
return err((errInvalidVotingChain,""))
|
|
# clique/snapshot.go(191): for i := 0; i < len(headers)-1; i++ {
|
|
first.pairWalkIj(last, 1):
|
|
if headers[j].blockNumber != headers[i].blockNumber+1:
|
|
return err((errInvalidVotingChain,""))
|
|
|
|
# Iterate through the headers and create a new snapshot
|
|
let
|
|
start = getTime()
|
|
var
|
|
logged = start
|
|
|
|
# clique/snapshot.go(206): for i, header := range headers [..]
|
|
first.doWalkIt(last):
|
|
let
|
|
# headersIndex => also used for logging at the end of this loop
|
|
headersIndex = it
|
|
header = headers[headersIndex]
|
|
number = header.blockNumber
|
|
|
|
s.say "applySnapshot processing #", number
|
|
|
|
# Remove any votes on checkpoint blocks
|
|
if (number mod s.cfg.epoch) == 0:
|
|
# Note that the correctness of the authorised accounts list is verified in
|
|
# clique/clique.verifyCascadingFields(),
|
|
# see clique/clique.go(355): if number%c.config.Epoch == 0 {
|
|
# This means, the account list passed with the epoch header is verified
|
|
# to be the same as the one we already have.
|
|
#
|
|
# clique/snapshot.go(210): snap.Votes = nil
|
|
s.ballot.flushVotes
|
|
s.say "applySnapshot epoch => reset, state=", s.pp(41)
|
|
|
|
# Delete the oldest signer from the recent list to allow it signing again
|
|
block:
|
|
let limit = s.ballot.authSignersThreshold.u256
|
|
if limit <= number:
|
|
s.recents.del(number - limit)
|
|
|
|
# Resolve the authorization key and check against signers
|
|
let signer = ? s.cfg.ecRecover(header)
|
|
#s.say "applySnapshot signer=", s.pp(signer)
|
|
|
|
if not s.ballot.isAuthSigner(signer):
|
|
s.say "applySnapshot signer not authorised => fail ", s.pp(29)
|
|
return err((errUnauthorizedSigner,""))
|
|
|
|
for recent in s.recents.values:
|
|
if recent == signer:
|
|
s.say "applySnapshot signer recently seen ", s.pp(signer)
|
|
echo "+++ applySnapshot #", header.blockNumber, " err=errRecentlySigned"
|
|
return err((errRecentlySigned,""))
|
|
s.recents[number] = signer
|
|
|
|
# Header authorized, discard any previous vote from the signer
|
|
# clique/snapshot.go(233): for i, vote := range snap.Votes {
|
|
s.ballot.delVote(signer = signer, address = header.coinbase)
|
|
|
|
# Tally up the new vote from the signer
|
|
# clique/snapshot.go(244): var authorize bool
|
|
var authOk = false
|
|
if header.nonce == NONCE_AUTH:
|
|
authOk = true
|
|
elif header.nonce != NONCE_DROP:
|
|
return err((errInvalidVote,""))
|
|
let vote = Vote(address: header.coinbase,
|
|
signer: signer,
|
|
blockNumber: number,
|
|
authorize: authOk)
|
|
#s.say "applySnapshot calling addVote ", s.pp(vote)
|
|
# clique/snapshot.go(253): if snap.cast(header.Coinbase, authorize) {
|
|
s.ballot.addVote(vote)
|
|
|
|
# clique/snapshot.go(269): if limit := uint64(len(snap.Signers)/2 [..]
|
|
if s.ballot.isAuthSignersListShrunk:
|
|
# Signer list shrunk, delete any leftover recent caches
|
|
let limit = s.ballot.authSignersThreshold.u256
|
|
if limit <= number:
|
|
# Pop off least block number from the list
|
|
let item = number - limit
|
|
s.say "will delete recent item #", item, " (", number, "-", limit,
|
|
") from recents={", s.pp(s.recents), "}"
|
|
s.recents.del(item)
|
|
|
|
#s.say "applySnapshot state=", s.pp(25)
|
|
|
|
# If we're taking too much time (ecrecover), notify the user once a while
|
|
if s.cfg.logInterval < getTime() - logged:
|
|
info "Reconstructing voting history",
|
|
processed = headersIndex,
|
|
total = headers.len,
|
|
elapsed = getTime() - start
|
|
logged = getTime()
|
|
|
|
let sinceStart = getTime() - start
|
|
if s.cfg.logInterval < sinceStart:
|
|
info "Reconstructed voting history",
|
|
processed = headers.len,
|
|
elapsed = sinceStart
|
|
|
|
# clique/snapshot.go(303): snap.Number += uint64(len(headers))
|
|
doAssert headers[last].blockNumber == s.blockNumber+(1+(last-first).abs).u256
|
|
s.blockNumber = headers[last].blockNumber
|
|
s.blockHash = headers[last].blockHash
|
|
|
|
s.say "applySnapshot ok"
|
|
ok()
|
|
|
|
|
|
proc snapshotApply*(s: Snapshot; headers: var seq[BlockHeader]): CliqueOkResult
|
|
{.gcsafe, raises: [Defect,CatchableError].} =
|
|
if headers.len == 0:
|
|
return ok()
|
|
s.snapshotApplySeq(headers, 0, headers.len - 1)
|
|
|
|
# ------------------------------------------------------------------------------
|
|
# End
|
|
# ------------------------------------------------------------------------------
|