diff --git a/examples/canvas/src/index.ts b/examples/canvas/src/index.ts index 1a319322..1eb66e79 100644 --- a/examples/canvas/src/index.ts +++ b/examples/canvas/src/index.ts @@ -4,6 +4,14 @@ import { Pixel } from "./objects/pixel"; import { GCounter } from "@topology-foundation/crdt"; import { handleCanvasMessages } from "./handlers"; +// all libp2p debug logs +localStorage.setItem("debug", "libp2p:*"); // then refresh the page to ensure the libraries can read this when spinning up. +// networking debug logs +localStorage.setItem( + "debug", + "libp2p:websockets,libp2p:webtransport,libp2p:kad-dht,libp2p:dialer", +); + const node = new TopologyNode(); let canvasCRO: ICanvas; let peers: string[] = []; diff --git a/examples/canvas/webpack.config.js b/examples/canvas/webpack.config.js index d15d89b4..46ac5e36 100644 --- a/examples/canvas/webpack.config.js +++ b/examples/canvas/webpack.config.js @@ -7,6 +7,9 @@ module.exports = { entry: path.resolve(__dirname, "./src/index.ts"), devServer: { allowedHosts: "all", + client: { + overlay: false, + }, static: { directory: path.join(__dirname, "public"), }, diff --git a/examples/canvas/yarn.lock b/examples/canvas/yarn.lock index a6a7679d..c74ebda3 100644 --- a/examples/canvas/yarn.lock +++ b/examples/canvas/yarn.lock @@ -256,6 +256,21 @@ uint8arraylist "^2.4.8" uint8arrays "^5.1.0" +"@libp2p/dcutr@^1.1.0": + version "1.1.0" + resolved "https://registry.yarnpkg.com/@libp2p/dcutr/-/dcutr-1.1.0.tgz#0a73881361bb265ad6b43f0acffb5b41158fdb4d" + integrity sha512-C8tBevJxb6D8c8Qb2N14/nzSGJY84MyTwzAhf8cwKp7Um17RouTljUJEOakk+TRCp/UrPx3tbbVDOb2cmUR1Tg== + dependencies: + "@libp2p/interface" "^1.5.0" + "@libp2p/interface-internal" "^1.2.4" + "@libp2p/utils" "^5.4.4" + "@multiformats/multiaddr" "^12.2.3" + "@multiformats/multiaddr-matcher" "^1.2.1" + delay "^6.0.0" + it-protobuf-stream "^1.1.3" + protons-runtime "^5.4.0" + uint8arraylist "^2.4.8" + "@libp2p/identify@^2.0.2": version "2.0.2" resolved "https://registry.yarnpkg.com/@libp2p/identify/-/identify-2.0.2.tgz#a11c566dbbcdfda15a1e3762e6b8e88188c4f48b" @@ -739,6 +754,7 @@ "@topology-foundation/node@file:../../packages/node": version "0.0.7" dependencies: + "@libp2p/dcutr" "^1.1.0" "@topology-foundation/crdt" "0.0.7" "@topology-foundation/network" "0.0.7" "@topology-foundation/object" "0.0.7" diff --git a/packages/network/src/node.ts b/packages/network/src/node.ts index ab1d8c31..aef034b6 100644 --- a/packages/network/src/node.ts +++ b/packages/network/src/node.ts @@ -6,6 +6,7 @@ import { import { noise } from "@chainsafe/libp2p-noise"; import { yamux } from "@chainsafe/libp2p-yamux"; import { circuitRelayTransport } from "@libp2p/circuit-relay-v2"; +import { dcutr } from "@libp2p/dcutr"; import { identify } from "@libp2p/identify"; import { EventHandler, PubSub, Stream, StreamHandler } from "@libp2p/interface"; import { pubsubPeerDiscovery } from "@libp2p/pubsub-peer-discovery"; @@ -16,6 +17,8 @@ import { Libp2p, createLibp2p } from "libp2p"; import { stringToStream } from "./stream"; import { webTransport } from "@libp2p/webtransport"; import { bootstrap } from "@libp2p/bootstrap"; +import { fromString as uint8ArrayFromString } from "uint8arrays/from-string"; +import { toString as uint8ArrayToString } from "uint8arrays/to-string"; export interface TopologyNetworkNodeConfig {} @@ -34,7 +37,8 @@ export class TopologyNetworkNode { this._node = await createLibp2p({ addresses: { listen: [ - "/webrtc", + // disabled to test connections through relay + // "/webrtc", "/dns4/relay.droak.sh/tcp/443/wss/p2p/Qma3GsJmB47xYuyahPZPSadh1avvxfyYQwk8R3UnFrQ6aP/p2p-circuit", ], }, @@ -46,8 +50,7 @@ export class TopologyNetworkNode { }, peerDiscovery: [ pubsubPeerDiscovery({ - interval: 10_000, - listenOnly: false, + interval: 5_000, }), bootstrap({ list: [ @@ -60,10 +63,13 @@ export class TopologyNetworkNode { pubsub: gossipsub({ allowPublishToZeroTopicPeers: true, }), + dcutr: dcutr(), }, streamMuxers: [yamux()], transports: [ - circuitRelayTransport(), + circuitRelayTransport({ + discoverRelays: 1, + }), webRTC({ rtcConfiguration: { iceServers: [ @@ -86,14 +92,35 @@ export class TopologyNetworkNode { this._pubsub = this._node.services.pubsub as PubSub; this.peerId = this._node.peerId.toString(); + this._pubsub.subscribe("topology::object::announcements"); + this._pubsub?.addEventListener("gossipsub:message", async (message) => { + console.log(message.detail.msg); + if (message.detail.msg.topic != "topology::object::announcements") return; + const group = uint8ArrayToString(new Uint8Array(message.detail.msg.data)); + console.log("announcing group", group); + if (!this._pubsub?.getTopics().includes(group)) return; + console.log("dialing", message.detail.propagationSource.toString()); + await this._node?.dial([ + multiaddr(`/p2p/${message.detail.propagationSource.toString()}`), + ]); + }); + console.log( "topology::network::start: Successfuly started topology network w/ peer_id", this.peerId, ); - this._node.addEventListener("peer:connect", (evt) => { - console.log("topology::network::peer::connect: ", evt.detail.toString()); - }); + // TODO remove this or add better logger + // we need to keep it now for debugging + this._node.addEventListener("peer:connect", (e) => + console.log("peer:connect", e.detail), + ); + this._node.addEventListener("peer:discovery", (e) => + console.log("peer:discovery", e.detail), + ); + this._node.addEventListener("peer:identify", (e) => + console.log("peer:identify", e.detail), + ); } subscribe(topic: string) { @@ -106,6 +133,10 @@ export class TopologyNetworkNode { try { this._pubsub?.subscribe(topic); + this.broadcastMessage( + "topology::object::announcements", + uint8ArrayFromString(topic), + ).then(); this._pubsub?.getPeers(); console.log( "topology::network::subscribe: Successfuly subscribed the topic", diff --git a/packages/node/package.json b/packages/node/package.json index ffe120c0..f7dbe873 100644 --- a/packages/node/package.json +++ b/packages/node/package.json @@ -36,6 +36,7 @@ "vitest": "^1.6.0" }, "dependencies": { + "@libp2p/dcutr": "^1.1.0", "@topology-foundation/crdt": "0.0.7", "@topology-foundation/network": "0.0.7", "@topology-foundation/object": "0.0.7" diff --git a/yarn.lock b/yarn.lock index eec11d86..82d606cd 100644 --- a/yarn.lock +++ b/yarn.lock @@ -310,6 +310,21 @@ uint8arraylist "^2.4.8" uint8arrays "^5.1.0" +"@libp2p/dcutr@^1.1.0": + version "1.1.0" + resolved "https://registry.yarnpkg.com/@libp2p/dcutr/-/dcutr-1.1.0.tgz#0a73881361bb265ad6b43f0acffb5b41158fdb4d" + integrity sha512-C8tBevJxb6D8c8Qb2N14/nzSGJY84MyTwzAhf8cwKp7Um17RouTljUJEOakk+TRCp/UrPx3tbbVDOb2cmUR1Tg== + dependencies: + "@libp2p/interface" "^1.5.0" + "@libp2p/interface-internal" "^1.2.4" + "@libp2p/utils" "^5.4.4" + "@multiformats/multiaddr" "^12.2.3" + "@multiformats/multiaddr-matcher" "^1.2.1" + delay "^6.0.0" + it-protobuf-stream "^1.1.3" + protons-runtime "^5.4.0" + uint8arraylist "^2.4.8" + "@libp2p/identify@^2.0.2": version "2.1.0" resolved "https://registry.yarnpkg.com/@libp2p/identify/-/identify-2.1.0.tgz#663daa8e1f541326630655a8bd52aba015797017"