diff --git a/src/storage/mod.rs b/src/storage/mod.rs index 4959611..34fd869 100644 --- a/src/storage/mod.rs +++ b/src/storage/mod.rs @@ -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()) ); diff --git a/tests/feed.rs b/tests/feed.rs index 6c37b30..b7aebcf 100644 --- a/tests/feed.rs +++ b/tests/feed.rs @@ -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; @@ -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();