2023-08-27 01:23:45 +00:00
|
|
|
# Nimbus
|
2024-03-26 15:13:30 +00:00
|
|
|
# Copyright (c) 2023-2024 Status Research & Development GmbH
|
2023-08-27 01:23:45 +00:00
|
|
|
# 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
|
2024-05-30 12:54:03 +00:00
|
|
|
results,
|
2023-08-27 01:23:45 +00:00
|
|
|
../web3_eth_conv,
|
2024-10-16 01:34:12 +00:00
|
|
|
eth/common/hashes,
|
2023-08-27 01:23:45 +00:00
|
|
|
../beacon_engine,
|
2024-10-27 22:20:04 +00:00
|
|
|
web3/[execution_types, primitives],
|
2023-08-27 01:23:45 +00:00
|
|
|
../payload_conv,
|
|
|
|
./api_utils,
|
|
|
|
chronicles
|
|
|
|
|
|
|
|
{.push gcsafe, raises:[CatchableError].}
|
|
|
|
|
2024-04-19 19:43:13 +00:00
|
|
|
func validateVersionedHashed(payload: ExecutionPayload,
|
2024-10-16 01:34:12 +00:00
|
|
|
expected: openArray[Hash32]): bool =
|
|
|
|
var versionedHashes: seq[VersionedHash]
|
2024-04-19 19:43:13 +00:00
|
|
|
for x in payload.transactions:
|
|
|
|
let tx = rlp.decode(distinctBase(x), Transaction)
|
|
|
|
versionedHashes.add tx.versionedHashes
|
|
|
|
|
|
|
|
if versionedHashes.len != expected.len:
|
|
|
|
return false
|
|
|
|
|
|
|
|
for i, x in expected:
|
|
|
|
if distinctBase(x) != versionedHashes[i].data:
|
|
|
|
return false
|
|
|
|
true
|
|
|
|
|
2024-10-26 11:10:54 +00:00
|
|
|
template validateVersion(com, timestamp, payloadVersion, apiVersion) =
|
2024-03-28 11:59:23 +00:00
|
|
|
if apiVersion == Version.V4:
|
|
|
|
if not com.isPragueOrLater(timestamp):
|
|
|
|
raise unsupportedFork("newPayloadV4 expect payload timestamp fall within Prague")
|
|
|
|
|
|
|
|
if com.isPragueOrLater(timestamp):
|
2024-10-26 11:10:54 +00:00
|
|
|
if payloadVersion != Version.V3:
|
2024-03-28 11:59:23 +00:00
|
|
|
raise invalidParams("if timestamp is Prague or later, " &
|
2024-10-26 11:10:54 +00:00
|
|
|
"payload must be ExecutionPayloadV3, got ExecutionPayload" & $payloadVersion)
|
2024-03-28 11:59:23 +00:00
|
|
|
|
2023-10-24 04:30:48 +00:00
|
|
|
if apiVersion == Version.V3:
|
|
|
|
if not com.isCancunOrLater(timestamp):
|
|
|
|
raise unsupportedFork("newPayloadV3 expect payload timestamp fall within Cancun")
|
|
|
|
|
2023-08-27 01:23:45 +00:00
|
|
|
if com.isCancunOrLater(timestamp):
|
2024-10-26 11:10:54 +00:00
|
|
|
if payloadVersion != Version.V3:
|
2023-08-27 01:23:45 +00:00
|
|
|
raise invalidParams("if timestamp is Cancun or later, " &
|
2024-10-26 11:10:54 +00:00
|
|
|
"payload must be ExecutionPayloadV3, got ExecutionPayload" & $payloadVersion)
|
2023-08-27 01:23:45 +00:00
|
|
|
|
|
|
|
elif com.isShanghaiOrLater(timestamp):
|
2024-10-26 11:10:54 +00:00
|
|
|
if payloadVersion != Version.V2:
|
2023-08-27 01:23:45 +00:00
|
|
|
raise invalidParams("if timestamp is Shanghai or later, " &
|
2024-10-26 11:10:54 +00:00
|
|
|
"payload must be ExecutionPayloadV2, got ExecutionPayload" & $payloadVersion)
|
2023-08-27 01:23:45 +00:00
|
|
|
|
2024-10-26 11:10:54 +00:00
|
|
|
elif payloadVersion != Version.V1:
|
2024-05-30 20:30:40 +00:00
|
|
|
raise invalidParams("if timestamp is earlier than Shanghai, " &
|
2024-10-26 11:10:54 +00:00
|
|
|
"payload must be ExecutionPayloadV1, got ExecutionPayload" & $payloadVersion)
|
2023-08-27 01:23:45 +00:00
|
|
|
|
2024-10-26 11:10:54 +00:00
|
|
|
if apiVersion == Version.V3 or apiVersion == Version.V4:
|
|
|
|
# both newPayloadV3 and newPayloadV4 expect ExecutionPayloadV3
|
|
|
|
if payloadVersion != Version.V3:
|
2023-10-24 04:30:48 +00:00
|
|
|
raise invalidParams("newPayload" & $apiVersion &
|
2024-10-26 11:10:54 +00:00
|
|
|
" expect ExecutionPayload3" &
|
|
|
|
" but got ExecutionPayload" & $payloadVersion)
|
2023-10-23 02:25:03 +00:00
|
|
|
|
2024-10-26 11:10:54 +00:00
|
|
|
template validatePayload(apiVersion, payloadVersion, payload) =
|
|
|
|
if payloadVersion >= Version.V2:
|
2024-03-28 11:59:23 +00:00
|
|
|
if payload.withdrawals.isNone:
|
|
|
|
raise invalidParams("newPayload" & $apiVersion &
|
|
|
|
"withdrawals is expected from execution payload")
|
|
|
|
|
2024-10-26 11:10:54 +00:00
|
|
|
if apiVersion >= Version.V3 or payloadVersion >= Version.V3:
|
2024-03-26 15:13:30 +00:00
|
|
|
if payload.blobGasUsed.isNone:
|
|
|
|
raise invalidParams("newPayload" & $apiVersion &
|
|
|
|
"blobGasUsed is expected from execution payload")
|
|
|
|
if payload.excessBlobGas.isNone:
|
|
|
|
raise invalidParams("newPayload" & $apiVersion &
|
|
|
|
"excessBlobGas is expected from execution payload")
|
2023-10-23 02:25:03 +00:00
|
|
|
|
2023-08-27 01:23:45 +00:00
|
|
|
proc newPayload*(ben: BeaconEngineRef,
|
2023-10-24 04:30:48 +00:00
|
|
|
apiVersion: Version,
|
2023-08-27 01:23:45 +00:00
|
|
|
payload: ExecutionPayload,
|
2024-10-16 01:34:12 +00:00
|
|
|
versionedHashes = Opt.none(seq[Hash32]),
|
2024-10-18 09:38:18 +00:00
|
|
|
beaconRoot = Opt.none(Hash32),
|
|
|
|
executionRequests = Opt.none(array[3, seq[byte]])): PayloadStatusV1 =
|
2023-08-27 01:23:45 +00:00
|
|
|
|
|
|
|
trace "Engine API request received",
|
|
|
|
meth = "newPayload",
|
|
|
|
number = payload.blockNumber,
|
|
|
|
hash = payload.blockHash
|
|
|
|
|
2024-04-19 19:43:13 +00:00
|
|
|
if apiVersion >= Version.V3:
|
2023-10-23 13:59:57 +00:00
|
|
|
if beaconRoot.isNone:
|
|
|
|
raise invalidParams("newPayloadV3 expect beaconRoot but got none")
|
|
|
|
|
2024-10-26 11:10:54 +00:00
|
|
|
if apiVersion >= Version.V4:
|
|
|
|
if executionRequests.isNone:
|
|
|
|
raise invalidParams("newPayload" & $apiVersion &
|
|
|
|
": executionRequests is expected from execution payload")
|
|
|
|
|
2023-08-27 01:23:45 +00:00
|
|
|
let
|
|
|
|
com = ben.com
|
|
|
|
db = com.db
|
|
|
|
timestamp = ethTime payload.timestamp
|
|
|
|
version = payload.version
|
2024-10-18 09:38:18 +00:00
|
|
|
requestsHash = calcRequestsHash(executionRequests)
|
2023-08-27 01:23:45 +00:00
|
|
|
|
2024-10-26 11:10:54 +00:00
|
|
|
validatePayload(apiVersion, version, payload)
|
2024-04-24 00:50:50 +00:00
|
|
|
validateVersion(com, timestamp, version, apiVersion)
|
2024-05-15 03:07:59 +00:00
|
|
|
|
2024-10-18 09:38:18 +00:00
|
|
|
var blk = ethBlock(payload, beaconRoot, requestsHash)
|
2024-10-16 01:34:12 +00:00
|
|
|
template header: Header = blk.header
|
2024-05-15 03:07:59 +00:00
|
|
|
|
2024-04-19 19:43:13 +00:00
|
|
|
if apiVersion >= Version.V3:
|
|
|
|
if versionedHashes.isNone:
|
|
|
|
raise invalidParams("newPayload" & $apiVersion &
|
|
|
|
" expect blobVersionedHashes but got none")
|
|
|
|
if not validateVersionedHashed(payload, versionedHashes.get):
|
|
|
|
return invalidStatus(header.parentHash, "invalid blob versionedHashes")
|
2024-05-15 03:07:59 +00:00
|
|
|
|
2024-10-16 01:34:12 +00:00
|
|
|
let blockHash = payload.blockHash
|
2024-04-21 14:44:05 +00:00
|
|
|
header.validateBlockHash(blockHash, version).isOkOr:
|
|
|
|
return error
|
2024-05-15 03:07:59 +00:00
|
|
|
|
2023-08-27 01:23:45 +00:00
|
|
|
# If we already have the block locally, ignore the entire execution and just
|
|
|
|
# return a fake success.
|
2024-09-04 09:54:54 +00:00
|
|
|
if ben.chain.haveBlockLocally(blockHash):
|
2023-08-27 01:23:45 +00:00
|
|
|
warn "Ignoring already known beacon payload",
|
2024-06-14 07:31:08 +00:00
|
|
|
number = header.number, hash = blockHash.short
|
2023-08-27 01:23:45 +00:00
|
|
|
return validStatus(blockHash)
|
|
|
|
|
2024-05-17 01:38:46 +00:00
|
|
|
# If this block was rejected previously, keep rejecting it
|
|
|
|
let res = ben.checkInvalidAncestor(blockHash, blockHash)
|
|
|
|
if res.isSome:
|
|
|
|
return res.get
|
|
|
|
|
2023-08-27 01:23:45 +00:00
|
|
|
# If the parent is missing, we - in theory - could trigger a sync, but that
|
|
|
|
# would also entail a reorg. That is problematic if multiple sibling blocks
|
|
|
|
# are being fed to us, and even moreso, if some semi-distant uncle shortens
|
|
|
|
# our live chain. As such, payload execution will not permit reorgs and thus
|
|
|
|
# will not trigger a sync cycle. That is fine though, if we get a fork choice
|
|
|
|
# update after legit payload executions.
|
2024-09-04 09:54:54 +00:00
|
|
|
let parent = ben.chain.headerByHash(header.parentHash).valueOr:
|
2024-05-17 01:38:46 +00:00
|
|
|
return ben.delayPayloadImport(header)
|
2023-08-27 01:23:45 +00:00
|
|
|
|
|
|
|
# We have an existing parent, do some sanity checks to avoid the beacon client
|
|
|
|
# triggering too early
|
2024-06-14 07:31:08 +00:00
|
|
|
let ttd = com.ttd.get(high(UInt256))
|
2023-08-27 01:23:45 +00:00
|
|
|
|
|
|
|
if version == Version.V1:
|
2024-07-17 10:05:53 +00:00
|
|
|
let ptd = db.getScore(header.parentHash).valueOr:
|
2024-06-14 05:10:00 +00:00
|
|
|
0.u256
|
2024-07-17 10:05:53 +00:00
|
|
|
let gptd = db.getScore(parent.parentHash)
|
|
|
|
if ptd < ttd:
|
2023-08-27 01:23:45 +00:00
|
|
|
warn "Ignoring pre-merge payload",
|
2024-07-17 10:05:53 +00:00
|
|
|
number = header.number, hash = blockHash.short, ptd, ttd
|
|
|
|
return invalidStatus()
|
|
|
|
if parent.difficulty > 0.u256 and gptd.isSome and gptd.value >= ttd:
|
|
|
|
warn "Ignoring pre-merge parent block",
|
|
|
|
number = header.number, hash = blockHash.short, ptd, ttd
|
2023-08-27 01:23:45 +00:00
|
|
|
return invalidStatus()
|
|
|
|
|
|
|
|
if header.timestamp <= parent.timestamp:
|
|
|
|
warn "Invalid timestamp",
|
2024-06-14 07:31:08 +00:00
|
|
|
number = header.number, parentNumber = parent.number,
|
2023-11-03 14:41:05 +00:00
|
|
|
parent = parent.timestamp, header = header.timestamp
|
|
|
|
return invalidStatus(parent.blockHash, "Invalid timestamp")
|
2023-08-27 01:23:45 +00:00
|
|
|
|
2024-05-17 01:38:46 +00:00
|
|
|
# Another corner case: if the node is in snap sync mode, but the CL client
|
|
|
|
# tries to make it import a block. That should be denied as pushing something
|
|
|
|
# into the database directly will conflict with the assumptions of snap sync
|
|
|
|
# that it has an empty db that it can fill itself.
|
|
|
|
when false:
|
|
|
|
if api.eth.SyncMode() != downloader.FullSync:
|
|
|
|
return api.delayPayloadImport(header)
|
|
|
|
|
2024-09-04 09:54:54 +00:00
|
|
|
if not ben.chain.haveBlockAndState(header.parentHash):
|
2023-08-27 01:23:45 +00:00
|
|
|
ben.put(blockHash, header)
|
|
|
|
warn "State not available, ignoring new payload",
|
|
|
|
hash = blockHash,
|
2024-06-14 07:31:08 +00:00
|
|
|
number = header.number
|
2023-08-27 01:23:45 +00:00
|
|
|
let blockHash = latestValidHash(db, parent, ttd)
|
|
|
|
return acceptedStatus(blockHash)
|
|
|
|
|
|
|
|
trace "Inserting block without sethead",
|
2024-06-14 07:31:08 +00:00
|
|
|
hash = blockHash, number = header.number
|
2024-09-04 09:54:54 +00:00
|
|
|
let vres = ben.chain.importBlock(blk)
|
2024-05-31 07:13:56 +00:00
|
|
|
if vres.isErr:
|
2024-11-02 09:30:45 +00:00
|
|
|
warn "Error importing block",
|
|
|
|
number = header.number,
|
|
|
|
hash = blockHash.short,
|
|
|
|
parent = header.parentHash.short,
|
|
|
|
error = vres.error()
|
2024-05-17 01:38:46 +00:00
|
|
|
ben.setInvalidAncestor(header, blockHash)
|
2023-08-27 01:23:45 +00:00
|
|
|
let blockHash = latestValidHash(db, parent, ttd)
|
2024-05-31 07:13:56 +00:00
|
|
|
return invalidStatus(blockHash, vres.error())
|
2023-08-27 01:23:45 +00:00
|
|
|
|
2024-10-27 22:20:04 +00:00
|
|
|
info "New payload received and validated",
|
2024-10-29 05:01:59 +00:00
|
|
|
number = header.number,
|
|
|
|
hash = blockHash.short,
|
2024-10-27 22:20:04 +00:00
|
|
|
parent = header.parentHash.short,
|
|
|
|
txs = blk.transactions.len,
|
|
|
|
gasUsed = header.gasUsed,
|
|
|
|
blobGas = header.blobGasUsed.get(0'u64)
|
|
|
|
|
2023-08-27 01:23:45 +00:00
|
|
|
return validStatus(blockHash)
|