2021-08-24 07:34:58 +00:00
|
|
|
# Nimbus
|
|
|
|
# Copyright (c) 2021 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.
|
|
|
|
|
|
|
|
import
|
2022-04-03 09:38:32 +00:00
|
|
|
std/[times, tables, typetraits, options],
|
2022-03-11 03:48:34 +00:00
|
|
|
pkg/[chronos,
|
|
|
|
stew/results,
|
|
|
|
stew/byteutils,
|
|
|
|
chronicles,
|
|
|
|
eth/common,
|
|
|
|
eth/keys,
|
|
|
|
eth/rlp],
|
2022-02-22 08:55:04 +00:00
|
|
|
"."/[config,
|
|
|
|
db/db_chain,
|
|
|
|
p2p/chain,
|
|
|
|
constants,
|
|
|
|
utils/header],
|
2021-08-24 07:34:58 +00:00
|
|
|
"."/p2p/clique/[clique_defs,
|
|
|
|
clique_desc,
|
|
|
|
clique_cfg,
|
2021-09-11 14:58:01 +00:00
|
|
|
clique_sealer],
|
2022-01-24 13:08:33 +00:00
|
|
|
./p2p/[gaslimit, validate],
|
2022-01-23 11:39:43 +00:00
|
|
|
"."/[chain_config, utils, context],
|
2022-05-09 14:04:48 +00:00
|
|
|
"."/utils/tx_pool
|
2021-08-24 07:34:58 +00:00
|
|
|
|
2021-10-05 23:31:35 +00:00
|
|
|
from web3/ethtypes as web3types import nil
|
2022-02-22 08:55:04 +00:00
|
|
|
from web3/engine_api_types import PayloadAttributesV1, ExecutionPayloadV1
|
2021-10-05 23:31:35 +00:00
|
|
|
|
2021-08-24 07:34:58 +00:00
|
|
|
type
|
2021-10-05 23:31:35 +00:00
|
|
|
EngineState* = enum
|
|
|
|
EngineStopped,
|
|
|
|
EngineRunning,
|
|
|
|
EnginePostMerge
|
|
|
|
|
|
|
|
Web3BlockHash = web3types.BlockHash
|
|
|
|
Web3Address = web3types.Address
|
|
|
|
Web3Bloom = web3types.FixedBytes[256]
|
|
|
|
Web3Quantity = web3types.Quantity
|
2021-08-24 07:34:58 +00:00
|
|
|
|
|
|
|
SealingEngineRef* = ref SealingEngineObj
|
|
|
|
SealingEngineObj = object of RootObj
|
|
|
|
state: EngineState
|
|
|
|
engineLoop: Future[void]
|
2021-10-05 23:31:35 +00:00
|
|
|
chain*: Chain
|
2021-09-07 13:45:01 +00:00
|
|
|
ctx: EthContext
|
|
|
|
signer: EthAddress
|
2022-01-23 11:39:43 +00:00
|
|
|
txPool: TxPoolRef
|
2021-08-24 07:34:58 +00:00
|
|
|
|
2022-02-22 08:55:04 +00:00
|
|
|
template asEthHash(hash: Web3BlockHash): Hash256 =
|
2021-10-05 23:31:35 +00:00
|
|
|
Hash256(data: distinctBase(hash))
|
|
|
|
|
2021-09-11 14:58:01 +00:00
|
|
|
proc validateSealer*(conf: NimbusConf, ctx: EthContext, chain: Chain): Result[void, string] =
|
2021-08-24 07:34:58 +00:00
|
|
|
if conf.engineSigner == ZERO_ADDRESS:
|
|
|
|
return err("signer address should not zero, use --engine-signer to set signer address")
|
|
|
|
|
2021-09-07 13:45:01 +00:00
|
|
|
let res = ctx.am.getAccount(conf.engineSigner)
|
|
|
|
if res.isErr:
|
2021-08-24 07:34:58 +00:00
|
|
|
return err("signer address not in registered accounts, use --import-key/account to register the account")
|
|
|
|
|
2021-09-07 13:45:01 +00:00
|
|
|
let acc = res.get()
|
2021-08-24 07:34:58 +00:00
|
|
|
if not acc.unlocked:
|
|
|
|
return err("signer account not unlocked, please unlock it first via rpc/password file")
|
|
|
|
|
|
|
|
let chainConf = chain.db.config
|
|
|
|
if not chainConf.poaEngine:
|
|
|
|
return err("currently only PoA engine is supported")
|
|
|
|
|
|
|
|
ok()
|
|
|
|
|
|
|
|
proc isLondon(c: ChainConfig, number: BlockNumber): bool {.inline.} =
|
|
|
|
number >= c.londonBlock
|
|
|
|
|
2022-04-03 09:38:32 +00:00
|
|
|
proc prepareBlock(engine: SealingEngineRef,
|
2021-10-05 23:31:35 +00:00
|
|
|
parent: BlockHeader,
|
2022-04-03 09:38:32 +00:00
|
|
|
time: Time,
|
|
|
|
prevRandao: Hash256): Result[EthBlock, string] =
|
2021-08-24 07:34:58 +00:00
|
|
|
let timestamp = if parent.timestamp >= time:
|
|
|
|
parent.timestamp + 1.seconds
|
|
|
|
else:
|
|
|
|
time
|
|
|
|
|
2022-04-03 09:38:32 +00:00
|
|
|
engine.txPool.prevRandao = prevRandao
|
|
|
|
|
|
|
|
var blk = engine.txPool.ethBlock()
|
|
|
|
|
|
|
|
# TODO: fix txPool GasLimit to match this GasLimit
|
2021-08-24 07:34:58 +00:00
|
|
|
let conf = engine.chain.db.config
|
2022-04-03 09:38:32 +00:00
|
|
|
if isLondon(conf, blk.header.blockNumber):
|
2021-08-24 07:34:58 +00:00
|
|
|
var parentGasLimit = parent.gasLimit
|
|
|
|
if not isLondon(conf, parent.blockNumber):
|
|
|
|
# Bump by 2x
|
|
|
|
parentGasLimit = parent.gasLimit * EIP1559_ELASTICITY_MULTIPLIER
|
|
|
|
# TODO: desiredLimit can be configured by user, gasCeil
|
2022-04-03 09:38:32 +00:00
|
|
|
blk.header.gasLimit = calcGasLimit1559(parentGasLimit, desiredLimit = DEFAULT_GAS_LIMIT)
|
|
|
|
else:
|
|
|
|
blk.header.gasLimit = computeGasLimit(
|
|
|
|
parent.gasUsed,
|
|
|
|
parent.gasLimit,
|
|
|
|
gasFloor = DEFAULT_GAS_LIMIT,
|
|
|
|
gasCeil = DEFAULT_GAS_LIMIT)
|
2021-08-24 07:34:58 +00:00
|
|
|
|
2022-04-03 09:38:32 +00:00
|
|
|
if engine.chain.isBlockAfterTtd(blk.header):
|
|
|
|
blk.header.difficulty = DifficultyInt.zero
|
|
|
|
blk.header.mixDigest = prevRandao
|
|
|
|
blk.header.nonce = default(BlockNonce)
|
|
|
|
blk.header.extraData = @[] # TODO: probably this should be configurable by user?
|
2022-03-21 09:15:52 +00:00
|
|
|
# Stop the block generator if we reach TTD
|
|
|
|
engine.state = EnginePostMerge
|
2022-01-24 13:08:33 +00:00
|
|
|
else:
|
2022-04-03 09:38:32 +00:00
|
|
|
let res = engine.chain.clique.prepare(parent, blk.header)
|
2022-01-24 13:08:33 +00:00
|
|
|
if res.isErr:
|
|
|
|
return err($res.error)
|
2021-10-05 23:31:35 +00:00
|
|
|
|
2022-04-03 09:38:32 +00:00
|
|
|
ok(blk)
|
2021-08-24 07:34:58 +00:00
|
|
|
|
2021-10-05 23:31:35 +00:00
|
|
|
proc generateBlock(engine: SealingEngineRef,
|
|
|
|
parentBlockHeader: BlockHeader,
|
2022-01-24 13:08:33 +00:00
|
|
|
outBlock: var EthBlock,
|
2022-04-03 09:38:32 +00:00
|
|
|
timestamp = getTime(),
|
|
|
|
prevRandao = Hash256()): Result[void, string] =
|
2021-08-24 07:34:58 +00:00
|
|
|
# deviation from standard block generator
|
|
|
|
# - no DAO hard fork
|
|
|
|
# - no local and remote uncles inclusion
|
|
|
|
|
2022-05-29 03:29:56 +00:00
|
|
|
let res = prepareBlock(engine, parentBlockHeader, timestamp, prevRandao)
|
2021-08-24 07:34:58 +00:00
|
|
|
if res.isErr:
|
|
|
|
return err("error prepare header")
|
|
|
|
|
2022-04-03 09:38:32 +00:00
|
|
|
outBlock = res.get()
|
2022-03-21 09:15:52 +00:00
|
|
|
if engine.state != EnginePostMerge:
|
2022-02-22 08:55:04 +00:00
|
|
|
# Post merge, Clique should not be executing
|
2022-01-24 13:08:33 +00:00
|
|
|
let sealRes = engine.chain.clique.seal(outBlock)
|
|
|
|
if sealRes.isErr:
|
|
|
|
return err("error sealing block header: " & $sealRes.error)
|
2021-08-24 07:34:58 +00:00
|
|
|
|
2021-10-05 23:31:35 +00:00
|
|
|
debug "generated block",
|
|
|
|
blockNumber = outBlock.header.blockNumber,
|
|
|
|
blockHash = blockHash(outBlock.header)
|
|
|
|
|
2021-08-24 07:34:58 +00:00
|
|
|
ok()
|
|
|
|
|
2021-10-05 23:31:35 +00:00
|
|
|
proc generateBlock(engine: SealingEngineRef,
|
|
|
|
parentHash: Hash256,
|
2022-01-24 13:08:33 +00:00
|
|
|
outBlock: var EthBlock,
|
2022-04-03 09:38:32 +00:00
|
|
|
timestamp = getTime(),
|
|
|
|
prevRandao = Hash256()): Result[void, string] =
|
2021-10-05 23:31:35 +00:00
|
|
|
var parentBlockHeader: BlockHeader
|
|
|
|
if engine.chain.db.getBlockHeader(parentHash, parentBlockHeader):
|
2022-05-29 03:29:56 +00:00
|
|
|
generateBlock(engine, parentBlockHeader, outBlock, timestamp, prevRandao)
|
2021-10-05 23:31:35 +00:00
|
|
|
else:
|
|
|
|
# TODO:
|
|
|
|
# This hack shouldn't be necessary if the database can find
|
|
|
|
# the genesis block hash in `getBlockHeader`.
|
|
|
|
let maybeGenesisBlock = engine.chain.currentBlock()
|
|
|
|
if parentHash == maybeGenesisBlock.blockHash:
|
2022-05-29 03:29:56 +00:00
|
|
|
generateBlock(engine, maybeGenesisBlock, outBlock, timestamp, prevRandao)
|
2021-10-05 23:31:35 +00:00
|
|
|
else:
|
|
|
|
return err "parent block not found"
|
|
|
|
|
|
|
|
proc generateBlock(engine: SealingEngineRef,
|
2022-01-24 13:08:33 +00:00
|
|
|
outBlock: var EthBlock,
|
2022-04-03 09:38:32 +00:00
|
|
|
timestamp = getTime(),
|
|
|
|
prevRandao = Hash256()): Result[void, string] =
|
2022-05-29 03:29:56 +00:00
|
|
|
generateBlock(engine, engine.chain.currentBlock(),
|
2022-04-03 09:38:32 +00:00
|
|
|
outBlock, timestamp, prevRandao)
|
2021-10-05 23:31:35 +00:00
|
|
|
|
2021-08-24 07:34:58 +00:00
|
|
|
proc sealingLoop(engine: SealingEngineRef): Future[void] {.async.} =
|
|
|
|
let clique = engine.chain.clique
|
|
|
|
|
2021-09-07 13:45:01 +00:00
|
|
|
proc signerFunc(signer: EthAddress, message: openArray[byte]):
|
|
|
|
Result[RawSignature, cstring] {.gcsafe.} =
|
|
|
|
let
|
|
|
|
hashData = keccakHash(message)
|
|
|
|
ctx = engine.ctx
|
|
|
|
acc = ctx.am.getAccount(signer).tryGet()
|
|
|
|
rawSign = sign(acc.privateKey, SkMessage(hashData.data)).toRaw
|
|
|
|
|
|
|
|
ok(rawSign)
|
|
|
|
|
|
|
|
clique.authorize(engine.signer, signerFunc)
|
2021-08-24 07:34:58 +00:00
|
|
|
|
2022-06-14 06:01:51 +00:00
|
|
|
proc diffCalculator(timeStamp: EthTime, parent: BlockHeader): DifficultyInt {.gcsafe, raises:[].} =
|
|
|
|
# pesky Nim effect system
|
|
|
|
try:
|
|
|
|
discard timestamp
|
|
|
|
let rc = clique.calcDifficulty(parent)
|
|
|
|
if rc.isErr:
|
|
|
|
return 0.u256
|
|
|
|
rc.get()
|
|
|
|
except:
|
|
|
|
0.u256
|
|
|
|
|
|
|
|
# switch to PoA difficulty calculator
|
|
|
|
engine.txPool.calcDifficulty = diffCalculator
|
|
|
|
|
2021-08-24 07:34:58 +00:00
|
|
|
# convert times.Duration to chronos.Duration
|
|
|
|
let period = chronos.seconds(clique.cfg.period.inSeconds)
|
|
|
|
|
|
|
|
while engine.state == EngineRunning:
|
|
|
|
# the sealing engine will tick every `cliquePeriod` seconds
|
|
|
|
await sleepAsync(period)
|
|
|
|
|
|
|
|
if engine.state != EngineRunning:
|
|
|
|
break
|
|
|
|
|
|
|
|
# deviation from 'correct' sealing engine:
|
|
|
|
# - no queue for chain reorgs
|
|
|
|
# - no async lock/guard against race with sync algo
|
|
|
|
var blk: EthBlock
|
2022-05-29 03:29:56 +00:00
|
|
|
let blkRes = engine.generateBlock(blk)
|
2021-08-24 07:34:58 +00:00
|
|
|
if blkRes.isErr:
|
|
|
|
error "sealing engine generateBlock error", msg=blkRes.error
|
|
|
|
break
|
|
|
|
|
|
|
|
let res = engine.chain.persistBlocks([blk.header], [
|
|
|
|
BlockBody(transactions: blk.txs, uncles: blk.uncles)
|
|
|
|
])
|
|
|
|
|
|
|
|
if res == ValidationResult.Error:
|
|
|
|
error "sealing engine: persistBlocks error"
|
|
|
|
break
|
|
|
|
|
2022-04-08 08:38:47 +00:00
|
|
|
discard engine.txPool.smartHead(blk.header) # add transactions update jobs
|
2022-04-03 09:38:32 +00:00
|
|
|
info "block generated", number=blk.header.blockNumber
|
2022-03-23 14:06:26 +00:00
|
|
|
|
2022-01-24 13:08:33 +00:00
|
|
|
template unsafeQuantityToInt64(q: web3types.Quantity): int64 =
|
|
|
|
int64 q
|
|
|
|
|
2021-10-05 23:31:35 +00:00
|
|
|
proc generateExecutionPayload*(engine: SealingEngineRef,
|
2022-01-24 13:08:33 +00:00
|
|
|
payloadAttrs: PayloadAttributesV1,
|
|
|
|
payloadRes: var ExecutionPayloadV1): Result[void, string] =
|
2022-04-03 09:38:32 +00:00
|
|
|
let
|
2022-04-11 09:36:22 +00:00
|
|
|
headBlock = try: engine.chain.db.getCanonicalHead()
|
|
|
|
except CatchableError: return err "No head block in database"
|
2022-04-03 09:38:32 +00:00
|
|
|
prevRandao = Hash256(data: distinctBase payloadAttrs.prevRandao)
|
|
|
|
timestamp = fromUnix(payloadAttrs.timestamp.unsafeQuantityToInt64)
|
|
|
|
coinbase = EthAddress payloadAttrs.suggestedFeeRecipient
|
|
|
|
|
2022-04-11 09:36:22 +00:00
|
|
|
if headBlock.blockHash != engine.txPool.head.blockHash:
|
|
|
|
# reorg
|
|
|
|
discard engine.txPool.smartHead(headBlock)
|
|
|
|
|
2021-10-05 23:31:35 +00:00
|
|
|
var blk: EthBlock
|
2022-04-04 02:01:39 +00:00
|
|
|
engine.txPool.feeRecipient = coinbase
|
|
|
|
|
2022-01-24 13:08:33 +00:00
|
|
|
let blkRes = engine.generateBlock(
|
|
|
|
headBlock,
|
|
|
|
blk,
|
2022-04-03 09:38:32 +00:00
|
|
|
timestamp,
|
|
|
|
prevRandao)
|
2022-01-24 13:08:33 +00:00
|
|
|
|
2021-10-05 23:31:35 +00:00
|
|
|
if blkRes.isErr:
|
|
|
|
error "sealing engine generateBlock error", msg = blkRes.error
|
|
|
|
return blkRes
|
|
|
|
|
2022-03-11 03:48:34 +00:00
|
|
|
# make sure both generated block header and payloadRes(ExecutionPayloadV1)
|
|
|
|
# produce the same blockHash
|
2022-04-04 02:01:39 +00:00
|
|
|
doAssert blk.header.coinbase == coinbase
|
2022-04-03 09:38:32 +00:00
|
|
|
blk.header.timestamp = timestamp
|
|
|
|
blk.header.prevRandao = prevRandao
|
2022-03-21 09:15:52 +00:00
|
|
|
blk.header.fee = some(blk.header.fee.get(UInt256.zero)) # force it with some(UInt256)
|
2022-03-11 03:48:34 +00:00
|
|
|
|
|
|
|
let blockHash = rlpHash(blk.header)
|
2022-02-22 08:55:04 +00:00
|
|
|
if blk.header.extraData.len > 32:
|
|
|
|
return err "extraData length should not exceed 32 bytes"
|
|
|
|
|
2021-10-05 23:31:35 +00:00
|
|
|
payloadRes.parentHash = Web3BlockHash blk.header.parentHash.data
|
2022-01-24 13:08:33 +00:00
|
|
|
payloadRes.feeRecipient = Web3Address blk.header.coinbase
|
2021-10-05 23:31:35 +00:00
|
|
|
payloadRes.stateRoot = Web3BlockHash blk.header.stateRoot.data
|
2022-01-24 13:08:33 +00:00
|
|
|
payloadRes.receiptsRoot = Web3BlockHash blk.header.receiptRoot.data
|
2021-10-05 23:31:35 +00:00
|
|
|
payloadRes.logsBloom = Web3Bloom blk.header.bloom
|
2022-04-04 02:01:39 +00:00
|
|
|
payloadRes.prevRandao = payloadAttrs.prevRandao
|
2021-10-05 23:31:35 +00:00
|
|
|
payloadRes.blockNumber = Web3Quantity blk.header.blockNumber.truncate(uint64)
|
|
|
|
payloadRes.gasLimit = Web3Quantity blk.header.gasLimit
|
|
|
|
payloadRes.gasUsed = Web3Quantity blk.header.gasUsed
|
|
|
|
payloadRes.timestamp = payloadAttrs.timestamp
|
2022-04-08 04:54:11 +00:00
|
|
|
payloadRes.extraData = web3types.DynamicBytes[0, 32] blk.header.extraData
|
2021-10-05 23:31:35 +00:00
|
|
|
payloadRes.baseFeePerGas = blk.header.fee.get(UInt256.zero)
|
2022-03-11 03:48:34 +00:00
|
|
|
payloadRes.blockHash = Web3BlockHash blockHash.data
|
2022-02-22 08:55:04 +00:00
|
|
|
|
|
|
|
for tx in blk.txs:
|
|
|
|
let txData = rlp.encode(tx)
|
|
|
|
payloadRes.transactions.add web3types.TypedTransaction(txData)
|
2021-10-05 23:31:35 +00:00
|
|
|
|
|
|
|
return ok()
|
|
|
|
|
2021-09-07 13:45:01 +00:00
|
|
|
proc new*(_: type SealingEngineRef,
|
|
|
|
chain: Chain,
|
|
|
|
ctx: EthContext,
|
2021-10-05 23:31:35 +00:00
|
|
|
signer: EthAddress,
|
2022-01-23 11:39:43 +00:00
|
|
|
txPool: TxPoolRef,
|
2021-10-05 23:31:35 +00:00
|
|
|
initialState: EngineState): SealingEngineRef =
|
2021-08-24 07:34:58 +00:00
|
|
|
SealingEngineRef(
|
2021-09-07 13:45:01 +00:00
|
|
|
chain: chain,
|
|
|
|
ctx: ctx,
|
2021-10-05 23:31:35 +00:00
|
|
|
signer: signer,
|
2022-01-23 11:39:43 +00:00
|
|
|
txPool: txPool,
|
2021-10-05 23:31:35 +00:00
|
|
|
state: initialState
|
2021-08-24 07:34:58 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
proc start*(engine: SealingEngineRef) =
|
|
|
|
## Starts sealing engine.
|
|
|
|
if engine.state == EngineStopped:
|
|
|
|
engine.state = EngineRunning
|
|
|
|
engine.engineLoop = sealingLoop(engine)
|
|
|
|
info "sealing engine started"
|
|
|
|
|
|
|
|
proc stop*(engine: SealingEngineRef) {.async.} =
|
|
|
|
## Stop sealing engine from producing more blocks.
|
|
|
|
if engine.state == EngineRunning:
|
|
|
|
engine.state = EngineStopped
|
|
|
|
await engine.engineLoop.cancelAndWait()
|
|
|
|
info "sealing engine stopped"
|