From 675723a44ff105e4c7e6114628073af9f37c4735 Mon Sep 17 00:00:00 2001 From: Matthias Seitz Date: Fri, 27 Dec 2024 16:01:07 +0100 Subject: [PATCH] feat: misc payloadenvelopeinput conversions (#1855) --- crates/rpc-types-engine/src/payload.rs | 33 ++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/crates/rpc-types-engine/src/payload.rs b/crates/rpc-types-engine/src/payload.rs index b34a84aa1c9..c5b539cc9b5 100644 --- a/crates/rpc-types-engine/src/payload.rs +++ b/crates/rpc-types-engine/src/payload.rs @@ -83,6 +83,25 @@ pub struct ExecutionPayloadInputV2 { pub withdrawals: Option>, } +impl ExecutionPayloadInputV2 { + /// Converts [`ExecutionPayloadInputV2`] to [`ExecutionPayload`] + pub fn into_payload(self) -> ExecutionPayload { + match self.withdrawals { + Some(withdrawals) => ExecutionPayload::V2(ExecutionPayloadV2 { + payload_inner: self.execution_payload, + withdrawals, + }), + None => ExecutionPayload::V1(self.execution_payload), + } + } +} + +impl From for ExecutionPayload { + fn from(input: ExecutionPayloadInputV2) -> Self { + input.into_payload() + } +} + /// This structure maps for the return value of `engine_getPayload` of the beacon chain spec, for /// V2. /// @@ -299,6 +318,20 @@ impl ExecutionPayloadV2 { self.payload_inner.timestamp } + /// Converts [`ExecutionPayloadV2`] to [`ExecutionPayloadInputV2`]. + /// + /// An [`ExecutionPayloadInputV2`] should have a [`Some`] withdrawals field if shanghai is + /// active, otherwise the withdrawals field should be [`None`], so the `is_shanghai_active` + /// argument is provided which will either: + /// - include the withdrawals field as [`Some`] if true + /// - set the withdrawals field to [`None`] if false + pub fn into_payload_input_v2(self, is_shanghai_active: bool) -> ExecutionPayloadInputV2 { + ExecutionPayloadInputV2 { + execution_payload: self.payload_inner, + withdrawals: is_shanghai_active.then_some(self.withdrawals), + } + } + /// Converts [`ExecutionPayloadV2`] to [`Block`]. /// /// This performs the same conversion as the underlying V1 payload, but calculates the