From 12b2eb8137914aa5e02ba52ac72d5a1a932d8ac7 Mon Sep 17 00:00:00 2001 From: antiochp <30642645+antiochp@users.noreply.github.com> Date: Fri, 28 Feb 2020 15:15:38 +0000 Subject: [PATCH 1/3] simplify when block_sums and spent_index are added to the db --- chain/src/chain.rs | 6 ++--- chain/src/pipe.rs | 45 +++++++++++++++----------------- chain/src/store.rs | 2 +- chain/src/txhashset/txhashset.rs | 7 ++--- 4 files changed, 29 insertions(+), 31 deletions(-) diff --git a/chain/src/chain.rs b/chain/src/chain.rs index f3846377e8..a026a6d619 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 2161dd71c5..1f40c55d82 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 +419,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 3cfbc62d6b..540de65358 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 00485d0769..48aa0f80f9 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> { From 88505e11dc808fc485a1459ab178537848bb17b0 Mon Sep 17 00:00:00 2001 From: antiochp <30642645+antiochp@users.noreply.github.com> Date: Mon, 2 Mar 2020 17:52:34 +0000 Subject: [PATCH 2/3] fix pool tests --- pool/tests/common.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pool/tests/common.rs b/pool/tests/common.rs index 885e0dfd49..ad7b94d45e 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(); From c4af801c2d7246ff3c0cb02b4aa5cc737556d401 Mon Sep 17 00:00:00 2001 From: antiochp <30642645+antiochp@users.noreply.github.com> Date: Tue, 10 Mar 2020 14:13:53 +0000 Subject: [PATCH 3/3] cleanup --- chain/src/pipe.rs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/chain/src/pipe.rs b/chain/src/pipe.rs index 1f40c55d82..8fd4c38b5f 100644 --- a/chain/src/pipe.rs +++ b/chain/src/pipe.rs @@ -156,9 +156,10 @@ pub fn process_block(b: &Block, ctx: &mut BlockContext<'_>) -> Result