Skip to content

Commit

Permalink
Update to new roaring_graphs library version
Browse files Browse the repository at this point in the history
Co-authored-by: James Walker <[email protected]>
  • Loading branch information
matheus23 and walkah committed Aug 15, 2023
1 parent 8968e86 commit 77b1f11
Show file tree
Hide file tree
Showing 6 changed files with 213 additions and 1 deletion.
86 changes: 86 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion car-mirror/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,21 @@ doc = true
anyhow = "1.0"
async-stream = "0.3.5"
bytes = "1.4.0"
fixedbitset = "0.4.2"
futures = "0.3.28"
libipld = "0.16.0"
libipld-core = "0.16.0"
proptest = { version = "1.1", optional = true }
roaring-graphs = "0.12"
tracing = "0.1"
tracing-subscriber = "0.3"
wnfs-common = "0.1.23"

[dev-dependencies]
proptest = "1.1"
async-std = { version = "1.11", features = ["attributes"] }
car-mirror = { path = ".", features = ["test_utils"] }
proptest = "1.1"
test-strategy = "0.3"

[features]
default = []
Expand Down
7 changes: 7 additions & 0 deletions car-mirror/proptest-regressions/lib.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Seeds for failure cases proptest has generated in the past. It is
# automatically read and these particular cases re-run before any
# novel cases are generated.
#
# It is recommended to check this file in to source control so that
# everyone who runs the test benefits from these saved cases.
cc ecfcb732e093de600a3a3012674efabacf88f817b47d75c0ad0da9762ca3b6f7 # shrinks to input = _WalkDagNeverIteratesBlockTwiceArgs { dag: [(Cid(bafyreigvk2vd4s7ecxqhr7vlf5ei5tpdpalx73wbt53zokomvab5ouzq2a), b"\x86\xd8*X%\0\x01q\x12 pg\xacO\xb3\xfe\xacy\xd9k\xee\xc10\xdd\x8b\xbbc\x81\xc1\x06\x12\xf5Uw\x9e\xed\x11n\r?8\xdc\xd8*X%\0\x01q\x12 \xe0\xed\xdb\xa6\x0c\x8b\xf9\xe2fk\x12\xdd3\xf9\xb9Y%[\x85\xf6\xd9\xd8\x15\x8a\xce3\xbb\xdfN\xcfM\x93\xd8*X%\0\x01q\x12 \xfei\x9fJr\x7f\xed\xfd\t\0\x02lz5\x0eD\xc5\xf9\xe2\xda\"\x9ez5y\xd5\xc8\x02\xab+\xc0\x92\xd8*X%\0\x01q\x12 \xc6M\xa8\xd1B\x12\xcfT\xbfC\xd0\x1e\x89\x8c\xaa\x11\xafq\xed^sF\xb5\xda\x19\x98\xf2B\xb9\xe2\x9f\xb3\xd8*X%\0\x01q\x12 v\xbe\x8bR\x8d\0u\xf7\xaa\xe9\x8do\xa5zm<\x83\xaeH\n\x84i\xe6h\xd7\xb0\xaf\x96\x89\x95\xacq\xd8*X%\0\x01q\x12 \x92`zHK\xcfR\xb8\x10G\xa3+t\t\xb7_\xc9\xf6\x9b+\xee\xe0\x83S\"#\xba\xe8Q\xdd\x10\x02")] }
60 changes: 60 additions & 0 deletions car-mirror/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,63 @@ mod tests {
Ok(())
}
}

#[cfg(test)]
mod proptests {
use crate::{
test_utils::{encode, generate_dag},
walk_dag_in_order_breadth_first,
};
use futures::TryStreamExt;
use libipld::{
multihash::{Code, MultihashDigest},
Cid, Ipld, IpldCodec,
};
use proptest::strategy::Strategy;
use std::collections::BTreeSet;
use test_strategy::proptest;
use wnfs_common::{BlockStore, MemoryBlockStore};

fn ipld_dags() -> impl Strategy<Value = (Vec<(Cid, Ipld)>, Cid)> {
generate_dag(256, |cids| {
let ipld = Ipld::List(cids.into_iter().map(Ipld::Link).collect());
let cid = Cid::new_v1(
IpldCodec::DagCbor.into(),
Code::Blake3_256.digest(&encode(&ipld)),
);
(cid, ipld)
})
}

#[proptest(max_shrink_iters = 100_000)]
fn walk_dag_never_iterates_block_twice(#[strategy(ipld_dags())] dag: (Vec<(Cid, Ipld)>, Cid)) {
async_std::task::block_on(async {
let (dag, root) = dag;
let store = &MemoryBlockStore::new();
for (cid, ipld) in dag.iter() {
let cid_store = store
.put_block(encode(ipld), IpldCodec::DagCbor.into())
.await
.unwrap();
assert_eq!(*cid, cid_store);
}

let mut cids = walk_dag_in_order_breadth_first(root, store)
.map_ok(|(cid, _)| cid)
.try_collect::<Vec<_>>()
.await
.unwrap();

cids.sort();

let unique_cids = cids
.iter()
.cloned()
.collect::<BTreeSet<_>>()
.into_iter()
.collect::<Vec<_>>();

assert_eq!(cids, unique_cids);
});
}
}
51 changes: 51 additions & 0 deletions car-mirror/src/test_utils/dag_strategy.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
use std::{collections::HashSet, fmt::Debug};

