Fix Babe revert when last finalized block is a leaf (#11500)

* Fix Babe revert when a leaf is the last finalized block

Without this fix the last finalized block weight data is wrongly removed
on revert scenario where the last finalized block is a leaf.

* Remove redundant check

* Added test to exercise the fix

* Rename test

* Give variables better names
This commit is contained in:
Davide Galassi
2022-05-24 19:24:55 +02:00
committed by GitHub
parent f744a1a01b
commit 35af8fd726
2 changed files with 55 additions and 17 deletions
@@ -814,6 +814,44 @@ fn revert_prunes_epoch_changes_and_removes_weights() {
assert!(weight_data_check(&fork3, false));
}
#[test]
fn revert_not_allowed_for_finalized() {
let mut net = BabeTestNet::new(1);
let peer = net.peer(0);
let data = peer.data.as_ref().expect("babe link set up during initialization");
let client = peer.client().as_client();
let backend = peer.client().as_backend();
let mut block_import = data.block_import.lock().take().expect("import set up during init");
let mut proposer_factory = DummyFactory {
client: client.clone(),
config: data.link.config.clone(),
epoch_changes: data.link.epoch_changes.clone(),
mutator: Arc::new(|_, _| ()),
};
let mut propose_and_import_blocks_wrap = |parent_id, n| {
propose_and_import_blocks(&client, &mut proposer_factory, &mut block_import, parent_id, n)
};
let canon = propose_and_import_blocks_wrap(BlockId::Number(0), 3);
// Finalize best block
client.finalize_block(BlockId::Hash(canon[2]), None, false).unwrap();
// Revert canon chain to last finalized block
revert(client.clone(), backend, 100).expect("revert should work for baked test scenario");
let weight_data_check = |hashes: &[Hash], expected: bool| {
hashes.iter().all(|hash| {
aux_schema::load_block_weight(&*client, hash).unwrap().is_some() == expected
})
};
assert!(weight_data_check(&canon, true));
}
#[test]
fn importing_epoch_change_block_prunes_tree() {
let mut net = BabeTestNet::new(1);