Finalized should be before best (#14308)

* Finalized block should not be after best block

* Remove unwrap

* Apply code review suggestion

Co-authored-by: Koute <koute@users.noreply.github.com>

* Add test

---------

Co-authored-by: Koute <koute@users.noreply.github.com>
This commit is contained in:
Davide Galassi
2023-06-07 11:17:45 +02:00
committed by GitHub
parent 6f79a9e941
commit b14238cb7d
2 changed files with 67 additions and 25 deletions
@@ -2001,3 +2001,41 @@ fn use_dalek_ext_works() {
.verify_ed25519(a1.hash(), zero_ed_sig(), zero_ed_pub(), vec![])
.unwrap());
}
#[test]
fn finalize_after_best_block_updates_best() {
let mut client = substrate_test_runtime_client::new();
// G -> A1
let a1 = client.new_block(Default::default()).unwrap().build().unwrap().block;
block_on(client.import(BlockOrigin::Own, a1.clone())).unwrap();
// A1 -> A2
let a2 = client
.new_block_at(a1.hash(), Default::default(), false)
.unwrap()
.build()
.unwrap()
.block;
block_on(client.import(BlockOrigin::Own, a2.clone())).unwrap();
// A2 -> A3
let a3 = client
.new_block_at(a2.hash(), Default::default(), false)
.unwrap()
.build()
.unwrap()
.block;
let (header, extrinsics) = a3.clone().deconstruct();
let mut import_params = BlockImportParams::new(BlockOrigin::Own, header);
import_params.body = Some(extrinsics);
import_params.fork_choice = Some(ForkChoiceStrategy::Custom(false));
block_on(client.import_block(import_params)).unwrap();
assert_eq!(client.chain_info().best_hash, a2.hash());
client.finalize_block(a3.hash(), None).unwrap();
assert_eq!(client.chain_info().finalized_hash, a3.hash());
assert_eq!(client.chain_info().best_hash, a3.hash());
}