Skip to content

Commit

Permalink
Ensure aggregate attestation endpoint v2 works pre electra and add so…
Browse files Browse the repository at this point in the history
…me test coverage
  • Loading branch information
eserilev committed Feb 11, 2025
1 parent 431dd7c commit b48c7df
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 49 deletions.
49 changes: 34 additions & 15 deletions beacon_node/http_api/src/aggregate_attestation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,41 @@ pub fn get_aggregate_attestation<T: BeaconChainTypes>(
chain: Arc<BeaconChain<T>>,
) -> Result<Response<Body>, warp::reject::Rejection> {
if endpoint_version == V2 {
let Some(committee_index) = committee_index else {
return Err(warp_utils::reject::custom_bad_request(
"missing committee index".to_string(),
));
let fork_name = chain.spec.fork_name_at_slot::<T::EthSpec>(slot);
let aggregate_attestation = if fork_name.electra_enabled() {
let Some(committee_index) = committee_index else {
return Err(warp_utils::reject::custom_bad_request(
"missing committee index".to_string(),
));
};
chain
.get_aggregated_attestation_electra(slot, attestation_data_root, committee_index)
.map_err(|e| {
warp_utils::reject::custom_bad_request(format!(
"unable to fetch aggregate: {:?}",
e
))
})?
.ok_or_else(|| {
warp_utils::reject::custom_not_found("no matching aggregate found".to_string())
})?
} else {
chain
.get_pre_electra_aggregated_attestation_by_slot_and_root(
slot,
attestation_data_root,
)
.map_err(|e| {
warp_utils::reject::custom_bad_request(format!(
"unable to fetch aggregate: {:?}",
e
))
})?
.ok_or_else(|| {
warp_utils::reject::custom_not_found("no matching aggregate found".to_string())
})?
};
let aggregate_attestation = chain
.get_aggregated_attestation_electra(slot, attestation_data_root, committee_index)
.map_err(|e| {
warp_utils::reject::custom_bad_request(format!(
"unable to fetch aggregate: {:?}",
e
))
})?
.ok_or_else(|| {
warp_utils::reject::custom_not_found("no matching aggregate found".to_string())
})?;

let fork_name = chain.spec.fork_name_at_slot::<T::EthSpec>(slot);
let fork_versioned_response = ForkVersionedResponse {
version: Some(fork_name),
Expand Down
65 changes: 31 additions & 34 deletions beacon_node/http_api/tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3555,54 +3555,51 @@ impl ApiTester {
}

#[allow(clippy::await_holding_lock)] // This is a test, so it should be fine.
pub async fn test_get_validator_aggregate_attestation(self) -> Self {
if self
pub async fn test_get_validator_aggregate_attestation_v2(self) -> Self {
let attestation = self
.chain
.spec
.fork_name_at_slot::<E>(self.chain.slot().unwrap())
.electra_enabled()
{
for attestation in self.chain.naive_aggregation_pool.read().iter() {
let result = self
.client
.get_validator_aggregate_attestation_v2(
attestation.data().slot,
attestation.data().tree_hash_root(),
attestation.committee_index().expect("committee index"),
)
.await
.unwrap()
.unwrap()
.data;
let expected = attestation;
.head_beacon_block()
.message()
.body()
.attestations()
.next()
.unwrap()
.clone_as_attestation();
let result = self
.client
.get_validator_aggregate_attestation_v1(
attestation.data().slot,
attestation.data().tree_hash_root(),
)
.await
.unwrap()
.unwrap()
.data;
let expected = attestation;

assert_eq!(&result, expected);
}
} else {
let attestation = self
.chain
.head_beacon_block()
.message()
.body()
.attestations()
.next()
.unwrap()
.clone_as_attestation();
assert_eq!(result, expected);

self
}

#[allow(clippy::await_holding_lock)] // This is a test, so it should be fine.
pub async fn test_get_validator_aggregate_attestation_v2(self) -> Self {
for attestation in self.chain.naive_aggregation_pool.read().iter() {
let result = self
.client
.get_validator_aggregate_attestation_v1(
.get_validator_aggregate_attestation_v2(
attestation.data().slot,
attestation.data().tree_hash_root(),
attestation.committee_index().expect("committee index"),
)
.await
.unwrap()
.unwrap()
.data;
let expected = attestation;

assert_eq!(result, expected);
assert_eq!(&result, expected);
}

self
}

Expand Down

0 comments on commit b48c7df

Please sign in to comment.