140 lines
3.8 KiB
TypeScript
Raw Normal View History

2023-10-03 10:05:32 +03:00
import {
createDecoder,
createEncoder,
DecodedMessage,
Decoder,
waitForRemotePeer
} from "@waku/core";
import {
DefaultPubsubTopic,
LightNode,
Protocols,
ShardInfo,
ShardingParams
} from "@waku/interfaces";
2023-09-29 14:03:43 +03:00
import { createLightNode } from "@waku/sdk";
import { Logger, singleShardInfoToPubsubTopic } from "@waku/utils";
2023-09-29 14:03:43 +03:00
import { expect } from "chai";
import { delay, NOISE_KEY_1, ServiceNode } from "../../src";
2023-09-29 14:03:43 +03:00
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);
export const customShardedPubsubTopic1 = singleShardInfoToPubsubTopic({
clusterId: 3,
shard: 1
});
export const customShardedPubsubTopic2 = singleShardInfoToPubsubTopic({
clusterId: 3,
shard: 2
});
export const shardInfo1: ShardInfo = { clusterId: 3, shards: [1] };
export const customContentTopic1 = "/test/2/waku-store/utf8";
export const customContentTopic2 = "/test/3/waku-store/utf8";
export const customDecoder1 = createDecoder(customContentTopic1, {
clusterId: 3,
shard: 1
});
export const customDecoder2 = createDecoder(customContentTopic2, {
clusterId: 3,
shard: 2
});
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(
instance: ServiceNode,
2023-09-29 14:03:43 +03:00
numMessages: number,
contentTopic: string,
pubsubTopic: string
2023-09-29 14:03:43 +03:00
): Promise<void> {
for (let i = 0; i < numMessages; i++) {
expect(
await instance.sendMessage(
ServiceNode.toMessageRpcQuery({
2023-09-29 14:03:43 +03:00
payload: new Uint8Array([i]),
contentTopic: contentTopic
}),
pubsubTopic
2023-09-29 14:03: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
}
}
export async function sendMessagesAutosharding(
instance: ServiceNode,
numMessages: number,
contentTopic: string
): Promise<void> {
for (let i = 0; i < numMessages; i++) {
expect(
await instance.sendMessageAutosharding(
ServiceNode.toMessageRpcQuery({
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) {
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(
instance: ServiceNode,
pubsubTopics: string[] = [DefaultPubsubTopic],
shardInfo?: ShardingParams
2023-09-29 14:03:43 +03:00
): Promise<LightNode> {
const waku = await createLightNode({
...((pubsubTopics.length !== 1 ||
pubsubTopics[0] !== DefaultPubsubTopic) && {
shardInfo: shardInfo
}),
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]);
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;
};