filter instead of convert

This commit is contained in:
realbigsean
2024-07-15 10:57:13 -07:00
parent 71a2eadc46
commit 4065ef66ab
3 changed files with 44 additions and 98 deletions

View File

@@ -1845,23 +1845,14 @@ pub fn serve<T: BeaconChainTypes>(
"unable to read slot clock".to_string(), "unable to read slot clock".to_string(),
))?; ))?;
let fork_name = chain.spec.fork_name_at_slot::<T::EthSpec>(current_slot); let fork_name = chain.spec.fork_name_at_slot::<T::EthSpec>(current_slot);
let attestations = attestations
let attestations = if fork_name.electra_enabled() { .into_iter()
attestations .filter(|att| {
.into_iter() (fork_name.electra_enabled() && matches!(att, Attestation::Electra(_)))
.map(|att| match att { || (!fork_name.electra_enabled()
Attestation::Base(a) => Ok(Attestation::Electra(a.try_into()?)), && matches!(att, Attestation::Base(_)))
Attestation::Electra(a) => Ok(Attestation::Electra(a)), })
}) .collect::<Vec<_>>();
.collect::<Result<Vec<_>, types::attestation::Error>>()
.map_err(|e| {
warp_utils::reject::custom_server_error(format!(
"could not convert base attestations to electra {e:?}"
))
})?
} else {
attestations
};
let res = fork_versioned_response(endpoint_version, fork_name, &attestations)?; let res = fork_versioned_response(endpoint_version, fork_name, &attestations)?;
Ok(add_consensus_version_header( Ok(add_consensus_version_header(
@@ -1921,48 +1912,45 @@ pub fn serve<T: BeaconChainTypes>(
); );
// GET beacon/pool/attester_slashings // GET beacon/pool/attester_slashings
let get_beacon_pool_attester_slashings = beacon_pool_path_any let get_beacon_pool_attester_slashings =
.clone() beacon_pool_path_any
.and(warp::path("attester_slashings")) .clone()
.and(warp::path::end()) .and(warp::path("attester_slashings"))
.then( .and(warp::path::end())
|endpoint_version: EndpointVersion, .then(
task_spawner: TaskSpawner<T::EthSpec>, |endpoint_version: EndpointVersion,
chain: Arc<BeaconChain<T>>| { task_spawner: TaskSpawner<T::EthSpec>,
task_spawner.blocking_response_task(Priority::P1, move || { chain: Arc<BeaconChain<T>>| {
let slashings = chain.op_pool.get_all_attester_slashings(); task_spawner.blocking_response_task(Priority::P1, move || {
let slashings = chain.op_pool.get_all_attester_slashings();
// Use the current slot to find the fork version, and convert all messages to the // Use the current slot to find the fork version, and convert all messages to the
// current fork's format. This is to ensure consistent message types matching // current fork's format. This is to ensure consistent message types matching
// `Eth-Consensus-Version`. // `Eth-Consensus-Version`.
let current_slot = let current_slot = chain.slot_clock.now().ok_or(
chain warp_utils::reject::custom_server_error(
.slot_clock
.now()
.ok_or(warp_utils::reject::custom_server_error(
"unable to read slot clock".to_string(), "unable to read slot clock".to_string(),
))?; ),
let fork_name = chain.spec.fork_name_at_slot::<T::EthSpec>(current_slot); )?;
let fork_name = chain.spec.fork_name_at_slot::<T::EthSpec>(current_slot);
let slashings = if fork_name.electra_enabled() { let slashings = slashings
slashings
.into_iter() .into_iter()
.map(|att| match att { .filter(|slashing| {
AttesterSlashing::Base(a) => AttesterSlashing::Electra(a.into()), (fork_name.electra_enabled()
AttesterSlashing::Electra(a) => AttesterSlashing::Electra(a), && matches!(slashing, AttesterSlashing::Electra(_)))
|| (!fork_name.electra_enabled()
&& matches!(slashing, AttesterSlashing::Base(_)))
}) })
.collect::<Vec<_>>() .collect::<Vec<_>>();
} else {
slashings let res = fork_versioned_response(endpoint_version, fork_name, &slashings)?;
}; Ok(add_consensus_version_header(
let res = fork_versioned_response(endpoint_version, fork_name, &slashings)?; warp::reply::json(&res).into_response(),
Ok(add_consensus_version_header( fork_name,
warp::reply::json(&res).into_response(), ))
fork_name, })
)) },
}) );
},
);
// POST beacon/pool/proposer_slashings // POST beacon/pool/proposer_slashings
let post_beacon_pool_proposer_slashings = beacon_pool_path let post_beacon_pool_proposer_slashings = beacon_pool_path

View File

@@ -422,35 +422,6 @@ impl<E: EthSpec> AttestationBase<E> {
} }
} }
impl<E: EthSpec> TryFrom<AttestationBase<E>> for AttestationElectra<E> {
type Error = Error;
fn try_from(att: AttestationBase<E>) -> Result<Self, Self::Error> {
// Extend the aggregation bits list.
let aggregation_bits = att.extend_aggregation_bits()?;
let AttestationBase {
aggregation_bits: _,
mut data,
signature,
} = att;
// Set the committee index based on the index field.
let mut committee_bits: BitVector<E::MaxCommitteesPerSlot> = BitVector::default();
committee_bits
.set(data.index as usize, true)
.map_err(|_| Error::InvalidCommitteeIndex)?;
// Set the attestation data's index to zero.
data.index = 0;
Ok(Self {
aggregation_bits,
data,
committee_bits,
signature,
})
}
}
impl<E: EthSpec> SlotData for Attestation<E> { impl<E: EthSpec> SlotData for Attestation<E> {
fn get_slot(&self) -> Slot { fn get_slot(&self) -> Slot {
self.data().slot self.data().slot

View File

@@ -1,5 +1,5 @@
use crate::indexed_attestation::{ use crate::indexed_attestation::{
IndexedAttestation, IndexedAttestationBase, IndexedAttestationElectra, IndexedAttestationRef, IndexedAttestationBase, IndexedAttestationElectra, IndexedAttestationRef,
}; };
use crate::{test_utils::TestRandom, EthSpec}; use crate::{test_utils::TestRandom, EthSpec};
use derivative::Derivative; use derivative::Derivative;
@@ -161,19 +161,6 @@ impl<E: EthSpec> AttesterSlashing<E> {
} }
} }
impl<E: EthSpec> From<AttesterSlashingBase<E>> for AttesterSlashingElectra<E> {
fn from(attester_slashing: AttesterSlashingBase<E>) -> Self {
let AttesterSlashingBase {
attestation_1,
attestation_2,
} = attester_slashing;
AttesterSlashingElectra {
attestation_1: IndexedAttestation::Base(attestation_1).to_electra(),
attestation_2: IndexedAttestation::Base(attestation_2).to_electra(),
}
}
}
impl<E: EthSpec> TestRandom for AttesterSlashing<E> { impl<E: EthSpec> TestRandom for AttesterSlashing<E> {
fn random_for_test(rng: &mut impl RngCore) -> Self { fn random_for_test(rng: &mut impl RngCore) -> Self {
if rng.gen_bool(0.5) { if rng.gen_bool(0.5) {