Skip to content

Commit

Permalink
feat: improve connectivity (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
d-roak authored Jun 23, 2024
1 parent caac5ee commit e9000d6
Show file tree
Hide file tree
Showing 5 changed files with 874 additions and 923 deletions.
12 changes: 5 additions & 7 deletions packages/network/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,7 @@
},
"type": "module",
"types": "./dist/src/index.d.ts",
"files": [
"src",
"dist",
"!dist/test",
"!**/*.tsbuildinfo"
],
"files": ["src", "dist", "!dist/test", "!**/*.tsbuildinfo"],
"exports": {
".": {
"types": "./dist/src/index.d.ts",
Expand All @@ -34,12 +29,15 @@
"@chainsafe/libp2p-gossipsub": "^13.0.0",
"@chainsafe/libp2p-noise": "^15.0.0",
"@chainsafe/libp2p-yamux": "^6.0.2",
"@libp2p/autonat": "^1.0.0",
"@libp2p/bootstrap": "^10.1.0",
"@libp2p/circuit-relay-v2": "^1.0.24",
"@libp2p/identify": "^2.0.2",
"@libp2p/mplex": "^10.0.24",
"@libp2p/interface-pubsub": "^4.0.1",
"@libp2p/pubsub-peer-discovery": "^10.0.2",
"@libp2p/webrtc": "^4.0.33",
"@libp2p/websockets": "^8.0.24",
"@libp2p/webtransport": "^4.0.32",
"@multiformats/multiaddr": "^12.3.0",
"it-pipe": "^3.0.1",
"libp2p": "^1.6.0"
Expand Down
56 changes: 39 additions & 17 deletions packages/network/src/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@ import { circuitRelayTransport } from "@libp2p/circuit-relay-v2";
import { identify } from "@libp2p/identify";
import { EventHandler, PubSub, Stream, StreamHandler } from "@libp2p/interface";
import { pubsubPeerDiscovery } from "@libp2p/pubsub-peer-discovery";
import { webRTC } from "@libp2p/webrtc";
import { webRTC, webRTCDirect } from "@libp2p/webrtc";
import { webSockets } from "@libp2p/websockets";
import * as filters from "@libp2p/websockets/filters";
import { multiaddr } from "@multiformats/multiaddr";
import { Libp2p, createLibp2p } from "libp2p";
import { stringToStream } from "./stream";
import { webTransport } from "@libp2p/webtransport";
import { bootstrap } from "@libp2p/bootstrap";

export interface TopologyNetworkNodeConfig {}

Expand All @@ -40,30 +41,45 @@ export class TopologyNetworkNode {
return false;
},
},
peerDiscovery: [pubsubPeerDiscovery()],
peerDiscovery: [
pubsubPeerDiscovery({
interval: 10_000,
listenOnly: false,
}),
bootstrap({
list: [
"/dns4/relay.droak.sh/tcp/443/wss/p2p/Qma3GsJmB47xYuyahPZPSadh1avvxfyYQwk8R3UnFrQ6aP",
],
}),
],
services: {
identify: identify(),
pubsub: gossipsub(),
pubsub: gossipsub({
allowPublishToZeroTopicPeers: true,
}),
},
streamMuxers: [yamux()],
transports: [
webSockets({ filter: filters.all }),
webRTC(),
circuitRelayTransport({
discoverRelays: 1,
circuitRelayTransport(),
webRTC({
rtcConfiguration: {
iceServers: [
{
// STUN servers help the browser discover its own public IPs
urls: [
"stun:stun.l.google.com:19302",
"stun:global.stun.twilio.com:3478",
],
},
],
},
}),
webRTCDirect(),
webSockets(),
webTransport(),
],
});

// bootstrap
// TODO: use another technique instead of dial
// Oak's server
await this._node.dial([
multiaddr(
"/dns4/relay.droak.sh/tcp/443/wss/p2p/Qma3GsJmB47xYuyahPZPSadh1avvxfyYQwk8R3UnFrQ6aP",
),
]);

this._pubsub = this._node.services.pubsub as PubSub<GossipsubEvents>;
this.peerId = this._node.peerId.toString();

Expand Down Expand Up @@ -116,6 +132,12 @@ export class TopologyNetworkNode {
}
}

getGroupPeers(group: string) {
const peers = this._pubsub?.getSubscribers(group);
if (!peers) return [];
return peers.map((peer) => peer.toString());
}

async broadcastMessage(topic: string, message: Uint8Array) {
try {
if (this._pubsub?.getSubscribers(topic)?.length === 0) return;
Expand Down
16 changes: 5 additions & 11 deletions packages/network/src/relay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import { identify } from "@libp2p/identify";
import { createFromJSON } from "@libp2p/peer-id-factory";
import { pubsubPeerDiscovery } from "@libp2p/pubsub-peer-discovery";
import { webSockets } from "@libp2p/websockets";
import * as filters from "@libp2p/websockets/filters";
import { createLibp2p } from "libp2p";

import relayerJson from "./peer-id-relayer";
import { autoNAT } from "@libp2p/autonat";

// TODO:
// - remove the peer-id-relayer in favor of static configs
Expand All @@ -20,24 +20,18 @@ export const createRelayNode = async () => {
const node = await createLibp2p({
peerId: idRelayer,
addresses: {
listen: ["/ip4/0.0.0.0/tcp/50000/ws"],
listen: ["/ip4/0.0.0.0/tcp/50000/ws", "/ip4/0.0.0.0/tcp/50001"],
},
connectionEncryption: [noise()],
connectionManager: {
minConnections: 0,
},
peerDiscovery: [pubsubPeerDiscovery()],
services: {
autonat: autoNAT(),
identify: identify(),
pubsub: gossipsub(),
relay: circuitRelayServer({
reservations: {
maxReservations: Infinity,
},
}),
relay: circuitRelayServer(),
},
streamMuxers: [yamux()],
transports: [webSockets({ filter: filters.all })],
transports: [webSockets()],
});

// Log a message when a remote peer connects to us
Expand Down
4 changes: 4 additions & 0 deletions packages/node/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ export class TopologyNode {
);
}

getPeersPerGroup(group: string) {
return this._networkNode.getGroupPeers(group);
}

/// Get the object from the local Object Store
getObject(objectId: string) {
return this._objectStore.get(objectId);
Expand Down
Loading

0 comments on commit e9000d6

Please sign in to comment.