Skip to content

Commit

Permalink
Merge pull request #129 from dusk-network/refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
herr-seppia authored Oct 13, 2023
2 parents 6100d07 + 321fa45 commit 2d96429
Show file tree
Hide file tree
Showing 18 changed files with 419 additions and 356 deletions.
2 changes: 1 addition & 1 deletion ARCHITECTURE.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ The `kadcast::peer` is the entry point of the library and the only public API. I
## Message Handler
The `Message Handler` represents the core component of the library. It handles the incoming messages and replies according to the specification.

## Mantainer
## Maintainer
The `Maintainer` performs the [initial bootstrap](../bootstrapping) as well as the `k-table` [maintenance](../periodic-network-manteinance).
It keeps track of idle buckets and is in charge of triggering the routing nodes lookup.

Expand Down
6 changes: 0 additions & 6 deletions HEADER_TEMPLATE

This file was deleted.

2 changes: 1 addition & 1 deletion rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[toolchain]
channel = "nightly-2022-02-19"
channel = "nightly-2023-05-22"
components = ["rustfmt", "clippy"]
1 change: 0 additions & 1 deletion rustfmt.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
max_width = 80
newline_style = "Unix"
wrap_comments = true
license_template_path = "HEADER_TEMPLATE"
47 changes: 40 additions & 7 deletions src/encoding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,30 +22,35 @@ mod tests {

use std::io::{BufReader, BufWriter, Cursor, Read, Seek};

use rand::RngCore;

use super::Marshallable;
use crate::encoding::header::Header;
use crate::encoding::message::Message;
use crate::encoding::payload::{BroadcastPayload, NodePayload};
use crate::encoding::payload::{
BroadcastPayload, NodePayload, PeerEncodedInfo,
};
use crate::peer::PeerNode;
use crate::tests::Result;

#[test]
fn test_encode_ping() -> Result<()> {
let peer = PeerNode::generate("192.168.0.1:666")?;
let a = Message::Ping(peer.as_header());
let a = Message::Ping(peer.to_header());
test_kadkast_marshal(a)
}
#[test]
fn test_encode_pong() -> Result<()> {
let peer = PeerNode::generate("192.168.0.1:666")?;
let a = Message::Pong(peer.as_header());
let a = Message::Pong(peer.to_header());
test_kadkast_marshal(a)
}

#[test]
fn test_encode_find_nodes() -> Result<()> {
let peer = PeerNode::generate("192.168.0.1:666")?;
let target = *PeerNode::generate("192.168.1.1:666")?.id().as_binary();
let a = Message::FindNodes(peer.as_header(), target);
let a = Message::FindNodes(peer.to_header(), target);
test_kadkast_marshal(a)
}

Expand All @@ -61,21 +66,21 @@ mod tests {
.iter()
.map(|f| f.as_peer_info())
.collect();
let a = Message::Nodes(peer.as_header(), NodePayload { peers: nodes });
let a = Message::Nodes(peer.to_header(), NodePayload { peers: nodes });
test_kadkast_marshal(a)
}

#[test]
fn test_encode_empty_nodes() -> Result<()> {
let peer = PeerNode::generate("192.168.0.1:666")?;
let a = Message::Nodes(peer.as_header(), NodePayload { peers: vec![] });
let a = Message::Nodes(peer.to_header(), NodePayload { peers: vec![] });
test_kadkast_marshal(a)
}
#[test]
fn test_encode_broadcast() -> Result<()> {
let peer = PeerNode::generate("192.168.0.1:666")?;
let a = Message::Broadcast(
peer.as_header(),
peer.to_header(),
BroadcastPayload {
height: 10,
gossip_frame: vec![3, 5, 6, 7],
Expand Down Expand Up @@ -103,4 +108,32 @@ mod tests {
assert_eq!(messge, deser);
Ok(())
}
#[test]
fn test_failing() -> Result<()> {
let mut data = [0u8; 4];
rand::thread_rng().fill_bytes(&mut data);
println!("{:?}", data);

let mut reader = BufReader::new(&data[..]);
Message::unmarshal_binary(&mut reader)
.expect_err("Message::unmarshal_binary should fail");

let mut reader = BufReader::new(&data[..]);
Header::unmarshal_binary(&mut reader)
.expect_err("Header::unmarshal_binary should fail");

let mut reader = BufReader::new(&data[..]);
BroadcastPayload::unmarshal_binary(&mut reader)
.expect_err("BroadcastPayload::unmarshal_binary should fail");

let mut reader = BufReader::new(&data[..]);
NodePayload::unmarshal_binary(&mut reader)
.expect_err("NodePayload::unmarshal_binary should fail");

let mut reader = BufReader::new(&data[..]);
PeerEncodedInfo::unmarshal_binary(&mut reader)
.expect_err("PeerEncodedInfo::unmarshal_binary should fail");

Ok(())
}
}
6 changes: 6 additions & 0 deletions src/encoding/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ pub struct Header {
pub(crate) reserved: [u8; 2],
}

impl Header {
pub fn binary_id(&self) -> &BinaryID {
&self.binary_id
}
}

impl Marshallable for Header {
fn marshal_binary<W: Write>(&self, writer: &mut W) -> io::Result<()> {
if !self.binary_id.verify_nonce() {
Expand Down
3 changes: 2 additions & 1 deletion src/encoding/payload/nodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ impl Marshallable for PeerEncodedInfo {
Ok(PeerEncodedInfo { ip, port, id })
}
}

impl Marshallable for NodePayload {
fn marshal_binary<W: Write>(&self, writer: &mut W) -> io::Result<()> {
let len = self.peers.len() as u16;
Expand All @@ -99,7 +100,7 @@ impl Marshallable for NodePayload {
Ok(())
}
fn unmarshal_binary<R: Read>(reader: &mut R) -> io::Result<Self> {
let mut peers: Vec<PeerEncodedInfo> = vec![];
let mut peers = vec![];
let mut len = [0; 2];
reader.read_exact(&mut len)?;
for _ in 0..u16::from_le_bytes(len) {
Expand Down
Loading

0 comments on commit 2d96429

Please sign in to comment.