Skip to content

Commit

Permalink
testing harness for compatability with negentropy reference test suit…
Browse files Browse the repository at this point in the history
…e, fix issues found by test suite
  • Loading branch information
hoytech committed Sep 13, 2023
1 parent 593a079 commit dbc3e78
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 25 deletions.
59 changes: 59 additions & 0 deletions examples/harness.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// This is a testing harness for compatibility with the negentropy reference
// implementation's test suite: https://github.com/hoytech/negentropy/tree/master/test

use negentropy::Negentropy;
use std::io;
use std::env;

fn main() {
let id_size = 16;

let frame_size_limit_env_var = env::var("FRAMESIZELIMIT");
let frame_size_limit = if frame_size_limit_env_var.is_ok() { frame_size_limit_env_var.unwrap().parse::<usize>().unwrap() } else { 0 };

let mut ne = Negentropy::new(id_size, Some(frame_size_limit as u64)).unwrap();

for line in io::stdin().lines() {
let line_unwrapped = line.unwrap();
let items: Vec<&str> = line_unwrapped.split(",").collect();

if items[0] == "item" {
let created = items[1].parse::<u64>().unwrap();
let id = items[2];
ne.add_item(created, id).unwrap();
} else if items[0] == "seal" {
ne.seal().unwrap();
} else if items[0] == "initiate" {
let q = ne.initiate().unwrap();
if frame_size_limit > 0 && q.len()/2 > frame_size_limit { panic!("frameSizeLimit exceeded"); }
println!("msg,{}", q);
} else if items[0] == "msg" {
let mut q = String::new();

if items.len() >= 2 {
q = items[1].to_string();
}

if ne.is_initiator() {
let mut have_ids = Vec::new();
let mut need_ids = Vec::new();
q = ne.reconcile_with_ids(&q, &mut have_ids, &mut need_ids).unwrap();

for id in &have_ids { println!("have,{}", id); }
for id in &need_ids { println!("need,{}", id); }

if q.len() == 0 {
println!("done");
continue;
}
} else {
q = ne.reconcile(&q).unwrap();
}

if frame_size_limit > 0 && q.len()/2 > frame_size_limit { panic!("frameSizeLimit exceeded"); }
println!("msg,{}", q);
} else {
panic!("unknwown cmd");
}
}
}
42 changes: 17 additions & 25 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ use alloc::vec::Vec;
use core::cmp::Ordering;
use core::convert::TryFrom;
use core::fmt;
use core::iter;
use core::ops::BitXorAssign;
#[cfg(feature = "std")]
use std::collections::HashSet;
Expand Down Expand Up @@ -461,7 +460,6 @@ impl Negentropy {
while outputs.len() > 0 {
self.pending_outputs.push_front(outputs.pop_back().unwrap());
}
//self.pending_outputs.extend(outputs.into_iter().rev());

Ok(())
}
Expand Down Expand Up @@ -533,41 +531,29 @@ impl Negentropy {
} else {
let items_per_bucket: usize = num_elems / BUCKETS;
let buckets_with_extra: usize = num_elems % BUCKETS;
let lower: XorElem = items.first().copied().unwrap_or_default();
let mut prev_bound: XorElem = lower;
let bucket_end = items.iter().copied().take(items_per_bucket);
let mut curr : usize = 0;
let mut prev_bound = items.iter().nth(0).unwrap();

for i in 0..BUCKETS {
let mut our_xor_set: XorElem = XorElem::new();
let bucket_end : usize = curr + items_per_bucket + (if i < buckets_with_extra { 1 } else { 0 });

let bucket_end = bucket_end.clone();
if i < buckets_with_extra {
for elem in bucket_end.chain(iter::once(lower)) {
our_xor_set ^= elem;
}
} else {
for elem in bucket_end {
our_xor_set ^= elem;
}
};
while curr != bucket_end {
our_xor_set ^= items[curr];
curr += 1;
}

let mut payload: Vec<u8> = Vec::with_capacity(10 + self.id_size as usize);
payload.extend(self.encode_mode(Mode::Fingerprint));
payload.extend(our_xor_set.get_id_subsize(self.id_size));

let next_bound: XorElem = if i == 0 {
lower_bound
} else {
self.get_minimal_bound(&prev_bound, &lower)?
};

outputs.push_back(BoundOutput {
start: if i == 0 { lower_bound } else { prev_bound },
end: upper_bound,
start: if i == 0 { lower_bound } else { *prev_bound },
end: if bucket_end == items.len() { upper_bound } else { self.get_minimal_bound(&items[curr-1], &items[curr])? },
payload,
});

prev_bound = next_bound;
prev_bound = &outputs.back().unwrap().end;
}

if let Some(output) = outputs.back_mut() {
Expand All @@ -583,7 +569,7 @@ impl Negentropy {
let mut curr_bound: XorElem = XorElem::new();
let mut last_timestamp_out: u64 = 0;

self.pending_outputs.make_contiguous().sort_by(|a, b| b.start.cmp(&a.start));
self.pending_outputs.make_contiguous().sort_by(|a, b| a.start.cmp(&b.start));

while let Some(p) = self.pending_outputs.front() {
let mut o: Vec<u8> = Vec::new();
Expand Down Expand Up @@ -697,6 +683,12 @@ impl Negentropy {
n >>= 7;
}

o.reverse();

for i in 0..(o.len() - 1) {
o[i] |= 0x80;
}

o
}

Expand Down

0 comments on commit dbc3e78

Please sign in to comment.