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

feat: Validate nargo version against expected one #2254

Merged
merged 1 commit into from
Sep 12, 2023
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
2 changes: 1 addition & 1 deletion yarn-project/noir-compiler/src/cli/contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export function compileContract(program: Command, name = 'contract', log: LogFn

const compile = compileUsingNargo;
log(`Compiling contracts...`);
const result = await compile(projectPath);
const result = await compile(projectPath, { log });

for (const contract of result) {
const artifactPath = resolve(projectPath, outdir, `${contract.name}.json`);
Expand Down
23 changes: 21 additions & 2 deletions yarn-project/noir-compiler/src/compile/nargo.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import { LogFn, createDebugLogger } from '@aztec/foundation/log';

import { execSync } from 'child_process';
import { readFileSync, readdirSync, statSync } from 'fs';
import { emptyDirSync } from 'fs-extra';
import path from 'path';

import { NoirCommit, NoirTag } from '../index.js';
import { NoirCompilationArtifacts, NoirCompiledContract, NoirDebugMetadata } from '../noir_artifact.js';

/** Compilation options */
Expand All @@ -11,13 +14,18 @@ export type CompileOpts = {
quiet?: boolean;
/** Path to the nargo binary. */
nargoBin?: string;
/** Logging function */
log?: LogFn;
};

/**
* A class that compiles noir contracts using nargo via the shell.
*/
export class NargoContractCompiler {
constructor(private projectPath: string, private opts: CompileOpts = {}) {}
private log: LogFn;
constructor(private projectPath: string, private opts: CompileOpts = {}) {
this.log = opts.log ?? createDebugLogger('aztec:noir-compiler');
}

/**
* Compiles the contracts in projectPath and returns the Noir artifact.
Expand All @@ -26,12 +34,23 @@ export class NargoContractCompiler {
public compile(): Promise<NoirCompilationArtifacts[]> {
const stdio = this.opts.quiet ? 'ignore' : 'inherit';
const nargoBin = this.opts.nargoBin ?? 'nargo';
execSync(`${nargoBin} --version`, { cwd: this.projectPath, stdio });
const version = execSync(`${nargoBin} --version`, { cwd: this.projectPath, stdio: 'pipe' }).toString();
this.checkNargoBinVersion(version.replace('\n', ''));
emptyDirSync(this.getTargetFolder());
execSync(`${nargoBin} compile --output-debug `, { cwd: this.projectPath, stdio });
return Promise.resolve(this.collectArtifacts());
}

private checkNargoBinVersion(version: string) {
if (!version.includes(NoirCommit)) {
this.log(
`Warning: the nargo version installed locally does not match the expected one. This may cause issues when compiling or deploying contracts. Consider updating your nargo or aztec-cli installation. \n- Expected: ${NoirTag} (git version hash: ${NoirCommit})\n- Found: ${version}`,
);
} else if (!this.opts.quiet) {
this.log(`Using ${version}`);
}
}

private collectArtifacts(): NoirCompilationArtifacts[] {
const contractArtifacts = new Map<string, NoirCompiledContract>();
const debugArtifacts = new Map<string, NoirDebugMetadata>();
Expand Down
4 changes: 4 additions & 0 deletions yarn-project/noir-compiler/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ import { ContractAbi } from '@aztec/foundation/abi';

import { CompileOpts, NargoContractCompiler } from './compile/nargo.js';
import { generateAztecAbi } from './contract-interface-gen/abi.js';
import NoirVersion from './noir-version.json' assert { type: 'json' };

const { commit: NoirCommit, tag: NoirTag } = NoirVersion;
export { NoirCommit, NoirTag };

export { generateNoirContractInterface } from './contract-interface-gen/noir.js';
export { generateTypescriptContractInterface } from './contract-interface-gen/typescript.js';
Expand Down
4 changes: 4 additions & 0 deletions yarn-project/noir-compiler/src/noir-version.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"tag": "aztec",
"commit": "39e1433a9c4184b72f3c5ab0368cfaa5d4ce537b"
}
2 changes: 1 addition & 1 deletion yarn-project/noir-compiler/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@
"path": "../foundation"
}
],
"include": ["src"]
"include": ["src", "src/*.json"]
}
1 change: 1 addition & 0 deletions yarn-project/noir-contracts/Dockerfile.build
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
FROM ubuntu:jammy

RUN apt-get update && apt-get install -y \
jq \
curl \
git \
sed
Expand Down
8 changes: 8 additions & 0 deletions yarn-project/noir-contracts/scripts/compile.sh
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,15 @@ build() {
nargo compile --output-debug;
}

# Check nargo version matches the expected one
echo "Using $(nargo --version)"
EXPECTED_VERSION=$(jq -r '.commit' ../noir-compiler/src/noir-version.json)
FOUND_VERSION=$(nargo --version | grep -oP 'git version hash: \K[0-9a-f]+')
if [ "$EXPECTED_VERSION" != "$FOUND_VERSION" ]; then
echo "Expected nargo version $EXPECTED_VERSION but found version $FOUND_VERSION. Aborting."
exit 1
fi


# Build contracts
for CONTRACT_NAME in "$@"; do
Expand Down
2 changes: 1 addition & 1 deletion yarn-project/noir-contracts/scripts/install_noir.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# Script to install noirup and the latest aztec nargo
set -eu

VERSION="aztec"
VERSION="${VERSION:-$(jq -r '.tag' ../noir-compiler/src/noir-version.json)}"

# Install nargo
noirup -v $VERSION