diff --git a/src/chat/index.ts b/src/chat/index.ts index e5760b00b8..2b079380a2 100644 --- a/src/chat/index.ts +++ b/src/chat/index.ts @@ -4,7 +4,7 @@ import util from 'util'; import Waku from '../lib/waku'; import { WakuMessage } from '../lib/waku_message'; import { RelayDefaultTopic } from '../lib/waku_relay'; -import { delay } from '../test_utils/delay'; +import { delay } from '../test_utils/'; import { ChatMessage } from './chat_message'; diff --git a/src/lib/waku.spec.ts b/src/lib/waku.spec.ts index 6f906e0e80..836faef89b 100644 --- a/src/lib/waku.spec.ts +++ b/src/lib/waku.spec.ts @@ -1,8 +1,6 @@ import { expect } from 'chai'; -import { NOISE_KEY_1 } from '../test_utils/constants'; -import { makeLogFileName } from '../test_utils/log_file'; -import { NimWaku } from '../test_utils/nim_waku'; +import { makeLogFileName, NimWaku, NOISE_KEY_1 } from '../test_utils/'; import Waku from './waku'; import { RelayCodec } from './waku_relay'; diff --git a/src/lib/waku_relay/index.ts b/src/lib/waku_relay/index.ts index 786a18c23d..286aa874f8 100644 --- a/src/lib/waku_relay/index.ts +++ b/src/lib/waku_relay/index.ts @@ -12,14 +12,7 @@ import PeerId from 'peer-id'; import { WakuMessage } from '../waku_message'; -import { - RelayCodec, - RelayDefaultTopic, - RelayGossipFactor, - RelayMaxIHaveLength, - RelayPruneBackoff, - RelayPrunePeers, -} from './constants'; +import * as constants from './constants'; import { getRelayPeers } from './get_relay_peers'; import { RelayHeartbeat } from './relay_heartbeat'; @@ -43,7 +36,7 @@ export class WakuRelayPubsub extends Gossipsub { this.heartbeat = new RelayHeartbeat(this); - const multicodecs = [RelayCodec]; + const multicodecs = [constants.RelayCodec]; // This is the downside of using `libp2p-gossipsub` instead of // implementing WakuRelay from scratch. @@ -193,7 +186,7 @@ export class WakuRelayPubsub extends Gossipsub { shuffle(messageIDs); // if we are emitting more than GossipsubMaxIHaveLength ids, truncate the list - if (messageIDs.length > RelayMaxIHaveLength) { + if (messageIDs.length > constants.RelayMaxIHaveLength) { // we do the truncation (with shuffling) per peer below this.log( 'too many messages for gossip; will truncate IHAVE list (%d messages)', @@ -219,7 +212,7 @@ export class WakuRelayPubsub extends Gossipsub { if ( !exclude.has(id) && !this.direct.has(id) && - peerStreams.protocol == RelayCodec && + peerStreams.protocol == constants.RelayCodec && this.score.score(id) >= this._options.scoreThresholds.gossipThreshold ) { peersToGossip.push(id); @@ -227,7 +220,7 @@ export class WakuRelayPubsub extends Gossipsub { }); let target = this._options.Dlazy; - const factor = RelayGossipFactor * peersToGossip.length; + const factor = constants.RelayGossipFactor * peersToGossip.length; if (factor > target) { target = factor; } @@ -239,13 +232,13 @@ export class WakuRelayPubsub extends Gossipsub { // Emit the IHAVE gossip to the selected peers up to the target peersToGossip.slice(0, target).forEach((id) => { let peerMessageIDs = messageIDs; - if (messageIDs.length > RelayMaxIHaveLength) { + if (messageIDs.length > constants.RelayMaxIHaveLength) { // shuffle and slice message IDs per peer so that we emit a different set for each peer // we have enough redundancy in the system that this will significantly increase the message // coverage when we do truncate peerMessageIDs = shuffle(peerMessageIDs.slice()).slice( 0, - RelayMaxIHaveLength + constants.RelayMaxIHaveLength ); } this._pushGossip(id, { @@ -265,14 +258,14 @@ export class WakuRelayPubsub extends Gossipsub { _makePrune(id: string, topic: string, doPX: boolean): ControlPrune { // backoff is measured in seconds // RelayPruneBackoff is measured in milliseconds - const backoff = RelayPruneBackoff / 1000; + const backoff = constants.RelayPruneBackoff / 1000; const px: PeerInfo[] = []; if (doPX) { // select peers for Peer eXchange const peers = getRelayPeers( this, topic, - RelayPrunePeers, + constants.RelayPrunePeers, (xid: string): boolean => { return xid !== id && this.score.score(xid) >= 0; } @@ -305,11 +298,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(RelayDefaultTopic); + await this.pubsub.subscribe(constants.RelayDefaultTopic); } async publish(message: WakuMessage) { const msg = message.toBinary(); - await this.pubsub.publish(RelayDefaultTopic, msg); + await this.pubsub.publish(constants.RelayDefaultTopic, msg); } } diff --git a/src/test_utils/index.ts b/src/test_utils/index.ts new file mode 100644 index 0000000000..62a0c6e5aa --- /dev/null +++ b/src/test_utils/index.ts @@ -0,0 +1,5 @@ +export * from './async_fs'; +export * from './constants'; +export * from './delay'; +export * from './log_file'; +export * from './nim_waku';