Skip to content

Commit

Permalink
Merge branch 'bettersending' into windows
Browse files Browse the repository at this point in the history
  • Loading branch information
Victor Roest committed Jun 17, 2019
2 parents d076d5c + 78fa50d commit ae76899
Show file tree
Hide file tree
Showing 8 changed files with 416 additions and 193 deletions.
32 changes: 18 additions & 14 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,41 +7,45 @@ description = "IPv8 implementation for rust."
homepage = "https://ip-v8.github.io/rust-ipv8/ipv8/"
documentation = "https://ip-v8.github.io/rust-ipv8/ipv8/"
repository = "https://github.com/ip-v8/rust-ipv8"
readme = "readme.md"
readme = "README.md"
license-file = "license.md"

[lib]
crate-type = ["lib", "cdylib"]

exclude = [
".editorconfig",
".travis.yml",
".codeclimate.yml"
]

[lib]
crate-type = ["lib", "cdylib"]

[badges]
travis-ci = { repository = "ip-v8/rust-ipv8", branch = "develop" }
maintenance = { status = "experimental" }

[dependencies]
serde = { version = "1.0", features = ["derive"] }
bincode = "1.1.3"
rust_sodium = "0.10.2"
lazy_static = "1.2.0"

bincode = "1.1"
rust_sodium = "0.10"
openssl = { version = "0.10", features = ["vendored"] }
lazy_static = "1.2"
log = "0.4"
mio = "0.6.19"
rayon = "1.0.3"
sha-1 = "0.8.1"
mio = "0.6"
rayon = "1.0"

[dev-dependencies]
criterion = "0.2.11"
simple_logger = "1.3.0"
simulacrum = "0.3.1"
criterion = "0.2"
simple_logger = "1.3"


[[bench]]
name = "bench_crypto"
harness = false

[[bench]]
name = "deserializer_throughput"
harness = false

[profile.release]
lto = true # Enables link time optimization (allows for inlining cross-crate)
opt-level = 3 # Ensures optimization level is set to the maximum
Expand Down
2 changes: 1 addition & 1 deletion benches/bench_crypto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,5 @@ fn e25519_benchmark(c: &mut Criterion) {
});
}

criterion_group!(benches, e25519_benchmark,);
criterion_group!(benches, e25519_benchmark);
criterion_main!(benches);
98 changes: 98 additions & 0 deletions benches/deserializer_throughput.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
use criterion::*;
use ipv8::serialization::Packet;
use ipv8::serialization::header::Header;
use ipv8::payloads::timedistributionpayload::TimeDistributionPayload;
use ipv8::payloads::introductionresponsepayload::IntroductionResponsePayload;
use ipv8::payloads::binmemberauthenticationpayload::BinMemberAuthenticationPayload;

