From eb9955314c1d1fa6c719aa8051d4e96af39d796b Mon Sep 17 00:00:00 2001 From: Franck Royer Date: Thu, 13 May 2021 16:29:44 +1000 Subject: [PATCH 1/3] Align terminology with specs In specs, this is referenced as a `pubsubTopic`. --- CHANGELOG.md | 1 + src/lib/waku_relay/constants.ts | 2 +- src/lib/waku_relay/index.spec.ts | 10 +++++----- src/lib/waku_relay/index.ts | 10 +++++----- src/lib/waku_store/history_rpc.ts | 4 ++-- src/test_utils/nim_waku.ts | 6 +++--- 6 files changed, 17 insertions(+), 16 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e993913632..ea1b4986f4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Enable passing `string`s to `addPeerToAddressBook`. - Use `addPeerToAddressBook` in examples and usage doc. - Settle on `js-waku` name across the board. +- **Breaking**: `RelayDefaultTopic` renamed to `DefaultPubsubTopic`. ## [0.1.0] - 2021-05-12 diff --git a/src/lib/waku_relay/constants.ts b/src/lib/waku_relay/constants.ts index 9cfee5660c..3733f9b9c7 100644 --- a/src/lib/waku_relay/constants.ts +++ b/src/lib/waku_relay/constants.ts @@ -9,7 +9,7 @@ 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'; +export const DefaultPubsubTopic = '/waku/2/default-waku/proto'; /** * RelayGossipFactor affects how many peers we will emit gossip to at each heartbeat. diff --git a/src/lib/waku_relay/index.spec.ts b/src/lib/waku_relay/index.spec.ts index cf3265dfb9..843d6a289a 100644 --- a/src/lib/waku_relay/index.spec.ts +++ b/src/lib/waku_relay/index.spec.ts @@ -12,7 +12,7 @@ import { delay } from '../delay'; import { Waku } from '../waku'; import { WakuMessage } from '../waku_message'; -import { RelayCodec, RelayDefaultTopic } from './index'; +import { DefaultPubsubTopic, RelayCodec } from './index'; const log = debug('waku:test'); @@ -57,8 +57,8 @@ describe('Waku Relay', () => { }); it('Subscribe', async function () { - const subscribers1 = waku1.libp2p.pubsub.getSubscribers(RelayDefaultTopic); - const subscribers2 = waku2.libp2p.pubsub.getSubscribers(RelayDefaultTopic); + const subscribers1 = waku1.libp2p.pubsub.getSubscribers(DefaultPubsubTopic); + const subscribers2 = waku2.libp2p.pubsub.getSubscribers(DefaultPubsubTopic); expect(subscribers1).to.contain(waku2.libp2p.peerId.toB58String()); expect(subscribers2).to.contain(waku1.libp2p.peerId.toB58String()); @@ -160,7 +160,7 @@ describe('Waku Relay', () => { it('nim subscribes to js', async function () { const nimPeerId = await nimWaku.getPeerId(); const subscribers = waku.libp2p.pubsub.getSubscribers( - RelayDefaultTopic + DefaultPubsubTopic ); expect(subscribers).to.contain(nimPeerId.toB58String()); @@ -244,7 +244,7 @@ describe('Waku Relay', () => { while (subscribers.length === 0) { await delay(200); - subscribers = waku.libp2p.pubsub.getSubscribers(RelayDefaultTopic); + subscribers = waku.libp2p.pubsub.getSubscribers(DefaultPubsubTopic); } const nimPeerId = await nimWaku.getPeerId(); diff --git a/src/lib/waku_relay/index.ts b/src/lib/waku_relay/index.ts index 2b21a42b3f..8ce2430e92 100644 --- a/src/lib/waku_relay/index.ts +++ b/src/lib/waku_relay/index.ts @@ -19,11 +19,11 @@ import PeerId from 'peer-id'; import { WakuMessage } from '../waku_message'; import * as constants from './constants'; -import { RelayCodec, RelayDefaultTopic } from './constants'; +import { DefaultPubsubTopic, RelayCodec } from './constants'; import { getRelayPeers } from './get_relay_peers'; import { RelayHeartbeat } from './relay_heartbeat'; -export { RelayCodec, RelayDefaultTopic }; +export { RelayCodec, DefaultPubsubTopic }; /** * See {GossipOptions} from libp2p-gossipsub @@ -94,7 +94,7 @@ export class WakuRelay extends Gossipsub implements Pubsub { * @returns {void} */ public start(): void { - this.on(constants.RelayDefaultTopic, (event) => { + this.on(constants.DefaultPubsubTopic, (event) => { const wakuMsg = WakuMessage.decode(event.data); if (this.observers['']) { this.observers[''].forEach((callbackFn) => { @@ -111,7 +111,7 @@ export class WakuRelay extends Gossipsub implements Pubsub { }); super.start(); - super.subscribe(constants.RelayDefaultTopic); + super.subscribe(constants.DefaultPubsubTopic); } /** @@ -122,7 +122,7 @@ export class WakuRelay extends Gossipsub implements Pubsub { */ public async send(message: WakuMessage): Promise { const msg = message.encode(); - await super.publish(constants.RelayDefaultTopic, Buffer.from(msg)); + await super.publish(constants.DefaultPubsubTopic, Buffer.from(msg)); } /** diff --git a/src/lib/waku_store/history_rpc.ts b/src/lib/waku_store/history_rpc.ts index 3e3fdb537a..41d057ce8b 100644 --- a/src/lib/waku_store/history_rpc.ts +++ b/src/lib/waku_store/history_rpc.ts @@ -3,7 +3,7 @@ import { v4 as uuid } from 'uuid'; import * as proto from '../../proto/waku/v2/store'; import { DefaultContentTopic } from '../waku_message'; -import { RelayDefaultTopic } from '../waku_relay'; +import { DefaultPubsubTopic } from '../waku_relay'; export class HistoryRPC { public constructor(public proto: proto.HistoryRPC) {} @@ -11,7 +11,7 @@ export class HistoryRPC { static createQuery( contentTopics: string[] = [DefaultContentTopic], cursor?: proto.Index, - pubsubTopic: string = RelayDefaultTopic + pubsubTopic: string = DefaultPubsubTopic ): HistoryRPC { const pagingInfo = { pageSize: 10, diff --git a/src/test_utils/nim_waku.ts b/src/test_utils/nim_waku.ts index bb9040499a..b20d3d5cb6 100644 --- a/src/test_utils/nim_waku.ts +++ b/src/test_utils/nim_waku.ts @@ -13,7 +13,7 @@ import { Multiaddr, multiaddr } from 'multiaddr'; import PeerId from 'peer-id'; import { WakuMessage } from '../lib/waku_message'; -import { RelayDefaultTopic } from '../lib/waku_relay'; +import { DefaultPubsubTopic } from '../lib/waku_relay'; import * as proto from '../proto/waku/v2/message'; import { existsAsync, mkdirAsync, openAsync } from './async_fs'; @@ -167,7 +167,7 @@ export class NimWaku { }; return this.rpcCall('post_waku_v2_relay_v1_message', [ - RelayDefaultTopic, + DefaultPubsubTopic, rpcMessage, ]); } @@ -176,7 +176,7 @@ export class NimWaku { this.checkProcess(); return this.rpcCall('get_waku_v2_relay_v1_messages', [ - RelayDefaultTopic, + DefaultPubsubTopic, ]).then((msgs) => msgs.map((protoMsg) => new WakuMessage(protoMsg))); } From a8d4d494f80995995187997b3917c7530b325ce5 Mon Sep 17 00:00:00 2001 From: Franck Royer Date: Thu, 13 May 2021 16:42:18 +1000 Subject: [PATCH 2/3] Add getPeers method To know if relay messages would be published. --- CHANGELOG.md | 3 +++ src/lib/waku_relay/index.ts | 12 ++++++++++++ 2 files changed, 15 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index ea1b4986f4..6593a46674 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Added +- `WakuRelay.getPeers` method. + ### Changed - Enable passing `string`s to `addPeerToAddressBook`. - Use `addPeerToAddressBook` in examples and usage doc. diff --git a/src/lib/waku_relay/index.ts b/src/lib/waku_relay/index.ts index 8ce2430e92..951d77648c 100644 --- a/src/lib/waku_relay/index.ts +++ b/src/lib/waku_relay/index.ts @@ -152,6 +152,18 @@ export class WakuRelay extends Gossipsub implements Pubsub { } } + /** + * Return the relay peers we are connected to and we would publish a message to + */ + getPeers(): Set { + return getRelayPeers(this, DefaultPubsubTopic, this._options.D, (id) => { + // Filter peers we would not publish to + return ( + this.score.score(id) >= this._options.scoreThresholds.publishThreshold + ); + }); + } + /** * Join pubsub topic. * This is present to override the behavior of Gossipsub and should not From 89b35eb4df8b5b86528b4850530b14f313293d89 Mon Sep 17 00:00:00 2001 From: Franck Royer Date: Thu, 13 May 2021 19:48:05 +1000 Subject: [PATCH 3/3] Disable send button if no peer available Yet keep it enabled if user tries to send a command. Resolves #136 --- CHANGELOG.md | 1 + examples/web-chat/src/MessageInput.tsx | 6 +++++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6593a46674..21178fd88f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added - `WakuRelay.getPeers` method. +- Use `WakuRelay.getPeers` in web chat app example to disable send button. ### Changed - Enable passing `string`s to `addPeerToAddressBook`. diff --git a/examples/web-chat/src/MessageInput.tsx b/examples/web-chat/src/MessageInput.tsx index d8a8bfe208..3911bb873b 100644 --- a/examples/web-chat/src/MessageInput.tsx +++ b/examples/web-chat/src/MessageInput.tsx @@ -34,11 +34,15 @@ export default function MessageInput(props: Props) { } }; + // Enable the button if there are relay peers available or the user is sending a command + const activeButton = + (waku && waku.relay.getPeers().size !== 0) || inputText.startsWith('/'); + return (