2021-05-28 17:39:55 +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.
|
|
|
|
|
|
|
|
##
|
|
|
|
## Votes Management 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-14 15:13:27 +00:00
|
|
|
std/[sequtils, tables],
|
2021-07-21 13:31:52 +00:00
|
|
|
../clique_helpers,
|
2021-06-14 18:33:57 +00:00
|
|
|
eth/common
|
2021-05-28 17:39:55 +00:00
|
|
|
|
|
|
|
type
|
2022-07-21 18:16:28 +00:00
|
|
|
Vote* = object
|
|
|
|
## Vote represent single votes that an authorized signer made to modify
|
|
|
|
## the list of authorizations.
|
2021-05-28 17:39:55 +00:00
|
|
|
signer*: EthAddress ## authorized signer that cast this vote
|
|
|
|
address*: EthAddress ## account being voted on to change its
|
|
|
|
## authorization type (`true` or `false`)
|
|
|
|
blockNumber*: BlockNumber ## block number the vote was cast in
|
|
|
|
## (expire old votes)
|
|
|
|
authorize*: bool ## authorization type, whether to authorize or
|
|
|
|
## deauthorize the voted account
|
|
|
|
|
2021-06-11 17:26:08 +00:00
|
|
|
Tally = object
|
2021-05-28 17:39:55 +00:00
|
|
|
authorize: bool
|
|
|
|
signers: Table[EthAddress,Vote]
|
|
|
|
|
2021-07-14 15:13:27 +00:00
|
|
|
Ballot* = object
|
2021-05-28 17:39:55 +00:00
|
|
|
votes: Table[EthAddress,Tally] ## votes by account -> signer
|
|
|
|
authSig: Table[EthAddress,bool] ## currently authorised signers
|
|
|
|
authRemoved: bool ## last `addVote()` action was removing an
|
|
|
|
## authorised signer from the `authSig` list
|
|
|
|
|
2023-01-30 22:10:23 +00:00
|
|
|
{.push raises: [].}
|
2021-06-14 18:33:57 +00:00
|
|
|
|
2021-05-28 17:39:55 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
2021-07-14 15:13:27 +00:00
|
|
|
# Public debugging/pretty-printer support
|
2021-05-28 17:39:55 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2021-07-14 15:13:27 +00:00
|
|
|
proc votesInternal*(t: var Ballot): seq[(EthAddress,EthAddress,Vote)] =
|
|
|
|
for account,tally in t.votes.pairs:
|
|
|
|
for signer,vote in tally.signers.pairs:
|
|
|
|
result.add (account, signer, vote)
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public constructor
|
|
|
|
# ------------------------------------------------------------------------------
|
2021-06-14 18:33:57 +00:00
|
|
|
|
2021-07-14 15:13:27 +00:00
|
|
|
proc initBallot*(t: var Ballot) =
|
|
|
|
## Ininialise an empty `Ballot` descriptor.
|
2021-05-28 17:39:55 +00:00
|
|
|
t.votes = initTable[EthAddress,Tally]()
|
|
|
|
t.authSig = initTable[EthAddress,bool]()
|
|
|
|
|
2021-07-14 15:13:27 +00:00
|
|
|
proc initBallot*(t: var Ballot; signers: openArray[EthAddress]) =
|
|
|
|
## Ininialise `Ballot` with a given authorised signers list
|
|
|
|
t.initBallot
|
2021-05-28 17:39:55 +00:00
|
|
|
for a in signers:
|
|
|
|
t.authSig[a] = true
|
|
|
|
|
2021-07-14 15:13:27 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public getters
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
proc authSigners*(t: var Ballot): seq[EthAddress] =
|
2021-05-28 17:39:55 +00:00
|
|
|
## Sorted ascending list of authorised signer addresses
|
2021-06-04 17:20:37 +00:00
|
|
|
toSeq(t.authSig.keys).sorted(EthAscending)
|
2021-05-28 17:39:55 +00:00
|
|
|
|
2021-07-30 14:06:51 +00:00
|
|
|
proc authSignersLen*(t: var Ballot): int =
|
|
|
|
## Returns the number of currently known authorised signers.
|
|
|
|
t.authSig.len
|
|
|
|
|
2021-07-14 15:13:27 +00:00
|
|
|
proc isAuthSignersListShrunk*(t: var Ballot): bool =
|
2021-05-28 17:39:55 +00:00
|
|
|
## Check whether the authorised signers list was shrunk recently after
|
|
|
|
## appying `addVote()`
|
|
|
|
t.authRemoved
|
|
|
|
|
2021-07-14 15:13:27 +00:00
|
|
|
proc authSignersThreshold*(t: var Ballot): int =
|
2021-05-28 17:39:55 +00:00
|
|
|
## Returns the minimum number of authorised signers needed for authorising
|
|
|
|
## a addres for voting. This is currently
|
|
|
|
## ::
|
|
|
|
## 1 + half of the number of authorised signers
|
|
|
|
##
|
|
|
|
1 + (t.authSig.len div 2)
|
|
|
|
|
2021-07-14 15:13:27 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public functions
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2021-07-30 14:06:51 +00:00
|
|
|
proc isAuthSigner*(t: var Ballot; addresses: var seq[EthAddress]): bool =
|
|
|
|
## Check whether all `addresses` entries are authorised signers.
|
|
|
|
##
|
|
|
|
## Using this function should be preferable over `authSigners()` which has
|
|
|
|
## complexity `O(log n)` while this function runs with `O(n)`.
|
|
|
|
for a in addresses:
|
|
|
|
if a notin t.authSig:
|
|
|
|
return false
|
|
|
|
true
|
|
|
|
|
2021-07-14 15:13:27 +00:00
|
|
|
proc isAuthSigner*(t: var Ballot; address: EthAddress): bool =
|
|
|
|
## Check whether `address` is an authorised signer
|
|
|
|
address in t.authSig
|
2021-05-28 17:39:55 +00:00
|
|
|
|
2021-07-14 15:13:27 +00:00
|
|
|
proc delVote*(t: var Ballot; signer, address: EthAddress) {.
|
2023-01-30 22:10:23 +00:00
|
|
|
gcsafe, raises: [KeyError].} =
|
2021-05-28 17:39:55 +00:00
|
|
|
## Remove a particular previously added vote.
|
|
|
|
if address in t.votes:
|
|
|
|
if signer in t.votes[address].signers:
|
|
|
|
if t.votes[address].signers.len <= 1:
|
|
|
|
t.votes.del(address)
|
|
|
|
else:
|
|
|
|
t.votes[address].signers.del(signer)
|
|
|
|
|
|
|
|
|
2021-07-14 15:13:27 +00:00
|
|
|
proc flushVotes*(t: var Ballot) =
|
2021-06-16 08:07:18 +00:00
|
|
|
## Reset/flush pending votes, authorised signers remain the same.
|
|
|
|
t.votes.clear
|
|
|
|
|
|
|
|
|
2021-05-28 17:39:55 +00:00
|
|
|
# clique/snapshot.go(141): func (s *Snapshot) validVote(address [..]
|
2021-07-14 15:13:27 +00:00
|
|
|
proc isValidVote*(t: var Ballot; address: EthAddress; authorize: bool): bool =
|
2021-05-28 17:39:55 +00:00
|
|
|
## Check whether voting would have an effect in `addVote()`
|
|
|
|
if address in t.authSig: not authorize else: authorize
|
|
|
|
|
|
|
|
|
2021-07-14 15:13:27 +00:00
|
|
|
proc addVote*(t: var Ballot; vote: Vote) {.
|
2023-01-30 22:10:23 +00:00
|
|
|
gcsafe, raises: [KeyError].} =
|
2021-05-28 17:39:55 +00:00
|
|
|
## Add a new vote collecting the signers for the particular voting address.
|
|
|
|
##
|
|
|
|
## Unless it is the first vote for this address, the authorisation type
|
|
|
|
## `true` or `false` of the vote must match the previous one. For the first
|
|
|
|
## vote, the authorisation type `true` is accepted if the address is not an
|
|
|
|
## authorised signer, and `false` if it is an authorised signer. Otherwise
|
|
|
|
## the vote is ignored.
|
|
|
|
##
|
|
|
|
## If the number of signers for the particular address are at least
|
|
|
|
## `authSignersThreshold()`, the status of this address will change as
|
|
|
|
## follows.
|
|
|
|
## * If the authorisation type is `true`, the address is added
|
|
|
|
## to the list of authorised signers.
|
|
|
|
## * If the authorisation type is `false`, the address is removed
|
|
|
|
## from the list of authorised signers.
|
|
|
|
t.authRemoved = false
|
2021-06-14 18:33:57 +00:00
|
|
|
var
|
|
|
|
numVotes = 0
|
|
|
|
authOk = vote.authorize
|
2021-06-11 17:26:08 +00:00
|
|
|
|
2021-05-28 17:39:55 +00:00
|
|
|
# clique/snapshot.go(147): if !s.validVote(address, [..]
|
2021-07-14 15:13:27 +00:00
|
|
|
if not t.isValidVote(vote.address, vote.authorize):
|
2021-05-28 17:39:55 +00:00
|
|
|
|
2021-06-14 18:33:57 +00:00
|
|
|
# Corner case: touch votes for this account
|
|
|
|
if t.votes.hasKey(vote.address):
|
|
|
|
let refVote = t.votes[vote.address]
|
|
|
|
numVotes = refVote.signers.len
|
|
|
|
authOk = refVote.authorize
|
2021-05-28 17:39:55 +00:00
|
|
|
|
2021-06-14 18:33:57 +00:00
|
|
|
elif not t.votes.hasKey(vote.address):
|
|
|
|
# Collect inital vote
|
2021-06-11 17:26:08 +00:00
|
|
|
t.votes[vote.address] = Tally(
|
|
|
|
authorize: vote.authorize,
|
|
|
|
signers: {vote.signer: vote}.toTable)
|
2021-05-28 17:39:55 +00:00
|
|
|
numVotes = 1
|
2021-06-14 18:33:57 +00:00
|
|
|
|
2021-05-28 17:39:55 +00:00
|
|
|
elif t.votes[vote.address].authorize == vote.authorize:
|
2021-06-14 18:33:57 +00:00
|
|
|
# Collect additional vote
|
2021-05-28 17:39:55 +00:00
|
|
|
t.votes[vote.address].signers[vote.signer] = vote
|
|
|
|
numVotes = t.votes[vote.address].signers.len
|
2021-06-14 18:33:57 +00:00
|
|
|
|
2021-05-28 17:39:55 +00:00
|
|
|
else:
|
|
|
|
return
|
|
|
|
|
|
|
|
# clique/snapshot.go(262): if tally := snap.Tally[header.Coinbase]; [..]
|
|
|
|
|
|
|
|
# Vote passed, update the list of authorised signers if enough votes
|
|
|
|
if numVotes < t.authSignersThreshold:
|
|
|
|
return
|
|
|
|
|
|
|
|
var obsolete = @[vote.address]
|
2021-06-14 18:33:57 +00:00
|
|
|
if authOk:
|
2021-05-28 17:39:55 +00:00
|
|
|
# Has minimum votes, so add it
|
|
|
|
t.authSig[vote.address] = true
|
|
|
|
else:
|
|
|
|
# clique/snapshot.go(266): delete(snap.Signers, [..]
|
|
|
|
t.authSig.del(vote.address)
|
|
|
|
t.authRemoved = true
|
|
|
|
|
|
|
|
# Not a signer anymore => remove it everywhere
|
|
|
|
for key,value in t.votes.mpairs:
|
|
|
|
if vote.address in value.signers:
|
|
|
|
if 1 < value.signers.len:
|
|
|
|
value.signers.del(vote.address)
|
|
|
|
else:
|
|
|
|
obsolete.add key
|
|
|
|
|
|
|
|
for key in obsolete:
|
|
|
|
t.votes.del(key)
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# End
|
|
|
|
# ------------------------------------------------------------------------------
|