Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/parallel tests #52

Merged
merged 5 commits into from
Sep 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ testvectors/commit.bin
testvectors/constants.bin
tmp
pil-config.json
parallel-tests
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,9 @@
"test:counters:padding_pg": "mocha test/counters/padding_pg.js",
"test:counters:poseidon": "mocha test/counters/poseidon.js",
"test:counters:storage": "mocha test/counters/storage.js",
"test:all-inputs": "mocha --timeout 0 --max-old-space-size=8000 tools/run-test/run-inputs-mocha.test.js"
"test:all-inputs": "mocha --timeout 0 --max-old-space-size=8000 tools/run-test/run-inputs-mocha.test.js",
"test:gen-parallel": "node tools/run-test/gen-parallel-tests.js",
"test:run-parallel": "mocha --jobs 2 --timeout 0 --max-old-space-size=8000 --parallel tools/run-test/parallel-tests/*.test.js"
},
"author": "Jordi Baylina",
"license": "UNLICENSED",
Expand Down
7 changes: 4 additions & 3 deletions src/main_executor.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const fileCachePil = path.join(__dirname, "../cache-main-pil.json");

const argv = require("yargs")
.version(version)
.usage("main_executor <input.json> -r <rom.json> -o <proof.json> -t <test.json> -l <logs.json> -s -d [-p <main.pil>] [-P <pilconfig.json>] -u -e -v")
.usage("main_executor <input.json> -r <rom.json> -o <proof.json> -t <test.json> -l <logs.json> -s -d [-p <main.pil>] [-P <pilconfig.json>] -u -e -v -T")
.alias("o", "output")
.alias("r", "rom")
.alias("t", "test")
Expand All @@ -39,8 +39,8 @@ const argv = require("yargs")
.alias("P", "pilconfig")
.alias("u", "unsigned")
.alias("e", "execute")
.alias("P", "pilconfig")
.alias("v", "verbose")
.alias("T", "tracer")
.argv;

async function run() {
Expand Down Expand Up @@ -106,7 +106,8 @@ async function run() {
inputName: path.basename(inputFile, ".json")
},
unsigned: (argv.unsigned === true),
execute: (argv.execute === true)
execute: (argv.execute === true),
tracer: (argv.tracer === true)
}

const N = cmPols.Main.PC.length;
Expand Down
128 changes: 0 additions & 128 deletions src/sm/sm_main/debug/tracer.js

This file was deleted.

22 changes: 7 additions & 15 deletions src/sm/sm_main/sm_main_exec.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ const { byteArray2HexString } = require("@0xpolygonhermez/zkevm-commonjs").utils

const testTools = require("./test_tools");

const Tracer = require("./debug/tracer");
const FullTracer = require("./debug/full-tracer");
const Prints = require("./debug/prints");

Expand All @@ -22,7 +21,6 @@ const twoTo256 = Scalar.shl(Scalar.one, 256);
const Mask256 = Scalar.sub(Scalar.shl(Scalar.e(1), 256), 1);
const byteMaskOn256 = Scalar.bor(Scalar.shl(Mask256, 256), Scalar.shr(Mask256, 8n));

let iTracer;
let fullTracer;

