Skip to content

Commit 4e72f7a

Browse files
committed
update for protocol version 1
1 parent b29263b commit 4e72f7a

File tree

10 files changed

+933
-751
lines changed

10 files changed

+933
-751
lines changed

negentropy/examples/negentropy.rs

Lines changed: 29 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,72 +2,71 @@
22
// Distributed under the MIT software license
33

44
use negentropy::{Bytes, Negentropy};
5+
use negentropy::storage::{NegentropyStorageVector};
56

67
fn main() {
78
// Client
8-
let mut client = Negentropy::new(16, None).unwrap();
9-
client
10-
.add_item(
9+
let mut storage_client = NegentropyStorageVector::new().unwrap();
10+
storage_client
11+
.insert(
1112
0,
12-
Bytes::from_hex("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa").unwrap(),
13+
Bytes::from_hex("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa").unwrap(),
1314
)
1415
.unwrap();
15-
client
16-
.add_item(
16+
storage_client
17+
.insert(
1718
1,
18-
Bytes::from_hex("bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb").unwrap(),
19+
Bytes::from_hex("bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb").unwrap(),
1920
)
2021
.unwrap();
21-
client.seal().unwrap();
22+
storage_client.seal().unwrap();
23+
let mut client = Negentropy::new(&mut storage_client, 0).unwrap();
2224
let init_output = client.initiate().unwrap();
2325
println!("Initiator Output: {}", init_output.as_hex());
2426

2527
// Relay
26-
let mut relay = Negentropy::new(16, None).unwrap();
27-
relay
28-
.add_item(
28+
let mut storage_relay = NegentropyStorageVector::new().unwrap();
29+
storage_relay
30+
.insert(
2931
0,
30-
Bytes::from_hex("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa").unwrap(),
32+
Bytes::from_hex("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa").unwrap(),
3133
)
3234
.unwrap();
33-
relay
34-
.add_item(
35+
storage_relay
36+
.insert(
3537
2,
36-
Bytes::from_hex("cccccccccccccccccccccccccccccccc").unwrap(),
38+
Bytes::from_hex("cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc").unwrap(),
3739
)
3840
.unwrap();
39-
relay
40-
.add_item(
41+
storage_relay
42+
.insert(
4143
3,
42-
Bytes::from_hex("11111111111111111111111111111111").unwrap(),
44+
Bytes::from_hex("1111111111111111111111111111111111111111111111111111111111111111").unwrap(),
4345
)
4446
.unwrap();
45-
relay
46-
.add_item(
47+
storage_relay
48+
.insert(
4749
5,
48-
Bytes::from_hex("22222222222222222222222222222222").unwrap(),
50+
Bytes::from_hex("2222222222222222222222222222222222222222222222222222222222222222").unwrap(),
4951
)
5052
.unwrap();
51-
relay
52-
.add_item(
53+
storage_relay
54+
.insert(
5355
10,
54-
Bytes::from_hex("33333333333333333333333333333333").unwrap(),
56+
Bytes::from_hex("3333333333333333333333333333333333333333333333333333333333333333").unwrap(),
5557
)
5658
.unwrap();
57-
relay.seal().unwrap();
59+
storage_relay.seal().unwrap();
60+
let mut relay = Negentropy::new(&mut storage_relay, 0).unwrap();
5861
let reconcile_output = relay.reconcile(&init_output).unwrap();
5962
println!("Reconcile Output: {}", reconcile_output.as_hex());
6063

6164
// Client
6265
let mut have_ids = Vec::new();
6366
let mut need_ids = Vec::new();
64-
let reconcile_output_with_ids = client
67+
client
6568
.reconcile_with_ids(&reconcile_output, &mut have_ids, &mut need_ids)
6669
.unwrap();
67-
println!(
68-
"Reconcile Output with IDs: {}",
69-
reconcile_output_with_ids.unwrap().as_hex()
70-
);
7170
println!(
7271
"Have IDs: {}",
7372
have_ids

negentropy/fuzz/harness/src/main.rs

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,18 @@ use std::io::BufRead;
55
use std::{env, io};
66

77
use negentropy::{Bytes, Negentropy};
8+
use negentropy::storage::{NegentropyStorageVector};
89

9-
fn main() {
10-
let id_size = 16;
1110

11+
fn main() {
1212
let frame_size_limit_env_var = env::var("FRAMESIZELIMIT");
1313
let frame_size_limit = if let Ok(frame_size_limit) = frame_size_limit_env_var {
1414
frame_size_limit.parse::<usize>().unwrap()
1515
} else {
1616
0
1717
};
1818

19-
let mut ne = Negentropy::new(id_size, Some(frame_size_limit as u64)).unwrap();
19+
let mut storage = NegentropyStorageVector::new().unwrap();
2020

2121
for line in io::stdin().lock().lines() {
2222
let line_unwrapped = line.unwrap();
@@ -25,13 +25,25 @@ fn main() {
2525
if items[0] == "item" {
2626
let created = items[1].parse::<u64>().unwrap();
2727
let id = items[2];
28-
ne.add_item(created, Bytes::from_hex(id).unwrap()).unwrap();
28+
storage.insert(created, Bytes::from_hex(id).unwrap()).unwrap();
2929
} else if items[0] == "seal" {
30-
ne.seal().unwrap();
31-
} else if items[0] == "initiate" {
30+
storage.seal().unwrap();
31+
break;
32+
} else {
33+
panic!("unknwown cmd");
34+
}
35+
}
36+
37+
let mut ne = Negentropy::new(&mut storage, frame_size_limit as u64).unwrap();
38+
39+
for line in io::stdin().lock().lines() {
40+
let line_unwrapped = line.unwrap();
41+
let items: Vec<&str> = line_unwrapped.split(',').collect();
42+
43+
if items[0] == "initiate" {
3244
let q = ne.initiate().unwrap();
3345
if frame_size_limit > 0 && q.len() / 2 > frame_size_limit {
34-
panic!("frameSizeLimit exceeded");
46+
panic!("frame_size_limit exceeded");
3547
}
3648
println!("msg,{}", q.as_hex());
3749
} else if items[0] == "msg" {
@@ -41,7 +53,7 @@ fn main() {
4153
q = items[1].to_string();
4254
}
4355

44-
if ne.is_initiator() {
56+
if ne.is_initiator {
4557
let mut have_ids = Vec::new();
4658
let mut need_ids = Vec::new();
4759
let resp = ne
@@ -66,7 +78,7 @@ fn main() {
6678
}
6779

6880
if frame_size_limit > 0 && q.len() / 2 > frame_size_limit {
69-
panic!("frameSizeLimit exceeded");
81+
panic!("frame_size_limit exceeded");
7082
}
7183
println!("msg,{}", q);
7284
} else {

negentropy/fuzz/perf/src/main.rs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,35 +4,35 @@
44
use std::time::Instant;
55

66
use negentropy::{Bytes, Negentropy};
7-
8-
const ID_SIZE: usize = 10;
9-
const FRAME_SIZE_LIMIT: Option<u64> = None;
7+
use negentropy::storage::{NegentropyStorageVector};
108

119
fn main() {
1210
let items = relay_set();
1311

1412
// Client
15-
let mut client = Negentropy::new(ID_SIZE, FRAME_SIZE_LIMIT).unwrap();
16-
client
17-
.add_item(0, Bytes::from_hex("aaaaaaaaaaaaaaaaaaaa").unwrap())
13+
let mut storage_client = NegentropyStorageVector::new().unwrap();
14+
storage_client
15+
.insert(0, Bytes::from_hex("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa").unwrap())
1816
.unwrap();
19-
client
20-
.add_item(1, Bytes::from_hex("bbbbbbbbbbbbbbbbbbbb").unwrap())
17+
storage_client
18+
.insert(1, Bytes::from_hex("bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb").unwrap())
2119
.unwrap();
22-
client.seal().unwrap();
20+
storage_client.seal().unwrap();
21+
let mut client = Negentropy::new(&mut storage_client, 0).unwrap();
2322
let now = Instant::now();
2423
let init_output = client.initiate().unwrap();
2524
println!("Client init took {} ms", now.elapsed().as_millis());
2625

2726
// Relay
28-
let mut relay = Negentropy::new(ID_SIZE, FRAME_SIZE_LIMIT).unwrap();
27+
let mut storage_relay = NegentropyStorageVector::new().unwrap();
2928
println!("Relay items: {}", items.len());
3029
for (index, item) in items.into_iter().enumerate() {
31-
relay
32-
.add_item(index as u64, Bytes::from_hex(item).unwrap())
30+
storage_relay
31+
.insert(index as u64, Bytes::from_hex(item).unwrap())
3332
.unwrap();
3433
}
35-
relay.seal().unwrap();
34+
storage_relay.seal().unwrap();
35+
let mut relay = Negentropy::new(&mut storage_relay, 0).unwrap();
3636
let now = Instant::now();
3737
let reconcile_output = relay.reconcile(&init_output).unwrap();
3838
println!("Relay reconcile took {} ms", now.elapsed().as_millis());
@@ -49,7 +49,7 @@ fn main() {
4949

5050
fn relay_set() -> Vec<String> {
5151
let characters = "abc";
52-
let length = 20;
52+
let length = 64;
5353
let max = 1_000_000;
5454
generate_combinations(characters, length, max)
5555
}

negentropy/src/bytes.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@ use alloc::string::String;
55
use alloc::vec::Vec;
66
use core::ops::Deref;
77

8-
use crate::{hex, Error};
8+
use crate::error;
9+
use crate::hex;
10+
11+
pub use self::error::Error;
12+
913

1014
/// Bytes
1115
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]

negentropy/src/encoding.rs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
use crate::error;
2+
3+
use self::error::Error;
4+
5+
6+
pub fn get_bytes(encoded: &mut &[u8], n: usize) -> Result<Vec<u8>, Error> {
7+
if encoded.len() < n {
8+
return Err(Error::ParseEndsPrematurely);
9+
}
10+
let res: Vec<u8> = encoded.get(..n).unwrap_or_default().to_vec();
11+
*encoded = encoded.get(n..).unwrap_or_default();
12+
Ok(res)
13+
}
14+
15+
pub fn decode_var_int(encoded: &mut &[u8]) -> Result<u64, Error> {
16+
let mut res = 0u64;
17+
18+
for byte in encoded.iter() {
19+
*encoded = &encoded[1..];
20+
res = (res << 7) | (*byte as u64 & 0b0111_1111);
21+
if (byte & 0b1000_0000) == 0 {
22+
break;
23+
}
24+
}
25+
26+
Ok(res)
27+
}
28+
29+
30+
pub fn encode_var_int(mut n: u64) -> Vec<u8> {
31+
if n == 0 {
32+
return vec![0];
33+
}
34+
35+
let mut o: Vec<u8> = Vec::with_capacity(10);
36+
37+
while n > 0 {
38+
o.push((n & 0x7F) as u8);
39+
n >>= 7;
40+
}
41+
42+
o.reverse();
43+
44+
for i in 0..(o.len() - 1) {
45+
o[i] |= 0x80;
46+
}
47+
48+
o
49+
}

negentropy/src/error.rs

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
use core::fmt;
2+
3+
use crate::hex;
4+
5+
6+
/// Error
7+
#[derive(Debug, PartialEq, Eq)]
8+
pub enum Error {
9+
/// ID too big
10+
IdTooBig,
11+
/// Invalid ID size
12+
InvalidIdSize,
13+
/// IdSizeNotMatch
14+
IdSizeNotMatch,
15+
/// Frame size limit too small
16+
FrameSizeLimitTooSmall,
17+
/// Not sealed
18+
NotSealed,
19+
/// Already sealed
20+
AlreadySealed,
21+
/// Already built initial message
22+
AlreadyBuiltInitialMessage,
23+
/// Initiator error
24+
Initiator,
25+
/// Non-initiator error
26+
NonInitiator,
27+
/// Initiate after reconcile
28+
InitiateAfterReconcile,
29+
/// Unexpected mode
30+
UnexpectedMode(u64),
31+
/// Parse ends prematurely
32+
ParseEndsPrematurely,
33+
/// Duplicate item added
34+
DuplicateItemAdded,
35+
/// Invalid protocol version
36+
InvalidProtocolVersion,
37+
/// Unsupported protocol version
38+
UnsupportedProtocolVersion,
39+
/// Unexpected output
40+
UnexpectedOutput {
41+
/// Expected output
42+
expected: String,
43+
/// Found output
44+
found: String,
45+
},
46+
/// Hex error
47+
Hex(hex::Error),
48+
/// Bad range
49+
BadRange,
50+
}
51+
52+
#[cfg(feature = "std")]
53+
impl std::error::Error for Error {}
54+
55+
impl fmt::Display for Error {
56+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
57+
match self {
58+
Self::IdTooBig => write!(f, "ID too big"),
59+
Self::InvalidIdSize => write!(f, "Invalid ID size"),
60+
Self::IdSizeNotMatch => write!(f, "Current item ID not match the client ID size"),
61+
Self::FrameSizeLimitTooSmall => write!(f, "Frame size limit too small"),
62+
Self::NotSealed => write!(f, "Not sealed"),
63+
Self::AlreadySealed => write!(f, "Already sealed"),
64+
Self::AlreadyBuiltInitialMessage => write!(f, "Already built initial message"),
65+
Self::Initiator => write!(f, "initiator not asking for have/need IDs"),
66+
Self::NonInitiator => write!(f, "non-initiator asking for have/need IDs"),
67+
Self::InitiateAfterReconcile => write!(f, "can't initiate after reconcile"),
68+
Self::UnexpectedMode(m) => write!(f, "Unexpected mode: {}", m),
69+
Self::ParseEndsPrematurely => write!(f, "parse ends prematurely"),
70+
Self::DuplicateItemAdded => write!(f, "duplicate item added"),
71+
Self::InvalidProtocolVersion => write!(f, "invalid negentropy protocol version byte"),
72+
Self::UnsupportedProtocolVersion => {
73+
write!(f, "server does not support our negentropy protocol version")
74+
}
75+
Self::UnexpectedOutput { expected, found } => write!(
76+
f,
77+
"Unexpected output: expected={}, found={}",
78+
expected, found
79+
),
80+
Self::Hex(e) => write!(f, "Hex: {}", e),
81+
Self::BadRange => write!(f, "bad range"),
82+
}
83+
}
84+
}
85+
86+
impl From<hex::Error> for Error {
87+
fn from(e: hex::Error) -> Self {
88+
Self::Hex(e)
89+
}
90+
}

0 commit comments

Comments
 (0)