From 4065ef66abad8ef76032844905f7e4a9c4310b39 Mon Sep 17 00:00:00 2001 From: realbigsean Date: Mon, 15 Jul 2024 10:57:13 -0700 Subject: [PATCH] filter instead of convert --- beacon_node/http_api/src/lib.rs | 100 ++++++++++------------- consensus/types/src/attestation.rs | 29 ------- consensus/types/src/attester_slashing.rs | 15 +--- 3 files changed, 45 insertions(+), 99 deletions(-) diff --git a/beacon_node/http_api/src/lib.rs b/beacon_node/http_api/src/lib.rs index 3adf84fee13..2d017d65391 100644 --- a/beacon_node/http_api/src/lib.rs +++ b/beacon_node/http_api/src/lib.rs @@ -1845,23 +1845,14 @@ pub fn serve( "unable to read slot clock".to_string(), ))?; let fork_name = chain.spec.fork_name_at_slot::(current_slot); - - let attestations = if fork_name.electra_enabled() { - attestations - .into_iter() - .map(|att| match att { - Attestation::Base(a) => Ok(Attestation::Electra(a.try_into()?)), - Attestation::Electra(a) => Ok(Attestation::Electra(a)), - }) - .collect::, types::attestation::Error>>() - .map_err(|e| { - warp_utils::reject::custom_server_error(format!( - "could not convert base attestations to electra {e:?}" - )) - })? - } else { - attestations - }; + let attestations = attestations + .into_iter() + .filter(|att| { + (fork_name.electra_enabled() && matches!(att, Attestation::Electra(_))) + || (!fork_name.electra_enabled() + && matches!(att, Attestation::Base(_))) + }) + .collect::>(); let res = fork_versioned_response(endpoint_version, fork_name, &attestations)?; Ok(add_consensus_version_header( @@ -1921,48 +1912,45 @@ pub fn serve( ); // GET beacon/pool/attester_slashings - let get_beacon_pool_attester_slashings = beacon_pool_path_any - .clone() - .and(warp::path("attester_slashings")) - .and(warp::path::end()) - .then( - |endpoint_version: EndpointVersion, - task_spawner: TaskSpawner, - chain: Arc>| { - 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 - // current fork's format. This is to ensure consistent message types matching - // `Eth-Consensus-Version`. - let current_slot = - chain - .slot_clock - .now() - .ok_or(warp_utils::reject::custom_server_error( + let get_beacon_pool_attester_slashings = + beacon_pool_path_any + .clone() + .and(warp::path("attester_slashings")) + .and(warp::path::end()) + .then( + |endpoint_version: EndpointVersion, + task_spawner: TaskSpawner, + chain: Arc>| { + 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 + // current fork's format. This is to ensure consistent message types matching + // `Eth-Consensus-Version`. + let current_slot = chain.slot_clock.now().ok_or( + warp_utils::reject::custom_server_error( "unable to read slot clock".to_string(), - ))?; - let fork_name = chain.spec.fork_name_at_slot::(current_slot); - - let slashings = if fork_name.electra_enabled() { - slashings + ), + )?; + let fork_name = chain.spec.fork_name_at_slot::(current_slot); + let slashings = slashings .into_iter() - .map(|att| match att { - AttesterSlashing::Base(a) => AttesterSlashing::Electra(a.into()), - AttesterSlashing::Electra(a) => AttesterSlashing::Electra(a), + .filter(|slashing| { + (fork_name.electra_enabled() + && matches!(slashing, AttesterSlashing::Electra(_))) + || (!fork_name.electra_enabled() + && matches!(slashing, AttesterSlashing::Base(_))) }) - .collect::>() - } else { - slashings - }; - let res = fork_versioned_response(endpoint_version, fork_name, &slashings)?; - Ok(add_consensus_version_header( - warp::reply::json(&res).into_response(), - fork_name, - )) - }) - }, - ); + .collect::>(); + + let res = fork_versioned_response(endpoint_version, fork_name, &slashings)?; + Ok(add_consensus_version_header( + warp::reply::json(&res).into_response(), + fork_name, + )) + }) + }, + ); // POST beacon/pool/proposer_slashings let post_beacon_pool_proposer_slashings = beacon_pool_path diff --git a/consensus/types/src/attestation.rs b/consensus/types/src/attestation.rs index f12f84a447d..7b53a98caa1 100644 --- a/consensus/types/src/attestation.rs +++ b/consensus/types/src/attestation.rs @@ -422,35 +422,6 @@ impl AttestationBase { } } -impl TryFrom> for AttestationElectra { - type Error = Error; - fn try_from(att: AttestationBase) -> Result { - // 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 = 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 SlotData for Attestation { fn get_slot(&self) -> Slot { self.data().slot diff --git a/consensus/types/src/attester_slashing.rs b/consensus/types/src/attester_slashing.rs index 18ab63ad44e..f6aa654d445 100644 --- a/consensus/types/src/attester_slashing.rs +++ b/consensus/types/src/attester_slashing.rs @@ -1,5 +1,5 @@ use crate::indexed_attestation::{ - IndexedAttestation, IndexedAttestationBase, IndexedAttestationElectra, IndexedAttestationRef, + IndexedAttestationBase, IndexedAttestationElectra, IndexedAttestationRef, }; use crate::{test_utils::TestRandom, EthSpec}; use derivative::Derivative; @@ -161,19 +161,6 @@ impl AttesterSlashing { } } -impl From> for AttesterSlashingElectra { - fn from(attester_slashing: AttesterSlashingBase) -> 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 TestRandom for AttesterSlashing { fn random_for_test(rng: &mut impl RngCore) -> Self { if rng.gen_bool(0.5) {