Skip to content

Commit ae76899

Browse files
author
Victor Roest
committed
Merge branch 'bettersending' into windows
2 parents d076d5c + 78fa50d commit ae76899

File tree

8 files changed

+416
-193
lines changed

8 files changed

+416
-193
lines changed

Cargo.toml

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,41 +7,45 @@ description = "IPv8 implementation for rust."
77
homepage = "https://ip-v8.github.io/rust-ipv8/ipv8/"
88
documentation = "https://ip-v8.github.io/rust-ipv8/ipv8/"
99
repository = "https://github.com/ip-v8/rust-ipv8"
10-
readme = "readme.md"
10+
readme = "README.md"
1111
license-file = "license.md"
12-
13-
[lib]
14-
crate-type = ["lib", "cdylib"]
15-
1612
exclude = [
1713
".editorconfig",
1814
".travis.yml",
1915
".codeclimate.yml"
2016
]
2117

18+
[lib]
19+
crate-type = ["lib", "cdylib"]
20+
2221
[badges]
2322
travis-ci = { repository = "ip-v8/rust-ipv8", branch = "develop" }
2423
maintenance = { status = "experimental" }
2524

2625
[dependencies]
2726
serde = { version = "1.0", features = ["derive"] }
28-
bincode = "1.1.3"
29-
rust_sodium = "0.10.2"
30-
lazy_static = "1.2.0"
27+
28+
bincode = "1.1"
29+
rust_sodium = "0.10"
30+
openssl = { version = "0.10", features = ["vendored"] }
31+
lazy_static = "1.2"
3132
log = "0.4"
32-
mio = "0.6.19"
33-
rayon = "1.0.3"
34-
sha-1 = "0.8.1"
33+
mio = "0.6"
34+
rayon = "1.0"
3535

3636
[dev-dependencies]
37-
criterion = "0.2.11"
38-
simple_logger = "1.3.0"
39-
simulacrum = "0.3.1"
37+
criterion = "0.2"
38+
simple_logger = "1.3"
39+
4040

4141
[[bench]]
4242
name = "bench_crypto"
4343
harness = false
4444

45+
[[bench]]
46+
name = "deserializer_throughput"
47+
harness = false
48+
4549
[profile.release]
4650
lto = true # Enables link time optimization (allows for inlining cross-crate)
4751
opt-level = 3 # Ensures optimization level is set to the maximum

benches/bench_crypto.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,5 +43,5 @@ fn e25519_benchmark(c: &mut Criterion) {
4343
});
4444
}
4545

46-
criterion_group!(benches, e25519_benchmark,);
46+
criterion_group!(benches, e25519_benchmark);
4747
criterion_main!(benches);

