2021-12-10 08:49:57 +00:00
|
|
|
# Nimbus
|
2024-05-27 19:49:07 +00:00
|
|
|
# Copyright (c) 2018-2024 Status Research & Development GmbH
|
2021-12-10 08:49:57 +00:00
|
|
|
# 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.
|
|
|
|
|
|
|
|
## Block PoW Support (Verifying & Mining)
|
|
|
|
## ======================================
|
|
|
|
##
|
|
|
|
|
2024-05-27 19:49:07 +00:00
|
|
|
{.push raises: [].}
|
|
|
|
|
2021-12-10 08:49:57 +00:00
|
|
|
import
|
|
|
|
std/[options, strutils],
|
2022-12-02 04:35:41 +00:00
|
|
|
../utils/utils,
|
2024-05-27 19:49:07 +00:00
|
|
|
./pow/pow_cache,
|
2021-12-10 08:49:57 +00:00
|
|
|
eth/[common, keys, p2p, rlp],
|
2023-09-13 02:32:38 +00:00
|
|
|
stew/endians2,
|
2021-12-10 08:49:57 +00:00
|
|
|
ethash,
|
|
|
|
stint
|
|
|
|
|
|
|
|
type
|
|
|
|
PowDigest = tuple ##\
|
|
|
|
## Return value from the `hashimotoLight()` function
|
|
|
|
mixDigest: Hash256
|
|
|
|
value: Hash256
|
|
|
|
|
|
|
|
PowSpecs* = object ##\
|
|
|
|
## Relevant block header parts for PoW mining & verifying. This object
|
|
|
|
## might be more useful for testing and debugging than for production.
|
|
|
|
blockNumber*: BlockNumber
|
|
|
|
miningHash*: Hash256
|
2024-05-28 05:10:10 +00:00
|
|
|
nonce: BlockNonce
|
2021-12-10 08:49:57 +00:00
|
|
|
mixDigest*: Hash256
|
2024-05-28 05:10:10 +00:00
|
|
|
difficulty: DifficultyInt
|
2021-12-10 08:49:57 +00:00
|
|
|
|
|
|
|
PowHeader = object ##\
|
|
|
|
## Stolen from `p2p/validate.MiningHeader`
|
|
|
|
parentHash : Hash256
|
|
|
|
ommersHash : Hash256
|
|
|
|
coinbase : EthAddress
|
|
|
|
stateRoot : Hash256
|
|
|
|
txRoot : Hash256
|
|
|
|
receiptRoot : Hash256
|
|
|
|
bloom : common.BloomFilter
|
|
|
|
difficulty : DifficultyInt
|
|
|
|
blockNumber : BlockNumber
|
|
|
|
gasLimit : GasInt
|
|
|
|
gasUsed : GasInt
|
|
|
|
timestamp : EthTime
|
|
|
|
extraData : Blob
|
|
|
|
|
|
|
|
PowRef* = ref object of RootObj ##\
|
|
|
|
## PoW context descriptor
|
|
|
|
lightByEpoch: PowCacheRef ## PoW cache indexed by epoch
|
|
|
|
|
|
|
|
# You should only create one instance of the RNG per application / library
|
|
|
|
# Ref is used so that it can be shared between components
|
2022-07-04 07:38:02 +00:00
|
|
|
rng: ref HmacDrbgContext
|
2021-12-10 08:49:57 +00:00
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Private functions: RLP support
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2024-05-27 19:49:07 +00:00
|
|
|
func append(w: var RlpWriter; specs: PowSpecs) =
|
2021-12-10 08:49:57 +00:00
|
|
|
## RLP support
|
|
|
|
w.startList(5)
|
|
|
|
w.append(HashOrNum(isHash: false, number: specs.blockNumber))
|
|
|
|
w.append(HashOrNum(isHash: true, hash: specs.miningHash))
|
|
|
|
w.append(specs.nonce.toUint)
|
|
|
|
w.append(HashOrNum(isHash: true, hash: specs.mixDigest))
|
|
|
|
w.append(specs.difficulty)
|
|
|
|
|
2024-05-27 19:49:07 +00:00
|
|
|
func read(rlp: var Rlp; Q: type PowSpecs): Q
|
2023-01-30 22:10:23 +00:00
|
|
|
{.raises: [RlpError].} =
|
2021-12-10 08:49:57 +00:00
|
|
|
## RLP support
|
|
|
|
rlp.tryEnterList()
|
|
|
|
result.blockNumber = rlp.read(HashOrNum).number
|
|
|
|
result.miningHash = rlp.read(HashOrNum).hash
|
|
|
|
result.nonce = rlp.read(uint64).toBlockNonce
|
|
|
|
result.mixDigest = rlp.read(HashOrNum).hash
|
|
|
|
result.difficulty = rlp.read(DifficultyInt)
|
|
|
|
|
2024-05-27 19:49:07 +00:00
|
|
|
func rlpTextEncode(specs: PowSpecs): string =
|
2021-12-10 08:49:57 +00:00
|
|
|
"specs #" & $specs.blockNumber & " " & rlp.encode(specs).toHex
|
|
|
|
|
2024-05-27 19:49:07 +00:00
|
|
|
func decodeRlpText(data: string): PowSpecs
|
2023-01-30 22:10:23 +00:00
|
|
|
{.raises: [CatchableError].} =
|
2021-12-10 08:49:57 +00:00
|
|
|
if 180 < data.len and data[0 .. 6] == "specs #":
|
|
|
|
let hexData = data.split
|
|
|
|
if hexData.len == 3:
|
|
|
|
var rlpData = hexData[2].rlpFromHex
|
|
|
|
result = rlpData.read(PowSpecs)
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Private functions
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2024-05-27 19:49:07 +00:00
|
|
|
func miningHash(header: BlockHeader): Hash256 =
|
2021-12-10 08:49:57 +00:00
|
|
|
## Calculate hash from mining relevant fields of the argument `header`
|
|
|
|
let miningHeader = PowHeader(
|
|
|
|
parentHash: header.parentHash,
|
|
|
|
ommersHash: header.ommersHash,
|
|
|
|
coinbase: header.coinbase,
|
|
|
|
stateRoot: header.stateRoot,
|
|
|
|
txRoot: header.txRoot,
|
|
|
|
receiptRoot: header.receiptRoot,
|
|
|
|
bloom: header.bloom,
|
|
|
|
difficulty: header.difficulty,
|
|
|
|
blockNumber: header.blockNumber,
|
|
|
|
gasLimit: header.gasLimit,
|
|
|
|
gasUsed: header.gasUsed,
|
|
|
|
timestamp: header.timestamp,
|
|
|
|
extraData: header.extraData)
|
|
|
|
|
|
|
|
rlp.encode(miningHeader).keccakHash
|
|
|
|
|
|
|
|
# ---------------
|
|
|
|
|
|
|
|
proc init(tm: PowRef;
|
2022-07-04 07:38:02 +00:00
|
|
|
rng: Option[ref HmacDrbgContext];
|
2024-05-27 19:49:07 +00:00
|
|
|
light: Option[PowCacheRef]) =
|
2021-12-10 08:49:57 +00:00
|
|
|
## Constructor
|
|
|
|
if rng.isSome:
|
|
|
|
tm.rng = rng.get
|
|
|
|
else:
|
|
|
|
tm.rng = newRng()
|
|
|
|
|
|
|
|
if light.isSome:
|
|
|
|
tm.lightByEpoch = light.get
|
|
|
|
else:
|
|
|
|
tm.lightByEpoch = PowCacheRef.new
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public functions, Constructor
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2024-05-27 19:49:07 +00:00
|
|
|
proc new*(T: type PowRef; cache: PowCacheRef): T =
|
2021-12-10 08:49:57 +00:00
|
|
|
## Constructor
|
|
|
|
new result
|
2024-05-27 19:49:07 +00:00
|
|
|
result.init(none(ref HmacDrbgContext), some(cache))
|
2021-12-10 08:49:57 +00:00
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public functions
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2024-05-27 19:49:07 +00:00
|
|
|
func getPowSpecs*(header: BlockHeader): PowSpecs =
|
2021-12-10 08:49:57 +00:00
|
|
|
## Extracts relevant parts from the `header` argument that are needed
|
|
|
|
## for mining or pow verification. This function might be more useful for
|
|
|
|
## testing and debugging than for production.
|
|
|
|
PowSpecs(
|
|
|
|
blockNumber: header.blockNumber,
|
|
|
|
miningHash: header.miningHash,
|
|
|
|
nonce: header.nonce,
|
|
|
|
mixDigest: header.mixDigest,
|
|
|
|
difficulty: header.difficulty)
|
|
|
|
|
2024-05-27 19:49:07 +00:00
|
|
|
func getPowCacheLookup*(tm: PowRef;
|
2021-10-05 23:31:35 +00:00
|
|
|
blockNumber: BlockNumber): (uint64, Hash256)
|
2023-01-30 22:10:23 +00:00
|
|
|
{.gcsafe, raises: [KeyError].} =
|
2021-12-10 08:49:57 +00:00
|
|
|
## Returns the pair `(size,digest)` derived from the lookup cache for the
|
|
|
|
## `hashimotoLight()` function for the given block number. The `size` is the
|
|
|
|
## full size of the dataset (the cache represents) as passed on to the
|
|
|
|
## `hashimotoLight()` function. The `digest` is a hash derived from the
|
|
|
|
## cache that would be passed on to `hashimotoLight()`.
|
|
|
|
##
|
|
|
|
## This function is intended for error reporting and might also be useful
|
|
|
|
## for testing and debugging.
|
|
|
|
let ds = tm.lightByEpoch.get(blockNumber)
|
2021-10-05 23:31:35 +00:00
|
|
|
if ds == nil:
|
|
|
|
raise newException(KeyError, "block not found")
|
2021-12-10 08:49:57 +00:00
|
|
|
|
|
|
|
result[0] = ds.size
|
2022-09-03 18:15:35 +00:00
|
|
|
result[1] = withKeccakHash:
|
|
|
|
for a in ds.data:
|
|
|
|
h.update(a.data)
|
2021-12-10 08:49:57 +00:00
|
|
|
|
|
|
|
# ------------------------
|
|
|
|
|
2024-05-27 19:49:07 +00:00
|
|
|
func getPowDigest(tm: PowRef; blockNumber: BlockNumber;
|
|
|
|
powHeaderDigest: Hash256; nonce: BlockNonce): PowDigest =
|
2021-12-10 08:49:57 +00:00
|
|
|
## Calculate the expected value of `header.mixDigest` using the
|
|
|
|
## `hashimotoLight()` library method.
|
|
|
|
let
|
|
|
|
ds = tm.lightByEpoch.get(blockNumber)
|
|
|
|
u64Nonce = uint64.fromBytesBE(nonce)
|
|
|
|
hashimotoLight(ds.size, ds.data, powHeaderDigest, u64Nonce)
|
|
|
|
|
2024-05-27 19:49:07 +00:00
|
|
|
func getPowDigest*(tm: PowRef; header: BlockHeader): PowDigest =
|
2021-12-10 08:49:57 +00:00
|
|
|
## Variant of `getPowDigest()`
|
|
|
|
tm.getPowDigest(header.blockNumber, header.miningHash, header.nonce)
|
|
|
|
|
2024-05-27 19:49:07 +00:00
|
|
|
func getPowDigest*(tm: PowRef; specs: PowSpecs): PowDigest =
|
2021-12-10 08:49:57 +00:00
|
|
|
## Variant of `getPowDigest()`
|
|
|
|
tm.getPowDigest(specs.blockNumber, specs.miningHash, specs.nonce)
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public functions, debugging & testing
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2024-05-27 19:49:07 +00:00
|
|
|
func dumpPowSpecs*(specs: PowSpecs): string =
|
2021-12-10 08:49:57 +00:00
|
|
|
## Text representation of `PowSpecs` argument object
|
|
|
|
specs.rlpTextEncode
|
|
|
|
|
2024-05-27 19:49:07 +00:00
|
|
|
func undumpPowSpecs*(data: string): PowSpecs
|
2023-01-30 22:10:23 +00:00
|
|
|
{.raises: [CatchableError].} =
|
2021-12-10 08:49:57 +00:00
|
|
|
## Recover `PowSpecs` object from text representation
|
|
|
|
data.decodeRlpText
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# End
|
|
|
|
# ------------------------------------------------------------------------------
|