Skip to content

Commit

Permalink
feat: raise errors on invalid input formats (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
TomAFrench authored Jan 6, 2025
1 parent 60c57a6 commit 7e4ddaa
Show file tree
Hide file tree
Showing 9 changed files with 361 additions and 161 deletions.
2 changes: 1 addition & 1 deletion .github/actions/setup/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ runs:
- uses: actions/setup-node@v4
id: node
with:
node-version: 18.19.0
node-version: v20.18.1
cache: 'yarn'
cache-dependency-path: 'yarn.lock'

Expand Down
14 changes: 2 additions & 12 deletions .github/workflows/check-dist.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,8 @@ jobs:
steps:
- uses: actions/checkout@v3

- uses: actions/setup-node@v3
with:
node-version: 16

- name: Node dependencies cache
uses: actions/cache@v3
with:
path: "**/node_modules"
key: yarn-${{ hashFiles('**/yarn.lock') }}

- name: Install dependencies
run: yarn install --frozen-lockfile
- name: Install Yarn dependencies
uses: ./.github/actions/setup

- name: Rebuild the dist/ directory
run: yarn release
Expand Down
123 changes: 112 additions & 11 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -536,7 +536,7 @@ const core = __importStar(__nccwpck_require__(2186));
const github_1 = __nccwpck_require__(5438);
const artifact_1 = __nccwpck_require__(7917);
const program_1 = __nccwpck_require__(1578);
const report_1 = __nccwpck_require__(8269);
const report_1 = __nccwpck_require__(4909);
const token = process.env.GITHUB_TOKEN || core.getInput("token");
const report = core.getInput("report");
const header = core.getInput("header");
Expand Down Expand Up @@ -602,16 +602,20 @@ function run() {
});
}
function loadReports(referenceContent) {
core.startGroup("Load gas reports");
core.info(`Loading gas reports from "${localReportPath}"`);
core.startGroup("Load gate reports");
core.info(`Loading gate reports from "${localReportPath}"`);
const compareContent = fs.readFileSync(localReportPath, "utf8");
core.info(`Mapping compared gas reports`);
core.info(`Mapping compared gate reports`);
const compareReports = (0, report_1.parseReport)(compareContent);
core.info(`Got ${compareReports.programs.length} compare programs`);
core.info(`Mapping reference gas reports`);
core.info(`Mapping reference gate reports`);
const referenceReports = (0, report_1.parseReport)(referenceContent);
core.info(`Got ${compareReports.programs.length} reference programs`);
core.endGroup();
core.startGroup("Print gate reports");
core.info(JSON.stringify(compareReports));
core.info(JSON.stringify(referenceReports));
core.endGroup();
return [referenceReports, compareReports];
}
function formatReport(diffCircuitRows, diffBrilligRows, refCommitHash) {
Expand Down Expand Up @@ -648,17 +652,15 @@ run();

/***/ }),

