Skip to content

Commit

Permalink
Fix Cancun validation in Engine API and TxPool
Browse files Browse the repository at this point in the history
  • Loading branch information
jangko committed Oct 23, 2023
1 parent 43b4796 commit f6c64a0
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 26 deletions.
2 changes: 1 addition & 1 deletion nimbus/beacon/api_handler/api_forkchoice.nim
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ template validateVersion(attrsOpt, com, expectedVersion) =
raise invalidParams("if timestamp is earlier than Shanghai," &
" payloadAttributes must be PayloadAttributesV1")

if version != expectedVersion:
if expectedVersion == Version.V3 and version != expectedVersion:
raise invalidParams("forkChoiceUpdated" & $expectedVersion &
" expect PayloadAttributes" & $expectedVersion &
" but got PayloadAttributes" & $version)
Expand Down
25 changes: 19 additions & 6 deletions nimbus/beacon/api_handler/api_getpayload.nim
Original file line number Diff line number Diff line change
Expand Up @@ -18,29 +18,42 @@ import

{.push gcsafe, raises:[CatchableError].}

proc getPayload*(ben: BeaconEngineRef, id: PayloadID): GetPayloadV2Response =
proc getPayload*(ben: BeaconEngineRef,
expectedVersion: Version,
id: PayloadID): GetPayloadV2Response =
trace "Engine API request received",
meth = "GetPayload", id

var payload: ExecutionPayloadV1OrV2
var payloadGeneric: ExecutionPayload
var blockValue: UInt256
if not ben.get(id, blockValue, payload):
if not ben.get(id, blockValue, payloadGeneric):
raise unknownPayload("Unknown payload")

let version = payloadGeneric.version
if version > expectedVersion:
raise unsupportedFork("getPayload" & $expectedVersion &
" expect ExecutionPayload" & $expectedVersion &
" but get ExecutionPayload" & $version)

GetPayloadV2Response(
executionPayload: payload,
executionPayload: payloadGeneric.V1V2,
blockValue: blockValue
)

proc getPayloadV3*(ben: BeaconEngineRef, id: PayloadID): GetPayloadV3Response =
trace "Engine API request received",
meth = "GetPayload", id

var payload: ExecutionPayloadV3
var payloadGeneric: ExecutionPayload
var blockValue: UInt256
if not ben.get(id, blockValue, payload):
if not ben.get(id, blockValue, payloadGeneric):
raise unknownPayload("Unknown payload")

let version = payloadGeneric.version
if version != Version.V3:
raise unsupportedFork("getPayloadV3 expect ExecutionPayloadV3 but get ExecutionPayload" & $version)

let payload = payloadGeneric.V3
let com = ben.com
if not com.isCancunOrLater(ethTime payload.timestamp):
raise unsupportedFork("payload timestamp is less than Cancun activation")
Expand Down
6 changes: 5 additions & 1 deletion nimbus/beacon/api_handler/api_newpayload.nim
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ template validateVersion(com, timestamp, version, expectedVersion) =
raise invalidParams("if timestamp is earlier than Shanghai, " &
"payload must be ExecutionPayloadV1")

