Skip to content

Commit

Permalink
Fix wasm test
Browse files Browse the repository at this point in the history
  • Loading branch information
spalladino committed Jan 12, 2024
1 parent e96af69 commit e608bc4
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 8 deletions.
4 changes: 4 additions & 0 deletions noir/compiler/wasm/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
"@types/mocha": "^10.0.6",
"@types/mocha-each": "^2",
"@types/node": "^20.10.5",
"@types/pako": "^2",
"@types/path-browserify": "^1",
"@types/readable-stream": "^4",
"@types/sinon": "^17",
Expand Down Expand Up @@ -75,5 +76,8 @@
"url": "^0.11.3",
"webpack": "^5.49.0",
"webpack-cli": "^4.7.2"
},
"dependencies": {
"pako": "^2.1.0"
}
}
72 changes: 64 additions & 8 deletions noir/compiler/wasm/test/compiler/node/compile_with_deps.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { join, resolve } from 'path';
import { getPaths } from '../../shared';

import { inflate } from 'pako';
import { expect } from 'chai';
import { readFile } from 'fs/promises';
import { compile, createFileManager } from '@noir-lang/noir_wasm';
import { CompiledContract } from '../../../src/types/noir_artifact';
import { CompiledContract, DebugFileMap, DebugInfo } from '../../../src/types/noir_artifact';
import { ContractCompilationArtifacts, NoirFunctionEntry } from 'dist/types/src/types/noir_artifact';

const basePath = resolve(join(__dirname, '../../'));
const { contractProjectPath, contractExpectedArtifact } = getPaths(basePath);
Expand All @@ -13,14 +15,68 @@ describe('noir-compiler', () => {
it('both nargo and noir_wasm should compile identically', async () => {
const fm = createFileManager(contractProjectPath);
const nargoArtifact = JSON.parse((await readFile(contractExpectedArtifact)).toString()) as CompiledContract;
nargoArtifact.functions.sort((a, b) => a.name.localeCompare(b.name));
const [nargoDebugInfos, nargoFileMap] = deleteDebugMetadata(nargoArtifact);
normalizeVersion(nargoArtifact);

const noirWasmArtifact = await compile(fm);
if (!('contract' in noirWasmArtifact)) {
throw new Error('Compilation failed');
}
const noirWasmContract = noirWasmArtifact.contract;
const noirWasmContract = (noirWasmArtifact as ContractCompilationArtifacts).contract;
expect(noirWasmContract).not.to.be.undefined;
noirWasmContract.functions.sort((a, b) => a.name.localeCompare(b.name));
const [noirWasmDebugInfos, norWasmFileMap] = deleteDebugMetadata(noirWasmContract);
normalizeVersion(noirWasmContract);

// We first compare both contracts without considering debug info
expect(nargoArtifact).to.deep.eq(noirWasmContract);
});

// Compare the file maps, ignoring keys, since those depend in the order in which files are visited,
// which may change depending on the file manager implementation. Also ignores paths, since the base
// path is reported differently between nargo and noir-wasm.
expect(getSources(nargoFileMap)).to.have.members(getSources(norWasmFileMap));

// Compare the debug symbol information, ignoring the actual ids used for file identifiers.
// Debug symbol info looks like the following, what we need is to ignore the 'file' identifiers
// {"locations":{"0":[{"span":{"start":141,"end":156},"file":39},{"span":{"start":38,"end":76},"file":38},{"span":{"start":824,"end":862},"file":23}]}}
expect(nargoDebugInfos).to.deep.eq(noirWasmDebugInfos);
}).timeout(5000);
});

/** Remove commit identifier from version, which may not match depending on cached nargo and noir-wasm */
function normalizeVersion(contract: CompiledContract) {
contract.noir_version = contract.noir_version.replace(/\+.+$/, '');
}

/** Decompresses and decodes the debug symbols */
function inflateDebugSymbols(debugSymbols: string) {
return JSON.parse(inflate(Buffer.from(debugSymbols, 'base64'), { to: 'string', raw: true }));
}

/** Extracts the debug symbols from all functions, decodes them, removes their file identifiers, and deletes them from the artifact. */
function extractDebugInfos(fns: NoirFunctionEntry[]) {
return fns.map((fn) => {
const debugSymbols = inflateDebugSymbols(fn.debug_symbols);
delete (fn as Partial<NoirFunctionEntry>).debug_symbols;
clearFileIdentifiers(debugSymbols);
return debugSymbols;
});
}

/** Deletes all debug info from a contract and returns it. */
function deleteDebugMetadata(contract: CompiledContract) {
contract.functions.sort((a, b) => a.name.localeCompare(b.name));
const fileMap = contract.file_map;
delete (contract as Partial<CompiledContract>).file_map;
return [extractDebugInfos(contract.functions), fileMap];
}

/** Clears file identifiers from a set of debug symbols. */
function clearFileIdentifiers(debugSymbols: DebugInfo) {
for (const loc of Object.values(debugSymbols.locations)) {
for (const span of loc) {
span.file = 0;
}
}
}

/** Returns list of sources from file map, dropping paths along the way, since they don't match depending on the file manager. */
function getSources(fileMap: DebugFileMap) {
return Object.values(fileMap).map((file) => file.source);
}
16 changes: 16 additions & 0 deletions noir/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4530,6 +4530,7 @@ __metadata:
"@types/mocha": ^10.0.6
"@types/mocha-each": ^2
"@types/node": ^20.10.5
"@types/pako": ^2
"@types/path-browserify": ^1
"@types/readable-stream": ^4
"@types/sinon": ^17
Expand All @@ -4546,6 +4547,7 @@ __metadata:
memfs: ^4.6.0
mocha: ^10.2.0
mocha-each: ^2.0.1
pako: ^2.1.0
path-browserify: ^1.0.1
process: ^0.11.10
readable-stream: ^4.4.2
Expand Down Expand Up @@ -6197,6 +6199,13 @@ __metadata:
languageName: node
linkType: hard

"@types/pako@npm:^2":
version: 2.0.3
resolution: "@types/pako@npm:2.0.3"
checksum: 0746dd5d29eccf5b2e6cceb3ccb093851219e78bd2e2e20d25757e247987139e061e5d4ba37cb5295493f06e3c683c74f8876011cd8a3f3748a09244fbc841d9
languageName: node
linkType: hard

"@types/parse-json@npm:^4.0.0":
version: 4.0.2
resolution: "@types/parse-json@npm:4.0.2"
Expand Down Expand Up @@ -17099,6 +17108,13 @@ __metadata:
languageName: node
linkType: hard

"pako@npm:^2.1.0":
version: 2.1.0
resolution: "pako@npm:2.1.0"
checksum: 71666548644c9a4d056bcaba849ca6fd7242c6cf1af0646d3346f3079a1c7f4a66ffec6f7369ee0dc88f61926c10d6ab05da3e1fca44b83551839e89edd75a3e
languageName: node
linkType: hard

"param-case@npm:^3.0.4":
version: 3.0.4
resolution: "param-case@npm:3.0.4"
Expand Down

0 comments on commit e608bc4

Please sign in to comment.