Skip to content

Commit

Permalink
feat: Validate nargo version against expected one (#2254)
Browse files Browse the repository at this point in the history
Adds a `noir-version.json` file in the `noir-compiler` project, which
pins what is the expected nargo tag and commit hash to be used for
Aztec.

- When installing nargo in the CI, the `tag` is used as the argument for
`noirup`.
- When using the noir compiler from the CLI or programmatically, a
warning is emitted if the current nargo version does not match the
expected one in the json file.
- When building contracts using `yarn noir:build`, an error is thrown if
the version does not match.

The `noir-version.json` file is reached by the `noir-complier` rebuild
patterns, which is a build-system dependency of `noir-contracts`, which
means that all contracts in the repo should get recompiled when we bump
to a new version of Noir. Fixes #2185.

---

This also allows us to **start versioning the aztec noir releases**
rather than having a moving tag that potentially breaks master (and any
open branches) when we update to a breaking release. To make the most of
out this, I believe we should publish each aztec release as both `aztec`
(so it's easy for end users to just install that version when they are
starting) and a specific tag like `aztec-0.7.0` that we refer to in the
`noir-version`.

Alternatively, we can drop the `aztec` tag altogether, and either 1)
automatically update our docs to reflect the tag listed in
`noir-verison.json` in the install instructions (requires a smarter
`include_code` macro) or 2) have the aztec-cli run `noirup` on its own
to install the version required.
  • Loading branch information
spalladino authored Sep 12, 2023
1 parent 71da236 commit 011c0b7
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 5 deletions.
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

0 comments on commit 011c0b7

Please sign in to comment.