2020-01-20 14:40:58 +03:00
|
|
|
// Copyright 2020 The Grin Developers
|
2018-05-30 23:57:13 +03:00
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
pub mod common;
|
|
|
|
|
2018-12-08 02:59:40 +03:00
|
|
|
use self::core::core::verifier_cache::LruVerifierCache;
|
2020-05-22 14:51:58 +03:00
|
|
|
use self::core::global;
|
2018-12-08 02:59:40 +03:00
|
|
|
use self::keychain::{ExtKeychain, Keychain};
|
2020-06-07 11:26:08 +03:00
|
|
|
use self::pool::types::PoolError;
|
2018-12-08 02:59:40 +03:00
|
|
|
use self::util::RwLock;
|
|
|
|
use crate::common::*;
|
|
|
|
use grin_core as core;
|
|
|
|
use grin_keychain as keychain;
|
|
|
|
use grin_pool as pool;
|
|
|
|
use grin_util as util;
|
2018-10-20 03:13:07 +03:00
|
|
|
use std::sync::Arc;
|
2018-05-30 23:57:13 +03:00
|
|
|
|
2020-06-07 11:26:08 +03:00
|
|
|
/// Test we correctly verify coinbase maturity when adding txs to the pool.
|
|
|
|
#[test]
|
|
|
|
fn test_coinbase_maturity() {
|
|
|
|
util::init_test_logger();
|
|
|
|
global::set_local_chain_type(global::ChainTypes::AutomatedTesting);
|
|
|
|
let keychain: ExtKeychain = Keychain::from_random_seed(false).unwrap();
|
2018-05-30 23:57:13 +03:00
|
|
|
|
2020-06-07 11:26:08 +03:00
|
|
|
let db_root = "target/.coinbase_maturity";
|
|
|
|
clean_output_dir(db_root.into());
|
2018-05-30 23:57:13 +03:00
|
|
|
|
2020-06-07 11:26:08 +03:00
|
|
|
let genesis = genesis_block(&keychain);
|
|
|
|
let chain = Arc::new(init_chain(db_root, genesis));
|
|
|
|
let verifier_cache = Arc::new(RwLock::new(LruVerifierCache::new()));
|
2018-08-20 16:48:05 +03:00
|
|
|
|
2020-06-07 11:26:08 +03:00
|
|
|
// Initialize a new pool with our chain adapter.
|
|
|
|
let mut pool = init_transaction_pool(
|
|
|
|
Arc::new(ChainAdapter {
|
|
|
|
chain: chain.clone(),
|
|
|
|
}),
|
|
|
|
verifier_cache,
|
|
|
|
);
|
2018-09-24 11:24:10 +03:00
|
|
|
|
2020-06-07 11:26:08 +03:00
|
|
|
// Add a single block, introducing coinbase output to be spent later.
|
|
|
|
add_block(&chain, vec![], &keychain);
|
2018-09-24 11:24:10 +03:00
|
|
|
|
2020-06-07 11:26:08 +03:00
|
|
|
let header_1 = chain.get_header_by_height(1).unwrap();
|
|
|
|
let tx = test_transaction_spending_coinbase(&keychain, &header_1, vec![100]);
|
2018-05-30 23:57:13 +03:00
|
|
|
|
2020-06-07 11:26:08 +03:00
|
|
|
// Coinbase is not yet matured and cannot be spent.
|
|
|
|
let header = chain.head_header().unwrap();
|
|
|
|
assert_eq!(
|
|
|
|
pool.add_to_pool(test_source(), tx.clone(), true, &header)
|
|
|
|
.err(),
|
|
|
|
Some(PoolError::ImmatureCoinbase)
|
|
|
|
);
|
2018-05-30 23:57:13 +03:00
|
|
|
|
2020-06-07 11:26:08 +03:00
|
|
|
// Add 2 more blocks. Original coinbase output is now matured and can be spent.
|
|
|
|
add_some_blocks(&chain, 2, &keychain);
|
|
|
|
let header = chain.head_header().unwrap();
|
|
|
|
assert_eq!(
|
|
|
|
pool.add_to_pool(test_source(), tx.clone(), true, &header),
|
2018-05-30 23:57:13 +03:00
|
|
|
Ok(())
|
2020-06-07 11:26:08 +03:00
|
|
|
);
|
2018-05-30 23:57:13 +03:00
|
|
|
|
2020-06-07 11:26:08 +03:00
|
|
|
clean_output_dir(db_root.into());
|
2018-05-30 23:57:13 +03:00
|
|
|
}
|