if version != expectedVersion:
if expectedVersion == Version.V3 and version != expectedVersion:
raise invalidParams("newPayload" & $expectedVersion &
" expect ExecutionPayload" & $expectedVersion &
" but got ExecutionPayload" & $version)
Expand All @@ -54,6 +54,10 @@ proc newPayload*(ben: BeaconEngineRef,
number = payload.blockNumber,
hash = payload.blockHash

if expectedVersion == Version.V3:
if beaconRoot.isNone:
raise invalidParams("newPayloadV3 expect beaconRoot but got none")

let
com = ben.com
db = com.db
Expand Down
16 changes: 16 additions & 0 deletions nimbus/beacon/web3_eth_conv.nim
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ func w3PrevRandao*(): Web3PrevRandao =
func w3Address*(): Web3Address =
discard

func w3Hash*(): Web3Hash =
discard

# ------------------------------------------------------------------------------
# Web3 types to Eth types
# ------------------------------------------------------------------------------
Expand Down Expand Up @@ -144,6 +147,19 @@ func ethTxs*(list: openArray[Web3Tx], removeBlobs = false):
func w3Hash*(x: common.Hash256): Web3Hash =
Web3Hash x.data

func w3Hashes*(list: openArray[common.Hash256]): seq[Web3Hash] =
for x in list:
result.add Web3Hash x.data

func w3Hashes*(z: Option[seq[common.Hash256]]): Option[seq[Web3Hash]] =
if z.isNone: none(seq[Web3Hash])
else:
let list = z.get
var v = newSeq[Web3Hash](list.len)
for x in list:
v.add Web3Hash x.data
some(v)

func w3Hash*(x: Option[common.Hash256]): Option[BlockHash] =
if x.isNone: none(BlockHash)
else: some(BlockHash x.get.data)
Expand Down
8 changes: 7 additions & 1 deletion nimbus/core/tx_pool/tx_tasks/tx_classify.nim
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,13 @@ logScope:
# ------------------------------------------------------------------------------

proc checkTxBasic(xp: TxPoolRef; item: TxItemRef): bool =
let res = validateTxBasic(item.tx.removeNetworkPayload, xp.chain.nextFork)
let res = validateTxBasic(
item.tx.removeNetworkPayload,
xp.chain.nextFork,
# A new transaction of the next fork may be
# coming before the fork activated
validateFork = false
)
if res.isOk:
return true
item.info = res.error
Expand Down
22 changes: 12 additions & 10 deletions nimbus/core/validate.nim
Original file line number Diff line number Diff line change
Expand Up @@ -226,16 +226,18 @@ func gasCost*(tx: Transaction): UInt256 =

proc validateTxBasic*(
tx: Transaction; ## tx to validate
fork: EVMFork): Result[void, string] =

if tx.txType == TxEip2930 and fork < FkBerlin:
return err("invalid tx: Eip2930 Tx type detected before Berlin")

if tx.txType == TxEip1559 and fork < FkLondon:
return err("invalid tx: Eip1559 Tx type detected before London")

if tx.txType == TxEip4844 and fork < FkCancun:
return err("invalid tx: Eip4844 Tx type detected before Cancun")
fork: EVMFork,
validateFork: bool = true): Result[void, string] =

if validateFork:
if tx.txType == TxEip2930 and fork < FkBerlin:
return err("invalid tx: Eip2930 Tx type detected before Berlin")

if tx.txType == TxEip1559 and fork < FkLondon:
return err("invalid tx: Eip1559 Tx type detected before London")

if tx.txType == TxEip4844 and fork < FkCancun:
return err("invalid tx: Eip4844 Tx type detected before Cancun")

if fork >= FkShanghai and tx.contractCreation and tx.payload.len > EIP3860_MAX_INITCODE_SIZE:
return err("invalid tx: initcode size exceeds maximum")
Expand Down
17 changes: 10 additions & 7 deletions nimbus/rpc/engine_api.nim
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ import
../beacon/api_handler,
../beacon/beacon_engine,
../beacon/web3_eth_conv,
../beacon/execution_types
../beacon/execution_types,
../beacon/api_handler/api_utils

{.push raises: [].}

Expand Down Expand Up @@ -50,17 +51,19 @@ proc setupEngineAPI*(engine: BeaconEngineRef, server: RpcServer) =
return engine.newPayload(Version.V2, payload)

server.rpc("engine_newPayloadV3") do(payload: ExecutionPayload,
expectedBlobVersionedHashes: seq[Web3Hash],
parentBeaconBlockRoot: Web3Hash) -> PayloadStatusV1:
if not validateVersionedHashed(payload, expectedBlobVersionedHashes):
expectedBlobVersionedHashes: Option[seq[Web3Hash]],
parentBeaconBlockRoot: Option[Web3Hash]) -> PayloadStatusV1:
if expectedBlobVersionedHashes.isNone:
raise invalidParams("newPayloadV3 expect blobVersionedHashes but got none")
if not validateVersionedHashed(payload, expectedBlobVersionedHashes.get):
return invalidStatus()
return engine.newPayload(Version.V3, payload, some(parentBeaconBlockRoot))
return engine.newPayload(Version.V3, payload, parentBeaconBlockRoot)

server.rpc("engine_getPayloadV1") do(payloadId: PayloadID) -> ExecutionPayloadV1:
return engine.getPayload(payloadId).executionPayload.V1
return engine.getPayload(Version.V1, payloadId).executionPayload.V1

server.rpc("engine_getPayloadV2") do(payloadId: PayloadID) -> GetPayloadV2Response:
return engine.getPayload(payloadId)
return engine.getPayload(Version.V2, payloadId)

server.rpc("engine_getPayloadV3") do(payloadId: PayloadID) -> GetPayloadV3Response:
return engine.getPayloadV3(payloadId)
Expand Down

0 comments on commit f6c64a0

Please sign in to comment.