Skip to content

Commit 7fac9cb

Browse files
committed
feat(p2p): increase max relay hops and improve gossip algorithm
Increase MAX_RELAY_HOPS from 2 to 5 for better message propagation. Update gossip algorithm to scale with connection count, using a minimum of 6 or 25% of eligible peers.
1 parent 43fe041 commit 7fac9cb

File tree

4 files changed

+10
-8
lines changed

4 files changed

+10
-8
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ Tune the network parameters to fit your system resources. The defaults are safe
101101
|----------|---------|-------------|
102102
| `MAX_PEERS` | `50000` | Maximum number of peers to track in memory. |
103103
| `MAX_MESSAGE_SIZE` | `2048` | Maximum size of a single message in bytes. |
104-
| `MAX_RELAY_HOPS` | `2` | Maximum number of times a message is relayed. |
104+
| `MAX_RELAY_HOPS` | `5` | Maximum number of times a message is relayed. |
105105
| `MAX_CONNECTIONS` | `15` | Maximum number of active P2P connections. |
106106
| `HEARTBEAT_INTERVAL` | `30000` | How often (ms) to send heartbeat messages. |
107107
| `CONNECTION_ROTATION_INTERVAL` | `300000` | How often (ms) to rotate connections. |

docker-compose.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,6 @@ services:
1919
# - CONNECTION_ROTATION_INTERVAL=300000 # Connection rotation frequency (ms)
2020
# - PEER_TIMEOUT=45000 # Time before peer is considered offline (ms)
2121
# - MAX_MESSAGE_SIZE=2048 # Max message size (bytes)
22-
# - MAX_RELAY_HOPS=2 # Max message relay hops
22+
# - MAX_RELAY_HOPS=5 # Max message relay hops
2323
# - CHAT_RATE_LIMIT=5000 # Chat rate limit window (ms)
2424
# - VISUAL_LIMIT=500 # Max particles on dashboard

src/config/constants.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ const VERIFICATION_POW_PREFIX = "0000";
1818

1919
const MAX_PEERS = parseInt(process.env.MAX_PEERS) || 50000;
2020
const MAX_MESSAGE_SIZE = parseInt(process.env.MAX_MESSAGE_SIZE) || 2048;
21-
const MAX_RELAY_HOPS = parseInt(process.env.MAX_RELAY_HOPS) || 2;
21+
const MAX_RELAY_HOPS = parseInt(process.env.MAX_RELAY_HOPS) || 5;
2222
const MAX_CONNECTIONS = parseInt(process.env.MAX_CONNECTIONS) || 15;
2323

2424
const HEARTBEAT_INTERVAL = parseInt(process.env.HEARTBEAT_INTERVAL) || 30000;

src/p2p/relay.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@ const relayMessage = (msg, sourceSocket, swarm, diagnostics) => {
22
const data = JSON.stringify(msg) + "\n";
33

44
// Gossip Subsampling:
5-
// Instead of flooding everyone (which causes massive bandwidth usage with 50 connections),
6-
// we relay to a random subset of peers (e.g., 6).
7-
// This maintains "Epidemic" reach (O(log N)) while capping bandwidth.
8-
9-
const TARGET_GOSSIP_COUNT = 6;
5+
// We relay to a subset of peers to prevent flooding, but scale with connection count.
6+
// We use a minimum of 6 or 25% of eligible peers, whichever is larger.
7+
108
const allSockets = Array.from(swarm.connections);
119
const eligible = allSockets.filter((s) => s !== sourceSocket);
10+
11+
const MIN_GOSSIP_COUNT = 6;
12+
const GOSSIP_FACTOR = 0.25; // Relay to 25% of peers
13+
const TARGET_GOSSIP_COUNT = Math.max(MIN_GOSSIP_COUNT, Math.ceil(eligible.length * GOSSIP_FACTOR));
1214

1315
let targets = eligible;
1416

0 commit comments

Comments
 (0)