From e3a918bca57922d00857523ee06d6581016948f3 Mon Sep 17 00:00:00 2001 From: hinto-janai Date: Wed, 18 Sep 2024 18:19:32 -0400 Subject: [PATCH] wire: enable workspace lints (#291) * wire: enable workspace lints * revert match arm formatting --- net/wire/Cargo.toml | 4 +- net/wire/src/network_address.rs | 16 +- net/wire/src/network_address/epee_builder.rs | 4 +- net/wire/src/p2p.rs | 219 +++++++++---------- net/wire/src/p2p/admin.rs | 8 +- net/wire/src/p2p/common.rs | 16 +- net/wire/src/p2p/protocol.rs | 6 +- 7 files changed, 138 insertions(+), 135 deletions(-) diff --git a/net/wire/Cargo.toml b/net/wire/Cargo.toml index cbeb5511..0b77cf1b 100644 --- a/net/wire/Cargo.toml +++ b/net/wire/Cargo.toml @@ -15,7 +15,7 @@ cuprate-levin = { path = "../levin" } cuprate-epee-encoding = { path = "../epee-encoding" } cuprate-fixed-bytes = { path = "../fixed-bytes" } cuprate-types = { path = "../../types", default-features = false, features = ["epee"] } -cuprate-helper = { path = "../../helper", default-features = false, features = ["cast"] } +cuprate-helper = { path = "../../helper", default-features = false, features = ["map"] } bitflags = { workspace = true, features = ["std"] } bytes = { workspace = true, features = ["std"] } @@ -24,3 +24,5 @@ thiserror = { workspace = true } [dev-dependencies] hex = { workspace = true, features = ["std"]} +[lints] +workspace = true diff --git a/net/wire/src/network_address.rs b/net/wire/src/network_address.rs index 632739af..ad599b70 100644 --- a/net/wire/src/network_address.rs +++ b/net/wire/src/network_address.rs @@ -51,38 +51,38 @@ impl EpeeObject for NetworkAddress { } impl NetworkAddress { - pub fn get_zone(&self) -> NetZone { + pub const fn get_zone(&self) -> NetZone { match self { - NetworkAddress::Clear(_) => NetZone::Public, + Self::Clear(_) => NetZone::Public, } } - pub fn is_loopback(&self) -> bool { + pub const fn is_loopback(&self) -> bool { // TODO false } - pub fn is_local(&self) -> bool { + pub const fn is_local(&self) -> bool { // TODO false } - pub fn port(&self) -> u16 { + pub const fn port(&self) -> u16 { match self { - NetworkAddress::Clear(ip) => ip.port(), + Self::Clear(ip) => ip.port(), } } } impl From for NetworkAddress { fn from(value: net::SocketAddrV4) -> Self { - NetworkAddress::Clear(value.into()) + Self::Clear(value.into()) } } impl From for NetworkAddress { fn from(value: net::SocketAddrV6) -> Self { - NetworkAddress::Clear(value.into()) + Self::Clear(value.into()) } } diff --git a/net/wire/src/network_address/epee_builder.rs b/net/wire/src/network_address/epee_builder.rs index 36db8241..c1d17423 100644 --- a/net/wire/src/network_address/epee_builder.rs +++ b/net/wire/src/network_address/epee_builder.rs @@ -74,7 +74,7 @@ impl From for TaggedNetworkAddress { fn from(value: NetworkAddress) -> Self { match value { NetworkAddress::Clear(addr) => match addr { - SocketAddr::V4(addr) => TaggedNetworkAddress { + SocketAddr::V4(addr) => Self { ty: Some(1), addr: Some(AllFieldsNetworkAddress { m_ip: Some(u32::from_be_bytes(addr.ip().octets())), @@ -82,7 +82,7 @@ impl From for TaggedNetworkAddress { addr: None, }), }, - SocketAddr::V6(addr) => TaggedNetworkAddress { + SocketAddr::V6(addr) => Self { ty: Some(2), addr: Some(AllFieldsNetworkAddress { addr: Some(addr.ip().octets()), diff --git a/net/wire/src/p2p.rs b/net/wire/src/p2p.rs index 3829d172..a7cd7848 100644 --- a/net/wire/src/p2p.rs +++ b/net/wire/src/p2p.rs @@ -55,27 +55,27 @@ pub enum LevinCommand { impl std::fmt::Display for LevinCommand { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { - if let LevinCommand::Unknown(id) = self { - return f.write_str(&format!("unknown id: {}", id)); + if let Self::Unknown(id) = self { + return f.write_str(&format!("unknown id: {id}")); } f.write_str(match self { - LevinCommand::Handshake => "handshake", - LevinCommand::TimedSync => "timed sync", - LevinCommand::Ping => "ping", - LevinCommand::SupportFlags => "support flags", + Self::Handshake => "handshake", + Self::TimedSync => "timed sync", + Self::Ping => "ping", + Self::SupportFlags => "support flags", - LevinCommand::NewBlock => "new block", - LevinCommand::NewTransactions => "new transactions", - LevinCommand::GetObjectsRequest => "get objects request", - LevinCommand::GetObjectsResponse => "get objects response", - LevinCommand::ChainRequest => "chain request", - LevinCommand::ChainResponse => "chain response", - LevinCommand::NewFluffyBlock => "new fluffy block", - LevinCommand::FluffyMissingTxsRequest => "fluffy missing transaction request", - LevinCommand::GetTxPoolCompliment => "get transaction pool compliment", + Self::NewBlock => "new block", + Self::NewTransactions => "new transactions", + Self::GetObjectsRequest => "get objects request", + Self::GetObjectsResponse => "get objects response", + Self::ChainRequest => "chain request", + Self::ChainResponse => "chain response", + Self::NewFluffyBlock => "new fluffy block", + Self::FluffyMissingTxsRequest => "fluffy missing transaction request", + Self::GetTxPoolCompliment => "get transaction pool compliment", - LevinCommand::Unknown(_) => unreachable!(), + Self::Unknown(_) => unreachable!(), }) } } @@ -83,50 +83,51 @@ impl std::fmt::Display for LevinCommand { impl LevinCommandTrait for LevinCommand { fn bucket_size_limit(&self) -> u64 { // https://github.com/monero-project/monero/blob/00fd416a99686f0956361d1cd0337fe56e58d4a7/src/cryptonote_basic/connection_context.cpp#L37 + #[expect(clippy::match_same_arms, reason = "formatting is more clear")] match self { - LevinCommand::Handshake => 65536, - LevinCommand::TimedSync => 65536, - LevinCommand::Ping => 4096, - LevinCommand::SupportFlags => 4096, + Self::Handshake => 65536, + Self::TimedSync => 65536, + Self::Ping => 4096, + Self::SupportFlags => 4096, - LevinCommand::NewBlock => 1024 * 1024 * 128, // 128 MB (max packet is a bit less than 100 MB though) - LevinCommand::NewTransactions => 1024 * 1024 * 128, // 128 MB (max packet is a bit less than 100 MB though) - LevinCommand::GetObjectsRequest => 1024 * 1024 * 2, // 2 MB - LevinCommand::GetObjectsResponse => 1024 * 1024 * 128, // 128 MB (max packet is a bit less than 100 MB though) - LevinCommand::ChainRequest => 512 * 1024, // 512 kB - LevinCommand::ChainResponse => 1024 * 1024 * 4, // 4 MB - LevinCommand::NewFluffyBlock => 1024 * 1024 * 4, // 4 MB - LevinCommand::FluffyMissingTxsRequest => 1024 * 1024, // 1 MB - LevinCommand::GetTxPoolCompliment => 1024 * 1024 * 4, // 4 MB + Self::NewBlock => 1024 * 1024 * 128, // 128 MB (max packet is a bit less than 100 MB though) + Self::NewTransactions => 1024 * 1024 * 128, // 128 MB (max packet is a bit less than 100 MB though) + Self::GetObjectsRequest => 1024 * 1024 * 2, // 2 MB + Self::GetObjectsResponse => 1024 * 1024 * 128, // 128 MB (max packet is a bit less than 100 MB though) + Self::ChainRequest => 512 * 1024, // 512 kB + Self::ChainResponse => 1024 * 1024 * 4, // 4 MB + Self::NewFluffyBlock => 1024 * 1024 * 4, // 4 MB + Self::FluffyMissingTxsRequest => 1024 * 1024, // 1 MB + Self::GetTxPoolCompliment => 1024 * 1024 * 4, // 4 MB - LevinCommand::Unknown(_) => u64::MAX, + Self::Unknown(_) => u64::MAX, } } fn is_handshake(&self) -> bool { - matches!(self, LevinCommand::Handshake) + matches!(self, Self::Handshake) } } impl From for LevinCommand { fn from(value: u32) -> Self { match value { - 1001 => LevinCommand::Handshake, - 1002 => LevinCommand::TimedSync, - 1003 => LevinCommand::Ping, - 1007 => LevinCommand::SupportFlags, + 1001 => Self::Handshake, + 1002 => Self::TimedSync, + 1003 => Self::Ping, + 1007 => Self::SupportFlags, - 2001 => LevinCommand::NewBlock, - 2002 => LevinCommand::NewTransactions, - 2003 => LevinCommand::GetObjectsRequest, - 2004 => LevinCommand::GetObjectsResponse, - 2006 => LevinCommand::ChainRequest, - 2007 => LevinCommand::ChainResponse, - 2008 => LevinCommand::NewFluffyBlock, - 2009 => LevinCommand::FluffyMissingTxsRequest, - 2010 => LevinCommand::GetTxPoolCompliment, + 2001 => Self::NewBlock, + 2002 => Self::NewTransactions, + 2003 => Self::GetObjectsRequest, + 2004 => Self::GetObjectsResponse, + 2006 => Self::ChainRequest, + 2007 => Self::ChainResponse, + 2008 => Self::NewFluffyBlock, + 2009 => Self::FluffyMissingTxsRequest, + 2010 => Self::GetTxPoolCompliment, - x => LevinCommand::Unknown(x), + x => Self::Unknown(x), } } } @@ -191,19 +192,19 @@ pub enum ProtocolMessage { } impl ProtocolMessage { - pub fn command(&self) -> LevinCommand { + pub const fn command(&self) -> LevinCommand { use LevinCommand as C; match self { - ProtocolMessage::NewBlock(_) => C::NewBlock, - ProtocolMessage::NewFluffyBlock(_) => C::NewFluffyBlock, - ProtocolMessage::GetObjectsRequest(_) => C::GetObjectsRequest, - ProtocolMessage::GetObjectsResponse(_) => C::GetObjectsResponse, - ProtocolMessage::ChainRequest(_) => C::ChainRequest, - ProtocolMessage::ChainEntryResponse(_) => C::ChainResponse, - ProtocolMessage::NewTransactions(_) => C::NewTransactions, - ProtocolMessage::FluffyMissingTransactionsRequest(_) => C::FluffyMissingTxsRequest, - ProtocolMessage::GetTxPoolCompliment(_) => C::GetTxPoolCompliment, + Self::NewBlock(_) => C::NewBlock, + Self::NewFluffyBlock(_) => C::NewFluffyBlock, + Self::GetObjectsRequest(_) => C::GetObjectsRequest, + Self::GetObjectsResponse(_) => C::GetObjectsResponse, + Self::ChainRequest(_) => C::ChainRequest, + Self::ChainEntryResponse(_) => C::ChainResponse, + Self::NewTransactions(_) => C::NewTransactions, + Self::FluffyMissingTransactionsRequest(_) => C::FluffyMissingTxsRequest, + Self::GetTxPoolCompliment(_) => C::GetTxPoolCompliment, } } @@ -230,26 +231,26 @@ impl ProtocolMessage { use LevinCommand as C; match self { - ProtocolMessage::NewBlock(val) => build_message(C::NewBlock, val, builder)?, - ProtocolMessage::NewTransactions(val) => { - build_message(C::NewTransactions, val, builder)? + Self::NewBlock(val) => build_message(C::NewBlock, val, builder)?, + Self::NewTransactions(val) => { + build_message(C::NewTransactions, val, builder)?; } - ProtocolMessage::GetObjectsRequest(val) => { - build_message(C::GetObjectsRequest, val, builder)? + Self::GetObjectsRequest(val) => { + build_message(C::GetObjectsRequest, val, builder)?; } - ProtocolMessage::GetObjectsResponse(val) => { - build_message(C::GetObjectsResponse, val, builder)? + Self::GetObjectsResponse(val) => { + build_message(C::GetObjectsResponse, val, builder)?; } - ProtocolMessage::ChainRequest(val) => build_message(C::ChainRequest, val, builder)?, - ProtocolMessage::ChainEntryResponse(val) => { - build_message(C::ChainResponse, val, builder)? + Self::ChainRequest(val) => build_message(C::ChainRequest, val, builder)?, + Self::ChainEntryResponse(val) => { + build_message(C::ChainResponse, val, builder)?; } - ProtocolMessage::NewFluffyBlock(val) => build_message(C::NewFluffyBlock, val, builder)?, - ProtocolMessage::FluffyMissingTransactionsRequest(val) => { - build_message(C::FluffyMissingTxsRequest, val, builder)? + Self::NewFluffyBlock(val) => build_message(C::NewFluffyBlock, val, builder)?, + Self::FluffyMissingTransactionsRequest(val) => { + build_message(C::FluffyMissingTxsRequest, val, builder)?; } - ProtocolMessage::GetTxPoolCompliment(val) => { - build_message(C::GetTxPoolCompliment, val, builder)? + Self::GetTxPoolCompliment(val) => { + build_message(C::GetTxPoolCompliment, val, builder)?; } } Ok(()) @@ -265,14 +266,14 @@ pub enum AdminRequestMessage { } impl AdminRequestMessage { - pub fn command(&self) -> LevinCommand { + pub const fn command(&self) -> LevinCommand { use LevinCommand as C; match self { - AdminRequestMessage::Handshake(_) => C::Handshake, - AdminRequestMessage::Ping => C::Ping, - AdminRequestMessage::SupportFlags => C::SupportFlags, - AdminRequestMessage::TimedSync(_) => C::TimedSync, + Self::Handshake(_) => C::Handshake, + Self::Ping => C::Ping, + Self::SupportFlags => C::SupportFlags, + Self::TimedSync(_) => C::TimedSync, } } @@ -286,13 +287,13 @@ impl AdminRequestMessage { cuprate_epee_encoding::from_bytes::(buf) .map_err(|e| BucketError::BodyDecodingError(e.into()))?; - AdminRequestMessage::Ping + Self::Ping } C::SupportFlags => { cuprate_epee_encoding::from_bytes::(buf) .map_err(|e| BucketError::BodyDecodingError(e.into()))?; - AdminRequestMessage::SupportFlags + Self::SupportFlags } _ => return Err(BucketError::UnknownCommand), }) @@ -302,11 +303,11 @@ impl AdminRequestMessage { use LevinCommand as C; match self { - AdminRequestMessage::Handshake(val) => build_message(C::Handshake, val, builder)?, - AdminRequestMessage::TimedSync(val) => build_message(C::TimedSync, val, builder)?, - AdminRequestMessage::Ping => build_message(C::Ping, EmptyMessage, builder)?, - AdminRequestMessage::SupportFlags => { - build_message(C::SupportFlags, EmptyMessage, builder)? + Self::Handshake(val) => build_message(C::Handshake, val, builder)?, + Self::TimedSync(val) => build_message(C::TimedSync, val, builder)?, + Self::Ping => build_message(C::Ping, EmptyMessage, builder)?, + Self::SupportFlags => { + build_message(C::SupportFlags, EmptyMessage, builder)?; } } Ok(()) @@ -322,14 +323,14 @@ pub enum AdminResponseMessage { } impl AdminResponseMessage { - pub fn command(&self) -> LevinCommand { + pub const fn command(&self) -> LevinCommand { use LevinCommand as C; match self { - AdminResponseMessage::Handshake(_) => C::Handshake, - AdminResponseMessage::Ping(_) => C::Ping, - AdminResponseMessage::SupportFlags(_) => C::SupportFlags, - AdminResponseMessage::TimedSync(_) => C::TimedSync, + Self::Handshake(_) => C::Handshake, + Self::Ping(_) => C::Ping, + Self::SupportFlags(_) => C::SupportFlags, + Self::TimedSync(_) => C::TimedSync, } } @@ -349,11 +350,11 @@ impl AdminResponseMessage { use LevinCommand as C; match self { - AdminResponseMessage::Handshake(val) => build_message(C::Handshake, val, builder)?, - AdminResponseMessage::TimedSync(val) => build_message(C::TimedSync, val, builder)?, - AdminResponseMessage::Ping(val) => build_message(C::Ping, val, builder)?, - AdminResponseMessage::SupportFlags(val) => { - build_message(C::SupportFlags, val, builder)? + Self::Handshake(val) => build_message(C::Handshake, val, builder)?, + Self::TimedSync(val) => build_message(C::TimedSync, val, builder)?, + Self::Ping(val) => build_message(C::Ping, val, builder)?, + Self::SupportFlags(val) => { + build_message(C::SupportFlags, val, builder)?; } } Ok(()) @@ -368,23 +369,23 @@ pub enum Message { } impl Message { - pub fn is_request(&self) -> bool { - matches!(self, Message::Request(_)) + pub const fn is_request(&self) -> bool { + matches!(self, Self::Request(_)) } - pub fn is_response(&self) -> bool { - matches!(self, Message::Response(_)) + pub const fn is_response(&self) -> bool { + matches!(self, Self::Response(_)) } - pub fn is_protocol(&self) -> bool { - matches!(self, Message::Protocol(_)) + pub const fn is_protocol(&self) -> bool { + matches!(self, Self::Protocol(_)) } - pub fn command(&self) -> LevinCommand { + pub const fn command(&self) -> LevinCommand { match self { - Message::Request(mes) => mes.command(), - Message::Response(mes) => mes.command(), - Message::Protocol(mes) => mes.command(), + Self::Request(mes) => mes.command(), + Self::Response(mes) => mes.command(), + Self::Protocol(mes) => mes.command(), } } } @@ -398,27 +399,25 @@ impl LevinBody for Message { command: LevinCommand, ) -> Result { Ok(match typ { - MessageType::Request => Message::Request(AdminRequestMessage::decode(body, command)?), - MessageType::Response => { - Message::Response(AdminResponseMessage::decode(body, command)?) - } - MessageType::Notification => Message::Protocol(ProtocolMessage::decode(body, command)?), + MessageType::Request => Self::Request(AdminRequestMessage::decode(body, command)?), + MessageType::Response => Self::Response(AdminResponseMessage::decode(body, command)?), + MessageType::Notification => Self::Protocol(ProtocolMessage::decode(body, command)?), }) } fn encode(self, builder: &mut BucketBuilder) -> Result<(), BucketError> { match self { - Message::Protocol(pro) => { + Self::Protocol(pro) => { builder.set_message_type(MessageType::Notification); builder.set_return_code(0); pro.build(builder) } - Message::Request(req) => { + Self::Request(req) => { builder.set_message_type(MessageType::Request); builder.set_return_code(0); req.build(builder) } - Message::Response(res) => { + Self::Response(res) => { builder.set_message_type(MessageType::Response); builder.set_return_code(1); res.build(builder) diff --git a/net/wire/src/p2p/admin.rs b/net/wire/src/p2p/admin.rs index 173c2938..67a8e212 100644 --- a/net/wire/src/p2p/admin.rs +++ b/net/wire/src/p2p/admin.rs @@ -45,7 +45,7 @@ pub struct HandshakeResponse { pub node_data: BasicNodeData, /// Core Sync Data pub payload_data: CoreSyncData, - /// PeerList + /// `PeerList` pub local_peerlist_new: Vec, } @@ -56,7 +56,7 @@ epee_object!( local_peerlist_new: Vec, ); -/// A TimedSync Request +/// A `TimedSync` Request #[derive(Debug, Clone, PartialEq, Eq)] pub struct TimedSyncRequest { /// Core Sync Data @@ -68,12 +68,12 @@ epee_object!( payload_data: CoreSyncData, ); -/// A TimedSync Response +/// A `TimedSync` Response #[derive(Debug, Clone, PartialEq, Eq)] pub struct TimedSyncResponse { /// Core Sync Data pub payload_data: CoreSyncData, - /// PeerList + /// `PeerList` pub local_peerlist_new: Vec, } diff --git a/net/wire/src/p2p/common.rs b/net/wire/src/p2p/common.rs index d585d071..d95a6203 100644 --- a/net/wire/src/p2p/common.rs +++ b/net/wire/src/p2p/common.rs @@ -18,6 +18,7 @@ use bitflags::bitflags; use cuprate_epee_encoding::epee_object; +use cuprate_helper::map::split_u128_into_low_high_bits; pub use cuprate_types::{BlockCompleteEntry, PrunedTxBlobEntry, TransactionBlobs}; use crate::NetworkAddress; @@ -34,7 +35,7 @@ bitflags! { impl From for PeerSupportFlags { fn from(value: u32) -> Self { - PeerSupportFlags(value) + Self(value) } } @@ -113,16 +114,17 @@ epee_object! { } impl CoreSyncData { - pub fn new( + pub const fn new( cumulative_difficulty_128: u128, current_height: u64, pruning_seed: u32, top_id: [u8; 32], top_version: u8, - ) -> CoreSyncData { - let cumulative_difficulty = cumulative_difficulty_128 as u64; - let cumulative_difficulty_top64 = (cumulative_difficulty_128 >> 64) as u64; - CoreSyncData { + ) -> Self { + let (cumulative_difficulty, cumulative_difficulty_top64) = + split_u128_into_low_high_bits(cumulative_difficulty_128); + + Self { cumulative_difficulty, cumulative_difficulty_top64, current_height, @@ -139,7 +141,7 @@ impl CoreSyncData { } } -/// PeerListEntryBase, information kept on a peer which will be entered +/// `PeerListEntryBase`, information kept on a peer which will be entered /// in a peer list/store. #[derive(Clone, Copy, Debug, Eq, PartialEq)] pub struct PeerListEntryBase { diff --git a/net/wire/src/p2p/protocol.rs b/net/wire/src/p2p/protocol.rs index 73694d57..1d1d45ab 100644 --- a/net/wire/src/p2p/protocol.rs +++ b/net/wire/src/p2p/protocol.rs @@ -127,7 +127,7 @@ pub struct ChainResponse { impl ChainResponse { #[inline] - pub fn cumulative_difficulty(&self) -> u128 { + pub const fn cumulative_difficulty(&self) -> u128 { let cumulative_difficulty = self.cumulative_difficulty_top64 as u128; cumulative_difficulty << 64 | self.cumulative_difficulty_low64 as u128 } @@ -159,7 +159,7 @@ epee_object!( current_blockchain_height: u64, ); -/// A request for Txs we are missing from our TxPool +/// A request for Txs we are missing from our `TxPool` #[derive(Debug, Clone, PartialEq, Eq)] pub struct FluffyMissingTransactionsRequest { /// The Block we are missing the Txs in @@ -177,7 +177,7 @@ epee_object!( missing_tx_indices: Vec as ContainerAsBlob, ); -/// TxPoolCompliment +/// `TxPoolCompliment` #[derive(Debug, Clone, PartialEq, Eq)] pub struct GetTxPoolCompliment { /// Tx Hashes