Factoring in mmr leaf count function

This commit is contained in:
Ignotus Peverell 2018-02-19 23:20:32 +00:00
parent 4022b82817
commit 866ec977da
No known key found for this signature in database
GPG key ID: 99CD25F39F8F8211
2 changed files with 10 additions and 6 deletions

View file

@ -468,9 +468,8 @@ impl<'a> Extension<'a> {
// rewind the kernel file store, the position is the number of kernels // rewind the kernel file store, the position is the number of kernels
// multiplied by their size // multiplied by their size
// the number of kernels is the number of leaves in the MMR, which is the // the number of kernels is the number of leaves in the MMR
// sum of the number of leaf nodes under each peak in the MMR let pos = pmmr::n_leaves(kern_pos_rew);
let pos: u64 = pmmr::peaks(kern_pos_rew).iter().map(|n| (1 << n) as u64).sum();
self.kernel_file.rewind(pos * (TxKernel::size() as u64)); self.kernel_file.rewind(pos * (TxKernel::size() as u64));
Ok(()) Ok(())
@ -618,9 +617,7 @@ impl<'a> Extension<'a> {
// make sure we have the right count of kernels using the MMR, the storage // make sure we have the right count of kernels using the MMR, the storage
// file may have a few more // file may have a few more
let mmr_sz = self.kernel_pmmr.unpruned_size(); let mmr_sz = self.kernel_pmmr.unpruned_size();
let count: u64 = pmmr::peaks(mmr_sz).iter().map(|n| { let count = pmmr::n_leaves(mmr_sz);
(1 << pmmr::bintree_postorder_height(*n)) as u64
}).sum();
let mut kernel_file = File::open(self.kernel_file.path())?; let mut kernel_file = File::open(self.kernel_file.path())?;
let first: TxKernel = ser::deserialize(&mut kernel_file)?; let first: TxKernel = ser::deserialize(&mut kernel_file)?;

View file

@ -661,6 +661,13 @@ pub fn peaks(num: u64) -> Vec<u64> {
peaks 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 /// 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, /// index. This function is the base on which all others, as well as the MMR,
/// are built. /// are built.