use bytes::Bytes;
use libipld::{Cid, Ipld, IpldCodec};
use libipld_core::codec::Encode;
use proptest::strategy::Strategy;
use roaring_graphs::{arb_dag, DirectedAcyclicGraph, Vertex};

pub fn encode(ipld: &Ipld) -> Bytes {

Check failure on line 9 in car-mirror/src/test_utils/dag_strategy.rs

View workflow job for this annotation

GitHub Actions / run-checks (stable)

missing documentation for a function

Check failure on line 9 in car-mirror/src/test_utils/dag_strategy.rs

View workflow job for this annotation

GitHub Actions / run-checks (nightly)

missing documentation for a function
let mut vec = Vec::new();
ipld.encode(IpldCodec::DagCbor, &mut vec).unwrap(); // TODO(matheus23) unwrap
Bytes::from(vec)
}

pub fn generate_dag<T: Debug + Clone>(

Check failure on line 15 in car-mirror/src/test_utils/dag_strategy.rs

View workflow job for this annotation

GitHub Actions / run-checks (stable)

missing documentation for a function

Check failure on line 15 in car-mirror/src/test_utils/dag_strategy.rs

View workflow job for this annotation

GitHub Actions / run-checks (nightly)

missing documentation for a function
max_nodes: u16,
generate_block: fn(Vec<Cid>) -> (Cid, T),
) -> impl Strategy<Value = (Vec<(Cid, T)>, Cid)> {
arb_dag(1..max_nodes, 0.5).prop_map(move |dag| dag_to_nodes(&dag, generate_block))
}

pub fn dag_to_nodes<T>(

Check failure on line 22 in car-mirror/src/test_utils/dag_strategy.rs

View workflow job for this annotation

GitHub Actions / run-checks (stable)

missing documentation for a function

Check failure on line 22 in car-mirror/src/test_utils/dag_strategy.rs

View workflow job for this annotation

GitHub Actions / run-checks (nightly)

missing documentation for a function
dag: &DirectedAcyclicGraph,
generate_node: fn(Vec<Cid>) -> (Cid, T),
) -> (Vec<(Cid, T)>, Cid) {
let mut blocks = Vec::new();
let mut visited = HashSet::new();
let (cid, block) = dag_to_nodes_helper(dag, 0, generate_node, &mut blocks, &mut visited);
blocks.push((cid, block));
(blocks, cid)
}

pub fn dag_to_nodes_helper<T>(

Check failure on line 33 in car-mirror/src/test_utils/dag_strategy.rs

View workflow job for this annotation

GitHub Actions / run-checks (stable)

missing documentation for a function

Check failure on line 33 in car-mirror/src/test_utils/dag_strategy.rs

View workflow job for this annotation

GitHub Actions / run-checks (nightly)

missing documentation for a function
dag: &DirectedAcyclicGraph,
root: Vertex,
generate_node: fn(Vec<Cid>) -> (Cid, T),
arr: &mut Vec<(Cid, T)>,
visited: &mut HashSet<Vertex>,
) -> (Cid, T) {
let mut child_blocks = Vec::new();
for child in dag.iter_children(root) {
if visited.contains(&child) {
continue;
}
visited.insert(child);
child_blocks.push(dag_to_nodes_helper(dag, child, generate_node, arr, visited));
}
let result = generate_node(child_blocks.iter().map(|(cid, _)| *cid).collect());
arr.extend(child_blocks);
result
}
4 changes: 4 additions & 0 deletions car-mirror/src/test_utils/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
#[cfg(feature = "test_utils")]
mod dag_strategy;
/// Random value generator for sampling data.
#[cfg(feature = "test_utils")]
mod rvg;
#[cfg(feature = "test_utils")]
pub use dag_strategy::*;
#[cfg(feature = "test_utils")]
pub use rvg::*;

0 comments on commit 77b1f11

Please sign in to comment.