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
|
|
|
|
eth/common,
|
2024-05-30 12:54:03 +00:00
|
|
|
results,
|
2023-08-27 01:23:45 +00:00
|
|
|
../web3_eth_conv,
|
|
|
|
../beacon_engine,
|
2023-12-08 09:35:50 +00:00
|
|
|
web3/execution_types,
|
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,
|
|
|
|
expected: openArray[Web3Hash]): bool =
|
|
|
|
var versionedHashes: seq[common.Hash256]
|
|
|
|
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
|
|
|
|
|
2023-10-24 04:30:48 +00:00
|
|
|
template validateVersion(com, timestamp, version, 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):
|
|
|
|
if version != Version.V4:
|
|
|
|
raise invalidParams("if timestamp is Prague or later, " &
|
2024-05-15 16:22:03 +00:00
|
|
|
"payload must be ExecutionPayloadV4, got ExecutionPayload" & $version)
|
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):
|
|
|
|
if version != Version.V3:
|
|
|
|
raise invalidParams("if timestamp is Cancun or later, " &
|
2024-05-15 16:22:03 +00:00
|
|
|
"payload must be ExecutionPayloadV3, got ExecutionPayload" & $version)
|
2023-08-27 01:23:45 +00:00
|
|
|
|
|
|
|
elif com.isShanghaiOrLater(timestamp):
|
|
|
|
if version != Version.V2:
|
|
|
|
raise invalidParams("if timestamp is Shanghai or later, " &
|
2024-05-15 16:22:03 +00:00
|
|
|
"payload must be ExecutionPayloadV2, got ExecutionPayload" & $version)
|
2023-08-27 01:23:45 +00:00
|
|
|
|
|
|
|
elif version != Version.V1:
|
|
|
|
if com.syncReqRelaxV2:
|
|
|
|
trace "Relaxed mode, treating payload as V1"
|
|
|
|
discard
|
|
|
|
else:
|
|
|
|
raise invalidParams("if timestamp is earlier than Shanghai, " &
|
2024-05-15 16:22:03 +00:00
|
|
|
"payload must be ExecutionPayloadV1, got ExecutionPayload" & $version)
|
2023-08-27 01:23:45 +00:00
|
|
|
|
2024-03-28 11:59:23 +00:00
|
|
|
if apiVersion >= Version.V3:
|
2023-10-24 04:30:48 +00:00
|
|
|
if version != apiVersion:
|
|
|
|
raise invalidParams("newPayload" & $apiVersion &
|
|
|
|
" expect ExecutionPayload" & $apiVersion &
|
|
|
|
" but got ExecutionPayload" & $version)
|
2023-10-23 02:25:03 +00:00
|
|
|
|
2024-03-26 15:13:30 +00:00
|
|
|
template validatePayload(apiVersion, version, payload) =
|
2024-03-28 11:59:23 +00:00
|
|
|
if version >= Version.V2:
|
|
|
|
if payload.withdrawals.isNone:
|
|
|
|
raise invalidParams("newPayload" & $apiVersion &
|
|
|
|
"withdrawals is expected from execution payload")
|
|
|
|
|
2024-04-21 14:44:05 +00:00
|
|
|
if apiVersion >= Version.V3 or version >= 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
|
|
|
|
2024-04-21 14:44:05 +00:00
|
|
|
if apiVersion >= Version.V4 or version >= Version.V4:
|
2024-03-28 11:59:23 +00:00
|
|
|
if payload.depositReceipts.isNone:
|
|
|
|
raise invalidParams("newPayload" & $apiVersion &
|
|
|
|
"depositReceipts is expected from execution payload")
|
|
|
|
if payload.exits.isNone:
|
|
|
|
raise invalidParams("newPayload" & $apiVersion &
|
|
|
|
"exits is expected from execution payload")
|
|
|
|
|
|
|
|
|
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-04-19 19:43:13 +00:00
|
|
|
versionedHashes = none(seq[Web3Hash]),
|
2023-08-27 01:23:45 +00:00
|
|
|
beaconRoot = none(Web3Hash)): PayloadStatusV1 =
|
|
|
|
|
|
|
|
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")
|
|
|
|
|
2023-08-27 01:23:45 +00:00
|
|
|
let
|
|
|
|
com = ben.com
|
|
|
|
db = com.db
|
|
|
|
timestamp = ethTime payload.timestamp
|
|
|
|
version = payload.version
|
|
|
|
|
2024-03-26 15:13:30 +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
|
|
|
|
|
|
|
var header = blockHeader(payload, beaconRoot = ethHash beaconRoot)
|
|
|
|
|
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-04-21 14:44:05 +00:00
|
|
|
let blockHash = ethHash payload.blockHash
|
|
|
|
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.
|
|
|
|
if db.getBlockHeader(blockHash, header):
|
|
|
|
warn "Ignoring already known beacon payload",
|
2023-09-28 06:20:12 +00:00
|
|
|
number = header.blockNumber, 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.
|
|
|
|
var parent: common.BlockHeader
|
|
|
|
if not db.getBlockHeader(header.parentHash, parent):
|
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
|
|
|
|
let ttd = com.ttd.get(high(common.BlockNumber))
|
|
|
|
|
|
|
|
if version == Version.V1:
|
|
|
|
let td = db.getScore(header.parentHash)
|
|
|
|
if (not com.forkGTE(MergeFork)) and td < ttd:
|
|
|
|
warn "Ignoring pre-merge payload",
|
|
|
|
number = header.blockNumber, hash = blockHash, td, ttd
|
|
|
|
return invalidStatus()
|
|
|
|
|
|
|
|
if header.timestamp <= parent.timestamp:
|
|
|
|
warn "Invalid timestamp",
|
2023-11-03 14:41:05 +00:00
|
|
|
number = header.blockNumber, parentNumber = parent.blockNumber,
|
|
|
|
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)
|
|
|
|
|
2023-08-27 01:23:45 +00:00
|
|
|
if not db.haveBlockAndState(header.parentHash):
|
|
|
|
ben.put(blockHash, header)
|
|
|
|
warn "State not available, ignoring new payload",
|
|
|
|
hash = blockHash,
|
|
|
|
number = header.blockNumber
|
|
|
|
let blockHash = latestValidHash(db, parent, ttd)
|
|
|
|
return acceptedStatus(blockHash)
|
|
|
|
|
|
|
|
trace "Inserting block without sethead",
|
|
|
|
hash = blockHash, number = header.blockNumber
|
2024-05-15 03:07:59 +00:00
|
|
|
let body = blockBody(payload)
|
2023-08-27 01:23:45 +00:00
|
|
|
let vres = ben.chain.insertBlockWithoutSetHead(header, body)
|
|
|
|
if vres != ValidationResult.OK:
|
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)
|
|
|
|
return invalidStatus(blockHash, "Failed to insert block")
|
|
|
|
|
|
|
|
# We've accepted a valid payload from the beacon client. Mark the local
|
|
|
|
# chain transitions to notify other subsystems (e.g. downloader) of the
|
|
|
|
# behavioral change.
|
|
|
|
if not ben.ttdReached():
|
|
|
|
ben.reachTTD()
|
|
|
|
# TODO: cancel downloader
|
|
|
|
|
|
|
|
return validStatus(blockHash)
|