diff --git a/benches/bench.rs b/benches/bench.rs index cde6db2..b3ac1b1 100644 --- a/benches/bench.rs +++ b/benches/bench.rs @@ -7,7 +7,7 @@ use test::Bencher; use hypercore::{Feed, Storage}; -async fn create_feed(page_size: usize) -> Result, Error> { +async fn create_feed(page_size: usize) -> Result { let storage = Storage::new(|_| Box::pin(async move { Ok(RandomAccessMemory::new(page_size)) })).await?; Feed::with_storage(storage).await diff --git a/src/lib.rs b/src/lib.rs index 9549f8e..73259a3 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -48,7 +48,9 @@ pub use crate::feed::Feed; pub use crate::feed_builder::FeedBuilder; pub use crate::proof::Proof; pub use crate::replicate::Peer; -pub use crate::storage::{Node, NodeTrait, Storage, Store}; +pub use crate::storage::{ + storage_disk, storage_memory, BoxStorage, Node, NodeTrait, Storage, Store, +}; pub use ed25519_dalek::{PublicKey, SecretKey}; use std::path::Path; diff --git a/src/storage/mod.rs b/src/storage/mod.rs index 6d39850..a5c45f7 100644 --- a/src/storage/mod.rs +++ b/src/storage/mod.rs @@ -29,6 +29,7 @@ pub struct PartialKeypair { pub secret: Option, } +/// Dynamic-dispatch Storage wrapper pub type BoxStorage = Box; /// Create a new instance backed by a `RandomAccessMemory` instance. diff --git a/tests/common/mod.rs b/tests/common/mod.rs index 8e5d830..f5e3c23 100644 --- a/tests/common/mod.rs +++ b/tests/common/mod.rs @@ -5,7 +5,7 @@ use futures::future::FutureExt; use hypercore::{Feed, Storage, Store}; use random_access_memory as ram; -pub async fn create_feed(page_size: usize) -> Result, Error> { +pub async fn create_feed(page_size: usize) -> Result { let create = |_store: Store| async move { Ok(ram::RandomAccessMemory::new(page_size)) }.boxed(); let storage = Storage::new(create).await?; Feed::with_storage(storage).await diff --git a/tests/compat.rs b/tests/compat.rs index 3a1a778..59d249e 100644 --- a/tests/compat.rs +++ b/tests/compat.rs @@ -11,7 +11,7 @@ use std::path::{Path, PathBuf}; use data_encoding::HEXLOWER; use ed25519_dalek::{Keypair, Signature}; use hypercore::Feed; -use hypercore::{Storage, Store}; +use hypercore::{Storage, Store, BoxStorage}; use random_access_disk::RandomAccessDisk; use remove_dir_all::remove_dir_all; @@ -144,7 +144,7 @@ fn storage_path>(dir: P, s: Store) -> PathBuf { dir.as_ref().join(filename) } -async fn mk_storage() -> (PathBuf, Storage) { +async fn mk_storage() -> (PathBuf, BoxStorage) { let temp_dir = tempfile::tempdir().unwrap(); let dir = temp_dir.into_path(); let storage = Storage::new(|s| { diff --git a/tests/feed.rs b/tests/feed.rs index 4f9d2c3..b592dbf 100644 --- a/tests/feed.rs +++ b/tests/feed.rs @@ -4,16 +4,15 @@ mod common; use common::create_feed; use hypercore::{generate_keypair, Feed, NodeTrait, PublicKey, SecretKey, Storage}; -use random_access_storage::RandomAccess; +use hypercore::{storage_disk, storage_memory}; use std::env::temp_dir; -use std::fmt::Debug; use std::fs; use std::io::Write; #[async_std::test] async fn create_with_key() { let keypair = generate_keypair(); - let storage = Storage::new_memory().await.unwrap(); + let storage = storage_memory().await.unwrap(); let _feed = Feed::builder(keypair.public, storage) .secret_key(keypair.secret) .build() @@ -155,7 +154,7 @@ async fn put_with_data() { // Create a second feed with the first feed's key. let (public, secret) = copy_keys(&a); - let storage = Storage::new_memory().await.unwrap(); + let storage = storage_memory().await.unwrap(); let mut b = Feed::builder(public, storage) .secret_key(secret) .build() @@ -187,7 +186,7 @@ async fn put_with_data() { #[async_std::test] async fn create_with_storage() { - let storage = Storage::new_memory().await.unwrap(); + let storage = storage_memory().await.unwrap(); assert!( Feed::with_storage(storage).await.is_ok(), "Could not create a feed with a storage." @@ -196,7 +195,7 @@ async fn create_with_storage() { #[async_std::test] async fn create_with_stored_public_key() { - let mut storage = Storage::new_memory().await.unwrap(); + let mut storage = storage_memory().await.unwrap(); let keypair = generate_keypair(); storage.write_public_key(&keypair.public).await.unwrap(); assert!( @@ -207,7 +206,7 @@ async fn create_with_stored_public_key() { #[async_std::test] async fn create_with_stored_keys() { - let mut storage = Storage::new_memory().await.unwrap(); + let mut storage = storage_memory().await.unwrap(); let keypair = generate_keypair(); storage.write_public_key(&keypair.public).await.unwrap(); storage.write_secret_key(&keypair.secret).await.unwrap(); @@ -218,7 +217,7 @@ async fn create_with_stored_keys() { } fn copy_keys( - feed: &Feed> + Debug + Send>, + feed: &Feed, ) -> (PublicKey, SecretKey) { match &feed.secret_key() { Some(secret) => { @@ -254,7 +253,7 @@ async fn audit() { async fn audit_bad_data() { let mut dir = temp_dir(); dir.push("audit_bad_data"); - let storage = Storage::new_disk(&dir).await.unwrap(); + let storage = storage_disk(&dir).await.unwrap(); let mut feed = Feed::with_storage(storage).await.unwrap(); feed.append(b"hello").await.unwrap(); feed.append(b"world").await.unwrap(); diff --git a/tests/storage.rs b/tests/storage.rs index d540b31..c8be8cc 100644 --- a/tests/storage.rs +++ b/tests/storage.rs @@ -1,5 +1,5 @@ use ed25519_dalek::PublicKey; -use hypercore::{generate_keypair, sign, verify, Signature, Storage}; +use hypercore::{generate_keypair, sign, storage_memory, verify, Signature}; #[async_std::test] async fn should_write_and_read_keypair() { @@ -8,7 +8,7 @@ async fn should_write_and_read_keypair() { // prepare a signature let sig: Signature = sign(&keypair.public, &keypair.secret, msg); - let mut storage = Storage::new_memory().await.unwrap(); + let mut storage = storage_memory().await.unwrap(); assert!( storage.write_secret_key(&keypair.secret).await.is_ok(), "Can not store secret key." @@ -27,7 +27,7 @@ async fn should_write_and_read_keypair() { #[async_std::test] async fn should_read_partial_keypair() { let keypair = generate_keypair(); - let mut storage = Storage::new_memory().await.unwrap(); + let mut storage = storage_memory().await.unwrap(); assert!( storage.write_public_key(&keypair.public).await.is_ok(), "Can not store public key." @@ -39,13 +39,13 @@ async fn should_read_partial_keypair() { #[async_std::test] async fn should_read_no_keypair() { - let mut storage = Storage::new_memory().await.unwrap(); + let mut storage = storage_memory().await.unwrap(); let partial = storage.read_partial_keypair().await; assert!(partial.is_none(), "A key is present"); } #[async_std::test] async fn should_read_empty_public_key() { - let mut storage = Storage::new_memory().await.unwrap(); + let mut storage = storage_memory().await.unwrap(); assert!(storage.read_public_key().await.is_err()); }