Move waku relay codec and default topic to constants module

This commit is contained in:
Franck Royer 2021-04-01 16:41:49 +11:00
parent 7bd48b6220
commit 433a490dec
No known key found for this signature in database
GPG Key ID: A82ED75A8DFC50A4
8 changed files with 42 additions and 28 deletions

View File

@ -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);

View File

@ -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,
},
]);

View File

@ -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() {

View File

@ -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.

View File

@ -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);
}
});

View File

@ -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<WakuMessage> {
return new Promise((resolve) => {
pubsub.once(TOPIC, resolve);
pubsub.once(RelayDefaultTopic, resolve);
}).then((msg: any) => {
return WakuMessage.decode(msg.data);
});

View File

@ -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);
}
}

View File

@ -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;
}