Skip to content

Commit

Permalink
update Pectra execution API types (#155)
Browse files Browse the repository at this point in the history
  • Loading branch information
tersec authored Jun 17, 2024
1 parent b705f81 commit eb17493
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 34 deletions.
10 changes: 5 additions & 5 deletions tests/test_execution_types.nim
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ suite "Execution types tests":
shouldOverrideBuilder: Opt.some(false),
)

deposit = DepositReceiptV1(
deposit = DepositRequestV1(
pubkey: FixedBytes[48].conv(1),
withdrawalCredentials: FixedBytes[32].conv(3),
amount: 5.Quantity,
Expand Down Expand Up @@ -167,24 +167,24 @@ suite "Execution types tests":

test "payload version 4":
var v4 = payload
v4.depositReceipts = Opt.some(@[deposit])
v4.depositRequests = Opt.some(@[deposit])
v4.exits = Opt.some(@[exit])
check v4.version == Version.V4

var bad41 = v4
bad41.depositReceipts = Opt.none(seq[DepositReceiptV1])
bad41.depositRequests = Opt.none(seq[DepositRequestV1])
check bad41.version == Version.V4

var bad42 = v4
bad42.exits = Opt.none(seq[WithdrawalRequestV1])
check bad42.version == Version.V4

let v41 = bad41.V4
check v41.depositRequests == newSeq[DepositReceiptV1]()
check v41.depositRequests == newSeq[DepositRequestV1]()
check v41.withdrawalRequests == v4.exits.get

let v42 = bad42.V4
check v42.depositRequests == v4.depositReceipts.get
check v42.depositRequests == v4.depositRequests.get
check v42.withdrawalRequests == newSeq[WithdrawalRequestV1]()

# roundtrip
Expand Down
2 changes: 1 addition & 1 deletion web3/conversions.nim
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ derefType(ReceiptObject).useDefaultSerializationIn JrpcConv
#------------------------------------------------------------------------------

WithdrawalV1.useDefaultSerializationIn JrpcConv
DepositReceiptV1.useDefaultSerializationIn JrpcConv
DepositRequestV1.useDefaultSerializationIn JrpcConv
WithdrawalRequestV1.useDefaultSerializationIn JrpcConv
ExecutionPayloadV1.useDefaultSerializationIn JrpcConv
ExecutionPayloadV2.useDefaultSerializationIn JrpcConv
Expand Down
51 changes: 27 additions & 24 deletions web3/engine_api_types.nim
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
# This file may not be copied, modified, or distributed except according to
# those terms.

{.push raises: [].}

import
std/typetraits,
stint,
Expand All @@ -19,15 +21,15 @@ export
type
TypedTransaction* = distinct seq[byte]

# https://github.com/ethereum/execution-apis/blob/v1.0.0-beta.3/src/engine/shanghai.md#withdrawalv1
# https://github.com/ethereum/execution-apis/blob/v1.0.0-beta.4/src/engine/shanghai.md#withdrawalv1
WithdrawalV1* = object
index*: Quantity
validatorIndex*: Quantity
address*: Address
amount*: Quantity

# https://github.com/ethereum/execution-apis/blob/v1.0.0-beta.4/src/engine/prague.md#depositrequestv1
DepositReceiptV1* = object
DepositRequestV1* = object
pubkey*: FixedBytes[48]
withdrawalCredentials*: FixedBytes[32]
amount*: Quantity
Expand All @@ -40,7 +42,13 @@ type
validatorPublicKey*: FixedBytes[48]
amount*: Quantity

# https://github.com/ethereum/execution-apis/blob/v1.0.0-beta.3/src/engine/paris.md#executionpayloadv1
# https://github.com/ethereum/execution-apis/blob/3ae3d29fc9900e5c48924c238dff7643fdc3680e/src/engine/prague.md#consolidationrequestv1
ConsolidationRequestV1* = object
sourceAddress*: Address
sourcePubkey*: FixedBytes[48]
targetPubkey*: FixedBytes[48]

# https://github.com/ethereum/execution-apis/blob/v1.0.0-beta.4/src/engine/paris.md#executionpayloadv1
ExecutionPayloadV1* = object
parentHash*: Hash256
feeRecipient*: Address
Expand All @@ -57,7 +65,7 @@ type
blockHash*: Hash256
transactions*: seq[TypedTransaction]

# https://github.com/ethereum/execution-apis/blob/v1.0.0-beta.3/src/engine/shanghai.md#executionpayloadv2
# https://github.com/ethereum/execution-apis/blob/v1.0.0-beta.4/src/engine/shanghai.md#executionpayloadv2
ExecutionPayloadV2* = object
parentHash*: Hash256
feeRecipient*: Address
Expand Down Expand Up @@ -87,7 +95,7 @@ type
# please fix this. (Maybe the RPC library does handle sum types?
# Or maybe we can enhance it to do so?) --Adam
#
# https://github.com/ethereum/execution-apis/blob/v1.0.0-beta.3/src/engine/shanghai.md
# https://github.com/ethereum/execution-apis/blob/v1.0.0-beta.4/src/engine/shanghai.md
ExecutionPayloadV1OrV2* = object
parentHash*: BlockHash
feeRecipient*: Address
Expand All @@ -105,7 +113,7 @@ type
transactions*: seq[TypedTransaction]
withdrawals*: Opt[seq[WithdrawalV1]]

# https://github.com/ethereum/execution-apis/blob/fe8e13c288c592ec154ce25c534e26cb7ce0530d/src/engine/cancun.md#executionpayloadv3
# https://github.com/ethereum/execution-apis/blob/v1.0.0-beta.4/src/engine/cancun.md#executionpayloadv3
ExecutionPayloadV3* = object
parentHash*: Hash256
feeRecipient*: Address
Expand All @@ -125,7 +133,7 @@ type
blobGasUsed*: Quantity
excessBlobGas*: Quantity

# https://github.com/ethereum/execution-apis/blob/v1.0.0-beta.4/src/engine/prague.md#executionpayloadv4
# https://github.com/ethereum/execution-apis/blob/3ae3d29fc9900e5c48924c238dff7643fdc3680e/src/engine/prague.md#executionpayloadv4
ExecutionPayloadV4* = object
parentHash*: Hash256
feeRecipient*: Address
Expand All @@ -144,48 +152,45 @@ type
withdrawals*: seq[WithdrawalV1]
blobGasUsed*: Quantity
excessBlobGas*: Quantity

# https://github.com/ethereum/consensus-specs/pull/3757
# https://github.com/ethereum/execution-apis/pull/544
# mainly for devnet-0
depositRequests*: seq[DepositReceiptV1]

depositRequests*: seq[DepositRequestV1]
withdrawalRequests*: seq[WithdrawalRequestV1]
consolidationRequests*: seq[ConsolidationRequestV1]

SomeExecutionPayload* =
ExecutionPayloadV1 |
ExecutionPayloadV2 |
ExecutionPayloadV3 |
ExecutionPayloadV4

# https://github.com/ethereum/execution-apis/blob/ee3df5bc38f28ef35385cefc9d9ca18d5e502778/src/engine/cancun.md#blobsbundlev1
# https://github.com/ethereum/execution-apis/blob/v1.0.0-beta.4/src/engine/cancun.md#blobsbundlev1
BlobsBundleV1* = object
commitments*: seq[KZGCommitment]
proofs*: seq[KZGProof]
blobs*: seq[Blob]

# https://github.com/ethereum/execution-apis/blob/d03c193dc317538e2a1a098030c21bacc2fd1333/src/engine/shanghai.md#executionpayloadbodyv1
# https://github.com/ethereum/execution-apis/blob/v1.0.0-beta.4/src/engine/shanghai.md#executionpayloadbodyv1
# For optional withdrawals field, see:
# https://github.com/ethereum/execution-apis/blob/main/src/engine/shanghai.md#engine_getpayloadbodiesbyhashv1
# https://github.com/ethereum/execution-apis/blob/main/src/engine/shanghai.md#engine_getpayloadbodiesbyrangev1
# https://github.com/ethereum/execution-apis/blob/v1.0.0-beta.4/src/engine/shanghai.md#engine_getpayloadbodiesbyhashv1
# https://github.com/ethereum/execution-apis/blob/v1.0.0-beta.4/src/engine/shanghai.md#engine_getpayloadbodiesbyrangev1
# "Client software MUST set withdrawals field to null for bodies of pre-Shanghai blocks."
ExecutionPayloadBodyV1* = object
transactions*: seq[TypedTransaction]
withdrawals*: Opt[seq[WithdrawalV1]]

# https://github.com/ethereum/execution-apis/blob/v1.0.0-beta.3/src/engine/paris.md#payloadattributesv1
# https://github.com/ethereum/execution-apis/blob/v1.0.0-beta.4/src/engine/paris.md#payloadattributesv1
PayloadAttributesV1* = object
timestamp*: Quantity
prevRandao*: FixedBytes[32]
suggestedFeeRecipient*: Address

# https://github.com/ethereum/execution-apis/blob/v1.0.0-beta.3/src/engine/shanghai.md#payloadattributesv2
# https://github.com/ethereum/execution-apis/blob/v1.0.0-beta.4/src/engine/shanghai.md#payloadattributesv2
PayloadAttributesV2* = object
timestamp*: Quantity
prevRandao*: FixedBytes[32]
suggestedFeeRecipient*: Address
withdrawals*: seq[WithdrawalV1]

# https://github.com/ethereum/execution-apis/blob/ee3df5bc38f28ef35385cefc9d9ca18d5e502778/src/engine/cancun.md#payloadattributesv3
# https://github.com/ethereum/execution-apis/blob/v1.0.0-beta.4/src/engine/cancun.md#payloadattributesv3
PayloadAttributesV3* = object
timestamp*: Quantity
prevRandao*: FixedBytes[32]
Expand All @@ -205,7 +210,7 @@ type
PayloadAttributesV2 |
PayloadAttributesV3

# https://github.com/ethereum/execution-apis/blob/v1.0.0-beta.3/src/engine/paris.md#payloadstatusv1
# https://github.com/ethereum/execution-apis/blob/v1.0.0-beta.4/src/engine/paris.md#payloadstatusv1
PayloadExecutionStatus* {.pure.} = enum
syncing = "SYNCING"
valid = "VALID"
Expand All @@ -218,7 +223,7 @@ type
latestValidHash*: Opt[BlockHash]
validationError*: Opt[string]

# https://github.com/ethereum/execution-apis/blob/v1.0.0-beta.3/src/engine/paris.md#forkchoicestatev1
# https://github.com/ethereum/execution-apis/blob/v1.0.0-beta.4/src/engine/paris.md#forkchoicestatev1
ForkchoiceStateV1* = object
headBlockHash*: BlockHash
safeBlockHash*: BlockHash
Expand Down Expand Up @@ -287,7 +292,5 @@ const
engineApiTooLargeRequest* = -38004
engineApiUnsupportedFork* = -38005

{.push raises: [].}

template `==`*(a, b: TypedTransaction): bool =
distinctBase(a) == distinctBase(b)
8 changes: 4 additions & 4 deletions web3/execution_types.nim
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ type
withdrawals*: Opt[seq[WithdrawalV1]]
blobGasUsed*: Opt[Quantity]
excessBlobGas*: Opt[Quantity]
depositReceipts*: Opt[seq[DepositReceiptV1]]
depositRequests*: Opt[seq[DepositRequestV1]]
exits*: Opt[seq[WithdrawalRequestV1]]

PayloadAttributes* = object
Expand Down Expand Up @@ -64,7 +64,7 @@ type
{.push raises: [].}

func version*(payload: ExecutionPayload): Version =
if payload.depositReceipts.isSome or payload.exits.isSome:
if payload.depositRequests.isSome or payload.exits.isSome:
Version.V4
elif payload.blobGasUsed.isSome or payload.excessBlobGas.isSome:
Version.V3
Expand Down Expand Up @@ -272,7 +272,7 @@ func V4*(p: ExecutionPayload): ExecutionPayloadV4 =
withdrawals: p.withdrawals.get,
blobGasUsed: p.blobGasUsed.get(0.Quantity),
excessBlobGas: p.excessBlobGas.get(0.Quantity),
depositRequests: p.depositReceipts.get(newSeq[DepositReceiptV1]()),
depositRequests: p.depositRequests.get(newSeq[DepositRequestV1]()),
withdrawalRequests: p.exits.get(newSeq[WithdrawalRequestV1]())
)

Expand Down Expand Up @@ -390,7 +390,7 @@ func executionPayload*(p: ExecutionPayloadV4): ExecutionPayload =
withdrawals: Opt.some(p.withdrawals),
blobGasUsed: Opt.some(p.blobGasUsed),
excessBlobGas: Opt.some(p.excessBlobGas),
depositReceipts: Opt.some(p.depositRequests),
depositRequests: Opt.some(p.depositRequests),
exits: Opt.some(p.withdrawalRequests)
)

Expand Down

0 comments on commit eb17493

Please sign in to comment.