Skip to content

Commit

Permalink
Remove generic param from Hypercore
Browse files Browse the repository at this point in the history
  • Loading branch information
cowlicks committed Jun 29, 2024
1 parent 29cc3cb commit 7d404dd
Show file tree
Hide file tree
Showing 10 changed files with 29 additions and 50 deletions.
5 changes: 2 additions & 3 deletions benches/disk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -24,7 +23,7 @@ fn bench_create_disk(c: &mut Criterion) {
}

#[cfg(feature = "cache")]
async fn create_hypercore(name: &str) -> Result<Hypercore<RandomAccessDisk>, HypercoreError> {
async fn create_hypercore(name: &str) -> Result<Hypercore, HypercoreError> {
let dir = TempfileBuilder::new()
.prefix(name)
.tempdir()
Expand All @@ -38,7 +37,7 @@ async fn create_hypercore(name: &str) -> Result<Hypercore<RandomAccessDisk>, Hyp
}

#[cfg(not(feature = "cache"))]
async fn create_hypercore(name: &str) -> Result<Hypercore<RandomAccessDisk>, HypercoreError> {
async fn create_hypercore(name: &str) -> Result<Hypercore, HypercoreError> {
let dir = TempfileBuilder::new()
.prefix(name)
.tempdir()
Expand Down
16 changes: 9 additions & 7 deletions benches/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@ fn bench_create_memory(c: &mut Criterion) {
}

#[cfg(feature = "cache")]
async fn create_hypercore(
page_size: usize,
) -> Result<Hypercore<RandomAccessMemory>, HypercoreError> {
async fn create_hypercore(page_size: usize) -> Result<Hypercore, HypercoreError> {
use hypercore::StorageTraits;

let storage = Storage::open(
Expand All @@ -40,11 +38,15 @@ async fn create_hypercore(
}

#[cfg(not(feature = "cache"))]
async fn create_hypercore(
page_size: usize,
) -> Result<Hypercore<RandomAccessMemory>, HypercoreError> {
async fn create_hypercore(page_size: usize) -> Result<Hypercore, HypercoreError> {
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<dyn StorageTraits>)
})
},
false,
)
.await?;
Expand Down
5 changes: 2 additions & 3 deletions examples/disk.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -25,7 +24,7 @@ async fn main() {

// Build a new disk hypercore
let mut hypercore = HypercoreBuilder::new(storage)
.build::<RandomAccessMemory>()
.build()
.await
.expect("Could not create disk hypercore");

Expand All @@ -44,7 +43,7 @@ async fn main() {
.expect("Could not open existing disk storage");
let mut hypercore = HypercoreBuilder::new(storage)
.open(true)
.build::<RandomAccessMemory>()
.build()
.await
.expect("Could not open disk hypercore");

Expand Down
3 changes: 1 addition & 2 deletions examples/memory.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -15,7 +14,7 @@ async fn main() {

// Build hypercore
let mut hypercore = HypercoreBuilder::new(storage)
.build::<RandomAccessMemory>()
.build()
.await
.expect("Could not create memory hypercore");

Expand Down
6 changes: 2 additions & 4 deletions examples/replication.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -73,8 +71,8 @@ async fn main() {
}

async fn replicate_index(
origin_hypercore: &mut Hypercore<RandomAccessDisk>,
replicated_hypercore: &mut Hypercore<RandomAccessMemory>,
origin_hypercore: &mut Hypercore,
replicated_hypercore: &mut Hypercore,
request_index: u64,
) {
let missing_nodes = origin_hypercore
Expand Down
5 changes: 1 addition & 4 deletions src/builder.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use random_access_storage::RandomAccess;
use std::fmt::Debug;
#[cfg(feature = "cache")]
use std::time::Duration;
Expand Down Expand Up @@ -88,9 +87,7 @@ impl HypercoreBuilder {

/// Build a new Hypercore.
#[instrument(err, skip_all)]
pub async fn build<T: RandomAccess + Debug + Send>(
self,
) -> Result<Hypercore<T>, HypercoreError> {
pub async fn build(self) -> Result<Hypercore, HypercoreError> {
Hypercore::new(self.storage, self.options).await
}
}
22 changes: 5 additions & 17 deletions src/core.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -40,10 +39,7 @@ impl HypercoreOptions {

/// Hypercore is an append-only log structure.
#[derive(Debug)]
pub struct Hypercore<T>
where
T: RandomAccess + Debug,
{
pub struct Hypercore {
pub(crate) key_pair: PartialKeypair,
pub(crate) storage: Storage,
pub(crate) oplog: Oplog,
Expand All @@ -52,7 +48,6 @@ where
pub(crate) bitfield: Bitfield,
skip_flush_count: u8, // autoFlush in Javascript
header: Header,
_foo: Option<T>,
}

/// Response from append, matches that of the Javascript result
Expand Down Expand Up @@ -80,15 +75,12 @@ pub struct Info {
pub writeable: bool,
}

impl<T> Hypercore<T>
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<Hypercore<T>, HypercoreError> {
) -> Result<Hypercore, HypercoreError> {
let key_pair: Option<PartialKeypair> = if options.open {
if options.key_pair.is_some() {
return Err(HypercoreError::BadArgument {
Expand Down Expand Up @@ -255,7 +247,6 @@ where
bitfield,
header,
skip_flush_count: 0,
_foo: None,
})
}

Expand Down Expand Up @@ -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> {
Expand Down Expand Up @@ -1101,9 +1091,7 @@ mod tests {
Ok(())
}

async fn create_hypercore_with_data(
length: u64,
) -> Result<Hypercore<RandomAccessMemory>, HypercoreError> {
async fn create_hypercore_with_data(length: u64) -> Result<Hypercore, HypercoreError> {
let signing_key = generate_signing_key();
create_hypercore_with_data_and_key_pair(
length,
Expand All @@ -1118,7 +1106,7 @@ mod tests {
async fn create_hypercore_with_data_and_key_pair(
length: u64,
key_pair: PartialKeypair,
) -> Result<Hypercore<RandomAccessMemory>, HypercoreError> {
) -> Result<Hypercore, HypercoreError> {
let storage = Storage::new_memory().await?;
let mut hypercore = Hypercore::new(
storage,
Expand Down
5 changes: 2 additions & 3 deletions tests/common/mod.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -35,7 +34,7 @@ pub fn get_test_key_pair() -> PartialKeypair {
PartialKeypair { public, secret }
}

pub async fn create_hypercore(work_dir: &str) -> Result<Hypercore<RandomAccessDisk>> {
pub async fn create_hypercore(work_dir: &str) -> Result<Hypercore> {
let path = Path::new(work_dir).to_owned();
let key_pair = get_test_key_pair();
let storage = Storage::new_disk(&path, true).await?;
Expand All @@ -45,7 +44,7 @@ pub async fn create_hypercore(work_dir: &str) -> Result<Hypercore<RandomAccessDi
.await?)
}

pub async fn open_hypercore(work_dir: &str) -> Result<Hypercore<RandomAccessDisk>> {
pub async fn open_hypercore(work_dir: &str) -> Result<Hypercore> {
let path = Path::new(work_dir).to_owned();
let storage = Storage::new_disk(&path, false).await?;
Ok(HypercoreBuilder::new(storage).open(true).build().await?)
Expand Down
9 changes: 4 additions & 5 deletions tests/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -15,15 +14,15 @@ 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::<RandomAccessMemory>();
let _hypercore = HypercoreBuilder::new(storage).build();
Ok(())
}

#[test(async_test)]
async fn hypercore_new_with_key_pair() -> Result<()> {
let storage = Storage::new_memory().await?;
let key_pair = get_test_key_pair();
let _hypercore: Hypercore<RandomAccessMemory> = HypercoreBuilder::new(storage)
let _hypercore = HypercoreBuilder::new(storage)
.key_pair(key_pair)
.build()
.await?;
Expand All @@ -37,7 +36,7 @@ async fn hypercore_open_with_key_pair_error() -> Result<()> {
assert!(HypercoreBuilder::new(storage)
.key_pair(key_pair)
.open(true)
.build::<RandomAccessMemory>()
.build()
.await
.is_err());
Ok(())
Expand Down
3 changes: 1 addition & 2 deletions tests/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -67,7 +66,7 @@ async fn assert_implementation_matches_model(ops: Vec<Op>) -> bool {
.await
.expect("Memory storage creation should be successful");
let mut hypercore = HypercoreBuilder::new(storage)
.build::<RandomAccessMemory>()
.build()
.await
.expect("Hypercore creation should be successful");

Expand Down

0 comments on commit 7d404dd

Please sign in to comment.