Skip to content

Commit 6cc960c

Browse files
committed
negentropy: protocol v1 cleanup and fixes
1 parent 4e72f7a commit 6cc960c

File tree

12 files changed

+291
-247
lines changed

12 files changed

+291
-247
lines changed

Cargo.lock

Lines changed: 1 addition & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

negentropy/Cargo.toml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,4 @@ default = ["std"]
1414
std = []
1515

1616
[[example]]
17-
name = "negentropy"
18-
19-
[dependencies]
20-
byteorder = "0.3.13"
17+
name = "negentropy"

negentropy/examples/negentropy.rs

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,69 @@
11
// Copyright (c) 2023 Yuki Kishimoto
22
// Distributed under the MIT software license
33

4-
use negentropy::{Bytes, Negentropy};
5-
use negentropy::storage::{NegentropyStorageVector};
4+
use negentropy::{Bytes, Negentropy, NegentropyStorageVector};
65

76
fn main() {
87
// Client
9-
let mut storage_client = NegentropyStorageVector::new().unwrap();
8+
let mut storage_client = NegentropyStorageVector::new();
109
storage_client
1110
.insert(
1211
0,
13-
Bytes::from_hex("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa").unwrap(),
12+
Bytes::from_hex("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
13+
.unwrap(),
1414
)
1515
.unwrap();
1616
storage_client
1717
.insert(
1818
1,
19-
Bytes::from_hex("bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb").unwrap(),
19+
Bytes::from_hex("bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb")
20+
.unwrap(),
2021
)
2122
.unwrap();
2223
storage_client.seal().unwrap();
23-
let mut client = Negentropy::new(&mut storage_client, 0).unwrap();
24+
let mut client = Negentropy::new(storage_client, 0).unwrap();
2425
let init_output = client.initiate().unwrap();
2526
println!("Initiator Output: {}", init_output.as_hex());
2627

2728
// Relay
28-
let mut storage_relay = NegentropyStorageVector::new().unwrap();
29+
let mut storage_relay = NegentropyStorageVector::new();
2930
storage_relay
3031
.insert(
3132
0,
32-
Bytes::from_hex("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa").unwrap(),
33+
Bytes::from_hex("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
34+
.unwrap(),
3335
)
3436
.unwrap();
3537
storage_relay
3638
.insert(
3739
2,
38-
Bytes::from_hex("cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc").unwrap(),
40+
Bytes::from_hex("cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc")
41+
.unwrap(),
3942
)
4043
.unwrap();
4144
storage_relay
4245
.insert(
4346
3,
44-
Bytes::from_hex("1111111111111111111111111111111111111111111111111111111111111111").unwrap(),
47+
Bytes::from_hex("1111111111111111111111111111111111111111111111111111111111111111")
48+
.unwrap(),
4549
)
4650
.unwrap();
4751
storage_relay
4852
.insert(
4953
5,
50-
Bytes::from_hex("2222222222222222222222222222222222222222222222222222222222222222").unwrap(),
54+
Bytes::from_hex("2222222222222222222222222222222222222222222222222222222222222222")
55+
.unwrap(),
5156
)
5257
.unwrap();
5358
storage_relay
5459
.insert(
5560
10,
56-
Bytes::from_hex("3333333333333333333333333333333333333333333333333333333333333333").unwrap(),
61+
Bytes::from_hex("3333333333333333333333333333333333333333333333333333333333333333")
62+
.unwrap(),
5763
)
5864
.unwrap();
5965
storage_relay.seal().unwrap();
60-
let mut relay = Negentropy::new(&mut storage_relay, 0).unwrap();
66+
let mut relay = Negentropy::new(storage_relay, 0).unwrap();
6167
let reconcile_output = relay.reconcile(&init_output).unwrap();
6268
println!("Reconcile Output: {}", reconcile_output.as_hex());
6369

negentropy/fuzz/harness/src/main.rs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1+
// Copyright (c) 2023 Doug Hoyte
2+
// Distributed under the MIT software license
3+
14
// This is a testing harness for compatibility with the negentropy reference
25
// implementation's test suite: https://github.com/hoytech/negentropy/tree/master/test
36

4-
use std::io::BufRead;
5-
use std::{env, io};
6-
7-
use negentropy::{Bytes, Negentropy};
8-
use negentropy::storage::{NegentropyStorageVector};
7+
use std::env;
8+
use std::io::{self, BufRead};
99

10+
use negentropy::{Bytes, Negentropy, NegentropyStorageVector};
1011

1112
fn main() {
1213
let frame_size_limit_env_var = env::var("FRAMESIZELIMIT");
@@ -16,7 +17,7 @@ fn main() {
1617
0
1718
};
1819

19-
let mut storage = NegentropyStorageVector::new().unwrap();
20+
let mut storage = NegentropyStorageVector::new();
2021

2122
for line in io::stdin().lock().lines() {
2223
let line_unwrapped = line.unwrap();
@@ -25,7 +26,9 @@ fn main() {
2526
if items[0] == "item" {
2627
let created = items[1].parse::<u64>().unwrap();
2728
let id = items[2];
28-
storage.insert(created, Bytes::from_hex(id).unwrap()).unwrap();
29+
storage
30+
.insert(created, Bytes::from_hex(id).unwrap())
31+
.unwrap();
2932
} else if items[0] == "seal" {
3033
storage.seal().unwrap();
3134
break;
@@ -34,7 +37,7 @@ fn main() {
3437
}
3538
}
3639

37-
let mut ne = Negentropy::new(&mut storage, frame_size_limit as u64).unwrap();
40+
let mut ne = Negentropy::new(storage, frame_size_limit as u64).unwrap();
3841

3942
for line in io::stdin().lock().lines() {
4043
let line_unwrapped = line.unwrap();
@@ -53,7 +56,7 @@ fn main() {
5356
q = items[1].to_string();
5457
}
5558

56-
if ne.is_initiator {
59+
if ne.is_initiator() {
5760
let mut have_ids = Vec::new();
5861
let mut need_ids = Vec::new();
5962
let resp = ne

negentropy/fuzz/perf/src/main.rs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,26 @@
33

44
use std::time::Instant;
55

6-
use negentropy::{Bytes, Negentropy};
7-
use negentropy::storage::{NegentropyStorageVector};
6+
use negentropy::{Bytes, Negentropy, NegentropyStorageVector};
87

98
fn main() {
109
let items = relay_set();
1110

1211
// Client
13-
let mut storage_client = NegentropyStorageVector::new().unwrap();
12+
let mut storage_client = NegentropyStorageVector::new();
1413
storage_client
15-
.insert(0, Bytes::from_hex("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa").unwrap())
14+
.insert(
15+
0,
16+
Bytes::from_hex("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
17+
.unwrap(),
18+
)
1619
.unwrap();
1720
storage_client
18-
.insert(1, Bytes::from_hex("bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb").unwrap())
21+
.insert(
22+
1,
23+
Bytes::from_hex("bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb")
24+
.unwrap(),
25+
)
1926
.unwrap();
2027
storage_client.seal().unwrap();
2128
let mut client = Negentropy::new(&mut storage_client, 0).unwrap();
@@ -24,7 +31,7 @@ fn main() {
2431
println!("Client init took {} ms", now.elapsed().as_millis());
2532

2633
// Relay
27-
let mut storage_relay = NegentropyStorageVector::new().unwrap();
34+
let mut storage_relay = NegentropyStorageVector::new();
2835
println!("Relay items: {}", items.len());
2936
for (index, item) in items.into_iter().enumerate() {
3037
storage_relay

negentropy/src/bytes.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
1+
// Copyright (c) 2023 Doug Hoyte
12
// Copyright (c) 2023 Yuki Kishimoto
23
// Distributed under the MIT software license
34

45
use alloc::string::String;
56
use alloc::vec::Vec;
67
use core::ops::Deref;
78

8-
use crate::error;
9+
use crate::error::Error;
910
use crate::hex;
1011

11-
pub use self::error::Error;
12-
13-
1412
/// Bytes
1513
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
1614
pub struct Bytes(Vec<u8>);

negentropy/src/constants.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// Copyright (c) 2023 Doug Hoyte
2+
// Copyright (c) 2023 Yuki Kishimoto
3+
// Distributed under the MIT software license
4+
5+
/// Implemented protocol version
6+
pub const PROTOCOL_VERSION: u64 = 0x61; // Version 1
7+
/// ID Size
8+
pub const ID_SIZE: usize = 32;
9+
/// Fingerprint Size
10+
pub const FINGERPRINT_SIZE: usize = 16;

negentropy/src/encoding.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1-
use crate::error;
1+
// Copyright (c) 2023 Doug Hoyte
2+
// Copyright (c) 2023 Yuki Kishimoto
3+
// Distributed under the MIT software license
24

3-
use self::error::Error;
5+
use alloc::vec;
6+
use alloc::vec::Vec;
47

8+
use crate::error::Error;
59

610
pub fn get_bytes(encoded: &mut &[u8], n: usize) -> Result<Vec<u8>, Error> {
711
if encoded.len() < n {
@@ -26,7 +30,6 @@ pub fn decode_var_int(encoded: &mut &[u8]) -> Result<u64, Error> {
2630
Ok(res)
2731
}
2832

29-
3033
pub fn encode_var_int(mut n: u64) -> Vec<u8> {
3134
if n == 0 {
3235
return vec![0];

negentropy/src/error.rs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
1+
// Copyright (c) 2023 Doug Hoyte
2+
// Copyright (c) 2023 Yuki Kishimoto
3+
// Distributed under the MIT software license
4+
5+
use alloc::string::{String, ToString};
6+
use core::array::TryFromSliceError;
17
use core::fmt;
28

39
use crate::hex;
410

5-
611
/// Error
712
#[derive(Debug, PartialEq, Eq)]
813
pub enum Error {
@@ -32,6 +37,8 @@ pub enum Error {
3237
ParseEndsPrematurely,
3338
/// Duplicate item added
3439
DuplicateItemAdded,
40+
/// Protocol version not found
41+
ProtocolVersionNotFound,
3542
/// Invalid protocol version
3643
InvalidProtocolVersion,
3744
/// Unsupported protocol version
@@ -45,6 +52,8 @@ pub enum Error {
4552
},
4653
/// Hex error
4754
Hex(hex::Error),
55+
/// Try from slice error
56+
TryFromSlice(String),
4857
/// Bad range
4958
BadRange,
5059
}
@@ -68,6 +77,7 @@ impl fmt::Display for Error {
6877
Self::UnexpectedMode(m) => write!(f, "Unexpected mode: {}", m),
6978
Self::ParseEndsPrematurely => write!(f, "parse ends prematurely"),
7079
Self::DuplicateItemAdded => write!(f, "duplicate item added"),
80+
Self::ProtocolVersionNotFound => write!(f, "protocol version not found"),
7181
Self::InvalidProtocolVersion => write!(f, "invalid negentropy protocol version byte"),
7282
Self::UnsupportedProtocolVersion => {
7383
write!(f, "server does not support our negentropy protocol version")
@@ -78,6 +88,7 @@ impl fmt::Display for Error {
7888
expected, found
7989
),
8090
Self::Hex(e) => write!(f, "Hex: {}", e),
91+
Self::TryFromSlice(e) => write!(f, "Try from slice: {}", e),
8192
Self::BadRange => write!(f, "bad range"),
8293
}
8394
}
@@ -88,3 +99,9 @@ impl From<hex::Error> for Error {
8899
Self::Hex(e)
89100
}
90101
}
102+
103+
impl From<TryFromSliceError> for Error {
104+
fn from(e: TryFromSliceError) -> Self {
105+
Self::TryFromSlice(e.to_string())
106+
}
107+
}

0 commit comments

Comments
 (0)