2023-10-03 10:05:32 +03:00
|
|
|
import {
|
|
|
|
|
createDecoder,
|
|
|
|
|
createEncoder,
|
|
|
|
|
DecodedMessage,
|
|
|
|
|
Decoder,
|
|
|
|
|
waitForRemotePeer
|
|
|
|
|
} from "@waku/core";
|
2023-11-16 15:17:17 +03:00
|
|
|
import {
|
|
|
|
|
DefaultPubsubTopic,
|
|
|
|
|
LightNode,
|
|
|
|
|
Protocols,
|
|
|
|
|
ShardInfo,
|
|
|
|
|
ShardingParams
|
|
|
|
|
} from "@waku/interfaces";
|
2023-09-29 14:03:43 +03: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-01-18 00:26:31 +05:30
|
|
|
import { delay, NOISE_KEY_1, ServiceNode } from "../../src";
|
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
|
|
|
|
2023-10-03 10:05:32 +03:00
|
|
|
export const TestContentTopic = "/test/1/waku-store/utf8";
|
|
|
|
|
export const TestEncoder = createEncoder({ contentTopic: TestContentTopic });
|
|
|
|
|
export const TestDecoder = createDecoder(TestContentTopic);
|
2023-11-28 15:57:18 +05:30
|
|
|
export const customShardedPubsubTopic1 = singleShardInfoToPubsubTopic({
|
2023-11-29 17:37:59 +05:30
|
|
|
clusterId: 3,
|
|
|
|
|
shard: 1
|
2023-11-28 15:57:18 +05:30
|
|
|
});
|
|
|
|
|
export const customShardedPubsubTopic2 = singleShardInfoToPubsubTopic({
|
2023-11-29 17:37:59 +05:30
|
|
|
clusterId: 3,
|
|
|
|
|
shard: 2
|
2023-11-28 15:57:18 +05:30
|
|
|
});
|
2023-11-29 17:37:59 +05:30
|
|
|
export const shardInfo1: ShardInfo = { clusterId: 3, shards: [1] };
|
2023-11-28 15:57:18 +05:30
|
|
|
export const customContentTopic1 = "/test/2/waku-store/utf8";
|
|
|
|
|
export const customContentTopic2 = "/test/3/waku-store/utf8";
|
|
|
|
|
export const customDecoder1 = createDecoder(customContentTopic1, {
|
2023-11-29 17:37:59 +05:30
|
|
|
clusterId: 3,
|
|
|
|
|
shard: 1
|
2023-11-28 15:57:18 +05:30
|
|
|
});
|
|
|
|
|
export const customDecoder2 = createDecoder(customContentTopic2, {
|
2023-11-29 17:37:59 +05:30
|
|
|
clusterId: 3,
|
|
|
|
|
shard: 2
|
2023-11-28 15:57:18 +05:30
|
|
|
});
|
2023-11-29 17:37:59 +05:30
|
|
|
export const shardInfoBothShards: ShardInfo = { clusterId: 3, shards: [1, 2] };
|
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,
|
2023-10-16 12:52:32 +05:30
|
|
|
pubsubTopic: string
|
2023-09-29 14:03:43 +03:00
|
|
|
): Promise<void> {
|
|
|
|
|
for (let i = 0; i < numMessages; i++) {
|
|
|
|
|
expect(
|
|
|
|
|
await instance.sendMessage(
|
2024-01-18 00:26:31 +05:30
|
|
|
ServiceNode.toMessageRpcQuery({
|
2023-09-29 14:03:43 +03:00
|
|
|
payload: new Uint8Array([i]),
|
|
|
|
|
contentTopic: contentTopic
|
|
|
|
|
}),
|
2023-10-16 12:52:32 +05:30
|
|
|
pubsubTopic
|
2023-09-29 14:03:43 +03:00
|
|
|
)
|
2023-10-13 12:36:43 +03:00
|
|
|
).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
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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,
|
2023-11-28 15:57:18 +05:30
|
|
|
pubsubTopics: string[] = [DefaultPubsubTopic],
|
2023-11-16 15:17:17 +03:00
|
|
|
shardInfo?: ShardingParams
|
2023-09-29 14:03:43 +03:00
|
|
|
): Promise<LightNode> {
|
|
|
|
|
const waku = await createLightNode({
|
2023-11-28 15:57:18 +05:30
|
|
|
...((pubsubTopics.length !== 1 ||
|
|
|
|
|
pubsubTopics[0] !== DefaultPubsubTopic) && {
|
|
|
|
|
shardInfo: shardInfo
|
|
|
|
|
}),
|
2024-01-09 23:34:30 -08:00
|
|
|
pubsubTopics: shardInfo ? undefined : pubsubTopics,
|
2023-09-29 14:03:43 +03:00
|
|
|
staticNoiseKey: NOISE_KEY_1
|
|
|
|
|
});
|
|
|
|
|
await waku.start();
|
|
|
|
|
await waku.dial(await instance.getMultiaddrWithId());
|
|
|
|
|
await waitForRemotePeer(waku, [Protocols.Store]);
|
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;
|
|
|
|
|
};
|