@moonsong-labs/moonwall-cli
Version:
Testing framework for the Moon family of projects
112 lines (109 loc) • 4.03 kB
JavaScript
import {
MoonwallContext
} from "./chunk-WICDGWJQ.js";
// src/internal/chopsticksHelpers.ts
import { setTimeout } from "timers/promises";
import chalk from "chalk";
import { assert } from "vitest";
async function getWsFromConfig(providerName) {
return providerName ? MoonwallContext.getContext().environment.providers.find(({ name }) => name == providerName).ws() : MoonwallContext.getContext().environment.providers.find(({ type }) => type == "moon" || type == "polkadotJs").ws();
}
async function sendNewBlockAndCheck(context, expectedEvents) {
const newBlock = await sendNewBlockRequest();
const api = context.getSubstrateApi();
const apiAt = await api.at(newBlock);
const actualEvents = await apiAt.query.system.events();
const match = expectedEvents.every((eEvt) => {
return actualEvents.map((aEvt) => {
if (api.events.system.ExtrinsicSuccess.is(aEvt.event) && aEvt.event.data.dispatchInfo.class.toString() !== "Normal") {
return false;
}
return eEvt.is(aEvt.event);
}).reduce((acc, curr) => acc || curr, false);
});
return { match, events: actualEvents };
}
async function createChopsticksBlock(context, options = { allowFailures: false }) {
const result = await sendNewBlockRequest(options);
const apiAt = await context.getSubstrateApi().at(result);
const actualEvents = await apiAt.query.system.events();
if (options && options.expectEvents) {
const match = options.expectEvents.every((eEvt) => {
const found = actualEvents.map((aEvt) => eEvt.is(aEvt.event)).reduce((acc, curr) => acc || curr, false);
if (!found) {
options.logger ? options.logger(
`Event ${chalk.bgWhiteBright.blackBright(eEvt.meta.name)} not present in block`
) : console.error(
`Event ${chalk.bgWhiteBright.blackBright(eEvt.meta.name)} not present in block`
);
}
return found;
});
assert(match, "Expected events not present in block");
}
if (options && options.allowFailures === true) {
} else {
actualEvents.forEach((event) => {
assert(
!context.getSubstrateApi().events.system.ExtrinsicFailed.is(event.event),
"ExtrinsicFailed event detected, enable 'allowFailures' if this is expected."
);
});
}
return { result };
}
async function chopForkToFinalizedHead(context) {
const api = context.providers.find(({ type }) => type == "moon" || type == "polkadotJs").api;
const finalizedHead = context.genesis;
await sendSetHeadRequest(finalizedHead);
await sendNewBlockRequest();
while (true) {
const newHead = (await api.rpc.chain.getFinalizedHead()).toString();
await setTimeout(50);
if (newHead !== finalizedHead) {
context.genesis = newHead;
break;
}
}
}
async function sendSetHeadRequest(newHead, providerName) {
const ws = providerName ? await getWsFromConfig(providerName) : await getWsFromConfig();
let result = "";
await ws.isReady;
result = await ws.send("dev_setHead", [newHead]);
await ws.disconnect();
return result;
}
async function sendNewBlockRequest(params) {
const ws = params ? await getWsFromConfig(params.providerName) : await getWsFromConfig();
let result = "";
while (!ws.isConnected) {
await setTimeout(100);
}
if (params && params.count || params && params.to) {
result = await ws.send("dev_newBlock", [{ count: params.count, to: params.to }]);
} else {
result = await ws.send("dev_newBlock", [{ count: 1 }]);
}
await ws.disconnect();
return result;
}
async function sendSetStorageRequest(params) {
const ws = params ? await getWsFromConfig(params.providerName) : await getWsFromConfig();
while (!ws.isConnected) {
await setTimeout(100);
}
await ws.send("dev_setStorage", [
{ [params.module]: { [params.method]: params.methodParams } }
]);
await ws.disconnect();
}
export {
getWsFromConfig,
sendNewBlockAndCheck,
createChopsticksBlock,
chopForkToFinalizedHead,
sendSetHeadRequest,
sendNewBlockRequest,
sendSetStorageRequest
};