mirror of
https://github.com/mimblewimble/grin.git
synced 2025-01-21 03:21:08 +03:00
Cleanup db directory after tests (#2752)
* Cleanup db directory after tests * Fix clean output dir windows * Remove behind chain tests
This commit is contained in:
parent
e8c50359e4
commit
606b4652f8
9 changed files with 1041 additions and 973 deletions
|
@ -112,6 +112,8 @@ fn data_files() {
|
|||
let chain = reload_chain(chain_dir);
|
||||
chain.validate(false).unwrap();
|
||||
}
|
||||
// Cleanup chain directory
|
||||
clean_output_dir(chain_dir);
|
||||
}
|
||||
|
||||
fn _prepare_block(kc: &ExtKeychain, prev: &BlockHeader, chain: &Chain, diff: u64) -> Block {
|
||||
|
|
|
@ -59,7 +59,11 @@ fn setup(dir_name: &str, genesis: Block) -> Chain {
|
|||
fn mine_empty_chain() {
|
||||
global::set_mining_mode(ChainTypes::AutomatedTesting);
|
||||
let keychain = keychain::ExtKeychain::from_random_seed(false).unwrap();
|
||||
{
|
||||
mine_some_on_top(".grin", pow::mine_genesis_block().unwrap(), &keychain);
|
||||
}
|
||||
// Cleanup chain directory
|
||||
clean_output_dir(".grin");
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
@ -73,9 +77,10 @@ fn mine_genesis_reward_chain() {
|
|||
let reward = reward::output(&keychain, &key_id, 0).unwrap();
|
||||
genesis = genesis.with_reward(reward.0, reward.1);
|
||||
|
||||
let tmp_chain_dir = ".grin.tmp";
|
||||
{
|
||||
// setup a tmp chain to hande tx hashsets
|
||||
let tmp_chain = setup(".grin.tmp", pow::mine_genesis_block().unwrap());
|
||||
let tmp_chain = setup(tmp_chain_dir, pow::mine_genesis_block().unwrap());
|
||||
tmp_chain.set_txhashset_roots(&mut genesis).unwrap();
|
||||
genesis.header.output_mmr_size = 1;
|
||||
genesis.header.kernel_mmr_size = 1;
|
||||
|
@ -91,6 +96,9 @@ fn mine_genesis_reward_chain() {
|
|||
.unwrap();
|
||||
|
||||
mine_some_on_top(".grin.genesis", genesis, &keychain);
|
||||
// Cleanup chain directories
|
||||
clean_output_dir(tmp_chain_dir);
|
||||
clean_output_dir(".grin.genesis");
|
||||
}
|
||||
|
||||
fn mine_some_on_top<K>(dir: &str, genesis: Block, keychain: &K)
|
||||
|
@ -157,6 +165,7 @@ where
|
|||
#[test]
|
||||
fn mine_forks() {
|
||||
global::set_mining_mode(ChainTypes::AutomatedTesting);
|
||||
{
|
||||
let chain = setup(".grin2", pow::mine_genesis_block().unwrap());
|
||||
let kc = ExtKeychain::from_random_seed(false).unwrap();
|
||||
|
||||
|
@ -195,12 +204,16 @@ fn mine_forks() {
|
|||
assert_eq!(head.last_block_h, bhash);
|
||||
assert_eq!(head.prev_block_h, prev.hash());
|
||||
}
|
||||
}
|
||||
// Cleanup chain directory
|
||||
clean_output_dir(".grin2");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn mine_losing_fork() {
|
||||
global::set_mining_mode(ChainTypes::AutomatedTesting);
|
||||
let kc = ExtKeychain::from_random_seed(false).unwrap();
|
||||
{
|
||||
let chain = setup(".grin3", pow::mine_genesis_block().unwrap());
|
||||
|
||||
// add a first block we'll be forking from
|
||||
|
@ -227,6 +240,9 @@ fn mine_losing_fork() {
|
|||
let b3head = b3.header.clone();
|
||||
chain.process_block(b3, chain::Options::SKIP_POW).unwrap();
|
||||
assert_eq!(chain.head_header().unwrap().hash(), b3head.hash());
|
||||
}
|
||||
// Cleanup chain directory
|
||||
clean_output_dir(".grin3");
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
@ -237,6 +253,7 @@ fn longer_fork() {
|
|||
// prepare 2 chains, the 2nd will be have the forked blocks we can
|
||||
// then send back on the 1st
|
||||
let genesis = pow::mine_genesis_block().unwrap();
|
||||
{
|
||||
let chain = setup(".grin4", genesis.clone());
|
||||
|
||||
// add blocks to both chains, 20 on the main one, only the first 5
|
||||
|
@ -267,12 +284,16 @@ fn longer_fork() {
|
|||
let head = chain.head_header().unwrap();
|
||||
assert_eq!(head.height, 12);
|
||||
assert_eq!(head.hash(), new_head.hash());
|
||||
}
|
||||
// Cleanup chain directory
|
||||
clean_output_dir(".grin4");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn spend_in_fork_and_compact() {
|
||||
global::set_mining_mode(ChainTypes::AutomatedTesting);
|
||||
util::init_test_logger();
|
||||
{
|
||||
let chain = setup(".grin6", pow::mine_genesis_block().unwrap());
|
||||
let prev = chain.head_header().unwrap();
|
||||
let kc = ExtKeychain::from_random_seed(false).unwrap();
|
||||
|
@ -394,12 +415,16 @@ fn spend_in_fork_and_compact() {
|
|||
if let Err(e) = chain.validate(false) {
|
||||
panic!("Validation error after compacting chain: {:?}", e);
|
||||
}
|
||||
}
|
||||
// Cleanup chain directory
|
||||
clean_output_dir(".grin6");
|
||||
}
|
||||
|
||||
/// Test ability to retrieve block headers for a given output
|
||||
#[test]
|
||||
fn output_header_mappings() {
|
||||
global::set_mining_mode(ChainTypes::AutomatedTesting);
|
||||
{
|
||||
let chain = setup(
|
||||
".grin_header_for_output",
|
||||
pow::mine_genesis_block().unwrap(),
|
||||
|
@ -453,6 +478,9 @@ fn output_header_mappings() {
|
|||
.unwrap();
|
||||
assert_eq!(header_for_output.height, n as u64);
|
||||
}
|
||||
}
|
||||
// Cleanup chain directory
|
||||
clean_output_dir(".grin_header_for_output");
|
||||
}
|
||||
|
||||
fn prepare_block<K>(kc: &K, prev: &BlockHeader, chain: &Chain, diff: u64) -> Block
|
||||
|
|
|
@ -53,6 +53,8 @@ fn test_various_store_indices() {
|
|||
|
||||
let keychain = ExtKeychain::from_random_seed(false).unwrap();
|
||||
let key_id = ExtKeychainPath::new(1, 1, 0, 0, 0).to_identifier();
|
||||
|
||||
{
|
||||
let db_env = Arc::new(store::new_env(chain_dir.to_string()));
|
||||
|
||||
let chain_store = Arc::new(chain::store::ChainStore::new(db_env).unwrap());
|
||||
|
@ -97,4 +99,7 @@ fn test_various_store_indices() {
|
|||
// Check the batch did not commit any changes to the store .
|
||||
assert!(chain_store.get_block(&block_hash).is_ok());
|
||||
}
|
||||
}
|
||||
// Cleanup chain directory
|
||||
clean_output_dir(chain_dir);
|
||||
}
|
||||
|
|
|
@ -38,16 +38,18 @@ fn clean_output_dir(dir_name: &str) {
|
|||
#[test]
|
||||
fn test_coinbase_maturity() {
|
||||
let _ = env_logger::init();
|
||||
clean_output_dir(".grin");
|
||||
let chain_dir = ".grin_coinbase";
|
||||
clean_output_dir(chain_dir);
|
||||
global::set_mining_mode(ChainTypes::AutomatedTesting);
|
||||
|
||||
let genesis_block = pow::mine_genesis_block().unwrap();
|
||||
|
||||
let verifier_cache = Arc::new(RwLock::new(LruVerifierCache::new()));
|
||||
|
||||
let db_env = Arc::new(store::new_env(".grin".to_string()));
|
||||
{
|
||||
let db_env = Arc::new(store::new_env(chain_dir.to_string()));
|
||||
let chain = chain::Chain::init(
|
||||
".grin".to_string(),
|
||||
chain_dir.to_string(),
|
||||
db_env,
|
||||
Arc::new(NoopAdapter {}),
|
||||
genesis_block,
|
||||
|
@ -146,7 +148,8 @@ fn test_coinbase_maturity() {
|
|||
let pk = ExtKeychainPath::new(1, 1, 0, 0, 0).to_identifier();
|
||||
|
||||
let reward = libtx::reward::output(&keychain, &pk, 0).unwrap();
|
||||
let mut block = core::core::Block::new(&prev, vec![], Difficulty::min(), reward).unwrap();
|
||||
let mut block =
|
||||
core::core::Block::new(&prev, vec![], Difficulty::min(), reward).unwrap();
|
||||
let next_header_info = consensus::next_difficulty(1, chain.difficulty_iter().unwrap());
|
||||
block.header.timestamp = prev.timestamp + Duration::seconds(60);
|
||||
block.header.pow.secondary_scaling = next_header_info.secondary_scaling;
|
||||
|
@ -194,4 +197,7 @@ fn test_coinbase_maturity() {
|
|||
Ok(_) => (),
|
||||
Err(_) => panic!("we did not expect an error here"),
|
||||
};
|
||||
}
|
||||
// Cleanup chain directory
|
||||
clean_output_dir(chain_dir);
|
||||
}
|
||||
|
|
|
@ -41,6 +41,7 @@ fn test_unexpected_zip() {
|
|||
|
||||
let db_root = format!(".grin_txhashset_zip");
|
||||
clean_output_dir(&db_root);
|
||||
{
|
||||
let db_env = Arc::new(store::new_env(db_root.clone()));
|
||||
let chain_store = ChainStore::new(db_env).unwrap();
|
||||
let store = Arc::new(chain_store);
|
||||
|
@ -82,6 +83,9 @@ fn test_unexpected_zip() {
|
|||
txhashset_path.clone()
|
||||
));
|
||||
fs::remove_dir_all(Path::new(&db_root).join("txhashset")).unwrap();
|
||||
}
|
||||
// Cleanup chain directory
|
||||
clean_output_dir(&db_root);
|
||||
}
|
||||
|
||||
fn write_file(db_root: String) {
|
||||
|
|
|
@ -34,13 +34,15 @@ fn test_transaction_pool_block_building() {
|
|||
|
||||
let db_root = ".grin_block_building".to_string();
|
||||
clean_output_dir(db_root.clone());
|
||||
{
|
||||
let mut chain = ChainAdapter::init(db_root.clone()).unwrap();
|
||||
|
||||
let verifier_cache = Arc::new(RwLock::new(LruVerifierCache::new()));
|
||||
|
||||
// Initialize the chain/txhashset with an initial block
|
||||
// so we have a non-empty UTXO set.
|
||||
let add_block = |prev_header: BlockHeader, txs: Vec<Transaction>, chain: &mut ChainAdapter| {
|
||||
let add_block =
|
||||
|prev_header: BlockHeader, txs: Vec<Transaction>, chain: &mut ChainAdapter| {
|
||||
let height = prev_header.height + 1;
|
||||
let key_id = ExtKeychain::derive_key_id(1, height as u32, 0, 0, 0);
|
||||
let fee = txs.iter().map(|x| x.fee()).sum();
|
||||
|
@ -59,7 +61,8 @@ fn test_transaction_pool_block_building() {
|
|||
|
||||
// Now create tx to spend that first coinbase (now matured).
|
||||
// Provides us with some useful outputs to test with.
|
||||
let initial_tx = test_transaction_spending_coinbase(&keychain, &header, vec![10, 20, 30, 40]);
|
||||
let initial_tx =
|
||||
test_transaction_spending_coinbase(&keychain, &header, vec![10, 20, 30, 40]);
|
||||
|
||||
// Mine that initial tx so we can spend it with multiple txs
|
||||
let block = add_block(header, vec![initial_tx], &mut chain);
|
||||
|
@ -117,4 +120,7 @@ fn test_transaction_pool_block_building() {
|
|||
|
||||
assert_eq!(write_pool.total_size(), 0);
|
||||
}
|
||||
}
|
||||
// Cleanup db directory
|
||||
clean_output_dir(db_root.clone());
|
||||
}
|
||||
|
|
|
@ -40,12 +40,14 @@ fn test_block_building_max_weight() {
|
|||
let db_root = ".grin_block_building_max_weight".to_string();
|
||||
clean_output_dir(db_root.clone());
|
||||
|
||||
{
|
||||
let mut chain = ChainAdapter::init(db_root.clone()).unwrap();
|
||||
|
||||
let verifier_cache = Arc::new(RwLock::new(LruVerifierCache::new()));
|
||||
|
||||
// Convenient was to add a new block to the chain.
|
||||
let add_block = |prev_header: BlockHeader, txs: Vec<Transaction>, chain: &mut ChainAdapter| {
|
||||
let add_block =
|
||||
|prev_header: BlockHeader, txs: Vec<Transaction>, chain: &mut ChainAdapter| {
|
||||
let height = prev_header.height + 1;
|
||||
let key_id = ExtKeychain::derive_key_id(1, height as u32, 0, 0, 0);
|
||||
let fee = txs.iter().map(|x| x.fee()).sum();
|
||||
|
@ -66,7 +68,8 @@ fn test_block_building_max_weight() {
|
|||
|
||||
// Now create tx to spend that first coinbase (now matured).
|
||||
// Provides us with some useful outputs to test with.
|
||||
let initial_tx = test_transaction_spending_coinbase(&keychain, &header, vec![100, 200, 300]);
|
||||
let initial_tx =
|
||||
test_transaction_spending_coinbase(&keychain, &header, vec![100, 200, 300]);
|
||||
|
||||
// Mine that initial tx so we can spend it with multiple txs
|
||||
let block = add_block(header, vec![initial_tx], &mut chain);
|
||||
|
@ -140,4 +143,7 @@ fn test_block_building_max_weight() {
|
|||
// remained in the txpool.
|
||||
assert_eq!(write_pool.total_size(), 2);
|
||||
}
|
||||
}
|
||||
// Cleanup db directory
|
||||
clean_output_dir(db_root.clone());
|
||||
}
|
||||
|
|
|
@ -34,6 +34,7 @@ fn test_transaction_pool_block_reconciliation() {
|
|||
|
||||
let db_root = ".grin_block_reconciliation".to_string();
|
||||
clean_output_dir(db_root.clone());
|
||||
{
|
||||
let chain = Arc::new(ChainAdapter::init(db_root.clone()).unwrap());
|
||||
|
||||
let verifier_cache = Arc::new(RwLock::new(LruVerifierCache::new()));
|
||||
|
@ -58,13 +59,15 @@ fn test_transaction_pool_block_reconciliation() {
|
|||
|
||||
// Now create tx to spend that first coinbase (now matured).
|
||||
// Provides us with some useful outputs to test with.
|
||||
let initial_tx = test_transaction_spending_coinbase(&keychain, &header, vec![10, 20, 30, 40]);
|
||||
let initial_tx =
|
||||
test_transaction_spending_coinbase(&keychain, &header, vec![10, 20, 30, 40]);
|
||||
|
||||
let block = {
|
||||
let key_id = ExtKeychain::derive_key_id(1, 2, 0, 0, 0);
|
||||
let fees = initial_tx.fee();
|
||||
let reward = libtx::reward::output(&keychain, &key_id, fees).unwrap();
|
||||
let mut block = Block::new(&header, vec![initial_tx], Difficulty::min(), reward).unwrap();
|
||||
let mut block =
|
||||
Block::new(&header, vec![initial_tx], Difficulty::min(), reward).unwrap();
|
||||
|
||||
// Set the prev_root to the prev hash for testing purposes (no MMR to obtain a root from).
|
||||
block.header.prev_root = header.hash();
|
||||
|
@ -183,4 +186,7 @@ fn test_transaction_pool_block_reconciliation() {
|
|||
assert_eq!(write_pool.txpool.entries[2].tx, conflict_valid_child);
|
||||
assert_eq!(write_pool.txpool.entries[3].tx, valid_child_valid);
|
||||
}
|
||||
}
|
||||
// Cleanup db directory
|
||||
clean_output_dir(db_root.clone());
|
||||
}
|
||||
|
|
|
@ -33,6 +33,7 @@ fn test_the_transaction_pool() {
|
|||
|
||||
let db_root = ".grin_transaction_pool".to_string();
|
||||
clean_output_dir(db_root.clone());
|
||||
{
|
||||
let chain = Arc::new(ChainAdapter::init(db_root.clone()).unwrap());
|
||||
|
||||
let verifier_cache = Arc::new(RwLock::new(LruVerifierCache::new()));
|
||||
|
@ -44,7 +45,8 @@ fn test_the_transaction_pool() {
|
|||
let height = 1;
|
||||
let key_id = ExtKeychain::derive_key_id(1, height as u32, 0, 0, 0);
|
||||
let reward = libtx::reward::output(&keychain, &key_id, 0).unwrap();
|
||||
let block = Block::new(&BlockHeader::default(), vec![], Difficulty::min(), reward).unwrap();
|
||||
let block =
|
||||
Block::new(&BlockHeader::default(), vec![], Difficulty::min(), reward).unwrap();
|
||||
|
||||
chain.update_db_for_block(&block);
|
||||
|
||||
|
@ -250,4 +252,7 @@ fn test_the_transaction_pool() {
|
|||
.add_to_pool(test_source(), double_spend_tx.clone(), false, &header)
|
||||
.is_err());
|
||||
}
|
||||
}
|
||||
// Cleanup db directory
|
||||
clean_output_dir(db_root.clone());
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue