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.
|
|
|
|
|
|
|
|
##
|
2021-08-03 07:15:32 +00:00
|
|
|
## Tools & Utils for Clique PoA Consensus Protocol
|
|
|
|
## ===============================================
|
2021-05-28 17:39:55 +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
|
2021-07-21 13:31:52 +00:00
|
|
|
std/[algorithm, times],
|
2021-05-28 17:39:55 +00:00
|
|
|
../../constants,
|
|
|
|
../../utils,
|
|
|
|
./clique_defs,
|
|
|
|
eth/[common, rlp],
|
2021-06-11 17:26:08 +00:00
|
|
|
stew/[objects, results],
|
2021-06-14 18:33:57 +00:00
|
|
|
stint
|
2021-05-28 17:39:55 +00:00
|
|
|
|
2021-06-04 17:20:37 +00:00
|
|
|
type
|
|
|
|
EthSortOrder* = enum
|
|
|
|
EthDescending = SortOrder.Descending.ord
|
|
|
|
EthAscending = SortOrder.Ascending.ord
|
|
|
|
|
2021-06-14 18:33:57 +00:00
|
|
|
{.push raises: [Defect].}
|
2021-06-04 17:20:37 +00:00
|
|
|
|
2021-05-28 17:39:55 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Private helpers
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
func zeroItem[T](t: typedesc[T]): T {.inline.} =
|
|
|
|
discard
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public functions
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
proc isZero*[T: EthAddress|Hash256|Duration](a: T): bool =
|
|
|
|
## `true` if `a` is all zero
|
|
|
|
a == zeroItem(T)
|
|
|
|
|
2021-06-04 17:20:37 +00:00
|
|
|
proc sorted*(e: openArray[EthAddress]; order = EthAscending): seq[EthAddress] =
|
|
|
|
proc eCmp(x, y: EthAddress): int =
|
|
|
|
for n in 0 ..< x.len:
|
|
|
|
if x[n] < y[n]:
|
|
|
|
return -1
|
|
|
|
elif y[n] < x[n]:
|
|
|
|
return 1
|
|
|
|
e.sorted(cmp = eCmp, order = order.SortOrder)
|
|
|
|
|
|
|
|
|
2021-07-14 15:13:27 +00:00
|
|
|
proc cliqueResultErr*(w: CliqueError): CliqueOkResult =
|
2021-05-28 17:39:55 +00:00
|
|
|
## Return error result (syntactic sugar)
|
|
|
|
err(w)
|
|
|
|
|
|
|
|
|
2021-06-15 16:34:22 +00:00
|
|
|
proc extraDataAddresses*(extraData: Blob): seq[EthAddress] =
|
2021-05-28 17:39:55 +00:00
|
|
|
## Extract signer addresses from extraData header field
|
2021-06-11 17:26:08 +00:00
|
|
|
|
|
|
|
proc toEthAddress(a: openArray[byte]; start: int): EthAddress {.inline.} =
|
|
|
|
toArray(EthAddress.len, a[start ..< start + EthAddress.len])
|
|
|
|
|
|
|
|
if EXTRA_VANITY + EXTRA_SEAL < extraData.len and
|
|
|
|
((extraData.len - (EXTRA_VANITY + EXTRA_SEAL)) mod EthAddress.len) == 0:
|
2021-05-28 17:39:55 +00:00
|
|
|
var addrOffset = EXTRA_VANITY
|
2021-06-11 17:26:08 +00:00
|
|
|
while addrOffset + EthAddress.len <= extraData.len - EXTRA_SEAL:
|
2021-05-28 17:39:55 +00:00
|
|
|
result.add extraData.toEthAddress(addrOffset)
|
|
|
|
addrOffset += EthAddress.len
|
|
|
|
|
2021-06-11 17:26:08 +00:00
|
|
|
|
2021-05-28 17:39:55 +00:00
|
|
|
# core/types/block.go(343): func (b *Block) WithSeal(header [..]
|
|
|
|
proc withHeader*(b: EthBlock; header: BlockHeader): EthBlock =
|
|
|
|
## New block with the data from `b` but the header replaced with the
|
|
|
|
## argument one.
|
2021-07-14 15:13:27 +00:00
|
|
|
EthBlock(header: header,
|
|
|
|
txs: b.txs,
|
|
|
|
uncles: b.uncles)
|
2021-05-28 17:39:55 +00:00
|
|
|
|
2021-06-04 17:20:37 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Seal hash support
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
# clique/clique.go(730): func encodeSigHeader(w [..]
|
|
|
|
proc encodeSealHeader*(header: BlockHeader): seq[byte] =
|
|
|
|
## Cut sigature off `extraData` header field and consider new `baseFee`
|
|
|
|
## field for Eip1559.
|
|
|
|
doAssert EXTRA_SEAL < header.extraData.len
|
|
|
|
|
|
|
|
var rlpHeader = header
|
|
|
|
rlpHeader.extraData.setLen(header.extraData.len - EXTRA_SEAL)
|
|
|
|
|
2021-06-27 04:19:43 +00:00
|
|
|
rlp.encode(rlpHeader)
|
2021-06-04 17:20:37 +00:00
|
|
|
|
|
|
|
# clique/clique.go(688): func SealHash(header *types.Header) common.Hash {
|
|
|
|
proc hashSealHeader*(header: BlockHeader): Hash256 =
|
|
|
|
## Returns the hash of a block prior to it being sealed.
|
|
|
|
header.encodeSealHeader.keccakHash
|
|
|
|
|
2021-05-28 17:39:55 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# End
|
|
|
|
# ------------------------------------------------------------------------------
|