2018-03-05 22:33:44 +03:00
|
|
|
// Copyright 2018 The Grin Developers
|
2017-09-12 20:24:24 +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.
|
|
|
|
|
2018-08-16 00:14:48 +03:00
|
|
|
extern crate chrono;
|
2017-11-01 02:32:33 +03:00
|
|
|
extern crate env_logger;
|
2017-09-12 20:24:24 +03:00
|
|
|
extern crate grin_chain as chain;
|
2017-11-01 02:32:33 +03:00
|
|
|
extern crate grin_core as core;
|
2017-10-03 03:02:31 +03:00
|
|
|
extern crate grin_keychain as keychain;
|
2018-06-22 11:08:06 +03:00
|
|
|
extern crate grin_store as store;
|
2018-05-09 12:15:58 +03:00
|
|
|
extern crate grin_wallet as wallet;
|
2017-09-12 20:24:24 +03:00
|
|
|
extern crate rand;
|
|
|
|
|
2018-08-16 00:14:48 +03:00
|
|
|
use chrono::Duration;
|
2017-09-12 20:24:24 +03:00
|
|
|
use std::fs;
|
2018-08-30 17:44:34 +03:00
|
|
|
use std::sync::{Arc, RwLock};
|
2017-09-12 20:24:24 +03:00
|
|
|
|
2018-07-01 01:36:38 +03:00
|
|
|
use chain::types::NoopAdapter;
|
2018-08-20 22:34:12 +03:00
|
|
|
use chain::ErrorKind;
|
2018-08-22 22:19:37 +03:00
|
|
|
use core::core::transaction;
|
2018-08-30 17:44:34 +03:00
|
|
|
use core::core::verifier_cache::LruVerifierCache;
|
2018-06-14 15:16:14 +03:00
|
|
|
use core::global::{self, ChainTypes};
|
2018-09-19 01:12:57 +03:00
|
|
|
use core::pow::Difficulty;
|
2018-06-14 15:16:14 +03:00
|
|
|
use core::{consensus, pow};
|
2018-10-10 12:11:01 +03:00
|
|
|
use keychain::{ExtKeychain, ExtKeychainPath, Keychain};
|
2018-06-14 15:16:14 +03:00
|
|
|
use wallet::libtx::{self, build};
|
2017-09-12 20:24:24 +03:00
|
|
|
|
2017-09-29 21:44:25 +03:00
|
|
|
fn clean_output_dir(dir_name: &str) {
|
|
|
|
let _ = fs::remove_dir_all(dir_name);
|
2017-09-12 20:24:24 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_coinbase_maturity() {
|
2017-09-29 21:44:25 +03:00
|
|
|
let _ = env_logger::init();
|
2017-09-12 20:24:24 +03:00
|
|
|
clean_output_dir(".grin");
|
2017-11-16 00:49:15 +03:00
|
|
|
global::set_mining_mode(ChainTypes::AutomatedTesting);
|
2017-09-12 20:24:24 +03:00
|
|
|
|
2018-04-24 11:18:24 +03:00
|
|
|
let genesis_block = pow::mine_genesis_block().unwrap();
|
2017-11-20 20:35:52 +03:00
|
|
|
|
2018-08-30 17:44:34 +03:00
|
|
|
let verifier_cache = Arc::new(RwLock::new(LruVerifierCache::new()));
|
|
|
|
|
2018-06-22 11:08:06 +03:00
|
|
|
let db_env = Arc::new(store::new_env(".grin".to_string()));
|
2017-09-29 21:44:25 +03:00
|
|
|
let chain = chain::Chain::init(
|
|
|
|
".grin".to_string(),
|
2018-06-22 11:08:06 +03:00
|
|
|
db_env,
|
2017-09-29 21:44:25 +03:00
|
|
|
Arc::new(NoopAdapter {}),
|
|
|
|
genesis_block,
|
|
|
|
pow::verify_size,
|
2018-08-30 17:44:34 +03:00
|
|
|
verifier_cache,
|
2018-09-10 17:59:42 +03:00
|
|
|
false,
|
2017-09-29 21:44:25 +03:00
|
|
|
).unwrap();
|
2017-09-12 20:24:24 +03:00
|
|
|
|
|
|
|
let prev = chain.head_header().unwrap();
|
2017-10-03 03:02:31 +03:00
|
|
|
|
2018-06-08 08:21:54 +03:00
|
|
|
let keychain = ExtKeychain::from_random_seed().unwrap();
|
2018-10-10 12:11:01 +03:00
|
|
|
let key_id1 = ExtKeychainPath::new(1, 1, 0, 0, 0).to_identifier();
|
|
|
|
let key_id2 = ExtKeychainPath::new(1, 2, 0, 0, 0).to_identifier();
|
|
|
|
let key_id3 = ExtKeychainPath::new(1, 3, 0, 0, 0).to_identifier();
|
|
|
|
let key_id4 = ExtKeychainPath::new(1, 4, 0, 0, 0).to_identifier();
|
2017-10-03 03:02:31 +03:00
|
|
|
|
2018-10-16 01:24:13 +03:00
|
|
|
let next_header_info = consensus::next_difficulty(1, chain.difficulty_iter());
|
2018-05-30 19:48:32 +03:00
|
|
|
let reward = libtx::reward::output(&keychain, &key_id1, 0, prev.height).unwrap();
|
2018-10-18 21:37:33 +03:00
|
|
|
let mut block = core::core::Block::new(&prev, vec![], Difficulty::min(), reward).unwrap();
|
2018-07-30 11:33:28 +03:00
|
|
|
block.header.timestamp = prev.timestamp + Duration::seconds(60);
|
2018-10-16 01:24:13 +03:00
|
|
|
block.header.pow.scaling_difficulty = next_header_info.secondary_scaling;
|
2018-03-15 22:16:34 +03:00
|
|
|
|
2018-06-22 11:33:42 +03:00
|
|
|
chain.set_txhashset_roots(&mut block, false).unwrap();
|
2017-09-12 20:24:24 +03:00
|
|
|
|
|
|
|
pow::pow_size(
|
|
|
|
&mut block.header,
|
2018-10-13 23:57:01 +03:00
|
|
|
next_header_info.difficulty,
|
2018-04-24 11:18:24 +03:00
|
|
|
global::proofsize(),
|
2018-10-16 02:14:23 +03:00
|
|
|
global::min_edge_bits(),
|
2017-09-12 20:24:24 +03:00
|
|
|
).unwrap();
|
|
|
|
|
2018-08-16 00:14:48 +03:00
|
|
|
assert_eq!(block.outputs().len(), 1);
|
|
|
|
let coinbase_output = block.outputs()[0];
|
2017-11-01 02:32:33 +03:00
|
|
|
assert!(
|
2018-03-02 23:47:27 +03:00
|
|
|
coinbase_output
|
2017-11-01 02:32:33 +03:00
|
|
|
.features
|
2018-02-05 22:43:54 +03:00
|
|
|
.contains(transaction::OutputFeatures::COINBASE_OUTPUT)
|
2017-11-01 02:32:33 +03:00
|
|
|
);
|
2017-09-12 20:24:24 +03:00
|
|
|
|
2018-03-04 03:19:54 +03:00
|
|
|
chain
|
|
|
|
.process_block(block.clone(), chain::Options::MINE)
|
|
|
|
.unwrap();
|
2018-03-02 23:47:27 +03:00
|
|
|
|
2017-09-29 21:44:25 +03:00
|
|
|
let prev = chain.head_header().unwrap();
|
2017-09-12 20:24:24 +03:00
|
|
|
|
2017-09-29 21:44:25 +03:00
|
|
|
let amount = consensus::REWARD;
|
2018-01-17 06:03:40 +03:00
|
|
|
|
2018-10-17 02:14:22 +03:00
|
|
|
let lock_height = 1 + global::coinbase_maturity();
|
2018-01-17 06:03:40 +03:00
|
|
|
assert_eq!(lock_height, 4);
|
|
|
|
|
|
|
|
// here we build a tx that attempts to spend the earlier coinbase output
|
|
|
|
// this is not a valid tx as the coinbase output cannot be spent yet
|
2018-02-13 18:35:30 +03:00
|
|
|
let coinbase_txn = build::transaction(
|
2017-10-03 03:02:31 +03:00
|
|
|
vec![
|
2018-06-29 04:56:07 +03:00
|
|
|
build::coinbase_input(amount, key_id1.clone()),
|
2018-01-17 06:03:40 +03:00
|
|
|
build::output(amount - 2, key_id2.clone()),
|
2017-10-05 12:56:39 +03:00
|
|
|
build::with_fee(2),
|
2017-10-03 03:02:31 +03:00
|
|
|
],
|
|
|
|
&keychain,
|
|
|
|
).unwrap();
|
2017-09-12 20:24:24 +03:00
|
|
|
|
2018-05-30 23:57:13 +03:00
|
|
|
let txs = vec![coinbase_txn.clone()];
|
2018-05-09 12:15:58 +03:00
|
|
|
let fees = txs.iter().map(|tx| tx.fee()).sum();
|
2018-05-30 19:48:32 +03:00
|
|
|
let reward = libtx::reward::output(&keychain, &key_id3, fees, prev.height).unwrap();
|
2018-10-18 21:37:33 +03:00
|
|
|
let mut block = core::core::Block::new(&prev, txs, Difficulty::min(), reward).unwrap();
|
2018-10-13 23:57:01 +03:00
|
|
|
let next_header_info = consensus::next_difficulty(1, chain.difficulty_iter());
|
2018-10-16 01:24:13 +03:00
|
|
|
block.header.timestamp = prev.timestamp + Duration::seconds(60);
|
|
|
|
block.header.pow.scaling_difficulty = next_header_info.secondary_scaling;
|
2018-01-17 06:03:40 +03:00
|
|
|
|
2018-06-22 11:33:42 +03:00
|
|
|
chain.set_txhashset_roots(&mut block, false).unwrap();
|
2018-05-30 23:57:13 +03:00
|
|
|
|
|
|
|
// Confirm the tx attempting to spend the coinbase output
|
|
|
|
// is not valid at the current block height given the current chain state.
|
|
|
|
match chain.verify_coinbase_maturity(&coinbase_txn) {
|
2018-07-01 01:36:38 +03:00
|
|
|
Ok(_) => {}
|
|
|
|
Err(e) => match e.kind() {
|
|
|
|
ErrorKind::ImmatureCoinbase => {}
|
|
|
|
_ => panic!("Expected transaction error with immature coinbase."),
|
|
|
|
},
|
2018-01-17 06:03:40 +03:00
|
|
|
}
|
2017-09-12 20:24:24 +03:00
|
|
|
|
|
|
|
pow::pow_size(
|
|
|
|
&mut block.header,
|
2018-10-13 23:57:01 +03:00
|
|
|
next_header_info.difficulty,
|
2018-04-24 11:18:24 +03:00
|
|
|
global::proofsize(),
|
2018-10-16 02:14:23 +03:00
|
|
|
global::min_edge_bits(),
|
2017-09-12 20:24:24 +03:00
|
|
|
).unwrap();
|
|
|
|
|
2017-10-03 03:02:31 +03:00
|
|
|
// mine enough blocks to increase the height sufficiently for
|
2018-01-17 06:03:40 +03:00
|
|
|
// coinbase to reach maturity and be spendable in the next block
|
2017-10-03 03:02:31 +03:00
|
|
|
for _ in 0..3 {
|
2017-09-29 21:44:25 +03:00
|
|
|
let prev = chain.head_header().unwrap();
|
2017-09-12 20:24:24 +03:00
|
|
|
|
2018-06-08 08:21:54 +03:00
|
|
|
let keychain = ExtKeychain::from_random_seed().unwrap();
|
2018-10-10 12:11:01 +03:00
|
|
|
let pk = ExtKeychainPath::new(1, 1, 0, 0, 0).to_identifier();
|
2017-10-03 03:02:31 +03:00
|
|
|
|
2018-05-30 19:48:32 +03:00
|
|
|
let reward = libtx::reward::output(&keychain, &pk, 0, prev.height).unwrap();
|
2018-10-18 21:37:33 +03:00
|
|
|
let mut block = core::core::Block::new(&prev, vec![], Difficulty::min(), reward).unwrap();
|
2018-10-13 23:57:01 +03:00
|
|
|
let next_header_info = consensus::next_difficulty(1, chain.difficulty_iter());
|
2018-10-16 01:24:13 +03:00
|
|
|
block.header.timestamp = prev.timestamp + Duration::seconds(60);
|
|
|
|
block.header.pow.scaling_difficulty = next_header_info.secondary_scaling;
|
2018-03-15 22:16:34 +03:00
|
|
|
|
2018-06-22 11:33:42 +03:00
|
|
|
chain.set_txhashset_roots(&mut block, false).unwrap();
|
2017-09-12 20:24:24 +03:00
|
|
|
|
2017-09-29 21:44:25 +03:00
|
|
|
pow::pow_size(
|
|
|
|
&mut block.header,
|
2018-10-13 23:57:01 +03:00
|
|
|
next_header_info.difficulty,
|
2018-04-24 11:18:24 +03:00
|
|
|
global::proofsize(),
|
2018-10-16 02:14:23 +03:00
|
|
|
global::min_edge_bits(),
|
2017-09-29 21:44:25 +03:00
|
|
|
).unwrap();
|
2017-09-12 20:24:24 +03:00
|
|
|
|
2018-02-05 22:43:54 +03:00
|
|
|
chain.process_block(block, chain::Options::MINE).unwrap();
|
2017-09-29 21:44:25 +03:00
|
|
|
}
|
2017-09-12 20:24:24 +03:00
|
|
|
|
2017-09-29 21:44:25 +03:00
|
|
|
let prev = chain.head_header().unwrap();
|
2017-09-12 20:24:24 +03:00
|
|
|
|
2018-05-30 23:57:13 +03:00
|
|
|
// Confirm the tx spending the coinbase output is now valid.
|
|
|
|
// The coinbase output has matured sufficiently based on current chain state.
|
|
|
|
chain.verify_coinbase_maturity(&coinbase_txn).unwrap();
|
2018-01-17 06:03:40 +03:00
|
|
|
|
2018-05-30 23:57:13 +03:00
|
|
|
let txs = vec![coinbase_txn];
|
2018-05-09 12:15:58 +03:00
|
|
|
let fees = txs.iter().map(|tx| tx.fee()).sum();
|
2018-10-16 01:24:13 +03:00
|
|
|
let next_header_info = consensus::next_difficulty(1, chain.difficulty_iter());
|
2018-05-30 19:48:32 +03:00
|
|
|
let reward = libtx::reward::output(&keychain, &key_id4, fees, prev.height).unwrap();
|
2018-10-18 21:37:33 +03:00
|
|
|
let mut block = core::core::Block::new(&prev, txs, Difficulty::min(), reward).unwrap();
|
2017-09-12 20:24:24 +03:00
|
|
|
|
2018-07-30 11:33:28 +03:00
|
|
|
block.header.timestamp = prev.timestamp + Duration::seconds(60);
|
2018-10-16 01:24:13 +03:00
|
|
|
block.header.pow.scaling_difficulty = next_header_info.secondary_scaling;
|
2018-03-15 22:16:34 +03:00
|
|
|
|
2018-06-22 11:33:42 +03:00
|
|
|
chain.set_txhashset_roots(&mut block, false).unwrap();
|
2017-09-12 20:24:24 +03:00
|
|
|
|
2017-09-29 21:44:25 +03:00
|
|
|
pow::pow_size(
|
|
|
|
&mut block.header,
|
2018-10-13 23:57:01 +03:00
|
|
|
next_header_info.difficulty,
|
2018-04-24 11:18:24 +03:00
|
|
|
global::proofsize(),
|
2018-10-16 02:14:23 +03:00
|
|
|
global::min_edge_bits(),
|
2017-09-29 21:44:25 +03:00
|
|
|
).unwrap();
|
|
|
|
|
2018-02-05 22:43:54 +03:00
|
|
|
let result = chain.process_block(block, chain::Options::MINE);
|
2017-09-29 21:44:25 +03:00
|
|
|
match result {
|
|
|
|
Ok(_) => (),
|
|
|
|
Err(_) => panic!("we did not expect an error here"),
|
|
|
|
};
|
2017-09-12 20:24:24 +03:00
|
|
|
}
|