Added more test coverage, simplified Attestation conversion, and other minor refactors

This commit is contained in:
Eitan Seri-Levi
2025-01-06 16:30:58 +07:00
parent 4700ef9798
commit c7ef72d01e
12 changed files with 181 additions and 188 deletions

View File

@@ -858,53 +858,6 @@ pub async fn fork_choice_before_proposal() {
assert_eq!(block_d.parent_root(), Hash256::from(block_root_b));
}
// Test that attestations to unknown blocks are requeued and processed when their block arrives.
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
async fn valid_single_attestation() {
let validator_count = 128;
let all_validators = (0..validator_count).collect::<Vec<_>>();
let tester = InteractiveTester::<E>::new(None, validator_count).await;
let harness = &tester.harness;
let client = tester.client.clone();
let num_initial = 5;
// Slot of the block attested to.
let attestation_slot = Slot::new(num_initial) + 1;
// Make some initial blocks.
harness.advance_slot();
harness
.extend_chain(
num_initial as usize,
BlockStrategy::OnCanonicalHead,
AttestationStrategy::AllValidators,
)
.await;
harness.advance_slot();
assert_eq!(harness.get_current_slot(), attestation_slot);
// Make the attested-to block without applying it.
let pre_state = harness.get_current_state();
let (block, post_state) = harness.make_block(pre_state, attestation_slot).await;
let block_root = block.0.canonical_root();
// Make attestations to the block and POST them to the beacon node on a background thread.
let attestations = harness
.make_single_attestations(
&all_validators,
&post_state,
block.0.state_root(),
block_root.into(),
attestation_slot,
)
.into_iter()
.flat_map(|attestations| attestations.into_iter().map(|(att, _subnet)| att))
.collect::<Vec<_>>();
}
// Test that attestations to unknown blocks are requeued and processed when their block arrives.
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn queue_attestations_from_http() {
@@ -937,27 +890,48 @@ async fn queue_attestations_from_http() {
let pre_state = harness.get_current_state();
let (block, post_state) = harness.make_block(pre_state, attestation_slot).await;
let block_root = block.0.canonical_root();
let fork_name = tester.harness.spec.fork_name_at_slot::<E>(attestation_slot);
// Make attestations to the block and POST them to the beacon node on a background thread.
let attestations = harness
.make_unaggregated_attestations(
&all_validators,
&post_state,
block.0.state_root(),
block_root.into(),
attestation_slot,
)
.into_iter()
.flat_map(|attestations| attestations.into_iter().map(|(att, _subnet)| att))
.collect::<Vec<_>>();
let attestation_future = if fork_name.electra_enabled() {
let single_attestations = harness
.make_single_attestations(
&all_validators,
&post_state,
block.0.state_root(),
block_root.into(),
attestation_slot,
)
.into_iter()
.flat_map(|attestations| attestations.into_iter().map(|(att, _subnet)| att))
.collect::<Vec<_>>();
let fork_name = tester.harness.spec.fork_name_at_slot::<E>(attestation_slot);
let attestation_future = tokio::spawn(async move {
client
.post_beacon_pool_attestations_v1(&attestations)
.await
.expect("attestations should be processed successfully")
});
tokio::spawn(async move {
client
.post_beacon_pool_attestations_v2(&single_attestations, fork_name)
.await
.expect("attestations should be processed successfully")
})
} else {
let attestations = harness
.make_unaggregated_attestations(
&all_validators,
&post_state,
block.0.state_root(),
block_root.into(),
attestation_slot,
)
.into_iter()
.flat_map(|attestations| attestations.into_iter().map(|(att, _subnet)| att))
.collect::<Vec<_>>();
tokio::spawn(async move {
client
.post_beacon_pool_attestations_v1(&attestations)
.await
.expect("attestations should be processed successfully")
})
};
// In parallel, apply the block. We need to manually notify the reprocess queue, because the
// `beacon_chain` does not know about the queue and will not update it for us.

View File

@@ -39,8 +39,9 @@ use tokio::time::Duration;
use tree_hash::TreeHash;
use types::application_domain::ApplicationDomain;
use types::{
attestation::AttestationBase, AggregateSignature, BitList, Domain, EthSpec, ExecutionBlockHash,
Hash256, Keypair, MainnetEthSpec, RelativeEpoch, SelectionProof, SignedRoot, Slot,
attestation::AttestationBase, attestation::SingleAttestation, AggregateSignature, BitList,
Domain, EthSpec, ExecutionBlockHash, Hash256, Keypair, MainnetEthSpec, RelativeEpoch,
SelectionProof, SignedRoot, Slot,
};
type E = MainnetEthSpec;
@@ -71,6 +72,7 @@ struct ApiTester {
next_block: PublishBlockRequest<E>,
reorg_block: PublishBlockRequest<E>,
attestations: Vec<Attestation<E>>,
single_attestations: Vec<SingleAttestation>,
contribution_and_proofs: Vec<SignedContributionAndProof<E>>,
attester_slashing: AttesterSlashing<E>,
proposer_slashing: ProposerSlashing,
@@ -203,6 +205,27 @@ impl ApiTester {
"precondition: attestations for testing"
);
let fork_name = harness
.chain
.spec
.fork_name_at_slot::<E>(harness.chain.slot().unwrap());
let single_attestations = if fork_name.electra_enabled() {
harness
.get_single_attestations(
&AttestationStrategy::AllValidators,
&head.beacon_state,
head_state_root,
head.beacon_block_root,
harness.chain.slot().unwrap(),
)
.into_iter()
.flat_map(|vec| vec.into_iter().map(|(attestation, _subnet_id)| attestation))
.collect::<Vec<_>>()
} else {
vec![]
};
let current_epoch = harness
.chain
.slot()
@@ -294,6 +317,7 @@ impl ApiTester {
next_block,
reorg_block,
attestations,
single_attestations,
contribution_and_proofs,
attester_slashing,
proposer_slashing,
@@ -381,6 +405,7 @@ impl ApiTester {
next_block,
reorg_block,
attestations,
single_attestations: vec![],
contribution_and_proofs: vec![],
attester_slashing,
proposer_slashing,
@@ -1801,12 +1826,12 @@ impl ApiTester {
pub async fn test_post_beacon_pool_attestations_valid_v2(mut self) -> Self {
let fork_name = self
.attestations
.single_attestations
.first()
.map(|att| self.chain.spec.fork_name_at_slot::<E>(att.data().slot))
.map(|att| self.chain.spec.fork_name_at_slot::<E>(att.data.slot))
.unwrap();
self.client
.post_beacon_pool_attestations_v1(self.attestations.as_slice())
.post_beacon_pool_attestations_v2(self.single_attestations.as_slice(), fork_name)
.await
.unwrap();
assert!(
@@ -1855,9 +1880,9 @@ impl ApiTester {
}
pub async fn test_post_beacon_pool_attestations_invalid_v2(mut self) -> Self {
let mut attestations = Vec::new();
for attestation in &self.attestations {
for attestation in &self.single_attestations {
let mut invalid_attestation = attestation.clone();
invalid_attestation.data_mut().slot += 1;
invalid_attestation.data.slot += 1;
// add both to ensure we only fail on invalid attestations
attestations.push(attestation.clone());
@@ -1872,7 +1897,7 @@ impl ApiTester {
let err_v2 = self
.client
.post_beacon_pool_attestations_v1(attestations.as_slice())
.post_beacon_pool_attestations_v2(attestations.as_slice(), fork_name)
.await
.unwrap_err();