Skip to content

Commit

Permalink
refactor: check Aztec.nr version instead of Noir
Browse files Browse the repository at this point in the history
  • Loading branch information
alexghr committed Nov 3, 2023
1 parent c1a34d4 commit 1a919a0
Show file tree
Hide file tree
Showing 21 changed files with 123 additions and 65 deletions.
2 changes: 1 addition & 1 deletion yarn-project/aztec-sandbox/src/bin/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { createAztecNodeRpcServer } from '@aztec/aztec-node';
import { deployInitialSandboxAccounts } from '@aztec/aztec.js';
import { createDebugLogger } from '@aztec/foundation/log';
import { fileURLToPath } from '@aztec/foundation/url';
import { NoirWasmVersion } from '@aztec/noir-compiler/noir-version';
import { NoirWasmVersion } from '@aztec/noir-compiler/versions';
import { createPXERpcServer } from '@aztec/pxe';

import { readFileSync } from 'fs';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -696,5 +696,5 @@
}
],
"events": [],
"compilerVersion": "0.18.0-6ca33a2.aztec"
"aztecNrVersion": ""
}
Original file line number Diff line number Diff line change
Expand Up @@ -684,5 +684,5 @@
}
],
"events": [],
"compilerVersion": "0.18.0-6ca33a2.aztec"
"aztecNrVersion": ""
}
Original file line number Diff line number Diff line change
Expand Up @@ -619,5 +619,5 @@
}
],
"events": [],
"compilerVersion": "0.18.0-6ca33a2.aztec"
"aztecNrVersion": ""
}
6 changes: 4 additions & 2 deletions yarn-project/cli/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import { mnemonicToAccount } from 'viem/accounts';

