Skip to content

Commit 87e0e65

Browse files
committed
\CRITICAL FIX: Prevent self-connection and fake peer creation in P2P discovery"
1 parent bbf90a1 commit 87e0e65

1 file changed

Lines changed: 15 additions & 32 deletions

File tree

‎development/qnet-integration/src/unified_p2p.rs‎

Lines changed: 15 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -280,8 +280,19 @@ impl SimplifiedP2P {
280280
println!("[P2P] ✅ Genesis bootstrap enabled - true decentralized network");
281281
}
282282

283+
// Get our own external IP to avoid self-connection
284+
let our_external_ip = match Self::get_our_ip_address().await {
285+
Ok(ip) => ip,
286+
Err(_) => "unknown".to_string(),
287+
};
288+
283289
// Search on known server IPs with proper regional ports
284290
for ip in known_node_ips {
291+
// CRITICAL: Skip our own IP to prevent self-connection
292+
if ip == our_external_ip {
293+
println!("[P2P] 🚫 Skipping self-connection to own IP: {}", ip);
294+
continue;
295+
}
285296
// Determine correct regional ports for this IP
286297
let target_ports = if let Some((_, region_name)) = GENESIS_BOOTSTRAP_NODES.iter().find(|(node_ip, _)| *node_ip == ip) {
287298
match *region_name {
@@ -1355,38 +1366,10 @@ impl SimplifiedP2P {
13551366
}
13561367
};
13571368

1358-
// Find more peers in our region using dynamic discovery
1359-
let peer_info = PeerInfo {
1360-
id: format!("regional_{}_{}", region_string(&region), rand::random::<u32>()),
1361-
addr: format!("{}:987{}", external_ip, 6 + rand::random::<u8>() % 10),
1362-
node_type: NodeType::Full,
1363-
region: region.clone(),
1364-
last_seen: std::time::SystemTime::now()
1365-
.duration_since(std::time::UNIX_EPOCH)
1366-
.unwrap()
1367-
.as_secs(),
1368-
is_stable: true,
1369-
cpu_load: 0.4,
1370-
latency_ms: 25,
1371-
connection_count: 0,
1372-
bandwidth_usage: 0,
1373-
};
1374-
1375-
// Add to regional peers
1376-
{
1377-
let mut regional_peers = regional_peers.lock().unwrap();
1378-
regional_peers
1379-
.entry(peer_info.region.clone())
1380-
.or_insert_with(Vec::new)
1381-
.push(peer_info.clone());
1382-
}
1383-
1384-
// Add to connected peers
1385-
{
1386-
let mut connected = connected_peers.lock().unwrap();
1387-
connected.push(peer_info);
1388-
println!("[P2P] ✅ Added regional peer to improve clustering");
1389-
}
1369+
// FIXED: Do not create fake peers with own IP and random ports
1370+
// Regional clustering should only use real discovered peers
1371+
println!("[P2P] 🔍 Region {} needs more peers, but not creating fake ones", region_string(&region));
1372+
println!("[P2P] 💡 Waiting for real peer discovery through internet search");
13901373
}
13911374

13921375
// Report regional distribution

0 commit comments

Comments
 (0)