2020-01-20 14:40:58 +03:00
|
|
|
// Copyright 2020 The Grin Developers
|
2019-07-23 11:46:29 +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.
|
|
|
|
|
|
|
|
use self::chain::types::NoopAdapter;
|
|
|
|
use self::chain::types::Options;
|
|
|
|
use self::chain::Chain;
|
2019-07-26 10:36:24 +03:00
|
|
|
use self::core::core::hash::Hashed;
|
2019-07-23 11:46:29 +03:00
|
|
|
use self::core::core::verifier_cache::LruVerifierCache;
|
|
|
|
use self::core::core::Block;
|
|
|
|
use self::core::genesis;
|
|
|
|
use self::core::global::ChainTypes;
|
|
|
|
use self::core::libtx::{self, reward};
|
|
|
|
use self::core::{consensus, global, pow};
|
|
|
|
use self::keychain::{ExtKeychainPath, Keychain};
|
|
|
|
use self::util::RwLock;
|
|
|
|
use chrono::Duration;
|
|
|
|
use grin_chain as chain;
|
|
|
|
use grin_core as core;
|
|
|
|
use grin_keychain as keychain;
|
|
|
|
use grin_util as util;
|
|
|
|
use std::fs;
|
|
|
|
use std::sync::Arc;
|
|
|
|
|
|
|
|
pub fn clean_output_dir(dir_name: &str) {
|
|
|
|
let _ = fs::remove_dir_all(dir_name);
|
|
|
|
}
|
|
|
|
|
2019-07-26 10:36:24 +03:00
|
|
|
pub fn init_chain(dir_name: &str, genesis: Block) -> Chain {
|
2019-07-23 11:46:29 +03:00
|
|
|
let verifier_cache = Arc::new(RwLock::new(LruVerifierCache::new()));
|
|
|
|
Chain::init(
|
|
|
|
dir_name.to_string(),
|
|
|
|
Arc::new(NoopAdapter {}),
|
|
|
|
genesis,
|
|
|
|
pow::verify_size,
|
|
|
|
verifier_cache,
|
|
|
|
false,
|
|
|
|
)
|
|
|
|
.unwrap()
|
|
|
|
}
|
|
|
|
|
2019-07-26 10:36:24 +03:00
|
|
|
/// Build genesis block with reward (non-empty, like we have in mainnet).
|
2020-05-29 11:56:24 +03:00
|
|
|
pub fn genesis_block<K>(keychain: &K) -> Block
|
2019-07-26 10:36:24 +03:00
|
|
|
where
|
|
|
|
K: Keychain,
|
|
|
|
{
|
2019-07-23 11:46:29 +03:00
|
|
|
let key_id = keychain::ExtKeychain::derive_key_id(0, 1, 0, 0, 0);
|
|
|
|
let reward = reward::output(
|
2019-07-26 10:36:24 +03:00
|
|
|
keychain,
|
|
|
|
&libtx::ProofBuilder::new(keychain),
|
2019-07-23 11:46:29 +03:00
|
|
|
&key_id,
|
|
|
|
0,
|
|
|
|
false,
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
2019-07-26 10:36:24 +03:00
|
|
|
genesis::genesis_dev().with_reward(reward.0, reward.1)
|
|
|
|
}
|
2019-07-23 11:46:29 +03:00
|
|
|
|
2019-07-26 10:36:24 +03:00
|
|
|
/// Mine a chain of specified length to assist with automated tests.
|
|
|
|
/// Probably a good idea to call clean_output_dir at the beginning and end of each test.
|
2020-05-29 11:56:24 +03:00
|
|
|
#[allow(dead_code)]
|
2019-07-26 10:36:24 +03:00
|
|
|
pub fn mine_chain(dir_name: &str, chain_length: u64) -> Chain {
|
2020-05-22 14:51:58 +03:00
|
|
|
global::set_local_chain_type(ChainTypes::AutomatedTesting);
|
2019-07-26 10:36:24 +03:00
|
|
|
let keychain = keychain::ExtKeychain::from_random_seed(false).unwrap();
|
|
|
|
let genesis = genesis_block(&keychain);
|
|
|
|
let mut chain = init_chain(dir_name, genesis.clone());
|
2019-07-23 11:46:29 +03:00
|
|
|
mine_some_on_top(&mut chain, chain_length, &keychain);
|
|
|
|
chain
|
|
|
|
}
|
|
|
|
|
2020-05-29 11:56:24 +03:00
|
|
|
#[allow(dead_code)]
|
2019-07-23 11:46:29 +03:00
|
|
|
fn mine_some_on_top<K>(chain: &mut Chain, chain_length: u64, keychain: &K)
|
|
|
|
where
|
|
|
|
K: Keychain,
|
|
|
|
{
|
|
|
|
for n in 1..chain_length {
|
|
|
|
let prev = chain.head_header().unwrap();
|
|
|
|
let next_header_info = consensus::next_difficulty(1, chain.difficulty_iter().unwrap());
|
|
|
|
let pk = ExtKeychainPath::new(1, n as u32, 0, 0, 0).to_identifier();
|
|
|
|
let reward =
|
|
|
|
libtx::reward::output(keychain, &libtx::ProofBuilder::new(keychain), &pk, 0, false)
|
|
|
|
.unwrap();
|
2020-07-27 13:07:18 +03:00
|
|
|
let mut b = core::core::Block::new(&prev, &[], next_header_info.clone().difficulty, reward)
|
|
|
|
.unwrap();
|
2019-07-25 12:08:24 +03:00
|
|
|
b.header.timestamp = prev.timestamp + Duration::seconds(60);
|
2019-07-23 11:46:29 +03:00
|
|
|
b.header.pow.secondary_scaling = next_header_info.secondary_scaling;
|
|
|
|
|
|
|
|
chain.set_txhashset_roots(&mut b).unwrap();
|
|
|
|
|
2020-05-04 14:40:16 +03:00
|
|
|
let edge_bits = global::min_edge_bits();
|
2019-07-23 11:46:29 +03:00
|
|
|
b.header.pow.proof.edge_bits = edge_bits;
|
|
|
|
pow::pow_size(
|
|
|
|
&mut b.header,
|
|
|
|
next_header_info.difficulty,
|
|
|
|
global::proofsize(),
|
|
|
|
edge_bits,
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
2019-07-26 10:36:24 +03:00
|
|
|
let bhash = b.hash();
|
2019-07-23 11:46:29 +03:00
|
|
|
chain.process_block(b, Options::MINE).unwrap();
|
2019-07-26 10:36:24 +03:00
|
|
|
|
|
|
|
// checking our new head
|
|
|
|
let head = chain.head().unwrap();
|
|
|
|
assert_eq!(head.height, n);
|
|
|
|
assert_eq!(head.last_block_h, bhash);
|
|
|
|
|
|
|
|
// now check the block_header of the head
|
|
|
|
let header = chain.head_header().unwrap();
|
|
|
|
assert_eq!(header.height, n);
|
|
|
|
assert_eq!(header.hash(), bhash);
|
|
|
|
|
|
|
|
// now check the block itself
|
|
|
|
let block = chain.get_block(&header.hash()).unwrap();
|
|
|
|
assert_eq!(block.header.height, n);
|
|
|
|
assert_eq!(block.hash(), bhash);
|
|
|
|
assert_eq!(block.outputs().len(), 1);
|
|
|
|
|
|
|
|
// now check the block height index
|
|
|
|
let header_by_height = chain.get_header_by_height(n).unwrap();
|
|
|
|
assert_eq!(header_by_height.hash(), bhash);
|
|
|
|
|
|
|
|
chain.validate(false).unwrap();
|
2019-07-23 11:46:29 +03:00
|
|
|
}
|
|
|
|
}
|