2023-08-27 01:23:45 +00:00
|
|
|
# Nimbus
|
|
|
|
# Copyright (c) 2023 Status Research & Development GmbH
|
|
|
|
# Licensed under either of
|
|
|
|
# * 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.
|
|
|
|
|
|
|
|
import
|
|
|
|
./web3_eth_conv,
|
|
|
|
./payload_conv,
|
2023-12-08 09:35:50 +00:00
|
|
|
web3/execution_types,
|
2023-08-27 01:23:45 +00:00
|
|
|
./merge_tracker,
|
|
|
|
./payload_queue,
|
|
|
|
../db/core_db,
|
|
|
|
../core/[tx_pool, casper, chain],
|
|
|
|
../common/common
|
|
|
|
|
|
|
|
export
|
|
|
|
common,
|
|
|
|
chain
|
|
|
|
|
|
|
|
type
|
|
|
|
BeaconEngineRef* = ref object
|
|
|
|
txPool: TxPoolRef
|
2023-09-28 06:20:12 +00:00
|
|
|
merge : MergeTracker
|
2023-08-27 01:23:45 +00:00
|
|
|
queue : PayloadQueue
|
|
|
|
chain : ChainRef
|
|
|
|
|
|
|
|
{.push gcsafe, raises:[].}
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Private helpers
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2023-10-19 00:50:07 +00:00
|
|
|
proc setWithdrawals(ctx: CasperRef, attrs: PayloadAttributes) =
|
2023-08-27 01:23:45 +00:00
|
|
|
case attrs.version
|
|
|
|
of Version.V2, Version.V3:
|
2023-10-19 00:50:07 +00:00
|
|
|
ctx.withdrawals = ethWithdrawals attrs.withdrawals.get
|
2023-08-27 01:23:45 +00:00
|
|
|
else:
|
2023-10-19 00:50:07 +00:00
|
|
|
ctx.withdrawals = @[]
|
2023-08-27 01:23:45 +00:00
|
|
|
|
|
|
|
template wrapException(body: untyped): auto =
|
|
|
|
try:
|
|
|
|
body
|
|
|
|
except CatchableError as ex:
|
|
|
|
err(ex.msg)
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Constructors
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
proc new*(_: type BeaconEngineRef,
|
|
|
|
txPool: TxPoolRef,
|
|
|
|
chain: ChainRef): BeaconEngineRef =
|
|
|
|
BeaconEngineRef(
|
|
|
|
txPool: txPool,
|
2023-09-28 06:20:12 +00:00
|
|
|
merge : MergeTracker.init(txPool.com.db),
|
2023-08-27 01:23:45 +00:00
|
|
|
queue : PayloadQueue(),
|
|
|
|
chain : chain,
|
|
|
|
)
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public functions, setters
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
proc reachTTD*(ben: BeaconEngineRef) =
|
|
|
|
## ReachTTD is called whenever the first NewHead message received
|
|
|
|
## from the consensus-layer.
|
|
|
|
ben.merge.reachTTD()
|
|
|
|
|
|
|
|
proc finalizePoS*(ben: BeaconEngineRef) =
|
|
|
|
## FinalizePoS is called whenever the first FinalisedBlock message received
|
|
|
|
## from the consensus-layer.
|
|
|
|
ben.merge.finalizePos()
|
|
|
|
|
|
|
|
proc put*(ben: BeaconEngineRef,
|
|
|
|
hash: common.Hash256, header: common.BlockHeader) =
|
|
|
|
ben.queue.put(hash, header)
|
|
|
|
|
|
|
|
proc put*(ben: BeaconEngineRef, id: PayloadID,
|
|
|
|
blockValue: UInt256, payload: ExecutionPayload) =
|
|
|
|
ben.queue.put(id, blockValue, payload)
|
|
|
|
|
|
|
|
proc put*(ben: BeaconEngineRef, id: PayloadID,
|
|
|
|
blockValue: UInt256, payload: SomeExecutionPayload) =
|
|
|
|
ben.queue.put(id, blockValue, payload)
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public functions, getters
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
func com*(ben: BeaconEngineRef): CommonRef =
|
|
|
|
ben.txPool.com
|
|
|
|
|
|
|
|
func chain*(ben: BeaconEngineRef): ChainRef =
|
|
|
|
ben.chain
|
|
|
|
|
|
|
|
func ttdReached*(ben: BeaconEngineRef): bool =
|
|
|
|
## TTDReached reports whether the chain has left the PoW stage.
|
|
|
|
ben.merge.ttdReached
|
|
|
|
|
|
|
|
func posFinalized*(ben: BeaconEngineRef): bool =
|
|
|
|
## PoSFinalized reports whether the chain has entered the PoS stage.
|
|
|
|
ben.merge.posFinalized
|
|
|
|
|
|
|
|
func blockValue*(ben: BeaconEngineRef): UInt256 =
|
|
|
|
## return sum of reward for feeRecipient for each
|
|
|
|
## tx included in a block
|
|
|
|
ben.txPool.blockValue
|
|
|
|
|
|
|
|
proc get*(ben: BeaconEngineRef, hash: common.Hash256,
|
|
|
|
header: var common.BlockHeader): bool =
|
|
|
|
ben.queue.get(hash, header)
|
|
|
|
|
|
|
|
proc get*(ben: BeaconEngineRef, id: PayloadID,
|
|
|
|
blockValue: var UInt256,
|
|
|
|
payload: var ExecutionPayload): bool =
|
|
|
|
ben.queue.get(id, blockValue, payload)
|
|
|
|
|
|
|
|
proc get*(ben: BeaconEngineRef, id: PayloadID,
|
|
|
|
blockValue: var UInt256,
|
|
|
|
payload: var ExecutionPayloadV1): bool =
|
|
|
|
ben.queue.get(id, blockValue, payload)
|
|
|
|
|
|
|
|
proc get*(ben: BeaconEngineRef, id: PayloadID,
|
|
|
|
blockValue: var UInt256,
|
|
|
|
payload: var ExecutionPayloadV2): bool =
|
|
|
|
ben.queue.get(id, blockValue, payload)
|
|
|
|
|
|
|
|
proc get*(ben: BeaconEngineRef, id: PayloadID,
|
|
|
|
blockValue: var UInt256,
|
|
|
|
payload: var ExecutionPayloadV3): bool =
|
|
|
|
ben.queue.get(id, blockValue, payload)
|
|
|
|
|
|
|
|
proc get*(ben: BeaconEngineRef, id: PayloadID,
|
|
|
|
blockValue: var UInt256,
|
|
|
|
payload: var ExecutionPayloadV1OrV2): bool =
|
|
|
|
ben.queue.get(id, blockValue, payload)
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public functions
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
proc generatePayload*(ben: BeaconEngineRef,
|
|
|
|
attrs: PayloadAttributes):
|
|
|
|
Result[ExecutionPayload, string] =
|
|
|
|
wrapException:
|
|
|
|
let
|
|
|
|
xp = ben.txPool
|
|
|
|
db = xp.com.db
|
|
|
|
pos = xp.com.pos
|
|
|
|
headBlock = db.getCanonicalHead()
|
|
|
|
|
|
|
|
pos.prevRandao = ethHash attrs.prevRandao
|
|
|
|
pos.timestamp = ethTime attrs.timestamp
|
|
|
|
pos.feeRecipient = ethAddr attrs.suggestedFeeRecipient
|
2023-08-14 10:26:38 +00:00
|
|
|
|
2023-08-30 16:29:48 +00:00
|
|
|
if attrs.parentBeaconBlockRoot.isSome:
|
|
|
|
pos.parentBeaconBlockRoot = ethHash attrs.parentBeaconBlockRoot.get
|
2023-08-27 01:23:45 +00:00
|
|
|
|
2023-10-19 00:50:07 +00:00
|
|
|
pos.setWithdrawals(attrs)
|
2023-08-27 01:23:45 +00:00
|
|
|
|
|
|
|
if headBlock.blockHash != xp.head.blockHash:
|
|
|
|
# reorg
|
|
|
|
discard xp.smartHead(headBlock)
|
|
|
|
|
2023-10-28 08:35:58 +00:00
|
|
|
if pos.timestamp <= headBlock.timestamp:
|
|
|
|
return err "timestamp must be strictly later than parent"
|
|
|
|
|
2023-08-27 01:23:45 +00:00
|
|
|
# someBaseFee = true: make sure blk.header
|
|
|
|
# have the same blockHash with generated payload
|
2023-11-01 02:24:32 +00:00
|
|
|
let blk = xp.assembleBlock(someBaseFee = true).valueOr:
|
|
|
|
return err(error)
|
|
|
|
|
2023-08-27 01:23:45 +00:00
|
|
|
if blk.header.extraData.len > 32:
|
|
|
|
return err "extraData length should not exceed 32 bytes"
|
|
|
|
|
|
|
|
ok(executionPayload(blk))
|