Skip to content
6 changes: 5 additions & 1 deletion bin/rpc/test/e2e-setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ const withRelPath = workspacePathFix(`${import.meta.dirname}/../../..`);

async function main() {
const nodeConfig = loadConfig([`${import.meta.dirname}/e2e.config.json`], withRelPath);
const jamConfig = JamConfig.new({ nodeName: NODE_DEFAULTS.name, nodeConfig, pvmBackend: NODE_DEFAULTS.pvm });
const jamConfig = JamConfig.new({
nodeName: NODE_DEFAULTS.name,
nodeConfig,
pvmBackend: NODE_DEFAULTS.pvm,
});
try {
const api = await node(jamConfig, withRelPath, null);
await importBlocks(api, blocksToImport);
Expand Down
58 changes: 56 additions & 2 deletions bin/test-runner/common.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import assert from "node:assert";
import { describe, it } from "node:test";
import { describe, it, mock } from "node:test";
import { deepEqual } from "@typeberry/utils";
import { parseArgs, SelectedPvm } from "./common.js";
import { HELP_MESSAGE, parseArgs, SelectedPvm } from "./common.js";

describe("test runner common", () => {
it("should parse pvm argument", () => {
Expand All @@ -12,6 +12,7 @@ describe("test runner common", () => {
deepEqual(result, {
initialFiles: ["file1.json", "file2.json"],
pvms: [SelectedPvm.Ananas],
accumulateSequentially: false,
});
});

Expand All @@ -23,6 +24,7 @@ describe("test runner common", () => {
deepEqual(result, {
initialFiles: ["file1.json", "file2.json"],
pvms: [SelectedPvm.Ananas, SelectedPvm.Builtin],
accumulateSequentially: false,
});
});

Expand All @@ -38,4 +40,56 @@ describe("test runner common", () => {
},
);
});

it("should parse --accumulate-sequentially without value as true", () => {
const args = ["--accumulate-sequentially", "file1.json"];

const result = parseArgs(args);

deepEqual(result, {
initialFiles: ["file1.json"],
pvms: [SelectedPvm.Ananas, SelectedPvm.Builtin],
accumulateSequentially: true,
});
});

it("should parse --accumulate-sequentially=something as true", () => {
const args = ["--accumulate-sequentially=something", "file1.json"];

const result = parseArgs(args);

deepEqual(result, {
initialFiles: ["file1.json"],
pvms: [SelectedPvm.Ananas, SelectedPvm.Builtin],
accumulateSequentially: true,
});
});

it("should parse --accumulate-sequentially=false as false", () => {
const args = ["--accumulate-sequentially=false", "file1.json"];

const result = parseArgs(args);

deepEqual(result, {
initialFiles: ["file1.json"],
pvms: [SelectedPvm.Ananas, SelectedPvm.Builtin],
accumulateSequentially: false,
});
});

it("should print help with --help", () => {
const args = ["--help"];
const logMock = mock.method(console, "log");
const exitMock = mock.method(process, "exit");

parseArgs(args);

logMock.mock.restore();
exitMock.mock.restore();

assert.strictEqual(exitMock.mock.calls.length, 1);
assert.strictEqual(logMock.mock.calls.length, 1);
const output = logMock.mock.calls[0].arguments[0] as string;
assert.strictEqual(output, HELP_MESSAGE);
});
});
58 changes: 55 additions & 3 deletions bin/test-runner/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export function selectedPvmToBackend(pvm: SelectedPvm): PvmBackend {

export type GlobalsOptions = {
pvms: SelectedPvm[];
accumulateSequentially: boolean;
};

export class RunnerBuilder<T, V> implements Runner<T, V> {
Expand Down Expand Up @@ -95,6 +96,7 @@ export type RunOptions = {
test: TestContext;
chainSpec: ChainSpec;
path: string;
accumulateSequentially: boolean;
};

export type RunFunction<T, V> = (test: T, options: RunOptions, variant: V) => Promise<void>;
Expand Down Expand Up @@ -134,16 +136,60 @@ export namespace testFile {
};
}

const PVM_OPTION = "pvm";
const ACCUMULATE_SEQUENTIALLY_OPTION = "accumulate-sequentially";
const HELP_OPTION = "help";
export const HELP_MESSAGE = `
Usage: test-runner [options] [files...]

Options:
--pvm <value> Select PVM backend(s). Comma-separated list.
Available: ${ALL_PVMS.join(", ")}
Default: all PVMs

--accumulate-sequentially Run accumulation sequentially instead of in parallel.
Default: false

-h, --help Show this help message.

Examples:
test-runner Run all tests with all PVMs
test-runner --pvm ananas Run tests with ananas PVM only
test-runner --accumulate-sequentially Run tests with sequential accumulation
test-runner test.json Run specific test file
`;

export function parseArgs(argv: string[]) {
const PVM_OPTION = "pvm";
const parsed = minimist(argv);
const parsed = minimist(argv, {
boolean: [ACCUMULATE_SEQUENTIALLY_OPTION, HELP_OPTION],
alias: { h: HELP_OPTION },
default: { [ACCUMULATE_SEQUENTIALLY_OPTION]: false },
});

const shouldShowHelp = getBooleanOption(parsed[HELP_OPTION]);

if (shouldShowHelp) {
console.log(HELP_MESSAGE);
process.exit(0);
}

const pvms = getPvms(parsed[PVM_OPTION]);
const accumulateSequentially = getBooleanOption(parsed[ACCUMULATE_SEQUENTIALLY_OPTION]);

return {
initialFiles: parsed._,
pvms,
accumulateSequentially,
};

function getBooleanOption(value: unknown): boolean {
if (value === true) {
return true;
}

return false;
}

function getPvms(parsed: string | undefined): SelectedPvm[] {
const allPvms = ALL_PVMS.slice();

Expand Down Expand Up @@ -171,12 +217,14 @@ export async function main(
{
initialFiles,
pvms,
accumulateSequentially,
patterns = [testFile.bin, testFile.json],
accepted,
ignored,
}: {
initialFiles: string[];
pvms: SelectedPvm[];
accumulateSequentially: boolean;
patterns?: (testFile.bin | testFile.json)[];
accepted?: {
[testFile.bin]?: string[];
Expand Down Expand Up @@ -233,7 +281,10 @@ export async function main(
// 3. If the list is defined, we make sure that the path is on that list.
(accepted[testFileContent.kind] ?? []).some((x) => absolutePath.includes(x));

const testVariants = prepareTest(runners, testFileContent, testFilePath, absolutePath, { pvms });
const testVariants = prepareTest(runners, testFileContent, testFilePath, absolutePath, {
pvms,
accumulateSequentially,
});
for (const test of testVariants) {
test.shouldSkip = !isAccepted;
tests.push(test);
Expand Down Expand Up @@ -412,6 +463,7 @@ function prepareTest<T, V>(
test: ctx,
path: fullPath,
chainSpec,
accumulateSequentially: globalOptions.accumulateSequentially,
},
variant,
);
Expand Down
8 changes: 7 additions & 1 deletion bin/test-runner/state-transition/state-transition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,13 @@ export async function runStateTransition(testContent: StateTransition, options:
}),
);

const stf = new OnChain(spec, preState, hasher, pvm, DbHeaderChain.new(blocksDb));
const stf = new OnChain(
spec,
preState,
hasher,
{ pvm, accumulateSequentially: options.accumulateSequentially },
DbHeaderChain.new(blocksDb),
);

// verify that we compute the state root exactly the same.
assert.deepStrictEqual(testContent.pre_state.state_root.toString(), preStateRoot.toString());
Expand Down
5 changes: 3 additions & 2 deletions bin/test-runner/w3f/accumulate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,11 @@ export class AccumulateTest {

export async function runAccumulateTest(
test: AccumulateTest,
{ chainSpec }: RunOptions,
{ chainSpec, accumulateSequentially }: RunOptions,
variant: "ananas" | "builtin",
) {
const pvm = variant === "ananas" ? PvmBackend.Ananas : PvmBackend.BuiltIn;
const options = { pvm, accumulateSequentially };
/**
* entropy has to be moved to input because state is incompatibile -
* in test state we have: `entropy: EntropyHash;`
Expand All @@ -151,7 +152,7 @@ export async function runAccumulateTest(

const post_state = TestState.toAccumulateState(test.post_state, chainSpec);
const state = TestState.toAccumulateState(test.pre_state, chainSpec);
const accumulate = new Accumulate(chainSpec, await Blake2b.createHasher(), state, pvm);
const accumulate = new Accumulate(chainSpec, await Blake2b.createHasher(), state, options);
const accumulateOutput = new AccumulateOutput();
const result = await accumulate.transition({ ...test.input, entropy });
if (result.isError) {
Expand Down
Loading
Loading