2017-08-22 21:23:54 +03:00
|
|
|
// Copyright 2017 The Grin Developers
|
2016-10-22 21:35:48 +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.
|
|
|
|
|
2017-11-01 02:32:33 +03:00
|
|
|
extern crate env_logger;
|
2017-08-22 21:23:54 +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;
|
2017-08-22 21:23:54 +03:00
|
|
|
extern crate grin_pow as pow;
|
2018-01-08 04:23:23 +03:00
|
|
|
extern crate grin_util as util;
|
2017-11-01 02:32:33 +03:00
|
|
|
extern crate rand;
|
|
|
|
extern crate time;
|
2017-07-18 23:57:09 +03:00
|
|
|
|
2017-08-12 00:04:47 +03:00
|
|
|
use std::fs;
|
2016-12-19 02:51:54 +03:00
|
|
|
use std::sync::Arc;
|
2016-10-21 03:06:12 +03:00
|
|
|
|
2017-09-28 02:46:32 +03:00
|
|
|
use chain::Chain;
|
2017-08-22 21:23:54 +03:00
|
|
|
use chain::types::*;
|
2018-01-17 06:03:40 +03:00
|
|
|
use core::core::{Block, BlockHeader, Transaction, OutputIdentifier, build};
|
2017-08-22 21:23:54 +03:00
|
|
|
use core::core::hash::Hashed;
|
|
|
|
use core::core::target::Difficulty;
|
|
|
|
use core::consensus;
|
|
|
|
use core::global;
|
2017-11-16 00:49:15 +03:00
|
|
|
use core::global::ChainTypes;
|
2017-07-18 23:57:09 +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-08-12 00:04:47 +03:00
|
|
|
|
2017-09-29 21:44:25 +03:00
|
|
|
fn clean_output_dir(dir_name: &str) {
|
2017-09-28 02:46:32 +03:00
|
|
|
let _ = fs::remove_dir_all(dir_name);
|
2017-08-12 00:04:47 +03:00
|
|
|
}
|
|
|
|
|
2017-09-28 02:46:32 +03:00
|
|
|
fn setup(dir_name: &str) -> Chain {
|
|
|
|
let _ = env_logger::init();
|
|
|
|
clean_output_dir(dir_name);
|
2017-11-16 00:49:15 +03:00
|
|
|
global::set_mining_mode(ChainTypes::AutomatedTesting);
|
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
|
|
|
chain::Chain::init(
|
|
|
|
dir_name.to_string(),
|
|
|
|
Arc::new(NoopAdapter {}),
|
|
|
|
genesis_block,
|
|
|
|
pow::verify_size,
|
|
|
|
).unwrap()
|
2017-09-28 02:46:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn mine_empty_chain() {
|
|
|
|
let chain = setup(".grin");
|
2017-10-03 03:02:31 +03:00
|
|
|
let keychain = Keychain::from_random_seed().unwrap();
|
2016-10-21 03:06:12 +03:00
|
|
|
|
2017-10-03 03:02:31 +03:00
|
|
|
// mine and add a few blocks
|
2017-08-22 21:23:54 +03:00
|
|
|
let mut miner_config = types::MinerConfig {
|
2017-07-28 00:13:34 +03:00
|
|
|
enable_mining: true,
|
|
|
|
burn_reward: true,
|
|
|
|
..Default::default()
|
|
|
|
};
|
2017-07-18 23:57:09 +03:00
|
|
|
miner_config.cuckoo_miner_plugin_dir = Some(String::from("../target/debug/deps"));
|
|
|
|
|
2017-09-28 02:46:32 +03:00
|
|
|
let mut cuckoo_miner = cuckoo::Miner::new(
|
2017-09-29 21:44:25 +03:00
|
|
|
consensus::EASINESS,
|
|
|
|
global::sizeshift() as u32,
|
|
|
|
global::proofsize(),
|
|
|
|
);
|
2017-01-10 02:16:44 +03:00
|
|
|
for n in 1..4 {
|
2017-07-28 00:13:34 +03:00
|
|
|
let prev = chain.head_header().unwrap();
|
2018-01-12 21:35:37 +03:00
|
|
|
let difficulty = consensus::next_difficulty(chain.difficulty_iter()).unwrap();
|
2017-10-13 07:45:07 +03:00
|
|
|
let pk = keychain.derive_key_id(n as u32).unwrap();
|
2018-01-12 21:35:37 +03:00
|
|
|
let mut b = core::core::Block::new(
|
|
|
|
&prev,
|
|
|
|
vec![],
|
|
|
|
&keychain,
|
|
|
|
&pk,
|
2018-01-17 06:03:40 +03:00
|
|
|
difficulty.clone(),
|
2018-01-12 21:35:37 +03:00
|
|
|
).unwrap();
|
2017-07-04 02:46:25 +03:00
|
|
|
b.header.timestamp = prev.timestamp + time::Duration::seconds(60);
|
2016-10-21 03:06:12 +03:00
|
|
|
|
2018-01-12 21:35:37 +03:00
|
|
|
b.header.difficulty = difficulty.clone(); // TODO: overwrite here? really?
|
2018-01-08 04:23:23 +03:00
|
|
|
chain.set_sumtree_roots(&mut b, false).unwrap();
|
2016-11-30 07:07:08 +03:00
|
|
|
|
2017-07-27 22:08:48 +03:00
|
|
|
pow::pow_size(
|
2017-07-28 00:13:34 +03:00
|
|
|
&mut cuckoo_miner,
|
|
|
|
&mut b.header,
|
|
|
|
difficulty,
|
2017-08-09 19:40:23 +03:00
|
|
|
global::sizeshift() as u32,
|
2017-09-29 21:44:25 +03:00
|
|
|
).unwrap();
|
2017-07-27 22:08:48 +03:00
|
|
|
|
2017-07-28 00:13:34 +03:00
|
|
|
let bhash = b.hash();
|
2018-02-05 22:43:54 +03:00
|
|
|
chain.process_block(b, chain::Options::MINE).unwrap();
|
2016-10-21 03:06:12 +03:00
|
|
|
|
2017-01-10 02:16:44 +03:00
|
|
|
// checking our new head
|
2017-07-04 02:46:25 +03:00
|
|
|
let head = chain.head().unwrap();
|
2017-01-10 02:16:44 +03:00
|
|
|
assert_eq!(head.height, n);
|
2017-07-27 22:08:48 +03:00
|
|
|
assert_eq!(head.last_block_h, bhash);
|
2017-08-29 19:32:45 +03:00
|
|
|
|
2017-09-28 02:46:32 +03:00
|
|
|
// 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);
|
2017-01-10 02:16:44 +03:00
|
|
|
}
|
2016-10-21 03:06:12 +03:00
|
|
|
}
|
2017-01-10 07:30:02 +03:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn mine_forks() {
|
2017-09-28 02:46:32 +03:00
|
|
|
let chain = setup(".grin2");
|
2018-01-08 04:23:23 +03:00
|
|
|
let kc = Keychain::from_random_seed().unwrap();
|
2017-08-12 00:04:47 +03:00
|
|
|
|
2017-09-28 02:46:32 +03:00
|
|
|
// add a first block to not fork genesis
|
|
|
|
let prev = chain.head_header().unwrap();
|
2018-01-08 04:23:23 +03:00
|
|
|
let b = prepare_block(&kc, &prev, &chain, 2);
|
2018-02-05 22:43:54 +03:00
|
|
|
chain.process_block(b, chain::Options::SKIP_POW).unwrap();
|
2017-01-10 07:30:02 +03:00
|
|
|
|
|
|
|
// mine and add a few blocks
|
|
|
|
|
|
|
|
for n in 1..4 {
|
2017-09-28 02:46:32 +03:00
|
|
|
// first block for one branch
|
2017-07-28 00:13:34 +03:00
|
|
|
let prev = chain.head_header().unwrap();
|
2018-01-08 04:23:23 +03:00
|
|
|
let b1 = prepare_block(&kc, &prev, &chain, 3 * n);
|
2017-09-28 02:46:32 +03:00
|
|
|
|
|
|
|
// 2nd block with higher difficulty for other branch
|
2018-01-08 04:23:23 +03:00
|
|
|
let b2 = prepare_block(&kc, &prev, &chain, 3 * n + 1);
|
2017-09-28 02:46:32 +03:00
|
|
|
|
|
|
|
// process the first block to extend the chain
|
|
|
|
let bhash = b1.hash();
|
2018-02-05 22:43:54 +03:00
|
|
|
chain.process_block(b1, chain::Options::SKIP_POW).unwrap();
|
2017-01-10 07:30:02 +03:00
|
|
|
|
|
|
|
// checking our new head
|
2017-07-04 02:46:25 +03:00
|
|
|
let head = chain.head().unwrap();
|
2017-09-29 21:44:25 +03:00
|
|
|
assert_eq!(head.height, (n + 1) as u64);
|
2017-07-27 22:08:48 +03:00
|
|
|
assert_eq!(head.last_block_h, bhash);
|
2017-01-10 07:30:02 +03:00
|
|
|
assert_eq!(head.prev_block_h, prev.hash());
|
|
|
|
|
2017-09-28 02:46:32 +03:00
|
|
|
// process the 2nd block to build a fork with more work
|
|
|
|
let bhash = b2.hash();
|
2018-02-05 22:43:54 +03:00
|
|
|
chain.process_block(b2, chain::Options::SKIP_POW).unwrap();
|
2017-01-10 07:30:02 +03:00
|
|
|
|
2017-07-04 02:46:25 +03:00
|
|
|
// checking head switch
|
|
|
|
let head = chain.head().unwrap();
|
2017-09-29 21:44:25 +03:00
|
|
|
assert_eq!(head.height, (n + 1) as u64);
|
2017-07-27 22:08:48 +03:00
|
|
|
assert_eq!(head.last_block_h, bhash);
|
2017-01-10 07:30:02 +03:00
|
|
|
assert_eq!(head.prev_block_h, prev.hash());
|
|
|
|
}
|
|
|
|
}
|
2017-09-28 02:46:32 +03:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn mine_losing_fork() {
|
2018-01-08 04:23:23 +03:00
|
|
|
let kc = Keychain::from_random_seed().unwrap();
|
2017-09-28 02:46:32 +03:00
|
|
|
let chain = setup(".grin3");
|
|
|
|
|
|
|
|
// add a first block we'll be forking from
|
|
|
|
let prev = chain.head_header().unwrap();
|
2018-01-08 04:23:23 +03:00
|
|
|
let b1 = prepare_block(&kc, &prev, &chain, 2);
|
2017-09-28 02:46:32 +03:00
|
|
|
let b1head = b1.header.clone();
|
2018-02-05 22:43:54 +03:00
|
|
|
chain.process_block(b1, chain::Options::SKIP_POW).unwrap();
|
2017-09-28 02:46:32 +03:00
|
|
|
|
|
|
|
// prepare the 2 successor, sibling blocks, one with lower diff
|
2018-01-08 04:23:23 +03:00
|
|
|
let b2 = prepare_block(&kc, &b1head, &chain, 4);
|
2017-09-28 02:46:32 +03:00
|
|
|
let b2head = b2.header.clone();
|
2018-01-08 04:23:23 +03:00
|
|
|
let bfork = prepare_block(&kc, &b1head, &chain, 3);
|
2017-09-28 02:46:32 +03:00
|
|
|
|
|
|
|
// add higher difficulty first, prepare its successor, then fork
|
2017-11-01 02:32:33 +03:00
|
|
|
// with lower diff
|
2018-02-05 22:43:54 +03:00
|
|
|
chain.process_block(b2, chain::Options::SKIP_POW).unwrap();
|
2017-09-28 02:46:32 +03:00
|
|
|
assert_eq!(chain.head_header().unwrap().hash(), b2head.hash());
|
2018-01-08 04:23:23 +03:00
|
|
|
let b3 = prepare_block(&kc, &b2head, &chain, 5);
|
2018-02-05 22:43:54 +03:00
|
|
|
chain.process_block(bfork, chain::Options::SKIP_POW).unwrap();
|
2017-09-28 02:46:32 +03:00
|
|
|
|
|
|
|
// adding the successor
|
|
|
|
let b3head = b3.header.clone();
|
2018-02-05 22:43:54 +03:00
|
|
|
chain.process_block(b3, chain::Options::SKIP_POW).unwrap();
|
2017-09-28 02:46:32 +03:00
|
|
|
assert_eq!(chain.head_header().unwrap().hash(), b3head.hash());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn longer_fork() {
|
2018-01-08 04:23:23 +03:00
|
|
|
let kc = Keychain::from_random_seed().unwrap();
|
2017-09-28 02:46:32 +03:00
|
|
|
// to make it easier to compute the sumtree roots in the test, we
|
2018-01-08 04:23:23 +03:00
|
|
|
// prepare 2 chains, the 2nd will be have the forked blocks we can
|
|
|
|
// then send back on the 1st
|
2017-09-28 02:46:32 +03:00
|
|
|
let chain = setup(".grin4");
|
|
|
|
let chain_fork = setup(".grin5");
|
|
|
|
|
|
|
|
// add blocks to both chains, 20 on the main one, only the first 5
|
2018-01-08 04:23:23 +03:00
|
|
|
// for the forked chain
|
2017-09-28 02:46:32 +03:00
|
|
|
let mut prev = chain.head_header().unwrap();
|
|
|
|
for n in 0..10 {
|
2018-01-08 04:23:23 +03:00
|
|
|
let b = prepare_block(&kc, &prev, &chain, 2*n + 2);
|
2017-09-28 02:46:32 +03:00
|
|
|
let bh = b.header.clone();
|
|
|
|
|
|
|
|
if n < 5 {
|
|
|
|
let b_fork = b.clone();
|
2018-02-05 22:43:54 +03:00
|
|
|
chain_fork.process_block(b_fork, chain::Options::SKIP_POW).unwrap();
|
2017-09-28 02:46:32 +03:00
|
|
|
}
|
|
|
|
|
2018-02-05 22:43:54 +03:00
|
|
|
chain.process_block(b, chain::Options::SKIP_POW).unwrap();
|
2017-09-28 02:46:32 +03:00
|
|
|
prev = bh;
|
|
|
|
}
|
|
|
|
|
|
|
|
// check both chains are in the expected state
|
|
|
|
let head = chain.head_header().unwrap();
|
|
|
|
assert_eq!(head.height, 10);
|
|
|
|
assert_eq!(head.hash(), prev.hash());
|
|
|
|
let head_fork = chain_fork.head_header().unwrap();
|
|
|
|
assert_eq!(head_fork.height, 5);
|
|
|
|
|
|
|
|
let mut prev_fork = head_fork.clone();
|
|
|
|
for n in 0..7 {
|
2018-01-08 04:23:23 +03:00
|
|
|
let b_fork = prepare_block(&kc, &prev_fork, &chain_fork, 2*n + 11);
|
2017-09-28 02:46:32 +03:00
|
|
|
let bh_fork = b_fork.header.clone();
|
|
|
|
|
|
|
|
let b = b_fork.clone();
|
2018-02-05 22:43:54 +03:00
|
|
|
chain.process_block(b, chain::Options::SKIP_POW).unwrap();
|
2017-09-28 02:46:32 +03:00
|
|
|
|
2018-02-05 22:43:54 +03:00
|
|
|
chain_fork.process_block(b_fork, chain::Options::SKIP_POW).unwrap();
|
2017-09-28 02:46:32 +03:00
|
|
|
prev_fork = bh_fork;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-08 04:23:23 +03:00
|
|
|
#[test]
|
|
|
|
fn spend_in_fork() {
|
|
|
|
util::init_test_logger();
|
|
|
|
let chain = setup(".grin6");
|
|
|
|
let prev = chain.head_header().unwrap();
|
|
|
|
let kc = Keychain::from_random_seed().unwrap();
|
|
|
|
|
|
|
|
let mut fork_head = prev;
|
2018-01-17 06:03:40 +03:00
|
|
|
|
|
|
|
// mine the first block and keep track of the block_hash
|
|
|
|
// so we can spend the coinbase later
|
|
|
|
let b = prepare_block(&kc, &fork_head, &chain, 2);
|
|
|
|
let block_hash = b.hash();
|
|
|
|
fork_head = b.header.clone();
|
2018-02-05 22:43:54 +03:00
|
|
|
chain.process_block(b, chain::Options::SKIP_POW).unwrap();
|
2018-01-17 06:03:40 +03:00
|
|
|
|
|
|
|
// now mine three further blocks
|
|
|
|
for n in 3..6 {
|
2018-01-08 04:23:23 +03:00
|
|
|
let b = prepare_block(&kc, &fork_head, &chain, n);
|
|
|
|
fork_head = b.header.clone();
|
2018-02-05 22:43:54 +03:00
|
|
|
chain.process_block(b, chain::Options::SKIP_POW).unwrap();
|
2018-01-08 04:23:23 +03:00
|
|
|
}
|
|
|
|
|
2018-01-17 06:03:40 +03:00
|
|
|
let lock_height = 1 + global::coinbase_maturity();
|
|
|
|
assert_eq!(lock_height, 4);
|
|
|
|
|
2018-01-08 04:23:23 +03:00
|
|
|
let (tx1, _) = build::transaction(
|
|
|
|
vec![
|
2018-01-17 06:03:40 +03:00
|
|
|
build::coinbase_input(consensus::REWARD, block_hash, kc.derive_key_id(2).unwrap()),
|
2018-01-08 04:23:23 +03:00
|
|
|
build::output(consensus::REWARD - 20000, kc.derive_key_id(30).unwrap()),
|
|
|
|
build::with_fee(20000),
|
|
|
|
],
|
|
|
|
&kc,
|
|
|
|
).unwrap();
|
|
|
|
|
|
|
|
let next = prepare_block_tx(&kc, &fork_head, &chain, 7, vec![&tx1]);
|
|
|
|
let prev_main = next.header.clone();
|
2018-02-05 22:43:54 +03:00
|
|
|
chain.process_block(next.clone(), chain::Options::SKIP_POW).unwrap();
|
2018-01-08 04:23:23 +03:00
|
|
|
|
|
|
|
let (tx2, _) = build::transaction(
|
|
|
|
vec![
|
2018-01-17 06:03:40 +03:00
|
|
|
build::input(consensus::REWARD - 20000, next.hash(), kc.derive_key_id(30).unwrap()),
|
2018-01-08 04:23:23 +03:00
|
|
|
build::output(consensus::REWARD - 40000, kc.derive_key_id(31).unwrap()),
|
|
|
|
build::with_fee(20000),
|
|
|
|
],
|
|
|
|
&kc,
|
|
|
|
).unwrap();
|
|
|
|
|
|
|
|
let next = prepare_block_tx(&kc, &prev_main, &chain, 9, vec![&tx2]);
|
|
|
|
let prev_main = next.header.clone();
|
2018-02-05 22:43:54 +03:00
|
|
|
chain.process_block(next, chain::Options::SKIP_POW).unwrap();
|
2018-01-08 04:23:23 +03:00
|
|
|
|
|
|
|
// mine 2 forked blocks from the first
|
|
|
|
let fork = prepare_fork_block_tx(&kc, &fork_head, &chain, 6, vec![&tx1]);
|
|
|
|
let prev_fork = fork.header.clone();
|
2018-02-05 22:43:54 +03:00
|
|
|
chain.process_block(fork, chain::Options::SKIP_POW).unwrap();
|
2018-01-08 04:23:23 +03:00
|
|
|
|
|
|
|
let fork_next = prepare_fork_block_tx(&kc, &prev_fork, &chain, 8, vec![&tx2]);
|
|
|
|
let prev_fork = fork_next.header.clone();
|
2018-02-05 22:43:54 +03:00
|
|
|
chain.process_block(fork_next, chain::Options::SKIP_POW).unwrap();
|
2018-01-08 04:23:23 +03:00
|
|
|
|
|
|
|
// check state
|
|
|
|
let head = chain.head_header().unwrap();
|
|
|
|
assert_eq!(head.height, 6);
|
|
|
|
assert_eq!(head.hash(), prev_main.hash());
|
2018-01-17 06:03:40 +03:00
|
|
|
assert!(chain.is_unspent(&OutputIdentifier::from_output(&tx2.outputs[0])).is_ok());
|
|
|
|
assert!(chain.is_unspent(&OutputIdentifier::from_output(&tx1.outputs[0])).is_err());
|
2018-01-08 04:23:23 +03:00
|
|
|
|
|
|
|
// make the fork win
|
|
|
|
let fork_next = prepare_fork_block(&kc, &prev_fork, &chain, 10);
|
|
|
|
let prev_fork = fork_next.header.clone();
|
2018-02-05 22:43:54 +03:00
|
|
|
chain.process_block(fork_next, chain::Options::SKIP_POW).unwrap();
|
2018-01-08 04:23:23 +03:00
|
|
|
|
|
|
|
// check state
|
|
|
|
let head = chain.head_header().unwrap();
|
|
|
|
assert_eq!(head.height, 7);
|
|
|
|
assert_eq!(head.hash(), prev_fork.hash());
|
2018-01-17 06:03:40 +03:00
|
|
|
assert!(chain.is_unspent(&OutputIdentifier::from_output(&tx2.outputs[0])).is_ok());
|
|
|
|
assert!(chain.is_unspent(&OutputIdentifier::from_output(&tx1.outputs[0])).is_err());
|
2018-01-08 04:23:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn prepare_block(kc: &Keychain, prev: &BlockHeader, chain: &Chain, diff: u64) -> Block {
|
|
|
|
let mut b = prepare_block_nosum(kc, prev, diff, vec![]);
|
|
|
|
chain.set_sumtree_roots(&mut b, false).unwrap();
|
2017-09-28 02:46:32 +03:00
|
|
|
b
|
|
|
|
}
|
|
|
|
|
2018-01-08 04:23:23 +03:00
|
|
|
fn prepare_block_tx(kc: &Keychain, prev: &BlockHeader, chain: &Chain, diff: u64, txs: Vec<&Transaction>) -> Block {
|
|
|
|
let mut b = prepare_block_nosum(kc, prev, diff, txs);
|
|
|
|
chain.set_sumtree_roots(&mut b, false).unwrap();
|
|
|
|
b
|
|
|
|
}
|
|
|
|
|
|
|
|
fn prepare_fork_block(kc: &Keychain, prev: &BlockHeader, chain: &Chain, diff: u64) -> Block {
|
|
|
|
let mut b = prepare_block_nosum(kc, prev, diff, vec![]);
|
|
|
|
chain.set_sumtree_roots(&mut b, true).unwrap();
|
|
|
|
b
|
|
|
|
}
|
|
|
|
|
|
|
|
fn prepare_fork_block_tx(kc: &Keychain, prev: &BlockHeader, chain: &Chain, diff: u64, txs: Vec<&Transaction>) -> Block {
|
|
|
|
let mut b = prepare_block_nosum(kc, prev, diff, txs);
|
|
|
|
chain.set_sumtree_roots(&mut b, true).unwrap();
|
|
|
|
b
|
|
|
|
}
|
|
|
|
|
|
|
|
fn prepare_block_nosum(kc: &Keychain, prev: &BlockHeader, diff: u64, txs: Vec<&Transaction>) -> Block {
|
|
|
|
let key_id = kc.derive_key_id(diff as u32).unwrap();
|
2017-10-03 03:02:31 +03:00
|
|
|
|
2018-01-12 21:35:37 +03:00
|
|
|
let mut b = match core::core::Block::new(prev, txs, kc, &key_id, Difficulty::from_num(diff)) {
|
2018-01-10 22:36:27 +03:00
|
|
|
Err(e) => panic!("{:?}",e),
|
|
|
|
Ok(b) => b
|
|
|
|
};
|
2017-09-28 02:46:32 +03:00
|
|
|
b.header.timestamp = prev.timestamp + time::Duration::seconds(60);
|
|
|
|
b.header.total_difficulty = Difficulty::from_num(diff);
|
|
|
|
b
|
|
|
|
}
|