Skip to content

Commit 3be7597

Browse files
committed
Fix critical P2P network issues
- Fix self-connection bug preventing proper peer discovery - Remove duplicate API server startup causing port conflicts - Add missing /api/v1/peers endpoint for P2P communication - Consolidate RPC/API servers to single unified server on port 8001 - Improve peer validation with additional localhost checks - Ensure proper HTTP API responses for blockchain height sync
1 parent 0509b66 commit 3be7597

3 files changed

Lines changed: 31 additions & 8 deletions

File tree

development/qnet-integration/src/node.rs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -358,24 +358,19 @@ impl BlockchainNode {
358358
8001 // fallback
359359
});
360360

361-
// Start both RPC and API servers for Full/Super nodes
362-
let node_clone_rpc = self.clone();
361+
// Start unified API/RPC server for Full/Super nodes
363362
let node_clone_api = self.clone();
364363

365-
tokio::spawn(async move {
366-
crate::rpc::start_rpc_server(node_clone_rpc, rpc_port).await;
367-
});
368-
369364
println!("[Node] 🚀 API server starting on port {}", api_port);
370365
tokio::spawn(async move {
371366
crate::rpc::start_rpc_server(node_clone_api, api_port).await;
372367
});
373368

374369
// Store ports for external access
375-
std::env::set_var("QNET_CURRENT_RPC_PORT", rpc_port.to_string());
370+
std::env::set_var("QNET_CURRENT_RPC_PORT", api_port.to_string()); // Unified port
376371
std::env::set_var("QNET_CURRENT_API_PORT", api_port.to_string());
377372

378-
println!("[Node] 🔌 RPC server: port {}", rpc_port);
373+
println!("[Node] 🔌 RPC server: port {}", api_port);
379374
println!("[Node] 🌐 API server: port {}", api_port);
380375
} else {
381376
// Light nodes: RPC only, no API server

development/qnet-integration/src/rpc.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,27 @@ pub async fn start_rpc_server(blockchain: BlockchainNode, port: u16) {
231231
.and(blockchain_filter.clone())
232232
.and_then(handle_mempool_transactions);
233233

234+
// Peer discovery endpoint (for P2P network)
235+
let peers_endpoint = api_v1
236+
.and(warp::path("peers"))
237+
.and(warp::path::end())
238+
.and(warp::get())
239+
.and(blockchain_filter.clone())
240+
.and_then(|blockchain: Arc<BlockchainNode>| async move {
241+
let peers = blockchain.get_connected_peers().await.unwrap_or_default();
242+
let peer_list: Vec<serde_json::Value> = peers.iter().map(|peer| {
243+
json!({
244+
"id": peer.id,
245+
"address": peer.address,
246+
"node_type": peer.node_type,
247+
"region": peer.region,
248+
"last_seen": peer.last_seen
249+
})
250+
}).collect();
251+
println!("[API] 📊 Peers request: returning {} peers", peer_list.len());
252+
Ok::<_, Rejection>(warp::reply::json(&json!({"peers": peer_list})))
253+
});
254+
234255
// Batch operations endpoints
235256
let batch_claim_rewards = api_v1
236257
.and(warp::path("batch"))
@@ -296,6 +317,7 @@ pub async fn start_rpc_server(blockchain: BlockchainNode, port: u16) {
296317
let routes = rpc_path
297318
.or(root_path)
298319
.or(chain_height)
320+
.or(peers_endpoint)
299321
.or(microblock_one)
300322
.or(microblocks_range)
301323
.or(account_info)

development/qnet-integration/src/unified_p2p.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,12 @@ impl SimplifiedP2P {
300300
continue;
301301
}
302302

303+
// ADDITIONAL CHECK: Skip if IP matches any of our listening addresses
304+
if ip == "127.0.0.1" || ip == "0.0.0.0" || ip == "localhost" {
305+
println!("[P2P] 🚫 Skipping local address: {}", ip);
306+
continue;
307+
}
308+
303309
println!("[P2P] 🌐 Attempting to connect to peer: {}", ip);
304310
// PRODUCTION FIX: Test actual HTTP API ports where nodes listen
305311
// 8001 = primary API port, 9877 = RPC port

0 commit comments

Comments
 (0)