Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: ensures mutable MMR bitmaps are compressed #5278

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -606,7 +606,6 @@ impl<'a, B: BlockchainBackend + 'static> HorizonStateSynchronization<'a, B> {
// in the output MMR
let bitmap = self.full_bitmap_mut();
bitmap.or_inplace(&diff_bitmap);
bitmap.run_optimize();

let pruned_output_set = output_mmr.get_pruned_hash_set()?;
let output_mmr = MutablePrunedOutputMmr::new(pruned_output_set.clone(), bitmap.clone())?;
Expand Down
2 changes: 0 additions & 2 deletions base_layer/core/src/chain_storage/blockchain_database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1360,8 +1360,6 @@ pub fn calculate_mmr_roots<T: BlockchainBackend>(
}
}

output_mmr.compress();

let block_height = block.header.height;
let epoch_len = rules.consensus_constants(block_height).epoch_length();
let validator_node_mr = if block_height % epoch_len == 0 {
Expand Down
1 change: 0 additions & 1 deletion base_layer/mmr/src/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ where
for index in deletions {
pruned_mmr.delete(index);
}
pruned_mmr.compress();
pruned_mmr.get_merkle_root()
}

Expand Down
18 changes: 10 additions & 8 deletions base_layer/mmr/src/mutable_mmr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,18 @@ where
// virtue of the fact that the underlying MMRs could be different, but all elements are marked as deleted in
// both sets.
let mmr_root = self.mmr.get_merkle_root()?;

// To avoid requiring mutability, we compress a clone of the bitmap
let mut bitmap = self.deleted.clone();
bitmap.run_optimize();
let bitmap_ser = bitmap.serialize();

// Include the compressed bitmap in the root hash
let mut hasher = D::new();
hasher.update(&mmr_root);
Ok(self.hash_deleted(hasher).finalize().to_vec())
hasher.update(&bitmap_ser);

Ok(hasher.finalize().to_vec())
}

/// Returns only the MMR merkle root without the compressed serialisation of the bitmap
Expand Down Expand Up @@ -197,13 +206,6 @@ where
self.mmr.validate()
}

/// Hash the roaring bitmap of nodes that are marked for deletion
fn hash_deleted(&self, mut hasher: D) -> D {
let bitmap_ser = self.deleted.serialize();
hasher.update(&bitmap_ser);
hasher
}

// Returns a bitmap with only the deleted nodes for the specified region in the MMR.
fn get_sub_bitmap(&self, leaf_index: LeafIndex, count: usize) -> Result<Bitmap, MerkleMountainRangeError> {
let mut deleted = self.deleted.clone();
Expand Down