Skip to content

Commit

Permalink
Implementation of EIP-7742 for Pectra
Browse files Browse the repository at this point in the history
  • Loading branch information
jangko committed Oct 21, 2024
1 parent c76ddef commit c0c6337
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 2 deletions.
7 changes: 7 additions & 0 deletions web3/engine_api.nim
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ createRpcSigsFromNim(RpcClient):
executionRequests: Opt[array[3, seq[byte]]]): 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,
Expand All @@ -77,6 +78,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,
Expand Down
12 changes: 11 additions & 1 deletion web3/engine_api_types.nim
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,15 @@ type
withdrawals*: seq[WithdrawalV1]
parentBeaconBlockRoot*: Hash32

PayloadAttributesV4* = object
timestamp*: Quantity
prevRandao*: Bytes32
suggestedFeeRecipient*: Address
withdrawals*: seq[WithdrawalV1]
parentBeaconBlockRoot*: Hash32
targetBlobCount*: Quantity
maximumBlobCount*: Quantity

# This is ugly, but see the comment on ExecutionPayloadV1OrV2.
PayloadAttributesV1OrV2* = object
timestamp*: Quantity
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions web3/eth_api_types.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
37 changes: 36 additions & 1 deletion web3/execution_types.nim
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ type
suggestedFeeRecipient*: Address
withdrawals*: Opt[seq[WithdrawalV1]]
parentBeaconBlockRoot*: Opt[Hash32]
targetBlobCount*: Opt[Quantity]
maximumBlobCount*: Opt[Quantity]

SomeOptionalPayloadAttributes* =
Opt[PayloadAttributesV1] |
Expand Down Expand Up @@ -71,7 +73,9 @@ func version*(payload: ExecutionPayload): Version =
Version.V1

func version*(attr: PayloadAttributes): Version =
if attr.parentBeaconBlockRoot.isSome:
if attr.targetBlobCount.isSome or attr.maximumBlobCount.isSome:
Version.V4
elif attr.parentBeaconBlockRoot.isSome:
Version.V3
elif attr.withdrawals.isSome:
Version.V2
Expand Down Expand Up @@ -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,
targetBlobCount: attr.targetBlobCount.get,
maximumBlobCount: attr.maximumBlobCount.get,
)

func V1*(attr: Opt[PayloadAttributes]): Opt[PayloadAttributesV1] =
if attr.isNone:
return Opt.none(PayloadAttributesV1)
Expand All @@ -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,
Expand All @@ -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),
targetBlobCount: Opt.some(attr.targetBlobCount),
maximumBlobCount: Opt.some(attr.maximumBlobCount),
)

func payloadAttributes*(x: Opt[PayloadAttributesV1]): Opt[PayloadAttributes] =
if x.isNone: Opt.none(PayloadAttributes)
else: Opt.some(payloadAttributes x.get)
Expand All @@ -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,
Expand Down

0 comments on commit c0c6337

Please sign in to comment.