Skip to content

Commit

Permalink
debugging stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
d-roak committed Jun 27, 2024
1 parent 28d9b2d commit c2a1c97
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 7 deletions.
8 changes: 8 additions & 0 deletions examples/canvas/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[] = [];
Expand Down
3 changes: 3 additions & 0 deletions examples/canvas/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
},
Expand Down
16 changes: 16 additions & 0 deletions examples/canvas/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down
45 changes: 38 additions & 7 deletions packages/network/src/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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 {}

Expand All @@ -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",
],
},
Expand All @@ -46,8 +50,7 @@ export class TopologyNetworkNode {
},
peerDiscovery: [
pubsubPeerDiscovery({
interval: 10_000,
listenOnly: false,
interval: 5_000,
}),
bootstrap({
list: [
Expand All @@ -60,10 +63,13 @@ export class TopologyNetworkNode {
pubsub: gossipsub({
allowPublishToZeroTopicPeers: true,
}),
dcutr: dcutr(),
},
streamMuxers: [yamux()],
transports: [
circuitRelayTransport(),
circuitRelayTransport({
discoverRelays: 1,
}),
webRTC({
rtcConfiguration: {
iceServers: [
Expand All @@ -86,14 +92,35 @@ export class TopologyNetworkNode {
this._pubsub = this._node.services.pubsub as PubSub<GossipsubEvents>;
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) {
Expand All @@ -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",
Expand Down
1 change: 1 addition & 0 deletions packages/node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
15 changes: 15 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit c2a1c97

Please sign in to comment.