Skip to content

Commit 5a24bda

Browse files
committed
Refactor
1 parent 07907ac commit 5a24bda

File tree

5 files changed

+76
-104
lines changed

5 files changed

+76
-104
lines changed

testing/web3signer_tests/src/lib.rs

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -605,12 +605,13 @@ mod tests {
605605
})
606606
.await
607607
.assert_signatures_match("attestation", |pubkey, validator_store| async move {
608-
let mut attestation = get_attestation();
608+
let attestation = get_attestation();
609609
validator_store
610-
.sign_attestation(pubkey, 0, &mut attestation, Epoch::new(0))
610+
.sign_attestations(vec![(pubkey, 0, attestation)])
611611
.await
612-
.unwrap();
613-
attestation
612+
.unwrap()
613+
.pop()
614+
.unwrap()
614615
})
615616
.await
616617
.assert_signatures_match("signed_aggregate", |pubkey, validator_store| async move {
@@ -820,8 +821,6 @@ mod tests {
820821
block
821822
};
822823

823-
let current_epoch = Epoch::new(5);
824-
825824
TestingRig::new(
826825
network,
827826
slashing_protection_config,
@@ -830,48 +829,47 @@ mod tests {
830829
)
831830
.await
832831
.assert_signatures_match("first_attestation", |pubkey, validator_store| async move {
833-
let mut attestation = first_attestation();
832+
let attestation = first_attestation();
834833
validator_store
835-
.sign_attestation(pubkey, 0, &mut attestation, current_epoch)
834+
.sign_attestations(vec![(pubkey, 0, attestation)])
836835
.await
837-
.unwrap();
838-
validator_store
839-
.check_and_insert_attestations(vec![(attestation, pubkey)])
840836
.unwrap()
841837
.pop()
842838
.unwrap()
843-
.0
844839
})
845840
.await
846841
.assert_slashable_message_should_sign(
847842
"double_vote_attestation",
848843
move |pubkey, validator_store| async move {
849-
let mut attestation = double_vote_attestation();
844+
let attestation = double_vote_attestation();
850845
validator_store
851-
.sign_attestation(pubkey, 0, &mut attestation, current_epoch)
846+
.sign_attestations(vec![(pubkey, 0, attestation)])
852847
.await
848+
.map(|_| ())
853849
},
854850
slashable_message_should_sign,
855851
)
856852
.await
857853
.assert_slashable_message_should_sign(
858854
"surrounding_attestation",
859855
move |pubkey, validator_store| async move {
860-
let mut attestation = surrounding_attestation();
856+
let attestation = surrounding_attestation();
861857
validator_store
862-
.sign_attestation(pubkey, 0, &mut attestation, current_epoch)
858+
.sign_attestations(vec![(pubkey, 0, attestation)])
863859
.await
860+
.map(|_| ())
864861
},
865862
slashable_message_should_sign,
866863
)
867864
.await
868865
.assert_slashable_message_should_sign(
869866
"surrounded_attestation",
870867
move |pubkey, validator_store| async move {
871-
let mut attestation = surrounded_attestation();
868+
let attestation = surrounded_attestation();
872869
validator_store
873-
.sign_attestation(pubkey, 0, &mut attestation, current_epoch)
870+
.sign_attestations(vec![(pubkey, 0, attestation)])
874871
.await
872+
.map(|_| ())
875873
},
876874
slashable_message_should_sign,
877875
)

validator_client/http_api/src/tests/keystores.rs

Lines changed: 22 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1099,20 +1099,15 @@ async fn generic_migration_test(
10991099
check_keystore_import_response(&import_res, all_imported(keystores.len()));
11001100

11011101
// Sign attestations on VC1.
1102-
for (validator_index, mut attestation) in first_vc_attestations {
1102+
for (validator_index, attestation) in first_vc_attestations {
11031103
let public_key = keystore_pubkey(&keystores[validator_index]);
1104-
let current_epoch = attestation.data().target.epoch;
1105-
tester1
1106-
.validator_store
1107-
.sign_attestation(public_key, 0, &mut attestation, current_epoch)
1108-
.await
1109-
.unwrap();
11101104
let safe_attestations = tester1
11111105
.validator_store
1112-
.check_and_insert_attestations(vec![(attestation.clone(), public_key)])
1106+
.sign_attestations(vec![(public_key, 0, attestation.clone())])
1107+
.await
11131108
.unwrap();
11141109
assert_eq!(safe_attestations.len(), 1);
1115-
assert_eq!(safe_attestations, vec![(attestation, public_key)]);
1110+
assert_eq!(safe_attestations, vec![attestation]);
11161111
}
11171112

11181113
// Delete the selected keys from VC1.
@@ -1184,27 +1179,24 @@ async fn generic_migration_test(
11841179
check_keystore_import_response(&import_res, all_imported(import_indices.len()));
11851180

11861181
// Sign attestations on the second VC.
1187-
for (validator_index, mut attestation, should_succeed) in second_vc_attestations {
1182+
for (validator_index, attestation, should_succeed) in second_vc_attestations {
11881183
let public_key = keystore_pubkey(&keystores[validator_index]);
1189-
let current_epoch = attestation.data().target.epoch;
1190-
if tester2
1191-
.validator_store
1192-
.sign_attestation(public_key, 0, &mut attestation, current_epoch)
1193-
.await
1194-
.is_err()
1195-
{
1196-
// Doppelganger protected.
1197-
assert!(!should_succeed);
1198-
continue;
1199-
}
1200-
let safe_attestations = tester2
1184+
let result = tester2
12011185
.validator_store
1202-
.check_and_insert_attestations(vec![(attestation.clone(), public_key)])
1203-
.unwrap();
1204-
if should_succeed {
1205-
assert_eq!(safe_attestations[0], (attestation, public_key));
1206-
} else {
1207-
assert!(safe_attestations.is_empty());
1186+
.sign_attestations(vec![(public_key, 0, attestation.clone())])
1187+
.await;
1188+
match result {
1189+
Ok(safe_attestations) => {
1190+
if should_succeed {
1191+
assert_eq!(safe_attestations, vec![attestation]);
1192+
} else {
1193+
assert!(safe_attestations.is_empty());
1194+
}
1195+
}
1196+
Err(_) => {
1197+
// Doppelganger protected or other error.
1198+
assert!(!should_succeed);
1199+
}
12081200
}
12091201
}
12101202
})
@@ -1330,10 +1322,10 @@ async fn delete_concurrent_with_signing() {
13301322

13311323
let handle = handle.spawn(async move {
13321324
for j in 0..num_attestations {
1333-
let mut att = make_attestation(j, j + 1);
1325+
let att = make_attestation(j, j + 1);
13341326
for public_key in thread_pubkeys.iter() {
13351327
let _ = validator_store
1336-
.sign_attestation(*public_key, 0, &mut att, Epoch::new(j + 1))
1328+
.sign_attestations(vec![(*public_key, 0, att.clone())])
13371329
.await;
13381330
}
13391331
}

validator_client/lighthouse_validator_store/src/lib.rs

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -750,8 +750,8 @@ impl<T: SlotClock + 'static, E: EthSpec> ValidatorStore for LighthouseValidatorS
750750

751751
async fn sign_attestations(
752752
&self,
753-
mut attestations: Vec<(PublicKeyBytes, usize, &mut Attestation<Self::E>)>,
754-
) -> Result<(), Error> {
753+
mut attestations: Vec<(PublicKeyBytes, usize, Attestation<Self::E>)>,
754+
) -> Result<Vec<Attestation<E>>, Error> {
755755
// Sign all attestations concurrently.
756756
let signing_futures = attestations.iter_mut().map(|(pubkey, validator_committee_index, attestation)| {
757757
let pubkey = *pubkey;
@@ -764,35 +764,43 @@ impl<T: SlotClock + 'static, E: EthSpec> ValidatorStore for LighthouseValidatorS
764764
attestation,
765765
)
766766
.await
767-
.map(|_| (pubkey, validator_committee_index))
767+
.map(|_| pubkey)
768768
}
769769
});
770770

771771
// Execute all signing in parallel.
772772
let results: Vec<_> = join_all(signing_futures).await;
773773

774-
// Log errors but don't fail the entire batch.
775-
for result in results {
776-
if let Err(e) = result {
777-
match e {
778-
ValidatorStoreError::UnknownPubkey(pubkey) => {
779-
warn!(
780-
info = "a validator may have recently been removed from this VC",
781-
?pubkey,
782-
"Missing pubkey for attestation"
783-
);
784-
}
785-
e => {
786-
crit!(
787-
error = ?e,
788-
"Failed to sign attestation"
789-
);
790-
}
774+
// Collect successfully signed attestations and log errors.
775+
let mut signed_attestations = Vec::new();
776+
for (result, (pubkey, _, attestation)) in results.into_iter().zip(attestations.into_iter()) {
777+
match result {
778+
Ok(_) => {
779+
signed_attestations.push((attestation, pubkey));
780+
}
781+
Err(ValidatorStoreError::UnknownPubkey(pubkey)) => {
782+
warn!(
783+
info = "a validator may have recently been removed from this VC",
784+
?pubkey,
785+
"Missing pubkey for attestation"
786+
);
787+
}
788+
Err(e) => {
789+
crit!(
790+
error = ?e,
791+
"Failed to sign attestation"
792+
);
791793
}
792794
}
793795
}
794796

795-
Ok(())
797+
if signed_attestations.is_empty() {
798+
return Ok(Vec::new());
799+
}
800+
801+
// Check slashing protection and insert into database.
802+
let safe_attestations = self.check_and_insert_attestations(signed_attestations)?;
803+
Ok(safe_attestations.into_iter().map(|(a, _)| a).collect())
796804
}
797805

798806
#[instrument(skip_all)]

validator_client/validator_services/src/attestation_service.rs

Lines changed: 6 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -445,48 +445,21 @@ impl<S: ValidatorStore + 'static, T: SlotClock + 'static> AttestationService<S,
445445
return Ok(());
446446
}
447447

448-
// Sign all attestations concurrently.
449-
let attestations_refs: Vec<_> = attestations_to_sign
450-
.iter_mut()
451-
.map(|(pubkey, index, attestation)| (*pubkey, *index, attestation))
452-
.collect();
453-
454-
self.validator_store
455-
.sign_attestations(attestations_refs)
448+
// Sign and check all attestations (includes slashing protection).
449+
let safe_attestations = self
450+
.validator_store
451+
.sign_attestations(attestations_to_sign)
456452
.await
457453
.map_err(|e| format!("Failed to sign attestations: {e:?}"))?;
458454

459-
// Collect signed attestations.
460-
let signed_attestations: Vec<_> = attestations_to_sign
461-
.into_iter()
462-
.map(|(pubkey, _, attestation)| (attestation, pubkey))
463-
.collect();
464-
465-
if signed_attestations.is_empty() {
455+
if safe_attestations.is_empty() {
466456
warn!("No attestations were published");
467457
return Ok(());
468458
}
469459
let fork_name = self
470460
.chain_spec
471461
.fork_name_at_slot::<S::E>(attestation_data.slot);
472462

473-
// Check slashing protection in a blocking thread (this is I/O bound).
474-
let service = self.clone();
475-
let safe_attestations = self
476-
.inner
477-
.executor
478-
.spawn_blocking_handle(
479-
move || {
480-
service
481-
.validator_store
482-
.check_and_insert_attestations(signed_attestations)
483-
},
484-
"check_and_insert_attestations",
485-
)
486-
.ok_or("shutting down")?
487-
.await
488-
.map_err(|e| format!("thread error checking slashability: {e:?}"))?
489-
.map_err(|e| format!("error checking slashability: {e:?}"))?;
490463
let safe_attestations = &safe_attestations;
491464

492465
// Post the attestations to the BN.
@@ -502,7 +475,7 @@ impl<S: ValidatorStore + 'static, T: SlotClock + 'static> AttestationService<S,
502475
let single_attestations = safe_attestations
503476
.iter()
504477
.zip(validator_indices_ref)
505-
.filter_map(|((a, _), i)| {
478+
.filter_map(|(a, i)| {
506479
match a.to_single_attestation_with_attester_index(*i) {
507480
Ok(a) => Some(a),
508481
Err(e) => {

validator_client/validator_store/src/lib.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,11 @@ pub trait ValidatorStore: Send + Sync {
103103
current_slot: Slot,
104104
) -> impl Future<Output = Result<SignedBlock<Self::E>, Error<Self::Error>>> + Send;
105105

106+
#[allow(clippy::type_complexity)]
106107
fn sign_attestations(
107108
&self,
108-
attestations: Vec<(PublicKeyBytes, usize, &mut Attestation<Self::E>)>,
109-
) -> impl Future<Output = Result<(), Error<Self::Error>>> + Send;
109+
attestations: Vec<(PublicKeyBytes, usize, Attestation<Self::E>)>,
110+
) -> impl Future<Output = Result<Vec<Attestation<Self::E>>, Error<Self::Error>>> + Send;
110111

111112
fn sign_attestation_no_checks(
112113
&self,

0 commit comments

Comments
 (0)