Skip to content
Closed
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
63 changes: 62 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,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".
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Frando

i tried to figure out why it doesnt work unless you put None first
i reimplemented your failing test with js version of hypercore like this:
https://gist.github.com/khodzha/36eb72671a087e5e3ae2e0e8b03d55f8

and noticed that for the first hi node:

  • proof in js has 2 nodes with indices [2, 5]
  • proof in rust has 3 nodes, indices [0, 2, 5]

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh, this happens because of let a_proof = a.proof(i, true).unwrap();
without true proof has 2 nodes and is inserted without problem (although with wrong data 😬 )

btw if i do proof(i, {hash: true}) in js hypercore it will err with Error: Missing tree roots needed for verify too

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<String> {
proof
.nodes
.iter()
.map(|n| {
format!(
"idx {} len {} parent {} hash {:?}..",
n.index(),
n.len(),
n.parent(),
&n.hash()[0..5]
)
})
.collect::<Vec<String>>()
}

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