Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: create default shard info, update tests #2085

Merged
merged 7 commits into from
Jul 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions packages/discovery/src/dns/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,11 @@ import type { NodeCapabilityCount } from "@waku/interfaces";
/**
* The ENR tree for the different fleets.
* SANDBOX and TEST fleets are for The Waku Network.
* DEPRECATED_DEFAULT_PUBSUB is the fleet of nodes supporting the now deprecated DefaultPubsubTopic.
*/
export const enrTree = {
SANDBOX:
"enrtree://AIRVQ5DDA4FFWLRBCHJWUWOO6X6S4ZTZ5B667LQ6AJU6PEYDLRD5O@sandbox.waku.nodes.status.im",
TEST: "enrtree://AOGYWMBYOUIMOENHXCHILPKY3ZRFEULMFI4DOM442QSZ73TT2A7VI@test.waku.nodes.status.im",
DEPRECATED_DEFAULT_PUBSUB:
"enrtree://ANEDLO25QVUGJOUTQFRYKWX6P4Z4GKVESBMHML7DZ6YK4LGS5FC5O@prod.wakuv2.nodes.status.im"
TEST: "enrtree://AOGYWMBYOUIMOENHXCHILPKY3ZRFEULMFI4DOM442QSZ73TT2A7VI@test.waku.nodes.status.im"
};

export const DEFAULT_BOOTSTRAP_TAG_NAME = "bootstrap";
Expand Down
13 changes: 9 additions & 4 deletions packages/interfaces/src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
/**
* DefaultPubsubTopic is the default gossipsub topic to use for Waku.
*/
export const DefaultPubsubTopic = "/waku/2/rs/0/0";
import { ShardInfo } from "./enr";

/**
* The default cluster ID for The Waku Network
*/
export const DEFAULT_CLUSTER_ID = 1;

