From 433a490decf6b1203f6ec2a1e06e2df92077f185 Mon Sep 17 00:00:00 2001 From: Franck Royer Date: Thu, 1 Apr 2021 16:41:49 +1100 Subject: [PATCH] Move waku relay codec and default topic to constants module --- src/chat/index.ts | 4 ++-- src/lib/waku.spec.ts | 4 ++-- src/lib/waku.ts | 6 +++--- src/lib/waku_relay/constants.ts | 10 ++++++++++ src/lib/waku_relay/get_waku_peers.ts | 4 ++-- src/lib/waku_relay/index.spec.ts | 22 ++++++++++++++-------- src/lib/waku_relay/index.ts | 12 ++++-------- src/test_utils/nim_waku.ts | 8 +++++--- 8 files changed, 42 insertions(+), 28 deletions(-) diff --git a/src/chat/index.ts b/src/chat/index.ts index 9be295e75b..e5760b00b8 100644 --- a/src/chat/index.ts +++ b/src/chat/index.ts @@ -3,7 +3,7 @@ import util from 'util'; import Waku from '../lib/waku'; import { WakuMessage } from '../lib/waku_message'; -import { TOPIC } from '../lib/waku_relay'; +import { RelayDefaultTopic } from '../lib/waku_relay'; import { delay } from '../test_utils/delay'; import { ChatMessage } from './chat_message'; @@ -29,7 +29,7 @@ import { ChatMessage } from './chat_message'; // TODO: Bubble event to waku, infer topic, decode msg // Tracked with https://github.com/status-im/js-waku/issues/19 - waku.libp2p.pubsub.on(TOPIC, (event) => { + waku.libp2p.pubsub.on(RelayDefaultTopic, (event) => { const wakuMsg = WakuMessage.decode(event.data); if (wakuMsg.payload) { const chatMsg = ChatMessage.decode(wakuMsg.payload); diff --git a/src/lib/waku.spec.ts b/src/lib/waku.spec.ts index 690feccb86..6f906e0e80 100644 --- a/src/lib/waku.spec.ts +++ b/src/lib/waku.spec.ts @@ -5,7 +5,7 @@ import { makeLogFileName } from '../test_utils/log_file'; import { NimWaku } from '../test_utils/nim_waku'; import Waku from './waku'; -import { CODEC } from './waku_relay'; +import { RelayCodec } from './waku_relay'; describe('Waku', function () { describe('Interop: Nim', function () { @@ -28,7 +28,7 @@ describe('Waku', function () { expect(nimPeers).to.deep.equal([ { multiaddr: multiAddrWithId, - protocol: CODEC, + protocol: RelayCodec, connected: true, }, ]); diff --git a/src/lib/waku.ts b/src/lib/waku.ts index 129313047c..1a7c0e1bbc 100644 --- a/src/lib/waku.ts +++ b/src/lib/waku.ts @@ -6,7 +6,7 @@ import TCP from 'libp2p-tcp'; import Multiaddr from 'multiaddr'; import PeerId from 'peer-id'; -import { CODEC, WakuRelay, WakuRelayPubsub } from './waku_relay'; +import { RelayCodec, WakuRelay, WakuRelayPubsub } from './waku_relay'; export interface CreateOptions { listenAddresses: string[]; @@ -56,12 +56,12 @@ export default class Waku { * @param peer The peer to dial */ async dial(peer: PeerId | Multiaddr | string) { - return this.libp2p.dialProtocol(peer, CODEC); + return this.libp2p.dialProtocol(peer, RelayCodec); } async dialWithMultiAddr(peerId: PeerId, multiaddr: Multiaddr[]) { this.libp2p.peerStore.addressBook.set(peerId, multiaddr); - await this.libp2p.dialProtocol(peerId, CODEC); + await this.libp2p.dialProtocol(peerId, RelayCodec); } async stop() { diff --git a/src/lib/waku_relay/constants.ts b/src/lib/waku_relay/constants.ts index 515f2e4c4f..c63d45459e 100644 --- a/src/lib/waku_relay/constants.ts +++ b/src/lib/waku_relay/constants.ts @@ -1,6 +1,16 @@ export const second = 1000; export const minute = 60 * second; +/** + * RelayCodec is the libp2p identifier for the waku relay protocol + */ +export const RelayCodec = '/vac/waku/relay/2.0.0-beta2'; + +/** + * RelayDefaultTopic is the default gossipsub topic to use for waku relay + */ +export const RelayDefaultTopic = '/waku/2/default-waku/proto'; + /** * GossipsubHeartbeatInitialDelay is the short delay before the heartbeat timer begins * after the router is initialized. diff --git a/src/lib/waku_relay/get_waku_peers.ts b/src/lib/waku_relay/get_waku_peers.ts index 9dd788a955..cda6067db3 100644 --- a/src/lib/waku_relay/get_waku_peers.ts +++ b/src/lib/waku_relay/get_waku_peers.ts @@ -1,6 +1,6 @@ import { shuffle } from 'libp2p-gossipsub/src/utils'; -import { CODEC, WakuRelayPubsub } from './index'; +import { RelayCodec, WakuRelayPubsub } from './index'; /** * Given a topic, returns up to count peers subscribed to that topic @@ -32,7 +32,7 @@ export function getWakuPeers( if (!peerStreams) { return; } - if (peerStreams.protocol == CODEC && filter(id)) { + if (peerStreams.protocol == RelayCodec && filter(id)) { peers.push(id); } }); diff --git a/src/lib/waku_relay/index.spec.ts b/src/lib/waku_relay/index.spec.ts index 6daab699a4..6e5a964483 100644 --- a/src/lib/waku_relay/index.spec.ts +++ b/src/lib/waku_relay/index.spec.ts @@ -8,7 +8,7 @@ import { NimWaku } from '../../test_utils/nim_waku'; import Waku from '../waku'; import { WakuMessage } from '../waku_message'; -import { CODEC, TOPIC } from './index'; +import { RelayCodec, RelayDefaultTopic } from './index'; describe('Waku Relay', () => { afterEach(function () { @@ -61,8 +61,8 @@ describe('Waku Relay', () => { }); it('Subscribe', async function () { - const subscribers1 = waku1.libp2p.pubsub.getSubscribers(TOPIC); - const subscribers2 = waku2.libp2p.pubsub.getSubscribers(TOPIC); + const subscribers1 = waku1.libp2p.pubsub.getSubscribers(RelayDefaultTopic); + const subscribers2 = waku2.libp2p.pubsub.getSubscribers(RelayDefaultTopic); expect(subscribers1).to.contain(waku2.libp2p.peerId.toB58String()); expect(subscribers2).to.contain(waku1.libp2p.peerId.toB58String()); @@ -71,7 +71,7 @@ describe('Waku Relay', () => { it('Register correct protocols', async function () { const protocols = Array.from(waku1.libp2p.upgrader.protocols.keys()); - expect(protocols).to.contain(CODEC); + expect(protocols).to.contain(RelayCodec); expect(protocols.findIndex((value) => value.match(/sub/))).to.eq(-1); }); @@ -124,7 +124,9 @@ describe('Waku Relay', () => { it('nim subscribes to js', async function () { const nimPeerId = await nimWaku.getPeerId(); - const subscribers = waku.libp2p.pubsub.getSubscribers(TOPIC); + const subscribers = waku.libp2p.pubsub.getSubscribers( + RelayDefaultTopic + ); expect(subscribers).to.contain(nimPeerId.toB58String()); }); @@ -196,7 +198,9 @@ describe('Waku Relay', () => { }); it('nim subscribes to js', async function () { - const subscribers = waku.libp2p.pubsub.getSubscribers(TOPIC); + const subscribers = waku.libp2p.pubsub.getSubscribers( + RelayDefaultTopic + ); const nimPeerId = await nimWaku.getPeerId(); expect(subscribers).to.contain(nimPeerId.toB58String()); @@ -265,7 +269,9 @@ describe('Waku Relay', () => { }); it('nim subscribes to js', async function () { - const subscribers = waku.libp2p.pubsub.getSubscribers(TOPIC); + const subscribers = waku.libp2p.pubsub.getSubscribers( + RelayDefaultTopic + ); const nimPeerId = await nimWaku.getPeerId(); expect(subscribers).to.contain(nimPeerId.toB58String()); @@ -381,7 +387,7 @@ describe('Waku Relay', () => { function waitForNextData(pubsub: Pubsub): Promise { return new Promise((resolve) => { - pubsub.once(TOPIC, resolve); + pubsub.once(RelayDefaultTopic, resolve); }).then((msg: any) => { return WakuMessage.decode(msg.data); }); diff --git a/src/lib/waku_relay/index.ts b/src/lib/waku_relay/index.ts index cd507ed797..7b1789db11 100644 --- a/src/lib/waku_relay/index.ts +++ b/src/lib/waku_relay/index.ts @@ -6,17 +6,13 @@ import { SignaturePolicy } from 'libp2p-interfaces/src/pubsub/signature-policy'; import { WakuMessage } from '../waku_message'; +import { RelayCodec, RelayDefaultTopic } from './constants'; import { getWakuPeers } from './get_waku_peers'; import { RelayHeartbeat } from './relay_heartbeat'; export * from './constants'; export * from './relay_heartbeat'; -export const CODEC = '/vac/waku/relay/2.0.0-beta2'; - -// As per waku specs, the topic is fixed, value taken from nim-waku -export const TOPIC = '/waku/2/default-waku/proto'; - // This is the class to pass to libp2p as pubsub protocol export class WakuRelayPubsub extends Gossipsub { heartbeat: RelayHeartbeat; @@ -34,7 +30,7 @@ export class WakuRelayPubsub extends Gossipsub { this.heartbeat = new RelayHeartbeat(this); - const multicodecs = [CODEC]; + const multicodecs = [RelayCodec]; // This is the downside of using `libp2p-gossipsub` instead of // implementing WakuRelay from scratch. @@ -175,11 +171,11 @@ export class WakuRelay { // At this stage we are always using the same topic so we do not pass it as a parameter async subscribe() { - await this.pubsub.subscribe(TOPIC); + await this.pubsub.subscribe(RelayDefaultTopic); } async publish(message: WakuMessage) { const msg = message.toBinary(); - await this.pubsub.publish(TOPIC, msg); + await this.pubsub.publish(RelayDefaultTopic, msg); } } diff --git a/src/test_utils/nim_waku.ts b/src/test_utils/nim_waku.ts index 11bd4030fc..811c1eeb5a 100644 --- a/src/test_utils/nim_waku.ts +++ b/src/test_utils/nim_waku.ts @@ -8,7 +8,7 @@ import multiaddr from 'multiaddr'; import PeerId from 'peer-id'; import { WakuMessage } from '../lib/waku_message'; -import { TOPIC } from '../lib/waku_relay'; +import { RelayDefaultTopic } from '../lib/waku_relay'; import { existsAsync, mkdirAsync, openAsync } from './async_fs'; import waitForLine from './log_file'; @@ -145,7 +145,7 @@ export class NimWaku { }; const res = await this.rpcCall('post_waku_v2_relay_v1_message', [ - TOPIC, + RelayDefaultTopic, rpcMessage, ]); @@ -155,7 +155,9 @@ export class NimWaku { async messages() { this.checkProcess(); - const res = await this.rpcCall('get_waku_v2_relay_v1_messages', [TOPIC]); + const res = await this.rpcCall('get_waku_v2_relay_v1_messages', [ + RelayDefaultTopic, + ]); return res.result; }