Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added

- Added MSRV, set to rust version `1.85`

### Changed

- Change rust edition to 2024
- Change rust toolchain to stable

## [0.7.0] - 2024-10-21

### Added
Expand Down
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
name = "kadcast"
authors = ["herr-seppia <seppia@dusk.network>"]
version = "0.7.0"
edition = "2018"
edition = "2024"
rust-version = "1.85"
description = "Implementation of the Kadcast Network Protocol."
categories = ["network-programming"]
keywords = ["p2p", "network", "kad", "peer-to-peer", "kadcast"]
Expand All @@ -20,7 +21,6 @@ tokio = { version = "1", features = ["rt", "net", "sync", "time", "io-std", "rt-
raptorq = { version = "2.0", optional = true }
tracing = "0.1"
itertools = "0.10"
konst = "0.2"
socket2 = "0.4"
serde_derive = "1"
serde = "1"
Expand Down
2 changes: 1 addition & 1 deletion examples/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use std::io::{self, BufRead};
use clap::{App, Arg};
use kadcast::config::Config;
use kadcast::{MessageInfo, NetworkListen, Peer};
use rustc_tools_util::{get_version_info, VersionInfo};
use rustc_tools_util::{VersionInfo, get_version_info};
use serde_derive::{Deserialize, Serialize};

#[derive(Serialize, Deserialize)]
Expand Down
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-2023-05-22"
channel = "stable"
components = ["rustfmt", "clippy"]
1 change: 0 additions & 1 deletion rustfmt.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
max_width = 80
newline_style = "Unix"
wrap_comments = true
6 changes: 3 additions & 3 deletions src/encoding/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
//
// Copyright (c) DUSK NETWORK. All rights reserved.

use std::io::{self, Error, ErrorKind, Read, Write};
use std::io::{self, Error, Read, Write};

use super::Marshallable;
use crate::{kbucket::BinaryID, K_ID_LEN_BYTES, K_NONCE_LEN};
use crate::{K_ID_LEN_BYTES, K_NONCE_LEN, kbucket::BinaryID};

#[derive(Debug, PartialEq, Clone, Copy)]
pub struct Header {
Expand All @@ -26,7 +26,7 @@ impl Header {
impl Marshallable for Header {
fn marshal_binary<W: Write>(&self, writer: &mut W) -> io::Result<()> {
if !self.binary_id.verify_nonce() {
return Err(Error::new(ErrorKind::Other, "Invalid Nonce"));
return Err(Error::other("Invalid Nonce"));
}
writer.write_all(self.binary_id.as_binary())?;
writer.write_all(self.binary_id.nonce())?;
Expand Down
12 changes: 6 additions & 6 deletions src/encoding/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
//
// Copyright (c) DUSK NETWORK. All rights reserved.

use std::io::{self, Error, ErrorKind, Read, Write};
use std::io::{self, Error, Read, Write};

use semver::Version;

pub(crate) use super::payload::{BroadcastPayload, NodePayload};
pub use super::{header::Header, Marshallable};
pub use super::{Marshallable, header::Header};
use crate::kbucket::BinaryKey;

// PingMsg wire Ping message id.
Expand Down Expand Up @@ -159,10 +159,10 @@ impl Marshallable for Message {
let payload = BroadcastPayload::unmarshal_binary(reader)?;
Ok(Message::broadcast(header, payload))
}
unknown => Err(Error::new(
ErrorKind::Other,
format!("Invalid message type: '{}'", unknown),
)),
unknown => Err(Error::other(format!(
"Invalid message type: '{}'",
unknown
))),
}
}
}
2 changes: 1 addition & 1 deletion src/encoding/payload/nodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::convert::TryInto;
use std::io::{self, Read, Write};
use std::net::{Ipv4Addr, Ipv6Addr, SocketAddr, SocketAddrV4, SocketAddrV6};

use crate::{encoding::Marshallable, kbucket::BinaryKey, K_ID_LEN_BYTES};
use crate::{K_ID_LEN_BYTES, encoding::Marshallable, kbucket::BinaryKey};

#[derive(Debug, PartialEq)]
pub(crate) struct NodePayload {
Expand Down
2 changes: 1 addition & 1 deletion src/handling.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use crate::encoding::message::{
use crate::kbucket::{BinaryKey, NodeInsertError, NodeInsertOk, Tree};
use crate::peer::{PeerInfo, PeerNode};
use crate::transport::{MessageBeanIn, MessageBeanOut};
use crate::{RwLock, K_K};
use crate::{K_K, RwLock};

/// Message metadata for incoming message notifications
#[derive(Debug)]
Expand Down
18 changes: 9 additions & 9 deletions src/kbucket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@ pub use bucket::InsertOk;
pub use bucket::{NodeInsertError, NodeInsertOk};
use itertools::Itertools;
pub use key::MAX_BUCKET_HEIGHT;
pub use key::{BinaryID, BinaryKey, BinaryNonce};
pub use key::{BinaryID, BinaryKey};
pub use node::Node;
use std::collections::hash_map::Entry;
use tracing::info;

mod bucket;
mod key;
mod node;
use crate::config::BucketConfig;
use crate::K_BETA;
use crate::config::BucketConfig;

pub type BucketHeight = u8;

Expand All @@ -35,7 +35,7 @@ impl<V> Tree<V> {
pub fn insert(
&mut self,
node: Node<V>,
) -> Result<InsertOk<V>, InsertError<V>> {
) -> Result<InsertOk<'_, V>, InsertError<V>> {
if self.root().network_id != node.network_id {
return Err(NodeInsertError::MismatchNetwork(node));
}
Expand All @@ -48,7 +48,7 @@ impl<V> Tree<V> {
pub fn refresh(
&mut self,
node: Node<V>,
) -> Result<InsertOk<V>, InsertError<V>> {
) -> Result<InsertOk<'_, V>, InsertError<V>> {
if self.root().network_id != node.network_id {
return Err(NodeInsertError::MismatchNetwork(node));
}
Expand All @@ -59,10 +59,10 @@ impl<V> Tree<V> {
}

fn get_or_create_bucket(&mut self, height: BucketHeight) -> &mut Bucket<V> {
return match self.buckets.entry(height) {
match self.buckets.entry(height) {
Entry::Occupied(o) => o.into_mut(),
Entry::Vacant(v) => v.insert(Bucket::new(self.config)),
};
}
}

// iter the buckets (up to max_height, inclusive) and pick at most Beta
Expand All @@ -75,7 +75,7 @@ impl<V> Tree<V> {
let max_h = max_h.unwrap_or(BucketHeight::MAX);
self.buckets
.iter()
.filter(move |(&height, _)| height <= max_h)
.filter(move |&(&height, _)| height <= max_h)
.map(|(&height, bucket)| (height, bucket.pick::<K_BETA>()))
}

Expand All @@ -86,7 +86,7 @@ impl<V> Tree<V> {
pub(crate) fn closest_peers<const ITEM_COUNT: usize>(
&self,
other: &BinaryKey,
) -> impl Iterator<Item = &Node<V>> {
) -> impl Iterator<Item = &Node<V>> + use<'_, ITEM_COUNT, V> {
self.buckets
.iter()
.flat_map(|(_, b)| b.peers())
Expand Down Expand Up @@ -162,7 +162,7 @@ impl<V> Tree<V> {
pub(crate) fn is_bucket_full(&self, height: BucketHeight) -> bool {
self.buckets
.get(&height)
.map_or(false, |bucket| bucket.is_full())
.is_some_and(|bucket| bucket.is_full())
}

pub(crate) fn bucket_size(&self, height: BucketHeight) -> usize {
Expand Down
15 changes: 7 additions & 8 deletions src/kbucket/bucket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ use rand::seq::SliceRandom;
use rand::thread_rng;
use semver::Version;

use super::node::{Node, NodeEvictionStatus};
use super::BinaryKey;
use crate::config::BucketConfig;
use super::node::{Node, NodeEvictionStatus};
use crate::K_K;
use crate::config::BucketConfig;

/// Represents a bucket for storing nodes in a Kademlia routing table.
pub(super) struct Bucket<V> {
Expand Down Expand Up @@ -153,7 +153,7 @@ impl<V> Bucket<V> {
pub fn insert(
&mut self,
node: Node<V>,
) -> Result<InsertOk<V>, InsertError<V>> {
) -> Result<InsertOk<'_, V>, InsertError<V>> {
if !node.id().verify_nonce() {
return Err(NodeInsertError::Invalid(node));
}
Expand Down Expand Up @@ -199,7 +199,7 @@ impl<V> Bucket<V> {
pub fn refresh(
&mut self,
node: Node<V>,
) -> Result<InsertOk<V>, InsertError<V>> {
) -> Result<InsertOk<'_, V>, InsertError<V>> {
if !node.id().verify_nonce() {
return Err(NodeInsertError::Invalid(node));
}
Expand Down Expand Up @@ -243,7 +243,7 @@ impl<V> Bucket<V> {

/// Checks if the bucket has at least one idle node.
pub(crate) fn has_idle(&self) -> bool {
self.nodes.first().map_or(false, |n| {
self.nodes.first().is_some_and(|n| {
Comment thread
herr-seppia marked this conversation as resolved.
n.seen_at.elapsed() > self.bucket_config.bucket_ttl
})
}
Expand Down Expand Up @@ -286,13 +286,12 @@ impl<V> Bucket<V> {
let node_idx =
self.nodes.iter().position(|s| s.id().as_binary() == id)?;

self.nodes.pop_at(node_idx).map(|removed| {
self.nodes.pop_at(node_idx).inspect(|_removed| {
if let Some(pending) = self.pending_node.take() {
if pending.is_alive(self.bucket_config.node_ttl) {
self.nodes.push(pending);
}
}
removed
})
}
}
Expand All @@ -303,10 +302,10 @@ mod tests {
use std::time::Duration;

use super::*;
use crate::K_BETA;
use crate::kbucket::Tree;
use crate::peer::PeerNode;
use crate::tests::Result;
use crate::K_BETA;

impl<V> Bucket<V> {
pub fn last_id(&self) -> Option<&BinaryKey> {
Expand Down
6 changes: 3 additions & 3 deletions src/kbucket/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@

use std::io;

use crate::encoding::Marshallable;
use crate::K_ID_LEN_BYTES;
use crate::K_NONCE_LEN;
use crate::encoding::Marshallable;

pub type BinaryKey = [u8; K_ID_LEN_BYTES];
pub type BinaryNonce = [u8; K_NONCE_LEN];
Expand Down Expand Up @@ -127,7 +127,7 @@ impl BinaryID {
if ret.verify_nonce() {
Ok(ret)
} else {
Err(io::Error::new(io::ErrorKind::Other, "Invalid Nonce"))
Err(io::Error::other("Invalid Nonce"))
}
}

Expand Down Expand Up @@ -177,7 +177,7 @@ impl BinaryID {
where
I: Iterator<Item = &'a u8>,
{
bytes.next().map_or(false, |b| {
bytes.next().is_some_and(|b| {
if difficulty <= 8 {
b.trailing_zeros() as usize >= difficulty
} else if b != &0 {
Expand Down
2 changes: 1 addition & 1 deletion src/kbucket/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

use std::time::{Duration, Instant};

use super::key::BinaryID;
use super::BucketHeight;
use super::key::BinaryID;

/// A struct representing a node in the network with an associated ID, value,
/// and eviction status.
Expand Down
6 changes: 4 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ const K_DIFF_PRODUCED_BIT: usize = 8;

const fn get_k_k() -> usize {
match option_env!("KADCAST_K") {
Some(v) => match konst::primitive::parse_usize(v) {
Some(v) => match usize::from_str_radix(v, 10) {
Comment thread
HDauven marked this conversation as resolved.
Ok(e) => e,
Err(_) => DEFAULT_K_K,
},
Expand Down Expand Up @@ -243,7 +243,9 @@ impl Peer {
const LAST_BUCKET_IDX: u8 = MAX_BUCKET_HEIGHT as u8 - 1;
let ktable = self.ktable.read().await;
if height.is_none() && ktable.bucket_size(LAST_BUCKET_IDX) == 0 {
warn!("Broadcasting a new message with empty bucket height {LAST_BUCKET_IDX}")
warn!(
"Broadcasting a new message with empty bucket height {LAST_BUCKET_IDX}"
)
}
ktable
.extract(height)
Expand Down
2 changes: 1 addition & 1 deletion src/maintainer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use crate::encoding::message::{Header, Message};
use crate::kbucket::Tree;
use crate::peer::PeerInfo;
use crate::transport::MessageBeanOut;
use crate::{RwLock, K_ALPHA};
use crate::{K_ALPHA, RwLock};

pub(crate) struct TableMaintainer {
bootstrapping_nodes: Vec<String>,
Expand Down
2 changes: 1 addition & 1 deletion src/peer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ use blake2::{Blake2s256, Digest};

use crate::kbucket::{BinaryID, BinaryKey};
pub type PeerNode = Node<PeerInfo>;
use crate::K_ID_LEN_BYTES;
use crate::encoding::message::Header;
use crate::encoding::payload::{IpInfo, PeerEncodedInfo};
use crate::kbucket::Node;
use crate::K_ID_LEN_BYTES;
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord)]
pub struct PeerInfo {
address: SocketAddr,
Expand Down
2 changes: 1 addition & 1 deletion src/transport.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ use tokio::sync::mpsc::{self, Receiver, Sender};
use tracing::{debug, error, info, trace, warn};

use crate::config::Config;
use crate::encoding::message::Message;
use crate::encoding::Marshallable;
use crate::encoding::message::Message;
use crate::rwlock::RwLock;
use crate::transport::encoding::{
Configurable, Decoder, Encoder, TransportDecoder, TransportEncoder,
Expand Down
Loading