Skip to content

Commit

Permalink
Merge branch 'chore/misc-fixes-and-improvements' into 'dev'
Browse files Browse the repository at this point in the history
Misc bug fixes and improvements

Closes #70 and #71

See merge request ergo/rosen-bridge/rosenet!30
  • Loading branch information
vorujack committed Aug 19, 2024
2 parents 2aa4234 + 3142e08 commit 99720a9
Show file tree
Hide file tree
Showing 13 changed files with 113 additions and 21 deletions.
6 changes: 6 additions & 0 deletions .changeset/cool-snakes-perform.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@rosen-bridge/rosenet-relay': minor
'@rosen-bridge/rosenet-node': minor
---

Limit node connections to relay to 3 to prevent network traffic issues
5 changes: 5 additions & 0 deletions .changeset/empty-lobsters-pump.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@rosen-bridge/rosenet-relay': minor
---

Add `subscribe` function for subscribing to a pubsub topic
5 changes: 5 additions & 0 deletions .changeset/long-hornets-swim.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@rosen-bridge/rosenet-relay': minor
---

Configuare relays as Gossipsub bootstrappers
5 changes: 5 additions & 0 deletions .changeset/modern-hairs-scream.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@rosen-bridge/rosenet-utils': patch
---

Append unused format parameters to format function
6 changes: 6 additions & 0 deletions .changeset/tame-queens-study.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@rosen-bridge/rosenet-relay': minor
'@rosen-bridge/rosenet-node': minor
---

Switch from mplex to yamux for multiplexing
6 changes: 6 additions & 0 deletions .changeset/wet-tomatoes-nail.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@rosen-bridge/rosenet-relay': minor
'@rosen-bridge/rosenet-node': minor
---

Prevent Gossipsub from running on transient connections
25 changes: 25 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 11 additions & 6 deletions packages/rosenet-node/lib/createRoseNetNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { bootstrap } from '@libp2p/bootstrap';
import { circuitRelayTransport } from '@libp2p/circuit-relay-v2';
import { identify } from '@libp2p/identify';
import { PeerId } from '@libp2p/interface';
import { mplex } from '@libp2p/mplex';
import { yamux } from '@chainsafe/libp2p-yamux';
import { pubsubPeerDiscovery } from '@libp2p/pubsub-peer-discovery';
import { tcp } from '@libp2p/tcp';
import map from 'it-map';
Expand All @@ -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';

Expand Down Expand Up @@ -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: [
Expand All @@ -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`,
),
],
Expand All @@ -95,10 +101,10 @@ const createRoseNetNode = async ({
denyDialPeer: isPeerUnauthorized,
}),
},
streamMuxers: [mplex()],
streamMuxers: [yamux()],
peerDiscovery: [
bootstrap({
list: config.relayMultiaddrs,
list: sampledRelayMultiaddrs,
}),
pubsubPeerDiscovery(),
],
Expand All @@ -110,7 +116,6 @@ const createRoseNetNode = async ({
identify: identify(),
pubsub: gossipsub({
allowPublishToZeroPeers: true,
runOnTransientConnection: true,
}),
},
logger: libp2pLoggerFactory(logger, config.debug?.libp2pComponents ?? []),
Expand Down
13 changes: 13 additions & 0 deletions packages/rosenet-node/lib/utils/sample.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/**
* Sample `size` elements from `array` randomly
* @param array
* @param size
*/
const sample = <T>(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;
2 changes: 1 addition & 1 deletion packages/rosenet-node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@
"dependencies": {
"@chainsafe/libp2p-gossipsub": "^12.0.0",
"@chainsafe/libp2p-noise": "^15.0.0",
"@chainsafe/libp2p-yamux": "^6.0.2",
"@libp2p/bootstrap": "^10.0.15",
"@libp2p/circuit-relay-v2": "^1.0.15",
"@libp2p/identify": "^1.0.14",
"@libp2p/mplex": "^10.0.15",
"@libp2p/peer-id": "^4.0.6",
"@libp2p/pubsub-peer-discovery": "^10.0.2",
"@libp2p/tcp": "^9.0.15",
Expand Down
20 changes: 16 additions & 4 deletions packages/rosenet-relay/lib/createRoseNetRelay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { noise } from '@chainsafe/libp2p-noise';
import { circuitRelayServer } from '@libp2p/circuit-relay-v2';
import { identify } from '@libp2p/identify';
import { PeerId } from '@libp2p/interface';
import { mplex } from '@libp2p/mplex';
import { yamux } from '@chainsafe/libp2p-yamux';
import { pubsubPeerDiscovery } from '@libp2p/pubsub-peer-discovery';
import { tcp } from '@libp2p/tcp';
import { createLibp2p } from 'libp2p';
Expand Down Expand Up @@ -63,7 +63,7 @@ const createRoseNetRelay = async ({
denyInboundRelayedConnection: () => true,
denyDialPeer: isPeerUnauthorized,
},
streamMuxers: [mplex()],
streamMuxers: [yamux()],
services: {
circuitRelay: circuitRelayServer({
reservations: {
Expand All @@ -75,11 +75,14 @@ const createRoseNetRelay = async ({
}),
pubsub: gossipsub({
allowPublishToZeroPeers: true,
runOnTransientConnection: true,
D: 0,
Dlo: 0,
Dhi: 0,
Dout: 0,
}),
identify: identify(),
},
peerDiscovery: [pubsubPeerDiscovery()],
peerDiscovery: [pubsubPeerDiscovery({ listenOnly: true })],
nodeInfo: {
name: 'rosenet-relay',
version: packageJson.version,
Expand All @@ -93,6 +96,15 @@ const createRoseNetRelay = async ({

return {
start: async () => node.start(),
subscribe: async (topic: string, handler: (message: string) => void) => {
node.services.pubsub.subscribe(topic);
node.services.pubsub.addEventListener('message', (event) => {
if (event.detail.topic === topic) {
const textDecoder = new TextDecoder();
handler(textDecoder.decode(event.detail.data));
}
});
},
};
};

Expand Down
2 changes: 1 addition & 1 deletion packages/rosenet-relay/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@
"dependencies": {
"@chainsafe/libp2p-gossipsub": "^12.0.0",
"@chainsafe/libp2p-noise": "^15.0.0",
"@chainsafe/libp2p-yamux": "^6.0.2",
"@libp2p/circuit-relay-v2": "^1.0.15",
"@libp2p/identify": "^1.0.14",
"@libp2p/interface": "^1.1.3",
"@libp2p/mplex": "^10.0.15",
"@libp2p/pubsub-peer-discovery": "^10.0.2",
"@libp2p/tcp": "^9.0.15",
"@rosen-bridge/logger-interface": "^0.2.0",
Expand Down
22 changes: 13 additions & 9 deletions packages/utils/lib/logger/format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,19 @@ const formatters = {
const format = (str: string, ...args: any[]) => {
let j = 0;

return str.replace(/%([a-z])/gi, (match, f: keyof typeof formatters) => {
if (formatters[f]) {
return formatters[f](args[j++]);
}
if (f) {
return utilFormat(`%${f}`, args[j++]);
}
return match;
});
const formatted = str.replace(
/%([a-z])/gi,
(match, f: keyof typeof formatters) => {
if (formatters[f]) {
return formatters[f](args[j++]);
}
if (f) {
return utilFormat(`%${f}`, args[j++]);
}
return match;
},
);
return `${formatted} ${args.slice(j).join(' ')}`.trimEnd();
};

export default format;

0 comments on commit 99720a9

Please sign in to comment.