Skip to content

Commit

Permalink
Testing connection to DHT
Browse files Browse the repository at this point in the history
  • Loading branch information
elielnfinic committed Oct 28, 2024
1 parent 0030825 commit 015a970
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 10 deletions.
36 changes: 28 additions & 8 deletions packages/network/src/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ import { type Libp2p, createLibp2p } from "libp2p";
import { fromString as uint8ArrayFromString } from "uint8arrays/from-string";
import { Message } from "./proto/messages_pb.js";
import { uint8ArrayToStream } from "./stream.js";
import { kadDHT } from "@libp2p/kad-dht";
import { KadDHT, kadDHT } from "@libp2p/kad-dht";
import { TopologyDHT } from "./utils/topology_dht.js";
import { peerIdFromString } from '@libp2p/peer-id'


export * from "./stream.js";

Expand All @@ -48,6 +51,7 @@ export class TopologyNetworkNode {
private _config?: TopologyNetworkNodeConfig;
private _node?: Libp2p;
private _pubsub?: PubSub<GossipsubEvents>;
private _topologyDHT?: TopologyDHT;

peerId = "";

Expand Down Expand Up @@ -93,7 +97,7 @@ export class TopologyNetworkNode {
dcutr: dcutr(),
identify: identify(),
pubsub: gossipsub(),
kadDHT: kadDHT({
dht: kadDHT({
protocol: "/topology/dht/1.0.0",
kBucketSize: 50,
clientMode: false,
Expand Down Expand Up @@ -136,31 +140,47 @@ export class TopologyNetworkNode {

if (!this._config?.bootstrap) {
for (const addr of this._config?.bootstrap_peers || []) {
this._node.dial(multiaddr(addr));
const stream = await this._node.dial(multiaddr(addr));
}
}

if (!this._config?.bootstrap) {
// get bootstrap node peerStore
setInterval(() => {
console.log("CHeck peers");
const peers = this._node?.peerStore;
console.log("PEERS", peers);

}, 10000);
}

const bootstrap_peer_id = peerIdFromString("12D3KooWC6sm9iwmYbeQJCJipKTRghmABNz1wnpJANvSMabvecwJ");

this._pubsub = this._node.services.pubsub as PubSub<GossipsubEvents>;

this.peerId = this._node.peerId.toString();

console.log(
"topology::network::start: Successfuly started topology network w/ peer_id",
this.peerId,
);

this._node.addEventListener("peer:connect", (e) =>
this._node.addEventListener("peer:connect", (e) => {
console.log("::start::peer::connect", e.detail),
);
this._topologyDHT = new TopologyDHT(this._node?.services.dht as KadDHT, bootstrap_peer_id);
console.log("CONNECTED TO DHT");
});
this._node.addEventListener("peer:discovery", (e) => {
// current bug in v11.0.0 requires manual dial (https://github.com/libp2p/js-libp2p-pubsub-peer-discovery/issues/149)
for (const ma of e.detail.multiaddrs) {
this._node?.dial(ma);
}
console.log("::start::peer::discovery", e.detail);
});
this._node.addEventListener("peer:identify", (e) =>
console.log("::start::peer::identify", e.detail),
);
this._node.addEventListener("peer:identify", (e) => {
// TO BE UNCOMMENTED
// console.log("::start::peer::identify", e.detail),
});
}

subscribe(topic: string) {
Expand Down
4 changes: 2 additions & 2 deletions packages/network/src/utils/topology_dht.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import { type KadDHT } from "@libp2p/kad-dht";
import { toString as uint8ArrayToString } from "uint8arrays";
import { fromString as uint8ArrayFromString } from "uint8arrays";

class TopologyDHT {
export class TopologyDHT {
private _dht: KadDHT;

constructor(dht: KadDHT) {
constructor(dht: KadDHT, peerId: PeerId) {
this._dht = dht;
}

Expand Down

0 comments on commit 015a970

Please sign in to comment.