/**
* DefaultShardInfo is default configuration for The Waku Network.
*/
export const DefaultShardInfo: ShardInfo = {
clusterId: DEFAULT_CLUSTER_ID,
shards: [0, 1, 2, 3, 4, 5, 6, 7, 8]
};
36 changes: 20 additions & 16 deletions packages/interfaces/src/protocols.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,31 +73,35 @@ export type ProtocolUseOptions = {
export type ProtocolCreateOptions = {
/**
* @deprecated
* Waku will stop supporting named sharding. Only static sharding and autosharding will be supported moving forward.
*/
pubsubTopics?: PubsubTopic[];
/**
* Waku supports usage of multiple pubsub topics. This is achieved through static sharding for now, and auto-sharding in the future.
* The format to specify a shard is:
* clusterId: number, shards: number[]
* To learn more about the sharding specifications implemented, see [Relay Sharding](https://rfc.vac.dev/spec/51/).
* The Pubsub Topic to use. Defaults to {@link @waku/core!DefaultPubsubTopic }.
* Should be used ONLY if some other than The Waku Network is in use.
*
* See [Waku v2 Topic Usage Recommendations](https://github.com/vacp2p/rfc-index/blob/main/waku/informational/23/topics.md#pubsub-topics) for details.
*
* If no pubsub topic is specified, the default pubsub topic is used.
* The set of pubsub topics that are used to initialize the Waku node, will need to be used by the protocols as well
* You cannot currently add or remove pubsub topics after initialization.
* This is used by:
* - WakuRelay to receive, route and send messages,
* - WakuLightPush to send messages,
* - WakuStore to retrieve messages.
* See [Waku v2 Topic Usage Recommendations](https://github.com/vacp2p/rfc-index/blob/main/waku/informational/23/topics.md) for details.
*
* If no pubsub topic is specified, the default pubsub topic will be determined from DefaultShardInfo.
*
* You cannot add or remove pubsub topics after initialization of the node.
*/
pubsubTopics?: PubsubTopic[];
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd vote to remove this completely as well. We are assuming that developers might still want to use named sharding, but a complete deprecation looks like the right step ahead.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was also thinking the same - but I changed my mind when recalled scenarios of some of our consumers that do not use any of our fleets, not to mention TWN.

So we need to continue providing support for pubsubTopics in order to allow them soft transition (or for some not to use our fleets at all).

/**
* ShardInfo is used to determine which network is in use.
* Defaults to {@link @waku/interfaces!DefaultShardInfo}.
* Default value is configured for The Waku Network
*
* The format to specify a shard is:
* clusterId: number, shards: number[]
* To learn more about the sharding specification, see [Relay Sharding](https://rfc.vac.dev/spec/51/).
*/
shardInfo?: Partial<ShardingParams>;
/**
* Content topics are used to determine pubsubTopics
* If not provided pubsubTopics will be determined based on shardInfo
* See [Waku v2 Topic Usage Recommendations](https://github.com/vacp2p/rfc-index/blob/main/waku/informational/23/topics.md) for details.
* Content topics are used to determine network in use.
* See [Waku v2 Topic Usage Recommendations](https://github.com/vacp2p/rfc-index/blob/main/waku/informational/23/topics.md#content-topics) for details.
*
* You cannot add or remove content topics after initialization of the node.
*/
contentTopics?: string[];
/**
Expand Down
5 changes: 2 additions & 3 deletions packages/message-encryption/src/symmetric.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Decoder as DecoderV0 } from "@waku/core/lib/message/version_0";
import {
type EncoderOptions as BaseEncoderOptions,
DefaultPubsubTopic,
type IDecoder,
type IEncoder,
type IMessage,
Expand Down Expand Up @@ -101,7 +100,7 @@ export interface EncoderOptions extends BaseEncoderOptions {
* in [26/WAKU2-PAYLOAD](https://rfc.vac.dev/spec/26/).
*/
export function createEncoder({
pubsubTopic = DefaultPubsubTopic,
pubsubTopic,
pubsubTopicShardInfo,
contentTopic,
symKey,
Expand Down Expand Up @@ -198,7 +197,7 @@ class Decoder extends DecoderV0 implements IDecoder<DecodedMessage> {
export function createDecoder(
contentTopic: string,
symKey: Uint8Array,
pubsubTopicShardInfo: SingleShardInfo | PubsubTopic = DefaultPubsubTopic
pubsubTopicShardInfo?: SingleShardInfo | PubsubTopic
): Decoder {
return new Decoder(
determinePubsubTopic(contentTopic, pubsubTopicShardInfo),
Expand Down
9 changes: 4 additions & 5 deletions packages/relay/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import { sha256 } from "@noble/hashes/sha256";
import {
ActiveSubscriptions,
Callback,
DefaultPubsubTopic,
IAsyncIterator,
IDecodedMessage,
IDecoder,
Expand Down Expand Up @@ -75,9 +74,8 @@ class Relay implements IRelay {

this.observers = new Map();

// Default PubsubTopic decoder
// TODO: User might want to decide what decoder should be used (e.g. for RLN)
this.defaultDecoder = new TopicOnlyDecoder();
this.defaultDecoder = new TopicOnlyDecoder(pubsubTopics[0]);
}

/**
Expand Down Expand Up @@ -204,8 +202,9 @@ class Relay implements IRelay {
return map;
}

public getMeshPeers(topic: TopicStr = DefaultPubsubTopic): PeerIdStr[] {
return this.gossipSub.getMeshPeers(topic);
public getMeshPeers(topic?: TopicStr): PeerIdStr[] {
// if no TopicStr is provided - returns empty array
return this.gossipSub.getMeshPeers(topic || "");
}

private subscribeToAllTopics(): void {
Expand Down
9 changes: 6 additions & 3 deletions packages/relay/src/topic_only_message.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { DefaultPubsubTopic } from "@waku/interfaces";
import type {
IDecodedMessage,
IDecoder,
IProtoMessage
IProtoMessage,
PubsubTopic
} from "@waku/interfaces";
import { TopicOnlyMessage as ProtoTopicOnlyMessage } from "@waku/proto";

Expand All @@ -23,10 +23,13 @@ export class TopicOnlyMessage implements IDecodedMessage {
}
}

// This decoder is used only for reading `contentTopic` from the WakuMessage
export class TopicOnlyDecoder implements IDecoder<TopicOnlyMessage> {
public pubsubTopic = DefaultPubsubTopic;
public contentTopic = "";

// pubsubTopic is ignored
public constructor(public pubsubTopic: PubsubTopic) {}

public fromWireToProtoObj(
bytes: Uint8Array
): Promise<IProtoMessage | undefined> {
Expand Down
11 changes: 2 additions & 9 deletions packages/sdk/src/create/discovery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,7 @@ import {
wakuLocalPeerCacheDiscovery,
wakuPeerExchangeDiscovery
} from "@waku/discovery";
import {
DefaultPubsubTopic,
type Libp2pComponents,
PubsubTopic
} from "@waku/interfaces";
import { type Libp2pComponents, PubsubTopic } from "@waku/interfaces";

const DEFAULT_NODE_REQUIREMENTS = {
lightPush: 1,
Expand All @@ -20,10 +16,7 @@ const DEFAULT_NODE_REQUIREMENTS = {
export function defaultPeerDiscoveries(
pubsubTopics: PubsubTopic[]
): ((components: Libp2pComponents) => PeerDiscovery)[] {
const isDefaultPubsubTopic = pubsubTopics.includes(DefaultPubsubTopic);
const dnsEnrTrees = isDefaultPubsubTopic
? [enrTree["DEPRECATED_DEFAULT_PUBSUB"]]
: [enrTree["SANDBOX"], enrTree["TEST"]];
const dnsEnrTrees = [enrTree["SANDBOX"], enrTree["TEST"]];

const discoveries = [
wakuDnsDiscovery(dnsEnrTrees, DEFAULT_NODE_REQUIREMENTS),
Expand Down
9 changes: 6 additions & 3 deletions packages/sdk/src/create/libp2p.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import { wakuMetadata } from "@waku/core";
import {
type CreateLibp2pOptions,
DefaultPubsubTopic,
DefaultShardInfo,
type IMetadata,
type Libp2p,
type Libp2pComponents,
Expand Down Expand Up @@ -86,7 +86,7 @@
...pubsubService,
...options?.services
}
}) as any as Libp2p; // TODO: make libp2p include it;

Check warning on line 89 in packages/sdk/src/create/libp2p.ts

View workflow job for this annotation

GitHub Actions / check

Unexpected any. Specify a different type

Check warning on line 89 in packages/sdk/src/create/libp2p.ts

View workflow job for this annotation

GitHub Actions / proto

Unexpected any. Specify a different type
}

export async function createLibp2pAndUpdateOptions(
Expand Down Expand Up @@ -138,12 +138,15 @@
options.shardInfo = { contentTopics: options.contentTopics };
}

if (!options.shardInfo) {
options.shardInfo = DefaultShardInfo;
}

const shardInfo = options.shardInfo
? ensureShardingConfigured(options.shardInfo)
: undefined;

options.pubsubTopics = shardInfo?.pubsubTopics ??
options.pubsubTopics ?? [DefaultPubsubTopic];
options.pubsubTopics = options.pubsubTopics ?? shardInfo?.pubsubTopics;
Comment on lines +141 to +149
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here in case shardInfo exists, we have redundancy updating shardInfo as well as pubsubTopic with same information. Comment above about complete deprecation of this property looks like the clean direction.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a good point but I don't think we should get rid of pubsubTopics for internal use because we:

  • have tight coupling to it in our code base;
  • libp2p itself uses it a lot;
  • it's native to gossipSub so anyway we face the need to refer to it one way or another;


return shardInfo?.shardInfo;
}
Expand Down
11 changes: 4 additions & 7 deletions packages/tests/src/lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
import { DecodedMessage } from "@waku/core";
import {
DefaultPubsubTopic,
PubsubTopic,
ShardingParams
} from "@waku/interfaces";
import { PubsubTopic, ShardingParams } from "@waku/interfaces";
import { ensureShardingConfigured, Logger } from "@waku/utils";
import { expect } from "chai";

import { DefaultTestPubsubTopic } from "../constants";
import { Args, MessageRpcQuery, MessageRpcResponse } from "../types";
import { delay, makeLogFileName } from "../utils/index.js";

Expand Down Expand Up @@ -105,7 +102,7 @@ export class ServiceNodesFleet {

public async sendRelayMessage(
message: MessageRpcQuery,
pubsubTopic: string = DefaultPubsubTopic
pubsubTopic: string = DefaultTestPubsubTopic
): Promise<boolean> {
const relayMessagePromises: Promise<boolean>[] = this.nodes.map((node) =>
node.sendMessage(message, pubsubTopic)
Expand Down Expand Up @@ -221,7 +218,7 @@ class MultipleNodesMessageCollector {
}
): Promise<boolean> {
const startTime = Date.now();
const pubsubTopic = options?.pubsubTopic || DefaultPubsubTopic;
const pubsubTopic = options?.pubsubTopic || DefaultTestPubsubTopic;
const timeoutDuration = options?.timeoutDuration || 400;
const exact = options?.exact || false;

Expand Down
6 changes: 4 additions & 2 deletions packages/tests/src/lib/message_collector.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { DecodedMessage } from "@waku/core";
import { DefaultPubsubTopic } from "@waku/interfaces";
import { Logger } from "@waku/utils";
import { bytesToUtf8, utf8ToBytes } from "@waku/utils/bytes";
import { AssertionError, expect } from "chai";
import { equals } from "uint8arrays/equals";

import { DefaultTestPubsubTopic } from "../constants.js";
import { MessageRpcResponse } from "../types.js";
import { base64ToUtf8 } from "../utils/base64_utf8.js";
import { delay } from "../utils/delay.js";
Expand Down Expand Up @@ -269,6 +269,8 @@ export class MessageCollector {
}

private getPubsubTopicToUse(pubsubTopic: string | undefined): string {
return pubsubTopic || this.nwaku?.pubsubTopics?.[0] || DefaultPubsubTopic;
return (
pubsubTopic || this.nwaku?.pubsubTopics?.[0] || DefaultTestPubsubTopic
);
}
}
11 changes: 6 additions & 5 deletions packages/tests/src/lib/service_node.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import type { PeerId } from "@libp2p/interface";
import { peerIdFromString } from "@libp2p/peer-id";
import { Multiaddr, multiaddr } from "@multiformats/multiaddr";
import { DefaultPubsubTopic } from "@waku/interfaces";
import { isDefined } from "@waku/utils";
import { Logger } from "@waku/utils";
import pRetry from "p-retry";
import portfinder from "portfinder";

import { DefaultTestPubsubTopic } from "../constants.js";
import {
Args,
LogLevel,
Expand Down Expand Up @@ -245,7 +245,7 @@
}

public async ensureSubscriptions(
pubsubTopics: string[] = [DefaultPubsubTopic]
pubsubTopics: string[] = [DefaultTestPubsubTopic]
): Promise<boolean> {
return this.restCall<boolean>(
"/relay/v1/subscriptions",
Expand All @@ -257,7 +257,7 @@

public async messages(pubsubTopic?: string): Promise<MessageRpcResponse[]> {
return this.restCall<MessageRpcResponse[]>(
`/relay/v1/messages/${encodeURIComponent(pubsubTopic || this?.args?.pubsubTopic?.[0] || DefaultPubsubTopic)}`,
`/relay/v1/messages/${encodeURIComponent(pubsubTopic || this?.args?.pubsubTopic?.[0] || DefaultTestPubsubTopic)}`,
"GET",
null,
async (response) => {
Expand Down Expand Up @@ -291,7 +291,7 @@
}

return this.restCall<boolean>(
`/relay/v1/messages/${encodeURIComponent(pubsubTopic || this.args?.pubsubTopic?.[0] || DefaultPubsubTopic)}`,
`/relay/v1/messages/${encodeURIComponent(pubsubTopic || this.args?.pubsubTopic?.[0] || DefaultTestPubsubTopic)}`,
"POST",
message,
async (response) => response.status === 200
Expand Down Expand Up @@ -375,7 +375,7 @@
public async restCall<T>(
endpoint: string,
method: "GET" | "POST",
body: any = null,

Check warning on line 378 in packages/tests/src/lib/service_node.ts

View workflow job for this annotation

GitHub Actions / check

Unexpected any. Specify a different type

Check warning on line 378 in packages/tests/src/lib/service_node.ts

View workflow job for this annotation

GitHub Actions / proto

Unexpected any. Specify a different type
processResponse: (response: Response) => Promise<T>
): Promise<T> {
this.checkProcess();
Expand Down Expand Up @@ -411,7 +411,8 @@
rest: true,
restAdmin: true,
websocketSupport: true,
logLevel: LogLevel.Trace
logLevel: LogLevel.Trace,
pubsubTopic: ["/waku/2/rs/0/0"]
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,12 @@ describe("Connection state", function () {

it("`waku:online` between 2 js-waku relay nodes", async function () {
const waku1 = await createRelayNode({
staticNoiseKey: NOISE_KEY_1
staticNoiseKey: NOISE_KEY_1,
shardInfo: DefaultTestShardInfo
});
const waku2 = await createRelayNode({
libp2p: { addresses: { listen: ["/ip4/0.0.0.0/tcp/0/ws"] } }
libp2p: { addresses: { listen: ["/ip4/0.0.0.0/tcp/0/ws"] } },
shardInfo: DefaultTestShardInfo
});

let eventCount1 = 0;
Expand Down
10 changes: 7 additions & 3 deletions packages/tests/tests/enr.node.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
ServiceNode,
tearDownNodes
} from "../src/index.js";
import { DefaultTestShardInfo } from "../src/index.js";

describe("ENR Interop: ServiceNode", function () {
let waku: RelayNode;
Expand All @@ -35,7 +36,8 @@ describe("ENR Interop: ServiceNode", function () {
const multiAddrWithId = await nwaku.getMultiaddrWithId();

waku = await createRelayNode({
staticNoiseKey: NOISE_KEY_1
staticNoiseKey: NOISE_KEY_1,
shardInfo: DefaultTestShardInfo
});
await waku.start();
await waku.dial(multiAddrWithId);
Expand Down Expand Up @@ -68,7 +70,8 @@ describe("ENR Interop: ServiceNode", function () {
const multiAddrWithId = await nwaku.getMultiaddrWithId();

waku = await createRelayNode({
staticNoiseKey: NOISE_KEY_1
staticNoiseKey: NOISE_KEY_1,
shardInfo: DefaultTestShardInfo
});
await waku.start();
await waku.dial(multiAddrWithId);
Expand Down Expand Up @@ -102,7 +105,8 @@ describe("ENR Interop: ServiceNode", function () {
const multiAddrWithId = await nwaku.getMultiaddrWithId();

waku = await createRelayNode({
staticNoiseKey: NOISE_KEY_1
staticNoiseKey: NOISE_KEY_1,
shardInfo: DefaultTestShardInfo
});
await waku.start();
await waku.dial(multiAddrWithId);
Expand Down
Loading
Loading