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

Hamt writing #600

Closed
wants to merge 1 commit into from
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
49 changes: 40 additions & 9 deletions iroh-resolver/tests/roundtrip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,12 @@ type TestDir = BTreeMap<String, TestDirEntry>;

/// builds an unixfs directory out of a TestDir
#[async_recursion(?Send)]
async fn build_directory(name: &str, dir: &TestDir) -> Result<Directory> {
async fn build_directory(name: &str, dir: &TestDir, hamt: bool) -> Result<Directory> {
let mut builder = DirectoryBuilder::new();
builder.name(name);
if hamt {
builder.hamt();
}
for (name, entry) in dir {
match entry {
TestDirEntry::File(content) => {
Expand All @@ -41,7 +44,7 @@ async fn build_directory(name: &str, dir: &TestDir) -> Result<Directory> {
builder.add_file(file);
}
TestDirEntry::Directory(dir) => {
let dir = build_directory(name, dir).await?;
let dir = build_directory(name, dir, hamt).await?;
builder.add_dir(dir)?;
}
}
Expand Down Expand Up @@ -111,8 +114,8 @@ async fn build_testdir(
}

/// a roundtrip test that converts a dir to an unixfs DAG and back
async fn dir_roundtrip_test(dir: TestDir) -> Result<bool> {
let directory = build_directory("", &dir).await?;
async fn dir_roundtrip_test(dir: TestDir, hamt: bool) -> Result<bool> {
let directory = build_directory("", &dir, hamt).await?;
let stream = directory.encode();
let (root, resolver) = stream_to_resolver(stream).await?;
let stream =
Expand All @@ -122,11 +125,11 @@ async fn dir_roundtrip_test(dir: TestDir) -> Result<bool> {
}

/// sync version of dir_roundtrip_test for use in proptest
fn dir_roundtrip_test_sync(dir: TestDir) -> bool {
fn dir_roundtrip_test_sync(dir: TestDir, hamt: bool) -> bool {
tokio::runtime::Builder::new_current_thread()
.build()
.unwrap()
.block_on(dir_roundtrip_test(dir))
.block_on(dir_roundtrip_test(dir, hamt))
.unwrap()
}

Expand Down Expand Up @@ -184,13 +187,14 @@ fn file_roundtrip_test_sync(data: Bytes, chunk_size: usize, degree: usize) -> bo

fn arb_test_dir() -> impl Strategy<Value = TestDir> {
// create an arbitrary nested directory structure
// zero size file names are not generated, since they are not allowed and don't work with hamt directories
fn arb_dir_entry() -> impl Strategy<Value = TestDirEntry> {
let leaf = any::<Vec<u8>>().prop_map(|x| TestDirEntry::File(Bytes::from(x)));
leaf.prop_recursive(3, 64, 10, |inner| {
prop::collection::btree_map(".*", inner, 0..10).prop_map(TestDirEntry::Directory)
prop::collection::btree_map(".+", inner, 0..10).prop_map(TestDirEntry::Directory)
})
}
prop::collection::btree_map(".*", arb_dir_entry(), 0..10)
prop::collection::btree_map(".+", arb_dir_entry(), 0..10)
}

fn arb_degree() -> impl Strategy<Value = usize> {
Expand All @@ -211,8 +215,35 @@ proptest! {

#[test]
fn test_dir_roundtrip(data in arb_test_dir()) {
assert!(dir_roundtrip_test_sync(data));
assert!(dir_roundtrip_test_sync(data, false));
}

#[test]
fn test_dir_roundtrip_hamt(data in arb_test_dir()) {
assert!(dir_roundtrip_test_sync(data, true));
}
}

#[test]
fn test_hamt_roundtrip_1() {
let mut dir = TestDir::new();
dir.insert("foo".to_string(), TestDirEntry::File(Bytes::from("bar")));
dir.insert("fnord".to_string(), TestDirEntry::File(Bytes::from("baz")));
assert!(dir_roundtrip_test_sync(dir, true));
}

#[test]
fn test_hamt_roundtrip_2() {
let mut dir = TestDir::new();
dir.insert("foo".to_string(), TestDirEntry::File(Bytes::from("bar")));
assert!(dir_roundtrip_test_sync(dir, true));
}

#[test]
fn test_hamt_roundtrip_3() {
let mut dir = TestDir::new();
dir.insert("a".to_string(), TestDirEntry::File(Bytes::from("bar")));
assert!(dir_roundtrip_test_sync(dir, true));
}

#[tokio::test]
Expand Down
3 changes: 1 addition & 2 deletions iroh-store/src/rpc.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::result;

use anyhow::Result;
use bytes::BytesMut;
use iroh_rpc_client::{create_server, ServerError, ServerSocket, StoreServer};
Expand All @@ -8,6 +6,7 @@ use iroh_rpc_types::store::{
HasRequest, HasResponse, PutManyRequest, PutRequest, StoreAddr, StoreRequest, StoreService,
VersionRequest, VersionResponse,
};
use std::result;
use tracing::info;

use crate::store::Store;
Expand Down
Loading