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: enable auto start upon node creation #2291

Merged
merged 2 commits into from
Feb 27, 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
8 changes: 8 additions & 0 deletions packages/interfaces/src/protocols.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ export type CreateNodeOptions = {
*/
userAgent?: string;

/**
* Starts Waku node automatically upon creations.
* Calls {@link @waku/sdk!WakuNode.start} before returning {@link @waku/sdk!WakuNode}
*
* @default true
*/
autoStart?: boolean;

/**
* Configuration for determining the network in use.
* Network configuration refers to the shards and clusters used in the network.
Expand Down
9 changes: 8 additions & 1 deletion packages/sdk/src/create/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,16 @@ export async function createLightNode(
): Promise<LightNode> {
const { libp2p, pubsubTopics } = await createLibp2pAndUpdateOptions(options);

return new WakuNode(pubsubTopics, options, libp2p, {
const node = new WakuNode(pubsubTopics, options, libp2p, {
store: true,
lightpush: true,
filter: true
}) as LightNode;

// only if `false` is passed explicitly
if (options?.autoStart !== false) {
await node.start();
}

return node;
}
20 changes: 19 additions & 1 deletion packages/sdk/src/waku/waku.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ export class WakuNode implements IWaku {
public connectionManager: ConnectionManager;
public health: HealthIndicator;

// needed to create a lock for async operations
private _nodeStateLock = false;
private _nodeStarted = false;

private readonly peerManager: PeerManager;

public constructor(
Expand Down Expand Up @@ -190,18 +194,32 @@ export class WakuNode implements IWaku {
}

public async start(): Promise<void> {
if (this._nodeStateLock || this.isStarted()) return;

this._nodeStateLock = true;

await this.libp2p.start();
this.peerManager.start();
this.health.start();
this.lightPush?.start();

this._nodeStateLock = false;
this._nodeStarted = true;
}

public async stop(): Promise<void> {
if (this._nodeStateLock || !this.isStarted()) return;

this._nodeStateLock = true;

this.lightPush?.stop();
this.health.stop();
this.peerManager.stop();
this.connectionManager.stop();
await this.libp2p.stop();

this._nodeStateLock = false;
this._nodeStarted = false;
}

public async getConnectedPeers(): Promise<Peer[]> {
Expand All @@ -216,7 +234,7 @@ export class WakuNode implements IWaku {
}

public isStarted(): boolean {
return this.libp2p.status == "started";
return this._nodeStarted && this.libp2p.status === "started";
}

public isConnected(): boolean {
Expand Down
Loading