Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: wait for node to be dialable after libp2p.start #418

Merged
merged 5 commits into from
Feb 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion packages/network/src/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,11 @@ export class DRPNetworkNode {
webTransport(),
],
});
log.info(
"::start: running on:",
this._node.getMultiaddrs().map((addr) => addr.toString())
);

log.info("running on:", this._node.getMultiaddrs());
if (!this._config?.bootstrap) {
for (const addr of this._config?.bootstrap_peers || []) {
try {
Expand Down Expand Up @@ -238,6 +241,25 @@ export class DRPNetworkNode {
await this.start();
}

async isDialable(callback?: () => void | Promise<void>) {
let dialable = await this._node?.isDialable(this._node.getMultiaddrs());
if (dialable && callback) {
await callback();
return true;
}
if (!callback) return false;

const checkDialable = async () => {
dialable = await this._node?.isDialable(this._node.getMultiaddrs());
if (dialable) {
await callback();
}
};

this._node?.addEventListener("transport:listening", checkDialable);
return false;
}

private _sortAddresses(a: Address, b: Address) {
const localRegex =
/(^\/ip4\/127\.)|(^\/ip4\/10\.)|(^\/ip4\/172\.1[6-9]\.)|(^\/ip4\/172\.2[0-9]\.)|(^\/ip4\/172\.3[0-1]\.)|(^\/ip4\/192\.168\.)/;
Expand Down
56 changes: 54 additions & 2 deletions packages/network/tests/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,55 @@
import { test } from "vitest";
import { beforeAll, describe, expect, test } from "vitest";

test("mock", () => {});
import { DRPNetworkNode } from "../src/node.js";

describe("isDialable", () => {
let btNode: DRPNetworkNode;
beforeAll(async () => {
btNode = new DRPNetworkNode({
bootstrap: true,
listen_addresses: ["/ip4/0.0.0.0/tcp/0/ws"],
bootstrap_peers: [],
private_key_seed: "bootstrap_is_dialable",
});
await btNode.start();
});

const isDialable = async (node: DRPNetworkNode, timeout = false) => {
let resolver: (value: boolean) => void;
const promise = new Promise<boolean>((resolve) => {
resolver = resolve;
});

if (timeout) {
setTimeout(() => {
resolver(false);
}, 10);
}

const callback = () => {
resolver(true);
};

await node.isDialable(callback);
return await promise;
};

test("should return true if the node is dialable", async () => {
const node = new DRPNetworkNode({
bootstrap_peers: btNode.getMultiaddrs()?.map((addr) => addr.toString()) || [],
private_key_seed: "is_dialable_node_1",
});
await node.start();
expect(await isDialable(node)).toBe(true);
});

test("should return false if the node is not dialable", async () => {
const node = new DRPNetworkNode({
bootstrap_peers: btNode.getMultiaddrs()?.map((addr) => addr.toString()) || [],
private_key_seed: "is_dialable_node_2",
listen_addresses: [],
});
await node.start();
expect(await isDialable(node, true)).toBe(false);
});
});
2 changes: 1 addition & 1 deletion packages/node/src/version.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export const VERSION = "0.6.1";
export const VERSION = "0.7.0";