Skip to content

Commit

Permalink
code quality:Fix code simplification clippy warnings (#371)
Browse files Browse the repository at this point in the history
Fixes some clippy warnings and remove unused code
  • Loading branch information
siddheshzz authored Feb 12, 2025
1 parent 7083de4 commit 121e3ea
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 58 deletions.
20 changes: 11 additions & 9 deletions crates/floresta-chain/src/pruned_utreexo/chain_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -666,16 +666,18 @@ impl<PersistedState: ChainStore> ChainState<PersistedState> {
}
fn verify_script(&self, height: u32) -> bool {
let inner = self.inner.read();

inner.assume_valid.map_or(true, |hash| {
match inner.chainstore.get_header(&hash).unwrap() {
// If the assume-valid block is in the best chain, only verify scripts if we are higher
Some(DiskBlockHeader::HeadersOnly(_, assume_h))
| Some(DiskBlockHeader::FullyValid(_, assume_h)) => height > assume_h,
// Assume-valid is not in the best chain, so verify all the scripts
_ => true,
match inner.assume_valid {
Some(hash) => {
match inner.chainstore.get_header(&hash).unwrap() {
// If the assume-valid block is in the best chain, only verify scripts if we are higher
Some(DiskBlockHeader::HeadersOnly(_, assume_h))
| Some(DiskBlockHeader::FullyValid(_, assume_h)) => height > assume_h,
// Assume-valid is not in the best chain, so verify all the scripts
_ => true,
}
}
})
None => true,
}
}
pub fn acc(&self) -> Stump {
read_lock!(self).acc.to_owned()
Expand Down
2 changes: 1 addition & 1 deletion crates/floresta-wire/src/p2p_wire/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ where
.fixed_peer
.as_ref()
.map(|address| {
Self::resolve_connect_host(&address, Self::get_port(config.network.into()))
Self::resolve_connect_host(address, Self::get_port(config.network.into()))
})
.transpose()?;

Expand Down
57 changes: 35 additions & 22 deletions crates/floresta-wire/src/p2p_wire/tests/sync_node.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
#[cfg(test)]
mod tests_utils {
use std::collections::HashMap;
use std::mem::ManuallyDrop;
use std::sync::Arc;
use std::time::Duration;

use bitcoin::block::Header;
use bitcoin::BlockHash;
use floresta_chain::pruned_utreexo::UpdatableChainstate;
use floresta_chain::AssumeValidArg;
use floresta_chain::ChainState;
use floresta_chain::KvChainStore;
use floresta_chain::UtreexoBlock;
use rustreexo::accumulator::pollard::Pollard;
use tokio::sync::Mutex;
use tokio::sync::RwLock;
Expand All @@ -23,13 +19,14 @@ mod tests_utils {
use crate::p2p_wire::tests::utils::create_peer;
use crate::p2p_wire::tests::utils::get_node_config;
use crate::p2p_wire::tests::utils::get_test_headers;
use crate::p2p_wire::tests::utils::BlockDataMap;
use crate::p2p_wire::tests::utils::BlockHashMap;
use crate::p2p_wire::tests::utils::HeaderList;

type PeerData = (HeaderList, BlockHashMap, BlockDataMap);

pub async fn setup_node(
peers: Vec<(
Vec<Header>,
HashMap<BlockHash, UtreexoBlock>,
HashMap<BlockHash, Vec<u8>>,
)>,
peers: Vec<PeerData>,
pow_fraud_proofs: bool,
network: floresta_chain::Network,
) -> Arc<ChainState<KvChainStore<'static>>> {
Expand Down Expand Up @@ -102,16 +99,19 @@ mod tests {

#[tokio::test]
async fn test_sync_valid_blocks() {
let (headers, blocks, _, _, _) = get_essentials();
let essentials = get_essentials();
let chain = setup_node(
vec![(Vec::new(), blocks.clone(), HashMap::new())],
vec![(Vec::new(), essentials.blocks.clone(), HashMap::new())],
false,
floresta_chain::Network::Signet,
)
.await;

assert_eq!(chain.get_validation_index().unwrap(), 9);
assert_eq!(chain.get_best_block().unwrap().1, headers[9].block_hash());
assert_eq!(
chain.get_best_block().unwrap().1,
essentials.headers[9].block_hash()
);
assert!(!chain.is_in_idb());
}

Expand All @@ -123,14 +123,19 @@ mod tests {
// THIS SIMULATION WILL TEST:
// 1) SENDING BLOCK WITH A BADMERKLEROOT: 7TH BLOCK WILL BE INVALIDATED.

let (headers, mut blocks, _, _, invalid_block) = get_essentials();
let mut essentials = get_essentials();

blocks.insert(headers[7].block_hash(), invalid_block);
let peer = vec![(Vec::new(), blocks.clone(), HashMap::new())];
essentials
.blocks
.insert(essentials.headers[7].block_hash(), essentials.invalid_block);
let peer = vec![(Vec::new(), essentials.blocks.clone(), HashMap::new())];
let chain = setup_node(peer, false, floresta_chain::Network::Signet).await;

assert_eq!(chain.get_validation_index().unwrap(), 6);
assert_eq!(chain.get_best_block().unwrap().1, headers[6].block_hash());
assert_eq!(
chain.get_best_block().unwrap().1,
essentials.headers[6].block_hash()
);
assert!(!chain.is_in_idb());
}

Expand All @@ -146,17 +151,22 @@ mod tests {
//
// SO FINALLY THE LAST VALIDATED BLOCK WILL BE 9.

let (headers, mut blocks, _, _, _) = get_essentials();
let v_blocks = blocks.clone();
let mut essentials = get_essentials();
let v_blocks = essentials.blocks.clone();

let u_block = blocks.get(&headers[3].block_hash().clone()).unwrap();
let u_block = essentials
.blocks
.get(&essentials.headers[3].block_hash().clone())
.unwrap();
let block = UtreexoBlock {
block: u_block.block.clone(),
udata: None,
};
blocks.insert(headers[3].block_hash(), block);
essentials
.blocks
.insert(essentials.headers[3].block_hash(), block);

let liar = (Vec::new(), blocks, HashMap::new());
let liar = (Vec::new(), essentials.blocks, HashMap::new());
let honest1 = (Vec::new(), v_blocks.clone(), HashMap::new());
let honest2 = (Vec::new(), v_blocks, HashMap::new());

Expand All @@ -168,7 +178,10 @@ mod tests {
.await;

assert_eq!(chain.get_validation_index().unwrap(), 9);
assert_eq!(chain.get_best_block().unwrap().1, headers[9].block_hash());
assert_eq!(
chain.get_best_block().unwrap().1,
essentials.headers[9].block_hash()
);
assert!(!chain.is_in_idb());
}
}
51 changes: 25 additions & 26 deletions crates/floresta-wire/src/p2p_wire/tests/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@ use bitcoin::p2p::ServiceFlags;
use bitcoin::BlockHash;
use floresta_chain::UtreexoBlock;
use floresta_common::service_flags;
use hex;
use rand::rngs::OsRng;
use rand::RngCore;
use serde::Deserialize;
use serde::Serialize;
use tokio::sync::mpsc::UnboundedReceiver;
Expand All @@ -32,6 +29,23 @@ use crate::p2p_wire::peer::PeerMessages;
use crate::p2p_wire::peer::Version;
use crate::UtreexoNodeConfig;

/// A list of headers, used to represent the collection of headers.
pub type HeaderList = Vec<Header>;

/// A map that associates block hashes with their corresponding `UtreexoBlock` objects.
/// This is useful for efficiently looking up blocks by their hash.
pub type BlockHashMap = HashMap<BlockHash, UtreexoBlock>;

/// A map of block hashes to raw block data (represented as bytes vector).
pub type BlockDataMap = HashMap<BlockHash, Vec<u8>>;

/// A collection of essential data related to blocks and headers.
pub struct Essentials {
pub headers: HeaderList,
pub blocks: BlockHashMap,
pub invalid_block: UtreexoBlock,
}

#[derive(Debug, Deserialize, Serialize, Clone)]
pub struct UtreexoRoots {
roots: Option<Vec<String>>,
Expand Down Expand Up @@ -188,19 +202,6 @@ pub fn serialize(root: UtreexoRoots) -> Vec<u8> {
buffer
}

pub fn create_false_acc(tip: usize) -> Vec<u8> {
let mut bytes = [0u8; 32];
OsRng.fill_bytes(&mut bytes);
let node_hash = hex::encode(bytes);

let utreexo_root = UtreexoRoots {
roots: Some(vec![node_hash]),
numleaves: tip,
};

serialize(utreexo_root)
}

pub fn get_test_headers() -> Vec<Header> {
let mut headers: Vec<Header> = Vec::new();

Expand Down Expand Up @@ -260,22 +261,20 @@ pub fn generate_invalid_block() -> UtreexoBlock {
block
}

pub fn get_essentials() -> (
Vec<Header>,
HashMap<BlockHash, UtreexoBlock>,
HashMap<BlockHash, Vec<u8>>,
BlockHash,
UtreexoBlock,
) {
pub fn get_essentials() -> Essentials {
let headers = get_test_headers();
let blocks = get_test_blocks().unwrap();
let true_filters = get_test_filters().unwrap();
let _filters = get_test_filters().unwrap();
let invalid_block = generate_invalid_block();

// BlockHash of chain_tip: 0000035f0e5513b26bba7cead874fdf06241a934e4bc4cf7a0381c60e4cdd2bb (119)
let tip_hash =
let _tip_hash =
BlockHash::from_str("0000035f0e5513b26bba7cead874fdf06241a934e4bc4cf7a0381c60e4cdd2bb")
.unwrap();

(headers, blocks, true_filters, tip_hash, invalid_block)
Essentials {
headers,
blocks,
invalid_block,
}
}

0 comments on commit 121e3ea

Please sign in to comment.