Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Merged
Changes from all commits
Commits
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
41 changes: 40 additions & 1 deletion client/db/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1472,7 +1472,8 @@ impl<Block: BlockT> Backend<Block> {
}

if !existing_header {
{
// Add a new leaf if the block has the potential to be finalized.
if number > last_finalized_num || last_finalized_num.is_zero() {
let mut leaves = self.blockchain.leaves.write();
leaves.import(hash, number, parent_hash);
leaves.prepare_transaction(
Expand Down Expand Up @@ -2330,6 +2331,29 @@ pub(crate) mod tests {
Ok(header_hash)
}

pub fn insert_header_no_head(
backend: &Backend<Block>,
number: u64,
parent_hash: H256,
extrinsics_root: H256,
) -> H256 {
use sp_runtime::testing::Digest;

let digest = Digest::default();
let header = Header {
number,
parent_hash,
state_root: BlakeTwo256::trie_root(Vec::new(), StateVersion::V1),
digest,
extrinsics_root,
};
let header_hash = header.hash();
let mut op = backend.begin_operation().unwrap();
op.set_block_data(header, None, None, None, NewBlockState::Normal).unwrap();
backend.commit_operation(op).unwrap();
header_hash
}

#[test]
fn block_hash_inserted_correctly() {
let backing = {
Expand Down Expand Up @@ -3309,4 +3333,19 @@ pub(crate) mod tests {
e @ _ => panic!("Unexpected error {:?}", e),
}
}

#[test]
fn test_leaves_not_created_for_ancient_blocks() {
let backend: Backend<Block> = Backend::new_test(10, 10);
let block0 = insert_header(&backend, 0, Default::default(), None, Default::default());

let block1_a = insert_header(&backend, 1, block0, None, Default::default());
let block2_a = insert_header(&backend, 2, block1_a, None, Default::default());
backend.finalize_block(BlockId::hash(block1_a), None).unwrap();
assert_eq!(backend.blockchain().leaves().unwrap(), vec![block2_a]);

// Insert a fork prior to finalization point. Leave should not be created.
insert_header_no_head(&backend, 1, block0, [1; 32].into());
assert_eq!(backend.blockchain().leaves().unwrap(), vec![block2_a]);
}
}