Skip to content

Commit

Permalink
Use typed block pointers
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasvdw committed Dec 6, 2016
1 parent 1cf7de0 commit d278819
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 11 deletions.
7 changes: 4 additions & 3 deletions bitcrust-lib/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand All @@ -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()
Expand Down
29 changes: 21 additions & 8 deletions bitcrust-lib/src/store/spent_tree/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ pub enum SpendingError {
OutputAlreadySpent,
}

pub struct BlockPtr {
pub start: FilePtr,
pub end: FilePtr
}


pub struct SpentTree {

Expand Down Expand Up @@ -97,15 +102,18 @@ 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, FilePtr) {
pub fn store_block(&mut self, blockheader: FilePtr, file_ptrs: Vec<FilePtr>) -> BlockPtr {

let block = SpentTree::create_block(blockheader, file_ptrs);


let result_ptr = self.fileset.write_all(&block);
let end_ptr = result_ptr.offset((block.len()-1) * mem::size_of::<Record>());

(result_ptr, end_ptr)
BlockPtr {
start: result_ptr,
end: end_ptr
}
}

pub fn set_previous(&mut self, target: FilePtr, previous: FilePtr) {
Expand All @@ -121,6 +129,7 @@ mod tests {
extern crate tempdir;
use store::fileptr::FilePtr;
use std::path::PathBuf;
use config;

use super::*;

Expand All @@ -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) ),+ )* ),+
)
])
)

}
Expand All @@ -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);
(*/



Expand Down

0 comments on commit d278819

Please sign in to comment.