diff --git a/chain/src/sumtree.rs b/chain/src/sumtree.rs index a70bb75cb..3abdceba6 100644 --- a/chain/src/sumtree.rs +++ b/chain/src/sumtree.rs @@ -468,9 +468,8 @@ impl<'a> Extension<'a> { // rewind the kernel file store, the position is the number of kernels // multiplied by their size - // the number of kernels is the number of leaves in the MMR, which is the - // sum of the number of leaf nodes under each peak in the MMR - let pos: u64 = pmmr::peaks(kern_pos_rew).iter().map(|n| (1 << n) as u64).sum(); + // the number of kernels is the number of leaves in the MMR + let pos = pmmr::n_leaves(kern_pos_rew); self.kernel_file.rewind(pos * (TxKernel::size() as u64)); Ok(()) @@ -618,9 +617,7 @@ impl<'a> Extension<'a> { // make sure we have the right count of kernels using the MMR, the storage // file may have a few more let mmr_sz = self.kernel_pmmr.unpruned_size(); - let count: u64 = pmmr::peaks(mmr_sz).iter().map(|n| { - (1 << pmmr::bintree_postorder_height(*n)) as u64 - }).sum(); + let count = pmmr::n_leaves(mmr_sz); let mut kernel_file = File::open(self.kernel_file.path())?; let first: TxKernel = ser::deserialize(&mut kernel_file)?; diff --git a/core/src/core/pmmr.rs b/core/src/core/pmmr.rs index 766bf6485..e1406ecdd 100644 --- a/core/src/core/pmmr.rs +++ b/core/src/core/pmmr.rs @@ -661,6 +661,13 @@ pub fn peaks(num: u64) -> Vec { peaks } +/// The number of leaves nodes in a MMR of the provided size. +pub fn n_leaves(sz: u64) -> u64 { + peaks(sz).iter().map(|n| { + (1 << bintree_postorder_height(*n)) as u64 + }).sum() +} + /// The height of a node in a full binary tree from its postorder traversal /// index. This function is the base on which all others, as well as the MMR, /// are built.