Skip to content

Commit

Permalink
Convert NewPayloadRequest to use Reference
Browse files Browse the repository at this point in the history
  • Loading branch information
ethDreamer committed Feb 6, 2024
1 parent 7bec3f9 commit 27716c0
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 28 deletions.
44 changes: 27 additions & 17 deletions beacon_node/execution_layer/src/engine_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -586,20 +586,20 @@ impl<E: EthSpec> ExecutionPayloadBodyV1<E> {
)
)]
#[derive(Clone, Debug, PartialEq)]
pub struct NewPayloadRequest<E: EthSpec> {
pub struct NewPayloadRequest<'block, E: EthSpec> {
#[superstruct(only(Merge), partial_getter(rename = "execution_payload_merge"))]
pub execution_payload: ExecutionPayloadMerge<E>,
pub execution_payload: &'block ExecutionPayloadMerge<E>,
#[superstruct(only(Capella), partial_getter(rename = "execution_payload_capella"))]
pub execution_payload: ExecutionPayloadCapella<E>,
pub execution_payload: &'block ExecutionPayloadCapella<E>,
#[superstruct(only(Deneb), partial_getter(rename = "execution_payload_deneb"))]
pub execution_payload: ExecutionPayloadDeneb<E>,
pub execution_payload: &'block ExecutionPayloadDeneb<E>,
#[superstruct(only(Deneb))]
pub versioned_hashes: Vec<VersionedHash>,
#[superstruct(only(Deneb))]
pub parent_beacon_block_root: Hash256,
}