benches/deserializer_throughput.rs

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
use criterion::*;
2+
use ipv8::serialization::Packet;
3+
use ipv8::serialization::header::Header;
4+
use ipv8::payloads::timedistributionpayload::TimeDistributionPayload;
5+
use ipv8::payloads::introductionresponsepayload::IntroductionResponsePayload;
6+
use ipv8::payloads::binmemberauthenticationpayload::BinMemberAuthenticationPayload;
7+
8+
/// Critirion benchmark example
9+
/// ```
10+
/// fn my_bench(c: &mut Criterion) {
11+
/// // One-time setup code goes here
12+
/// c.bench_function("my_bench", |b| {
13+
/// // Per-sample (note that a sample can be many iterations) setup goes here
14+
/// b.iter(|| {
15+
/// // Measured code goes here
16+
/// });
17+
/// });
18+
///}
19+
20+
fn throughput(c: &mut Criterion) {
21+
// These are the bytes of packet number 1
22+
static BYTES: [u8; 208] = [
23+
0x00, 0x02, 0xba, 0xf3, 0x0e, 0xd9, 0x19, 0x2b, 0xa3, 0x54, 0xcd, 0xd7, 0xb1, 0x73, 0xe0,
24+
0xef, 0x2c, 0x32, 0x80, 0x27, 0xf1, 0xd3, 0xf5, 0x00, 0x4a, 0x4c, 0x69, 0x62, 0x4e, 0x61,
25+
0x43, 0x4c, 0x50, 0x4b, 0x3a, 0x51, 0xe7, 0x12, 0xc4, 0xeb, 0x8a, 0xc2, 0x5a, 0xe3, 0xa5,
26+
0x68, 0x24, 0x08, 0xb2, 0xad, 0xbd, 0x6b, 0x78, 0xa4, 0x25, 0x54, 0x7f, 0x26, 0x85, 0xcf,
27+
0xdf, 0x1e, 0xe9, 0x27, 0x0c, 0xbe, 0x7e, 0xc3, 0x36, 0xc4, 0x16, 0x0f, 0xf5, 0x72, 0x05,
28+
0x4c, 0x87, 0x78, 0x42, 0xbe, 0x37, 0x73, 0x50, 0x45, 0xa9, 0x3b, 0xc4, 0xe2, 0x04, 0x15,
29+
0x31, 0x6f, 0xdb, 0x14, 0x71, 0x61, 0xa2, 0xd7, 0x46, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
30+
0x00, 0x01, 0x51, 0xab, 0x1b, 0xc2, 0x2b, 0x67, 0xc0, 0xa8, 0x01, 0x4b, 0x1f, 0x9a, 0xc0,
31+
0xa8, 0x01, 0x4b, 0x1f, 0x9a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
32+
0x00, 0x00, 0x00, 0xd2, 0x0e, 0x00, 0x00, 0x00, 0x00, 0xce, 0xe9, 0x32, 0x6b, 0x9d, 0xd4,
33+
0xbb, 0x8a, 0xaf, 0x8d, 0xc0, 0x39, 0x28, 0x8e, 0xbf, 0xc2, 0x4a, 0x10, 0xad, 0xc3, 0x7a,
34+
0xf1, 0xd9, 0xc8, 0x04, 0x17, 0x72, 0x5d, 0x2d, 0x3e, 0x5e, 0x07, 0x52, 0x4d, 0xab, 0x6e,
35+
0xa7, 0x1b, 0x17, 0x5a, 0x77, 0x5d, 0xb5, 0xd8, 0x91, 0x0c, 0x2b, 0x4b, 0xc8, 0xbb, 0x03,
36+
0xd3, 0x55, 0xed, 0x10, 0x26, 0xdd, 0xbb, 0xd8, 0xb2, 0x3b, 0xfd, 0xfc, 0x01,
37+
];
38+
39+
let packet = Packet(BYTES.to_vec());
40+
41+
c.bench(
42+
"throughput",
43+
Benchmark::new("simple-deserialize", |b| {
44+
b.iter(|| {
45+
let data = Packet(BYTES.to_vec());
46+
let mut de = data.start_deserialize();
47+
let _: Header = de.pop_header().unwrap();
48+
// de.verify();
49+
let _: TimeDistributionPayload = de.next_payload().unwrap();
50+
let _: IntroductionResponsePayload = de.next_payload().unwrap();
51+
})
52+
})
53+
.throughput(Throughput::Bytes(BYTES.len() as u32)),
54+
);
55+
56+
c.bench(
57+
"throughput",
58+
Benchmark::new("only-bin-member-auth", |b| {
59+
b.iter(|| {
60+
let data = Packet(BYTES.to_vec());
61+
let de = data.start_deserialize();
62+
let bin: BinMemberAuthenticationPayload =
63+
de.skip_header().unwrap().next_payload().unwrap();
64+
})
65+
})
66+
.throughput(Throughput::Bytes(BYTES.len() as u32)),
67+
);
68+
69+
c.bench(
70+
"throughput",
71+
Benchmark::new("only-verify", |b| {
72+
b.iter(|| {
73+
let data = Packet(BYTES.to_vec());
74+
let de = data.start_deserialize();
75+
de.skip_header().unwrap().verify();
76+
})
77+
})
78+
.throughput(Throughput::Bytes(BYTES.len() as u32)),
79+
);
80+
81+
c.bench(
82+
"throughput",
83+
Benchmark::new("deserialize+verify", |b| {
84+
b.iter(|| {
85+
let data = Packet(BYTES.to_vec());
86+
let mut de = data.start_deserialize();
87+
let _: Header = de.pop_header().unwrap();
88+
de.verify();
89+
let _: TimeDistributionPayload = de.next_payload().unwrap();
90+
let _: IntroductionResponsePayload = de.next_payload().unwrap();
91+
})
92+
})
93+
.throughput(Throughput::Bytes(BYTES.len() as u32)),
94+
);
95+
}
96+
97+
criterion_group!(benches, throughput);
98+
criterion_main!(benches);

examples/ipv8-join.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@
66
use ipv8;
77

88
fn main() {
9-
let new = ipv8::IPv8::new(ipv8::configuration::Config::default());
9+
ipv8::IPv8::new(ipv8::configuration::Config::default());
1010
}

src/community/mod.rs

Lines changed: 42 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use std::error::Error;
44
use std::collections::HashMap;
55
use crate::networking::NetworkManager;
66
use crate::networking::address::Address;
7+
use crate::networking::NetworkSender;
78

