Skip to content
Closed
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
3 changes: 1 addition & 2 deletions src/util_types/mutator_set/archival_mutator_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -410,8 +410,7 @@ where

// insert all indices
let mut new_target_chunks: ChunkDictionary = removal_record.target_chunks.clone();
let chunkindices_to_indices_dict: HashMap<u64, Vec<u128>> =
removal_record.get_chunkidx_to_indices_dict();
let chunkindices_to_indices_dict = removal_record.get_chunkidx_to_indices_dict();

for (chunk_index, indices) in chunkindices_to_indices_dict {
if chunk_index >= batch_index as u64 {
Expand Down
3 changes: 1 addition & 2 deletions src/util_types/mutator_set/mutator_set_accumulator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,7 @@ impl MutatorSetAccumulator {

// insert all indices
let mut new_target_chunks: ChunkDictionary = removal_record.target_chunks.clone();
let chunkindices_to_indices_dict: HashMap<u64, Vec<u128>> =
removal_record.get_chunkidx_to_indices_dict();
let chunkindices_to_indices_dict = removal_record.get_chunkidx_to_indices_dict();

for (chunk_index, indices) in chunkindices_to_indices_dict {
if chunk_index >= batch_index {
Expand Down
40 changes: 24 additions & 16 deletions src/util_types/mutator_set/removal_record.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::collections::BTreeMap;
use std::collections::HashMap;
use std::collections::HashSet;
use std::marker::PhantomData;
Expand All @@ -23,7 +24,7 @@ use twenty_first::util_types::mmr::mmr_trait::Mmr;
use super::chunk_dictionary::ChunkDictionary;
use super::mutator_set_accumulator::MutatorSetAccumulator;
use super::shared::get_batch_mutation_argument_for_removal_record;
use super::shared::indices_to_hash_map;
use super::shared::indices_to_map;
use super::shared::BATCH_SIZE;
use super::shared::CHUNK_SIZE;
use super::shared::NUM_TRIALS;
Expand Down Expand Up @@ -80,9 +81,9 @@ impl AbsoluteIndexSet {
pub fn split_by_activity(
&self,
mutator_set: &MutatorSetAccumulator,
) -> Result<(HashMap<u64, Vec<u128>>, Vec<u128>), MutatorSetError> {
) -> Result<(BTreeMap<u64, Vec<u128>>, Vec<u128>), MutatorSetError> {
let (aw_chunk_index_min, aw_chunk_index_max) = mutator_set.active_window_chunk_interval();
let (inactive, active): (HashMap<_, _>, HashMap<_, _>) = indices_to_hash_map(&self.0)
let (inactive, active): (BTreeMap<_, _>, BTreeMap<_, _>) = indices_to_map(&self.0)
.into_iter()
.partition(|&(chunk_index, _)| chunk_index < aw_chunk_index_min);

Expand Down Expand Up @@ -364,8 +365,8 @@ impl RemovalRecord {
}

/// Returns a hashmap from chunk index to chunk.
pub fn get_chunkidx_to_indices_dict(&self) -> HashMap<u64, Vec<u128>> {
indices_to_hash_map(&self.absolute_indices.to_array())
pub fn get_chunkidx_to_indices_dict(&self) -> BTreeMap<u64, Vec<u128>> {
indices_to_map(&self.absolute_indices.to_array())
}
}

Expand All @@ -378,6 +379,8 @@ pub(crate) enum RemovalRecordValidityError {
#[cfg(test)]
mod removal_record_tests {
use itertools::Itertools;
use proptest::collection;
use proptest_arbitrary_interop::arb;
use rand::prelude::IndexedRandom;
use rand::Rng;
use rand::RngCore;
Expand Down Expand Up @@ -587,6 +590,10 @@ mod removal_record_tests {
fn removal_record_missing_chunk_element_is_invalid_pbt(
#[strategy(1u64..20*u64::from(BATCH_SIZE))] initial_additions: u64,
#[strategy(0u64..(#initial_additions as u64))] index_to_drop: u64,
#[any] to_remove_i: usize,
#[strategy(collection::vec(arb(), #initial_additions as usize))] mut item_vec: Vec<Digest>,
#[strategy(collection::vec(arb(), #initial_additions as usize))] mut sender_randomness_vec: Vec<Digest>,
#[strategy(collection::vec(arb(), #initial_additions as usize))] mut receiver_preimage_vec: Vec<Digest>,
) {
// Construct a valid removal record, verify that it is considered
// valid, then remove one element from its chunk dictionary and verify
Expand All @@ -595,7 +602,11 @@ mod removal_record_tests {
let mut mps = vec![];
let mut items = vec![];
for j in 0..initial_additions {
let (item, sender_randomness, receiver_preimage) = mock_item_and_randomnesses();
let (item, sender_randomness, receiver_preimage) = (
item_vec.pop().unwrap(),
sender_randomness_vec.pop().unwrap(),
receiver_preimage_vec.pop().unwrap(),
);
let addition_record: AdditionRecord =
commit(item, sender_randomness, receiver_preimage.hash());
let mp = accumulator.prove(item, sender_randomness, receiver_preimage);
Expand All @@ -620,19 +631,16 @@ mod removal_record_tests {
let mut rr = accumulator.drop(item, &msmp);
assert!(rr.validate(&accumulator));

// If the removal record has no indices in the inactive part of the
// Bloom filter, then continue to next test case.
let (inactive, _) = rr.absolute_indices.split_by_activity(&accumulator).unwrap();
if inactive.is_empty() {
let l = inactive.len();
if l == 0 {
// If the removal record has no indices in the inactive part of the
// Bloom filter, then continue to next test case.
return Ok(());
}

let to_remove = **inactive
.keys()
.collect_vec()
.choose(&mut rand::rng())
.unwrap();
rr.target_chunks.remove(&to_remove);
let to_remove = to_remove_i % l;
rr.target_chunks
.remove(inactive.keys().nth(to_remove).unwrap());
assert!(!rr.validate(&accumulator));
}

Expand Down
5 changes: 3 additions & 2 deletions src/util_types/mutator_set/shared.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::collections::BTreeMap;
use std::collections::HashMap;
use std::collections::HashSet;

Expand All @@ -19,11 +20,11 @@ pub const NUM_TRIALS: u32 = 45;
/// {chunk_index => absolute_indices}
/// where the values are sorted after chunk index, i.e. put in the correct
/// chunk bucket.
pub fn indices_to_hash_map(all_indices: &[u128; NUM_TRIALS as usize]) -> HashMap<u64, Vec<u128>> {
pub fn indices_to_map(all_indices: &[u128; NUM_TRIALS as usize]) -> BTreeMap<u64, Vec<u128>> {
all_indices
.iter()
.map(|bi| ((bi / u128::from(CHUNK_SIZE)) as u64, *bi))
.fold(HashMap::new(), |mut acc, (chunk_index, index)| {
.fold(BTreeMap::new(), |mut acc, (chunk_index, index)| {
acc.entry(chunk_index).or_default().push(index);
acc
})
Expand Down
Loading