-
Notifications
You must be signed in to change notification settings - Fork 0
/
runExecutionTestVectors.ts
72 lines (66 loc) · 2.06 KB
/
runExecutionTestVectors.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import bs58 from "bs58";
import { readFileSync } from "fs";
import { lightClient } from "near-api-js";
import { ExecutionTestVector, getAllJsonFiles } from "./testVector";
function runTestVectors(testVectors: ExecutionTestVector[]): void {
let passed = 0;
let failed = 0;
testVectors.forEach((test, idx) => {
const {
description,
params: { proof, block_merkle_root },
expected: { is_valid, error },
} = test;
let wasValid: boolean;
let executionError: Error | undefined;
try {
lightClient.validateExecutionProof({
proof,
blockMerkleRoot: bs58.decode(block_merkle_root),
});
wasValid = true;
} catch (error) {
wasValid = false;
executionError = error;
}
if (wasValid !== is_valid) {
const prefix = `Test Case at index ${idx} "${description}": FAILED - expected`;
console.log(
`${prefix} ${
is_valid
? `valid, got error ${executionError}`
: `invalid result${error ? ` with error "${error}"` : ""}`
}`
);
failed++;
} else {
console.log(`Test Case ${idx}: PASSED`);
passed++;
}
});
console.log(`\nSummary: ${passed} PASSED, ${failed} FAILED`);
}
const args = process.argv.slice(2);
if (args.length !== 1 && args.length !== 2) {
console.error(
"Usage: ts-node runExecutionTestVectors.ts (--all <directory> | --file <file_path>)"
);
process.exit(1);
}
const [flag, path] = args;
if (flag === "--all") {
const jsonFiles = getAllJsonFiles("./test-vectors/executions");
jsonFiles.forEach((file) => {
console.log("\n\tTesting file: ", file);
const testVectorsJson = readFileSync(file, "utf-8");
const testVectors = JSON.parse(testVectorsJson) as ExecutionTestVector[];
runTestVectors(testVectors);
});
} else if (flag === "--file") {
const testVectorsJson = readFileSync(path, "utf-8");
const testVectors = JSON.parse(testVectorsJson) as ExecutionTestVector[];
runTestVectors(testVectors);
} else {
console.error("Invalid flag. Use --all or --file.");
process.exit(1);
}