diff options
| -rw-r--r-- | src/blocks.rs | 14 | ||||
| -rw-r--r-- | src/hash_chain.rs | 8 | ||||
| -rw-r--r-- | src/network.rs | 2 |
3 files changed, 9 insertions, 15 deletions
diff --git a/src/blocks.rs b/src/blocks.rs index 53f9326..d1f287a 100644 --- a/src/blocks.rs +++ b/src/blocks.rs @@ -4,11 +4,7 @@ use std::ops::Deref; use serde::{Deserialize, Serialize}; -use crate::{ - ZERO_PREFIX_AMOUNT, - hash_chain::{self, HashChain}, - transaction::Transaction, -}; +use crate::{ZERO_PREFIX_AMOUNT, hash_chain::HashChain, transaction::Transaction}; use std::sync::{ Arc, @@ -134,17 +130,15 @@ impl BlockChain { } pub fn new_block(&self, trans: Transaction) -> Block { - let mut hasher = Sha256::new(); let prev = self .blocks .last(); let prev_hash = if let Some(b) = prev { - b.digest(&mut hasher); - Some(&*hasher.finalize()) + Some(HashChain::hash_o_item(b)) } else { None }; - Block::new(prev_hash, trans) + Block::new(prev_hash.as_deref(), trans) } pub fn store(&mut self, block: Block) { @@ -159,7 +153,7 @@ impl BlockChain { } impl Deref for BlockChain { - type Target = Vec<Block>; + type Target = HashChain<Block>; fn deref(&self) -> &Self::Target { &self.blocks diff --git a/src/hash_chain.rs b/src/hash_chain.rs index 0fe2c1d..f21b1d2 100644 --- a/src/hash_chain.rs +++ b/src/hash_chain.rs @@ -26,7 +26,7 @@ impl<T: Encode> HashChain<T> { this } - fn push(&mut self, data: T) { + pub fn push(&mut self, data: T) { let prev_hash = self .data .last() @@ -40,7 +40,7 @@ impl<T: Encode> HashChain<T> { // TODO: make try_last that does same as this but returns Err() if it's not the // last one and/or refuse to insert - fn insert(&mut self, data: T, base_hash: &Vec<u8>) { + pub fn insert(&mut self, data: T, base_hash: &Vec<u8>) { let pos = self .data .iter() @@ -53,7 +53,7 @@ impl<T: Encode> HashChain<T> { self.rehash(&base_hash); } - fn verify(&self, until_hash: &[u8]) -> bool { + pub fn verify(&self, until_hash: &[u8]) -> bool { let pos = self .data .iter() @@ -68,7 +68,7 @@ impl<T: Encode> HashChain<T> { }) } - fn rehash(&mut self, from_hash: &[u8]) { + pub fn rehash(&mut self, from_hash: &[u8]) { let pos = self .data .iter() diff --git a/src/network.rs b/src/network.rs index 4c3d2f6..08693c5 100644 --- a/src/network.rs +++ b/src/network.rs @@ -286,7 +286,7 @@ impl Peer { // TODO: Optimize via try_fold .fold(true, |i, b| { if i { - b.pow + b.1.pow .is_none() } else { false |
