From 7d404ddaa6029822dcd6e45b7523648cec0d7195 Mon Sep 17 00:00:00 2001 From: Blake Griffith Date: Fri, 28 Jun 2024 18:08:27 -0700 Subject: [PATCH] Remove generic param from Hypercore --- benches/disk.rs | 5 ++--- benches/memory.rs | 16 +++++++++------- examples/disk.rs | 5 ++--- examples/memory.rs | 3 +-- examples/replication.rs | 6 ++---- src/builder.rs | 5 +---- src/core.rs | 22 +++++----------------- tests/common/mod.rs | 5 ++--- tests/core.rs | 9 ++++----- tests/model.rs | 3 +-- 10 files changed, 29 insertions(+), 50 deletions(-) diff --git a/benches/disk.rs b/benches/disk.rs index 326f57b..a1ad5b7 100644 --- a/benches/disk.rs +++ b/benches/disk.rs @@ -4,7 +4,6 @@ use std::time::{Duration, Instant}; use criterion::async_executor::AsyncStdExecutor; use criterion::{black_box, criterion_group, criterion_main, Criterion}; use hypercore::{Hypercore, HypercoreBuilder, HypercoreError, Storage}; -use random_access_disk::RandomAccessDisk; use tempfile::Builder as TempfileBuilder; fn bench_create_disk(c: &mut Criterion) { @@ -24,7 +23,7 @@ fn bench_create_disk(c: &mut Criterion) { } #[cfg(feature = "cache")] -async fn create_hypercore(name: &str) -> Result, HypercoreError> { +async fn create_hypercore(name: &str) -> Result { let dir = TempfileBuilder::new() .prefix(name) .tempdir() @@ -38,7 +37,7 @@ async fn create_hypercore(name: &str) -> Result, Hyp } #[cfg(not(feature = "cache"))] -async fn create_hypercore(name: &str) -> Result, HypercoreError> { +async fn create_hypercore(name: &str) -> Result { let dir = TempfileBuilder::new() .prefix(name) .tempdir() diff --git a/benches/memory.rs b/benches/memory.rs index 2323391..bb97685 100644 --- a/benches/memory.rs +++ b/benches/memory.rs @@ -19,9 +19,7 @@ fn bench_create_memory(c: &mut Criterion) { } #[cfg(feature = "cache")] -async fn create_hypercore( - page_size: usize, -) -> Result, HypercoreError> { +async fn create_hypercore(page_size: usize) -> Result { use hypercore::StorageTraits; let storage = Storage::open( @@ -40,11 +38,15 @@ async fn create_hypercore( } #[cfg(not(feature = "cache"))] -async fn create_hypercore( - page_size: usize, -) -> Result, HypercoreError> { +async fn create_hypercore(page_size: usize) -> Result { + use hypercore::StorageTraits; + let storage = Storage::open( - |_| Box::pin(async move { Ok(RandomAccessMemory::new(page_size)) }), + |_| { + Box::pin(async move { + Ok(Box::new(RandomAccessMemory::new(page_size)) as Box) + }) + }, false, ) .await?; diff --git a/examples/disk.rs b/examples/disk.rs index ccd1c7e..9999089 100644 --- a/examples/disk.rs +++ b/examples/disk.rs @@ -1,7 +1,6 @@ #[cfg(feature = "async-std")] use async_std::main as async_main; use hypercore::{HypercoreBuilder, HypercoreError, Storage}; -use random_access_memory::RandomAccessMemory; use tempfile::Builder; #[cfg(feature = "tokio")] use tokio::main as async_main; @@ -25,7 +24,7 @@ async fn main() { // Build a new disk hypercore let mut hypercore = HypercoreBuilder::new(storage) - .build::() + .build() .await .expect("Could not create disk hypercore"); @@ -44,7 +43,7 @@ async fn main() { .expect("Could not open existing disk storage"); let mut hypercore = HypercoreBuilder::new(storage) .open(true) - .build::() + .build() .await .expect("Could not open disk hypercore"); diff --git a/examples/memory.rs b/examples/memory.rs index 15ae4cf..a510ed6 100644 --- a/examples/memory.rs +++ b/examples/memory.rs @@ -1,7 +1,6 @@ #[cfg(feature = "async-std")] use async_std::main as async_main; use hypercore::{HypercoreBuilder, HypercoreError, Storage}; -use random_access_memory::RandomAccessMemory; #[cfg(feature = "tokio")] use tokio::main as async_main; @@ -15,7 +14,7 @@ async fn main() { // Build hypercore let mut hypercore = HypercoreBuilder::new(storage) - .build::() + .build() .await .expect("Could not create memory hypercore"); diff --git a/examples/replication.rs b/examples/replication.rs index 52c205a..0e658cd 100644 --- a/examples/replication.rs +++ b/examples/replication.rs @@ -4,8 +4,6 @@ use hypercore::{ Hypercore, HypercoreBuilder, HypercoreError, PartialKeypair, RequestBlock, RequestUpgrade, Storage, }; -use random_access_disk::RandomAccessDisk; -use random_access_memory::RandomAccessMemory; use tempfile::Builder; #[cfg(feature = "tokio")] use tokio::main as async_main; @@ -73,8 +71,8 @@ async fn main() { } async fn replicate_index( - origin_hypercore: &mut Hypercore, - replicated_hypercore: &mut Hypercore, + origin_hypercore: &mut Hypercore, + replicated_hypercore: &mut Hypercore, request_index: u64, ) { let missing_nodes = origin_hypercore diff --git a/src/builder.rs b/src/builder.rs index dfece90..37af78e 100644 --- a/src/builder.rs +++ b/src/builder.rs @@ -1,4 +1,3 @@ -use random_access_storage::RandomAccess; use std::fmt::Debug; #[cfg(feature = "cache")] use std::time::Duration; @@ -88,9 +87,7 @@ impl HypercoreBuilder { /// Build a new Hypercore. #[instrument(err, skip_all)] - pub async fn build( - self, - ) -> Result, HypercoreError> { + pub async fn build(self) -> Result { Hypercore::new(self.storage, self.options).await } } diff --git a/src/core.rs b/src/core.rs index 9de0bef..886ff98 100644 --- a/src/core.rs +++ b/src/core.rs @@ -1,7 +1,6 @@ //! Hypercore's main abstraction. Exposes an append-only, secure log structure. use ed25519_dalek::Signature; use futures::future::Either; -use random_access_storage::RandomAccess; use std::convert::TryFrom; use std::fmt::Debug; use tracing::instrument; @@ -40,10 +39,7 @@ impl HypercoreOptions { /// Hypercore is an append-only log structure. #[derive(Debug)] -pub struct Hypercore -where - T: RandomAccess + Debug, -{ +pub struct Hypercore { pub(crate) key_pair: PartialKeypair, pub(crate) storage: Storage, pub(crate) oplog: Oplog, @@ -52,7 +48,6 @@ where pub(crate) bitfield: Bitfield, skip_flush_count: u8, // autoFlush in Javascript header: Header, - _foo: Option, } /// Response from append, matches that of the Javascript result @@ -80,15 +75,12 @@ pub struct Info { pub writeable: bool, } -impl Hypercore -where - T: RandomAccess + Debug + Send, -{ +impl Hypercore { /// Creates/opens new hypercore using given storage and options pub(crate) async fn new( mut storage: Storage, mut options: HypercoreOptions, - ) -> Result, HypercoreError> { + ) -> Result { let key_pair: Option = if options.open { if options.key_pair.is_some() { return Err(HypercoreError::BadArgument { @@ -255,7 +247,6 @@ where bitfield, header, skip_flush_count: 0, - _foo: None, }) } @@ -736,7 +727,6 @@ fn update_contiguous_length( #[cfg(test)] mod tests { use super::*; - use random_access_memory::RandomAccessMemory; #[async_std::test] async fn core_create_proof_block_only() -> Result<(), HypercoreError> { @@ -1101,9 +1091,7 @@ mod tests { Ok(()) } - async fn create_hypercore_with_data( - length: u64, - ) -> Result, HypercoreError> { + async fn create_hypercore_with_data(length: u64) -> Result { let signing_key = generate_signing_key(); create_hypercore_with_data_and_key_pair( length, @@ -1118,7 +1106,7 @@ mod tests { async fn create_hypercore_with_data_and_key_pair( length: u64, key_pair: PartialKeypair, - ) -> Result, HypercoreError> { + ) -> Result { let storage = Storage::new_memory().await?; let mut hypercore = Hypercore::new( storage, diff --git a/tests/common/mod.rs b/tests/common/mod.rs index fbe8616..247b13c 100644 --- a/tests/common/mod.rs +++ b/tests/common/mod.rs @@ -1,6 +1,5 @@ use anyhow::Result; use ed25519_dalek::{SigningKey, VerifyingKey, PUBLIC_KEY_LENGTH, SECRET_KEY_LENGTH}; -use random_access_disk::RandomAccessDisk; use sha2::{Digest, Sha256}; use std::io::prelude::*; use std::path::Path; @@ -35,7 +34,7 @@ pub fn get_test_key_pair() -> PartialKeypair { PartialKeypair { public, secret } } -pub async fn create_hypercore(work_dir: &str) -> Result> { +pub async fn create_hypercore(work_dir: &str) -> Result { let path = Path::new(work_dir).to_owned(); let key_pair = get_test_key_pair(); let storage = Storage::new_disk(&path, true).await?; @@ -45,7 +44,7 @@ pub async fn create_hypercore(work_dir: &str) -> Result Result> { +pub async fn open_hypercore(work_dir: &str) -> Result { let path = Path::new(work_dir).to_owned(); let storage = Storage::new_disk(&path, false).await?; Ok(HypercoreBuilder::new(storage).open(true).build().await?) diff --git a/tests/core.rs b/tests/core.rs index c6b42b7..f3e8d2e 100644 --- a/tests/core.rs +++ b/tests/core.rs @@ -2,8 +2,7 @@ pub mod common; use anyhow::Result; use common::{create_hypercore, get_test_key_pair, open_hypercore, storage_contains_data}; -use hypercore::{Hypercore, HypercoreBuilder, Storage}; -use random_access_memory::RandomAccessMemory; +use hypercore::{HypercoreBuilder, Storage}; use tempfile::Builder; use test_log::test; @@ -15,7 +14,7 @@ use tokio::test as async_test; #[test(async_test)] async fn hypercore_new() -> Result<()> { let storage = Storage::new_memory().await?; - let _hypercore = HypercoreBuilder::new(storage).build::(); + let _hypercore = HypercoreBuilder::new(storage).build(); Ok(()) } @@ -23,7 +22,7 @@ async fn hypercore_new() -> Result<()> { async fn hypercore_new_with_key_pair() -> Result<()> { let storage = Storage::new_memory().await?; let key_pair = get_test_key_pair(); - let _hypercore: Hypercore = HypercoreBuilder::new(storage) + let _hypercore = HypercoreBuilder::new(storage) .key_pair(key_pair) .build() .await?; @@ -37,7 +36,7 @@ async fn hypercore_open_with_key_pair_error() -> Result<()> { assert!(HypercoreBuilder::new(storage) .key_pair(key_pair) .open(true) - .build::() + .build() .await .is_err()); Ok(()) diff --git a/tests/model.rs b/tests/model.rs index 3f81ead..e6a52fe 100644 --- a/tests/model.rs +++ b/tests/model.rs @@ -3,7 +3,6 @@ pub mod common; use proptest::prelude::*; use proptest::test_runner::FileFailurePersistence; use proptest_derive::Arbitrary; -use random_access_memory::RandomAccessMemory; const MAX_FILE_SIZE: u64 = 50000; @@ -67,7 +66,7 @@ async fn assert_implementation_matches_model(ops: Vec) -> bool { .await .expect("Memory storage creation should be successful"); let mut hypercore = HypercoreBuilder::new(storage) - .build::() + .build() .await .expect("Hypercore creation should be successful");