Make total_difficulty the sum of network difficulty [testnet2] (#523)

* Make total_difficulty the sum of network difficulty, not whatever the miner happened to mine.
  - Only for Testnet2 / Mainnet (hardforks Testnet1)
  - update chain::pipe validate_header to validate according to Testnet2 rules for cumulative difference

Fixes #280

* tests that should ignore (network) difficulty

* fn new_block is explained as "utility to create a block without worrying about the key or previous header" so it gets network difficulty := 0 too

* update tx pool tests (going with "minimum" for network difficulty for now)

* add ERR outputs about bannable offences
(#406 should know about these)

* whitespace fix

* mine_simple_chain: Probably DON'T overwrite difficulty (?)

* core/mod tests "reward_empty_block" and "reward_with_tx_block" tests set to use lowest network difficulty possible
This commit is contained in:
Simon B
2018-01-12 19:35:37 +01:00
committed by Ignotus Peverell
parent 5edc63f617
commit 8b2f9503c9
8 changed files with 114 additions and 28 deletions
+11 -3
View File
@@ -292,10 +292,11 @@ impl Block {
txs: Vec<&Transaction>,
keychain: &keychain::Keychain,
key_id: &keychain::Identifier,
difficulty: Difficulty,
) -> Result<Block, Error> {
let fees = txs.iter().map(|tx| tx.fee).sum();
let (reward_out, reward_proof) = Block::reward_output(keychain, key_id, fees)?;
let block = Block::with_reward(prev, txs, reward_out, reward_proof)?;
let block = Block::with_reward(prev, txs, reward_out, reward_proof, difficulty)?;
Ok(block)
}
@@ -307,6 +308,7 @@ impl Block {
txs: Vec<&Transaction>,
reward_out: Output,
reward_kern: TxKernel,
difficulty: Difficulty,
) -> Result<Block, Error> {
let mut kernels = vec![];
let mut inputs = vec![];
@@ -350,7 +352,7 @@ impl Block {
..time::now_utc()
},
previous: prev.hash(),
total_difficulty: prev.pow.clone().to_difficulty() +
total_difficulty: difficulty +
prev.total_difficulty.clone(),
..Default::default()
},
@@ -615,7 +617,13 @@ mod test {
// header
fn new_block(txs: Vec<&Transaction>, keychain: &Keychain) -> Block {
let key_id = keychain.derive_key_id(1).unwrap();
Block::new(&BlockHeader::default(), txs, keychain, &key_id).unwrap()
Block::new(
&BlockHeader::default(),
txs,
keychain,
&key_id,
Difficulty::minimum()
).unwrap()
}
// utility producing a transaction that spends an output with the provided
+18 -2
View File
@@ -28,6 +28,7 @@ use std::cmp::Ordering;
use std::num::ParseFloatError;
use consensus::GRIN_BASE;
use core::target::Difficulty;
use util::{secp, static_secp_instance};
use util::secp::pedersen::*;
@@ -388,7 +389,13 @@ mod test {
let keychain = keychain::Keychain::from_random_seed().unwrap();
let key_id = keychain.derive_key_id(1).unwrap();
let b = Block::new(&BlockHeader::default(), vec![], &keychain, &key_id).unwrap();
let b = Block::new(
&BlockHeader::default(),
vec![],
&keychain,
&key_id,
Difficulty::minimum(),
).unwrap();
b.compact().validate().unwrap();
}
@@ -400,7 +407,13 @@ mod test {
let mut tx1 = tx2i1o();
tx1.verify_sig().unwrap();
let b = Block::new(&BlockHeader::default(), vec![&mut tx1], &keychain, &key_id).unwrap();
let b = Block::new(
&BlockHeader::default(),
vec![&mut tx1],
&keychain,
&key_id,
Difficulty::minimum(),
).unwrap();
b.compact().validate().unwrap();
}
@@ -417,6 +430,7 @@ mod test {
vec![&mut tx1, &mut tx2],
&keychain,
&key_id,
Difficulty::minimum(),
).unwrap();
b.validate().unwrap();
}
@@ -447,6 +461,7 @@ mod test {
vec![&tx1],
&keychain,
&key_id3.clone(),
Difficulty::minimum(),
).unwrap();
b.validate().unwrap();
@@ -467,6 +482,7 @@ mod test {
vec![&tx1],
&keychain,
&key_id3.clone(),
Difficulty::minimum(),
).unwrap();
match b.validate() {
Err(KernelLockHeight { lock_height: height }) => {