|
| 1 | +use crate::api_types::GenericResponse; |
| 2 | +use crate::unsupported_version_rejection; |
| 3 | +use crate::version::{add_consensus_version_header, V1, V2}; |
| 4 | +use beacon_chain::{BeaconChain, BeaconChainTypes}; |
| 5 | +use eth2::types::{self, EndpointVersion, Hash256, Slot}; |
| 6 | +use std::sync::Arc; |
| 7 | +use types::fork_versioned_response::EmptyMetadata; |
| 8 | +use types::{CommitteeIndex, ForkVersionedResponse}; |
| 9 | +use warp::{ |
| 10 | + hyper::{Body, Response}, |
| 11 | + reply::Reply, |
| 12 | +}; |
| 13 | + |
| 14 | +pub fn get_aggregate_attestation<T: BeaconChainTypes>( |
| 15 | + slot: Slot, |
| 16 | + attestation_data_root: &Hash256, |
| 17 | + committee_index: Option<CommitteeIndex>, |
| 18 | + endpoint_version: EndpointVersion, |
| 19 | + chain: Arc<BeaconChain<T>>, |
| 20 | +) -> Result<Response<Body>, warp::reject::Rejection> { |
| 21 | + if endpoint_version == V2 { |
| 22 | + let Some(committee_index) = committee_index else { |
| 23 | + return Err(warp_utils::reject::custom_bad_request( |
| 24 | + "missing committee index".to_string(), |
| 25 | + )); |
| 26 | + }; |
| 27 | + let aggregate_attestation = chain |
| 28 | + .get_aggregated_attestation_electra(slot, attestation_data_root, committee_index) |
| 29 | + .map_err(|e| { |
| 30 | + warp_utils::reject::custom_bad_request(format!( |
| 31 | + "unable to fetch aggregate: {:?}", |
| 32 | + e |
| 33 | + )) |
| 34 | + })? |
| 35 | + .ok_or_else(|| { |
| 36 | + warp_utils::reject::custom_not_found("no matching aggregate found".to_string()) |
| 37 | + })?; |
| 38 | + let fork_name = chain.spec.fork_name_at_slot::<T::EthSpec>(slot); |
| 39 | + let fork_versioned_response = ForkVersionedResponse { |
| 40 | + version: Some(fork_name), |
| 41 | + metadata: EmptyMetadata {}, |
| 42 | + data: aggregate_attestation, |
| 43 | + }; |
| 44 | + Ok(add_consensus_version_header( |
| 45 | + warp::reply::json(&fork_versioned_response).into_response(), |
| 46 | + fork_name, |
| 47 | + )) |
| 48 | + } else if endpoint_version == V1 { |
| 49 | + let aggregate_attestation = chain |
| 50 | + .get_pre_electra_aggregated_attestation_by_slot_and_root(slot, attestation_data_root) |
| 51 | + .map_err(|e| { |
| 52 | + warp_utils::reject::custom_bad_request(format!( |
| 53 | + "unable to fetch aggregate: {:?}", |
| 54 | + e |
| 55 | + )) |
| 56 | + })? |
| 57 | + .map(GenericResponse::from) |
| 58 | + .ok_or_else(|| { |
| 59 | + warp_utils::reject::custom_not_found("no matching aggregate found".to_string()) |
| 60 | + })?; |
| 61 | + Ok(warp::reply::json(&aggregate_attestation).into_response()) |
| 62 | + } else { |
| 63 | + return Err(unsupported_version_rejection(endpoint_version)); |
| 64 | + } |
| 65 | +} |
0 commit comments