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
|
2023-01-30 22:10:23 +00:00
|
|
|
std/times,
|
2021-07-14 15:13:27 +00:00
|
|
|
chronicles,
|
2023-01-30 22:10:23 +00:00
|
|
|
eth/common,
|
2022-07-21 18:16:28 +00:00
|
|
|
stew/results,
|
|
|
|
".."/[clique_cfg, clique_defs],
|
|
|
|
"."/[ballot, snapshot_desc]
|
2021-07-14 15:13:27 +00:00
|
|
|
|
2023-01-30 22:10:23 +00:00
|
|
|
{.push raises: [].}
|
2021-07-14 15:13:27 +00:00
|
|
|
|
|
|
|
logScope:
|
|
|
|
topics = "clique PoA snapshot-apply"
|
|
|
|
|
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
|
2023-01-30 22:10:23 +00:00
|
|
|
{.gcsafe, raises: [CatchableError].} =
|
2021-07-14 15:13:27 +00:00
|
|
|
## Initialises an authorization snapshot `snap` by applying the `headers`
|
|
|
|
## to the argument snapshot desciptor `s`.
|
|
|
|
|
|
|
|
# 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
|
|
|
|
|
|
|
|
# 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
|
|
|
|
|
|
|
|
# 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-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
|
|
|
return err((errUnauthorizedSigner,""))
|
|
|
|
|
|
|
|
for recent in s.recents.values:
|
2021-07-27 11:28:05 +00:00
|
|
|
if recent == signer.value:
|
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)
|
|
|
|
# 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.recents.del(item)
|
|
|
|
|
|
|
|
# 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:
|
2022-03-28 08:35:26 +00:00
|
|
|
debug "Reconstructing voting history",
|
2021-07-14 15:13:27 +00:00
|
|
|
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:
|
2022-03-28 08:35:26 +00:00
|
|
|
debug "Reconstructed voting history",
|
2021-07-14 15:13:27 +00:00
|
|
|
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
|
|
|
|
|
|
|
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
|
2023-01-30 22:10:23 +00:00
|
|
|
{.gcsafe, raises: [CatchableError].} =
|
2021-07-21 13:31:52 +00:00
|
|
|
if headers.len == 0:
|
|
|
|
return ok()
|
|
|
|
s.snapshotApplySeq(headers, 0, headers.len - 1)
|
|
|
|
|
2021-07-14 15:13:27 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# End
|
|
|
|
# ------------------------------------------------------------------------------
|