Skip to content
Merged
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
2 changes: 1 addition & 1 deletion bench/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "bench"
version = "0.1.0"
authors = ["piaoliu <441594700@qq.com>"]
edition = "2021"
edition = "2024"

[lib]
name = "bench"
Expand Down
2 changes: 1 addition & 1 deletion bench/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ impl Bench {
self.cycles,
total,
total / self.cycles,
self.executed_time_list[(self.cycles/2) as usize],
self.executed_time_list[(self.cycles / 2) as usize],
self.executed_time_list[self.cycles as usize - 1],
self.executed_time_list[0]
)
Expand Down
72 changes: 33 additions & 39 deletions bench/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use bench::Bench;
use bytes::Bytes;
use futures::channel;
use p2p::{
async_trait,
ProtocolId, async_trait,
builder::{MetaBuilder, ServiceBuilder},
context::{ProtocolContext, ProtocolContextMutRef},
multiaddr::Multiaddr,
Expand All @@ -11,19 +11,21 @@ use p2p::{
ProtocolHandle, ProtocolMeta, Service, ServiceControl, TargetProtocol, TargetSession,
},
traits::{ServiceHandle, ServiceProtocol},
ProtocolId,
};
use std::{sync::Once, thread};
use std::{
sync::{Once, OnceLock},
thread,
};
use tokio_util::codec::length_delimited::Builder;

static START_SECIO: Once = Once::new();
static START_NO_SECIO: Once = Once::new();

static mut SECIO_CONTROL: Option<ServiceControl> = None;
static mut NO_SECIO_CONTROL: Option<ServiceControl> = None;
static SECIO_CONTROL: OnceLock<ServiceControl> = OnceLock::new();
static NO_SECIO_CONTROL: OnceLock<ServiceControl> = OnceLock::new();

static mut SECIO_RECV: Option<crossbeam_channel::Receiver<Notify>> = None;
static mut NO_SECIO_RECV: Option<crossbeam_channel::Receiver<Notify>> = None;
static SECIO_RECV: OnceLock<crossbeam_channel::Receiver<Notify>> = OnceLock::new();
static NO_SECIO_RECV: OnceLock<crossbeam_channel::Receiver<Notify>> = OnceLock::new();

#[derive(Debug, PartialEq)]
enum Notify {
Expand Down Expand Up @@ -134,10 +136,8 @@ pub fn init() {
});

assert_eq!(client_receiver.recv(), Ok(Notify::Connected));
unsafe {
SECIO_CONTROL = Some(control.into());
SECIO_RECV = Some(client_receiver);
}
assert!(SECIO_CONTROL.set(control.into()).is_ok());
assert!(SECIO_RECV.set(client_receiver).is_ok());
});

// init no secio two peers
Expand Down Expand Up @@ -174,43 +174,37 @@ pub fn init() {
});

assert_eq!(client_receiver.recv(), Ok(Notify::Connected));
unsafe {
NO_SECIO_CONTROL = Some(control.into());
NO_SECIO_RECV = Some(client_receiver);
}
assert!(NO_SECIO_CONTROL.set(control.into()).is_ok());
assert!(NO_SECIO_RECV.set(client_receiver).is_ok());
});
}

