Skip to content

Commit b6b4181

Browse files
authored
refactor: TcpConnection -> Peer and ConnectionPool -> PeerPool (#190)
1 parent da21b9d commit b6b4181

File tree

11 files changed

+226
-226
lines changed

11 files changed

+226
-226
lines changed

dash-spv/ARCHITECTURE.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -680,7 +680,7 @@ The network module handles all P2P communication with the Dash network.
680680
- Graceful shutdown
681681

682682
**Complex Types Used**:
683-
- `HashMap<PeerId, Arc<TcpConnection>>` - **JUSTIFIED**: Efficient peer lookup
683+
- `HashMap<PeerId, Arc<Peer>>` - **JUSTIFIED**: Efficient peer lookup
684684
- Multiple tokio::sync primitives - **JUSTIFIED**: Complex concurrent operations
685685

686686
**Critical Issues**:

dash-spv/CLAUDE.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ TCP-based networking with proper Dash protocol implementation:
116116
- **DNS-first peer discovery**: Automatically uses DNS seeds (`dnsseed.dash.org`, `testnet-seed.dashdot.io`) when no explicit peers are configured
117117
- **Immediate startup**: No delay for initial peer discovery (10-second delay only for subsequent searches)
118118
- **Exclusive mode**: When explicit peers are provided, uses only those peers (no DNS discovery)
119-
- Connection management via `TcpConnection`
119+
- Connection management via `Peer`
120120
- Handshake handling via `HandshakeManager`
121121
- Message routing via `MessageHandler`
122122
- Peer support via `PeerNetworkManager`

dash-spv/src/network/handshake.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use dashcore::Network;
1212

1313
use crate::client::config::MempoolStrategy;
1414
use crate::error::{NetworkError, NetworkResult};
15-
use crate::network::connection::TcpConnection;
15+
use crate::network::peer::Peer;
1616

1717
/// Handshake state.
1818
#[derive(Debug, Clone, PartialEq, Eq)]
@@ -65,7 +65,7 @@ impl HandshakeManager {
6565
}
6666

6767
/// Perform the handshake with a peer.
68-
pub async fn perform_handshake(&mut self, connection: &mut TcpConnection) -> NetworkResult<()> {
68+
pub async fn perform_handshake(&mut self, connection: &mut Peer) -> NetworkResult<()> {
6969
use tokio::time::{timeout, Duration};
7070

7171
// Send version message
@@ -145,7 +145,7 @@ impl HandshakeManager {
145145
/// Handle a handshake message.
146146
async fn handle_handshake_message(
147147
&mut self,
148-
connection: &mut TcpConnection,
148+
connection: &mut Peer,
149149
message: NetworkMessage,
150150
) -> NetworkResult<Option<HandshakeState>> {
151151
match message {
@@ -238,7 +238,7 @@ impl HandshakeManager {
238238
}
239239

240240
/// Send version message.
241-
async fn send_version(&mut self, connection: &mut TcpConnection) -> NetworkResult<()> {
241+
async fn send_version(&mut self, connection: &mut Peer) -> NetworkResult<()> {
242242
let version_message = self.build_version_message(connection.peer_info().address)?;
243243
connection.send_message(NetworkMessage::Version(version_message)).await?;
244244
tracing::debug!("Sent version message");
@@ -309,7 +309,7 @@ impl HandshakeManager {
309309
}
310310

311311
/// Negotiate headers2 support with the peer after handshake completion.
312-
async fn negotiate_headers2(&self, connection: &mut TcpConnection) -> NetworkResult<()> {
312+
async fn negotiate_headers2(&self, connection: &mut Peer) -> NetworkResult<()> {
313313
// Headers2 is currently disabled due to protocol compatibility issues
314314
// Always send SendHeaders regardless of peer support
315315
tracing::info!("Headers2 is disabled - sending SendHeaders only");

dash-spv/src/network/manager.rs

Lines changed: 100 additions & 100 deletions
Large diffs are not rendered by default.

dash-spv/src/network/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
//! Network layer for the Dash SPV client.
22
33
pub mod addrv2;
4-
pub mod connection;
54
pub mod constants;
65
pub mod discovery;
76
pub mod handshake;
87
pub mod manager;
8+
pub mod peer;
99
pub mod persist;
1010
pub mod pool;
1111
pub mod reputation;
@@ -23,9 +23,9 @@ use crate::error::NetworkResult;
2323
use dashcore::network::message::NetworkMessage;
2424
use dashcore::BlockHash;
2525

26-
pub use connection::TcpConnection;
2726
pub use handshake::{HandshakeManager, HandshakeState};
2827
pub use manager::PeerNetworkManager;
28+
pub use peer::Peer;
2929

3030
/// Network manager trait for abstracting network operations.
3131
#[async_trait]
Lines changed: 41 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//! TCP connection management.
1+
//! Dash peer connection management.
22
33
use std::collections::HashMap;
44
use std::net::SocketAddr;
@@ -23,8 +23,8 @@ struct ConnectionState {
2323
framing_buffer: Vec<u8>,
2424
}
2525

26-
/// TCP connection to a Dash peer
27-
pub struct TcpConnection {
26+
/// Dash P2P peer
27+
pub struct Peer {
2828
address: SocketAddr,
2929
// Use a single mutex to protect both the write stream and read buffer
3030
// This ensures no concurrent access to the underlying socket
@@ -38,23 +38,23 @@ pub struct TcpConnection {
3838
last_pong_received: Option<SystemTime>,
3939
pending_pings: HashMap<u64, SystemTime>, // nonce -> sent_time
4040
// Peer information from Version message
41-
peer_version: Option<u32>,
42-
peer_services: Option<u64>,
43-
peer_user_agent: Option<String>,
44-
peer_best_height: Option<u32>,
45-
peer_relay: Option<bool>,
46-
peer_prefers_headers2: bool,
47-
peer_sent_sendheaders2: bool,
41+
version: Option<u32>,
42+
services: Option<u64>,
43+
user_agent: Option<String>,
44+
best_height: Option<u32>,
45+
relay: Option<bool>,
46+
prefers_headers2: bool,
47+
sent_sendheaders2: bool,
4848
// Basic telemetry for resync events
4949
consecutive_resyncs: u32,
5050
}
5151

52-
impl TcpConnection {
52+
impl Peer {
5353
/// Get the remote peer socket address.
5454
pub fn address(&self) -> SocketAddr {
5555
self.address
5656
}
57-
/// Create a new TCP connection to the given address.
57+
/// Create a new peer.
5858
pub fn new(address: SocketAddr, timeout: Duration, network: Network) -> Self {
5959
Self {
6060
address,
@@ -66,13 +66,13 @@ impl TcpConnection {
6666
last_ping_sent: None,
6767
last_pong_received: None,
6868
pending_pings: HashMap::new(),
69-
peer_version: None,
70-
peer_services: None,
71-
peer_user_agent: None,
72-
peer_best_height: None,
73-
peer_relay: None,
74-
peer_prefers_headers2: false,
75-
peer_sent_sendheaders2: false,
69+
version: None,
70+
services: None,
71+
user_agent: None,
72+
best_height: None,
73+
relay: None,
74+
prefers_headers2: false,
75+
sent_sendheaders2: false,
7676
consecutive_resyncs: 0,
7777
}
7878
}
@@ -113,13 +113,13 @@ impl TcpConnection {
113113
last_ping_sent: None,
114114
last_pong_received: None,
115115
pending_pings: HashMap::new(),
116-
peer_version: None,
117-
peer_services: None,
118-
peer_user_agent: None,
119-
peer_best_height: None,
120-
peer_relay: None,
121-
peer_prefers_headers2: false,
122-
peer_sent_sendheaders2: false,
116+
version: None,
117+
services: None,
118+
user_agent: None,
119+
best_height: None,
120+
relay: None,
121+
prefers_headers2: false,
122+
sent_sendheaders2: false,
123123
consecutive_resyncs: 0,
124124
})
125125
}
@@ -249,11 +249,11 @@ impl TcpConnection {
249249
}
250250

251251
// All validations passed, update peer info
252-
self.peer_version = Some(version_msg.version);
253-
self.peer_services = Some(version_msg.services.as_u64());
254-
self.peer_user_agent = Some(version_msg.user_agent.clone());
255-
self.peer_best_height = Some(version_msg.start_height as u32);
256-
self.peer_relay = Some(version_msg.relay);
252+
self.version = Some(version_msg.version);
253+
self.services = Some(version_msg.services.as_u64());
254+
self.user_agent = Some(version_msg.user_agent.clone());
255+
self.best_height = Some(version_msg.start_height as u32);
256+
self.relay = Some(version_msg.relay);
257257

258258
tracing::info!(
259259
"Updated peer info for {}: height={}, version={}, services={:?}",
@@ -690,11 +690,11 @@ impl TcpConnection {
690690
address: self.address,
691691
connected: self.is_connected(),
692692
last_seen: self.connected_at.unwrap_or(SystemTime::UNIX_EPOCH),
693-
version: self.peer_version,
694-
services: self.peer_services,
695-
user_agent: self.peer_user_agent.clone(),
696-
best_height: self.peer_best_height,
697-
wants_dsq_messages: None, // We don't track this in TcpConnection yet
693+
version: self.version,
694+
services: self.services,
695+
user_agent: self.user_agent.clone(),
696+
best_height: self.best_height,
697+
wants_dsq_messages: None, // We don't track this yet
698698
has_sent_headers2: false, // Will be tracked by the connection pool
699699
}
700700
}
@@ -803,20 +803,20 @@ impl TcpConnection {
803803

804804
/// Set that peer prefers headers2.
805805
pub fn set_prefers_headers2(&mut self, prefers: bool) {
806-
self.peer_prefers_headers2 = prefers;
806+
self.prefers_headers2 = prefers;
807807
if prefers {
808808
tracing::info!("Peer {} prefers headers2 compression", self.address);
809809
}
810810
}
811811

812812
/// Check if peer prefers headers2.
813813
pub fn prefers_headers2(&self) -> bool {
814-
self.peer_prefers_headers2
814+
self.prefers_headers2
815815
}
816816

817817
/// Set that peer sent us SendHeaders2.
818818
pub fn set_peer_sent_sendheaders2(&mut self, sent: bool) {
819-
self.peer_sent_sendheaders2 = sent;
819+
self.sent_sendheaders2 = sent;
820820
if sent {
821821
tracing::info!(
822822
"Peer {} sent SendHeaders2 - they will send compressed headers",
@@ -827,15 +827,15 @@ impl TcpConnection {
827827

828828
/// Check if peer sent us SendHeaders2.
829829
pub fn peer_sent_sendheaders2(&self) -> bool {
830-
self.peer_sent_sendheaders2
830+
self.sent_sendheaders2
831831
}
832832

833833
/// Check if we can request headers2 from this peer.
834834
pub fn can_request_headers2(&self) -> bool {
835835
// We can request headers2 if peer has the service flag for headers2 support
836836
// Note: We don't wait for SendHeaders2 from peer as that creates a race condition
837837
// during initial sync. The service flag is sufficient to know they support headers2.
838-
if let Some(services) = self.peer_services {
838+
if let Some(services) = self.services {
839839
dashcore::network::constants::ServiceFlags::from(services)
840840
.has(dashcore::network::constants::NODE_HEADERS_COMPRESSED)
841841
} else {

0 commit comments

Comments
 (0)