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: replace waitForRemotePeers() with waku.waitForPeer() method #2161

Merged
merged 30 commits into from
Oct 8, 2024
Merged
Show file tree
Hide file tree
Changes from 25 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
4dbb1b6
fix comment of default number of peers
weboko Oct 1, 2024
315d82f
export default number of peers from base protocol sdk
weboko Oct 1, 2024
23ded77
rename to light_push, move class to separate file
weboko Oct 1, 2024
240b1b6
move waitForRemotePeer to sdk package
weboko Oct 1, 2024
ad56eae
add todo to move waitForGossipSubPeerInMesh into @waku/relay
weboko Oct 1, 2024
7580eeb
clean up waitForRemotePeer, split metadata await from event and optim…
weboko Oct 1, 2024
800eae2
simplify and rename ILightPush interface
weboko Oct 1, 2024
6bcf283
use only connected peers in light push based on connections instead o…
weboko Oct 2, 2024
9742d68
improve readability of result processing in light push
weboko Oct 2, 2024
0f09905
fix check & update tests
weboko Oct 2, 2024
3dda8ad
address tests, add new test cases, fix racing condition in StreamManager
weboko Oct 3, 2024
06498f8
use libp2p.getPeers
weboko Oct 3, 2024
eaee635
feat: confirm metadata and protocols needed in waitForRemotePeer
weboko Oct 3, 2024
3928603
merge with master
weboko Oct 4, 2024
21056fd
rely on passed protocols and fallback to mounted
weboko Oct 4, 2024
ac212c1
add I prefix to Waku interface
weboko Oct 4, 2024
4b94686
implement waku.connect method
weboko Oct 4, 2024
690e630
add docs to IWaku interface
weboko Oct 4, 2024
ba78ed2
remove export and usage of waitForRemotePeer
weboko Oct 4, 2024
9267c70
move wait for remote peer related to Realy out of @waku/sdk
weboko Oct 4, 2024
440f908
change tests to use new API
weboko Oct 4, 2024
4f194ab
fix linting
weboko Oct 4, 2024
cd6acee
update size limit
weboko Oct 4, 2024
64b8f3a
rename .connect to .waitForPeer
weboko Oct 4, 2024
d1e2ba1
export waitForRemotePeer and mark as deprecated
weboko Oct 4, 2024
0c2e7e9
merge with master
weboko Oct 5, 2024
8937a56
feat: add mocha tests to @waku/sdk and cover waitForRemotePeer (#2163)
weboko Oct 8, 2024
027a1d1
feat: expose peerId and protocols from WakuNode (#2166)
weboko Oct 8, 2024
448e659
rename to waitForPeers
weboko Oct 8, 2024
ec8ccb4
up test
weboko Oct 8, 2024
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
2 changes: 1 addition & 1 deletion .size-limit.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ module.exports = [
path: ["packages/sdk/bundle/index.js", "packages/core/bundle/index.js"],
import: {
"packages/sdk/bundle/index.js":
"{ createLightNode, waitForRemotePeer, createEncoder, createDecoder, bytesToUtf8, utf8ToBytes, Decoder, Encoder, DecodedMessage, WakuNode }",
"{ createLightNode, createEncoder, createDecoder, bytesToUtf8, utf8ToBytes, Decoder, Encoder, DecodedMessage, WakuNode }",
},
},
{
Expand Down
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/interfaces/src/relay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export interface IRelayAPI {
readonly pubsubTopics: Set<PubsubTopic>;
readonly gossipSub: GossipSub;
start: () => Promise<void>;
waitForPeer: () => Promise<void>;
weboko marked this conversation as resolved.
Show resolved Hide resolved
getMeshPeers: (topic?: TopicStr) => PeerIdStr[];
}

Expand Down
81 changes: 76 additions & 5 deletions packages/interfaces/src/waku.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,36 +10,107 @@ import { Protocols } from "./protocols.js";
import type { IRelay } from "./relay.js";
import type { IStore } from "./store.js";

export interface Waku {
export interface IWaku {
libp2p: Libp2p;
relay?: IRelay;
store?: IStore;
filter?: IFilter;
lightPush?: ILightPush;

health: IHealthManager;
connectionManager: IConnectionManager;

/**
* Dials to the provided peer
*
* @param {PeerId | MultiaddrInput} peer information to use for dialing
* @param {Protocols[]} [protocols] array of Waku protocols to be used for dialing. If no provided - will be derived from mounted protocols.
*
* @returns {Promise<Stream>} `Promise` that will resolve to a `Stream` to a dialed peer
*
* @example
* ```typescript
* await waku.dial(remotePeerId, [Protocols.LightPush]);
*
* waku.isConnected() === true;
* ```
*/
dial(peer: PeerId | MultiaddrInput, protocols?: Protocols[]): Promise<Stream>;

/**
* Starts all services and components related to functionality of Waku node.
*
* @returns {Promise<boolean>} `Promise` that will resolve when started.
*
* @example
* ```typescript
* await waku.start();
*
* waku.isStarted() === true;
* ```
*/
start(): Promise<void>;

/**
* Stops all recurring processes and services that are needed for functionality of Waku node.
*
* @returns {Promise<boolean>} `Promise` that resolves when stopped.
*
* @example
* ```typescript
* await waku.stop();
*
* waku.isStarted === false;
* ```
*/
stop(): Promise<void>;

/**
* Resolves when Waku successfully gains connection to a remote peers that fits provided requirements.
weboko marked this conversation as resolved.
Show resolved Hide resolved
* Must be used after attempting to connect to nodes, using {@link IWaku.dial} or
* if was bootstrapped by using {@link IPeerExchange} or {@link DnsDiscoveryComponents}.
*
* @param {Protocols[]} [protocols] Protocols that need to be enabled by remote peers
* @param {number} [timeoutMs] Timeout value in milliseconds after which promise rejects
*
* @returns {Promise<void>} `Promise` that **resolves** if all desired protocols are fulfilled by
* at least one remote peer, **rejects** if the timeoutMs is reached
* @throws If passing a protocol that is not mounted or Waku node is not started
*
* @example
* ```typescript
* try {
* // let's wait for at least one LightPush node and timeout in 1 second
* await waku.waitForPeer([Protocols.LightPush], 1000);
* } catch(e) {
* waku.isConnected() === false;
* console.error("Failed to connect due to", e);
* }
*
* waku.isConnected() === true;
* ```
*/
waitForPeer(protocols?: Protocols[], timeoutMs?: number): Promise<void>;

/**
* @returns {boolean} `true` if the node was started and `false` otherwise
*/
isStarted(): boolean;

/**
* @returns {boolean} `true` if the node has working connection and `false` otherwise
*/
isConnected(): boolean;

health: IHealthManager;
}

export interface LightNode extends Waku {
export interface LightNode extends IWaku {
relay: undefined;
store: IStore;
filter: IFilter;
lightPush: ILightPush;
}

export interface RelayNode extends Waku {
export interface RelayNode extends IWaku {
relay: IRelay;
store: undefined;
filter: undefined;
Expand Down
3 changes: 2 additions & 1 deletion packages/relay/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@
"@waku/utils": "0.0.20",
"chai": "^4.3.10",
"debug": "^4.3.4",
"fast-check": "^3.19.0"
"fast-check": "^3.19.0",
"p-event": "^6.0.1"
},
"devDependencies": {
"@rollup/plugin-commonjs": "^25.0.7",
Expand Down
17 changes: 17 additions & 0 deletions packages/relay/src/relay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
import { isWireSizeUnderCap, toAsyncIterator } from "@waku/utils";
import { pushOrInitMapSet } from "@waku/utils";
import { Logger } from "@waku/utils";
import { pEvent } from "p-event";

import { RelayCodecs } from "./constants.js";
import { messageValidator } from "./message_validator.js";
Expand Down Expand Up @@ -94,6 +95,22 @@ class Relay implements IRelay {
this.subscribeToAllTopics();
}

/**
* Wait for at least one peer with the given protocol to be connected and in the gossipsub
* mesh for all pubsubTopics.
*/
public async waitForPeer(): Promise<void> {
let peers = this.getMeshPeers();
const pubsubTopics = this.pubsubTopics;

for (const topic of pubsubTopics) {
while (peers.length == 0) {
await pEvent(this.gossipSub, "gossipsub:heartbeat");
peers = this.getMeshPeers(topic);
}
}
}

/**
* Send Waku message.
*/
Expand Down
3 changes: 1 addition & 2 deletions packages/sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,7 @@
"@waku/proto": "^0.0.8",
"@waku/utils": "0.0.20",
"@waku/message-hash": "0.1.16",
"libp2p": "^1.8.1",
"p-event": "^6.0.1"
"libp2p": "^1.8.1"
},
"devDependencies": {
"@rollup/plugin-commonjs": "^25.0.7",
Expand Down
2 changes: 1 addition & 1 deletion packages/sdk/src/create/create.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { type LightNode } from "@waku/interfaces";

import { CreateWakuNodeOptions, WakuNode } from "../waku.js";
import { CreateWakuNodeOptions, WakuNode } from "../waku/index.js";

import { createLibp2pAndUpdateOptions } from "./libp2p.js";

Expand Down
2 changes: 1 addition & 1 deletion packages/sdk/src/create/libp2p.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import {
CreateWakuNodeOptions,
DefaultPingMaxInboundStreams,
DefaultUserAgent
} from "../waku.js";
} from "../waku/index.js";

import { defaultPeerDiscoveries } from "./discovery.js";

Expand Down
4 changes: 1 addition & 3 deletions packages/sdk/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export {

export { utf8ToBytes, bytesToUtf8 } from "@waku/utils/bytes";

export * from "./waku.js";
export * from "./waku/index.js";

export {
createLightNode,
Expand All @@ -18,8 +18,6 @@ export { wakuLightPush } from "./protocols/light_push/index.js";
export { wakuFilter } from "./protocols/filter/index.js";
export { wakuStore } from "./protocols/store/index.js";

export { waitForRemotePeer } from "./wait_for_remote_peer.js";

export * as waku from "@waku/core";
export * as utils from "@waku/utils";
export * from "@waku/interfaces";
2 changes: 2 additions & 0 deletions packages/sdk/src/waku/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from "./waku.js";
export { waitForRemotePeer } from "./wait_for_remote_peer.js";
Loading