fn secio_and_send_data(data: &[u8]) {
unsafe {
SECIO_CONTROL.as_mut().map(|control| {
control.filter_broadcast(
TargetSession::All,
ProtocolId::new(1),
Bytes::from(data.to_owned()),
)
});
if let Some(rev) = SECIO_RECV.as_ref() {
assert_eq!(
rev.recv(),
Ok(Notify::Message(bytes::Bytes::from(data.to_owned())))
)
}
SECIO_CONTROL.get().map(|control| {
control.filter_broadcast(
TargetSession::All,
ProtocolId::new(1),
Bytes::from(data.to_owned()),
)
});
if let Some(rev) = SECIO_RECV.get() {
assert_eq!(
rev.recv(),
Ok(Notify::Message(bytes::Bytes::from(data.to_owned())))
)
}
}

fn no_secio_and_send_data(data: &[u8]) {
unsafe {
NO_SECIO_CONTROL.as_mut().map(|control| {
control.filter_broadcast(TargetSession::All, 1.into(), Bytes::from(data.to_owned()))
});
NO_SECIO_CONTROL.get().map(|control| {
control.filter_broadcast(TargetSession::All, 1.into(), Bytes::from(data.to_owned()))
});

if let Some(rev) = NO_SECIO_RECV.as_ref() {
assert_eq!(
rev.recv(),
Ok(Notify::Message(bytes::Bytes::from(data.to_owned())))
)
}
if let Some(rev) = NO_SECIO_RECV.get() {
assert_eq!(
rev.recv(),
Ok(Notify::Message(bytes::Bytes::from(data.to_owned())))
)
}
}

Expand Down
2 changes: 1 addition & 1 deletion fuzz/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name = "tentacle-fuzz"
version = "0.0.1"
license = "MIT"
authors = ["Nervos Core Dev <dev@nervos.org>"]
edition = "2021"
edition = "2024"

[package.metadata]
cargo-fuzz = true
Expand Down
4 changes: 2 additions & 2 deletions multiaddr/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
name = "tentacle-multiaddr"
version = "0.3.5"
authors = ["driftluo <driftluo@foxmail.com>"]
edition = "2021"
rust-version = "1.61.0"
edition = "2024"
rust-version = "1.85.0"
repository = "https://github.com/nervosnetwork/tentacle"
license = "MIT"
description = "Mini Implementation of multiaddr"
Expand Down
4 changes: 2 additions & 2 deletions multiaddr/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
///! Mini Implementation of [multiaddr](https://github.com/jbenet/multiaddr) in Rust.
//! Mini Implementation of [multiaddr](https://github.com/jbenet/multiaddr) in Rust.
mod error;
mod onion_addr;
mod protocol;
Expand All @@ -8,8 +8,8 @@ pub use self::onion_addr::Onion3Addr;
pub use self::protocol::Protocol;
use bytes::{Bytes, BytesMut};
use serde::{
de::{self, Error as DeserializerError},
Deserialize, Deserializer, Serialize, Serializer,
de::{self, Error as DeserializerError},
};
use std::{
fmt,
Expand Down
2 changes: 1 addition & 1 deletion multiaddr/src/onion_addr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::{borrow::Cow, fmt};
#[derive(Clone)]
pub struct Onion3Addr<'a>(Cow<'a, [u8; 35]>, u16);

impl<'a> Onion3Addr<'a> {
impl Onion3Addr<'_> {
/// Return the hash of the public key as bytes
pub fn hash(&self) -> &[u8; 35] {
self.0.as_ref()
Expand Down
10 changes: 5 additions & 5 deletions multiaddr/src/protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use std::{
str::{self, FromStr},
};

use crate::{error::Error, Onion3Addr};
use crate::{Onion3Addr, error::Error};

const DNS4: u32 = 0x36;
const DNS6: u32 = 0x37;
Expand Down Expand Up @@ -258,7 +258,7 @@ impl<'a> Protocol<'a> {
}
}

impl<'a> fmt::Display for Protocol<'a> {
impl fmt::Display for Protocol<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
use self::Protocol::*;
match self {
Expand All @@ -280,7 +280,7 @@ impl<'a> fmt::Display for Protocol<'a> {
}
}

impl<'a> From<IpAddr> for Protocol<'a> {
impl From<IpAddr> for Protocol<'_> {
#[inline]
fn from(addr: IpAddr) -> Self {
match addr {
Expand All @@ -290,14 +290,14 @@ impl<'a> From<IpAddr> for Protocol<'a> {
}
}

impl<'a> From<Ipv4Addr> for Protocol<'a> {
impl From<Ipv4Addr> for Protocol<'_> {
#[inline]
fn from(addr: Ipv4Addr) -> Self {
Protocol::Ip4(addr)
}
}

impl<'a> From<Ipv6Addr> for Protocol<'a> {
impl From<Ipv6Addr> for Protocol<'_> {
#[inline]
fn from(addr: Ipv6Addr) -> Self {
Protocol::Ip6(addr)
Expand Down
2 changes: 1 addition & 1 deletion protocols/discovery/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ description = "p2p discovery protocol main reference bitcoin"
keywords = ["network", "peer-to-peer", "p2p", "discovery"]
repository = "https://github.com/nervosnetwork/tentacle"
categories = ["network-programming", "asynchronous"]
edition = "2018"
edition = "2024"

[package.metadata.docs.rs]
features = []
Expand Down
2 changes: 1 addition & 1 deletion protocols/identify/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ description = "p2p identify protocol"
keywords = ["network", "peer-to-peer", "p2p", "identify"]
repository = "https://github.com/nervosnetwork/tentacle"
categories = ["network-programming", "asynchronous"]
edition = "2018"
edition = "2024"

[package.metadata.docs.rs]
features = []
Expand Down
2 changes: 1 addition & 1 deletion protocols/ping/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ keywords = ["network", "peer-to-peer", "p2p", "ping"]
categories = ["network-programming", "asynchronous"]
repository = "https://github.com/nervosnetwork/tentacle"
description = "ping protocol implementation for tentacle"
edition = "2018"
edition = "2024"

[package.metadata.docs.rs]
features = []
Expand Down
2 changes: 1 addition & 1 deletion rust-toolchain
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.81.0
1.85.0
4 changes: 2 additions & 2 deletions secio/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ authors = ["piaoliu <driftluo@foxmail.com>", "Nervos Core Dev <dev@nervos.org>"]
repository = "https://github.com/nervosnetwork/tentacle"
keywords = ["network", "peer-to-peer"]
categories = ["network-programming", "asynchronous"]
edition = "2021"
rust-version = "1.70.0"
edition = "2024"
rust-version = "1.85.0"
build = "build.rs"

[package.metadata.docs.rs]
Expand Down
8 changes: 4 additions & 4 deletions secio/benches/bench.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use criterion::{criterion_group, criterion_main, Bencher, Criterion};
use tentacle_secio::crypto::{cipher::CipherType, new_stream, CryptoMode};
use criterion::{Bencher, Criterion, criterion_group, criterion_main};
use tentacle_secio::crypto::{CryptoMode, cipher::CipherType, new_stream};

fn decode_encode(data: &[u8], cipher: CipherType) {
let cipher_key = (0..cipher.key_size())
Expand All @@ -9,11 +9,11 @@ fn decode_encode(data: &[u8], cipher: CipherType) {
let mut encode_cipher = new_stream(cipher, &cipher_key, CryptoMode::Encrypt);
let mut decode_cipher = new_stream(cipher, &cipher_key, CryptoMode::Decrypt);

let encode_data = encode_cipher.encrypt(&data[..]).unwrap();
let encode_data = encode_cipher.encrypt(data).unwrap();

let decode_data = decode_cipher.decrypt(&encode_data).unwrap();

assert_eq!(&decode_data[..], &data[..]);
assert_eq!(&decode_data[..], data);
}

fn bench_test(bench: &mut Bencher, cipher: CipherType, data: &[u8]) {
Expand Down
3 changes: 1 addition & 2 deletions secio/examples/secio_simple.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use bytes::BytesMut;
use env_logger;
use log::info;
use tentacle_secio::{handshake::Config, SecioKeyPair};
use tentacle_secio::{SecioKeyPair, handshake::Config};
use tokio::{
io::{AsyncReadExt, AsyncWriteExt},
net::{TcpListener, TcpStream},
Expand Down
2 changes: 1 addition & 1 deletion secio/src/codec/hmac_compat/openssl_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ impl Hmac {

pub struct Context<'a>(Signer<'a>);

impl<'a> Context<'a> {
impl Context<'_> {
pub fn update(&mut self, data: &[u8]) {
self.0.update(data).expect("openssl hmac update fail")
}
Expand Down
4 changes: 2 additions & 2 deletions secio/src/codec/hmac_compat/wasm_compat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ impl Hmac {
/// Return a multi-step hmac context
pub fn context(&self) -> Context {
match self {
Hmac::Sha256(ref hmac) => Context::Sha256(hmac.clone()),
Hmac::Sha512(ref hmac) => Context::Sha512(hmac.clone()),
Hmac::Sha256(hmac) => Context::Sha256(hmac.clone()),
Hmac::Sha512(hmac) => Context::Sha512(hmac.clone()),
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion secio/src/codec/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/// Hmac struct on this module comes from `rust-libp2p`, but use high version of hamc

///
/// Encryption and decryption stream
pub mod secure_stream;
// hmac compatible
Expand Down
18 changes: 9 additions & 9 deletions secio/src/codec/secure_stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use bytes::{Buf, Bytes, BytesMut};
use futures::{SinkExt, StreamExt};
use log::{debug, trace};
use tokio::io::{AsyncRead, AsyncReadExt, AsyncWrite, ReadBuf};
use tokio_util::codec::{length_delimited::LengthDelimitedCodec, Framed};
use tokio_util::codec::{Framed, length_delimited::LengthDelimitedCodec};

use std::{
cmp::min,
Expand All @@ -21,10 +21,10 @@ enum RecvBuf {
impl RecvBuf {
fn drain_to(&mut self, buf: &mut ReadBuf, size: usize) {
match self {
RecvBuf::Vec(ref mut b) => {
RecvBuf::Vec(b) => {
buf.put_slice(b.drain(..size).as_slice());
}
RecvBuf::Byte(ref mut b) => {
RecvBuf::Byte(b) => {
buf.put_slice(&b[..size]);
b.advance(size);
}
Expand All @@ -33,8 +33,8 @@ impl RecvBuf {

fn len(&self) -> usize {
match self {
RecvBuf::Vec(ref b) => b.len(),
RecvBuf::Byte(ref b) => b.len(),
RecvBuf::Vec(b) => b.len(),
RecvBuf::Byte(b) => b.len(),
}
}

Expand All @@ -46,8 +46,8 @@ impl RecvBuf {
impl AsRef<[u8]> for RecvBuf {
fn as_ref(&self) -> &[u8] {
match self {
RecvBuf::Vec(ref b) => b.as_ref(),
RecvBuf::Byte(ref b) => b.as_ref(),
RecvBuf::Vec(b) => b.as_ref(),
RecvBuf::Byte(b) => b.as_ref(),
}
}
}
Expand Down Expand Up @@ -231,14 +231,14 @@ where
#[cfg(test)]
mod tests {
use super::SecureStream;
use crate::crypto::{cipher::CipherType, new_stream, CryptoMode};
use crate::crypto::{CryptoMode, cipher::CipherType, new_stream};
use bytes::BytesMut;
use futures::channel;
use tokio::{
io::{AsyncReadExt, AsyncWriteExt},
net::{TcpListener, TcpStream},
};
use tokio_util::codec::{length_delimited::LengthDelimitedCodec, Framed};
use tokio_util::codec::{Framed, length_delimited::LengthDelimitedCodec};

fn rt() -> &'static tokio::runtime::Runtime {
static RT: std::sync::OnceLock<tokio::runtime::Runtime> = std::sync::OnceLock::new();
Expand Down
4 changes: 2 additions & 2 deletions secio/src/crypto/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@ fn nonce_advance(nonce: &mut [u8]) {
#[cfg(all(test, unix))]
mod test {
use super::{
cipher::CipherType, openssl_impl::OpenSsLCrypt, ring_impl::RingAeadCipher,
wasm_compat::WasmCrypt, CryptoMode,
CryptoMode, cipher::CipherType, openssl_impl::OpenSsLCrypt, ring_impl::RingAeadCipher,
wasm_compat::WasmCrypt,
};
use proptest::prelude::*;

Expand Down
Loading