mirror of
https://github.com/sigp/lighthouse.git
synced 2026-04-18 21:38:31 +00:00
ContextDeserialize and Beacon API Improvements (#7372)
* #7286 * BeaconAPI is not returning a versioned response when it should for some V1 endpoints * these [strange functions with vX in the name that still accept `endpoint_version` arguments](https://github.com/sigp/lighthouse/blob/stable/beacon_node/http_api/src/produce_block.rs#L192) This refactor is a prerequisite to get the fulu EF tests running.
This commit is contained in:
@@ -16,7 +16,7 @@ pub mod types;
|
||||
|
||||
use self::mixin::{RequestAccept, ResponseOptional};
|
||||
use self::types::{Error as ResponseError, *};
|
||||
use ::types::fork_versioned_response::ExecutionOptimisticFinalizedForkVersionedResponse;
|
||||
use ::types::beacon_response::ExecutionOptimisticFinalizedBeaconResponse;
|
||||
use derivative::Derivative;
|
||||
use either::Either;
|
||||
use futures::Stream;
|
||||
@@ -283,6 +283,52 @@ impl BeaconNodeHttpClient {
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn get_fork_contextual<T, U, Ctx, Meta>(
|
||||
&self,
|
||||
url: U,
|
||||
ctx_constructor: impl Fn(ForkName) -> Ctx,
|
||||
) -> Result<Option<ForkVersionedResponse<T, Meta>>, Error>
|
||||
where
|
||||
U: IntoUrl,
|
||||
T: ContextDeserialize<'static, Ctx>,
|
||||
Meta: DeserializeOwned,
|
||||
Ctx: Clone,
|
||||
{
|
||||
let response = self
|
||||
.get_response(url, |b| b.accept(Accept::Json))
|
||||
.await
|
||||
.optional()?;
|
||||
|
||||
let Some(resp) = response else {
|
||||
return Ok(None);
|
||||
};
|
||||
|
||||
let bytes = resp.bytes().await?;
|
||||
|
||||
#[derive(serde::Deserialize)]
|
||||
struct Helper {
|
||||
version: ForkName,
|
||||
#[serde(flatten)]
|
||||
metadata: serde_json::Value,
|
||||
data: serde_json::Value,
|
||||
}
|
||||
|
||||
let helper: Helper = serde_json::from_slice(&bytes).map_err(Error::InvalidJson)?;
|
||||
|
||||
let metadata: Meta = serde_json::from_value(helper.metadata).map_err(Error::InvalidJson)?;
|
||||
|
||||
let ctx = ctx_constructor(helper.version);
|
||||
|
||||
let data: T = ContextDeserialize::context_deserialize(helper.data, ctx)
|
||||
.map_err(Error::InvalidJson)?;
|
||||
|
||||
Ok(Some(ForkVersionedResponse {
|
||||
version: helper.version,
|
||||
metadata,
|
||||
data,
|
||||
}))
|
||||
}
|
||||
|
||||
/// Perform a HTTP GET request using an 'accept' header, returning `None` on a 404 error.
|
||||
pub async fn get_bytes_opt_accept_header<U: IntoUrl>(
|
||||
&self,
|
||||
@@ -850,7 +896,7 @@ impl BeaconNodeHttpClient {
|
||||
&self,
|
||||
start_period: u64,
|
||||
count: u64,
|
||||
) -> Result<Option<Vec<ForkVersionedResponse<LightClientUpdate<E>>>>, Error> {
|
||||
) -> Result<Option<Vec<BeaconResponse<LightClientUpdate<E>>>>, Error> {
|
||||
let mut path = self.eth_path(V1)?;
|
||||
|
||||
path.path_segments_mut()
|
||||
@@ -865,7 +911,14 @@ impl BeaconNodeHttpClient {
|
||||
path.query_pairs_mut()
|
||||
.append_pair("count", &count.to_string());
|
||||
|
||||
self.get_opt(path).await
|
||||
self.get_opt(path).await.map(|opt| {
|
||||
opt.map(|updates: Vec<_>| {
|
||||
updates
|
||||
.into_iter()
|
||||
.map(BeaconResponse::ForkVersioned)
|
||||
.collect()
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
/// `GET beacon/light_client/bootstrap`
|
||||
@@ -874,7 +927,7 @@ impl BeaconNodeHttpClient {
|
||||
pub async fn get_light_client_bootstrap<E: EthSpec>(
|
||||
&self,
|
||||
block_root: Hash256,
|
||||
) -> Result<Option<ForkVersionedResponse<LightClientBootstrap<E>>>, Error> {
|
||||
) -> Result<Option<BeaconResponse<LightClientBootstrap<E>>>, Error> {
|
||||
let mut path = self.eth_path(V1)?;
|
||||
|
||||
path.path_segments_mut()
|
||||
@@ -884,7 +937,9 @@ impl BeaconNodeHttpClient {
|
||||
.push("bootstrap")
|
||||
.push(&format!("{:?}", block_root));
|
||||
|
||||
self.get_opt(path).await
|
||||
self.get_opt(path)
|
||||
.await
|
||||
.map(|opt| opt.map(BeaconResponse::ForkVersioned))
|
||||
}
|
||||
|
||||
/// `GET beacon/light_client/optimistic_update`
|
||||
@@ -892,7 +947,7 @@ impl BeaconNodeHttpClient {
|
||||
/// Returns `Ok(None)` on a 404 error.
|
||||
pub async fn get_beacon_light_client_optimistic_update<E: EthSpec>(
|
||||
&self,
|
||||
) -> Result<Option<ForkVersionedResponse<LightClientOptimisticUpdate<E>>>, Error> {
|
||||
) -> Result<Option<BeaconResponse<LightClientOptimisticUpdate<E>>>, Error> {
|
||||
let mut path = self.eth_path(V1)?;
|
||||
|
||||
path.path_segments_mut()
|
||||
@@ -901,7 +956,9 @@ impl BeaconNodeHttpClient {
|
||||
.push("light_client")
|
||||
.push("optimistic_update");
|
||||
|
||||
self.get_opt(path).await
|
||||
self.get_opt(path)
|
||||
.await
|
||||
.map(|opt| opt.map(BeaconResponse::ForkVersioned))
|
||||
}
|
||||
|
||||
/// `GET beacon/light_client/finality_update`
|
||||
@@ -909,7 +966,7 @@ impl BeaconNodeHttpClient {
|
||||
/// Returns `Ok(None)` on a 404 error.
|
||||
pub async fn get_beacon_light_client_finality_update<E: EthSpec>(
|
||||
&self,
|
||||
) -> Result<Option<ForkVersionedResponse<LightClientFinalityUpdate<E>>>, Error> {
|
||||
) -> Result<Option<BeaconResponse<LightClientFinalityUpdate<E>>>, Error> {
|
||||
let mut path = self.eth_path(V1)?;
|
||||
|
||||
path.path_segments_mut()
|
||||
@@ -918,7 +975,9 @@ impl BeaconNodeHttpClient {
|
||||
.push("light_client")
|
||||
.push("finality_update");
|
||||
|
||||
self.get_opt(path).await
|
||||
self.get_opt(path)
|
||||
.await
|
||||
.map(|opt| opt.map(BeaconResponse::ForkVersioned))
|
||||
}
|
||||
|
||||
/// `GET beacon/headers?slot,parent_root`
|
||||
@@ -981,8 +1040,14 @@ impl BeaconNodeHttpClient {
|
||||
.push("beacon")
|
||||
.push("blocks");
|
||||
|
||||
self.post_with_timeout(path, block_contents, self.timeouts.proposal)
|
||||
.await?;
|
||||
let fork_name = block_contents.signed_block().fork_name_unchecked();
|
||||
self.post_generic_with_consensus_version(
|
||||
path,
|
||||
block_contents,
|
||||
Some(self.timeouts.proposal),
|
||||
fork_name,
|
||||
)
|
||||
.await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@@ -1200,16 +1265,12 @@ impl BeaconNodeHttpClient {
|
||||
pub async fn get_beacon_blocks<E: EthSpec>(
|
||||
&self,
|
||||
block_id: BlockId,
|
||||
) -> Result<
|
||||
Option<ExecutionOptimisticFinalizedForkVersionedResponse<SignedBeaconBlock<E>>>,
|
||||
Error,
|
||||
> {
|
||||
) -> Result<Option<ExecutionOptimisticFinalizedBeaconResponse<SignedBeaconBlock<E>>>, Error>
|
||||
{
|
||||
let path = self.get_beacon_blocks_path(block_id)?;
|
||||
let Some(response) = self.get_response(path, |b| b).await.optional()? else {
|
||||
return Ok(None);
|
||||
};
|
||||
|
||||
Ok(Some(response.json().await?))
|
||||
self.get_opt(path)
|
||||
.await
|
||||
.map(|opt| opt.map(BeaconResponse::ForkVersioned))
|
||||
}
|
||||
|
||||
/// `GET v1/beacon/blob_sidecars/{block_id}`
|
||||
@@ -1219,8 +1280,8 @@ impl BeaconNodeHttpClient {
|
||||
&self,
|
||||
block_id: BlockId,
|
||||
indices: Option<&[u64]>,
|
||||
) -> Result<Option<ExecutionOptimisticFinalizedForkVersionedResponse<BlobSidecarList<E>>>, Error>
|
||||
{
|
||||
spec: &ChainSpec,
|
||||
) -> Result<Option<ExecutionOptimisticFinalizedBeaconResponse<BlobSidecarList<E>>>, Error> {
|
||||
let mut path = self.get_blobs_path(block_id)?;
|
||||
if let Some(indices) = indices {
|
||||
let indices_string = indices
|
||||
@@ -1232,11 +1293,11 @@ impl BeaconNodeHttpClient {
|
||||
.append_pair("indices", &indices_string);
|
||||
}
|
||||
|
||||
let Some(response) = self.get_response(path, |b| b).await.optional()? else {
|
||||
return Ok(None);
|
||||
};
|
||||
|
||||
Ok(Some(response.json().await?))
|
||||
self.get_fork_contextual(path, |fork| {
|
||||
(fork, spec.max_blobs_per_block_by_fork(fork) as usize)
|
||||
})
|
||||
.await
|
||||
.map(|opt| opt.map(BeaconResponse::ForkVersioned))
|
||||
}
|
||||
|
||||
/// `GET v1/beacon/blinded_blocks/{block_id}`
|
||||
@@ -1246,15 +1307,13 @@ impl BeaconNodeHttpClient {
|
||||
&self,
|
||||
block_id: BlockId,
|
||||
) -> Result<
|
||||
Option<ExecutionOptimisticFinalizedForkVersionedResponse<SignedBlindedBeaconBlock<E>>>,
|
||||
Option<ExecutionOptimisticFinalizedBeaconResponse<SignedBlindedBeaconBlock<E>>>,
|
||||
Error,
|
||||
> {
|
||||
let path = self.get_beacon_blinded_blocks_path(block_id)?;
|
||||
let Some(response) = self.get_response(path, |b| b).await.optional()? else {
|
||||
return Ok(None);
|
||||
};
|
||||
|
||||
Ok(Some(response.json().await?))
|
||||
self.get_opt(path)
|
||||
.await
|
||||
.map(|opt| opt.map(BeaconResponse::ForkVersioned))
|
||||
}
|
||||
|
||||
/// `GET v1/beacon/blocks` (LEGACY)
|
||||
@@ -1263,7 +1322,7 @@ impl BeaconNodeHttpClient {
|
||||
pub async fn get_beacon_blocks_v1<E: EthSpec>(
|
||||
&self,
|
||||
block_id: BlockId,
|
||||
) -> Result<Option<ForkVersionedResponse<SignedBeaconBlock<E>>>, Error> {
|
||||
) -> Result<Option<BeaconResponse<SignedBeaconBlock<E>>>, Error> {
|
||||
let mut path = self.eth_path(V1)?;
|
||||
|
||||
path.path_segments_mut()
|
||||
@@ -1272,7 +1331,9 @@ impl BeaconNodeHttpClient {
|
||||
.push("blocks")
|
||||
.push(&block_id.to_string());
|
||||
|
||||
self.get_opt(path).await
|
||||
self.get_opt(path)
|
||||
.await
|
||||
.map(|opt| opt.map(BeaconResponse::Unversioned))
|
||||
}
|
||||
|
||||
/// `GET beacon/blocks` as SSZ
|
||||
@@ -1353,7 +1414,7 @@ impl BeaconNodeHttpClient {
|
||||
pub async fn get_beacon_blocks_attestations_v2<E: EthSpec>(
|
||||
&self,
|
||||
block_id: BlockId,
|
||||
) -> Result<Option<ExecutionOptimisticFinalizedForkVersionedResponse<Vec<Attestation<E>>>>, Error>
|
||||
) -> Result<Option<ExecutionOptimisticFinalizedBeaconResponse<Vec<Attestation<E>>>>, Error>
|
||||
{
|
||||
let mut path = self.eth_path(V2)?;
|
||||
|
||||
@@ -1364,7 +1425,9 @@ impl BeaconNodeHttpClient {
|
||||
.push(&block_id.to_string())
|
||||
.push("attestations");
|
||||
|
||||
self.get_opt(path).await
|
||||
self.get_opt(path)
|
||||
.await
|
||||
.map(|opt| opt.map(BeaconResponse::ForkVersioned))
|
||||
}
|
||||
|
||||
/// `POST v1/beacon/pool/attestations`
|
||||
@@ -1456,7 +1519,7 @@ impl BeaconNodeHttpClient {
|
||||
&self,
|
||||
slot: Option<Slot>,
|
||||
committee_index: Option<u64>,
|
||||
) -> Result<ForkVersionedResponse<Vec<Attestation<E>>>, Error> {
|
||||
) -> Result<BeaconResponse<Vec<Attestation<E>>>, Error> {
|
||||
let mut path = self.eth_path(V2)?;
|
||||
|
||||
path.path_segments_mut()
|
||||
@@ -1475,7 +1538,7 @@ impl BeaconNodeHttpClient {
|
||||
.append_pair("committee_index", &index.to_string());
|
||||
}
|
||||
|
||||
self.get(path).await
|
||||
self.get(path).await.map(BeaconResponse::ForkVersioned)
|
||||
}
|
||||
|
||||
/// `POST v1/beacon/pool/attester_slashings`
|
||||
@@ -1534,7 +1597,7 @@ impl BeaconNodeHttpClient {
|
||||
/// `GET v2/beacon/pool/attester_slashings`
|
||||
pub async fn get_beacon_pool_attester_slashings_v2<E: EthSpec>(
|
||||
&self,
|
||||
) -> Result<ForkVersionedResponse<Vec<AttesterSlashing<E>>>, Error> {
|
||||
) -> Result<BeaconResponse<Vec<AttesterSlashing<E>>>, Error> {
|
||||
let mut path = self.eth_path(V2)?;
|
||||
|
||||
path.path_segments_mut()
|
||||
@@ -1543,7 +1606,7 @@ impl BeaconNodeHttpClient {
|
||||
.push("pool")
|
||||
.push("attester_slashings");
|
||||
|
||||
self.get(path).await
|
||||
self.get(path).await.map(BeaconResponse::ForkVersioned)
|
||||
}
|
||||
|
||||
/// `POST beacon/pool/proposer_slashings`
|
||||
@@ -1964,10 +2027,11 @@ impl BeaconNodeHttpClient {
|
||||
pub async fn get_debug_beacon_states<E: EthSpec>(
|
||||
&self,
|
||||
state_id: StateId,
|
||||
) -> Result<Option<ExecutionOptimisticFinalizedForkVersionedResponse<BeaconState<E>>>, Error>
|
||||
{
|
||||
) -> Result<Option<ExecutionOptimisticFinalizedBeaconResponse<BeaconState<E>>>, Error> {
|
||||
let path = self.get_debug_beacon_states_path(state_id)?;
|
||||
self.get_opt(path).await
|
||||
self.get_opt(path)
|
||||
.await
|
||||
.map(|opt| opt.map(BeaconResponse::ForkVersioned))
|
||||
}
|
||||
|
||||
/// `GET debug/beacon/states/{state_id}`
|
||||
@@ -2051,7 +2115,7 @@ impl BeaconNodeHttpClient {
|
||||
slot: Slot,
|
||||
randao_reveal: &SignatureBytes,
|
||||
graffiti: Option<&Graffiti>,
|
||||
) -> Result<ForkVersionedResponse<FullBlockContents<E>>, Error> {
|
||||
) -> Result<BeaconResponse<FullBlockContents<E>>, Error> {
|
||||
self.get_validator_blocks_modular(slot, randao_reveal, graffiti, SkipRandaoVerification::No)
|
||||
.await
|
||||
}
|
||||
@@ -2063,12 +2127,12 @@ impl BeaconNodeHttpClient {
|
||||
randao_reveal: &SignatureBytes,
|
||||
graffiti: Option<&Graffiti>,
|
||||
skip_randao_verification: SkipRandaoVerification,
|
||||
) -> Result<ForkVersionedResponse<FullBlockContents<E>>, Error> {
|
||||
) -> Result<BeaconResponse<FullBlockContents<E>>, Error> {
|
||||
let path = self
|
||||
.get_validator_blocks_path::<E>(slot, randao_reveal, graffiti, skip_randao_verification)
|
||||
.await?;
|
||||
|
||||
self.get(path).await
|
||||
self.get(path).await.map(BeaconResponse::ForkVersioned)
|
||||
}
|
||||
|
||||
/// returns `GET v2/validator/blocks/{slot}` URL path
|
||||
@@ -2324,7 +2388,7 @@ impl BeaconNodeHttpClient {
|
||||
slot: Slot,
|
||||
randao_reveal: &SignatureBytes,
|
||||
graffiti: Option<&Graffiti>,
|
||||
) -> Result<ForkVersionedResponse<BlindedBeaconBlock<E>>, Error> {
|
||||
) -> Result<BeaconResponse<BlindedBeaconBlock<E>>, Error> {
|
||||
self.get_validator_blinded_blocks_modular(
|
||||
slot,
|
||||
randao_reveal,
|
||||
@@ -2373,7 +2437,7 @@ impl BeaconNodeHttpClient {
|
||||
randao_reveal: &SignatureBytes,
|
||||
graffiti: Option<&Graffiti>,
|
||||
skip_randao_verification: SkipRandaoVerification,
|
||||
) -> Result<ForkVersionedResponse<BlindedBeaconBlock<E>>, Error> {
|
||||
) -> Result<BeaconResponse<BlindedBeaconBlock<E>>, Error> {
|
||||
let path = self
|
||||
.get_validator_blinded_blocks_path::<E>(
|
||||
slot,
|
||||
@@ -2383,7 +2447,7 @@ impl BeaconNodeHttpClient {
|
||||
)
|
||||
.await?;
|
||||
|
||||
self.get(path).await
|
||||
self.get(path).await.map(BeaconResponse::ForkVersioned)
|
||||
}
|
||||
|
||||
/// `GET v2/validator/blinded_blocks/{slot}` in ssz format
|
||||
@@ -2472,7 +2536,7 @@ impl BeaconNodeHttpClient {
|
||||
slot: Slot,
|
||||
attestation_data_root: Hash256,
|
||||
committee_index: CommitteeIndex,
|
||||
) -> Result<Option<ForkVersionedResponse<Attestation<E>>>, Error> {
|
||||
) -> Result<Option<BeaconResponse<Attestation<E>>>, Error> {
|
||||
let mut path = self.eth_path(V2)?;
|
||||
|
||||
path.path_segments_mut()
|
||||
@@ -2490,6 +2554,7 @@ impl BeaconNodeHttpClient {
|
||||
|
||||
self.get_opt_with_timeout(path, self.timeouts.attestation)
|
||||
.await
|
||||
.map(|opt| opt.map(BeaconResponse::ForkVersioned))
|
||||
}
|
||||
|
||||
/// `GET validator/sync_committee_contribution`
|
||||
|
||||
@@ -10,7 +10,6 @@ use mediatype::{names, MediaType, MediaTypeList};
|
||||
use multiaddr::Multiaddr;
|
||||
use reqwest::header::HeaderMap;
|
||||
use serde::{Deserialize, Deserializer, Serialize};
|
||||
use serde_json::Value;
|
||||
use serde_utils::quoted_u64::Quoted;
|
||||
use ssz::{Decode, DecodeError};
|
||||
use ssz_derive::{Decode, Encode};
|
||||
@@ -1050,51 +1049,56 @@ pub struct SseExtendedPayloadAttributesGeneric<T> {
|
||||
pub type SseExtendedPayloadAttributes = SseExtendedPayloadAttributesGeneric<SsePayloadAttributes>;
|
||||
pub type VersionedSsePayloadAttributes = ForkVersionedResponse<SseExtendedPayloadAttributes>;
|
||||
|
||||
impl ForkVersionDeserialize for SsePayloadAttributes {
|
||||
fn deserialize_by_fork<'de, D: serde::Deserializer<'de>>(
|
||||
value: serde_json::value::Value,
|
||||
fork_name: ForkName,
|
||||
) -> Result<Self, D::Error> {
|
||||
match fork_name {
|
||||
ForkName::Bellatrix => serde_json::from_value(value)
|
||||
.map(Self::V1)
|
||||
.map_err(serde::de::Error::custom),
|
||||
ForkName::Capella => serde_json::from_value(value)
|
||||
.map(Self::V2)
|
||||
.map_err(serde::de::Error::custom),
|
||||
ForkName::Deneb => serde_json::from_value(value)
|
||||
.map(Self::V3)
|
||||
.map_err(serde::de::Error::custom),
|
||||
ForkName::Electra => serde_json::from_value(value)
|
||||
.map(Self::V3)
|
||||
.map_err(serde::de::Error::custom),
|
||||
ForkName::Fulu => serde_json::from_value(value)
|
||||
.map(Self::V3)
|
||||
.map_err(serde::de::Error::custom),
|
||||
ForkName::Base | ForkName::Altair => Err(serde::de::Error::custom(format!(
|
||||
"SsePayloadAttributes deserialization for {fork_name} not implemented"
|
||||
))),
|
||||
}
|
||||
impl<'de> ContextDeserialize<'de, ForkName> for SsePayloadAttributes {
|
||||
fn context_deserialize<D>(deserializer: D, context: ForkName) -> Result<Self, D::Error>
|
||||
where
|
||||
D: Deserializer<'de>,
|
||||
{
|
||||
let convert_err = |e| {
|
||||
serde::de::Error::custom(format!(
|
||||
"SsePayloadAttributes failed to deserialize: {:?}",
|
||||
e
|
||||
))
|
||||
};
|
||||
Ok(match context {
|
||||
ForkName::Base | ForkName::Altair => {
|
||||
return Err(serde::de::Error::custom(format!(
|
||||
"SsePayloadAttributes failed to deserialize: unsupported fork '{}'",
|
||||
context
|
||||
)))
|
||||
}
|
||||
ForkName::Bellatrix => {
|
||||
Self::V1(Deserialize::deserialize(deserializer).map_err(convert_err)?)
|
||||
}
|
||||
ForkName::Capella => {
|
||||
Self::V2(Deserialize::deserialize(deserializer).map_err(convert_err)?)
|
||||
}
|
||||
ForkName::Deneb | ForkName::Electra | ForkName::Fulu => {
|
||||
Self::V3(Deserialize::deserialize(deserializer).map_err(convert_err)?)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl ForkVersionDeserialize for SseExtendedPayloadAttributes {
|
||||
fn deserialize_by_fork<'de, D: serde::Deserializer<'de>>(
|
||||
value: serde_json::value::Value,
|
||||
fork_name: ForkName,
|
||||
) -> Result<Self, D::Error> {
|
||||
let helper: SseExtendedPayloadAttributesGeneric<serde_json::Value> =
|
||||
serde_json::from_value(value).map_err(serde::de::Error::custom)?;
|
||||
impl<'de> ContextDeserialize<'de, ForkName> for SseExtendedPayloadAttributes {
|
||||
fn context_deserialize<D>(deserializer: D, context: ForkName) -> Result<Self, D::Error>
|
||||
where
|
||||
D: Deserializer<'de>,
|
||||
{
|
||||
let helper =
|
||||
SseExtendedPayloadAttributesGeneric::<serde_json::Value>::deserialize(deserializer)?;
|
||||
|
||||
Ok(Self {
|
||||
proposal_slot: helper.proposal_slot,
|
||||
proposer_index: helper.proposer_index,
|
||||
parent_block_root: helper.parent_block_root,
|
||||
parent_block_number: helper.parent_block_number,
|
||||
parent_block_hash: helper.parent_block_hash,
|
||||
payload_attributes: SsePayloadAttributes::deserialize_by_fork::<D>(
|
||||
payload_attributes: SsePayloadAttributes::context_deserialize(
|
||||
helper.payload_attributes,
|
||||
fork_name,
|
||||
)?,
|
||||
context,
|
||||
)
|
||||
.map_err(serde::de::Error::custom)?,
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -1112,8 +1116,8 @@ pub enum EventKind<E: EthSpec> {
|
||||
ChainReorg(SseChainReorg),
|
||||
ContributionAndProof(Box<SignedContributionAndProof<E>>),
|
||||
LateHead(SseLateHead),
|
||||
LightClientFinalityUpdate(Box<LightClientFinalityUpdate<E>>),
|
||||
LightClientOptimisticUpdate(Box<LightClientOptimisticUpdate<E>>),
|
||||
LightClientFinalityUpdate(Box<BeaconResponse<LightClientFinalityUpdate<E>>>),
|
||||
LightClientOptimisticUpdate(Box<BeaconResponse<LightClientOptimisticUpdate<E>>>),
|
||||
#[cfg(feature = "lighthouse")]
|
||||
BlockReward(BlockReward),
|
||||
PayloadAttributes(VersionedSsePayloadAttributes),
|
||||
@@ -1193,22 +1197,24 @@ impl<E: EthSpec> EventKind<E> {
|
||||
ServerError::InvalidServerSentEvent(format!("Payload Attributes: {:?}", e))
|
||||
})?,
|
||||
)),
|
||||
"light_client_finality_update" => Ok(EventKind::LightClientFinalityUpdate(
|
||||
serde_json::from_str(data).map_err(|e| {
|
||||
"light_client_finality_update" => Ok(EventKind::LightClientFinalityUpdate(Box::new(
|
||||
BeaconResponse::ForkVersioned(serde_json::from_str(data).map_err(|e| {
|
||||
ServerError::InvalidServerSentEvent(format!(
|
||||
"Light Client Finality Update: {:?}",
|
||||
e
|
||||
))
|
||||
})?,
|
||||
)),
|
||||
"light_client_optimistic_update" => Ok(EventKind::LightClientOptimisticUpdate(
|
||||
serde_json::from_str(data).map_err(|e| {
|
||||
ServerError::InvalidServerSentEvent(format!(
|
||||
"Light Client Optimistic Update: {:?}",
|
||||
e
|
||||
))
|
||||
})?,
|
||||
)),
|
||||
})?),
|
||||
))),
|
||||
"light_client_optimistic_update" => {
|
||||
Ok(EventKind::LightClientOptimisticUpdate(Box::new(
|
||||
BeaconResponse::ForkVersioned(serde_json::from_str(data).map_err(|e| {
|
||||
ServerError::InvalidServerSentEvent(format!(
|
||||
"Light Client Optimistic Update: {:?}",
|
||||
e
|
||||
))
|
||||
})?),
|
||||
)))
|
||||
}
|
||||
#[cfg(feature = "lighthouse")]
|
||||
"block_reward" => Ok(EventKind::BlockReward(serde_json::from_str(data).map_err(
|
||||
|e| ServerError::InvalidServerSentEvent(format!("Block Reward: {:?}", e)),
|
||||
@@ -1597,7 +1603,7 @@ mod tests {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Encode, Serialize, Deserialize)]
|
||||
#[derive(Debug, Encode, Serialize)]
|
||||
#[serde(untagged)]
|
||||
#[serde(bound = "E: EthSpec")]
|
||||
#[ssz(enum_behaviour = "transparent")]
|
||||
@@ -1610,7 +1616,7 @@ pub type JsonProduceBlockV3Response<E> =
|
||||
ForkVersionedResponse<ProduceBlockV3Response<E>, ProduceBlockV3Metadata>;
|
||||
|
||||
/// A wrapper over a [`BeaconBlock`] or a [`BlockContents`].
|
||||
#[derive(Debug, Encode, Serialize, Deserialize)]
|
||||
#[derive(Debug, Encode, Serialize)]
|
||||
#[serde(untagged)]
|
||||
#[serde(bound = "E: EthSpec")]
|
||||
#[ssz(enum_behaviour = "transparent")]
|
||||
@@ -1724,18 +1730,18 @@ impl<E: EthSpec> FullBlockContents<E> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<E: EthSpec> ForkVersionDeserialize for FullBlockContents<E> {
|
||||
fn deserialize_by_fork<'de, D: serde::Deserializer<'de>>(
|
||||
value: serde_json::value::Value,
|
||||
fork_name: ForkName,
|
||||
) -> Result<Self, D::Error> {
|
||||
if fork_name.deneb_enabled() {
|
||||
impl<'de, E: EthSpec> ContextDeserialize<'de, ForkName> for FullBlockContents<E> {
|
||||
fn context_deserialize<D>(deserializer: D, context: ForkName) -> Result<Self, D::Error>
|
||||
where
|
||||
D: Deserializer<'de>,
|
||||
{
|
||||
if context.deneb_enabled() {
|
||||
Ok(FullBlockContents::BlockContents(
|
||||
BlockContents::deserialize_by_fork::<'de, D>(value, fork_name)?,
|
||||
BlockContents::context_deserialize::<D>(deserializer, context)?,
|
||||
))
|
||||
} else {
|
||||
Ok(FullBlockContents::Block(
|
||||
BeaconBlock::deserialize_by_fork::<'de, D>(value, fork_name)?,
|
||||
BeaconBlock::context_deserialize::<D>(deserializer, context)?,
|
||||
))
|
||||
}
|
||||
}
|
||||
@@ -1802,7 +1808,7 @@ impl TryFrom<&HeaderMap> for ProduceBlockV3Metadata {
|
||||
}
|
||||
|
||||
/// A wrapper over a [`SignedBeaconBlock`] or a [`SignedBlockContents`].
|
||||
#[derive(Clone, Debug, Encode, Serialize, Deserialize)]
|
||||
#[derive(Clone, Debug, PartialEq, Encode, Serialize)]
|
||||
#[serde(untagged)]
|
||||
#[serde(bound = "E: EthSpec")]
|
||||
#[ssz(enum_behaviour = "transparent")]
|
||||
@@ -1811,6 +1817,26 @@ pub enum PublishBlockRequest<E: EthSpec> {
|
||||
Block(Arc<SignedBeaconBlock<E>>),
|
||||
}
|
||||
|
||||
impl<'de, E: EthSpec> ContextDeserialize<'de, ForkName> for PublishBlockRequest<E> {
|
||||
fn context_deserialize<D>(deserializer: D, context: ForkName) -> Result<Self, D::Error>
|
||||
where
|
||||
D: Deserializer<'de>,
|
||||
{
|
||||
let value =
|
||||
serde_json::Value::deserialize(deserializer).map_err(serde::de::Error::custom)?;
|
||||
|
||||
SignedBlockContents::<E>::context_deserialize(&value, context)
|
||||
.map(PublishBlockRequest::BlockContents)
|
||||
.or_else(|_| {
|
||||
Arc::<SignedBeaconBlock<E>>::context_deserialize(&value, context)
|
||||
.map(PublishBlockRequest::Block)
|
||||
})
|
||||
.map_err(|_| {
|
||||
serde::de::Error::custom("could not match any variant of PublishBlockRequest")
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl<E: EthSpec> PublishBlockRequest<E> {
|
||||
pub fn new(
|
||||
block: Arc<SignedBeaconBlock<E>>,
|
||||
@@ -1887,7 +1913,7 @@ impl<E: EthSpec> From<SignedBlockContentsTuple<E>> for PublishBlockRequest<E> {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, Encode)]
|
||||
#[derive(Debug, Clone, PartialEq, Serialize, Encode)]
|
||||
#[serde(bound = "E: EthSpec")]
|
||||
pub struct SignedBlockContents<E: EthSpec> {
|
||||
pub signed_block: Arc<SignedBeaconBlock<E>>,
|
||||
@@ -1896,7 +1922,33 @@ pub struct SignedBlockContents<E: EthSpec> {
|
||||
pub blobs: BlobsList<E>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, Encode)]
|
||||
impl<'de, E: EthSpec> ContextDeserialize<'de, ForkName> for SignedBlockContents<E> {
|
||||
fn context_deserialize<D>(deserializer: D, context: ForkName) -> Result<Self, D::Error>
|
||||
where
|
||||
D: Deserializer<'de>,
|
||||
{
|
||||
#[derive(Deserialize)]
|
||||
#[serde(bound = "E: EthSpec")]
|
||||
struct Helper<E: EthSpec> {
|
||||
signed_block: serde_json::Value,
|
||||
kzg_proofs: KzgProofs<E>,
|
||||
#[serde(with = "ssz_types::serde_utils::list_of_hex_fixed_vec")]
|
||||
blobs: BlobsList<E>,
|
||||
}
|
||||
let helper = Helper::<E>::deserialize(deserializer).map_err(serde::de::Error::custom)?;
|
||||
|
||||
let block = SignedBeaconBlock::context_deserialize(helper.signed_block, context)
|
||||
.map_err(serde::de::Error::custom)?;
|
||||
|
||||
Ok(Self {
|
||||
signed_block: Arc::new(block),
|
||||
kzg_proofs: helper.kzg_proofs,
|
||||
blobs: helper.blobs,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Encode)]
|
||||
#[serde(bound = "E: EthSpec")]
|
||||
pub struct BlockContents<E: EthSpec> {
|
||||
pub block: BeaconBlock<E>,
|
||||
@@ -1905,11 +1957,11 @@ pub struct BlockContents<E: EthSpec> {
|
||||
pub blobs: BlobsList<E>,
|
||||
}
|
||||
|
||||
impl<E: EthSpec> ForkVersionDeserialize for BlockContents<E> {
|
||||
fn deserialize_by_fork<'de, D: serde::Deserializer<'de>>(
|
||||
value: serde_json::value::Value,
|
||||
fork_name: ForkName,
|
||||
) -> Result<Self, D::Error> {
|
||||
impl<'de, E: EthSpec> ContextDeserialize<'de, ForkName> for BlockContents<E> {
|
||||
fn context_deserialize<D>(deserializer: D, context: ForkName) -> Result<Self, D::Error>
|
||||
where
|
||||
D: Deserializer<'de>,
|
||||
{
|
||||
#[derive(Deserialize)]
|
||||
#[serde(bound = "E: EthSpec")]
|
||||
struct Helper<E: EthSpec> {
|
||||
@@ -1918,10 +1970,13 @@ impl<E: EthSpec> ForkVersionDeserialize for BlockContents<E> {
|
||||
#[serde(with = "ssz_types::serde_utils::list_of_hex_fixed_vec")]
|
||||
blobs: BlobsList<E>,
|
||||
}
|
||||
let helper: Helper<E> = serde_json::from_value(value).map_err(serde::de::Error::custom)?;
|
||||
let helper = Helper::<E>::deserialize(deserializer).map_err(serde::de::Error::custom)?;
|
||||
|
||||
let block = BeaconBlock::context_deserialize(helper.block, context)
|
||||
.map_err(serde::de::Error::custom)?;
|
||||
|
||||
Ok(Self {
|
||||
block: BeaconBlock::deserialize_by_fork::<'de, D>(helper.block, fork_name)?,
|
||||
block,
|
||||
kzg_proofs: helper.kzg_proofs,
|
||||
blobs: helper.blobs,
|
||||
})
|
||||
@@ -1993,22 +2048,22 @@ impl<E: EthSpec> FullPayloadContents<E> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<E: EthSpec> ForkVersionDeserialize for FullPayloadContents<E> {
|
||||
fn deserialize_by_fork<'de, D: Deserializer<'de>>(
|
||||
value: Value,
|
||||
fork_name: ForkName,
|
||||
) -> Result<Self, D::Error> {
|
||||
if fork_name.deneb_enabled() {
|
||||
ExecutionPayloadAndBlobs::deserialize_by_fork::<'de, D>(value, fork_name)
|
||||
impl<'de, E: EthSpec> ContextDeserialize<'de, ForkName> for FullPayloadContents<E> {
|
||||
fn context_deserialize<D>(deserializer: D, context: ForkName) -> Result<Self, D::Error>
|
||||
where
|
||||
D: Deserializer<'de>,
|
||||
{
|
||||
if context.deneb_enabled() {
|
||||
ExecutionPayloadAndBlobs::context_deserialize::<D>(deserializer, context)
|
||||
.map(Self::PayloadAndBlobs)
|
||||
.map_err(serde::de::Error::custom)
|
||||
} else if fork_name.bellatrix_enabled() {
|
||||
ExecutionPayload::deserialize_by_fork::<'de, D>(value, fork_name)
|
||||
} else if context.bellatrix_enabled() {
|
||||
ExecutionPayload::context_deserialize::<D>(deserializer, context)
|
||||
.map(Self::Payload)
|
||||
.map_err(serde::de::Error::custom)
|
||||
} else {
|
||||
Err(serde::de::Error::custom(format!(
|
||||
"FullPayloadContents deserialization for {fork_name} not implemented"
|
||||
"FullPayloadContents deserialization for {context} not implemented"
|
||||
)))
|
||||
}
|
||||
}
|
||||
@@ -2021,23 +2076,25 @@ pub struct ExecutionPayloadAndBlobs<E: EthSpec> {
|
||||
pub blobs_bundle: BlobsBundle<E>,
|
||||
}
|
||||
|
||||
impl<E: EthSpec> ForkVersionDeserialize for ExecutionPayloadAndBlobs<E> {
|
||||
fn deserialize_by_fork<'de, D: Deserializer<'de>>(
|
||||
value: Value,
|
||||
fork_name: ForkName,
|
||||
) -> Result<Self, D::Error> {
|
||||
impl<'de, E: EthSpec> ContextDeserialize<'de, ForkName> for ExecutionPayloadAndBlobs<E> {
|
||||
fn context_deserialize<D>(deserializer: D, context: ForkName) -> Result<Self, D::Error>
|
||||
where
|
||||
D: Deserializer<'de>,
|
||||
{
|
||||
#[derive(Deserialize)]
|
||||
#[serde(bound = "E: EthSpec")]
|
||||
struct Helper<E: EthSpec> {
|
||||
execution_payload: serde_json::Value,
|
||||
blobs_bundle: BlobsBundle<E>,
|
||||
}
|
||||
let helper: Helper<E> = serde_json::from_value(value).map_err(serde::de::Error::custom)?;
|
||||
let helper = Helper::<E>::deserialize(deserializer).map_err(serde::de::Error::custom)?;
|
||||
|
||||
Ok(Self {
|
||||
execution_payload: ExecutionPayload::deserialize_by_fork::<'de, D>(
|
||||
execution_payload: ExecutionPayload::context_deserialize(
|
||||
helper.execution_payload,
|
||||
fork_name,
|
||||
)?,
|
||||
context,
|
||||
)
|
||||
.map_err(serde::de::Error::custom)?,
|
||||
blobs_bundle: helper.blobs_bundle,
|
||||
})
|
||||
}
|
||||
@@ -2186,6 +2243,73 @@ mod test {
|
||||
assert_eq!(serde_json::to_string(&y).unwrap(), pubkey_str);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_publish_block_request_context_deserialize() {
|
||||
let round_trip_test = |request: PublishBlockRequest<MainnetEthSpec>| {
|
||||
let fork_name = request.signed_block().fork_name_unchecked();
|
||||
let json_str = serde_json::to_string(&request).unwrap();
|
||||
let mut de = serde_json::Deserializer::from_str(&json_str);
|
||||
let deserialized_request =
|
||||
PublishBlockRequest::<MainnetEthSpec>::context_deserialize(&mut de, fork_name)
|
||||
.unwrap();
|
||||
assert_eq!(request, deserialized_request);
|
||||
};
|
||||
|
||||
let rng = &mut XorShiftRng::from_seed([42; 16]);
|
||||
for fork_name in ForkName::list_all() {
|
||||
let signed_beacon_block =
|
||||
map_fork_name!(fork_name, SignedBeaconBlock, <_>::random_for_test(rng));
|
||||
let request = if fork_name.deneb_enabled() {
|
||||
let kzg_proofs = KzgProofs::<MainnetEthSpec>::random_for_test(rng);
|
||||
let blobs = BlobsList::<MainnetEthSpec>::random_for_test(rng);
|
||||
let block_contents = SignedBlockContents {
|
||||
signed_block: Arc::new(signed_beacon_block),
|
||||
kzg_proofs,
|
||||
blobs,
|
||||
};
|
||||
PublishBlockRequest::BlockContents(block_contents)
|
||||
} else {
|
||||
PublishBlockRequest::Block(Arc::new(signed_beacon_block))
|
||||
};
|
||||
round_trip_test(request);
|
||||
println!("fork_name: {:?} PASSED", fork_name);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_signed_block_contents_context_deserialize() {
|
||||
let round_trip_test = |contents: SignedBlockContents<MainnetEthSpec>| {
|
||||
let fork_name = contents.signed_block.fork_name_unchecked();
|
||||
let json_str = serde_json::to_string(&contents).unwrap();
|
||||
let mut de = serde_json::Deserializer::from_str(&json_str);
|
||||
let deserialized_contents =
|
||||
SignedBlockContents::<MainnetEthSpec>::context_deserialize(&mut de, fork_name)
|
||||
.unwrap();
|
||||
assert_eq!(contents, deserialized_contents);
|
||||
};
|
||||
|
||||
let mut fork_name = ForkName::Deneb;
|
||||
let rng = &mut XorShiftRng::from_seed([42; 16]);
|
||||
loop {
|
||||
let signed_beacon_block =
|
||||
map_fork_name!(fork_name, SignedBeaconBlock, <_>::random_for_test(rng));
|
||||
let kzg_proofs = KzgProofs::<MainnetEthSpec>::random_for_test(rng);
|
||||
let blobs = BlobsList::<MainnetEthSpec>::random_for_test(rng);
|
||||
let block_contents = SignedBlockContents {
|
||||
signed_block: Arc::new(signed_beacon_block),
|
||||
kzg_proofs,
|
||||
blobs,
|
||||
};
|
||||
round_trip_test(block_contents);
|
||||
println!("fork_name: {:?} PASSED", fork_name);
|
||||
if let Some(next_fork_name) = fork_name.next_fork() {
|
||||
fork_name = next_fork_name;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_execution_payload_execution_payload_deserialize_by_fork() {
|
||||
let rng = &mut XorShiftRng::from_seed([42; 16]);
|
||||
@@ -2278,14 +2402,13 @@ mod test {
|
||||
fn generic_deserialize_by_fork<
|
||||
'de,
|
||||
D: Deserializer<'de>,
|
||||
O: ForkVersionDeserialize + PartialEq + Debug,
|
||||
O: ContextDeserialize<'de, ForkName> + PartialEq + Debug,
|
||||
>(
|
||||
deserializer: D,
|
||||
original: O,
|
||||
fork_name: ForkName,
|
||||
) {
|
||||
let val = Value::deserialize(deserializer).unwrap();
|
||||
let roundtrip = O::deserialize_by_fork::<'de, D>(val, fork_name).unwrap();
|
||||
let roundtrip = O::context_deserialize::<D>(deserializer, fork_name).unwrap();
|
||||
assert_eq!(original, roundtrip);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user