2021-08-24 07:34:58 +00:00
|
|
|
# Nimbus
|
2023-08-27 01:23:45 +00:00
|
|
|
# Copyright (c) 2018-2023 Status Research & Development GmbH
|
2021-08-24 07:34:58 +00:00
|
|
|
# Licensed under either of
|
2023-08-27 01:23:45 +00:00
|
|
|
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE))
|
|
|
|
# * MIT license ([LICENSE-MIT](LICENSE-MIT))
|
|
|
|
# at your option.
|
|
|
|
# This file may not be copied, modified, or distributed except according to
|
|
|
|
# those terms.
|
2021-08-24 07:34:58 +00:00
|
|
|
|
|
|
|
import
|
2023-08-27 01:23:45 +00:00
|
|
|
std/[times],
|
2022-03-11 03:48:34 +00:00
|
|
|
pkg/[chronos,
|
|
|
|
stew/results,
|
|
|
|
chronicles,
|
2023-08-27 01:23:45 +00:00
|
|
|
eth/keys],
|
2022-12-02 04:35:41 +00:00
|
|
|
".."/[config,
|
|
|
|
constants],
|
|
|
|
"."/[
|
|
|
|
chain,
|
|
|
|
tx_pool,
|
|
|
|
validate],
|
2023-01-30 22:10:23 +00:00
|
|
|
"."/clique/[
|
2021-08-24 07:34:58 +00:00
|
|
|
clique_desc,
|
|
|
|
clique_cfg,
|
2021-09-11 14:58:01 +00:00
|
|
|
clique_sealer],
|
2022-12-02 04:35:41 +00:00
|
|
|
../utils/utils,
|
2023-08-27 01:23:45 +00:00
|
|
|
../common/[common, context]
|
2023-05-22 10:55:19 +00:00
|
|
|
|
2021-08-24 07:34:58 +00:00
|
|
|
type
|
2021-10-05 23:31:35 +00:00
|
|
|
EngineState* = enum
|
|
|
|
EngineStopped,
|
|
|
|
EngineRunning,
|
|
|
|
EnginePostMerge
|
|
|
|
|
2021-08-24 07:34:58 +00:00
|
|
|
SealingEngineRef* = ref SealingEngineObj
|
|
|
|
SealingEngineObj = object of RootObj
|
|
|
|
state: EngineState
|
|
|
|
engineLoop: Future[void]
|
2022-12-02 04:35:41 +00:00
|
|
|
chain*: ChainRef
|
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-12-02 04:35:41 +00:00
|
|
|
proc validateSealer*(conf: NimbusConf, ctx: EthContext, chain: ChainRef): 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")
|
|
|
|
|
2022-12-02 04:35:41 +00:00
|
|
|
let com = chain.com
|
|
|
|
if com.consensus != ConsensusType.POA:
|
2021-08-24 07:34:58 +00:00
|
|
|
return err("currently only PoA engine is supported")
|
|
|
|
|
|
|
|
ok()
|
|
|
|
|
2022-12-06 05:55:40 +00:00
|
|
|
proc generateBlock(engine: SealingEngineRef,
|
|
|
|
outBlock: var EthBlock): Result[void, string] =
|
2022-04-03 09:38:32 +00:00
|
|
|
|
2022-12-06 05:55:40 +00:00
|
|
|
outBlock = engine.txPool.ethBlock()
|
|
|
|
if engine.chain.com.consensus == ConsensusType.POS:
|
2022-03-21 09:15:52 +00:00
|
|
|
# Stop the block generator if we reach TTD
|
|
|
|
engine.state = EnginePostMerge
|
2021-08-24 07:34:58 +00:00
|
|
|
|
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()
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
# 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
|
|
|
|
2021-09-07 13:45:01 +00:00
|
|
|
proc new*(_: type SealingEngineRef,
|
2022-12-02 04:35:41 +00:00
|
|
|
chain: ChainRef,
|
2021-09-07 13:45:01 +00:00
|
|
|
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"
|