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

remove builder pattern #80

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
42 changes: 26 additions & 16 deletions src/feed.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
//! Hypercore's main abstraction. Exposes an append-only, secure log structure.

use crate::feed_builder::FeedBuilder;
use crate::replicate::{Message, Peer};
pub use crate::storage::{Node, NodeTrait, Storage, Store};

Expand Down Expand Up @@ -50,39 +49,50 @@ impl<T> Feed<T>
where
T: RandomAccess<Error = Error> + Debug,
{
/// create a new feed
pub fn new(public_key: PublicKey, storage: Storage<T>) -> Self {
Feed {
merkle: Merkle::new(),
byte_length: 0,
length: 0,
bitfield: Bitfield::default(),
tree: TreeIndex::default(),
public_key,
secret_key: None,
storage,
peers: vec![],
}
}

/// Set the secret key.
pub fn set_secret_key(mut self, secret_key: SecretKey) -> Self {
self.secret_key = Some(secret_key);
self
}

/// Create a new instance with a custom storage backend.
pub fn with_storage(mut storage: crate::storage::Storage<T>) -> Result<Self> {
match storage.read_partial_keypair() {
Some(partial_keypair) => {
let builder = FeedBuilder::new(partial_keypair.public, storage);
let mut feed = Feed::new(partial_keypair.public, storage);

// return early without secret key
if partial_keypair.secret.is_none() {
return Ok(builder.build()?);
if partial_keypair.secret.is_some() {
feed = feed.set_secret_key(partial_keypair.secret.unwrap());
}

Ok(builder
.secret_key(partial_keypair.secret.unwrap())
.build()?)
Ok(feed)
}
None => {
// we have no keys, generate a pair and save them to the storage
let keypair = generate_keypair();
storage.write_public_key(&keypair.public)?;
storage.write_secret_key(&keypair.secret)?;

Ok(FeedBuilder::new(keypair.public, storage)
.secret_key(keypair.secret)
.build()?)
Ok(Feed::new(keypair.public, storage).set_secret_key(keypair.secret))
}
}
}

/// Starts a `FeedBuilder` with the provided `Keypair` and `Storage`.
pub fn builder(public_key: PublicKey, storage: Storage<T>) -> FeedBuilder<T> {
FeedBuilder::new(public_key, storage)
}

/// Get the amount of entries in the feed.
#[inline]
pub fn len(&self) -> usize {
Expand Down
62 changes: 0 additions & 62 deletions src/feed_builder.rs

This file was deleted.

2 changes: 0 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,13 @@ mod audit;
mod crypto;
mod event;
mod feed;
mod feed_builder;
mod proof;
mod replicate;
mod storage;

pub use crate::crypto::{generate_keypair, sign, verify, Signature};
pub use crate::event::Event;
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};
Expand Down
5 changes: 1 addition & 4 deletions tests/compat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,7 @@ fn deterministic_signatures() {
for _ in 0..5 {
let (dir, storage) = mk_storage();
let keypair = mk_keypair(&keypair_bytes, &key);
let mut feed = Feed::builder(keypair.public, storage)
.secret_key(keypair.secret)
.build()
.unwrap();
let mut feed = Feed::new(keypair.public, storage).set_secret_key(keypair.secret);

let data = b"abc";
for &b in data {
Expand Down
15 changes: 3 additions & 12 deletions tests/feed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,7 @@ use std::io::Write;
fn create_with_key() {
let keypair = generate_keypair();
let storage = Storage::new_memory().unwrap();
let _feed = Feed::builder(keypair.public, storage)
.secret_key(keypair.secret)
.build()
.unwrap();
let _feed = Feed::new(keypair.public, storage);
}

#[test]
Expand Down Expand Up @@ -90,10 +87,7 @@ fn verify() {
let (public, secret) = copy_keys(&feed);
let feed_bytes = secret.to_bytes().to_vec();
let storage = Storage::new(|_| Ok(ram::RandomAccessMemory::new(50))).unwrap();
let mut evil_feed = Feed::builder(public, storage)
.secret_key(secret)
.build()
.unwrap();
let mut evil_feed = Feed::new(public, storage).set_secret_key(secret);

let evil_bytes = match &feed.secret_key() {
Some(key) => key.to_bytes(),
Expand All @@ -119,10 +113,7 @@ fn put() {
let mut a = create_feed(50).unwrap();
let (public, secret) = copy_keys(&a);
let storage = Storage::new(|_| Ok(ram::RandomAccessMemory::new(50))).unwrap();
let mut b = Feed::builder(public, storage)
.secret_key(secret)
.build()
.unwrap();
let mut b = Feed::new(public, storage).set_secret_key(secret);

for _ in 0..10 {
a.append(b"foo").unwrap();
Expand Down