diff --git a/README.md b/README.md index a678ca4..b5b7e84 100644 --- a/README.md +++ b/README.md @@ -101,7 +101,7 @@ Tune the network parameters to fit your system resources. The defaults are safe |----------|---------|-------------| | `MAX_PEERS` | `50000` | Maximum number of peers to track in memory. | | `MAX_MESSAGE_SIZE` | `2048` | Maximum size of a single message in bytes. | -| `MAX_RELAY_HOPS` | `2` | Maximum number of times a message is relayed. | +| `MAX_RELAY_HOPS` | `5` | Maximum number of times a message is relayed. | | `MAX_CONNECTIONS` | `15` | Maximum number of active P2P connections. | | `HEARTBEAT_INTERVAL` | `30000` | How often (ms) to send heartbeat messages. | | `CONNECTION_ROTATION_INTERVAL` | `300000` | How often (ms) to rotate connections. | diff --git a/docker-compose.yml b/docker-compose.yml index ac6e4ef..388befd 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -19,6 +19,6 @@ services: # - CONNECTION_ROTATION_INTERVAL=300000 # Connection rotation frequency (ms) # - PEER_TIMEOUT=45000 # Time before peer is considered offline (ms) # - MAX_MESSAGE_SIZE=2048 # Max message size (bytes) - # - MAX_RELAY_HOPS=2 # Max message relay hops + # - MAX_RELAY_HOPS=5 # Max message relay hops # - CHAT_RATE_LIMIT=5000 # Chat rate limit window (ms) # - VISUAL_LIMIT=500 # Max particles on dashboard diff --git a/src/config/constants.js b/src/config/constants.js index b4c3acb..bf243c1 100644 --- a/src/config/constants.js +++ b/src/config/constants.js @@ -18,7 +18,7 @@ const VERIFICATION_POW_PREFIX = "0000"; const MAX_PEERS = parseInt(process.env.MAX_PEERS) || 50000; const MAX_MESSAGE_SIZE = parseInt(process.env.MAX_MESSAGE_SIZE) || 2048; -const MAX_RELAY_HOPS = parseInt(process.env.MAX_RELAY_HOPS) || 2; +const MAX_RELAY_HOPS = parseInt(process.env.MAX_RELAY_HOPS) || 5; const MAX_CONNECTIONS = parseInt(process.env.MAX_CONNECTIONS) || 15; const HEARTBEAT_INTERVAL = parseInt(process.env.HEARTBEAT_INTERVAL) || 30000; diff --git a/src/p2p/relay.js b/src/p2p/relay.js index 70f708f..b0c0c2d 100644 --- a/src/p2p/relay.js +++ b/src/p2p/relay.js @@ -2,13 +2,15 @@ const relayMessage = (msg, sourceSocket, swarm, diagnostics) => { const data = JSON.stringify(msg) + "\n"; // Gossip Subsampling: - // Instead of flooding everyone (which causes massive bandwidth usage with 50 connections), - // we relay to a random subset of peers (e.g., 6). - // This maintains "Epidemic" reach (O(log N)) while capping bandwidth. - - const TARGET_GOSSIP_COUNT = 6; + // We relay to a subset of peers to prevent flooding, but scale with connection count. + // We use a minimum of 6 or 25% of eligible peers, whichever is larger. + const allSockets = Array.from(swarm.connections); const eligible = allSockets.filter((s) => s !== sourceSocket); + + const MIN_GOSSIP_COUNT = 6; + const GOSSIP_FACTOR = 0.25; // Relay to 25% of peers + const TARGET_GOSSIP_COUNT = Math.max(MIN_GOSSIP_COUNT, Math.ceil(eligible.length * GOSSIP_FACTOR)); let targets = eligible;