Moved `DefaultPubSubTopic` to `waku.ts` and fixed the casing

The pubsub topic is used by several protocol, not just relay.
This commit is contained in:
Franck Royer 2021-08-19 14:12:24 +10:00
parent d97613bb83
commit 60290e8d84
No known key found for this signature in database
GPG Key ID: A82ED75A8DFC50A4
11 changed files with 44 additions and 46 deletions

View File

@ -17,6 +17,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
refers to low latency network connections.
- Examples (eth-pm): Use sign typed data EIP-712 instead of personal sign.
- Upgraded dependencies to remove warning at installation.
- **Breaking**: Moved `DefaultPubSubTopic` to `waku.ts` and fixed the casing.
### Removed
- Examples (cli-chat): The focus of this library is Web environment;

View File

@ -5,7 +5,6 @@
"requires": true,
"packages": {
"": {
"name": "eth-pm",
"version": "0.1.0",
"dependencies": {
"@material-ui/core": "^4.11.4",

View File

@ -2,7 +2,7 @@ export { getBootstrapNodes } from './lib/discovery';
export * as utils from './lib/utils';
export { Waku } from './lib/waku';
export { Waku, DefaultPubSubTopic } from './lib/waku';
export { WakuMessage } from './lib/waku_message';

View File

@ -29,11 +29,16 @@ const websocketsTransportKey = Websockets.prototype[Symbol.toStringTag];
export const DefaultPingKeepAliveValueSecs = 0;
export const DefaultRelayKeepAliveValueSecs = 5 * 60;
/**
* DefaultPubSubTopic is the default gossipsub topic to use for Waku.
*/
export const DefaultPubSubTopic = '/waku/2/default-waku/proto';
const dbg = debug('waku:waku');
export interface CreateOptions {
/**
* The PubSub Topic to use. Defaults to {@link DefaultPubsubTopic}.
* The PubSub Topic to use. Defaults to {@link DefaultPubSubTopic}.
*
* One and only one pubsub topic is used by Waku. This is used by:
* - WakuRelay to receive, route and send messages,
@ -43,7 +48,7 @@ export interface CreateOptions {
* The usage of the default pubsub topic is recommended.
* See [Waku v2 Topic Usage Recommendations](https://rfc.vac.dev/spec/23/) for details.
*
* @default {@link DefaultPubsubTopic}
* @default {@link DefaultPubSubTopic}
*/
pubsubTopic?: string;
/**

View File

@ -7,8 +7,8 @@ import PeerId from 'peer-id';
import { PushResponse } from '../../proto/waku/v2/light_push';
import { getPeersForProtocol, selectRandomPeer } from '../select_peer';
import { DefaultPubSubTopic } from '../waku';
import { WakuMessage } from '../waku_message';
import { DefaultPubsubTopic } from '../waku_relay';
import { PushRPC } from './push_rpc';
@ -17,32 +17,32 @@ export { PushResponse };
export interface CreateOptions {
/**
* The PubSub Topic to use. Defaults to {@link DefaultPubsubTopic}.
* The PubSub Topic to use. Defaults to {@link DefaultPubSubTopic}.
*
* The usage of the default pubsub topic is recommended.
* See [Waku v2 Topic Usage Recommendations](https://rfc.vac.dev/spec/23/) for details.
*
* @default {@link DefaultPubsubTopic}
* @default {@link DefaultPubSubTopic}
*/
pubsubTopic?: string;
pubSubTopic?: string;
}
export interface PushOptions {
peerId?: PeerId;
pubsubTopic?: string;
pubSubTopic?: string;
}
/**
* Implements the [Waku v2 Light Push protocol](https://rfc.vac.dev/spec/19/).
*/
export class WakuLightPush {
pubsubTopic: string;
pubSubTopic: string;
constructor(public libp2p: Libp2p, options?: CreateOptions) {
if (options?.pubsubTopic) {
this.pubsubTopic = options.pubsubTopic;
if (options?.pubSubTopic) {
this.pubSubTopic = options.pubSubTopic;
} else {
this.pubsubTopic = DefaultPubsubTopic;
this.pubSubTopic = DefaultPubSubTopic;
}
}
@ -66,9 +66,9 @@ export class WakuLightPush {
const { stream } = await connection.newStream(LightPushCodec);
try {
const pubsubTopic = opts?.pubsubTopic
? opts.pubsubTopic
: this.pubsubTopic;
const pubsubTopic = opts?.pubSubTopic
? opts.pubSubTopic
: this.pubSubTopic;
const query = PushRPC.createRequest(message, pubsubTopic);
const res = await pipe(
[query.encode()],

View File

@ -2,15 +2,15 @@ import { Reader } from 'protobufjs/minimal';
import { v4 as uuid } from 'uuid';
import * as proto from '../../proto/waku/v2/light_push';
import { DefaultPubSubTopic } from '../waku';
import { WakuMessage } from '../waku_message';
import { DefaultPubsubTopic } from '../waku_relay';
export class PushRPC {
public constructor(public proto: proto.PushRPC) {}
static createRequest(
message: WakuMessage,
pubsubTopic: string = DefaultPubsubTopic
pubsubTopic: string = DefaultPubSubTopic
): PushRPC {
return new PushRPC({
requestId: uuid(),

View File

@ -9,11 +9,6 @@ export const RelayCodecs = [
'/vac/waku/relay/2.0.0',
];
/**
* DefaultPubsubTopic is the default gossipsub topic to use for Waku.
*/
export const DefaultPubsubTopic = '/waku/2/default-waku/proto';
export const RelayPingContentTopic = '/relay-ping/1/ping/null';
/**

View File

@ -11,11 +11,9 @@ import {
NOISE_KEY_2,
} from '../../test_utils';
import { delay } from '../delay';
import { Waku } from '../waku';
import { DefaultPubSubTopic, Waku } from '../waku';
import { WakuMessage } from '../waku_message';
import { DefaultPubsubTopic } from './index';
const log = debug('waku:test');
const TestContentTopic = '/test/1/waku-relay/utf8';
@ -63,9 +61,9 @@ describe('Waku Relay', () => {
it('Subscribe', async function () {
const subscribers1 =
waku1.libp2p.pubsub.getSubscribers(DefaultPubsubTopic);
waku1.libp2p.pubsub.getSubscribers(DefaultPubSubTopic);
const subscribers2 =
waku2.libp2p.pubsub.getSubscribers(DefaultPubsubTopic);
waku2.libp2p.pubsub.getSubscribers(DefaultPubSubTopic);
expect(subscribers1).to.contain(waku2.libp2p.peerId.toB58String());
expect(subscribers2).to.contain(waku1.libp2p.peerId.toB58String());
@ -282,7 +280,7 @@ describe('Waku Relay', () => {
it('nim subscribes to js', async function () {
const nimPeerId = await nimWaku.getPeerId();
const subscribers =
waku.libp2p.pubsub.getSubscribers(DefaultPubsubTopic);
waku.libp2p.pubsub.getSubscribers(DefaultPubSubTopic);
expect(subscribers).to.contain(nimPeerId.toB58String());
});
@ -371,7 +369,7 @@ describe('Waku Relay', () => {
while (subscribers.length === 0) {
await delay(200);
subscribers = waku.libp2p.pubsub.getSubscribers(DefaultPubsubTopic);
subscribers = waku.libp2p.pubsub.getSubscribers(DefaultPubSubTopic);
}
const nimPeerId = await nimWaku.getPeerId();

View File

@ -17,17 +17,17 @@ import { InMessage } from 'libp2p-interfaces/src/pubsub';
import { SignaturePolicy } from 'libp2p-interfaces/src/pubsub/signature-policy';
import PeerId from 'peer-id';
import { CreateOptions } from '../waku';
import { CreateOptions, DefaultPubSubTopic } from '../waku';
import { WakuMessage } from '../waku_message';
import * as constants from './constants';
import { DefaultPubsubTopic, RelayCodecs } from './constants';
import { RelayCodecs } from './constants';
import { getRelayPeers } from './get_relay_peers';
import { RelayHeartbeat } from './relay_heartbeat';
const dbg = debug('waku:relay');
export { RelayCodecs, DefaultPubsubTopic };
export { RelayCodecs };
/**
* See constructor libp2p-gossipsub [API](https://github.com/ChainSafe/js-libp2p-gossipsub#api).
@ -97,7 +97,7 @@ export class WakuRelay extends Gossipsub {
Object.assign(this, { multicodecs });
this.pubsubTopic = options?.pubsubTopic || constants.DefaultPubsubTopic;
this.pubsubTopic = options?.pubsubTopic || DefaultPubSubTopic;
}
/**
@ -250,7 +250,7 @@ export class WakuRelay extends Gossipsub {
*/
join(topic: string): void {
if (!this.started) {
throw new Error('WakuRelayPubsub has not started');
throw new Error('WakuRelayPubSub has not started');
}
const fanoutPeers = this.fanout.get(topic);

View File

@ -8,8 +8,8 @@ import PeerId from 'peer-id';
import { HistoryResponse_Error } from '../../proto/waku/v2/store';
import { getPeersForProtocol, selectRandomPeer } from '../select_peer';
import { DefaultPubSubTopic } from '../waku';
import { WakuMessage } from '../waku_message';
import { DefaultPubsubTopic } from '../waku_relay';
import { Direction, HistoryRPC } from './history_rpc';
@ -21,12 +21,12 @@ export { Direction };
export interface CreateOptions {
/**
* The PubSub Topic to use. Defaults to {@link DefaultPubsubTopic}.
* The PubSub Topic to use. Defaults to {@link DefaultPubSubTopic}.
*
* The usage of the default pubsub topic is recommended.
* See [Waku v2 Topic Usage Recommendations](https://rfc.vac.dev/spec/23/) for details.
*
* @default {@link DefaultPubsubTopic}
* @default {@link DefaultPubSubTopic}
*/
pubsubTopic?: string;
}
@ -50,7 +50,7 @@ export class WakuStore {
if (options?.pubsubTopic) {
this.pubsubTopic = options.pubsubTopic;
} else {
this.pubsubTopic = DefaultPubsubTopic;
this.pubsubTopic = DefaultPubSubTopic;
}
}

View File

@ -13,8 +13,8 @@ import { Multiaddr, multiaddr } from 'multiaddr';
import PeerId from 'peer-id';
import { hexToBuf } from '../lib/utils';
import { DefaultPubSubTopic } from '../lib/waku';
import { WakuMessage } from '../lib/waku_message';
import { DefaultPubsubTopic } from '../lib/waku_relay';
import * as proto from '../proto/waku/v2/message';
import { existsAsync, mkdirAsync, openAsync } from './async_fs';
@ -195,7 +195,7 @@ export class NimWaku {
};
return this.rpcCall<boolean>('post_waku_v2_relay_v1_message', [
pubsubTopic ? pubsubTopic : DefaultPubsubTopic,
pubsubTopic ? pubsubTopic : DefaultPubSubTopic,
rpcMessage,
]);
}
@ -209,7 +209,7 @@ export class NimWaku {
const protoMsgs = await this.rpcCall<proto.WakuMessage[]>(
'get_waku_v2_relay_v1_messages',
[DefaultPubsubTopic]
[DefaultPubSubTopic]
);
const msgs = await Promise.all(
@ -242,7 +242,7 @@ export class NimWaku {
}
return this.rpcCall<boolean>('post_waku_v2_private_v1_asymmetric_message', [
pubsubTopic ? pubsubTopic : DefaultPubsubTopic,
pubsubTopic ? pubsubTopic : DefaultPubSubTopic,
message,
'0x' + bufToHex(publicKey),
]);
@ -257,7 +257,7 @@ export class NimWaku {
return await this.rpcCall<WakuRelayMessage[]>(
'get_waku_v2_private_v1_asymmetric_messages',
[
pubsubTopic ? pubsubTopic : DefaultPubsubTopic,
pubsubTopic ? pubsubTopic : DefaultPubSubTopic,
'0x' + bufToHex(privateKey),
]
);
@ -284,7 +284,7 @@ export class NimWaku {
}
return this.rpcCall<boolean>('post_waku_v2_private_v1_symmetric_message', [
pubsubTopic ? pubsubTopic : DefaultPubsubTopic,
pubsubTopic ? pubsubTopic : DefaultPubSubTopic,
message,
'0x' + bufToHex(symKey),
]);
@ -298,7 +298,7 @@ export class NimWaku {
return await this.rpcCall<WakuRelayMessage[]>(
'get_waku_v2_private_v1_symmetric_messages',
[pubsubTopic ? pubsubTopic : DefaultPubsubTopic, '0x' + bufToHex(symKey)]
[pubsubTopic ? pubsubTopic : DefaultPubSubTopic, '0x' + bufToHex(symKey)]
);
}