import { createCompatibleClient } from './client.js';
import { encodeArgs, parseStructString } from './encoding.js';
import { GITHUB_TAG_PREFIX } from './github.js';
import { unboxContract } from './unbox.js';
import { update } from './update/update.js';
import {
Expand Down Expand Up @@ -248,9 +249,10 @@ export function getProgram(log: LogFn, debugLogger: DebugLogger): Command {

const client = await createCompatibleClient(rpcUrl, debugLogger);
const nodeInfo = await client.getNodeInfo();
if (contractArtifact.compilerVersion && contractArtifact.compilerVersion !== nodeInfo.compatibleNargoVersion) {
const expectedAztecNrVersion = `${GITHUB_TAG_PREFIX}-v${nodeInfo.sandboxVersion}`;
if (contractArtifact.aztecNrVersion && contractArtifact.aztecNrVersion !== expectedAztecNrVersion) {
log(
`\nWarning: Contract was compiled with a different version of Noir: ${contractArtifact.compilerVersion}. Consider re-compiling the contract with Noir ${nodeInfo.compatibleNargoVersion}\n`,
`\nWarning: Contract was compiled with a different version of Aztec.nr: ${contractArtifact.aztecNrVersion}. Consider updating Aztec.nr to ${expectedAztecNrVersion}\n`,
);
}

Expand Down
2 changes: 1 addition & 1 deletion yarn-project/foundation/src/abi/abi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ export interface ContractArtifact {
/**
* The version of compiler used to create this artifact
*/
compilerVersion?: string;
aztecNrVersion?: string;

/**
* The functions of the contract.
Expand Down
2 changes: 1 addition & 1 deletion yarn-project/noir-compiler/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"exports": {
".": "./dest/index.js",
"./cli": "./dest/cli/index.js",
"./noir-version": "./dest/noir-version.js"
"./versions": "./dest/versions.js"
},
"typedocOptions": {
"entryPoints": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
exports[`noir-compiler using nargo compiles the test contract 1`] = `
[
{
"aztecNrVersion": undefined,
"events": [],
"functions": [
{
Expand Down Expand Up @@ -195,6 +196,7 @@ export class TestContractContract extends ContractBase {
exports[`noir-compiler using wasm binary compiles the test contract 1`] = `
[
{
"aztecNrVersion": undefined,
"events": [],
"functions": [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { NoirDependencyConfig } from '@aztec/foundation/noir';

import { NoirPackage } from '../package.js';
import { NoirDependencyManager } from './dependency-manager.js';
import { DependencyResolver } from './dependency-resolver.js';
import { Dependency, DependencyResolver } from './dependency-resolver.js';

describe('DependencyManager', () => {
let manager: NoirDependencyManager;
Expand Down Expand Up @@ -51,42 +51,51 @@ describe('DependencyManager', () => {

class TestDependencyResolver implements DependencyResolver {
// eslint-disable-next-line require-await
public async resolveDependency(pkg: NoirPackage, dep: NoirDependencyConfig): Promise<NoirPackage | null> {
public async resolveDependency(pkg: NoirPackage, dep: NoirDependencyConfig): Promise<Dependency | null> {
if (!('path' in dep)) {
return null;
}

switch (dep.path) {
case '/lib1':
return new NoirPackage('/lib1', '/lib1/src', {
dependencies: {},
package: {
name: 'lib1',
type: 'lib',
},
});
return {
version: '',
package: new NoirPackage('/lib1', '/lib1/src', {
dependencies: {},
package: {
name: 'lib1',
type: 'lib',
},
}),
};

case '/lib2':
return new NoirPackage('/lib2', '/lib2/src', {
dependencies: {
lib3: {
path: '/lib3',
return {
version: '',
package: new NoirPackage('/lib2', '/lib2/src', {
dependencies: {
lib3: {
path: '/lib3',
},
},
},
package: {
name: 'lib2',
type: 'lib',
},
});
package: {
name: 'lib2',
type: 'lib',
},
}),
};

case '/lib3':
return new NoirPackage('/lib3', '/lib3/src', {
dependencies: {},
package: {
name: 'lib3',
type: 'lib',
},
});
return {
version: '',
package: new NoirPackage('/lib3', '/lib3/src', {
dependencies: {},
package: {
name: 'lib3',
type: 'lib',
},
}),
};

default:
throw new Error();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import { NoirDependencyConfig } from '@aztec/foundation/noir';
import { join } from 'node:path';

import { NoirPackage } from '../package.js';
import { DependencyResolver } from './dependency-resolver.js';
import { Dependency, DependencyResolver } from './dependency-resolver.js';

/**
* Noir Dependency Resolver
*/
export class NoirDependencyManager {
#entryPoint: NoirPackage;
#libraries = new Map<string, NoirPackage>();
#libraries = new Map<string, Dependency>();
#dependencies = new Map<string, string[]>();
#log: LogFn;
#resolvers: readonly DependencyResolver[];
Expand Down Expand Up @@ -49,6 +49,16 @@ export class NoirDependencyManager {
await this.#recursivelyResolveDependencies('', this.#entryPoint);
}

/**
* Gets the version of a dependency in the dependency tree
* @param name - Dependency name
* @returns The dependency's version
*/
public getVersionOf(name: string): string | undefined {
const dep = this.#libraries.get(name);
return dep?.version;
}

async #recursivelyResolveDependencies(packageName: string, noirPackage: NoirPackage): Promise<void> {
for (const [name, config] of Object.entries(noirPackage.getDependencies())) {
// TODO what happens if more than one package has the same name but different versions?
Expand All @@ -60,20 +70,20 @@ export class NoirDependencyManager {
}

const dependency = await this.#resolveDependency(noirPackage, config);
if (dependency.getType() !== 'lib') {
if (dependency.package.getType() !== 'lib') {
this.#log(`Non-library package ${name}`, config);
throw new Error(`Dependency ${name} is not a library`);
}

this.#libraries.set(name, dependency);
this.#dependencies.set(packageName, [...(this.#dependencies.get(packageName) ?? []), name]);

await this.#recursivelyResolveDependencies(name, dependency);
await this.#recursivelyResolveDependencies(name, dependency.package);
}
}

async #resolveDependency(pkg: NoirPackage, config: NoirDependencyConfig) {
let dependency: NoirPackage | null = null;
async #resolveDependency(pkg: NoirPackage, config: NoirDependencyConfig): Promise<Dependency> {
let dependency: Dependency | null = null;
for (const resolver of this.#resolvers) {
dependency = await resolver.resolveDependency(pkg, config);
if (dependency) {
Expand Down Expand Up @@ -102,9 +112,9 @@ export class NoirDependencyManager {
*/
public findFile(sourceId: string): string | null {
const [lib, ...path] = sourceId.split('/').filter(x => x);
const pkg = this.#libraries.get(lib);
if (pkg) {
return join(pkg.getSrcPath(), ...path);
const dep = this.#libraries.get(lib);
if (dep) {
return join(dep.package.getSrcPath(), ...path);
} else {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@ import { NoirDependencyConfig } from '@aztec/foundation/noir';

import { NoirPackage } from '../package.js';

/**
* A Noir dependency
*/
export type Dependency = {
/** version string as determined by the resolver */
version: string;
/** the actual package source code */
package: NoirPackage;
};

/**
* Resolves a dependency for a package.
*/
Expand All @@ -11,5 +21,5 @@ export interface DependencyResolver {
* @param pkg - The package to resolve dependencies for
* @param dep - The dependency config to resolve
*/
resolveDependency(pkg: NoirPackage, dep: NoirDependencyConfig): Promise<NoirPackage | null>;
resolveDependency(pkg: NoirPackage, dep: NoirDependencyConfig): Promise<Dependency | null>;
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,10 @@ describe('GithubDependencyResolver', () => {

it('resolves Github dependency', async () => {
fetchMock.mockResolvedValueOnce(new Response(await readFile(join(fixtures, 'test_lib.zip')), { status: 200 }));
const libPkg = await resolver.resolveDependency(pkg, libDependency);
expect(libPkg).toBeDefined();
expect(fm.hasFileSync(libPkg!.getEntryPointPath())).toBe(true);
const lib = await resolver.resolveDependency(pkg, libDependency);
expect(lib).toBeDefined();
expect(lib!.version).toEqual(libDependency.tag);
expect(fm.hasFileSync(lib!.package.getEntryPointPath())).toBe(true);
});

it.each<[NoirGitDependencyConfig, 'zip' | 'tar', string]>([
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { unzip } from 'unzipit';

import { FileManager } from '../file-manager/file-manager.js';
import { NoirPackage } from '../package.js';
import { DependencyResolver } from './dependency-resolver.js';
import { Dependency, DependencyResolver } from './dependency-resolver.js';

/**
* Downloads dependencies from github
Expand All @@ -25,7 +25,7 @@ export class GithubDependencyResolver implements DependencyResolver {
* @param dependency - The dependency configuration
* @returns asd
*/
async resolveDependency(_pkg: NoirPackage, dependency: NoirDependencyConfig): Promise<NoirPackage | null> {
async resolveDependency(_pkg: NoirPackage, dependency: NoirDependencyConfig): Promise<Dependency | null> {
// TODO accept ssh urls?
// TODO github authentication?
if (!('git' in dependency) || !dependency.git.startsWith('https://github.com')) {
Expand All @@ -34,7 +34,10 @@ export class GithubDependencyResolver implements DependencyResolver {

const archivePath = await this.#fetchZipFromGithub(dependency);
const libPath = await this.#extractZip(dependency, archivePath);
return NoirPackage.open(libPath, this.#fm);
return {
version: dependency.tag,
package: NoirPackage.open(libPath, this.#fm),
};
}

async #fetchZipFromGithub(dependency: Pick<NoirGitDependencyConfig, 'git' | 'tag'>): Promise<string> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,11 @@ describe('DependencyResolver', () => {
});

it.each(['../test_contract', '/test_contract'])('resolves a known dependency', async path => {
const libPkg = await resolver.resolveDependency(pkg, {
const lib = await resolver.resolveDependency(pkg, {
path,
});
expect(libPkg).toBeDefined();
expect(fm.hasFileSync(libPkg!.getEntryPointPath())).toBe(true);
expect(lib).toBeDefined();
expect(lib!.version).toBe('');
expect(fm.hasFileSync(lib!.package.getEntryPointPath())).toBe(true);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { resolve } from 'path';

import { FileManager } from '../file-manager/file-manager.js';
import { NoirPackage } from '../package.js';
import { DependencyResolver } from './dependency-resolver.js';
import { Dependency, DependencyResolver } from './dependency-resolver.js';

/**
* Resolves dependencies on-disk, relative to current package
Expand All @@ -16,9 +16,13 @@ export class LocalDependencyResolver implements DependencyResolver {
this.#fm = fm;
}

resolveDependency(pkg: NoirPackage, config: NoirDependencyConfig): Promise<NoirPackage | null> {
resolveDependency(pkg: NoirPackage, config: NoirDependencyConfig): Promise<Dependency | null> {
if ('path' in config) {
return Promise.resolve(NoirPackage.open(resolve(pkg.getPackagePath(), config.path), this.#fm));
return Promise.resolve({
// unknown version, Nargo.toml doesn't have a version field
version: '',
package: NoirPackage.open(resolve(pkg.getPackagePath(), config.path), this.#fm),
});
} else {
return Promise.resolve(null);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,15 @@ export class NoirWasmContractCompiler {
return new NoirWasmContractCompiler(noirPackage, dependencyManager, fileManager, opts);
}

/**
* Gets the version of Aztec.nr that was used compiling this contract.
*/
public getResolvedAztecNrVersion() {
// TODO eliminate this hardcoded library name!
// see docs/docs/dev_docs/contracts/setup.md
return this.#dependencyManager.getVersionOf('aztec');
}

/**
* Compiles the project.
*/
Expand Down
1 change: 1 addition & 0 deletions yarn-project/noir-compiler/src/compile/noir/package.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export class NoirPackage {
#packagePath: string;
#srcPath: string;
#config: NoirPackageConfig;
#version: string | null = null;

public constructor(path: string, srcDir: string, config: NoirPackageConfig) {
this.#packagePath = path;
Expand Down
4 changes: 2 additions & 2 deletions yarn-project/noir-compiler/src/contract-interface-gen/abi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ function generateFunctionArtifact(fn: NoirFunctionEntry): FunctionArtifact {
*/
export function generateContractArtifact(
{ contract, debug }: NoirCompilationArtifacts,
compilerVersion: string,
aztecNrVersion?: string,
): ContractArtifact {
const originalFunctions = contract.functions;
// TODO why sort? we should have idempotent compilation so this should not be needed.
Expand All @@ -61,6 +61,6 @@ export function generateContractArtifact(
functions: sortedFunctions.map(generateFunctionArtifact),
events: contract.events,
debug: parsedDebug,
compilerVersion,
aztecNrVersion,
};
}
Loading

0 comments on commit 1a919a0

Please sign in to comment.