From d278819cadef784cc7b04fc60c7c12e8ddf8a15d Mon Sep 17 00:00:00 2001 From: Tomas van der Wansem Date: Tue, 6 Dec 2016 10:57:06 +0100 Subject: [PATCH] Use typed block pointers --- bitcrust-lib/src/block.rs | 7 +++--- bitcrust-lib/src/store/spent_tree/mod.rs | 29 +++++++++++++++++------- 2 files changed, 25 insertions(+), 11 deletions(-) diff --git a/bitcrust-lib/src/block.rs b/bitcrust-lib/src/block.rs index 3ab2535..0c6cf50 100644 --- a/bitcrust-lib/src/block.rs +++ b/bitcrust-lib/src/block.rs @@ -117,6 +117,7 @@ impl<'a> Block<'a> { let block_hash = Hash32Buf::double_sha256(self.header.to_raw()); + // see if it exists let ptr = store.hash_index.get(block_hash.as_ref()); if ptr.iter().any(|ptr| ptr.is_blockheader()) { @@ -130,12 +131,12 @@ impl<'a> Block<'a> { // let's store the blockheader in block_content let blockheader_ptr = store.block_content.write_blockheader(&self.header); - let (spent_tree_start, spent_tree_end) = store.spent_tree.store_block(blockheader_ptr, transactions); + let block_ptr = store.spent_tree.store_block(blockheader_ptr, transactions); - let previous = store.hash_index.get_or_set(self.header.prev_hash, spent_tree_end.as_guardblock()); + let previous = store.hash_index.get_or_set(self.header.prev_hash, block_ptr.end.as_guardblock()); if let Some(previous) = previous { - store.spent_tree.set_previous(spent_tree_start, previous); + store.spent_tree.set_previous(block_ptr.start, previous); } let previous = previous .iter() diff --git a/bitcrust-lib/src/store/spent_tree/mod.rs b/bitcrust-lib/src/store/spent_tree/mod.rs index 80038d2..9461458 100644 --- a/bitcrust-lib/src/store/spent_tree/mod.rs +++ b/bitcrust-lib/src/store/spent_tree/mod.rs @@ -46,6 +46,11 @@ pub enum SpendingError { OutputAlreadySpent, } +pub struct BlockPtr { + pub start: FilePtr, + pub end: FilePtr +} + pub struct SpentTree { @@ -97,7 +102,7 @@ impl SpentTree { /// Stores a block in the spent_tree. The block will be initially orphan. /// /// The result is a pointer to the first and last record - pub fn store_block(&mut self, blockheader: FilePtr, file_ptrs: Vec) -> (FilePtr, FilePtr) { + pub fn store_block(&mut self, blockheader: FilePtr, file_ptrs: Vec) -> BlockPtr { let block = SpentTree::create_block(blockheader, file_ptrs); @@ -105,7 +110,10 @@ impl SpentTree { let result_ptr = self.fileset.write_all(&block); let end_ptr = result_ptr.offset((block.len()-1) * mem::size_of::()); - (result_ptr, end_ptr) + BlockPtr { + start: result_ptr, + end: end_ptr + } } pub fn set_previous(&mut self, target: FilePtr, previous: FilePtr) { @@ -121,6 +129,7 @@ mod tests { extern crate tempdir; use store::fileptr::FilePtr; use std::path::PathBuf; + use config; use super::*; @@ -143,9 +152,9 @@ mod tests { $( [tx $tx:expr $(=> $( ($tx_in:expr;$tx_in_idx:expr) ),+)*] ),+ ) => - ( ( FilePtr::new(0,$header), + ( ( FilePtr::new(0,$header), vec![ $( FilePtr::new(0,$tx), $( $( FilePtr::new(0,$tx_in).as_input($tx_in_idx) ),+ )* ),+ - ) + ]) ) } @@ -159,12 +168,16 @@ mod tests { [tx 3] ); -/* + let dir = tempdir::TempDir::new("test1").unwrap(); - let cfg = config::Config { root: PathBuf::from(dir.path) }; + //let path = PathBuf::from(dir.path()); + let cfg = config::Config { root: PathBuf::from(dir.path()) }; + + let mut st = SpentTree::new(&cfg); + + + st.store_block(block1.0, block1.1); - let st = SpentTree::new(&cfg); - (*/