Skip to content

Commit 1704179

Browse files
committed
Fix rustfmt/clippy issues
1 parent 7776c53 commit 1704179

File tree

3 files changed

+42
-33
lines changed

3 files changed

+42
-33
lines changed

lib/types/hashes.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -241,22 +241,22 @@ where
241241
T: BorshSerialize + ?Sized,
242242
{
243243
use smallvec::SmallVec;
244-
244+
245245
thread_local! {
246246
// Thread-local scratch buffer that starts with 256 bytes on the stack
247247
// and grows as needed. This avoids heap allocations for most transactions.
248-
static SCRATCH_BUFFER: std::cell::RefCell<SmallVec<[u8; 256]>> =
248+
static SCRATCH_BUFFER: std::cell::RefCell<SmallVec<[u8; 256]>> =
249249
std::cell::RefCell::new(SmallVec::new());
250250
}
251-
251+
252252
SCRATCH_BUFFER.with(|buffer| {
253253
let mut buffer = buffer.borrow_mut();
254254
buffer.clear(); // Reuse the buffer
255-
255+
256256
// Serialize directly into the reused buffer
257257
borsh::to_writer(&mut *buffer, data)
258258
.expect("failed to serialize with borsh to compute a hash");
259-
259+
260260
blake3::hash(&buffer).into()
261261
})
262262
}

lib/types/mod.rs

Lines changed: 37 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -534,7 +534,6 @@ struct CbmtNode {
534534
index: usize,
535535
}
536536

537-
538537
impl PartialOrd for CbmtNode {
539538
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
540539
Some(self.cmp(other))
@@ -561,12 +560,13 @@ impl merkle_cbt::merkle_tree::Merge for MergeFeeSizeTotal {
561560
// see https://github.com/nervosnetwork/merkle-tree/blob/5d1898263e7167560fdaa62f09e8d52991a1c712/README.md#tree-struct
562561
assert_eq!(lnode.index + 1, rnode.index);
563562
let index = (lnode.index - 1) / 2;
564-
let commitment = hashes::hash_with_scratch_buffer(&CbmtNodePreCommitment {
565-
left_commitment: lnode.commitment,
566-
fees,
567-
canonical_size,
568-
right_commitment: rnode.commitment,
569-
});
563+
let commitment =
564+
hashes::hash_with_scratch_buffer(&CbmtNodePreCommitment {
565+
left_commitment: lnode.commitment,
566+
fees,
567+
canonical_size,
568+
right_commitment: rnode.commitment,
569+
});
570570
Self::Item {
571571
commitment,
572572
fees,
@@ -658,10 +658,10 @@ impl Body {
658658
..
659659
} = {
660660
let n_txs = txs.len();
661-
661+
662662
// Pre-allocate Vec for direct indexing by parallel threads
663663
let mut leaves = vec![CbmtNode::default(); n_txs];
664-
664+
665665
// Use Rayon to compute leaves in parallel across all CPU cores
666666
use rayon::prelude::*;
667667
let results: Result<Vec<_>, ComputeMerkleRootError> = txs
@@ -681,7 +681,9 @@ impl Body {
681681
tx: &tx.transaction,
682682
};
683683
let node = CbmtNode {
684-
commitment: hashes::hash_with_scratch_buffer(&leaf_pre_commitment),
684+
commitment: hashes::hash_with_scratch_buffer(
685+
&leaf_pre_commitment,
686+
),
685687
fees,
686688
canonical_size,
687689
// see https://github.com/nervosnetwork/merkle-tree/blob/5d1898263e7167560fdaa62f09e8d52991a1c712/README.md#tree-struct
@@ -690,12 +692,12 @@ impl Body {
690692
Ok((idx, node))
691693
})
692694
.collect();
693-
695+
694696
// Fill the pre-allocated vector with computed nodes
695697
for (idx, node) in results? {
696698
leaves[idx] = node;
697699
}
698-
700+
699701
// Replace tree-based CBMT with parallel levelized approach for better performance
700702
if n_txs >= 1000 {
701703
// Use optimized parallel level merging for large transaction sets
@@ -714,35 +716,40 @@ impl Body {
714716

715717
/// Optimized level-by-level parallel merkle tree construction
716718
/// Processes each level of the tree in parallel for maximum performance
717-
fn compute_merkle_root_levelized_parallel(mut current_level: Vec<CbmtNode>) -> CbmtNode {
719+
fn compute_merkle_root_levelized_parallel(
720+
mut current_level: Vec<CbmtNode>,
721+
) -> CbmtNode {
718722
use rayon::prelude::*;
719-
723+
720724
while current_level.len() > 1 {
721725
// Handle odd number of nodes by duplicating the last node (standard merkle tree approach)
722726
if current_level.len() % 2 == 1 {
723727
let last_node = current_level.last().unwrap().clone();
724728
current_level.push(last_node);
725729
}
726-
730+
727731
// Process pairs of nodes in parallel across all CPU cores
728732
current_level = current_level
729733
.par_chunks_exact(2)
730734
.enumerate()
731735
.map(|(parent_idx, pair)| {
732736
let lnode = &pair[0];
733737
let rnode = &pair[1];
734-
738+
735739
// Compute parent node using the same merge logic as MergeFeeSizeTotal
736740
let fees = lnode.fees + rnode.fees;
737-
let canonical_size = lnode.canonical_size + rnode.canonical_size;
738-
739-
let commitment = hashes::hash_with_scratch_buffer(&CbmtNodePreCommitment {
740-
left_commitment: lnode.commitment,
741-
fees,
742-
canonical_size,
743-
right_commitment: rnode.commitment,
744-
});
745-
741+
let canonical_size =
742+
lnode.canonical_size + rnode.canonical_size;
743+
744+
let commitment = hashes::hash_with_scratch_buffer(
745+
&CbmtNodePreCommitment {
746+
left_commitment: lnode.commitment,
747+
fees,
748+
canonical_size,
749+
right_commitment: rnode.commitment,
750+
},
751+
);
752+
746753
CbmtNode {
747754
commitment,
748755
fees,
@@ -752,9 +759,12 @@ impl Body {
752759
})
753760
.collect();
754761
}
755-
762+
756763
// Return the root node
757-
current_level.into_iter().next().expect("Tree should have exactly one root")
764+
current_level
765+
.into_iter()
766+
.next()
767+
.expect("Tree should have exactly one root")
758768
}
759769

760770
#[cfg(feature = "utreexo")]

lib/types/proto.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -835,7 +835,6 @@ pub mod mainchain {
835835
prev_block_hash,
836836
height,
837837
work,
838-
timestamp: _timestamp,
839838
} = header_info;
840839
let block_hash = block_hash
841840
.as_ref()

0 commit comments

Comments
 (0)