babe: account for skipped epochs when handling equivocations (#13335)

* babe: account for skipped epochs when handling equivocations

* typos

* babe: enforce epoch index >= session index
This commit is contained in:
André Silva
2023-02-17 11:46:43 +00:00
committed by GitHub
parent b7e58e7c75
commit 64bff4529d
2 changed files with 146 additions and 6 deletions
+62
View File
@@ -826,6 +826,56 @@ fn report_equivocation_has_valid_weight() {
.all(|w| w[0].ref_time() < w[1].ref_time()));
}
#[test]
fn report_equivocation_after_skipped_epochs_works() {
let (pairs, mut ext) = new_test_ext_with_pairs(3);
ext.execute_with(|| {
let epoch_duration: u64 = <Test as Config>::EpochDuration::get();
// this sets the genesis slot to 100;
let genesis_slot = 100;
go_to_block(1, genesis_slot);
assert_eq!(EpochIndex::<Test>::get(), 0);
// skip from epoch #0 to epoch #10
go_to_block(System::block_number() + 1, genesis_slot + epoch_duration * 10);
assert_eq!(EpochIndex::<Test>::get(), 10);
assert_eq!(SkippedEpochs::<Test>::get(), vec![(10, 1)]);
// generate an equivocation proof for validator at index 1
let authorities = Babe::authorities();
let offending_validator_index = 1;
let offending_authority_pair = pairs
.into_iter()
.find(|p| p.public() == authorities[offending_validator_index].0)
.unwrap();
let equivocation_proof = generate_equivocation_proof(
offending_validator_index as u32,
&offending_authority_pair,
CurrentSlot::<Test>::get(),
);
// create the key ownership proof
let key = (sp_consensus_babe::KEY_TYPE, &offending_authority_pair.public());
let key_owner_proof = Historical::prove(key).unwrap();
// which is for session index 1 (while current epoch index is 10)
assert_eq!(key_owner_proof.session, 1);
// report the equivocation, in order for the validation to pass the mapping
// between epoch index and session index must be checked.
assert!(Babe::report_equivocation_unsigned(
RuntimeOrigin::none(),
Box::new(equivocation_proof),
key_owner_proof
)
.is_ok());
})
}
#[test]
fn valid_equivocation_reports_dont_pay_fees() {
let (pairs, mut ext) = new_test_ext_with_pairs(3);
@@ -977,5 +1027,17 @@ fn skipping_over_epochs_works() {
assert_eq!(EpochIndex::<Test>::get(), 4);
assert_eq!(Randomness::<Test>::get(), randomness_for_epoch_2);
// after skipping epochs the information is registered on-chain so that
// we can map epochs to sessions
assert_eq!(SkippedEpochs::<Test>::get(), vec![(4, 2)]);
// before epochs are skipped the mapping should be one to one
assert_eq!(Babe::session_index_for_epoch(0), 0);
assert_eq!(Babe::session_index_for_epoch(1), 1);
// otherwise the session index is offset by the number of skipped epochs
assert_eq!(Babe::session_index_for_epoch(4), 2);
assert_eq!(Babe::session_index_for_epoch(5), 3);
});
}