2017-09-12 20:24:24 +03:00
|
|
|
// Copyright 2017 The Grin Developers
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
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;
|
|
|
|
extern crate grin_pow as pow;
|
2017-09-12 20:24:24 +03:00
|
|
|
extern crate rand;
|
2017-11-01 02:32:33 +03:00
|
|
|
extern crate time;
|
2017-09-12 20:24:24 +03:00
|
|
|
|
|
|
|
use std::fs;
|
|
|
|
use std::sync::Arc;
|
|
|
|
|
|
|
|
use chain::types::*;
|
|
|
|
use core::core::build;
|
2018-01-12 21:35:37 +03:00
|
|
|
use core::core::target::Difficulty;
|
2017-09-12 20:24:24 +03:00
|
|
|
use core::core::transaction;
|
|
|
|
use core::consensus;
|
|
|
|
use core::global;
|
2017-11-16 00:49:15 +03:00
|
|
|
use core::global::ChainTypes;
|
2017-09-12 20:24:24 +03:00
|
|
|
|
2017-10-03 03:02:31 +03:00
|
|
|
use keychain::Keychain;
|
|
|
|
|
2017-11-01 02:32:33 +03:00
|
|
|
use pow::{cuckoo, types, MiningWorker};
|
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
|
|
|
|
2017-11-20 20:35:52 +03:00
|
|
|
let genesis_block = pow::mine_genesis_block(None).unwrap();
|
|
|
|
|
2017-09-29 21:44:25 +03:00
|
|
|
let chain = chain::Chain::init(
|
|
|
|
".grin".to_string(),
|
|
|
|
Arc::new(NoopAdapter {}),
|
|
|
|
genesis_block,
|
|
|
|
pow::verify_size,
|
|
|
|
).unwrap();
|
2017-09-12 20:24:24 +03:00
|
|
|
|
|
|
|
let mut miner_config = types::MinerConfig {
|
|
|
|
enable_mining: true,
|
|
|
|
burn_reward: true,
|
|
|
|
..Default::default()
|
|
|
|
};
|
|
|
|
miner_config.cuckoo_miner_plugin_dir = Some(String::from("../target/debug/deps"));
|
|
|
|
|
2017-09-29 21:44:25 +03:00
|
|
|
let mut cuckoo_miner = cuckoo::Miner::new(
|
|
|
|
consensus::EASINESS,
|
|
|
|
global::sizeshift() as u32,
|
|
|
|
global::proofsize(),
|
|
|
|
);
|
2017-09-12 20:24:24 +03:00
|
|
|
|
|
|
|
let prev = chain.head_header().unwrap();
|
2017-10-03 03:02:31 +03:00
|
|
|
|
|
|
|
let keychain = Keychain::from_random_seed().unwrap();
|
2017-10-13 07:45:07 +03:00
|
|
|
let key_id1 = keychain.derive_key_id(1).unwrap();
|
|
|
|
let key_id2 = keychain.derive_key_id(2).unwrap();
|
|
|
|
let key_id3 = keychain.derive_key_id(3).unwrap();
|
|
|
|
let key_id4 = keychain.derive_key_id(4).unwrap();
|
2017-10-03 03:02:31 +03:00
|
|
|
|
2018-01-12 21:35:37 +03:00
|
|
|
let mut block = core::core::Block::new(
|
|
|
|
&prev,
|
|
|
|
vec![],
|
|
|
|
&keychain,
|
|
|
|
&key_id1,
|
2018-01-27 10:48:53 +03:00
|
|
|
Difficulty::one()
|
2018-01-12 21:35:37 +03:00
|
|
|
).unwrap();
|
2017-09-12 20:24:24 +03:00
|
|
|
block.header.timestamp = prev.timestamp + time::Duration::seconds(60);
|
|
|
|
|
|
|
|
let difficulty = consensus::next_difficulty(chain.difficulty_iter()).unwrap();
|
|
|
|
block.header.difficulty = difficulty.clone();
|
2018-01-08 04:23:23 +03:00
|
|
|
chain.set_sumtree_roots(&mut block, false).unwrap();
|
2017-09-12 20:24:24 +03:00
|
|
|
|
|
|
|
pow::pow_size(
|
|
|
|
&mut cuckoo_miner,
|
|
|
|
&mut block.header,
|
|
|
|
difficulty,
|
|
|
|
global::sizeshift() as u32,
|
|
|
|
).unwrap();
|
|
|
|
|
2017-09-29 21:44:25 +03:00
|
|
|
assert_eq!(block.outputs.len(), 1);
|
2017-11-01 02:32:33 +03:00
|
|
|
assert!(
|
|
|
|
block.outputs[0]
|
|
|
|
.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-01-17 06:03:40 +03:00
|
|
|
// we will need this later when we want to spend the coinbase output
|
|
|
|
let block_hash = block.hash();
|
|
|
|
|
2018-02-05 22:43:54 +03:00
|
|
|
chain.process_block(block, chain::Options::MINE).unwrap();
|
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
|
|
|
|
2017-09-29 21:44:25 +03:00
|
|
|
let amount = consensus::REWARD;
|
2018-01-17 06:03:40 +03:00
|
|
|
|
|
|
|
let lock_height = 1 + global::coinbase_maturity();
|
|
|
|
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
|
2017-10-03 03:02:31 +03:00
|
|
|
let (coinbase_txn, _) = build::transaction(
|
|
|
|
vec![
|
2018-01-17 06:03:40 +03:00
|
|
|
build::coinbase_input(amount, block_hash, key_id1.clone()),
|
|
|
|
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
|
|
|
|
2017-11-01 02:32:33 +03:00
|
|
|
let mut block =
|
2018-01-12 21:35:37 +03:00
|
|
|
core::core::Block::new(
|
|
|
|
&prev,
|
|
|
|
vec![&coinbase_txn],
|
|
|
|
&keychain,
|
|
|
|
&key_id3,
|
2018-01-27 10:48:53 +03:00
|
|
|
Difficulty::one(),
|
2018-01-12 21:35:37 +03:00
|
|
|
).unwrap();
|
2017-09-12 20:24:24 +03:00
|
|
|
block.header.timestamp = prev.timestamp + time::Duration::seconds(60);
|
|
|
|
|
|
|
|
let difficulty = consensus::next_difficulty(chain.difficulty_iter()).unwrap();
|
|
|
|
block.header.difficulty = difficulty.clone();
|
2018-01-17 06:03:40 +03:00
|
|
|
|
|
|
|
match chain.set_sumtree_roots(&mut block, false) {
|
|
|
|
Err(Error::ImmatureCoinbase) => (),
|
|
|
|
_ => panic!("expected ImmatureCoinbase error here"),
|
|
|
|
}
|
2017-09-12 20:24:24 +03:00
|
|
|
|
|
|
|
pow::pow_size(
|
|
|
|
&mut cuckoo_miner,
|
|
|
|
&mut block.header,
|
|
|
|
difficulty,
|
|
|
|
global::sizeshift() as u32,
|
|
|
|
).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
|
|
|
|
2017-10-03 03:02:31 +03:00
|
|
|
let keychain = Keychain::from_random_seed().unwrap();
|
2017-10-13 07:45:07 +03:00
|
|
|
let pk = keychain.derive_key_id(1).unwrap();
|
2017-10-03 03:02:31 +03:00
|
|
|
|
2018-01-12 21:35:37 +03:00
|
|
|
let mut block = core::core::Block::new(
|
|
|
|
&prev,
|
|
|
|
vec![],
|
|
|
|
&keychain,
|
|
|
|
&pk,
|
2018-01-27 10:48:53 +03:00
|
|
|
Difficulty::one()
|
2018-01-12 21:35:37 +03:00
|
|
|
).unwrap();
|
2017-09-29 21:44:25 +03:00
|
|
|
block.header.timestamp = prev.timestamp + time::Duration::seconds(60);
|
2017-09-12 20:24:24 +03:00
|
|
|
|
2017-09-29 21:44:25 +03:00
|
|
|
let difficulty = consensus::next_difficulty(chain.difficulty_iter()).unwrap();
|
|
|
|
block.header.difficulty = difficulty.clone();
|
2018-01-08 04:23:23 +03:00
|
|
|
chain.set_sumtree_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 cuckoo_miner,
|
|
|
|
&mut block.header,
|
|
|
|
difficulty,
|
|
|
|
global::sizeshift() as u32,
|
|
|
|
).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-01-17 06:03:40 +03:00
|
|
|
let (coinbase_txn, _) = build::transaction(
|
|
|
|
vec![
|
|
|
|
build::coinbase_input(amount, block_hash, key_id1.clone()),
|
|
|
|
build::output(amount - 2, key_id2.clone()),
|
|
|
|
build::with_fee(2),
|
|
|
|
],
|
|
|
|
&keychain,
|
|
|
|
).unwrap();
|
|
|
|
|
|
|
|
let mut block = core::core::Block::new(
|
|
|
|
&prev,
|
|
|
|
vec![&coinbase_txn],
|
|
|
|
&keychain,
|
|
|
|
&key_id4,
|
2018-01-27 10:48:53 +03:00
|
|
|
Difficulty::one(),
|
2018-01-17 06:03:40 +03:00
|
|
|
).unwrap();
|
2017-09-12 20:24:24 +03:00
|
|
|
|
2017-09-29 21:44:25 +03:00
|
|
|
block.header.timestamp = prev.timestamp + time::Duration::seconds(60);
|
2017-09-12 20:24:24 +03:00
|
|
|
|
2017-09-29 21:44:25 +03:00
|
|
|
let difficulty = consensus::next_difficulty(chain.difficulty_iter()).unwrap();
|
|
|
|
block.header.difficulty = difficulty.clone();
|
2018-01-08 04:23:23 +03:00
|
|
|
chain.set_sumtree_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 cuckoo_miner,
|
|
|
|
&mut block.header,
|
|
|
|
difficulty,
|
|
|
|
global::sizeshift() as u32,
|
|
|
|
).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(Error::ImmatureCoinbase) => panic!("we should not get an ImmatureCoinbase here"),
|
|
|
|
Err(_) => panic!("we did not expect an error here"),
|
|
|
|
};
|
2017-09-12 20:24:24 +03:00
|
|
|
}
|