From d9ff1ea07a5e8a4dc07eac76fd61f11ae609a9d3 Mon Sep 17 00:00:00 2001 From: Shamir Khodzha Date: Sat, 2 May 2020 17:49:41 +0300 Subject: [PATCH 1/4] fixed skewed range in put_data of Storage ensure! macro was mutating the range so by the end of the macro evaluation the range was in wrong state --- src/storage/mod.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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()) ); From df2ea6f2ca733f1f35c327aa91a4b158472704e5 Mon Sep 17 00:00:00 2001 From: "Franz Heinzmann (Frando)" Date: Tue, 7 Apr 2020 23:32:33 +0200 Subject: [PATCH 2/4] Add failing put test --- tests/feed.rs | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 62 insertions(+), 1 deletion(-) diff --git a/tests/feed.rs b/tests/feed.rs index 6c37b30..d8189c9 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,67 @@ 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 { + let a_proof = a.proof(i, true).unwrap(); + let a_data = a.get(i).unwrap(); + eprintln!("A: idx {} data {:?}", i, &a_data); + eprintln!("Proof: {:#?}", fmt_proof(&a_proof)); + + // When putting with data right away (line after next) without putting + // with data = None first, I get errors about "missing tree roots needed for verify". + b.put(i, None, a_proof.clone()).unwrap(); + // With the line before, the next line works and gives no errors. + b.put(i, a_data.as_deref(), a_proof.clone()).unwrap(); + + // Load the data we've put. + let b_data = b.get(i).unwrap(); + + // However, this assertion does not hold. The data in B is incorrect. + // It seems the data is somehow stored at the wrong index. + eprintln!("B: idx {} {:?}", i, &b_data); + // Comment out the assertion to see a debug output for all nodes. + assert!(a_data.unwrap() == b_data.unwrap(), "Data correct"); + } +} + +/// Helper function to format proofs in a readable debug format. +fn fmt_proof(proof: &Proof) -> Vec { + proof + .nodes + .iter() + .map(|n| { + format!( + "idx {} len {} parent {} hash {:?}..", + n.index(), + n.len(), + n.parent(), + &n.hash()[0..5] + ) + }) + .collect::>() +} + #[test] fn create_with_storage() { let storage = Storage::new_memory().unwrap(); From fa03598caf8197aa5a2735ed67f33c2fb94dea79 Mon Sep 17 00:00:00 2001 From: "Franz Heinzmann (Frando)" Date: Mon, 4 May 2020 14:28:55 +0200 Subject: [PATCH 3/4] Fix put test --- tests/feed.rs | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/tests/feed.rs b/tests/feed.rs index d8189c9..987f08c 100644 --- a/tests/feed.rs +++ b/tests/feed.rs @@ -158,29 +158,30 @@ fn put_with_data() { a.append(b"salut").unwrap(); for i in 0..4 { - let a_proof = a.proof(i, true).unwrap(); + // 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(); - eprintln!("A: idx {} data {:?}", i, &a_data); - eprintln!("Proof: {:#?}", fmt_proof(&a_proof)); - // When putting with data right away (line after next) without putting - // with data = None first, I get errors about "missing tree roots needed for verify". - b.put(i, None, a_proof.clone()).unwrap(); - // With the line before, the next line works and gives no errors. + // 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(); - // However, this assertion does not hold. The data in B is incorrect. - // It seems the data is somehow stored at the wrong index. - eprintln!("B: idx {} {:?}", i, &b_data); - // Comment out the assertion to see a debug output for all nodes. - assert!(a_data.unwrap() == b_data.unwrap(), "Data correct"); + // Debug output. + // eprintln!("A: idx {} data {:?}", i, &a_data); + // eprintln!("Proof: {:#?}", fmt_proof(&a_proof)); + // eprintln!("B: idx {} {:?}", i, &b_data); + + // Assert the data was put correctly. + assert!(a_data == b_data, "Data correct"); } } /// Helper function to format proofs in a readable debug format. +#[allow(dead_code)] fn fmt_proof(proof: &Proof) -> Vec { proof .nodes From b2021e80486a9a63ab8e6b366ba36156f4a2d1fe Mon Sep 17 00:00:00 2001 From: "Franz Heinzmann (Frando)" Date: Mon, 4 May 2020 14:29:48 +0200 Subject: [PATCH 4/4] Remove debug output --- tests/feed.rs | 23 ----------------------- 1 file changed, 23 deletions(-) diff --git a/tests/feed.rs b/tests/feed.rs index 987f08c..b7aebcf 100644 --- a/tests/feed.rs +++ b/tests/feed.rs @@ -170,34 +170,11 @@ fn put_with_data() { // Load the data we've put. let b_data = b.get(i).unwrap(); - // Debug output. - // eprintln!("A: idx {} data {:?}", i, &a_data); - // eprintln!("Proof: {:#?}", fmt_proof(&a_proof)); - // eprintln!("B: idx {} {:?}", i, &b_data); - // Assert the data was put correctly. assert!(a_data == b_data, "Data correct"); } } -/// Helper function to format proofs in a readable debug format. -#[allow(dead_code)] -fn fmt_proof(proof: &Proof) -> Vec { - proof - .nodes - .iter() - .map(|n| { - format!( - "idx {} len {} parent {} hash {:?}..", - n.index(), - n.len(), - n.parent(), - &n.hash()[0..5] - ) - }) - .collect::>() -} - #[test] fn create_with_storage() { let storage = Storage::new_memory().unwrap();