diff --git a/chain/src/chain.rs b/chain/src/chain.rs index f3846377e..a026a6d61 100644 --- a/chain/src/chain.rs +++ b/chain/src/chain.rs @@ -957,7 +957,7 @@ impl Chain { // Save the block_sums (utxo_sum, kernel_sum) to the db for use later. batch.save_block_sums( &header.hash(), - &BlockSums { + BlockSums { utxo_sum, kernel_sum, }, @@ -1480,7 +1480,7 @@ fn setup_head( // Save the block_sums to the db for use later. batch.save_block_sums( &header.hash(), - &BlockSums { + BlockSums { utxo_sum, kernel_sum, }, @@ -1542,7 +1542,7 @@ fn setup_head( })?; // Save the block_sums to the db for use later. - batch.save_block_sums(&genesis.hash(), &sums)?; + batch.save_block_sums(&genesis.hash(), sums)?; info!("init: saved genesis: {:?}", genesis.hash()); } diff --git a/chain/src/pipe.rs b/chain/src/pipe.rs index 2161dd71c..8fd4c38b5 100644 --- a/chain/src/pipe.rs +++ b/chain/src/pipe.rs @@ -23,7 +23,7 @@ use crate::core::pow; use crate::error::{Error, ErrorKind}; use crate::store; use crate::txhashset; -use crate::types::{CommitPos, Options, Tip}; +use crate::types::{Options, Tip}; use crate::util::RwLock; use grin_store; use std::sync::Arc; @@ -121,7 +121,7 @@ pub fn process_block(b: &Block, ctx: &mut BlockContext<'_>) -> Result) -> Result) -> Result) -> Result { +/// Saves the new block_sums to the db via the current batch if successful. +fn verify_block_sums(b: &Block, batch: &store::Batch<'_>) -> Result<(), Error> { // Retrieve the block_sums for the previous block. let block_sums = batch.get_block_sums(&b.header.prev_hash)?; @@ -419,10 +420,15 @@ fn verify_block_sums(b: &Block, batch: &store::Batch<'_>) -> Result, batch: &store::Batch<'_>, -) -> Result, Error> { - let spent = ext.extension.apply_block(block, batch)?; +) -> Result<(), Error> { + ext.extension.apply_block(block, batch)?; ext.extension.validate_roots(&block.header)?; ext.extension.validate_sizes(&block.header)?; - Ok(spent) + Ok(()) } /// Officially adds the block to our chain (possibly on a losing fork). -/// Adds the associated block_sums and spent_index as well. /// Header must be added separately (assume this has been done previously). -fn add_block( - b: &Block, - block_sums: &BlockSums, - spent: &Vec, - batch: &store::Batch<'_>, -) -> Result<(), Error> { +fn add_block(b: &Block, batch: &store::Batch<'_>) -> Result<(), Error> { batch.save_block(b)?; - batch.save_block_sums(&b.hash(), block_sums)?; - batch.save_spent_index(&b.hash(), spent)?; Ok(()) } diff --git a/chain/src/store.rs b/chain/src/store.rs index 3cfbc62d6..540de6535 100644 --- a/chain/src/store.rs +++ b/chain/src/store.rs @@ -318,7 +318,7 @@ impl<'a> Batch<'a> { } /// Save block_sums for the block. - pub fn save_block_sums(&self, h: &Hash, sums: &BlockSums) -> Result<(), Error> { + pub fn save_block_sums(&self, h: &Hash, sums: BlockSums) -> Result<(), Error> { self.db .put_ser(&to_key(BLOCK_SUMS_PREFIX, &mut h.to_vec())[..], &sums) } diff --git a/chain/src/txhashset/txhashset.rs b/chain/src/txhashset/txhashset.rs index 00485d076..48aa0f80f 100644 --- a/chain/src/txhashset/txhashset.rs +++ b/chain/src/txhashset/txhashset.rs @@ -933,9 +933,8 @@ impl<'a> Extension<'a> { /// Apply a new block to the current txhashet extension (output, rangeproof, kernel MMRs). /// Returns a vec of commit_pos representing the pos and height of the outputs spent /// by this block. - pub fn apply_block(&mut self, b: &Block, batch: &Batch<'_>) -> Result, Error> { + pub fn apply_block(&mut self, b: &Block, batch: &Batch<'_>) -> Result<(), Error> { let mut affected_pos = vec![]; - let mut spent = vec![]; // Apply the output to the output and rangeproof MMRs. // Add pos to affected_pos to update the accumulator later on. @@ -949,12 +948,14 @@ impl<'a> Extension<'a> { // Remove the output from the output and rangeproof MMRs. // Add spent_pos to affected_pos to update the accumulator later on. // Remove the spent output from the output_pos index. + let mut spent = vec![]; for input in b.inputs() { let spent_pos = self.apply_input(input, batch)?; affected_pos.push(spent_pos.pos); batch.delete_output_pos_height(&input.commitment())?; spent.push(spent_pos); } + batch.save_spent_index(&b.hash(), &spent)?; for kernel in b.kernels() { self.apply_kernel(kernel)?; @@ -966,7 +967,7 @@ impl<'a> Extension<'a> { // Update the head of the extension to reflect the block we just applied. self.head = Tip::from_header(&b.header); - Ok(spent) + Ok(()) } fn apply_to_bitmap_accumulator(&mut self, output_pos: &[u64]) -> Result<(), Error> { diff --git a/pool/tests/common.rs b/pool/tests/common.rs index 885e0dfd4..ad7b94d45 100644 --- a/pool/tests/common.rs +++ b/pool/tests/common.rs @@ -83,7 +83,7 @@ impl ChainAdapter { utxo_sum, kernel_sum, }; - batch.save_block_sums(&header.hash(), &block_sums).unwrap(); + batch.save_block_sums(&header.hash(), block_sums).unwrap(); batch.commit().unwrap();