grin/chain/tests/mine_simple_chain.rs

126 lines
4 KiB
Rust
Raw Normal View History

// Copyright 2016 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.
2016-10-21 03:06:12 +03:00
extern crate grin_core;
extern crate grin_chain;
extern crate env_logger;
extern crate time;
extern crate rand;
2016-10-21 03:06:12 +03:00
extern crate secp256k1zkp as secp;
extern crate grin_grin as grin;
use std::sync::Arc;
use std::thread;
2016-10-21 03:06:12 +03:00
use rand::os::OsRng;
use grin_chain::types::*;
use grin_core::core::hash::Hashed;
use grin_core::core::target::Difficulty;
2016-10-21 03:06:12 +03:00
use grin_core::pow;
use grin_core::core;
use grin_core::consensus;
use grin_core::pow::cuckoo;
use grin_core::global;
use grin_core::global::MiningParameterMode;
use grin_core::pow::MiningWorker;
2016-10-21 03:06:12 +03:00
#[test]
fn mine_empty_chain() {
let _ = env_logger::init();
global::set_mining_mode(MiningParameterMode::AutomatedTesting);
2016-10-21 03:06:12 +03:00
let mut rng = OsRng::new().unwrap();
let chain = grin_chain::Chain::init(".grin".to_string(), Arc::new(NoopAdapter {}))
2017-07-28 00:13:34 +03:00
.unwrap();
2016-10-21 03:06:12 +03:00
// mine and add a few blocks
2016-10-21 03:06:12 +03:00
let secp = secp::Secp256k1::with_caps(secp::ContextFlag::Commit);
let reward_key = secp::key::SecretKey::new(&secp, &mut rng);
2017-07-28 00:13:34 +03:00
let mut miner_config = grin::MinerConfig {
enable_mining: true,
burn_reward: true,
..Default::default()
};
miner_config.cuckoo_miner_plugin_dir = Some(String::from("../target/debug/deps"));
let mut cuckoo_miner = cuckoo::Miner::new(consensus::EASINESS, global::sizeshift() as u32, global::proofsize());
for n in 1..4 {
2017-07-28 00:13:34 +03:00
let prev = chain.head_header().unwrap();
let mut b = core::Block::new(&prev, vec![], reward_key).unwrap();
b.header.timestamp = prev.timestamp + time::Duration::seconds(60);
2016-10-21 03:06:12 +03:00
let difficulty = consensus::next_difficulty(chain.difficulty_iter()).unwrap();
b.header.difficulty = difficulty.clone();
2016-11-30 07:07:08 +03:00
pow::pow_size(
2017-07-28 00:13:34 +03:00
&mut cuckoo_miner,
&mut b.header,
difficulty,
global::sizeshift() as u32,
2017-07-28 00:13:34 +03:00
).unwrap();
2017-07-28 00:13:34 +03:00
let bhash = b.hash();
chain.process_block(b, grin_chain::EASY_POW).unwrap();
2016-10-21 03:06:12 +03:00
// checking our new head
let head = chain.head().unwrap();
assert_eq!(head.height, n);
assert_eq!(head.last_block_h, bhash);
}
2016-10-21 03:06:12 +03:00
}
#[test]
fn mine_forks() {
let _ = env_logger::init();
let mut rng = OsRng::new().unwrap();
let chain = grin_chain::Chain::init(".grin2".to_string(), Arc::new(NoopAdapter {}))
2017-07-28 00:13:34 +03:00
.unwrap();
// mine and add a few blocks
let secp = secp::Secp256k1::with_caps(secp::ContextFlag::Commit);
let reward_key = secp::key::SecretKey::new(&secp, &mut rng);
for n in 1..4 {
2017-07-28 00:13:34 +03:00
let prev = chain.head_header().unwrap();
let mut b = core::Block::new(&prev, vec![], reward_key).unwrap();
b.header.timestamp = prev.timestamp + time::Duration::seconds(60);
2017-07-28 00:13:34 +03:00
b.header.total_difficulty = Difficulty::from_num(2 * n);
let bhash = b.hash();
chain.process_block(b, grin_chain::SKIP_POW).unwrap();
// checking our new head
2017-07-28 00:13:34 +03:00
thread::sleep(::std::time::Duration::from_millis(50));
let head = chain.head().unwrap();
assert_eq!(head.height, n as u64);
assert_eq!(head.last_block_h, bhash);
assert_eq!(head.prev_block_h, prev.hash());
2017-07-28 00:13:34 +03:00
// build another block with higher difficulty
let mut b = core::Block::new(&prev, vec![], reward_key).unwrap();
b.header.timestamp = prev.timestamp + time::Duration::seconds(60);
2017-07-28 00:13:34 +03:00
b.header.total_difficulty = Difficulty::from_num(2 * n + 1);
let bhash = b.hash();
chain.process_block(b, grin_chain::SKIP_POW).unwrap();
// checking head switch
2017-07-28 00:13:34 +03:00
thread::sleep(::std::time::Duration::from_millis(50));
let head = chain.head().unwrap();
assert_eq!(head.height, n as u64);
assert_eq!(head.last_block_h, bhash);
assert_eq!(head.prev_block_h, prev.hash());
}
}