[testnet2] Store output sum (#1043)

* block sums and reworked block validation
read and write block_sums
refactor validate on both block and txhashset
write block_sum on fast sync
we store the kernel_sum (need to account for the offset)

* block_sums

* rustfmt

* cleanup
This commit is contained in:
Antioch Peverell
2018-05-07 09:21:41 -04:00
committed by GitHub
parent b42b2a4f77
commit 4dd94ff39e
13 changed files with 426 additions and 141 deletions
+2 -4
View File
@@ -152,8 +152,8 @@ impl p2p::ChainAdapter for NetToChainAdapter {
.upgrade()
.expect("failed to upgrade weak ref to chain");
if let Ok(prev_header) = chain.get_block_header(&cb.header.previous) {
if let Ok(()) = block.validate(&prev_header) {
if let Ok(sums) = chain.get_block_sums(&cb.header.previous) {
if block.validate(&sums.output_sum, &sums.kernel_sum).is_ok() {
debug!(LOGGER, "adapter: successfully hydrated block from tx pool!");
self.process_block(block, addr)
} else {
@@ -351,7 +351,6 @@ impl p2p::ChainAdapter for NetToChainAdapter {
}
impl NetToChainAdapter {
/// Construct a new NetToChainAdapter instance
pub fn new(
currently_syncing: Arc<AtomicBool>,
@@ -599,7 +598,6 @@ impl ChainAdapter for ChainToPoolAndNetAdapter {
}
impl ChainToPoolAndNetAdapter {
/// Construct a ChainToPoolAndNetAdaper instance.
pub fn new(
tx_pool: Arc<RwLock<pool::TransactionPool<PoolToChainAdapter>>>,
+11 -2
View File
@@ -24,10 +24,12 @@ use itertools::Itertools;
use core::ser::AsFixedBytes;
use chain;
use chain::types::BlockSums;
use pool;
use core::consensus;
use core::core;
use core::core::Transaction;
use core::core::hash::Hashed;
use core::ser;
use keychain::{Identifier, Keychain};
use wallet;
@@ -137,7 +139,14 @@ fn build_block(
wallet_listener_url: Option<String>,
) -> Result<(core::Block, BlockFees), Error> {
// prepare the block header timestamp
let head = chain.head_header().unwrap();
let head = chain.head_header()?;
let prev_sums = if head.height == 0 {
BlockSums::default()
} else {
chain.get_block_sums(&head.hash())?
};
let mut now_sec = time::get_time().sec;
let head_sec = head.timestamp.to_timespec().sec;
if now_sec <= head_sec {
@@ -168,7 +177,7 @@ fn build_block(
let mut b = core::Block::with_reward(&head, txs, output, kernel, difficulty.clone())?;
// making sure we're not spending time mining a useless block
b.validate(&head)?;
b.validate(&prev_sums.output_sum, &prev_sums.kernel_sum)?;
let mut rng = rand::OsRng::new().unwrap();
b.header.nonce = rng.gen();