/// Critirion benchmark example
/// ```
/// fn my_bench(c: &mut Criterion) {
/// // One-time setup code goes here
/// c.bench_function("my_bench", |b| {
/// // Per-sample (note that a sample can be many iterations) setup goes here
/// b.iter(|| {
/// // Measured code goes here
/// });
/// });
///}
fn throughput(c: &mut Criterion) {
// These are the bytes of packet number 1
static BYTES: [u8; 208] = [
0x00, 0x02, 0xba, 0xf3, 0x0e, 0xd9, 0x19, 0x2b, 0xa3, 0x54, 0xcd, 0xd7, 0xb1, 0x73, 0xe0,
0xef, 0x2c, 0x32, 0x80, 0x27, 0xf1, 0xd3, 0xf5, 0x00, 0x4a, 0x4c, 0x69, 0x62, 0x4e, 0x61,
0x43, 0x4c, 0x50, 0x4b, 0x3a, 0x51, 0xe7, 0x12, 0xc4, 0xeb, 0x8a, 0xc2, 0x5a, 0xe3, 0xa5,
0x68, 0x24, 0x08, 0xb2, 0xad, 0xbd, 0x6b, 0x78, 0xa4, 0x25, 0x54, 0x7f, 0x26, 0x85, 0xcf,
0xdf, 0x1e, 0xe9, 0x27, 0x0c, 0xbe, 0x7e, 0xc3, 0x36, 0xc4, 0x16, 0x0f, 0xf5, 0x72, 0x05,
0x4c, 0x87, 0x78, 0x42, 0xbe, 0x37, 0x73, 0x50, 0x45, 0xa9, 0x3b, 0xc4, 0xe2, 0x04, 0x15,
0x31, 0x6f, 0xdb, 0x14, 0x71, 0x61, 0xa2, 0xd7, 0x46, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x01, 0x51, 0xab, 0x1b, 0xc2, 0x2b, 0x67, 0xc0, 0xa8, 0x01, 0x4b, 0x1f, 0x9a, 0xc0,
0xa8, 0x01, 0x4b, 0x1f, 0x9a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0xd2, 0x0e, 0x00, 0x00, 0x00, 0x00, 0xce, 0xe9, 0x32, 0x6b, 0x9d, 0xd4,
0xbb, 0x8a, 0xaf, 0x8d, 0xc0, 0x39, 0x28, 0x8e, 0xbf, 0xc2, 0x4a, 0x10, 0xad, 0xc3, 0x7a,
0xf1, 0xd9, 0xc8, 0x04, 0x17, 0x72, 0x5d, 0x2d, 0x3e, 0x5e, 0x07, 0x52, 0x4d, 0xab, 0x6e,
0xa7, 0x1b, 0x17, 0x5a, 0x77, 0x5d, 0xb5, 0xd8, 0x91, 0x0c, 0x2b, 0x4b, 0xc8, 0xbb, 0x03,
0xd3, 0x55, 0xed, 0x10, 0x26, 0xdd, 0xbb, 0xd8, 0xb2, 0x3b, 0xfd, 0xfc, 0x01,
];

let packet = Packet(BYTES.to_vec());

c.bench(
"throughput",
Benchmark::new("simple-deserialize", |b| {
b.iter(|| {
let data = Packet(BYTES.to_vec());
let mut de = data.start_deserialize();
let _: Header = de.pop_header().unwrap();
// de.verify();
let _: TimeDistributionPayload = de.next_payload().unwrap();
let _: IntroductionResponsePayload = de.next_payload().unwrap();
})
})
.throughput(Throughput::Bytes(BYTES.len() as u32)),
);

c.bench(
"throughput",
Benchmark::new("only-bin-member-auth", |b| {
b.iter(|| {
let data = Packet(BYTES.to_vec());
let de = data.start_deserialize();
let bin: BinMemberAuthenticationPayload =
de.skip_header().unwrap().next_payload().unwrap();
})
})
.throughput(Throughput::Bytes(BYTES.len() as u32)),
);

c.bench(
"throughput",
Benchmark::new("only-verify", |b| {
b.iter(|| {
let data = Packet(BYTES.to_vec());
let de = data.start_deserialize();
de.skip_header().unwrap().verify();
})
})
.throughput(Throughput::Bytes(BYTES.len() as u32)),
);

c.bench(
"throughput",
Benchmark::new("deserialize+verify", |b| {
b.iter(|| {
let data = Packet(BYTES.to_vec());
let mut de = data.start_deserialize();
let _: Header = de.pop_header().unwrap();
de.verify();
let _: TimeDistributionPayload = de.next_payload().unwrap();
let _: IntroductionResponsePayload = de.next_payload().unwrap();
})
})
.throughput(Throughput::Bytes(BYTES.len() as u32)),
);
}

criterion_group!(benches, throughput);
criterion_main!(benches);
2 changes: 1 addition & 1 deletion examples/ipv8-join.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@
use ipv8;

fn main() {
let new = ipv8::IPv8::new(ipv8::configuration::Config::default());
ipv8::IPv8::new(ipv8::configuration::Config::default());
}
152 changes: 42 additions & 110 deletions src/community/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::error::Error;
use std::collections::HashMap;
use crate::networking::NetworkManager;
use crate::networking::address::Address;
use crate::networking::NetworkSender;

#[cfg(test)]
use std::sync::atomic::AtomicUsize;
Expand Down Expand Up @@ -31,102 +32,8 @@ static WARN_DEPRECATED_CALLS: AtomicUsize = AtomicUsize::new(0);
///
/// _**Note:** Try to avoid the use of .unwrap() in actual production code, this is just an example_
///
///
/// ```
/// use ipv8::community::peer::Peer;
/// use ipv8::community::Community;
/// use ipv8::serialization::header::Header;
/// use ipv8::serialization::{PacketDeserializer, Packet};
/// use std::net::{Ipv4Addr, SocketAddr, IpAddr};
/// use ipv8::networking::address::Address;
/// use std::error::Error;
/// use ipv8::IPv8;
/// use ipv8::configuration::Config;
/// use ipv8::serialization::header::HeaderVersion::PyIPV8Header;
/// use ipv8::crypto::keytypes::PublicKey;
/// use ipv8::networking::NetworkManager;
/// use rust_sodium::crypto::sign::ed25519;
///
/// pub struct TestCommunity{
/// peer: Peer
/// }
///
/// impl TestCommunity{
/// }
///
/// impl Community for TestCommunity{
/// fn new(endpoint: &NetworkManager) -> Result<Self, Box<dyn Error>> {
/// // Use the highest available key
/// let seed = ed25519::Seed::from_slice(&[
/// 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22,
/// 23, 24, 25, 26, 27, 28, 29, 30, 31,
/// ])
/// .unwrap();
/// let (pkey1, _) = ed25519::keypair_from_seed(&seed);
/// let (pkey2, _) = ed25519::keypair_from_seed(&seed);
/// let pk = PublicKey(pkey1, pkey2);
/// // Actually create the community
/// Ok(TestCommunity {
/// peer: Peer::new(
/// pk,
/// Address(SocketAddr::new(
/// IpAddr::V4(Ipv4Addr::new(42, 42, 42, 42)),
/// 8000,
/// )),
/// true,
/// ),
/// })
/// }
///
/// // Returns the hash of our master peer
/// fn get_mid(&self) -> Vec<u8> {
/// self.peer.get_sha1().0
/// }
///
/// // The function which will be called when the community receives a packet
/// fn on_receive(&self, header: Header, deserializer: PacketDeserializer, address: Address) -> Result<(),Box<dyn Error>>{
/// # assert_eq!(header.mid_hash.unwrap(), self.get_mid());
/// # assert_eq!(header.version, PyIPV8Header);
/// # assert_eq!(header.message_type, Some(42));
/// // Do some stuff here like to distribute the message based on it's message_type (in the header)
/// // and check it's signature
/// Ok(())
/// }
/// }
///
/// let mut config = Config::default();
/// let mut ipv8 = IPv8::new(config).unwrap();
///
/// let community = TestCommunity::new(&ipv8.networkmanager).unwrap();
/// let mid = community.get_mid();
/// ipv8.communities.add_community(Box::new(community));
///
/// // now simulate a packet coming in
///
/// // Create a packet to test the community with
/// let packet = Packet::new(Header{
/// size: 23,
/// version: PyIPV8Header,
/// mid_hash: Some(mid),
/// message_type: Some(42),
/// }).unwrap();
///
/// // Normally you would want to sign the packet here
///
/// // Send the packet
/// ipv8.communities
/// .forward_message(
/// packet,
/// Address(SocketAddr::new(
/// IpAddr::V4(Ipv4Addr::new(42, 42, 42, 42)),
/// 42,
/// )),
/// )
/// .unwrap();
///
/// ```
pub trait Community {
fn new(endpoint: &NetworkManager) -> Result<Self, Box<dyn Error>>
fn new(endpoint: &NetworkSender) -> Result<Self, Box<dyn Error>>
where
Self: Sized;

Expand Down Expand Up @@ -204,15 +111,6 @@ pub trait Community {
deserializer: PacketDeserializer,
address: Address,
) -> Result<(), Box<dyn Error>>;

fn send(
&self,
endpoint: NetworkManager,
address: Address,
packet: Packet,
) -> Result<(), Box<dyn Error>> {
endpoint.send(&address, packet)
}
}

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