89
#[cfg(test)]
910
use std::sync::atomic::AtomicUsize;
@@ -31,102 +32,8 @@ static WARN_DEPRECATED_CALLS: AtomicUsize = AtomicUsize::new(0);
3132
///
3233
/// _**Note:** Try to avoid the use of .unwrap() in actual production code, this is just an example_
3334
///
34-
///
35-
/// ```
36-
/// use ipv8::community::peer::Peer;
37-
/// use ipv8::community::Community;
38-
/// use ipv8::serialization::header::Header;
39-
/// use ipv8::serialization::{PacketDeserializer, Packet};
40-
/// use std::net::{Ipv4Addr, SocketAddr, IpAddr};
41-
/// use ipv8::networking::address::Address;
42-
/// use std::error::Error;
43-
/// use ipv8::IPv8;
44-
/// use ipv8::configuration::Config;
45-
/// use ipv8::serialization::header::HeaderVersion::PyIPV8Header;
46-
/// use ipv8::crypto::keytypes::PublicKey;
47-
/// use ipv8::networking::NetworkManager;
48-
/// use rust_sodium::crypto::sign::ed25519;
49-
///
50-
/// pub struct TestCommunity{
51-
/// peer: Peer
52-
/// }
53-
///
54-
/// impl TestCommunity{
55-
/// }
56-
///
57-
/// impl Community for TestCommunity{
58-
/// fn new(endpoint: &NetworkManager) -> Result<Self, Box<dyn Error>> {
59-
/// // Use the highest available key
60-
/// let seed = ed25519::Seed::from_slice(&[
61-
/// 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22,
62-
/// 23, 24, 25, 26, 27, 28, 29, 30, 31,
63-
/// ])
64-
/// .unwrap();
65-
/// let (pkey1, _) = ed25519::keypair_from_seed(&seed);
66-
/// let (pkey2, _) = ed25519::keypair_from_seed(&seed);
67-
/// let pk = PublicKey(pkey1, pkey2);
68-
/// // Actually create the community
69-
/// Ok(TestCommunity {
70-
/// peer: Peer::new(
71-
/// pk,
72-
/// Address(SocketAddr::new(
73-
/// IpAddr::V4(Ipv4Addr::new(42, 42, 42, 42)),
74-
/// 8000,
75-
/// )),
76-
/// true,
77-
/// ),
78-
/// })
79-
/// }
80-
///
81-
/// // Returns the hash of our master peer
82-
/// fn get_mid(&self) -> Vec<u8> {
83-
/// self.peer.get_sha1().0
84-
/// }
85-
///
86-
/// // The function which will be called when the community receives a packet
87-
/// fn on_receive(&self, header: Header, deserializer: PacketDeserializer, address: Address) -> Result<(),Box<dyn Error>>{
88-
/// # assert_eq!(header.mid_hash.unwrap(), self.get_mid());
89-
/// # assert_eq!(header.version, PyIPV8Header);
90-
/// # assert_eq!(header.message_type, Some(42));
91-
/// // Do some stuff here like to distribute the message based on it's message_type (in the header)
92-
/// // and check it's signature
93-
/// Ok(())
94-
/// }
95-
/// }
96-
///
97-
/// let mut config = Config::default();
98-
/// let mut ipv8 = IPv8::new(config).unwrap();
99-
///
100-
/// let community = TestCommunity::new(&ipv8.networkmanager).unwrap();
101-
/// let mid = community.get_mid();
102-
/// ipv8.communities.add_community(Box::new(community));
103-
///
104-
/// // now simulate a packet coming in
105-
///
106-
/// // Create a packet to test the community with
107-
/// let packet = Packet::new(Header{
108-
/// size: 23,
109-
/// version: PyIPV8Header,
110-
/// mid_hash: Some(mid),
111-
/// message_type: Some(42),
112-
/// }).unwrap();
113-
///
114-
/// // Normally you would want to sign the packet here
115-
///
116-
/// // Send the packet
117-
/// ipv8.communities
118-
/// .forward_message(
119-
/// packet,
120-
/// Address(SocketAddr::new(
121-
/// IpAddr::V4(Ipv4Addr::new(42, 42, 42, 42)),
122-
/// 42,
123-
/// )),
124-
/// )
125-
/// .unwrap();
126-
///
127-
/// ```
12835
pub trait Community {
129-
fn new(endpoint: &NetworkManager) -> Result<Self, Box<dyn Error>>
36+
fn new(endpoint: &NetworkSender) -> Result<Self, Box<dyn Error>>
13037
where
13138
Self: Sized;
13239

@@ -204,15 +111,6 @@ pub trait Community {
204111
deserializer: PacketDeserializer,
205112
address: Address,
206113
) -> Result<(), Box<dyn Error>>;
207-
208-
fn send(
209-
&self,
210-
endpoint: NetworkManager,
211-
address: Address,
212-
packet: Packet,
213-
) -> Result<(), Box<dyn Error>> {
214-
endpoint.send(&address, packet)
215-
}
216114
}
217115

