Engine API: Fix API version validation

This commit is contained in:
jangko 2023-10-23 09:25:03 +07:00
parent d1252665b2
commit 0d91d8736c
No known key found for this signature in database
GPG Key ID: 31702AE10541E6B9
3 changed files with 23 additions and 10 deletions

View File

@ -19,7 +19,7 @@ import
{.push gcsafe, raises:[CatchableError].} {.push gcsafe, raises:[CatchableError].}
template validateVersion(attrsOpt, com) = template validateVersion(attrsOpt, com, expectedVersion) =
if attrsOpt.isSome: if attrsOpt.isSome:
let let
attr = attrsOpt.get attr = attrsOpt.get
@ -39,7 +39,13 @@ template validateVersion(attrsOpt, com) =
raise invalidParams("if timestamp is earlier than Shanghai," & raise invalidParams("if timestamp is earlier than Shanghai," &
" payloadAttributes must be PayloadAttributesV1") " payloadAttributes must be PayloadAttributesV1")
if version != expectedVersion:
raise invalidParams("forkChoiceUpdated" & $expectedVersion &
" expect PayloadAttributes" & $expectedVersion &
" but got PayloadAttributes" & $version)
proc forkchoiceUpdated*(ben: BeaconEngineRef, proc forkchoiceUpdated*(ben: BeaconEngineRef,
expectedVersion: Version,
update: ForkchoiceStateV1, update: ForkchoiceStateV1,
attrsOpt: Option[PayloadAttributes]): attrsOpt: Option[PayloadAttributes]):
ForkchoiceUpdatedResponse = ForkchoiceUpdatedResponse =
@ -49,7 +55,7 @@ proc forkchoiceUpdated*(ben: BeaconEngineRef,
chain = ben.chain chain = ben.chain
blockHash = ethHash update.headBlockHash blockHash = ethHash update.headBlockHash
validateVersion(attrsOpt, com) validateVersion(attrsOpt, com, expectedVersion)
if blockHash == common.Hash256(): if blockHash == common.Hash256():
warn "Forkchoice requested update to zero hash" warn "Forkchoice requested update to zero hash"

View File

@ -19,7 +19,7 @@ import
{.push gcsafe, raises:[CatchableError].} {.push gcsafe, raises:[CatchableError].}
template validateVersion(com, timestamp, version) = template validateVersion(com, timestamp, version, expectedVersion) =
if com.isCancunOrLater(timestamp): if com.isCancunOrLater(timestamp):
if version != Version.V3: if version != Version.V3:
raise invalidParams("if timestamp is Cancun or later, " & raise invalidParams("if timestamp is Cancun or later, " &
@ -38,7 +38,14 @@ template validateVersion(com, timestamp, version) =
raise invalidParams("if timestamp is earlier than Shanghai, " & raise invalidParams("if timestamp is earlier than Shanghai, " &
"payload must be ExecutionPayloadV1") "payload must be ExecutionPayloadV1")
if version != expectedVersion:
raise invalidParams("newPayload" & $expectedVersion &
" expect ExecutionPayload" & $expectedVersion &
" but got ExecutionPayload" & $version)
proc newPayload*(ben: BeaconEngineRef, proc newPayload*(ben: BeaconEngineRef,
expectedVersion: Version,
payload: ExecutionPayload, payload: ExecutionPayload,
beaconRoot = none(Web3Hash)): PayloadStatusV1 = beaconRoot = none(Web3Hash)): PayloadStatusV1 =
@ -53,7 +60,7 @@ proc newPayload*(ben: BeaconEngineRef,
timestamp = ethTime payload.timestamp timestamp = ethTime payload.timestamp
version = payload.version version = payload.version
validateVersion(com, timestamp, version) validateVersion(com, timestamp, version, expectedVersion)
var header = blockHeader(payload, ethHash beaconRoot) var header = blockHeader(payload, ethHash beaconRoot)
let blockHash = ethHash payload.blockHash let blockHash = ethHash payload.blockHash

View File

@ -44,17 +44,17 @@ proc setupEngineAPI*(engine: BeaconEngineRef, server: RpcServer) =
return methods.filterIt(supportedMethods.contains(it)) return methods.filterIt(supportedMethods.contains(it))
server.rpc("engine_newPayloadV1") do(payload: ExecutionPayloadV1) -> PayloadStatusV1: server.rpc("engine_newPayloadV1") do(payload: ExecutionPayloadV1) -> PayloadStatusV1:
return engine.newPayload(payload.executionPayload) return engine.newPayload(Version.V1, payload.executionPayload)
server.rpc("engine_newPayloadV2") do(payload: ExecutionPayload) -> PayloadStatusV1: server.rpc("engine_newPayloadV2") do(payload: ExecutionPayload) -> PayloadStatusV1:
return engine.newPayload(payload) return engine.newPayload(Version.V2, payload)
server.rpc("engine_newPayloadV3") do(payload: ExecutionPayload, server.rpc("engine_newPayloadV3") do(payload: ExecutionPayload,
expectedBlobVersionedHashes: seq[Web3Hash], expectedBlobVersionedHashes: seq[Web3Hash],
parentBeaconBlockRoot: Web3Hash) -> PayloadStatusV1: parentBeaconBlockRoot: Web3Hash) -> PayloadStatusV1:
if not validateVersionedHashed(payload, expectedBlobVersionedHashes): if not validateVersionedHashed(payload, expectedBlobVersionedHashes):
return invalidStatus() return invalidStatus()
return engine.newPayload(payload, some(parentBeaconBlockRoot)) return engine.newPayload(Version.V3, payload, some(parentBeaconBlockRoot))
server.rpc("engine_getPayloadV1") do(payloadId: PayloadID) -> ExecutionPayloadV1: server.rpc("engine_getPayloadV1") do(payloadId: PayloadID) -> ExecutionPayloadV1:
return engine.getPayload(payloadId).executionPayload.V1 return engine.getPayload(payloadId).executionPayload.V1
@ -71,15 +71,15 @@ proc setupEngineAPI*(engine: BeaconEngineRef, server: RpcServer) =
server.rpc("engine_forkchoiceUpdatedV1") do(update: ForkchoiceStateV1, server.rpc("engine_forkchoiceUpdatedV1") do(update: ForkchoiceStateV1,
attrs: Option[PayloadAttributesV1]) -> ForkchoiceUpdatedResponse: attrs: Option[PayloadAttributesV1]) -> ForkchoiceUpdatedResponse:
return engine.forkchoiceUpdated(update, attrs.payloadAttributes) return engine.forkchoiceUpdated(Version.V1, update, attrs.payloadAttributes)
server.rpc("engine_forkchoiceUpdatedV2") do(update: ForkchoiceStateV1, server.rpc("engine_forkchoiceUpdatedV2") do(update: ForkchoiceStateV1,
attrs: Option[PayloadAttributes]) -> ForkchoiceUpdatedResponse: attrs: Option[PayloadAttributes]) -> ForkchoiceUpdatedResponse:
return engine.forkchoiceUpdated(update, attrs) return engine.forkchoiceUpdated(Version.V2, update, attrs)
server.rpc("engine_forkchoiceUpdatedV3") do(update: ForkchoiceStateV1, server.rpc("engine_forkchoiceUpdatedV3") do(update: ForkchoiceStateV1,
attrs: Option[PayloadAttributes]) -> ForkchoiceUpdatedResponse: attrs: Option[PayloadAttributes]) -> ForkchoiceUpdatedResponse:
return engine.forkchoiceUpdated(update, attrs) return engine.forkchoiceUpdated(Version.V3, update, attrs)
server.rpc("engine_getPayloadBodiesByHashV1") do(hashes: seq[Web3Hash]) -> server.rpc("engine_getPayloadBodiesByHashV1") do(hashes: seq[Web3Hash]) ->
seq[Option[ExecutionPayloadBodyV1]]: seq[Option[ExecutionPayloadBodyV1]]: