Add method calc_height
Add a private method to the `PartialMerkleTree` to calculate the height. Enables removal of duplicate code.
This commit is contained in:
parent
46f5588646
commit
752adff9d1
|
@ -250,11 +250,8 @@ impl PartialMerkleTree {
|
||||||
bits: Vec::with_capacity(txids.len()),
|
bits: Vec::with_capacity(txids.len()),
|
||||||
hashes: vec![],
|
hashes: vec![],
|
||||||
};
|
};
|
||||||
// calculate height of tree
|
let height = pmt.calc_tree_height();
|
||||||
let mut height = 0;
|
|
||||||
while pmt.calc_tree_width(height) > 1 {
|
|
||||||
height += 1;
|
|
||||||
}
|
|
||||||
// traverse the partial tree
|
// traverse the partial tree
|
||||||
pmt.traverse_and_build(height, 0, txids, matches);
|
pmt.traverse_and_build(height, 0, txids, matches);
|
||||||
pmt
|
pmt
|
||||||
|
@ -286,11 +283,9 @@ impl PartialMerkleTree {
|
||||||
if self.bits.len() < self.hashes.len() {
|
if self.bits.len() < self.hashes.len() {
|
||||||
return Err(NotEnoughBits);
|
return Err(NotEnoughBits);
|
||||||
};
|
};
|
||||||
// calculate height of tree
|
|
||||||
let mut height = 0;
|
let height = self.calc_tree_height();
|
||||||
while self.calc_tree_width(height) > 1 {
|
|
||||||
height += 1;
|
|
||||||
}
|
|
||||||
// traverse the partial tree
|
// traverse the partial tree
|
||||||
let mut bits_used = 0u32;
|
let mut bits_used = 0u32;
|
||||||
let mut hash_used = 0u32;
|
let mut hash_used = 0u32;
|
||||||
|
@ -308,6 +303,15 @@ impl PartialMerkleTree {
|
||||||
Ok(TxMerkleNode::from_byte_array(hash_merkle_root.to_byte_array()))
|
Ok(TxMerkleNode::from_byte_array(hash_merkle_root.to_byte_array()))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Calculates the height of the tree.
|
||||||
|
fn calc_tree_height(&self) -> u32 {
|
||||||
|
let mut height = 0;
|
||||||
|
while self.calc_tree_width(height) > 1 {
|
||||||
|
height += 1;
|
||||||
|
}
|
||||||
|
height
|
||||||
|
}
|
||||||
|
|
||||||
/// Helper function to efficiently calculate the number of nodes at given height
|
/// Helper function to efficiently calculate the number of nodes at given height
|
||||||
/// in the merkle tree
|
/// in the merkle tree
|
||||||
#[inline]
|
#[inline]
|
||||||
|
|
Loading…
Reference in New Issue