Engine API: rearrange version and fork validation in fcU and newPayload (#1848)

This commit is contained in:
andri lim 2023-10-24 11:30:48 +07:00 committed by GitHub
parent 77289c7795
commit 5bfdcd0d27
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 48 additions and 35 deletions

View File

@ -169,7 +169,7 @@ method forkchoiceUpdatedVersion(cust: UpgradeforkchoiceUpdatedVersion, headTimes
let version = procCall forkchoiceUpdatedVersion(EngineAPIVersionResolver(cust), headTimestamp, payloadAttributesTimestamp) let version = procCall forkchoiceUpdatedVersion(EngineAPIVersionResolver(cust), headTimestamp, payloadAttributesTimestamp)
doAssert(version != Version.high, "cannot upgrade version " & $Version.high) doAssert(version != Version.high, "cannot upgrade version " & $Version.high)
version.succ version.succ
# Customizer that downgrades the version of the forkchoice directive call to the previous version. # Customizer that downgrades the version of the forkchoice directive call to the previous version.
type type
DowngradeforkchoiceUpdatedVersion* = ref object of BaseForkchoiceUpdatedCustomizer DowngradeforkchoiceUpdatedVersion* = ref object of BaseForkchoiceUpdatedCustomizer
@ -188,7 +188,7 @@ method getPayloadAttributes(cust: TimestampDeltaPayloadAttributesCustomizer, bas
var customPayloadAttributes = procCall getPayloadAttributes(cust.BasePayloadAttributesCustomizer, basePayloadAttributes) var customPayloadAttributes = procCall getPayloadAttributes(cust.BasePayloadAttributesCustomizer, basePayloadAttributes)
customPayloadAttributes.timestamp = w3Qty(customPayloadAttributes.timestamp, cust.timestampDelta) customPayloadAttributes.timestamp = w3Qty(customPayloadAttributes.timestamp, cust.timestampDelta)
return customPayloadAttributes return customPayloadAttributes
type type
VersionedHashesCustomizer* = ref object of RootRef VersionedHashesCustomizer* = ref object of RootRef
blobs*: Option[seq[BlobID]] blobs*: Option[seq[BlobID]]

View File

@ -210,10 +210,10 @@ method execute*(step: NewPayloads, ctx: CancunTestContext): bool =
forkchoiceState = env.clMock.latestForkchoice forkchoiceState = env.clMock.latestForkchoice
expectedError = step.fcUOnPayloadRequest.getExpectedError() expectedError = step.fcUOnPayloadRequest.getExpectedError()
expectedStatus = PayloadExecutionStatus.valid expectedStatus = PayloadExecutionStatus.valid
timestamp = env.clMock.latestHeader.timestamp.uint64 timestamp = env.clMock.latestHeader.timestamp.uint64
version = step.fcUOnPayloadRequest.forkchoiceUpdatedVersion(timestamp)
payloadAttributes = step.fcUOnPayloadRequest.getPayloadAttributes(payloadAttributes) payloadAttributes = step.fcUOnPayloadRequest.getPayloadAttributes(payloadAttributes)
let version = step.fcUOnPayloadRequest.forkchoiceUpdatedVersion(timestamp, some(payloadAttributes.timestamp.uint64))
if step.fcUOnPayloadRequest.getExpectInvalidStatus(): if step.fcUOnPayloadRequest.getExpectInvalidStatus():
expectedStatus = PayloadExecutionStatus.invalid expectedStatus = PayloadExecutionStatus.invalid

View File

@ -238,6 +238,7 @@ let cancunTestList* = [
] ]
), ),
), ),
TestDesc( TestDesc(
name: "Blob Transaction Ordering, Single Account 2", name: "Blob Transaction Ordering, Single Account 2",
about: """ about: """
@ -619,7 +620,7 @@ let cancunTestList* = [
run: specExecute, run: specExecute,
spec: CancunSpec( spec: CancunSpec(
mainFork: ForkCancun, mainFork: ForkCancun,
forkHeight: 1, forkHeight: 2,
testSequence: @[ testSequence: @[
NewPayloads( NewPayloads(
fcUOnPayloadRequest: DowngradeForkchoiceUpdatedVersion( fcUOnPayloadRequest: DowngradeForkchoiceUpdatedVersion(
@ -651,11 +652,12 @@ let cancunTestList* = [
testSequence: @[ testSequence: @[
NewPayloads( NewPayloads(
fcUOnPayloadRequest: DowngradeForkchoiceUpdatedVersion( fcUOnPayloadRequest: DowngradeForkchoiceUpdatedVersion(
beaconRoot: some(common.Hash256()),
expectedError: engineApiInvalidParams, expectedError: engineApiInvalidParams,
), ),
expectationDescription: """ expectationDescription: """
ForkchoiceUpdatedV2 after Cancun with beacon root field must return INVALID_PARAMS_ERROR (code $1) ForkchoiceUpdatedV2 after Cancun with beacon root field must return INVALID_PARAMS_ERROR (code $1)
"""% [$engineApiInvalidParams], """ % [$engineApiInvalidParams],
).TestStep, ).TestStep,
] ]
), ),

View File

@ -163,7 +163,7 @@ template expectErrorCode*(res: untyped, errCode: int, expectedDesc: string) =
testCond res.isErr: testCond res.isErr:
error "unexpected result, want error, get ok" error "unexpected result, want error, get ok"
testCond res.error.find($errCode) != -1: testCond res.error.find($errCode) != -1:
fatal "DEBUG", msg=expectedDesc fatal "DEBUG", msg=expectedDesc, got=res.error
template expectNoError*(res: untyped, expectedDesc: string) = template expectNoError*(res: untyped, expectedDesc: string) =
testCond res.isOk: testCond res.isOk:

View File

@ -19,33 +19,39 @@ import
{.push gcsafe, raises:[CatchableError].} {.push gcsafe, raises:[CatchableError].}
template validateVersion(attrsOpt, com, expectedVersion) = template validateVersion(attrsOpt, com, apiVersion) =
if attrsOpt.isSome: if attrsOpt.isSome:
let let
attr = attrsOpt.get attr = attrsOpt.get
version = attr.version version = attr.version
timestamp = ethTime attr.timestamp timestamp = ethTime attr.timestamp
if com.isCancunOrLater(timestamp): if apiVersion == Version.V3:
if version != Version.V3: if version != apiVersion:
raise invalidParams("if timestamp is Cancun or later," & raise invalidParams("forkChoiceUpdatedV3 expect PayloadAttributesV3" &
" payloadAttributes must be PayloadAttributesV3") " but got PayloadAttributes" & $version)
elif com.isShanghaiOrLater(timestamp): if not com.isCancunOrLater(timestamp):
if version != Version.V2: raise unsupportedFork(
raise invalidParams("if timestamp is Shanghai or later," & "forkchoiceUpdatedV3 get invalid payloadAttributes timestamp")
" payloadAttributes must be PayloadAttributesV2")
else: else:
if version != Version.V1: if com.isCancunOrLater(timestamp):
raise invalidParams("if timestamp is earlier than Shanghai," & if version < Version.V3:
" payloadAttributes must be PayloadAttributesV1") raise unsupportedFork("forkChoiceUpdated" & $apiVersion &
" doesn't support payloadAttributes with Cancun timestamp")
if expectedVersion == Version.V3 and version != expectedVersion: if version >= Version.V3:
raise invalidParams("forkChoiceUpdated" & $expectedVersion & raise invalidParams("forkChoiceUpdated" & $apiVersion &
" expect PayloadAttributes" & $expectedVersion & " doesn't support PayloadAttributes" & $version)
" but got PayloadAttributes" & $version) elif com.isShanghaiOrLater(timestamp):
if version != Version.V2:
raise invalidParams("if timestamp is Shanghai or later," &
" payloadAttributes must be PayloadAttributesV2")
else:
if version != Version.V1:
raise invalidParams("if timestamp is earlier than Shanghai," &
" payloadAttributes must be PayloadAttributesV1")
proc forkchoiceUpdated*(ben: BeaconEngineRef, proc forkchoiceUpdated*(ben: BeaconEngineRef,
expectedVersion: Version, apiVersion: Version,
update: ForkchoiceStateV1, update: ForkchoiceStateV1,
attrsOpt: Option[PayloadAttributes]): attrsOpt: Option[PayloadAttributes]):
ForkchoiceUpdatedResponse = ForkchoiceUpdatedResponse =
@ -55,7 +61,7 @@ proc forkchoiceUpdated*(ben: BeaconEngineRef,
chain = ben.chain chain = ben.chain
blockHash = ethHash update.headBlockHash blockHash = ethHash update.headBlockHash
validateVersion(attrsOpt, com, expectedVersion) validateVersion(attrsOpt, com, apiVersion)
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,11 @@ import
{.push gcsafe, raises:[CatchableError].} {.push gcsafe, raises:[CatchableError].}
template validateVersion(com, timestamp, version, expectedVersion) = template validateVersion(com, timestamp, version, apiVersion) =
if apiVersion == Version.V3:
if not com.isCancunOrLater(timestamp):
raise unsupportedFork("newPayloadV3 expect payload timestamp fall within Cancun")
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,14 +42,15 @@ template validateVersion(com, timestamp, version, expectedVersion) =
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 expectedVersion == Version.V3 and version != expectedVersion: if apiVersion == Version.V3:
raise invalidParams("newPayload" & $expectedVersion & if version != apiVersion:
" expect ExecutionPayload" & $expectedVersion & raise invalidParams("newPayload" & $apiVersion &
" but got ExecutionPayload" & $version) " expect ExecutionPayload" & $apiVersion &
" but got ExecutionPayload" & $version)
proc newPayload*(ben: BeaconEngineRef, proc newPayload*(ben: BeaconEngineRef,
expectedVersion: Version, apiVersion: Version,
payload: ExecutionPayload, payload: ExecutionPayload,
beaconRoot = none(Web3Hash)): PayloadStatusV1 = beaconRoot = none(Web3Hash)): PayloadStatusV1 =
@ -54,7 +59,7 @@ proc newPayload*(ben: BeaconEngineRef,
number = payload.blockNumber, number = payload.blockNumber,
hash = payload.blockHash hash = payload.blockHash
if expectedVersion == Version.V3: if apiVersion == Version.V3:
if beaconRoot.isNone: if beaconRoot.isNone:
raise invalidParams("newPayloadV3 expect beaconRoot but got none") raise invalidParams("newPayloadV3 expect beaconRoot but got none")
@ -64,7 +69,7 @@ proc newPayload*(ben: BeaconEngineRef,
timestamp = ethTime payload.timestamp timestamp = ethTime payload.timestamp
version = payload.version version = payload.version
validateVersion(com, timestamp, version, expectedVersion) validateVersion(com, timestamp, version, apiVersion)
var header = blockHeader(payload, ethHash beaconRoot) var header = blockHeader(payload, ethHash beaconRoot)
let blockHash = ethHash payload.blockHash let blockHash = ethHash payload.blockHash

View File

@ -115,7 +115,7 @@ func V3*(attr: PayloadAttributes): PayloadAttributesV3 =
timestamp: attr.timestamp, timestamp: attr.timestamp,
prevRandao: attr.prevRandao, prevRandao: attr.prevRandao,
suggestedFeeRecipient: attr.suggestedFeeRecipient, suggestedFeeRecipient: attr.suggestedFeeRecipient,
withdrawals: attr.withdrawals.get, withdrawals: attr.withdrawals.get(newSeq[WithdrawalV1]()),
parentBeaconBlockRoot: attr.parentBeaconBlockRoot.get parentBeaconBlockRoot: attr.parentBeaconBlockRoot.get
) )