218116
/// Every different kind of community is registered here with it's MID.
@@ -221,6 +119,9 @@ pub trait Community {
221119
/// O(1) lookup time.
222120
pub struct CommunityRegistry {
223121
// mid, community
122+
#[cfg(test)]
123+
pub communities: HashMap<Vec<u8>, Box<dyn Community>>,
124+
#[cfg(not(test))]
224125
communities: HashMap<Vec<u8>, Box<dyn Community>>,
225126
}
226127

@@ -270,8 +171,20 @@ mod tests {
270171
use super::*;
271172
use crate::networking::NetworkManager;
272173
use crate::networking::address::Address;
273-
use std::net::{SocketAddr, IpAddr};
174+
use std::net::{SocketAddr, IpAddr, SocketAddrV4};
274175
use std::error::Error;
176+
use crate::networking::NetworkSender;
177+
use crate::community::peer::Peer;
178+
use crate::community::{Community, CommunityRegistry};
179+
use crate::serialization::header::Header;
180+
use crate::serialization::{PacketDeserializer, Packet};
181+
use std::net::Ipv4Addr;
182+
use crate::IPv8;
183+
use crate::configuration::Config;
184+
use crate::serialization::header::HeaderVersion::PyIPV8Header;
185+
use crate::crypto::keytypes::PublicKey;
186+
use std::sync::atomic::Ordering;
187+
use crate::networking::test_helper::localhost;
275188
use crate::community::peer::Peer;
276189
use crate::serialization::header::Header;
277190
use crate::serialization::{PacketDeserializer, Packet};
@@ -312,17 +225,18 @@ mod tests {
312225
}
313226

314227
// Returns the hash of our master peer
315-
fn get_mid(&self) -> Vec<u8> {
316-
self.peer.get_sha1().0
228+
fn get_mid(&self) -> Option<Vec<u8>> {
229+
Some(self.peer.get_sha1()?.to_vec())
317230
}
318-
231+
319232
// The function which will be called when the community receives a packet
320233
fn on_receive(
321234
&self,
322235
header: Header,
323236
deserializer: PacketDeserializer,
324237
_address: Address,
325238
) -> Result<(), Box<dyn Error>> {
239+
326240
assert_eq!(header.mid_hash.unwrap(), self.get_mid());
327241
assert_eq!(header.version, PyIPV8Header);
328242
assert_eq!(header.message_type, Some(42));
@@ -356,17 +270,35 @@ mod tests {
356270
let header = deser.peek_header().unwrap();
357271
community.receive(header, deser, address).unwrap();
358272
}
359-
360273
assert_eq!(17, WARN_DEPRECATED_CALLS.load(Ordering::SeqCst))
361274
}
362275

363276
#[test]
364277
fn test_networking() {
365278
let config = Config::default();
279+
let ipv8 = IPv8::new(config).unwrap();
280+
let community = Box::new(TestCommunity::new(&ipv8.network_sender).unwrap());
281+
let the_same = Box::new(TestCommunity::new(&ipv8.network_sender).unwrap());
282+
let mid = &*community.get_mid().unwrap();
283+
let mut registry: CommunityRegistry = CommunityRegistry::default();
284+
285+
registry.add_community(community).unwrap();
286+
287+
let get = registry.communities.get(mid).unwrap();
288+
289+
assert_eq!(the_same.get_mid(), get.get_mid()); // TODO: More thorough comparison
290+
}
291+
292+
#[test]
293+
fn test_networking() {
294+
let mut config = Config::default();
295+
config.receiving_address = localhost();
296+
config.sending_address = localhost();
297+
config.buffersize = 2048;
366298

367299
let mut ipv8 = IPv8::new(config).unwrap();
368300

369-
let community = TestCommunity::new(&ipv8.networkmanager).unwrap();
301+
let community = TestCommunity::new(&ipv8.network_sender).unwrap();
370302
let mid = community.get_mid();
371303

372304
ipv8.communities.add_community(Box::new(community)).unwrap();

0 commit comments

Comments
 (0)