Skip to content

Batch attestation slashibility checking #6219

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 29 commits into
base: unstable
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
b5ac2a6
refactor to separate attn signing and db slashibility checks
eserilev Aug 4, 2024
a7fe031
adding comments, a few logs, some more TODO's
eserilev Aug 4, 2024
ee691f9
only download attn data once and mutate index when required
eserilev Aug 5, 2024
14cbae8
batch db txn
eserilev Aug 5, 2024
9b1302a
fix some tests, linting, logs
eserilev Aug 6, 2024
57bd693
remove unneeded clones
eserilev Aug 7, 2024
e4d5e79
Merge branch 'unstable' of https://github.com/sigp/lighthouse into ba…
eserilev Aug 7, 2024
73f1d55
linnt
eserilev Aug 7, 2024
d07564c
fmt
eserilev Aug 7, 2024
420ce57
working on test fixes
eserilev Aug 9, 2024
e9112b1
fix test
eserilev Aug 12, 2024
d7a023f
Merge branch 'unstable' of https://github.com/sigp/lighthouse into ba…
eserilev Aug 12, 2024
ecf42f0
prevent db commit when slashing is not avail
eserilev Aug 13, 2024
b469d65
add more granular metrics
eserilev Aug 15, 2024
cdf219b
fix test
eserilev Aug 23, 2024
9140a6d
Merge branch 'unstable' into batch-attestation-slashibility-checking
eserilev Sep 22, 2024
b3b818f
resolve merge conflicts
eserilev Oct 3, 2024
4715750
Resolve merge conflicts
eserilev Oct 3, 2024
509d926
merge conflicts
eserilev Nov 21, 2024
f5dacb6
remove unused import
eserilev Nov 22, 2024
98b2f7c
fix
eserilev Nov 23, 2024
67c6f3a
retry
eserilev Nov 23, 2024
c5c6c5b
merge conflicts
eserilev Apr 4, 2025
6ece2be
fix test
eserilev Apr 4, 2025
dece5b6
optimize publish aggs
eserilev Apr 4, 2025
d9e27c6
fix lint
eserilev Apr 4, 2025
a463e8d
fmt
eserilev Apr 4, 2025
f35a028
Merge branch 'unstable' of https://github.com/sigp/lighthouse into ba…
eserilev Apr 4, 2025
d8d6da9
linting
eserilev Apr 4, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions beacon_node/http_api/tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1862,7 +1862,7 @@ impl ApiTester {

pub async fn test_post_beacon_pool_attestations_valid(mut self) -> Self {
self.client
.post_beacon_pool_attestations_v1(self.attestations.as_slice())
.post_beacon_pool_attestations_v1(&self.attestations)
.await
.unwrap();

Expand Down Expand Up @@ -1923,7 +1923,7 @@ impl ApiTester {

let err = self
.client
.post_beacon_pool_attestations_v1(attestations.as_slice())
.post_beacon_pool_attestations_v1(&attestations)
.await
.unwrap_err();

Expand Down Expand Up @@ -4062,7 +4062,7 @@ impl ApiTester {

// Attest to the current slot
self.client
.post_beacon_pool_attestations_v1(self.attestations.as_slice())
.post_beacon_pool_attestations_v1(&self.attestations)
.await
.unwrap();

Expand Down Expand Up @@ -5801,7 +5801,7 @@ impl ApiTester {

// Attest to the current slot
self.client
.post_beacon_pool_attestations_v1(self.attestations.as_slice())
.post_beacon_pool_attestations_v1(&self.attestations)
.await
.unwrap();

Expand Down Expand Up @@ -5857,7 +5857,7 @@ impl ApiTester {
let expected_attestation_len = self.attestations.len();

self.client
.post_beacon_pool_attestations_v1(self.attestations.as_slice())
.post_beacon_pool_attestations_v1(&self.attestations)
.await
.unwrap();

Expand Down
61 changes: 61 additions & 0 deletions testing/web3signer_tests/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ mod tests {
use parking_lot::Mutex;
use reqwest::Client;
use serde::Serialize;
use slashing_protection::NotSafe;
use slashing_protection::{SlashingDatabase, SLASHING_PROTECTION_FILENAME};
use slot_clock::{SlotClock, TestingSlotClock};
use std::env;
Expand Down Expand Up @@ -838,9 +839,29 @@ mod tests {
"double_vote_attestation",
move |pubkey, validator_store| async move {
let mut attestation = double_vote_attestation();

validator_store
.sign_attestation(pubkey, 0, &mut attestation, current_epoch)
.await
.unwrap();

let safe_attestations = validator_store
.check_and_insert_attestations(vec![(attestation, pubkey)])
.unwrap();

if !slashable_message_should_sign && !safe_attestations.is_empty() {
// if slashability checks are disabled and we don't return any safe attestations
// we raise an error to indicate that thi test case has failed
return Err(ValidatorStoreError::Slashable(NotSafe::ConsistencyError));
}

if slashable_message_should_sign && safe_attestations.is_empty() {
// if slashability checks are en abled and we return safe attestations
// we raise an error to indicate that this test case has failed
return Err(ValidatorStoreError::Slashable(NotSafe::ConsistencyError));
}

Ok(())
},
slashable_message_should_sign,
)
Expand All @@ -849,9 +870,29 @@ mod tests {
"surrounding_attestation",
move |pubkey, validator_store| async move {
let mut attestation = surrounding_attestation();

validator_store
.sign_attestation(pubkey, 0, &mut attestation, current_epoch)
.await
.unwrap();

let safe_attestations = validator_store
.check_and_insert_attestations(vec![(attestation, pubkey)])
.unwrap();

if !slashable_message_should_sign && !safe_attestations.is_empty() {
// if slashability checks are disabled and we don't return any safe attestations
// we raise an error to indicate that thi test case has failed
return Err(ValidatorStoreError::Slashable(NotSafe::ConsistencyError));
}

if slashable_message_should_sign && safe_attestations.is_empty() {
// if slashability checks are eabled and we return safe attestations
// we raise an error to indicate that this test case has failed
return Err(ValidatorStoreError::Slashable(NotSafe::ConsistencyError));
}

Ok(())
},
slashable_message_should_sign,
)
Expand All @@ -860,9 +901,29 @@ mod tests {
"surrounded_attestation",
move |pubkey, validator_store| async move {
let mut attestation = surrounded_attestation();

validator_store
.sign_attestation(pubkey, 0, &mut attestation, current_epoch)
.await
.unwrap();

let safe_attestations = validator_store
.check_and_insert_attestations(vec![(attestation, pubkey)])
.unwrap();

if !slashable_message_should_sign && !safe_attestations.is_empty() {
// if slashability checks are disabled and we don't return any safe attestations
// we raise an error to indicate that thi test case has failed
return Err(ValidatorStoreError::Slashable(NotSafe::ConsistencyError));
}

if slashable_message_should_sign && safe_attestations.is_empty() {
// if slashability checks are eabled and we return safe attestations
// we raise an error to indicate that this test case has failed
return Err(ValidatorStoreError::Slashable(NotSafe::ConsistencyError));
}

Ok(())
},
slashable_message_should_sign,
)
Expand Down
108 changes: 100 additions & 8 deletions validator_client/http_api/src/tests/keystores.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ use eth2::lighthouse_vc::{
std_types::{KeystoreJsonStr as Keystore, *},
types::Web3SignerValidatorRequest,
};
use eth2::types::AttesterData;
use itertools::Itertools;
use rand::{rngs::SmallRng, Rng, SeedableRng};
use slashing_protection::interchange::{Interchange, InterchangeMetadata};
use std::{collections::HashMap, path::Path};
use tokio::runtime::Handle;
use types::{attestation::AttestationBase, Address};
use validator_services::duties_service::DutyAndProof;
use validator_store::DEFAULT_GAS_LIMIT;
use zeroize::Zeroizing;

Expand Down Expand Up @@ -1092,17 +1094,53 @@ async fn generic_migration_test(
.unwrap();
check_keystore_import_response(&import_res, all_imported(keystores.len()));

let attestations_and_duties = first_vc_attestations
.into_iter()
.map(|(validator_index, attestation)| {
(
attestation.clone(),
DutyAndProof::new_without_selection_proof(
AttesterData {
pubkey: keystore_pubkey(&keystores[validator_index]),
validator_index: validator_index as u64,
committees_at_slot: 0,
committee_index: 0,
committee_length: 0,
validator_committee_index: 0,
slot: attestation.data().slot,
},
attestation.data().slot,
),
)
})
.collect::<Vec<_>>();

// Sign attestations on VC1.
for (validator_index, mut attestation) in first_vc_attestations {
let public_key = keystore_pubkey(&keystores[validator_index]);
for (mut attestation, validator_duty) in attestations_and_duties.clone() {
let current_epoch = attestation.data().target.epoch;
tester1
.validator_store
.sign_attestation(public_key, 0, &mut attestation, current_epoch)
.sign_attestation(
validator_duty.duty.pubkey,
0,
&mut attestation,
current_epoch,
)
.await
.unwrap();
}

tester1
.validator_store
.check_and_insert_attestations(
attestations_and_duties
.clone()
.into_iter()
.map(|(a, b)| (a.clone(), b.duty.pubkey))
.collect::<Vec<_>>(),
)
.unwrap();

// Delete the selected keys from VC1.
let delete_res = tester1
.client
Expand All @@ -1115,6 +1153,7 @@ async fn generic_migration_test(
})
.await
.unwrap();

check_keystore_delete_response(&delete_res, all_deleted(delete_indices.len()));

// Check that slashing protection data was returned for all selected validators.
Expand Down Expand Up @@ -1168,17 +1207,70 @@ async fn generic_migration_test(
.unwrap();
check_keystore_import_response(&import_res, all_imported(import_indices.len()));

let attestations_and_duties = second_vc_attestations
.into_iter()
.map(|(validator_index, attestation, should_succeed)| {
(
attestation.clone(),
DutyAndProof::new_without_selection_proof(
AttesterData {
pubkey: keystore_pubkey(&keystores[validator_index]),
validator_index: validator_index as u64,
committees_at_slot: 0,
committee_index: 0,
committee_length: 0,
validator_committee_index: 0,
slot: attestation.data().slot,
},
attestation.data().slot,
),
should_succeed,
)
})
.collect::<Vec<_>>();

// Sign attestations on the second VC.
for (validator_index, mut attestation, should_succeed) in second_vc_attestations {
let public_key = keystore_pubkey(&keystores[validator_index]);
for (mut attestation, validator_duty, should_succeed) in attestations_and_duties.clone() {
let current_epoch = attestation.data().target.epoch;

match tester2
.validator_store
.sign_attestation(public_key, 0, &mut attestation, current_epoch)
.sign_attestation(
validator_duty.duty.pubkey,
0,
&mut attestation,
current_epoch,
)
.await
{
Ok(()) => assert!(should_succeed),
Err(e) => assert!(!should_succeed, "{:?}", e),
Ok(_) => (),
Err(_) => {
if should_succeed {
panic!("Should succeed");
} else {
continue;
}
}
};

let attestation_and_duty = vec![(attestation, validator_duty)];

let Ok(safe_attestation) = tester2.validator_store.check_and_insert_attestations(
attestation_and_duty
.clone()
.into_iter()
.map(|(a, b)| (a.clone(), b.duty.pubkey))
.collect::<Vec<_>>(),
) else {
panic!("Should succeed");
};

if !safe_attestation.is_empty() && !should_succeed {
panic!("should fail")
}

if safe_attestation.is_empty() && should_succeed {
panic!("should succeed")
}
}
})
Expand Down
9 changes: 9 additions & 0 deletions validator_client/slashing_protection/src/interchange_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use crate::{
test_utils::{pubkey, DEFAULT_GENESIS_VALIDATORS_ROOT},
SigningRoot, SlashingDatabase,
};
use rusqlite::TransactionBehavior;
use serde::{Deserialize, Serialize};
use std::collections::HashSet;
use tempfile::tempdir;
Expand Down Expand Up @@ -132,12 +133,18 @@ impl MultiTestCase {
}
}

let mut conn = slashing_db.get_db_connection().unwrap();
let txn = conn
.transaction_with_behavior(TransactionBehavior::Exclusive)
.unwrap();

for (i, att) in test_case.attestations.iter().enumerate() {
match slashing_db.check_and_insert_attestation_signing_root(
&att.pubkey,
att.source_epoch,
att.target_epoch,
SigningRoot::from(att.signing_root),
&txn,
) {
Ok(safe) if !att.should_succeed => {
panic!(
Expand All @@ -154,6 +161,8 @@ impl MultiTestCase {
_ => (),
}
}

slashing_db.commit(txn).unwrap();
}
}
}
Expand Down
20 changes: 17 additions & 3 deletions validator_client/slashing_protection/src/parallel_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use crate::block_tests::block;
use crate::test_utils::*;
use crate::*;
use rayon::prelude::*;
use rusqlite::TransactionBehavior;
use tempfile::tempdir;

#[test]
Expand Down Expand Up @@ -44,11 +45,18 @@ fn attestation_same_target() {
let results = (0..num_attestations)
.into_par_iter()
.map(|i| {
slashing_db.check_and_insert_attestation(
let mut conn = slashing_db.get_db_connection().unwrap();
let txn = conn
.transaction_with_behavior(TransactionBehavior::Exclusive)
.unwrap();
let result = slashing_db.check_and_insert_attestation(
&pk,
&attestation_data_builder(i, num_attestations),
DEFAULT_DOMAIN,
)
&txn,
);
slashing_db.commit(txn).unwrap();
result
})
.collect::<Vec<_>>();

Expand All @@ -72,8 +80,14 @@ fn attestation_surround_fest() {
let results = (0..num_attestations)
.into_par_iter()
.map(|i| {
let mut conn = slashing_db.get_db_connection().unwrap();
let txn = conn
.transaction_with_behavior(TransactionBehavior::Exclusive)
.unwrap();
let att = attestation_data_builder(i, 2 * num_attestations - i);
slashing_db.check_and_insert_attestation(&pk, &att, DEFAULT_DOMAIN)
let result = slashing_db.check_and_insert_attestation(&pk, &att, DEFAULT_DOMAIN, &txn);
slashing_db.commit(txn).unwrap();
result
})
.collect::<Vec<_>>();

Expand Down
Loading
Loading