fn cas_consistency_basic()

in merkledb/src/tests.rs [98:153]


    fn cas_consistency_basic() {
        // make a bunch of random chunks
        // 128k of chunks with seed=0
        // 128k of chunks with seed=1
        // 128k of chunks with seed=2
        // 128k of chunks with seed=3
        // and repeat a bunch of times
        let mut chunks1234: Vec<ChunkInfo> = Vec::new();

        for seed in 0..16_u64 {
            let mut ch = generate_random_chunks(seed % 4);
            chunks1234.append(&mut ch);
        }

        // insert this into the MerkleDB

        let mut mdb = MerkleMemDB::default();
        let mut staging = mdb.start_insertion_staging();
        mdb.add_file(&mut staging, &chunks1234);
        let file_root = mdb.finalize(staging);
        assert!(mdb.all_invariant_checks());

        // check the CAS's produced
        // there should be 1 unique CAS node
        let file_recon = mdb.reconstruct_from_cas(&[file_root.clone()]).unwrap();
        assert_eq!(file_recon[0].0, *file_root.hash());
        let cas_ranges = file_recon[0].1.clone();
        // and the CAS should be chunks of seed 0..3 concatenated 4 times
        assert_eq!(cas_ranges.len(), 4);

        let unique_cas_hashes = cas_ranges
            .iter()
            .map(|x| x.hash) // get the hash
            .collect::<HashSet<_>>() // collect into a hashset
            .into_iter() // iterate through the hashset again consuming
            .collect::<Vec<_>>(); // collect into a vec

        assert_eq!(unique_cas_hashes.len(), 1);
        let cas_node_hash = unique_cas_hashes[0];

        // check that the cas node contains the string for seed=0,1,2,3
        let cas_node = mdb.find_node(&cas_node_hash).unwrap();
        assert_eq!(cas_node.len(), 4 * PER_SEED_INPUT_SIZE); // strings for seed=0,1,2,3

        // ok. validate we that the cas node comprises of exactly the chunks for
        // seed=0,1,2,3
        let cas_leaves = mdb.find_all_leaves(&cas_node).unwrap();
        let mut expected_leaves: Vec<ChunkInfo> = Vec::new();
        for seed in 0..4_u64 {
            expected_leaves.append(&mut generate_random_chunks(seed));
        }
        assert_eq!(expected_leaves.len(), cas_leaves.len());
        for i in 0..expected_leaves.len() {
            assert_eq!(expected_leaves[i].hash, *cas_leaves[i].hash());
        }
    }