mirror of https://github.com/waku-org/js-waku.git
Merge pull request #1134 from waku-org/chore/remove-peer-exchange-dep
This commit is contained in:
commit
f2f3cdc2a7
|
@ -26317,7 +26317,6 @@
|
|||
"@waku/byte-utils": "*",
|
||||
"@waku/interfaces": "*",
|
||||
"@waku/libp2p-utils": "*",
|
||||
"@waku/peer-exchange": "*",
|
||||
"@waku/proto": "*",
|
||||
"debug": "^4.3.4",
|
||||
"it-all": "^1.0.6",
|
||||
|
@ -29834,7 +29833,6 @@
|
|||
"@waku/byte-utils": "*",
|
||||
"@waku/interfaces": "*",
|
||||
"@waku/libp2p-utils": "*",
|
||||
"@waku/peer-exchange": "*",
|
||||
"@waku/proto": "*",
|
||||
"app-root-path": "^3.0.0",
|
||||
"chai": "^4.3.4",
|
||||
|
|
|
@ -7,6 +7,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|||
|
||||
## [Unreleased]
|
||||
|
||||
### Added
|
||||
|
||||
- `multicodec` property from protocol interfaces.
|
||||
|
||||
### Removed
|
||||
|
||||
- Dependency on `@waku/peer-exchange`.
|
||||
|
||||
## [@waku/core@0.0.10] - 2023-01-25
|
||||
|
||||
### Changed
|
||||
|
|
|
@ -91,7 +91,6 @@
|
|||
"@waku/byte-utils": "*",
|
||||
"@waku/interfaces": "*",
|
||||
"@waku/libp2p-utils": "*",
|
||||
"@waku/peer-exchange": "*",
|
||||
"@waku/proto": "*",
|
||||
"debug": "^4.3.4",
|
||||
"it-all": "^1.0.6",
|
||||
|
|
|
@ -65,6 +65,7 @@ export type UnsubscribeFunction = () => Promise<void>;
|
|||
* - https://github.com/status-im/nwaku/issues/948
|
||||
*/
|
||||
class Filter implements IFilter {
|
||||
multicodec: string;
|
||||
pubSubTopic: string;
|
||||
private subscriptions: Map<string, Callback<any>>;
|
||||
private decoders: Map<
|
||||
|
@ -73,6 +74,7 @@ class Filter implements IFilter {
|
|||
>;
|
||||
|
||||
constructor(public components: FilterComponents, options?: CreateOptions) {
|
||||
this.multicodec = FilterCodec;
|
||||
this.subscriptions = new Map();
|
||||
this.decoders = new Map();
|
||||
this.pubSubTopic = options?.pubSubTopic ?? DefaultPubSubTopic;
|
||||
|
|
|
@ -52,9 +52,11 @@ export interface CreateOptions {
|
|||
* Implements the [Waku v2 Light Push protocol](https://rfc.vac.dev/spec/19/).
|
||||
*/
|
||||
class LightPush implements ILightPush {
|
||||
multicodec: string;
|
||||
pubSubTopic: string;
|
||||
|
||||
constructor(public components: LightPushComponents, options?: CreateOptions) {
|
||||
this.multicodec = LightPushCodec;
|
||||
this.pubSubTopic = options?.pubSubTopic ?? DefaultPubSubTopic;
|
||||
}
|
||||
|
||||
|
|
|
@ -105,9 +105,11 @@ export interface QueryOptions {
|
|||
* The Waku Store protocol can be used to retrieved historical messages.
|
||||
*/
|
||||
class Store implements IStore {
|
||||
multicodec: string;
|
||||
pubSubTopic: string;
|
||||
|
||||
constructor(public components: StoreComponents, options?: CreateOptions) {
|
||||
this.multicodec = StoreCodec;
|
||||
this.pubSubTopic = options?.pubSubTopic ?? DefaultPubSubTopic;
|
||||
}
|
||||
|
||||
|
|
|
@ -1,14 +1,9 @@
|
|||
import { PeerProtocolsChangeData } from "@libp2p/interface-peer-store";
|
||||
import type { IRelay, PointToPointProtocol, Waku } from "@waku/interfaces";
|
||||
import { Protocols } from "@waku/interfaces";
|
||||
import { PeerExchangeCodec } from "@waku/peer-exchange";
|
||||
import debug from "debug";
|
||||
import { pEvent } from "p-event";
|
||||
|
||||
import { FilterCodec } from "./filter/index.js";
|
||||
import { LightPushCodec } from "./light_push/index.js";
|
||||
import { StoreCodec } from "./store/index.js";
|
||||
|
||||
const log = debug("waku:wait-for-remote-peer");
|
||||
|
||||
/**
|
||||
|
@ -50,19 +45,19 @@ export async function waitForRemotePeer(
|
|||
if (protocols.includes(Protocols.Store)) {
|
||||
if (!waku.store)
|
||||
throw new Error("Cannot wait for Store peer: protocol not mounted");
|
||||
promises.push(waitForConnectedPeer(waku.store, [StoreCodec]));
|
||||
promises.push(waitForConnectedPeer(waku.store));
|
||||
}
|
||||
|
||||
if (protocols.includes(Protocols.LightPush)) {
|
||||
if (!waku.lightPush)
|
||||
throw new Error("Cannot wait for LightPush peer: protocol not mounted");
|
||||
promises.push(waitForConnectedPeer(waku.lightPush, [LightPushCodec]));
|
||||
promises.push(waitForConnectedPeer(waku.lightPush));
|
||||
}
|
||||
|
||||
if (protocols.includes(Protocols.Filter)) {
|
||||
if (!waku.filter)
|
||||
throw new Error("Cannot wait for Filter peer: protocol not mounted");
|
||||
promises.push(waitForConnectedPeer(waku.filter, [FilterCodec]));
|
||||
promises.push(waitForConnectedPeer(waku.filter));
|
||||
}
|
||||
|
||||
if (protocols.includes(Protocols.PeerExchange)) {
|
||||
|
@ -70,7 +65,7 @@ export async function waitForRemotePeer(
|
|||
throw new Error(
|
||||
"Cannot wait for Peer Exchange peer: protocol not mounted"
|
||||
);
|
||||
promises.push(waitForConnectedPeer(waku.peerExchange, [PeerExchangeCodec]));
|
||||
promises.push(waitForConnectedPeer(waku.peerExchange));
|
||||
}
|
||||
|
||||
if (timeoutMs) {
|
||||
|
@ -88,28 +83,25 @@ export async function waitForRemotePeer(
|
|||
* Wait for a peer with the given protocol to be connected.
|
||||
*/
|
||||
async function waitForConnectedPeer(
|
||||
waku: PointToPointProtocol,
|
||||
codecs: string[]
|
||||
protocol: PointToPointProtocol
|
||||
): Promise<void> {
|
||||
const peers = await waku.peers();
|
||||
const codec = protocol.multicodec;
|
||||
const peers = await protocol.peers();
|
||||
|
||||
if (peers.length) {
|
||||
log(`${codecs} peer found: `, peers[0].id.toString());
|
||||
log(`${codec} peer found: `, peers[0].id.toString());
|
||||
return;
|
||||
}
|
||||
|
||||
await new Promise<void>((resolve) => {
|
||||
const cb = (evt: CustomEvent<PeerProtocolsChangeData>): void => {
|
||||
for (const codec of codecs) {
|
||||
if (evt.detail.protocols.includes(codec)) {
|
||||
log("Resolving for", codec, evt.detail.protocols);
|
||||
waku.peerStore.removeEventListener("change:protocols", cb);
|
||||
resolve();
|
||||
break;
|
||||
}
|
||||
if (evt.detail.protocols.includes(codec)) {
|
||||
log("Resolving for", codec, evt.detail.protocols);
|
||||
protocol.peerStore.removeEventListener("change:protocols", cb);
|
||||
resolve();
|
||||
}
|
||||
};
|
||||
waku.peerStore.addEventListener("change:protocols", cb);
|
||||
protocol.peerStore.addEventListener("change:protocols", cb);
|
||||
});
|
||||
}
|
||||
|
||||
|
|
|
@ -12,16 +12,15 @@ import type {
|
|||
Waku,
|
||||
} from "@waku/interfaces";
|
||||
import { Protocols } from "@waku/interfaces";
|
||||
import { PeerExchangeCodec } from "@waku/peer-exchange";
|
||||
import debug from "debug";
|
||||
import type { Libp2p } from "libp2p";
|
||||
|
||||
import { FilterCodec, FilterComponents } from "./filter/index.js";
|
||||
import { LightPushCodec, LightPushComponents } from "./light_push/index.js";
|
||||
import { FilterComponents } from "./filter/index.js";
|
||||
import { LightPushComponents } from "./light_push/index.js";
|
||||
import { createEncoder } from "./message/version_0.js";
|
||||
import * as relayConstants from "./relay/constants.js";
|
||||
import { RelayCodecs, RelayPingContentTopic } from "./relay/constants.js";
|
||||
import { StoreCodec, StoreComponents } from "./store/index.js";
|
||||
import { RelayPingContentTopic } from "./relay/constants.js";
|
||||
import { StoreComponents } from "./store/index.js";
|
||||
|
||||
export const DefaultPingKeepAliveValueSecs = 0;
|
||||
export const DefaultRelayKeepAliveValueSecs = 5 * 60;
|
||||
|
@ -165,20 +164,50 @@ export class WakuNode implements Waku {
|
|||
|
||||
const codecs: string[] = [];
|
||||
if (_protocols.includes(Protocols.Relay)) {
|
||||
RelayCodecs.forEach((codec) => codecs.push(codec));
|
||||
if (this.relay) {
|
||||
this.relay.multicodecs.forEach((codec) => codecs.push(codec));
|
||||
} else {
|
||||
log(
|
||||
"Relay codec not included in dial codec: protocol not mounted locally"
|
||||
);
|
||||
}
|
||||
}
|
||||
if (_protocols.includes(Protocols.Store)) {
|
||||
codecs.push(StoreCodec);
|
||||
if (this.store) {
|
||||
codecs.push(this.store.multicodec);
|
||||
} else {
|
||||
log(
|
||||
"Store codec not included in dial codec: protocol not mounted locally"
|
||||
);
|
||||
}
|
||||
}
|
||||
if (_protocols.includes(Protocols.LightPush)) {
|
||||
codecs.push(LightPushCodec);
|
||||
if (this.lightPush) {
|
||||
codecs.push(this.lightPush.multicodec);
|
||||
} else {
|
||||
log(
|
||||
"Light Push codec not included in dial codec: protocol not mounted locally"
|
||||
);
|
||||
}
|
||||
}
|
||||
if (_protocols.includes(Protocols.Filter)) {
|
||||
codecs.push(FilterCodec);
|
||||
if (this.filter) {
|
||||
codecs.push(this.filter.multicodec);
|
||||
} else {
|
||||
log(
|
||||
"Filter codec not included in dial codec: protocol not mounted locally"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
if (_protocols.includes(Protocols.PeerExchange)) {
|
||||
codecs.push(PeerExchangeCodec);
|
||||
if (this.peerExchange) {
|
||||
codecs.push(this.peerExchange.multicodec);
|
||||
} else {
|
||||
log(
|
||||
"Peer Exchange codec not included in dial codec: protocol not mounted locally"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
log(`Dialing to ${peer.toString()} with protocols ${_protocols}`);
|
||||
|
|
|
@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|||
|
||||
## [Unreleased]
|
||||
|
||||
### Added
|
||||
|
||||
- `multicodec` property on protocol interfaces.
|
||||
|
||||
## [0.0.7] - 2023-01-18
|
||||
|
||||
### Added
|
||||
|
|
|
@ -12,6 +12,7 @@ export enum Protocols {
|
|||
}
|
||||
|
||||
export interface PointToPointProtocol {
|
||||
multicodec: string;
|
||||
peerStore: PeerStore;
|
||||
peers: () => Promise<Peer[]>;
|
||||
}
|
||||
|
|
|
@ -30,6 +30,7 @@ const log = debug("waku:peer-exchange");
|
|||
* Implementation of the Peer Exchange protocol (https://rfc.vac.dev/spec/34/)
|
||||
*/
|
||||
export class WakuPeerExchange implements IPeerExchange {
|
||||
multicodec: string;
|
||||
private callback:
|
||||
| ((response: PeerExchangeResponse) => Promise<void>)
|
||||
| undefined;
|
||||
|
@ -42,6 +43,7 @@ export class WakuPeerExchange implements IPeerExchange {
|
|||
public components: PeerExchangeComponents,
|
||||
public createOptions?: ProtocolOptions
|
||||
) {
|
||||
this.multicodec = PeerExchangeCodec;
|
||||
this.components.registrar
|
||||
.handle(PeerExchangeCodec, this.handler.bind(this))
|
||||
.catch((e) => log("Failed to register peer exchange protocol", e));
|
||||
|
|
Loading…
Reference in New Issue