2023-10-03 10:05:32 +03:00
|
|
|
import {
|
|
|
|
|
createDecoder,
|
|
|
|
|
createEncoder,
|
|
|
|
|
DecodedMessage,
|
2024-04-28 11:15:17 +02:00
|
|
|
Decoder
|
2023-10-03 10:05:32 +03:00
|
|
|
} from "@waku/core";
|
2023-11-16 15:17:17 +03:00
|
|
|
import {
|
|
|
|
|
LightNode,
|
2024-08-13 05:23:20 +05:30
|
|
|
NetworkConfig,
|
2023-11-16 15:17:17 +03:00
|
|
|
Protocols,
|
|
|
|
|
ShardInfo,
|
2024-01-19 20:42:52 +05:30
|
|
|
type SingleShardInfo
|
2023-11-16 15:17:17 +03:00
|
|
|
} from "@waku/interfaces";
|
2024-10-09 00:43:34 +02:00
|
|
|
import { createLightNode } from "@waku/sdk";
|
2023-11-28 15:57:18 +05:30
|
|
|
import { Logger, singleShardInfoToPubsubTopic } from "@waku/utils";
|
2023-09-29 14:03:43 +03:00
|
|
|
import { expect } from "chai";
|
2024-04-28 11:15:17 +02:00
|
|
|
import { Context } from "mocha";
|
2023-09-29 14:03:43 +03:00
|
|
|
|
2024-09-13 14:18:29 +05:30
|
|
|
import { delay, NOISE_KEY_1, runNodes, ServiceNode } from "../../src/index.js";
|
2025-05-30 11:44:03 -07:00
|
|
|
import { MessageRpcQuery } from "../../src/types.js";
|
2023-09-29 14:03:43 +03:00
|
|
|
|
2023-10-20 16:36:47 +05:30
|
|
|
export const log = new Logger("test:store");
|
2023-09-29 19:10:03 +03:00
|
|
|
|
2024-04-28 11:15:17 +02:00
|
|
|
export const TestClusterId = 3;
|
|
|
|
|
export const TestShardInfo: ShardInfo = {
|
|
|
|
|
clusterId: TestClusterId,
|
|
|
|
|
shards: [1, 2]
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const TestShardInfo1: SingleShardInfo = { clusterId: 3, shard: 1 };
|
|
|
|
|
export const TestPubsubTopic1 = singleShardInfoToPubsubTopic(TestShardInfo1);
|
|
|
|
|
|
|
|
|
|
export const TestShardInfo2: SingleShardInfo = { clusterId: 3, shard: 2 };
|
|
|
|
|
export const TestPubsubTopic2 = singleShardInfoToPubsubTopic(TestShardInfo2);
|
|
|
|
|
|
|
|
|
|
export const TestContentTopic1 = "/test/1/waku-store/utf8";
|
|
|
|
|
export const TestEncoder = createEncoder({
|
|
|
|
|
contentTopic: TestContentTopic1,
|
|
|
|
|
pubsubTopicShardInfo: TestShardInfo1
|
2023-11-28 15:57:18 +05:30
|
|
|
});
|
2024-04-28 11:15:17 +02:00
|
|
|
export const TestDecoder = createDecoder(TestContentTopic1, TestPubsubTopic1);
|
|
|
|
|
|
|
|
|
|
export const TestContentTopic2 = "/test/3/waku-store/utf8";
|
|
|
|
|
export const TestDecoder2 = createDecoder(TestContentTopic2, TestPubsubTopic2);
|
|
|
|
|
|
2023-10-03 10:05:32 +03:00
|
|
|
export const totalMsgs = 20;
|
|
|
|
|
export const messageText = "Store Push works!";
|
|
|
|
|
|
2023-09-29 14:03:43 +03:00
|
|
|
export async function sendMessages(
|
2024-01-18 00:26:31 +05:30
|
|
|
instance: ServiceNode,
|
2023-09-29 14:03:43 +03:00
|
|
|
numMessages: number,
|
|
|
|
|
contentTopic: string,
|
2025-05-30 11:44:03 -07:00
|
|
|
pubsubTopic: string,
|
|
|
|
|
timestamp: boolean = false
|
|
|
|
|
): Promise<MessageRpcQuery[]> {
|
|
|
|
|
const messages: MessageRpcQuery[] = new Array<MessageRpcQuery>(numMessages);
|
2023-09-29 14:03:43 +03:00
|
|
|
for (let i = 0; i < numMessages; i++) {
|
2025-05-30 11:44:03 -07:00
|
|
|
messages[i] = ServiceNode.toMessageRpcQuery({
|
|
|
|
|
payload: new Uint8Array([i]),
|
|
|
|
|
contentTopic: contentTopic,
|
|
|
|
|
timestamp: timestamp ? new Date() : undefined
|
|
|
|
|
});
|
|
|
|
|
expect(await instance.sendMessage(messages[i], pubsubTopic)).to.eq(true);
|
2023-10-03 10:05:32 +03:00
|
|
|
await delay(1); // to ensure each timestamp is unique.
|
2023-09-29 14:03:43 +03:00
|
|
|
}
|
2025-05-30 11:44:03 -07:00
|
|
|
return messages;
|
2023-09-29 14:03:43 +03:00
|
|
|
}
|
|
|
|
|
|
2023-11-16 15:17:17 +03:00
|
|
|
export async function sendMessagesAutosharding(
|
2024-01-18 00:26:31 +05:30
|
|
|
instance: ServiceNode,
|
2023-11-16 15:17:17 +03:00
|
|
|
numMessages: number,
|
|
|
|
|
contentTopic: string
|
|
|
|
|
): Promise<void> {
|
|
|
|
|
for (let i = 0; i < numMessages; i++) {
|
|
|
|
|
expect(
|
|
|
|
|
await instance.sendMessageAutosharding(
|
2024-01-18 00:26:31 +05:30
|
|
|
ServiceNode.toMessageRpcQuery({
|
2023-11-16 15:17:17 +03:00
|
|
|
payload: new Uint8Array([i]),
|
|
|
|
|
contentTopic: contentTopic
|
|
|
|
|
})
|
|
|
|
|
)
|
|
|
|
|
).to.eq(true);
|
|
|
|
|
await delay(1); // to ensure each timestamp is unique.
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-04 15:40:50 +03:00
|
|
|
export async function processQueriedMessages(
|
2023-09-29 14:03:43 +03:00
|
|
|
instance: LightNode,
|
|
|
|
|
decoders: Array<Decoder>,
|
2023-10-04 15:40:50 +03:00
|
|
|
expectedTopic?: string
|
2023-10-03 10:05:32 +03:00
|
|
|
): Promise<DecodedMessage[]> {
|
|
|
|
|
const localMessages: DecodedMessage[] = [];
|
2023-10-04 15:40:50 +03:00
|
|
|
for await (const query of instance.store.queryGenerator(decoders)) {
|
|
|
|
|
for await (const msg of query) {
|
2023-09-29 14:03:43 +03:00
|
|
|
if (msg) {
|
2023-10-16 12:52:32 +05:30
|
|
|
expect(msg.pubsubTopic).to.eq(expectedTopic);
|
2023-10-04 15:40:50 +03:00
|
|
|
localMessages.push(msg as DecodedMessage);
|
2023-09-29 14:03:43 +03:00
|
|
|
}
|
2023-10-04 15:40:50 +03:00
|
|
|
}
|
2023-09-29 14:03:43 +03:00
|
|
|
}
|
|
|
|
|
return localMessages;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function startAndConnectLightNode(
|
2024-01-18 00:26:31 +05:30
|
|
|
instance: ServiceNode,
|
2024-08-13 05:23:20 +05:30
|
|
|
networkConfig: NetworkConfig
|
2023-09-29 14:03:43 +03:00
|
|
|
): Promise<LightNode> {
|
|
|
|
|
const waku = await createLightNode({
|
2024-01-18 11:05:35 +01:00
|
|
|
staticNoiseKey: NOISE_KEY_1,
|
|
|
|
|
libp2p: { addresses: { listen: ["/ip4/0.0.0.0/tcp/0/ws"] } },
|
2024-08-13 05:23:20 +05:30
|
|
|
networkConfig: networkConfig
|
2023-09-29 14:03:43 +03:00
|
|
|
});
|
|
|
|
|
await waku.start();
|
|
|
|
|
await waku.dial(await instance.getMultiaddrWithId());
|
2024-10-09 00:43:34 +02:00
|
|
|
await waku.waitForPeers([Protocols.Store]);
|
2024-01-18 11:05:35 +01:00
|
|
|
|
|
|
|
|
const wakuConnections = waku.libp2p.getConnections();
|
|
|
|
|
|
2024-08-31 15:18:51 +02:00
|
|
|
if (wakuConnections.length < 1) {
|
|
|
|
|
throw new Error(`Expected at least 1 connection for js-waku.`);
|
2024-01-18 11:05:35 +01:00
|
|
|
}
|
|
|
|
|
|
2024-08-31 15:18:51 +02:00
|
|
|
await instance.waitForLog(waku.libp2p.peerId.toString(), 100);
|
|
|
|
|
|
2023-10-20 16:36:47 +05:30
|
|
|
log.info("Waku node created");
|
2023-09-29 14:03:43 +03:00
|
|
|
return waku;
|
|
|
|
|
}
|
2023-10-04 15:40:50 +03:00
|
|
|
|
|
|
|
|
export function chunkAndReverseArray(
|
|
|
|
|
arr: number[],
|
|
|
|
|
chunkSize: number
|
|
|
|
|
): number[] {
|
|
|
|
|
const result: number[] = [];
|
|
|
|
|
for (let i = 0; i < arr.length; i += chunkSize) {
|
|
|
|
|
result.push(...arr.slice(i, i + chunkSize).reverse());
|
|
|
|
|
}
|
|
|
|
|
return result.reverse();
|
|
|
|
|
}
|
2023-10-05 19:06:37 +03:00
|
|
|
|
|
|
|
|
export const adjustDate = (baseDate: Date, adjustMs: number): Date => {
|
|
|
|
|
const adjusted = new Date(baseDate);
|
|
|
|
|
adjusted.setTime(adjusted.getTime() + adjustMs);
|
|
|
|
|
return adjusted;
|
|
|
|
|
};
|
2024-04-28 11:15:17 +02:00
|
|
|
|
|
|
|
|
export const runStoreNodes = (
|
|
|
|
|
context: Context,
|
2024-08-13 05:23:20 +05:30
|
|
|
networkConfig: NetworkConfig
|
2024-04-28 11:15:17 +02:00
|
|
|
): Promise<[ServiceNode, LightNode]> =>
|
|
|
|
|
runNodes({
|
|
|
|
|
context,
|
2024-08-13 05:23:20 +05:30
|
|
|
networkConfig,
|
2024-04-28 11:15:17 +02:00
|
|
|
createNode: createLightNode,
|
|
|
|
|
protocols: [Protocols.Store]
|
|
|
|
|
});
|