Skip to content

Commit

Permalink
Add ExecutionPayloadV4 and GetPayloadV4Response to execution_types fu…
Browse files Browse the repository at this point in the history
…nctions (#141)
  • Loading branch information
jangko authored Mar 27, 2024
1 parent 85e34e8 commit e049d14
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 3 deletions.
48 changes: 48 additions & 0 deletions tests/test_execution_types.nim
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,19 @@ suite "Execution types tests":
shouldOverrideBuilder: some(false),
)

deposit = DepositReceiptV1(
pubkey: FixedBytes[48].conv(1),
withdrawalCredentials: FixedBytes[32].conv(3),
amount: 5.Quantity,
signature: FixedBytes[96].conv(7),
index: 9.Quantity
)

exit = ExitV1(
sourceAddress: address(7),
validatorPublicKey: FixedBytes[48].conv(9)
)

test "payload version":
var badv31 = payload
badv31.blobGasUsed = none(Quantity)
Expand Down Expand Up @@ -151,3 +164,38 @@ suite "Execution types tests":

let v1 = response.V1
check v1 == v1.getPayloadResponse.V1

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

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

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

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

let v42 = bad42.V4
check v42.depositReceipts == v4.depositReceipts.get
check v42.exits == newSeq[ExitV1]()

# roundtrip
let v4p = v4.V4
check v4p == v4p.executionPayload.V4

# response version 4
var resv4 = response
resv4.executionPayload = v4
check resv4.version == Version.V4

# response roundtrip
let rv3p = resv4.V4
check rv3p == rv3p.getPayloadResponse.V4
2 changes: 1 addition & 1 deletion web3/engine_api_types.nim
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ type

# https://github.com/ethereum/execution-apis/blob/90a46e9137c89d58e818e62fa33a0347bba50085/src/engine/prague.md#response-1
GetPayloadV4Response* = object
executionPayload*: ExecutionPayloadV3
executionPayload*: ExecutionPayloadV4
blockValue*: UInt256
blobsBundle*: BlobsBundleV1
shouldOverrideBuilder*: bool
Expand Down
76 changes: 74 additions & 2 deletions web3/execution_types.nim
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ type
withdrawals*: Option[seq[WithdrawalV1]]
blobGasUsed*: Option[Quantity]
excessBlobGas*: Option[Quantity]
depositReceipts*: Option[seq[DepositReceiptV1]]
exits*: Option[seq[ExitV1]]

PayloadAttributes* = object
timestamp*: Quantity
Expand All @@ -57,11 +59,14 @@ type
V1
V2
V3
V4

{.push raises: [].}

func version*(payload: ExecutionPayload): Version =
if payload.blobGasUsed.isSome or payload.excessBlobGas.isSome:
if payload.depositReceipts.isSome or payload.exits.isSome:
Version.V4
elif payload.blobGasUsed.isSome or payload.excessBlobGas.isSome:
Version.V3
elif payload.withdrawals.isSome:
Version.V2
Expand All @@ -77,7 +82,11 @@ func version*(attr: PayloadAttributes): Version =
Version.V1

func version*(res: GetPayloadResponse): Version =
if res.blobsBundle.isSome or res.shouldOverrideBuilder.isSome:
# TODO: should this return whatever version of
# executionPayload.version?
if res.executionPayload.version == Version.V4:
Version.V4
elif res.blobsBundle.isSome or res.shouldOverrideBuilder.isSome:
Version.V3
elif res.blockValue.isSome:
Version.V2
Expand Down Expand Up @@ -244,6 +253,29 @@ func V3*(p: ExecutionPayload): ExecutionPayloadV3 =
excessBlobGas: p.excessBlobGas.get(0.Quantity)
)

func V4*(p: ExecutionPayload): ExecutionPayloadV4 =
ExecutionPayloadV4(
parentHash: p.parentHash,
feeRecipient: p.feeRecipient,
stateRoot: p.stateRoot,
receiptsRoot: p.receiptsRoot,
logsBloom: p.logsBloom,
prevRandao: p.prevRandao,
blockNumber: p.blockNumber,
gasLimit: p.gasLimit,
gasUsed: p.gasUsed,
timestamp: p.timestamp,
extraData: p.extraData,
baseFeePerGas: p.baseFeePerGas,
blockHash: p.blockHash,
transactions: p.transactions,
withdrawals: p.withdrawals.get,
blobGasUsed: p.blobGasUsed.get(0.Quantity),
excessBlobGas: p.excessBlobGas.get(0.Quantity),
depositReceipts: p.depositReceipts.get(newSeq[DepositReceiptV1]()),
exits: p.exits.get(newSeq[ExitV1]())
)

func V1*(p: ExecutionPayloadV1OrV2): ExecutionPayloadV1 =
ExecutionPayloadV1(
parentHash: p.parentHash,
Expand Down Expand Up @@ -339,6 +371,29 @@ func executionPayload*(p: ExecutionPayloadV3): ExecutionPayload =
excessBlobGas: some(p.excessBlobGas)
)

func executionPayload*(p: ExecutionPayloadV4): ExecutionPayload =
ExecutionPayload(
parentHash: p.parentHash,
feeRecipient: p.feeRecipient,
stateRoot: p.stateRoot,
receiptsRoot: p.receiptsRoot,
logsBloom: p.logsBloom,
prevRandao: p.prevRandao,
blockNumber: p.blockNumber,
gasLimit: p.gasLimit,
gasUsed: p.gasUsed,
timestamp: p.timestamp,
extraData: p.extraData,
baseFeePerGas: p.baseFeePerGas,
blockHash: p.blockHash,
transactions: p.transactions,
withdrawals: some(p.withdrawals),
blobGasUsed: some(p.blobGasUsed),
excessBlobGas: some(p.excessBlobGas),
depositReceipts: some(p.depositReceipts),
exits: some(p.exits)
)

func executionPayload*(p: ExecutionPayloadV1OrV2): ExecutionPayload =
ExecutionPayload(
parentHash: p.parentHash,
Expand Down Expand Up @@ -375,6 +430,14 @@ func V3*(res: GetPayloadResponse): GetPayloadV3Response =
shouldOverrideBuilder: res.shouldOverrideBuilder.get(false)
)

func V4*(res: GetPayloadResponse): GetPayloadV4Response =
GetPayloadV4Response(
executionPayload: res.executionPayload.V4,
blockValue: res.blockValue.get,
blobsBundle: res.blobsBundle.get(BlobsBundleV1()),
shouldOverrideBuilder: res.shouldOverrideBuilder.get(false)
)

func getPayloadResponse*(x: ExecutionPayloadV1): GetPayloadResponse =
GetPayloadResponse(executionPayload: x.executionPayload)

Expand All @@ -391,3 +454,12 @@ func getPayloadResponse*(x: GetPayloadV3Response): GetPayloadResponse =
blobsBundle: some(x.blobsBundle),
shouldOverrideBuilder: some(x.shouldOverrideBuilder)
)

func getPayloadResponse*(x: GetPayloadV4Response): GetPayloadResponse =
GetPayloadResponse(
executionPayload: x.executionPayload.executionPayload,
blockValue: some(x.blockValue),
blobsBundle: some(x.blobsBundle),
shouldOverrideBuilder: some(x.shouldOverrideBuilder)
)

0 comments on commit e049d14

Please sign in to comment.