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.
|
|
|
|
|
|
|
|
##
|
|
|
|
## Tuoole & Utils 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>`_
|
|
|
|
##
|
|
|
|
## Caveat: Not supporting RLP serialisation encode()/decode()
|
|
|
|
##
|
|
|
|
|
|
|
|
import
|
2021-06-14 18:33:57 +00:00
|
|
|
std/[algorithm, strformat, times],
|
2021-05-28 17:39:55 +00:00
|
|
|
../../chain_config,
|
|
|
|
../../constants,
|
|
|
|
../../db/db_chain,
|
|
|
|
../../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-05-28 17:39:55 +00:00
|
|
|
proc cliqueResultErr*(w: CliqueError): CliqueResult =
|
|
|
|
## 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-07-06 13:14:45 +00:00
|
|
|
proc getBlockHeaderResult*(db: BaseChainDB;
|
2021-06-14 18:33:57 +00:00
|
|
|
number: BlockNumber): Result[BlockHeader,void] {.
|
|
|
|
gcsafe, raises: [Defect,RlpError].} =
|
2021-05-28 17:39:55 +00:00
|
|
|
## Slightly re-phrased dbChain.getBlockHeader(..) command
|
|
|
|
var header: BlockHeader
|
2021-07-06 13:14:45 +00:00
|
|
|
if db_chain.getBlockHeader(db, number, header):
|
2021-05-28 17:39:55 +00:00
|
|
|
return ok(header)
|
2021-06-14 18:33:57 +00:00
|
|
|
err()
|
2021-05-28 17:39:55 +00:00
|
|
|
|
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.
|
|
|
|
EthBlock(
|
|
|
|
header: header,
|
|
|
|
txs: b.txs,
|
|
|
|
uncles: b.uncles)
|
|
|
|
|
|
|
|
# consensus/misc/forks.go(30): func VerifyForkHashes(config [..]
|
2021-06-14 18:33:57 +00:00
|
|
|
proc verifyForkHashes*(c: var ChainConfig; header: BlockHeader): CliqueResult {.
|
|
|
|
gcsafe, raises: [Defect,ValueError].} =
|
2021-05-28 17:39:55 +00:00
|
|
|
## Verify that blocks conforming to network hard-forks do have the correct
|
|
|
|
## hashes, to avoid clients going off on different chains.
|
|
|
|
|
|
|
|
# If the homestead reprice hash is set, validate it
|
|
|
|
if c.eip150Block.isZero or c.eip150Block != header.blockNumber:
|
|
|
|
return ok()
|
|
|
|
|
|
|
|
let hash = header.hash
|
|
|
|
if c.eip150Hash.isZero or c.eip150Hash == hash:
|
|
|
|
return ok()
|
|
|
|
|
|
|
|
return err((errCliqueGasRepriceFork,
|
|
|
|
&"Homestead gas reprice fork: have {c.eip150Hash}, want {hash}"))
|
|
|
|
|
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
|
|
|
|
# ------------------------------------------------------------------------------
|