impl<E: EthSpec> NewPayloadRequest<E> {
impl<'block, E: EthSpec> NewPayloadRequest<'block, E> {
pub fn parent_hash(&self) -> ExecutionBlockHash {
match self {
Self::Merge(payload) => payload.execution_payload.parent_hash,
Expand All @@ -624,14 +624,24 @@ impl<E: EthSpec> NewPayloadRequest<E> {
}
}

pub fn execution_payload_ref(&self) -> ExecutionPayloadRef<'block, E> {
match self {
Self::Merge(request) => ExecutionPayloadRef::Merge(request.execution_payload),
Self::Capella(request) => ExecutionPayloadRef::Capella(request.execution_payload),
Self::Deneb(request) => ExecutionPayloadRef::Deneb(request.execution_payload),
}
}

pub fn into_execution_payload(self) -> ExecutionPayload<E> {
map_new_payload_request_into_execution_payload!(self, |request, cons| {
cons(request.execution_payload)
})
match self {
Self::Merge(request) => ExecutionPayload::Merge(request.execution_payload.clone()),
Self::Capella(request) => ExecutionPayload::Capella(request.execution_payload.clone()),
Self::Deneb(request) => ExecutionPayload::Deneb(request.execution_payload.clone()),
}
}
}

impl<'a, E: EthSpec> TryFrom<BeaconBlockRef<'a, E>> for NewPayloadRequest<E> {
impl<'a, E: EthSpec> TryFrom<BeaconBlockRef<'a, E>> for NewPayloadRequest<'a, E> {
type Error = BeaconStateError;

fn try_from(block: BeaconBlockRef<'a, E>) -> Result<Self, Self::Error> {
Expand All @@ -640,13 +650,13 @@ impl<'a, E: EthSpec> TryFrom<BeaconBlockRef<'a, E>> for NewPayloadRequest<E> {
Err(Self::Error::IncorrectStateVariant)
}
BeaconBlockRef::Merge(block_ref) => Ok(Self::Merge(NewPayloadRequestMerge {
execution_payload: block_ref.body.execution_payload.execution_payload.clone(),
execution_payload: &block_ref.body.execution_payload.execution_payload,
})),
BeaconBlockRef::Capella(block_ref) => Ok(Self::Capella(NewPayloadRequestCapella {
execution_payload: block_ref.body.execution_payload.execution_payload.clone(),
execution_payload: &block_ref.body.execution_payload.execution_payload,
})),
BeaconBlockRef::Deneb(block_ref) => Ok(Self::Deneb(NewPayloadRequestDeneb {
execution_payload: block_ref.body.execution_payload.execution_payload.clone(),
execution_payload: &block_ref.body.execution_payload.execution_payload,
versioned_hashes: block_ref
.body
.blob_kzg_commitments
Expand All @@ -659,18 +669,18 @@ impl<'a, E: EthSpec> TryFrom<BeaconBlockRef<'a, E>> for NewPayloadRequest<E> {
}
}

impl<E: EthSpec> TryFrom<ExecutionPayload<E>> for NewPayloadRequest<E> {
impl<'a, E: EthSpec> TryFrom<ExecutionPayloadRef<'a, E>> for NewPayloadRequest<'a, E> {
type Error = BeaconStateError;

fn try_from(payload: ExecutionPayload<E>) -> Result<Self, Self::Error> {
fn try_from(payload: ExecutionPayloadRef<'a, E>) -> Result<Self, Self::Error> {
match payload {
ExecutionPayload::Merge(payload) => Ok(Self::Merge(NewPayloadRequestMerge {
ExecutionPayloadRef::Merge(payload) => Ok(Self::Merge(NewPayloadRequestMerge {
execution_payload: payload,
})),
ExecutionPayload::Capella(payload) => Ok(Self::Capella(NewPayloadRequestCapella {
ExecutionPayloadRef::Capella(payload) => Ok(Self::Capella(NewPayloadRequestCapella {
execution_payload: payload,
})),
ExecutionPayload::Deneb(_) => Err(Self::Error::IncorrectStateVariant),
ExecutionPayloadRef::Deneb(_) => Err(Self::Error::IncorrectStateVariant),
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions beacon_node/execution_layer/src/engine_api/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -803,10 +803,10 @@ impl HttpJsonRpc {

pub async fn new_payload_v3<T: EthSpec>(
&self,
new_payload_request_deneb: NewPayloadRequestDeneb<T>,
new_payload_request_deneb: NewPayloadRequestDeneb<'_, T>,
) -> Result<PayloadStatusV1, Error> {
let params = json!([
JsonExecutionPayload::V3(new_payload_request_deneb.execution_payload.into()),
JsonExecutionPayload::V3(new_payload_request_deneb.execution_payload.clone().into()),
new_payload_request_deneb.versioned_hashes,
new_payload_request_deneb.parent_beacon_block_root,
]);
Expand Down Expand Up @@ -1079,7 +1079,7 @@ impl HttpJsonRpc {
// new_payload that the execution engine supports
pub async fn new_payload<T: EthSpec>(
&self,
new_payload_request: NewPayloadRequest<T>,
new_payload_request: NewPayloadRequest<'_, T>,
) -> Result<PayloadStatusV1, Error> {
let engine_capabilities = self.get_engine_capabilities(None).await?;
match new_payload_request {
Expand Down
2 changes: 1 addition & 1 deletion beacon_node/execution_layer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1321,7 +1321,7 @@ impl<T: EthSpec> ExecutionLayer<T> {
/// Maps to the `engine_newPayload` JSON-RPC call.
pub async fn notify_new_payload(
&self,
new_payload_request: NewPayloadRequest<T>,
new_payload_request: NewPayloadRequest<'_, T>,
) -> Result<PayloadStatus, Error> {
let _timer = metrics::start_timer_vec(
&metrics::EXECUTION_LAYER_REQUEST_TIMES,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ impl<T: EthSpec> MockExecutionLayer<T> {
// TODO: again consider forks
let status = self
.el
.notify_new_payload(payload.try_into().unwrap())
.notify_new_payload(payload.to_ref().try_into().unwrap())
.await
.unwrap();
assert_eq!(status, PayloadStatus::Valid);
Expand Down
12 changes: 6 additions & 6 deletions testing/execution_engine_integration/src/test_rig.rs
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ impl<E: GenericExecutionEngine> TestRig<E> {
let status = self
.ee_a
.execution_layer
.notify_new_payload(valid_payload.clone().try_into().unwrap())
.notify_new_payload(valid_payload.to_ref().try_into().unwrap())
.await
.unwrap();
assert_eq!(status, PayloadStatus::Valid);
Expand Down Expand Up @@ -435,7 +435,7 @@ impl<E: GenericExecutionEngine> TestRig<E> {
let status = self
.ee_a
.execution_layer
.notify_new_payload(invalid_payload.try_into().unwrap())
.notify_new_payload(invalid_payload.to_ref().try_into().unwrap())
.await
.unwrap();
assert!(matches!(
Expand Down Expand Up @@ -507,7 +507,7 @@ impl<E: GenericExecutionEngine> TestRig<E> {
let status = self
.ee_a
.execution_layer
.notify_new_payload(second_payload.clone().try_into().unwrap())
.notify_new_payload(second_payload.to_ref().try_into().unwrap())
.await
.unwrap();
assert_eq!(status, PayloadStatus::Valid);
Expand Down Expand Up @@ -559,7 +559,7 @@ impl<E: GenericExecutionEngine> TestRig<E> {
let status = self
.ee_b
.execution_layer
.notify_new_payload(second_payload.clone().try_into().unwrap())
.notify_new_payload(second_payload.to_ref().try_into().unwrap())
.await
.unwrap();
assert!(matches!(status, PayloadStatus::Syncing));
Expand Down Expand Up @@ -597,7 +597,7 @@ impl<E: GenericExecutionEngine> TestRig<E> {
let status = self
.ee_b
.execution_layer
.notify_new_payload(valid_payload.clone().try_into().unwrap())
.notify_new_payload(valid_payload.to_ref().try_into().unwrap())
.await
.unwrap();
assert_eq!(status, PayloadStatus::Valid);
Expand All @@ -611,7 +611,7 @@ impl<E: GenericExecutionEngine> TestRig<E> {
let status = self
.ee_b
.execution_layer
.notify_new_payload(second_payload.clone().try_into().unwrap())
.notify_new_payload(second_payload.to_ref().try_into().unwrap())
.await
.unwrap();
assert_eq!(status, PayloadStatus::Valid);
Expand Down

0 comments on commit 27716c0

Please sign in to comment.