Skip to content
Merged
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
4 changes: 2 additions & 2 deletions src/storage/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,10 @@ where
return Ok(());
}

let mut range = self.data_offset(index, nodes)?;
let range = self.data_offset(index, nodes)?;

ensure!(
range.by_ref().count() == data.len(),
(range.end - range.start) as usize == data.len(),
format!("length `{:?} != {:?}`", range.count(), data.len())
);

Expand Down
41 changes: 40 additions & 1 deletion tests/feed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ mod common;
use self::failure::Error;
use self::random_access_storage::RandomAccess;
use common::create_feed;
use hypercore::{generate_keypair, Feed, NodeTrait, PublicKey, SecretKey, Storage};
use hypercore::{generate_keypair, Feed, NodeTrait, Proof, PublicKey, SecretKey, Storage};
use std::env::temp_dir;
use std::fmt::Debug;
use std::fs;
Expand Down Expand Up @@ -136,6 +136,45 @@ fn put() {
b.put(4, None, proof).unwrap();
}

#[test]
/// Put data from one feed into another, while veryfing hashes.
/// I.e. manual replication between two feeds.
fn put_with_data() {
// Create a writable feed.
let mut a = create_feed(50).unwrap();

// Create a second feed with the first feed's key.
let (public, secret) = copy_keys(&a);
let storage = Storage::new_memory().unwrap();
let mut b = Feed::builder(public, storage)
.secret_key(secret)
.build()
.unwrap();

// Append 4 blocks of data to the writable feed.
a.append(b"hi").unwrap();
a.append(b"ola").unwrap();
a.append(b"ahoj").unwrap();
a.append(b"salut").unwrap();

for i in 0..4 {
// Generate a proof for the index.
// The `include_hash` argument has to be set to false.
let a_proof = a.proof(i, false).unwrap();
// Get the data for the index.
let a_data = a.get(i).unwrap();

// Put the data into the other hypercore.
b.put(i, a_data.as_deref(), a_proof.clone()).unwrap();

// Load the data we've put.
let b_data = b.get(i).unwrap();

// Assert the data was put correctly.
assert!(a_data == b_data, "Data correct");
}
}

#[test]
fn create_with_storage() {
let storage = Storage::new_memory().unwrap();
Expand Down