#![forbid(unsafe_code)]
#[path = "common/mod.rs"]
#[allow(dead_code)]
mod common;
use chia_protocol::Bytes32;
use dig_blockstore::BlockStore;
use common::{build_chain, temp_blockstore_dir, test_block, test_config};
#[test]
fn test_non_canonical_blocks_pruned() {
let (_guard, path) = temp_blockstore_dir();
let store = BlockStore::open(test_config(path)).expect("open");
let chain = build_chain(10);
for block in &chain {
store.extend_chain(block).expect("extend");
}
let fork1 = test_block(2, Bytes32::new([0xAA; 32]));
let fork2 = test_block(3, fork1.hash());
store.put_block(&fork1, false).expect("put fork1");
store.put_block(&fork2, false).expect("put fork2");
store.prune_before_height(5).expect("prune");
assert!(
store.get_block(&fork1.hash()).expect("f1").is_none(),
"non-canonical fork block at height 2 should be pruned"
);
assert!(
store.get_block(&fork2.hash()).expect("f2").is_none(),
"non-canonical fork block at height 3 should be pruned"
);
}
#[test]
fn test_non_canonical_blocks_above_height_retained() {
let (_guard, path) = temp_blockstore_dir();
let store = BlockStore::open(test_config(path)).expect("open");
let chain = build_chain(10);
for block in &chain {
store.extend_chain(block).expect("extend");
}
let fork = test_block(7, Bytes32::new([0xBB; 32]));
store.put_block(&fork, false).expect("put fork");
store.prune_before_height(5).expect("prune");
assert!(
store.get_block(&fork.hash()).expect("f").is_some(),
"non-canonical block at height 7 should survive pruning to 5"
);
}
#[test]
fn test_canonical_blocks_unaffected_by_non_canonical_scan() {
let (_guard, path) = temp_blockstore_dir();
let store = BlockStore::open(test_config(path)).expect("open");
let chain = build_chain(10);
for block in &chain {
store.extend_chain(block).expect("extend");
}
store.prune_before_height(3).expect("prune");
for block in &chain[3..] {
assert!(
store.get_block(&block.hash()).expect("g").is_some(),
"canonical block at height {} should survive",
block.height()
);
}
}