From e7cc2ad4601e5bf0f809072d77718d5823db96be Mon Sep 17 00:00:00 2001 From: Fabio Barone Date: Wed, 19 Apr 2023 11:48:05 -0500 Subject: [PATCH] rustfmt --- src/blockchain/.block_store.rs.rustfmt | 110 ++++++++++++------------- src/blockchain/block_store.rs | 14 ++-- 2 files changed, 62 insertions(+), 62 deletions(-) diff --git a/src/blockchain/.block_store.rs.rustfmt b/src/blockchain/.block_store.rs.rustfmt index ecf3dab00364..500e1b0f0630 100644 --- a/src/blockchain/.block_store.rs.rustfmt +++ b/src/blockchain/.block_store.rs.rustfmt @@ -24,10 +24,7 @@ use crate::{ Error, Result, }; -use darkfi_sdk::{ - crypto::{MerkleNode}, - pasta::pallas, -}; +use darkfi_sdk::{crypto::MerkleNode, pasta::pallas}; const SLED_HEADER_TREE: &[u8] = b"_headers"; const SLED_BLOCK_TREE: &[u8] = b"_blocks"; @@ -93,7 +90,7 @@ impl HeaderStore { } else { if strict { let s = hash.to_hex().as_str().to_string(); - return Err(Error::HeaderNotFound(s)) + return Err(Error::HeaderNotFound(s)); } ret.push(None); } @@ -121,8 +118,7 @@ impl HeaderStore { /// The `BlockStore` is a `sled` tree storing all the blockchain's blocks /// where the key is the blocks' hash, and value is the serialized block. -#[derive(Clone)] -#[derive(Debug)] +#[derive(Clone, Debug)] pub struct BlockStore(sled::Tree); impl BlockStore { @@ -183,7 +179,7 @@ impl BlockStore { } else { if strict { let s = hash.to_hex().as_str().to_string(); - return Err(Error::BlockNotFound(s)) + return Err(Error::BlockNotFound(s)); } ret.push(None); } @@ -265,7 +261,7 @@ impl BlockOrderStore { ret.push(Some(hash)); } else { if strict { - return Err(Error::SlotNotFound(*slot)) + return Err(Error::SlotNotFound(*slot)); } ret.push(None); } @@ -307,9 +303,9 @@ impl BlockOrderStore { let blockhash = deserialize(&found.1)?; ret.push(blockhash); counter += 1; - continue + continue; } - break + break; } Ok(ret) @@ -342,48 +338,52 @@ impl BlockOrderStore { #[cfg(test)] mod tests { - // Note this useful idiom: importing names from outer (for mod tests) scope. - use super::*; - - fn create_tmp_db() -> Result { - let db = sled::Config::new().temporary(true).open()?; - return BlockStore::new(&db,Timestamp::current_time(),blake3::hash(b"unit-testing-blockstore")); - } - - #[test] - fn test_new() { - let bs = create_tmp_db(); - assert_eq!(bs.is_ok(), true); - } - - #[test] - fn test_insert() { - let res = create_tmp_db(); - let bs = res.unwrap(); - let txs:Vec = gen_hashes(3); - let merkle = MerkleNode::new(pallas::Base::from(42)); - let blk = Block::new(blake3::hash(b"some-block"),1,0,txs, merkle, LeadInfo::default()); - let hres = bs.insert(&[blk]); - let hh = hres.unwrap(); - let first = hh[0]; - - let mut contains = bs.contains(&first); - assert_eq!(contains.is_ok(), true); - let badhash = blake3::hash(b"blabla"); - contains = bs.contains(&badhash); - assert_eq!(contains.is_ok(), true); - let v=contains.unwrap(); - assert_eq!(v, false); - let g = bs.get(&[first], true); - assert_eq!(g.is_ok(), true); - } - - fn gen_hashes(num:u64) -> Vec { - let mut v:Vec = Vec::new(); - for n in 0..num { - let h = blake3::hash(&n.to_be_bytes()); - v.push(h); - } - return v; - } + // Note this useful idiom: importing names from outer (for mod tests) scope. + use super::*; + + fn create_tmp_db() -> Result { + let db = sled::Config::new().temporary(true).open()?; + return BlockStore::new( + &db, + Timestamp::current_time(), + blake3::hash(b"unit-testing-blockstore"), + ); + } + + #[test] + fn test_new() { + let bs = create_tmp_db(); + assert_eq!(bs.is_ok(), true); + } + + #[test] + fn test_insert() { + let res = create_tmp_db(); + let bs = res.unwrap(); + let txs: Vec = gen_hashes(3); + let merkle = MerkleNode::new(pallas::Base::from(42)); + let blk = Block::new(blake3::hash(b"some-block"), 1, 0, txs, merkle, LeadInfo::default()); + let hres = bs.insert(&[blk]); + let hh = hres.unwrap(); + let first = hh[0]; + + let mut contains = bs.contains(&first); + assert_eq!(contains.is_ok(), true); + let badhash = blake3::hash(b"blabla"); + contains = bs.contains(&badhash); + assert_eq!(contains.is_ok(), true); + let v = contains.unwrap(); + assert_eq!(v, false); + let g = bs.get(&[first], true); + assert_eq!(g.is_ok(), true); + } + + fn gen_hashes(num: u64) -> Vec { + let mut v: Vec = Vec::new(); + for n in 0..num { + let h = blake3::hash(&n.to_be_bytes()); + v.push(h); + } + return v; + } } diff --git a/src/blockchain/block_store.rs b/src/blockchain/block_store.rs index 500e1b0f0630..6803c97870fd 100644 --- a/src/blockchain/block_store.rs +++ b/src/blockchain/block_store.rs @@ -90,7 +90,7 @@ impl HeaderStore { } else { if strict { let s = hash.to_hex().as_str().to_string(); - return Err(Error::HeaderNotFound(s)); + return Err(Error::HeaderNotFound(s)) } ret.push(None); } @@ -179,7 +179,7 @@ impl BlockStore { } else { if strict { let s = hash.to_hex().as_str().to_string(); - return Err(Error::BlockNotFound(s)); + return Err(Error::BlockNotFound(s)) } ret.push(None); } @@ -261,7 +261,7 @@ impl BlockOrderStore { ret.push(Some(hash)); } else { if strict { - return Err(Error::SlotNotFound(*slot)); + return Err(Error::SlotNotFound(*slot)) } ret.push(None); } @@ -303,9 +303,9 @@ impl BlockOrderStore { let blockhash = deserialize(&found.1)?; ret.push(blockhash); counter += 1; - continue; + continue } - break; + break } Ok(ret) @@ -347,7 +347,7 @@ mod tests { &db, Timestamp::current_time(), blake3::hash(b"unit-testing-blockstore"), - ); + ) } #[test] @@ -384,6 +384,6 @@ mod tests { let h = blake3::hash(&n.to_be_bytes()); v.push(h); } - return v; + return v } }