Expand Down Expand Up @@ -270,8 +171,20 @@ mod tests {
use super::*;
use crate::networking::NetworkManager;
use crate::networking::address::Address;
use std::net::{SocketAddr, IpAddr};
use std::net::{SocketAddr, IpAddr, SocketAddrV4};
use std::error::Error;
use crate::networking::NetworkSender;
use crate::community::peer::Peer;
use crate::community::{Community, CommunityRegistry};
use crate::serialization::header::Header;
use crate::serialization::{PacketDeserializer, Packet};
use std::net::Ipv4Addr;
use crate::IPv8;
use crate::configuration::Config;
use crate::serialization::header::HeaderVersion::PyIPV8Header;
use crate::crypto::keytypes::PublicKey;
use std::sync::atomic::Ordering;
use crate::networking::test_helper::localhost;
use crate::community::peer::Peer;
use crate::serialization::header::Header;
use crate::serialization::{PacketDeserializer, Packet};
Expand Down Expand Up @@ -312,17 +225,18 @@ mod tests {
}

// Returns the hash of our master peer
fn get_mid(&self) -> Vec<u8> {
self.peer.get_sha1().0
fn get_mid(&self) -> Option<Vec<u8>> {
Some(self.peer.get_sha1()?.to_vec())
}

// The function which will be called when the community receives a packet
fn on_receive(
&self,
header: Header,
deserializer: PacketDeserializer,
_address: Address,
) -> Result<(), Box<dyn Error>> {

assert_eq!(header.mid_hash.unwrap(), self.get_mid());
assert_eq!(header.version, PyIPV8Header);
assert_eq!(header.message_type, Some(42));
Expand Down Expand Up @@ -356,17 +270,35 @@ mod tests {
let header = deser.peek_header().unwrap();
community.receive(header, deser, address).unwrap();
}

assert_eq!(17, WARN_DEPRECATED_CALLS.load(Ordering::SeqCst))
}

#[test]
fn test_networking() {
let config = Config::default();
let ipv8 = IPv8::new(config).unwrap();
let community = Box::new(TestCommunity::new(&ipv8.network_sender).unwrap());
let the_same = Box::new(TestCommunity::new(&ipv8.network_sender).unwrap());
let mid = &*community.get_mid().unwrap();
let mut registry: CommunityRegistry = CommunityRegistry::default();

registry.add_community(community).unwrap();

let get = registry.communities.get(mid).unwrap();

assert_eq!(the_same.get_mid(), get.get_mid()); // TODO: More thorough comparison
}

#[test]
fn test_networking() {
let mut config = Config::default();
config.receiving_address = localhost();
config.sending_address = localhost();
config.buffersize = 2048;

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

let community = TestCommunity::new(&ipv8.networkmanager).unwrap();
let community = TestCommunity::new(&ipv8.network_sender).unwrap();
let mid = community.get_mid();

ipv8.communities.add_community(Box::new(community)).unwrap();
Expand Down
Loading

0 comments on commit ae76899

Please sign in to comment.