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 Nov 2, 2024
1 parent 6f441a9 commit 182aac5
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 5 deletions.
31 changes: 28 additions & 3 deletions web3/engine_api.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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]] =
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
targetBlobsPerBlock*: Quantity
maxBlobsPerBlock*: 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]
targetBlobsPerBlock*: Opt[Quantity]
maxBlobsPerBlock*: 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.targetBlobsPerBlock.isSome or attr.maxBlobsPerBlock.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,
targetBlobsPerBlock: attr.targetBlobsPerBlock.get,
maxBlobsPerBlock: attr.maxBlobsPerBlock.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),
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)
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 182aac5

Please sign in to comment.