Skip to content

Commit 1093142

Browse files
committed
Fix aggregate attestation v2 response (#6926)
Squashed commit of the following: commit c11c343 Author: Eitan Seri-Levi <[email protected]> Date: Thu Feb 6 17:32:34 2025 +0200 fix test commit 4203b56 Author: Eitan Seri-Levi <[email protected]> Date: Thu Feb 6 16:38:13 2025 +0200 add consensus version header commit 37c9b67 Author: Eitan Seri-Levi <[email protected]> Date: Thu Feb 6 14:39:11 2025 +0200 Return fork versioned response and a small refactor
1 parent b1a4169 commit 1093142

File tree

2 files changed

+74
-33
lines changed

2 files changed

+74
-33
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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+
}

beacon_node/http_api/src/lib.rs

Lines changed: 9 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
//! There are also some additional, non-standard endpoints behind the `/lighthouse/` path which are
66
//! used for development.
77
8+
mod aggregate_attestation;
89
mod attestation_performance;
910
mod attester_duties;
1011
mod block_id;
@@ -3384,40 +3385,15 @@ pub fn serve<T: BeaconChainTypes>(
33843385
not_synced_filter: Result<(), Rejection>,
33853386
task_spawner: TaskSpawner<T::EthSpec>,
33863387
chain: Arc<BeaconChain<T>>| {
3387-
task_spawner.blocking_json_task(Priority::P0, move || {
3388+
task_spawner.blocking_response_task(Priority::P0, move || {
33883389
not_synced_filter?;
3389-
let res = if endpoint_version == V2 {
3390-
let Some(committee_index) = query.committee_index else {
3391-
return Err(warp_utils::reject::custom_bad_request(
3392-
"missing committee index".to_string(),
3393-
));
3394-
};
3395-
chain.get_aggregated_attestation_electra(
3396-
query.slot,
3397-
&query.attestation_data_root,
3398-
committee_index,
3399-
)
3400-
} else if endpoint_version == V1 {
3401-
// Do nothing
3402-
chain.get_pre_electra_aggregated_attestation_by_slot_and_root(
3403-
query.slot,
3404-
&query.attestation_data_root,
3405-
)
3406-
} else {
3407-
return Err(unsupported_version_rejection(endpoint_version));
3408-
};
3409-
res.map_err(|e| {
3410-
warp_utils::reject::custom_bad_request(format!(
3411-
"unable to fetch aggregate: {:?}",
3412-
e
3413-
))
3414-
})?
3415-
.map(api_types::GenericResponse::from)
3416-
.ok_or_else(|| {
3417-
warp_utils::reject::custom_not_found(
3418-
"no matching aggregate found".to_string(),
3419-
)
3420-
})
3390+
crate::aggregate_attestation::get_aggregate_attestation(
3391+
query.slot,
3392+
&query.attestation_data_root,
3393+
query.committee_index,
3394+
endpoint_version,
3395+
chain,
3396+
)
34213397
})
34223398
},
34233399
);

0 commit comments

Comments
 (0)