2021-07-14 15:13:27 +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.
|
|
|
|
|
|
|
|
##
|
|
|
|
## 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
|
2021-07-30 14:06:51 +00:00
|
|
|
std/[algorithm, sequtils, strutils, tables, times],
|
2021-07-14 15:13:27 +00:00
|
|
|
../clique_cfg,
|
|
|
|
../clique_defs,
|
|
|
|
./ballot,
|
|
|
|
./snapshot_desc,
|
|
|
|
chronicles,
|
|
|
|
eth/[common, rlp],
|
|
|
|
stew/results
|
|
|
|
|
|
|
|
{.push raises: [Defect].}
|
|
|
|
|
|
|
|
logScope:
|
|
|
|
topics = "clique PoA snapshot-apply"
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
2021-07-30 14:06:51 +00:00
|
|
|
# Private helpers, pretty printing
|
2021-07-14 15:13:27 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2021-07-21 13:31:52 +00:00
|
|
|
proc say(s: Snapshot; v: varargs[string,`$`]) {.inline.} =
|
2021-07-14 15:13:27 +00:00
|
|
|
discard
|
2021-07-30 14:06:51 +00:00
|
|
|
# uncomment body to enable
|
2021-08-03 07:15:32 +00:00
|
|
|
#s.cfg.say v
|
2021-07-30 14:06:51 +00:00
|
|
|
|
|
|
|
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 &= "]"
|
2021-07-14 15:13:27 +00:00
|
|
|
|
2021-07-21 13:31:52 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# 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
|
|
|
|
|
2021-07-14 15:13:27 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public functions
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
# clique/snapshot.go(185): func (s *Snapshot) apply(headers [..]
|
2021-07-21 13:31:52 +00:00
|
|
|
proc snapshotApplySeq*(s: Snapshot; headers: var seq[BlockHeader],
|
|
|
|
first, last: int): CliqueOkResult
|
|
|
|
{.gcsafe, raises: [Defect,CatchableError].} =
|
2021-07-14 15:13:27 +00:00
|
|
|
## Initialises an authorization snapshot `snap` by applying the `headers`
|
|
|
|
## to the argument snapshot desciptor `s`.
|
|
|
|
|
2021-07-30 14:06:51 +00:00
|
|
|
s.say "applySnapshot begin #", s.blockNumber, " + ", headers.pp(first, last)
|
2021-07-14 15:13:27 +00:00
|
|
|
|
|
|
|
# Sanity check that the headers can be applied
|
2021-07-21 13:31:52 +00:00
|
|
|
if headers[first].blockNumber != s.blockNumber + 1:
|
2021-07-14 15:13:27 +00:00
|
|
|
return err((errInvalidVotingChain,""))
|
|
|
|
# clique/snapshot.go(191): for i := 0; i < len(headers)-1; i++ {
|
2021-07-21 13:31:52 +00:00
|
|
|
first.pairWalkIj(last, 1):
|
|
|
|
if headers[j].blockNumber != headers[i].blockNumber+1:
|
2021-07-14 15:13:27 +00:00
|
|
|
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 [..]
|
2021-07-21 13:31:52 +00:00
|
|
|
first.doWalkIt(last):
|
2021-07-14 15:13:27 +00:00
|
|
|
let
|
|
|
|
# headersIndex => also used for logging at the end of this loop
|
2021-07-21 13:31:52 +00:00
|
|
|
headersIndex = it
|
2021-07-14 15:13:27 +00:00
|
|
|
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
|
2021-07-27 11:28:05 +00:00
|
|
|
let signer = s.cfg.ecRecover(header)
|
|
|
|
if signer.isErr:
|
|
|
|
return err((errEcRecover,$signer.error))
|
2021-07-30 14:06:51 +00:00
|
|
|
#s.say "applySnapshot signer=", s.pp(signer)
|
2021-07-14 15:13:27 +00:00
|
|
|
|
2021-07-27 11:28:05 +00:00
|
|
|
if not s.ballot.isAuthSigner(signer.value):
|
2021-07-14 15:13:27 +00:00
|
|
|
s.say "applySnapshot signer not authorised => fail ", s.pp(29)
|
|
|
|
return err((errUnauthorizedSigner,""))
|
|
|
|
|
|
|
|
for recent in s.recents.values:
|
2021-07-27 11:28:05 +00:00
|
|
|
if recent == signer.value:
|
|
|
|
s.say "applySnapshot signer recently seen ", s.pp(signer.value)
|
2021-07-30 14:06:51 +00:00
|
|
|
echo "+++ applySnapshot #", header.blockNumber, " err=errRecentlySigned"
|
2021-07-14 15:13:27 +00:00
|
|
|
return err((errRecentlySigned,""))
|
2021-07-27 11:28:05 +00:00
|
|
|
s.recents[number] = signer.value
|
2021-07-14 15:13:27 +00:00
|
|
|
|
|
|
|
# Header authorized, discard any previous vote from the signer
|
|
|
|
# clique/snapshot.go(233): for i, vote := range snap.Votes {
|
2021-07-27 11:28:05 +00:00
|
|
|
s.ballot.delVote(signer = signer.value, address = header.coinbase)
|
2021-07-14 15:13:27 +00:00
|
|
|
|
|
|
|
# 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,
|
2021-07-27 11:28:05 +00:00
|
|
|
signer: signer.value,
|
2021-07-14 15:13:27 +00:00
|
|
|
blockNumber: number,
|
|
|
|
authorize: authOk)
|
2021-07-30 14:06:51 +00:00
|
|
|
#s.say "applySnapshot calling addVote ", s.pp(vote)
|
2021-07-14 15:13:27 +00:00
|
|
|
# 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)
|
|
|
|
|
2021-07-30 14:06:51 +00:00
|
|
|
#s.say "applySnapshot state=", s.pp(25)
|
2021-07-14 15:13:27 +00:00
|
|
|
|
|
|
|
# If we're taking too much time (ecrecover), notify the user once a while
|
2021-07-21 13:31:52 +00:00
|
|
|
if s.cfg.logInterval < getTime() - logged:
|
2021-07-14 15:13:27 +00:00
|
|
|
info "Reconstructing voting history",
|
|
|
|
processed = headersIndex,
|
|
|
|
total = headers.len,
|
2021-07-21 13:31:52 +00:00
|
|
|
elapsed = getTime() - start
|
2021-07-14 15:13:27 +00:00
|
|
|
logged = getTime()
|
|
|
|
|
2021-07-21 13:31:52 +00:00
|
|
|
let sinceStart = getTime() - start
|
|
|
|
if s.cfg.logInterval < sinceStart:
|
2021-07-14 15:13:27 +00:00
|
|
|
info "Reconstructed voting history",
|
|
|
|
processed = headers.len,
|
|
|
|
elapsed = sinceStart
|
|
|
|
|
|
|
|
# clique/snapshot.go(303): snap.Number += uint64(len(headers))
|
2021-07-30 14:06:51 +00:00
|
|
|
doAssert headers[last].blockNumber == s.blockNumber+(1+(last-first).abs).u256
|
|
|
|
s.blockNumber = headers[last].blockNumber
|
2021-07-21 13:31:52 +00:00
|
|
|
s.blockHash = headers[last].blockHash
|
2021-07-30 14:06:51 +00:00
|
|
|
|
|
|
|
s.say "applySnapshot ok"
|
|
|
|
ok()
|
2021-07-14 15:13:27 +00:00
|
|
|
|
2021-07-21 13:31:52 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
|
2021-07-14 15:13:27 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# End
|
|
|
|
# ------------------------------------------------------------------------------
|