/***/ 8269:
/***/ ((__unused_webpack_module, exports) => {
/***/ 4909:
/***/ ((__unused_webpack_module, exports, __nccwpck_require__) => {

"use strict";

Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.computeContractDiffs = exports.computeProgramDiffs = exports.computedWorkspaceDiff = exports.variation = exports.parseReport = void 0;
const parseReport = (content) => {
return JSON.parse(content);
};
exports.parseReport = parseReport;
var parsing_1 = __nccwpck_require__(5087);
Object.defineProperty(exports, "parseReport", ({ enumerable: true, get: function () { return parsing_1.parseReport; } }));
const variation = (current, previous) => {
const delta = current - previous;
return {
Expand Down Expand Up @@ -802,6 +804,105 @@ const computeContractDiff = (sourceReport, compareReport) => {
};


/***/ }),

/***/ 5087:
/***/ ((__unused_webpack_module, exports) => {

"use strict";

Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.parseReport = void 0;
const parseReport = (content) => {
var _a, _b, _c, _d;
const report = JSON.parse(content);
report.programs = (_b = (_a = report.programs) === null || _a === void 0 ? void 0 : _a.map(parseProgramReport)) !== null && _b !== void 0 ? _b : [];
report.contracts = (_d = (_c = report.contracts) === null || _c === void 0 ? void 0 : _c.map(parseContractReport)) !== null && _d !== void 0 ? _d : [];
if (isWorkspaceReport(report)) {
return report;
}
else {
console.log(report);
throw Error("Report is invalid");
}
};
exports.parseReport = parseReport;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
function parseProgramReport(report) {
var _a, _b, _c, _d;
report.functions = (_b = (_a = report.functions) === null || _a === void 0 ? void 0 : _a.map(parseCircuitReport)) !== null && _b !== void 0 ? _b : [];
report.unconstrained_functions = (_d = (_c = report.unconstrained_functions) === null || _c === void 0 ? void 0 : _c.map(parseBrilligReport)) !== null && _d !== void 0 ? _d : [];
if (isProgramReport(report)) {
return report;
}
else {
console.log(report);
throw Error(`Program report is invalid: ${report}`);
}
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
function parseContractReport(report) {
var _a, _b;
report.functions = (_b = (_a = report.functions) === null || _a === void 0 ? void 0 : _a.map(parseCircuitReport)) !== null && _b !== void 0 ? _b : [];
if (isContractReport(report)) {
return report;
}
else {
console.log(report);
throw Error(`Contract report is invalid: ${JSON.stringify(report)}`);
}
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
function parseCircuitReport(report) {
// Currently this the wrong key is used so we rename it.
if (typeof report.acir_opcodes !== "undefined" && typeof report.opcodes == "undefined") {
report.opcodes = report.acir_opcodes;
delete report.acir_opcodes;
}
if (isCircuitReport(report)) {
return report;
}
else {
console.log(report);
throw Error("Circuit report is invalid");
}
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
function parseBrilligReport(report) {
if (isBrilligReport(report)) {
return report;
}
else {
console.log(report);
throw Error("Brillig report is invalid");
}
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
function isWorkspaceReport(report) {
return report.programs.every(isProgramReport) && report.contracts.every(isContractReport);
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
function isProgramReport(report) {
return (typeof report.package_name == "string" &&
report.functions.every(isCircuitReport) &&
report.unconstrained_functions.every(isBrilligReport));
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
function isContractReport(report) {
return typeof report.name == "string" && report.functions.every(isCircuitReport);
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
function isCircuitReport(report) {
return (typeof report.name == "string" &&
typeof report.opcodes == "number" &&
typeof report.circuit_size == "number");
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
function isBrilligReport(report) {
return typeof report.name == "string" && typeof report.opcodes == "number";
}


/***/ }),

/***/ 2605:
Expand Down
2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

15 changes: 11 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,19 +97,26 @@ async function run() {
}

function loadReports(referenceContent: string): [WorkspaceReport, WorkspaceReport] {
core.startGroup("Load gas reports");
core.info(`Loading gas reports from "${localReportPath}"`);
core.startGroup("Load gate reports");
core.info(`Loading gate reports from "${localReportPath}"`);
const compareContent = fs.readFileSync(localReportPath, "utf8");

core.info(`Mapping compared gas reports`);
core.info(`Mapping compared gate reports`);
const compareReports = parseReport(compareContent);
core.info(`Got ${compareReports.programs.length} compare programs`);

core.info(`Mapping reference gas reports`);
core.info(`Mapping reference gate reports`);
const referenceReports = parseReport(referenceContent);
core.info(`Got ${compareReports.programs.length} reference programs`);
core.endGroup();

core.startGroup("Print gate reports");

core.info(JSON.stringify(compareReports));
core.info(JSON.stringify(referenceReports));

core.endGroup();

return [referenceReports, compareReports];
}

Expand Down
6 changes: 2 additions & 4 deletions src/report.ts → src/report/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,9 @@ import {
ProgramReport,
BrilligReport,
DiffBrillig,
} from "./types";
} from "../types";

export const parseReport = (content: string): WorkspaceReport => {
return JSON.parse(content);
};
export { parseReport } from "./parsing";

export const variation = (current: number, previous: number) => {
const delta = current - previous;
Expand Down
105 changes: 105 additions & 0 deletions src/report/parsing.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
import {
ContractReport,
CircuitReport,
WorkspaceReport,
ProgramReport,
BrilligReport,
} from "../types";

export const parseReport = (content: string): WorkspaceReport => {
const report = JSON.parse(content);

report.programs = report.programs?.map(parseProgramReport) ?? [];
report.contracts = report.contracts?.map(parseContractReport) ?? [];

if (isWorkspaceReport(report)) {
return report;
} else {
console.log(report);
throw Error("Report is invalid");
}
};

// eslint-disable-next-line @typescript-eslint/no-explicit-any
function parseProgramReport(report: any): ProgramReport {
report.functions = report.functions?.map(parseCircuitReport) ?? [];
report.unconstrained_functions = report.unconstrained_functions?.map(parseBrilligReport) ?? [];

if (isProgramReport(report)) {
return report;
} else {
console.log(report);
throw Error(`Program report is invalid: ${report}`);
}
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
function parseContractReport(report: any): ContractReport {
report.functions = report.functions?.map(parseCircuitReport) ?? [];

if (isContractReport(report)) {
return report;
} else {
console.log(report);
throw Error(`Contract report is invalid: ${JSON.stringify(report)}`);
}
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
function parseCircuitReport(report: any): CircuitReport {
// Currently this the wrong key is used so we rename it.
if (typeof report.acir_opcodes !== "undefined" && typeof report.opcodes == "undefined") {
report.opcodes = report.acir_opcodes;
delete report.acir_opcodes;
}

if (isCircuitReport(report)) {
return report;
} else {
console.log(report);
throw Error("Circuit report is invalid");
}
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
function parseBrilligReport(report: any): BrilligReport {
if (isBrilligReport(report)) {
return report;
} else {
console.log(report);
throw Error("Brillig report is invalid");
}
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
function isWorkspaceReport(report: any): report is WorkspaceReport {
return report.programs.every(isProgramReport) && report.contracts.every(isContractReport);
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
function isProgramReport(report: any): report is ProgramReport {
return (
typeof report.package_name == "string" &&
report.functions.every(isCircuitReport) &&
report.unconstrained_functions.every(isBrilligReport)
);
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
function isContractReport(report: any): report is ContractReport {
return typeof report.name == "string" && report.functions.every(isCircuitReport);
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
function isCircuitReport(report: any): report is CircuitReport {
return (
typeof report.name == "string" &&
typeof report.opcodes == "number" &&
typeof report.circuit_size == "number"
);
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
function isBrilligReport(report: any): report is BrilligReport {
return typeof report.name == "string" && typeof report.opcodes == "number";
}
1 change: 0 additions & 1 deletion tests/mocks/full_gas_report.json

This file was deleted.

Loading

0 comments on commit 7e4ddaa

Please sign in to comment.