From 182aac502a9327ba369c386951aad0a39f4743c5 Mon Sep 17 00:00:00 2001 From: jangko Date: Mon, 21 Oct 2024 08:31:30 +0700 Subject: [PATCH 1/2] Implementation of EIP-7742 for Pectra --- web3/engine_api.nim | 31 ++++++++++++++++++++++++++++--- web3/engine_api_types.nim | 12 +++++++++++- web3/eth_api_types.nim | 1 + web3/execution_types.nim | 37 ++++++++++++++++++++++++++++++++++++- 4 files changed, 76 insertions(+), 5 deletions(-) diff --git a/web3/engine_api.nim b/web3/engine_api.nim index 6434083..adc249e 100644 --- a/web3/engine_api.nim +++ b/web3/engine_api.nim @@ -26,8 +26,14 @@ createRpcSigsFromNim(RpcClient): proc engine_newPayloadV1(payload: ExecutionPayloadV1): PayloadStatusV1 proc engine_newPayloadV2(payload: ExecutionPayloadV2): PayloadStatusV1 - proc engine_newPayloadV3(payload: ExecutionPayloadV3, expectedBlobVersionedHashes: seq[VersionedHash], parentBeaconBlockRoot: Hash32): PayloadStatusV1 - proc engine_newPayloadV4(payload: ExecutionPayloadV3, expectedBlobVersionedHashes: seq[VersionedHash], parentBeaconBlockRoot: Hash32, executionRequests: array[3, seq[byte]]): PayloadStatusV1 + proc engine_newPayloadV3(payload: ExecutionPayloadV3, + expectedBlobVersionedHashes: seq[VersionedHash], + parentBeaconBlockRoot: Hash32): PayloadStatusV1 + proc engine_newPayloadV4(payload: ExecutionPayloadV3, + expectedBlobVersionedHashes: seq[VersionedHash], + parentBeaconBlockRoot: Hash32, + executionRequests: array[3, seq[byte]], + targetBlobsPerBlock: Quantity): PayloadStatusV1 proc engine_forkchoiceUpdatedV1(forkchoiceState: ForkchoiceStateV1, payloadAttributes: Opt[PayloadAttributesV1]): ForkchoiceUpdatedResponse proc engine_forkchoiceUpdatedV2(forkchoiceState: ForkchoiceStateV1, payloadAttributes: Opt[PayloadAttributesV2]): ForkchoiceUpdatedResponse proc engine_forkchoiceUpdatedV3(forkchoiceState: ForkchoiceStateV1, payloadAttributes: Opt[PayloadAttributesV3]): ForkchoiceUpdatedResponse @@ -55,9 +61,11 @@ createRpcSigsFromNim(RpcClient): proc engine_newPayloadV4(payload: ExecutionPayload, expectedBlobVersionedHashes: Opt[seq[VersionedHash]], parentBeaconBlockRoot: Opt[Hash32], - executionRequests: Opt[array[3, seq[byte]]]): PayloadStatusV1 + executionRequests: Opt[array[3, seq[byte]]], + targetBlobsPerBlock: Opt[Quantity]): PayloadStatusV1 proc engine_forkchoiceUpdatedV2(forkchoiceState: ForkchoiceStateV1, payloadAttributes: Opt[PayloadAttributes]): ForkchoiceUpdatedResponse proc engine_forkchoiceUpdatedV3(forkchoiceState: ForkchoiceStateV1, payloadAttributes: Opt[PayloadAttributes]): ForkchoiceUpdatedResponse + proc engine_forkchoiceUpdatedV4(forkchoiceState: ForkchoiceStateV1, payloadAttributes: Opt[PayloadAttributes]): ForkchoiceUpdatedResponse template forkchoiceUpdated*( rpcClient: RpcClient, @@ -77,6 +85,12 @@ template forkchoiceUpdated*( payloadAttributes: Opt[PayloadAttributesV3]): Future[ForkchoiceUpdatedResponse] = engine_forkchoiceUpdatedV3(rpcClient, forkchoiceState, payloadAttributes) +template forkchoiceUpdated*( + rpcClient: RpcClient, + forkchoiceState: ForkchoiceStateV1, + payloadAttributes: Opt[PayloadAttributesV4]): Future[ForkchoiceUpdatedResponse] = + engine_forkchoiceUpdatedV4(rpcClient, forkchoiceState, payloadAttributes) + template getPayload*( rpcClient: RpcClient, T: type ExecutionPayloadV1, @@ -125,6 +139,17 @@ template newPayload*( engine_newPayloadV3( rpcClient, payload, versionedHashes, parentBeaconBlockRoot) +template newPayload*( + rpcClient: RpcClient, + payload: ExecutionPayloadV3, + versionedHashes: seq[VersionedHash], + parentBeaconBlockRoot: Hash32, + executionRequests: array[3, seq[byte]], + targetBlobsPerBlock: Quantity): Future[PayloadStatusV1] = + engine_newPayloadV4( + rpcClient, payload, versionedHashes, parentBeaconBlockRoot, + executionRequests, targetBlobsPerBlock) + template exchangeCapabilities*( rpcClient: RpcClient, methods: seq[string]): Future[seq[string]] = diff --git a/web3/engine_api_types.nim b/web3/engine_api_types.nim index 5dff6b9..2719238 100644 --- a/web3/engine_api_types.nim +++ b/web3/engine_api_types.nim @@ -157,6 +157,15 @@ type withdrawals*: seq[WithdrawalV1] parentBeaconBlockRoot*: Hash32 + PayloadAttributesV4* = object + timestamp*: Quantity + prevRandao*: Bytes32 + suggestedFeeRecipient*: Address + withdrawals*: seq[WithdrawalV1] + parentBeaconBlockRoot*: Hash32 + targetBlobsPerBlock*: Quantity + maxBlobsPerBlock*: Quantity + # This is ugly, but see the comment on ExecutionPayloadV1OrV2. PayloadAttributesV1OrV2* = object timestamp*: Quantity @@ -167,7 +176,8 @@ type SomePayloadAttributes* = PayloadAttributesV1 | PayloadAttributesV2 | - PayloadAttributesV3 + PayloadAttributesV3 | + PayloadAttributesV4 # https://github.com/ethereum/execution-apis/blob/v1.0.0-beta.4/src/engine/paris.md#payloadstatusv1 PayloadExecutionStatus* {.pure.} = enum diff --git a/web3/eth_api_types.nim b/web3/eth_api_types.nim index c3d5c9a..bf95c37 100644 --- a/web3/eth_api_types.nim +++ b/web3/eth_api_types.nim @@ -92,6 +92,7 @@ type excessBlobGas*: Opt[Quantity] # EIP-4844 parentBeaconBlockRoot*: Opt[Hash32] # EIP-4788 requestsRoot*: Opt[Hash32] # EIP-7685 + targetBlobCount*: Opt[Quantity] # EIP-7742 WithdrawalObject* = object index*: Quantity diff --git a/web3/execution_types.nim b/web3/execution_types.nim index 3607d01..ff368a9 100644 --- a/web3/execution_types.nim +++ b/web3/execution_types.nim @@ -43,6 +43,8 @@ type suggestedFeeRecipient*: Address withdrawals*: Opt[seq[WithdrawalV1]] parentBeaconBlockRoot*: Opt[Hash32] + targetBlobsPerBlock*: Opt[Quantity] + maxBlobsPerBlock*: Opt[Quantity] SomeOptionalPayloadAttributes* = Opt[PayloadAttributesV1] | @@ -71,7 +73,9 @@ func version*(payload: ExecutionPayload): Version = Version.V1 func version*(attr: PayloadAttributes): Version = - if attr.parentBeaconBlockRoot.isSome: + if attr.targetBlobsPerBlock.isSome or attr.maxBlobsPerBlock.isSome: + Version.V4 + elif attr.parentBeaconBlockRoot.isSome: Version.V3 elif attr.withdrawals.isSome: Version.V2 @@ -120,6 +124,17 @@ func V3*(attr: PayloadAttributes): PayloadAttributesV3 = parentBeaconBlockRoot: attr.parentBeaconBlockRoot.get ) +func V4*(attr: PayloadAttributes): PayloadAttributesV4 = + PayloadAttributesV4( + timestamp: attr.timestamp, + prevRandao: attr.prevRandao, + suggestedFeeRecipient: attr.suggestedFeeRecipient, + withdrawals: attr.withdrawals.get(newSeq[WithdrawalV1]()), + parentBeaconBlockRoot: attr.parentBeaconBlockRoot.get, + targetBlobsPerBlock: attr.targetBlobsPerBlock.get, + maxBlobsPerBlock: attr.maxBlobsPerBlock.get, + ) + func V1*(attr: Opt[PayloadAttributes]): Opt[PayloadAttributesV1] = if attr.isNone: return Opt.none(PayloadAttributesV1) @@ -135,6 +150,11 @@ func V3*(attr: Opt[PayloadAttributes]): Opt[PayloadAttributesV3] = return Opt.none(PayloadAttributesV3) Opt.some(attr.get.V3) +func V4*(attr: Opt[PayloadAttributes]): Opt[PayloadAttributesV4] = + if attr.isNone: + return Opt.none(PayloadAttributesV4) + Opt.some(attr.get.V4) + func payloadAttributes*(attr: PayloadAttributesV1): PayloadAttributes = PayloadAttributes( timestamp: attr.timestamp, @@ -159,6 +179,17 @@ func payloadAttributes*(attr: PayloadAttributesV3): PayloadAttributes = parentBeaconBlockRoot: Opt.some(attr.parentBeaconBlockRoot) ) +func payloadAttributes*(attr: PayloadAttributesV4): PayloadAttributes = + PayloadAttributes( + timestamp: attr.timestamp, + prevRandao: attr.prevRandao, + suggestedFeeRecipient: attr.suggestedFeeRecipient, + withdrawals: Opt.some(attr.withdrawals), + parentBeaconBlockRoot: Opt.some(attr.parentBeaconBlockRoot), + targetBlobsPerBlock: Opt.some(attr.targetBlobsPerBlock), + maxBlobsPerBlock: Opt.some(attr.maxBlobsPerBlock), + ) + func payloadAttributes*(x: Opt[PayloadAttributesV1]): Opt[PayloadAttributes] = if x.isNone: Opt.none(PayloadAttributes) else: Opt.some(payloadAttributes x.get) @@ -171,6 +202,10 @@ func payloadAttributes*(x: Opt[PayloadAttributesV3]): Opt[PayloadAttributes] = if x.isNone: Opt.none(PayloadAttributes) else: Opt.some(payloadAttributes x.get) +func payloadAttributes*(x: Opt[PayloadAttributesV4]): Opt[PayloadAttributes] = + if x.isNone: Opt.none(PayloadAttributes) + else: Opt.some(payloadAttributes x.get) + func V1V2*(p: ExecutionPayload): ExecutionPayloadV1OrV2 = ExecutionPayloadV1OrV2( parentHash: p.parentHash, From d70ad1e63ae88c35c52cddc0daa74147c619d601 Mon Sep 17 00:00:00 2001 From: jangko Date: Fri, 22 Nov 2024 17:40:58 +0700 Subject: [PATCH 2/2] Replace targetBlobCount with targetBlobsPerBlock --- web3/eth_api_types.nim | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/web3/eth_api_types.nim b/web3/eth_api_types.nim index 10a6034..467af09 100644 --- a/web3/eth_api_types.nim +++ b/web3/eth_api_types.nim @@ -92,7 +92,7 @@ type excessBlobGas*: Opt[Quantity] # EIP-4844 parentBeaconBlockRoot*: Opt[Hash32] # EIP-4788 requestsHash*: Opt[Hash32] # EIP-7685 - targetBlobCount*: Opt[Quantity] # EIP-7742 + targetBlobsPerBlock*: Opt[Quantity] # EIP-7742 WithdrawalObject* = object index*: Quantity @@ -129,7 +129,7 @@ type excessBlobGas*: Opt[Quantity] # EIP-4844 parentBeaconBlockRoot*: Opt[Hash32] # EIP-4788 requestsHash*: Opt[Hash32] # EIP-7685 - targetBlobCount*: Opt[Quantity] # EIP-7742 + targetBlobsPerBlock*: Opt[Quantity] # EIP-7742 TxOrHashKind* = enum tohHash