module.exports = async function execute(pols, input, rom, config = {}) {
Expand All @@ -44,6 +42,7 @@ module.exports = async function execute(pols, input, rom, config = {}) {
}

const debug = config && config.debug;
const flagTracer = config && config.tracer;
const N = pols.zkPC.length;
const stepsN = (debug && config.stepsN) ? config.stepsN : N;

Expand Down Expand Up @@ -85,12 +84,10 @@ module.exports = async function execute(pols, input, rom, config = {}) {

preprocessTxs(ctx);

if (debug && config.debugInfo) {
iTracer = new Tracer(config.debugInfo.inputName);
if (debug && flagTracer) {
fullTracer = new FullTracer(config.debugInfo.inputName)
} else {
iTracer = null
}

const iPrint = new Prints(ctx, smt);
let fastDebugExit = false;

Expand Down Expand Up @@ -139,9 +136,6 @@ module.exports = async function execute(pols, input, rom, config = {}) {
// console.log("### > "+l.fileName + ':' + l.line);
}

if (iTracer)
await iTracer.getTrace(ctx, l, false);

if (l.cmdBefore) {
for (let j=0; j< l.cmdBefore.length; j++) {
evalCommand(ctx, l.cmdBefore[j]);
Expand Down Expand Up @@ -750,7 +744,6 @@ module.exports = async function execute(pols, input, rom, config = {}) {
(!Fr.eq(ctx.A[6], op6)) ||
(!Fr.eq(ctx.A[7], op7))
) {
if (iTracer) iTracer.exportTrace();
throw new Error(`Assert does not match: ${ctx.ln} at ${ctx.fileName}:${ctx.line} (op:${fea2scalar(Fr, [op0, op1, op2, op3, op4, op5, op6, op7])} A:${fea2scalar(Fr, ctx.A)})`);
}
pols.assert[i] = 1n;
Expand Down Expand Up @@ -1717,9 +1710,6 @@ module.exports = async function execute(pols, input, rom, config = {}) {
checkFinalState(Fr, pols);
}

if (iTracer)
iTracer.exportTrace();

for (let i=0; i<ctx.hashK.length; i++) {
const h = {
data: ctx.hashK[i].data,
Expand Down Expand Up @@ -2295,15 +2285,17 @@ function eval_getBytecode(ctx, tag) {
if (tag.params.length != 2 && tag.params.length != 3) throw new Error(`Invalid number of parameters function ${tag.funcName}: ${ctx.ln}`)
let hashcontract = evalCommand(ctx, tag.params[0]);
hashcontract = "0x" + hashcontract.toString(16).padStart(64, '0');
const bytecode = ctx.input.contractsBytecode[hashcontract];
const bytecode = ctx.input.contractsBytecode[hashcontract] || ctx.input.contractsBytecode[hashcontract.slice(2)];
const offset = Number(evalCommand(ctx, tag.params[1]));
let len;
if (tag.params[2])
len = Number(evalCommand(ctx, tag.params[2]));
else
len = 1;
if (bytecode === undefined) return scalar2fea(ctx.Fr, Scalar.e(0));
let d = "0x" + bytecode.slice(2 + offset * 2, 2 + offset * 2 + len * 2);
// TODO: handle "0x"
const offset0x = bytecode.startsWith('0x') ? 2 : 0;
let d = "0x" + bytecode.slice(offset0x + offset * 2, offset0x + offset * 2 + len * 2);
if (d.length == 2) d = d + '0';
const ret = scalar2fea(ctx.Fr, Scalar.e(d));
return scalar2fea(ctx.Fr, Scalar.e(d));
Expand Down
6 changes: 6 additions & 0 deletions tools/run-test/checker.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/sh
FILE=$PWD/tools/run-test/parallel-tests/checker.txt
if [ -f "$FILE" ]; then
cat "$FILE"
exit 1
fi
61 changes: 61 additions & 0 deletions tools/run-test/gen-parallel-tests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@

const chai = require("chai");
const expect = chai.expect;
const { compile } = require("pilcom");
const folderPaths = ["../../../zkevm-testvectors/inputs-executor", "../../../zkevm-testvectors/tools/ethereum-tests/GeneralStateTests"];
const buildPoseidon = require("@0xpolygonhermez/zkevm-commonjs").getPoseidon;
const fs = require("fs");
const path = require("path");
const fileCachePil = path.join(__dirname, "../../cache-main-pil.json");
const inputs = [];
const testsFolder = path.join(__dirname, "parallel-tests");
const sampleDir = path.join(__dirname, "sample.test.js");

async function main() {

const poseidon = await buildPoseidon();
const F = poseidon.F;
for (const folder of folderPaths) {
let inputsPath = path.join(__dirname, folder);
fs.readdirSync(inputsPath).forEach(file => {
const filePath = path.join(inputsPath, file);
if (file.endsWith(".json")) {
inputs.push(filePath);
} else {
if (fs.statSync(filePath).isDirectory()) {
fs.readdirSync(filePath).forEach(subFile => {
const subFilePath = path.join(filePath, subFile);
if (subFile.endsWith(".json"))
inputs.push(subFilePath);
});
}
}
});
}

const pilConfig = {
defines: { N: 4096 },
namespaces: ['Main', 'Global']
};

const pil = await compile(F, "pil/main.pil", null, pilConfig);
fs.writeFileSync(fileCachePil, JSON.stringify(pil, null, 1) + "\n", "utf8");
genTestsFiles();

}

async function genTestsFiles() {

if (!fs.existsSync(testsFolder)){
fs.mkdirSync(testsFolder);
}
for (const inputPath of inputs) {
const name = inputPath.split("/").slice(-1)[0].replace('json','test.js');
const sample = fs.readFileSync(sampleDir, "utf-8");
const test = sample.replace("\"%%INPUT_PATH%%\"", `"${inputPath}"`);
fs.writeFileSync(`${testsFolder}/${name}`, test);
}
expect(true).to.be.true
}

main();
15 changes: 11 additions & 4 deletions tools/run-test/run-inputs-mocha.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const chalk = require("chalk");
describe("Run executor inputs from config file", () => {
let cmPols;
const inputs = [];
let pilConfig;

before(async () => {
const poseidon = await buildPoseidon();
Expand All @@ -35,17 +36,21 @@ describe("Run executor inputs from config file", () => {
}
});
}
const pilConfig = {
defines: { N: 2 ** 21 },

pilConfig = {
defines: { N: 4096 },
namespaces: ['Main', 'Global']
};

const pil = await compile(F, "pil/main.pil", null, pilConfig);
fs.writeFileSync(fileCachePil, JSON.stringify(pil, null, 1) + "\n", "utf8");

cmPols = newCommitPolsArray(pil);
});

it("Print info", async () => {
console.log("CONFIG PIL:");
console.log(pilConfig);
});

it('Should run all the inputs', async () => {
Expand All @@ -61,8 +66,10 @@ describe("Run executor inputs from config file", () => {
debug: true,
debugInfo: {
inputName: path.basename(inputPath)
}
},
stepsN: 8388608
}
console.log("Running test: ", inputPath);
await smMain.execute(cmPols.Main, input, rom, config);
const stopTime = performance.now();
info += `${chalk.green(`Finish executor JS ==> ${(stopTime - startTime) / 1000} s\n`)}`;
Expand Down
5 changes: 3 additions & 2 deletions tools/run-test/run-inputs.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ async function main(){
pil = JSON.parse(await fs.promises.readFile(fileCachePil, "utf8"));
} else {
const pilConfig = {
defines: {N: 2 ** 21},
defines: { N: 4096 },
namespaces: ['Main', 'Global']
};

Expand Down Expand Up @@ -103,7 +103,8 @@ async function main(){
debug: true,
debugInfo: {
inputName: path.basename(fileName)
}
},
stepsN: 8388608
}
await smMain.execute(cmPols.Main, input, rom, config);
const stopTime = performance.now();
Expand Down
Loading