From c1e300832011b219c2838b090738b3168524b4ec Mon Sep 17 00:00:00 2001 From: Mohammad Kermani Date: Sun, 18 Aug 2024 12:18:17 +0330 Subject: [PATCH] feat(rosenet-node, rosenet-relay): limit relays count for nodes Connecting to a lot of relays can significantly affect pubsub network traffic. For 40 nodes, 3 relay per node seems a good balance between traffic, delivery and performance. This number may change in the future, though. Also prevent relays from broadcasting their addresses over discovery network. If not disable, it totally negates the point of limiting relays count to 3. --- packages/rosenet-node/lib/createRoseNetNode.ts | 12 +++++++++--- packages/rosenet-node/lib/utils/sample.ts | 13 +++++++++++++ packages/rosenet-relay/lib/createRoseNetRelay.ts | 2 +- 3 files changed, 23 insertions(+), 4 deletions(-) create mode 100644 packages/rosenet-node/lib/utils/sample.ts diff --git a/packages/rosenet-node/lib/createRoseNetNode.ts b/packages/rosenet-node/lib/createRoseNetNode.ts index 0e785a1..d1f5ad5 100644 --- a/packages/rosenet-node/lib/createRoseNetNode.ts +++ b/packages/rosenet-node/lib/createRoseNetNode.ts @@ -23,6 +23,7 @@ import addressService from './address/address-service'; import streamService from './stream/stream-service'; import { decode, encode } from './utils/codec'; +import sample from './utils/sample'; import RoseNetNodeError from './errors/RoseNetNodeError'; @@ -61,6 +62,11 @@ const createRoseNetNode = async ({ const announceMultiaddr = await addressService.getAnnounceMultiaddr(port); logger.info(`${announceMultiaddr} set as announce multiaddr`); + const sampledRelayMultiaddrs = sample( + config.relayMultiaddrs, + RELAYS_COUNT_TO_CONNECT, + ); + const node = await createLibp2p({ peerId, transports: [ @@ -72,13 +78,13 @@ const createRoseNetNode = async ({ addresses: { listen: [ `/ip4/0.0.0.0/tcp/${port}`, - ...config.relayMultiaddrs.map( + ...sampledRelayMultiaddrs.map( (multiaddr) => `${multiaddr}/p2p-circuit`, ), ], announce: [ announceMultiaddr, - ...config.relayMultiaddrs.map( + ...sampledRelayMultiaddrs.map( (multiaddr) => `${multiaddr}/p2p-circuit`, ), ], @@ -98,7 +104,7 @@ const createRoseNetNode = async ({ streamMuxers: [yamux()], peerDiscovery: [ bootstrap({ - list: config.relayMultiaddrs, + list: sampledRelayMultiaddrs, }), pubsubPeerDiscovery(), ], diff --git a/packages/rosenet-node/lib/utils/sample.ts b/packages/rosenet-node/lib/utils/sample.ts new file mode 100644 index 0000000..f986386 --- /dev/null +++ b/packages/rosenet-node/lib/utils/sample.ts @@ -0,0 +1,13 @@ +/** + * Sample `size` elements from `array` randomly + * @param array + * @param size + */ +const sample = (array: T[], size: number): T[] => + array + .map((value) => ({ value, sort: Math.random() })) + .sort((a, b) => a.sort - b.sort) + .map(({ value }) => value) + .slice(0, size); + +export default sample; diff --git a/packages/rosenet-relay/lib/createRoseNetRelay.ts b/packages/rosenet-relay/lib/createRoseNetRelay.ts index 40585bd..2caad00 100644 --- a/packages/rosenet-relay/lib/createRoseNetRelay.ts +++ b/packages/rosenet-relay/lib/createRoseNetRelay.ts @@ -82,7 +82,7 @@ const createRoseNetRelay = async ({ }), identify: identify(), }, - peerDiscovery: [pubsubPeerDiscovery()], + peerDiscovery: [pubsubPeerDiscovery({ listenOnly: true })], nodeInfo: { name: 'rosenet-relay', version: packageJson.version,