From 67a4ca0f373567ebe7f8de910929c0dca3b6b8e8 Mon Sep 17 00:00:00 2001 From: thunkar Date: Wed, 17 Jul 2024 14:51:36 +0000 Subject: [PATCH 01/14] more refactor --- yarn-project/aztec/src/bin/index.ts | 4 +-- yarn-project/builder/Dockerfile | 2 +- yarn-project/builder/package.json | 5 ++-- yarn-project/builder/src/{ => bin}/cli.ts | 5 ++-- .../src}/contract-interface-gen/codegen.ts | 0 .../src}/contract-interface-gen/typescript.ts | 0 yarn-project/builder/src/index.ts | 16 ++++++++++ yarn-project/cli/package.json | 1 - yarn-project/cli/src/cmds/builder/codegen.ts | 8 ----- yarn-project/cli/src/cmds/builder/index.ts | 30 ------------------- yarn-project/cli/src/cmds/utils/index.ts | 12 ++++++++ .../cli/src/cmds/{builder => utils}/update.ts | 0 .../cmds/{builder => utils}/update/common.ts | 0 .../cmds/{builder => utils}/update/github.ts | 0 .../cmds/{builder => utils}/update/noir.ts | 0 .../src/cmds/{builder => utils}/update/npm.ts | 0 .../cmds/{builder => utils}/update/utils.ts | 0 17 files changed, 36 insertions(+), 47 deletions(-) rename yarn-project/builder/src/{ => bin}/cli.ts (83%) rename yarn-project/{cli/src/cmds/builder => builder/src}/contract-interface-gen/codegen.ts (100%) rename yarn-project/{cli/src/cmds/builder => builder/src}/contract-interface-gen/typescript.ts (100%) create mode 100644 yarn-project/builder/src/index.ts delete mode 100644 yarn-project/cli/src/cmds/builder/codegen.ts delete mode 100644 yarn-project/cli/src/cmds/builder/index.ts rename yarn-project/cli/src/cmds/{builder => utils}/update.ts (100%) rename yarn-project/cli/src/cmds/{builder => utils}/update/common.ts (100%) rename yarn-project/cli/src/cmds/{builder => utils}/update/github.ts (100%) rename yarn-project/cli/src/cmds/{builder => utils}/update/noir.ts (100%) rename yarn-project/cli/src/cmds/{builder => utils}/update/npm.ts (100%) rename yarn-project/cli/src/cmds/{builder => utils}/update/utils.ts (100%) diff --git a/yarn-project/aztec/src/bin/index.ts b/yarn-project/aztec/src/bin/index.ts index 5a2835bbfa4c..26a96b7f89e2 100644 --- a/yarn-project/aztec/src/bin/index.ts +++ b/yarn-project/aztec/src/bin/index.ts @@ -1,5 +1,5 @@ import { fileURLToPath } from '@aztec/aztec.js'; -import { injectCommands as injectBuilderCommands } from '@aztec/cli/builder'; +import { injectCommands as injectBuilderCommands } from '@aztec/builder'; import { injectCommands as injectContractCommands } from '@aztec/cli/contracts'; import { injectCommands as injectInfrastructureCommands } from '@aztec/cli/infrastructure'; import { injectCommands as injectL1Commands } from '@aztec/cli/l1'; @@ -23,7 +23,7 @@ async function main() { let program = new Command('aztec'); program.description('Aztec command line interface').version(cliVersion); program = injectAztecCommands(program, userLog, debugLogger); - program = injectBuilderCommands(program, userLog); + program = injectBuilderCommands(program); program = injectContractCommands(program, userLog, debugLogger); program = injectInfrastructureCommands(program, userLog, debugLogger); program = injectL1Commands(program, userLog, debugLogger); diff --git a/yarn-project/builder/Dockerfile b/yarn-project/builder/Dockerfile index 086573488ef6..daf29bc9b04c 100644 --- a/yarn-project/builder/Dockerfile +++ b/yarn-project/builder/Dockerfile @@ -1,5 +1,5 @@ FROM aztecprotocol/yarn-project AS yarn-project -ENTRYPOINT ["node", "--no-warnings", "/usr/src/yarn-project/builder/dest/cli.js"] +ENTRYPOINT ["node", "--no-warnings", "/usr/src/yarn-project/builder/dest/bin/cli.js"] # The version has been updated in yarn-project. # Adding COMMIT_TAG here to rebuild versioned image. diff --git a/yarn-project/builder/package.json b/yarn-project/builder/package.json index a94c6252f895..7b042e1c3732 100644 --- a/yarn-project/builder/package.json +++ b/yarn-project/builder/package.json @@ -4,7 +4,7 @@ "type": "module", "exports": { ".": "./dest/index.js", - "./cli": "./dest/cli/index.js" + "./cli": "./dest/bin/cli.js" }, "typedocOptions": { "entryPoints": [ @@ -14,7 +14,7 @@ "tsconfig": "./tsconfig.json" }, "bin": { - "aztec-builder": "dest/cli.js" + "aztec-builder": "dest/bin/cli.js" }, "scripts": { "build": "yarn clean && tsc -b", @@ -65,7 +65,6 @@ ] }, "dependencies": { - "@aztec/cli": "workspace:^", "@aztec/foundation": "workspace:^", "commander": "^12.1.0" }, diff --git a/yarn-project/builder/src/cli.ts b/yarn-project/builder/src/bin/cli.ts similarity index 83% rename from yarn-project/builder/src/cli.ts rename to yarn-project/builder/src/bin/cli.ts index 0bbe358b53fd..2e87e6dcec87 100644 --- a/yarn-project/builder/src/cli.ts +++ b/yarn-project/builder/src/bin/cli.ts @@ -1,15 +1,16 @@ #!/usr/bin/env node -import { injectCommands as injectBuilderCommands } from '@aztec/cli/builder'; import { createConsoleLogger } from '@aztec/foundation/log'; import { Command } from 'commander'; +import { injectCommands as injectBuilderCommands } from '../index.js'; + const log = createConsoleLogger('aztec:builder'); const main = async () => { const program = new Command('aztec-builder'); - injectBuilderCommands(program, log); + injectBuilderCommands(program); await program.parseAsync(process.argv); // I force exit here because spawnSync in npm.ts just blocks the process from exiting. Spent a bit of time debugging // it without success and I think it doesn't make sense to invest more time in this. diff --git a/yarn-project/cli/src/cmds/builder/contract-interface-gen/codegen.ts b/yarn-project/builder/src/contract-interface-gen/codegen.ts similarity index 100% rename from yarn-project/cli/src/cmds/builder/contract-interface-gen/codegen.ts rename to yarn-project/builder/src/contract-interface-gen/codegen.ts diff --git a/yarn-project/cli/src/cmds/builder/contract-interface-gen/typescript.ts b/yarn-project/builder/src/contract-interface-gen/typescript.ts similarity index 100% rename from yarn-project/cli/src/cmds/builder/contract-interface-gen/typescript.ts rename to yarn-project/builder/src/contract-interface-gen/typescript.ts diff --git a/yarn-project/builder/src/index.ts b/yarn-project/builder/src/index.ts new file mode 100644 index 000000000000..dd19f3411ddf --- /dev/null +++ b/yarn-project/builder/src/index.ts @@ -0,0 +1,16 @@ +import { Command } from 'commander'; +import { dirname } from 'path'; + +export function injectCommands(program: Command) { + program + .command('codegen') + .argument('', 'Path to the Noir ABI or project dir.') + .option('-o, --outdir ', 'Output folder for the generated code.') + .option('--force', 'Force code generation even when the contract has not changed.') + .description('Validates and generates an Aztec Contract ABI from Noir ABI.') + .action(async (noirAbiPath: string, { outdir, force }) => { + const { generateCode } = await import('./contract-interface-gen/codegen.js'); + generateCode(outdir || dirname(noirAbiPath), noirAbiPath, { force }); + }); + return program; +} diff --git a/yarn-project/cli/package.json b/yarn-project/cli/package.json index cb1ada4c7f08..d60d222ed5e4 100644 --- a/yarn-project/cli/package.json +++ b/yarn-project/cli/package.json @@ -3,7 +3,6 @@ "version": "0.32.0", "type": "module", "exports": { - "./builder": "./dest/cmds/builder/index.js", "./contracts": "./dest/cmds/contracts/index.js", "./infrastructure": "./dest/cmds/infrastructure/index.js", "./l1": "./dest/cmds/l1/index.js", diff --git a/yarn-project/cli/src/cmds/builder/codegen.ts b/yarn-project/cli/src/cmds/builder/codegen.ts deleted file mode 100644 index d721b5da63ef..000000000000 --- a/yarn-project/cli/src/cmds/builder/codegen.ts +++ /dev/null @@ -1,8 +0,0 @@ -import { type GenerateCodeOptions, generateCode } from './contract-interface-gen/codegen.js'; - -/** - * Generates Noir interface or Typescript interface for a folder or single file from a Noir compilation artifact. - */ -export function codegen(outputPath: string, fileOrDirPath: string, opts: GenerateCodeOptions = {}) { - generateCode(outputPath, fileOrDirPath, opts); -} diff --git a/yarn-project/cli/src/cmds/builder/index.ts b/yarn-project/cli/src/cmds/builder/index.ts deleted file mode 100644 index 971a63a22a5f..000000000000 --- a/yarn-project/cli/src/cmds/builder/index.ts +++ /dev/null @@ -1,30 +0,0 @@ -import { type LogFn } from '@aztec/foundation/log'; - -import { type Command } from 'commander'; -import { dirname } from 'path'; - -export function injectCommands(program: Command, log: LogFn) { - program - .command('codegen') - .argument('', 'Path to the Noir ABI or project dir.') - .option('-o, --outdir ', 'Output folder for the generated code.') - .option('--force', 'Force code generation even when the contract has not changed.') - .description('Validates and generates an Aztec Contract ABI from Noir ABI.') - .action(async (noirAbiPath: string, { outdir, force }) => { - const { codegen } = await import('./codegen.js'); - codegen(outdir || dirname(noirAbiPath), noirAbiPath, { force }); - }); - - program - .command('update') - .description('Updates Nodejs and Noir dependencies') - .argument('[projectPath]', 'Path to the project directory', process.cwd()) - .option('--contract [paths...]', 'Paths to contracts to update dependencies', []) - .option('--aztec-version ', 'The version to update Aztec packages to. Defaults to latest', 'latest') - .action(async (projectPath: string, options) => { - const { updateProject } = await import('./update.js'); - const { contract, aztecVersion } = options; - await updateProject(projectPath, contract, aztecVersion, log); - }); - return program; -} diff --git a/yarn-project/cli/src/cmds/utils/index.ts b/yarn-project/cli/src/cmds/utils/index.ts index 55b90bfd9272..ede4a816e1ab 100644 --- a/yarn-project/cli/src/cmds/utils/index.ts +++ b/yarn-project/cli/src/cmds/utils/index.ts @@ -43,5 +43,17 @@ export function injectCommands(program: Command, log: LogFn) { computeSelector(functionSignature, log); }); + program + .command('update') + .description('Updates Nodejs and Noir dependencies') + .argument('[projectPath]', 'Path to the project directory', process.cwd()) + .option('--contract [paths...]', 'Paths to contracts to update dependencies', []) + .option('--aztec-version ', 'The version to update Aztec packages to. Defaults to latest', 'latest') + .action(async (projectPath: string, options) => { + const { updateProject } = await import('../utils/update.js'); + const { contract, aztecVersion } = options; + await updateProject(projectPath, contract, aztecVersion, log); + }); + return program; } diff --git a/yarn-project/cli/src/cmds/builder/update.ts b/yarn-project/cli/src/cmds/utils/update.ts similarity index 100% rename from yarn-project/cli/src/cmds/builder/update.ts rename to yarn-project/cli/src/cmds/utils/update.ts diff --git a/yarn-project/cli/src/cmds/builder/update/common.ts b/yarn-project/cli/src/cmds/utils/update/common.ts similarity index 100% rename from yarn-project/cli/src/cmds/builder/update/common.ts rename to yarn-project/cli/src/cmds/utils/update/common.ts diff --git a/yarn-project/cli/src/cmds/builder/update/github.ts b/yarn-project/cli/src/cmds/utils/update/github.ts similarity index 100% rename from yarn-project/cli/src/cmds/builder/update/github.ts rename to yarn-project/cli/src/cmds/utils/update/github.ts diff --git a/yarn-project/cli/src/cmds/builder/update/noir.ts b/yarn-project/cli/src/cmds/utils/update/noir.ts similarity index 100% rename from yarn-project/cli/src/cmds/builder/update/noir.ts rename to yarn-project/cli/src/cmds/utils/update/noir.ts diff --git a/yarn-project/cli/src/cmds/builder/update/npm.ts b/yarn-project/cli/src/cmds/utils/update/npm.ts similarity index 100% rename from yarn-project/cli/src/cmds/builder/update/npm.ts rename to yarn-project/cli/src/cmds/utils/update/npm.ts diff --git a/yarn-project/cli/src/cmds/builder/update/utils.ts b/yarn-project/cli/src/cmds/utils/update/utils.ts similarity index 100% rename from yarn-project/cli/src/cmds/builder/update/utils.ts rename to yarn-project/cli/src/cmds/utils/update/utils.ts From e17624bdd251299008b772c5406e119b3c1c6292 Mon Sep 17 00:00:00 2001 From: thunkar Date: Wed, 17 Jul 2024 14:51:42 +0000 Subject: [PATCH 02/14] fix --- yarn-project/builder/aztec-builder-dest | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/yarn-project/builder/aztec-builder-dest b/yarn-project/builder/aztec-builder-dest index c5b74ae02dfa..3a0f43e3061e 100755 --- a/yarn-project/builder/aztec-builder-dest +++ b/yarn-project/builder/aztec-builder-dest @@ -1,3 +1,3 @@ #!/bin/sh SCRIPT_PATH=$(dirname $(realpath $0)) -node --no-warnings $SCRIPT_PATH/dest/cli.js $@ +node --no-warnings $SCRIPT_PATH/dest/bin/cli.js $@ From fbaa265a11ed580b42184e2681f7f0aa4d3d8359 Mon Sep 17 00:00:00 2001 From: thunkar Date: Thu, 18 Jul 2024 15:11:48 +0000 Subject: [PATCH 03/14] wip --- yarn-project/builder/package.json | 1 + yarn-project/builder/tsconfig.json | 3 --- yarn-project/noir-contracts.js/scripts/generate-types.sh | 2 +- yarn-project/txe/src/index.ts | 7 ++++++- yarn-project/txe/src/txe_service/txe_service.ts | 5 +---- yarn-project/yarn.lock | 4 ++-- 6 files changed, 11 insertions(+), 11 deletions(-) diff --git a/yarn-project/builder/package.json b/yarn-project/builder/package.json index 7b042e1c3732..acde8d5092b1 100644 --- a/yarn-project/builder/package.json +++ b/yarn-project/builder/package.json @@ -66,6 +66,7 @@ }, "dependencies": { "@aztec/foundation": "workspace:^", + "@aztec/types": "workspace:^", "commander": "^12.1.0" }, "devDependencies": { diff --git a/yarn-project/builder/tsconfig.json b/yarn-project/builder/tsconfig.json index 471eb519c823..a2d81aa678d5 100644 --- a/yarn-project/builder/tsconfig.json +++ b/yarn-project/builder/tsconfig.json @@ -6,9 +6,6 @@ "tsBuildInfoFile": ".tsbuildinfo" }, "references": [ - { - "path": "../cli" - }, { "path": "../foundation" } diff --git a/yarn-project/noir-contracts.js/scripts/generate-types.sh b/yarn-project/noir-contracts.js/scripts/generate-types.sh index 8c1168b24e81..26a7eb20560f 100755 --- a/yarn-project/noir-contracts.js/scripts/generate-types.sh +++ b/yarn-project/noir-contracts.js/scripts/generate-types.sh @@ -37,7 +37,7 @@ for ABI in $(find ../../noir-projects/noir-contracts/target -maxdepth 1 -type f done # Generate types for the contracts -node --no-warnings ../builder/dest/cli.js codegen -o $OUT_DIR artifacts +node --no-warnings ../builder/dest/bin/cli.js codegen -o $OUT_DIR artifacts # Append exports for each generated TypeScript file to index.ts find "$OUT_DIR" -maxdepth 1 -type f -name '*.ts' ! -name 'index.ts' | while read -r TS_FILE; do diff --git a/yarn-project/txe/src/index.ts b/yarn-project/txe/src/index.ts index 4bd804b1332f..a712ee457cd4 100644 --- a/yarn-project/txe/src/index.ts +++ b/yarn-project/txe/src/index.ts @@ -3,7 +3,7 @@ import { JsonRpcServer } from '@aztec/foundation/json-rpc/server'; import { type Logger } from '@aztec/foundation/log'; import { TXEService } from './txe_service/txe_service.js'; -import { type ForeignCallResult, toForeignCallResult } from './util/encoding.js'; +import { ForeignCallArray, type ForeignCallResult, fromArray, toForeignCallResult } from './util/encoding.js'; const TXESessions = new Map(); @@ -38,6 +38,11 @@ class TXEDispatcher { this.logger.info(`Called reset on session ${sessionId}, yeeting it out of existence`); return toForeignCallResult([]); } else { + if (functionName === 'deploy') { + const pathStr = fromArray(inputs[0] as ForeignCallArray) + .map(char => String.fromCharCode(char.toNumber())) + .join(''); + } const txeService = TXESessions.get(sessionId); const response = await (txeService as any)[functionName](...inputs); return response; diff --git a/yarn-project/txe/src/txe_service/txe_service.ts b/yarn-project/txe/src/txe_service/txe_service.ts index 019e331d2bdb..b4b4d9573efe 100644 --- a/yarn-project/txe/src/txe_service/txe_service.ts +++ b/yarn-project/txe/src/txe_service/txe_service.ts @@ -92,15 +92,12 @@ export class TXEService { } async deploy( - path: ForeignCallArray, + pathStr: string, initializer: ForeignCallArray, _length: ForeignCallSingle, args: ForeignCallArray, publicKeysHash: ForeignCallSingle, ) { - const pathStr = fromArray(path) - .map(char => String.fromCharCode(char.toNumber())) - .join(''); const initializerStr = fromArray(initializer) .map(char => String.fromCharCode(char.toNumber())) .join(''); diff --git a/yarn-project/yarn.lock b/yarn-project/yarn.lock index 244cd7ec0fa6..ecb58b8557f6 100644 --- a/yarn-project/yarn.lock +++ b/yarn-project/yarn.lock @@ -277,8 +277,8 @@ __metadata: version: 0.0.0-use.local resolution: "@aztec/builder@workspace:builder" dependencies: - "@aztec/cli": "workspace:^" "@aztec/foundation": "workspace:^" + "@aztec/types": "workspace:^" "@jest/globals": ^29.5.0 "@types/jest": ^29.5.0 "@types/node": ^18.7.23 @@ -287,7 +287,7 @@ __metadata: ts-node: ^10.9.1 typescript: ^5.0.4 bin: - aztec-builder: dest/cli.js + aztec-builder: dest/bin/cli.js languageName: unknown linkType: soft From 6a6051f268ea5644095e1243603a91b015f942f3 Mon Sep 17 00:00:00 2001 From: thunkar Date: Fri, 19 Jul 2024 15:20:05 +0000 Subject: [PATCH 04/14] working concept --- .../aztec/src/test/helpers/cheatcodes.nr | 6 +- .../src/test/helpers/test_environment.nr | 28 ++----- .../aztec-nr/aztec/src/test/helpers/utils.nr | 6 +- .../contracts/counter_contract/src/main.nr | 2 +- .../contracts/parent_contract/src/main.nr | 2 +- .../token_contract/src/test/utils.nr | 4 +- .../tooling/acvm_cli/src/cli/execute_cmd.rs | 5 +- .../tooling/debugger/src/foreign_calls.rs | 2 +- noir/noir-repo/tooling/fuzzer/src/lib.rs | 2 +- .../tooling/lsp/src/requests/test_run.rs | 1 + .../tooling/nargo/src/ops/foreign_calls.rs | 31 ++++++-- noir/noir-repo/tooling/nargo/src/ops/test.rs | 9 ++- .../tooling/nargo_cli/src/cli/compile_cmd.rs | 8 +- .../tooling/nargo_cli/src/cli/execute_cmd.rs | 17 ++++- .../tooling/nargo_cli/src/cli/test_cmd.rs | 7 +- .../src/contract-interface-gen/codegen.ts | 75 +++++++++++-------- yarn-project/builder/src/index.ts | 2 +- yarn-project/builder/tsconfig.json | 3 + yarn-project/txe/package.json | 1 - yarn-project/txe/src/index.ts | 25 +++++++ .../txe/src/txe_service/txe_service.ts | 39 +++++++--- yarn-project/txe/tsconfig.json | 3 - yarn-project/yarn.lock | 1 - 23 files changed, 180 insertions(+), 99 deletions(-) diff --git a/noir-projects/aztec-nr/aztec/src/test/helpers/cheatcodes.nr b/noir-projects/aztec-nr/aztec/src/test/helpers/cheatcodes.nr index e230ebbab672..1ebf07f9ccb5 100644 --- a/noir-projects/aztec-nr/aztec/src/test/helpers/cheatcodes.nr +++ b/noir-projects/aztec-nr/aztec/src/test/helpers/cheatcodes.nr @@ -55,8 +55,8 @@ unconstrained pub fn create_account() -> TestAccount { oracle_create_account() } -unconstrained pub fn add_account(secret: Field, partial_address: PartialAddress) -> TestAccount { - oracle_add_account(secret, partial_address) +unconstrained pub fn add_account(secret: Field) -> TestAccount { + oracle_add_account(secret) } unconstrained pub fn derive_keys(secret: Field) -> PublicKeys { @@ -163,7 +163,7 @@ fn direct_storage_write_oracle( fn oracle_create_account() -> TestAccount {} #[oracle(addAccount)] -fn oracle_add_account(secret: Field, partial_address: PartialAddress) -> TestAccount {} +fn oracle_add_account(secret: Field) -> TestAccount {} #[oracle(deriveKeys)] fn oracle_derive_keys(secret: Field) -> PublicKeys {} diff --git a/noir-projects/aztec-nr/aztec/src/test/helpers/test_environment.nr b/noir-projects/aztec-nr/aztec/src/test/helpers/test_environment.nr index b6238d4e02d8..12ff64f5a646 100644 --- a/noir-projects/aztec-nr/aztec/src/test/helpers/test_environment.nr +++ b/noir-projects/aztec-nr/aztec/src/test/helpers/test_environment.nr @@ -85,27 +85,11 @@ impl TestEnvironment { } fn create_account_contract(&mut self, secret: Field) -> AztecAddress { - let public_keys = cheatcodes::derive_keys(secret); - let args = [public_keys.ivpk_m.x, public_keys.ivpk_m.y]; - let instance = cheatcodes::deploy( - "@aztec/noir-contracts.js/SchnorrAccount", - "constructor", - args.as_slice(), - public_keys.hash().to_field() - ); + let test_account = cheatcodes::add_account(secret); + let address = test_account.address; cheatcodes::advance_blocks_by(1); - let test_account = cheatcodes::add_account( - secret, - PartialAddress::compute( - instance.contract_class_id, - instance.salt, - instance.initialization_hash, - instance.deployer - ) - ); - let keys = test_account.keys; - let address = instance.to_address(); + let keys = test_account.keys; keys::store_master_key(NULLIFIER_INDEX, address, keys.npk_m); keys::store_master_key(INCOMING_INDEX, address, keys.ivpk_m); @@ -115,7 +99,7 @@ impl TestEnvironment { let selector = FunctionSelector::from_signature("constructor(Field,Field)"); let mut context = self.private_at(cheatcodes::get_block_number()); - + let args = [test_account.keys.ivpk_m.x, test_account.keys.ivpk_m.y]; let _ = context.call_private_function(address, selector, args); address @@ -125,6 +109,10 @@ impl TestEnvironment { Deployer { path, public_keys_hash: 0 } } + fn deploy_self(self) -> Deployer<0> { + Deployer { path: "", public_keys_hash: 0 } + } + fn call_private( self, call_interface: C diff --git a/noir-projects/aztec-nr/aztec/src/test/helpers/utils.nr b/noir-projects/aztec-nr/aztec/src/test/helpers/utils.nr index 12b5fddf2633..d6bf6243f6f9 100644 --- a/noir-projects/aztec-nr/aztec/src/test/helpers/utils.nr +++ b/noir-projects/aztec-nr/aztec/src/test/helpers/utils.nr @@ -30,9 +30,9 @@ pub fn apply_side_effects_private(contract_address: AztecAddress, public_inputs: } struct Deployer { - path: str, - public_keys_hash: Field - } + path: str, + public_keys_hash: Field +} impl Deployer { pub fn with_private_initializer( diff --git a/noir-projects/noir-contracts/contracts/counter_contract/src/main.nr b/noir-projects/noir-contracts/contracts/counter_contract/src/main.nr index 011ebdd5d087..5350a69f7b2a 100644 --- a/noir-projects/noir-contracts/contracts/counter_contract/src/main.nr +++ b/noir-projects/noir-contracts/contracts/counter_contract/src/main.nr @@ -57,7 +57,7 @@ contract Counter { // Deploy contract and initialize let initializer = Counter::interface().initialize(initial_value as u64, owner, outgoing_viewer); - let counter_contract = env.deploy("@aztec/noir-contracts.js/Counter").with_private_initializer(initializer); + let counter_contract = env.deploy_self().with_private_initializer(initializer); let contract_address = counter_contract.to_address(); // docs:start:txe_test_read_notes diff --git a/noir-projects/noir-contracts/contracts/parent_contract/src/main.nr b/noir-projects/noir-contracts/contracts/parent_contract/src/main.nr index 2747bfed0043..8bce0f09f642 100644 --- a/noir-projects/noir-contracts/contracts/parent_contract/src/main.nr +++ b/noir-projects/noir-contracts/contracts/parent_contract/src/main.nr @@ -257,7 +257,7 @@ contract Parent { let owner = env.create_account(); // Deploy child contract - let child_contract = env.deploy("@aztec/noir-contracts.js/Child").without_initializer(); + let child_contract = env.deploy("./@child_contract").without_initializer(); let child_contract_address = child_contract.to_address(); cheatcodes::advance_blocks_by(1); diff --git a/noir-projects/noir-contracts/contracts/token_contract/src/test/utils.nr b/noir-projects/noir-contracts/contracts/token_contract/src/test/utils.nr index c82d4246ceb7..4435d6e0da67 100644 --- a/noir-projects/noir-contracts/contracts/token_contract/src/test/utils.nr +++ b/noir-projects/noir-contracts/contracts/token_contract/src/test/utils.nr @@ -15,7 +15,7 @@ pub fn setup(with_account_contracts: bool) -> (&mut TestEnvironment, AztecAddres let owner = env.create_account_contract(1); let recipient = env.create_account_contract(2); // Deploy canonical auth registry - let _auth_registry = env.deploy("@aztec/noir-contracts.js/AuthRegistry").without_initializer(); + let _auth_registry = env.deploy("./@auth_registry_contract").without_initializer(); (owner, recipient) } else { let owner = env.create_account(); @@ -33,7 +33,7 @@ pub fn setup(with_account_contracts: bool) -> (&mut TestEnvironment, AztecAddres "TT00000000000000000000000000000", 18 ); - let token_contract = env.deploy("@aztec/noir-contracts.js/Token").with_public_initializer(initializer_call_interface); + let token_contract = env.deploy_self().with_public_initializer(initializer_call_interface); let token_contract_address = token_contract.to_address(); env.advance_block_by(1); (&mut env, token_contract_address, owner, recipient) diff --git a/noir/noir-repo/tooling/acvm_cli/src/cli/execute_cmd.rs b/noir/noir-repo/tooling/acvm_cli/src/cli/execute_cmd.rs index ac3af03684f7..e58d8128f9a2 100644 --- a/noir/noir-repo/tooling/acvm_cli/src/cli/execute_cmd.rs +++ b/noir/noir-repo/tooling/acvm_cli/src/cli/execute_cmd.rs @@ -39,7 +39,7 @@ pub(crate) struct ExecuteCommand { fn run_command(args: ExecuteCommand) -> Result { let bytecode = read_bytecode_from_file(&args.working_directory, &args.bytecode)?; let circuit_inputs = read_inputs_from_file(&args.working_directory, &args.input_witness)?; - let output_witness = execute_program_from_witness(circuit_inputs, &bytecode, None)?; + let output_witness = execute_program_from_witness(circuit_inputs, &bytecode)?; assert_eq!(output_witness.length(), 1, "ACVM CLI only supports a witness stack of size 1"); let output_witness_string = create_output_witness_string( &output_witness.peek().expect("Should have a witness stack item").witness, @@ -66,7 +66,6 @@ pub(crate) fn run(args: ExecuteCommand) -> Result { pub(crate) fn execute_program_from_witness( inputs_map: WitnessMap, bytecode: &[u8], - foreign_call_resolver_url: Option<&str>, ) -> Result, CliError> { let program: Program = Program::deserialize_program(bytecode) .map_err(|_| CliError::CircuitDeserializationError())?; @@ -74,7 +73,7 @@ pub(crate) fn execute_program_from_witness( &program, inputs_map, &Bn254BlackBoxSolver, - &mut DefaultForeignCallExecutor::new(true, foreign_call_resolver_url), + &mut DefaultForeignCallExecutor::new(true, None, None), ) .map_err(CliError::CircuitExecutionError) } diff --git a/noir/noir-repo/tooling/debugger/src/foreign_calls.rs b/noir/noir-repo/tooling/debugger/src/foreign_calls.rs index 62443d4065c6..0f428049eaa6 100644 --- a/noir/noir-repo/tooling/debugger/src/foreign_calls.rs +++ b/noir/noir-repo/tooling/debugger/src/foreign_calls.rs @@ -49,7 +49,7 @@ pub struct DefaultDebugForeignCallExecutor { impl DefaultDebugForeignCallExecutor { pub fn new(show_output: bool) -> Self { Self { - executor: DefaultForeignCallExecutor::new(show_output, None), + executor: DefaultForeignCallExecutor::new(show_output, None, None), debug_vars: DebugVars::default(), } } diff --git a/noir/noir-repo/tooling/fuzzer/src/lib.rs b/noir/noir-repo/tooling/fuzzer/src/lib.rs index 28d7353f35a2..d6616bcb1284 100644 --- a/noir/noir-repo/tooling/fuzzer/src/lib.rs +++ b/noir/noir-repo/tooling/fuzzer/src/lib.rs @@ -80,7 +80,7 @@ impl FuzzedExecutor { &self.program.bytecode, initial_witness, &StubbedBlackBoxSolver, - &mut DefaultForeignCallExecutor::::new(false, None), + &mut DefaultForeignCallExecutor::::new(false, None, None), ); // TODO: Add handling for `vm.assume` equivalent diff --git a/noir/noir-repo/tooling/lsp/src/requests/test_run.rs b/noir/noir-repo/tooling/lsp/src/requests/test_run.rs index b4b9b62d6b68..114ca499bdaf 100644 --- a/noir/noir-repo/tooling/lsp/src/requests/test_run.rs +++ b/noir/noir-repo/tooling/lsp/src/requests/test_run.rs @@ -86,6 +86,7 @@ fn on_test_run_request_inner( &test_function, false, None, + workspace_file_manager.path(test_function.file_id()).map(|p| p.to_path_buf()), &CompileOptions::default(), ); let result = match test_result { diff --git a/noir/noir-repo/tooling/nargo/src/ops/foreign_calls.rs b/noir/noir-repo/tooling/nargo/src/ops/foreign_calls.rs index 987c7dd9cb9d..765216904a6c 100644 --- a/noir/noir-repo/tooling/nargo/src/ops/foreign_calls.rs +++ b/noir/noir-repo/tooling/nargo/src/ops/foreign_calls.rs @@ -1,3 +1,5 @@ +use std::path::PathBuf; + use acvm::{ acir::brillig::{ForeignCallParam, ForeignCallResult}, pwg::ForeignCallWaitInfo, @@ -112,6 +114,8 @@ pub struct DefaultForeignCallExecutor { show_output: bool, /// JSON RPC client to resolve foreign calls external_resolver: Option, + /// Path to the file currently in execution. + program_artifact_path: Option, } #[derive(Debug, Serialize, Deserialize)] @@ -126,10 +130,18 @@ struct ResolveForeignCallRequest { #[serde(flatten)] /// The foreign call which the external RPC server is to provide a response for. function_call: ForeignCallWaitInfo, + + #[serde(skip_serializing_if = "Option::is_none")] + /// Path to the file of the current program execution. + program_artifact_path: Option, } impl DefaultForeignCallExecutor { - pub fn new(show_output: bool, resolver_url: Option<&str>) -> Self { + pub fn new( + show_output: bool, + resolver_url: Option<&str>, + program_artifact_path: Option, + ) -> Self { let oracle_resolver = resolver_url.map(|resolver_url| { let mut transport_builder = Builder::new().url(resolver_url).expect("Invalid oracle resolver URL"); @@ -148,6 +160,7 @@ impl DefaultForeignCallExecutor { id: rand::thread_rng().gen(), mocked_responses: Vec::new(), last_mock_id: 0, + program_artifact_path, } } } @@ -302,6 +315,10 @@ impl Deserialize<'a>> ForeignCallExecutor let encoded_params = vec![build_json_rpc_arg(ResolveForeignCallRequest { session_id: self.id, function_call: foreign_call.clone(), + program_artifact_path: self + .program_artifact_path + .clone() + .map(|path| path.to_str().unwrap().to_string()), })]; let req = @@ -402,7 +419,7 @@ mod tests { fn test_oracle_resolver_echo() { let (server, url) = build_oracle_server(); - let mut executor = DefaultForeignCallExecutor::::new(false, Some(&url)); + let mut executor = DefaultForeignCallExecutor::::new(false, Some(&url), None); let foreign_call = ForeignCallWaitInfo { function: "echo".to_string(), @@ -419,7 +436,7 @@ mod tests { fn test_oracle_resolver_sum() { let (server, url) = build_oracle_server(); - let mut executor = DefaultForeignCallExecutor::new(false, Some(&url)); + let mut executor = DefaultForeignCallExecutor::new(false, Some(&url), None); let foreign_call = ForeignCallWaitInfo { function: "sum".to_string(), @@ -436,7 +453,7 @@ mod tests { fn foreign_call_executor_id_is_persistent() { let (server, url) = build_oracle_server(); - let mut executor = DefaultForeignCallExecutor::::new(false, Some(&url)); + let mut executor = DefaultForeignCallExecutor::::new(false, Some(&url), None); let foreign_call = ForeignCallWaitInfo { function: "id".to_string(), inputs: Vec::new() }; @@ -451,8 +468,10 @@ mod tests { fn oracle_resolver_rpc_can_distinguish_executors() { let (server, url) = build_oracle_server(); - let mut executor_1 = DefaultForeignCallExecutor::::new(false, Some(&url)); - let mut executor_2 = DefaultForeignCallExecutor::::new(false, Some(&url)); + let mut executor_1 = + DefaultForeignCallExecutor::::new(false, Some(&url), None); + let mut executor_2 = + DefaultForeignCallExecutor::::new(false, Some(&url), None); let foreign_call = ForeignCallWaitInfo { function: "id".to_string(), inputs: Vec::new() }; diff --git a/noir/noir-repo/tooling/nargo/src/ops/test.rs b/noir/noir-repo/tooling/nargo/src/ops/test.rs index 18c6f2530b96..66015c8053cf 100644 --- a/noir/noir-repo/tooling/nargo/src/ops/test.rs +++ b/noir/noir-repo/tooling/nargo/src/ops/test.rs @@ -1,3 +1,5 @@ +use std::path::PathBuf; + use acvm::{ acir::native_types::{WitnessMap, WitnessStack}, BlackBoxFunctionSolver, FieldElement, @@ -29,6 +31,7 @@ pub fn run_test>( test_function: &TestFunction, show_output: bool, foreign_call_resolver_url: Option<&str>, + program_artifact_path: Option, config: &CompileOptions, ) -> TestStatus { let compiled_program = compile_no_check(context, config, test_function.get_id(), None, false); @@ -40,7 +43,11 @@ pub fn run_test>( &compiled_program.program, WitnessMap::new(), blackbox_solver, - &mut DefaultForeignCallExecutor::new(show_output, foreign_call_resolver_url), + &mut DefaultForeignCallExecutor::new( + show_output, + foreign_call_resolver_url, + program_artifact_path, + ), ); test_status_program_compile_pass( test_function, diff --git a/noir/noir-repo/tooling/nargo_cli/src/cli/compile_cmd.rs b/noir/noir-repo/tooling/nargo_cli/src/cli/compile_cmd.rs index e83b1728c932..1034cf9813dd 100644 --- a/noir/noir-repo/tooling/nargo_cli/src/cli/compile_cmd.rs +++ b/noir/noir-repo/tooling/nargo_cli/src/cli/compile_cmd.rs @@ -233,12 +233,8 @@ fn save_contract( target_dir: &Path, show_artifact_paths: bool, ) { - let contract_name = contract.name.clone(); - let artifact_path = save_contract_to_file( - &contract.into(), - &format!("{}-{}", package.name, contract_name), - target_dir, - ); + let artifact_path = + save_contract_to_file(&contract.into(), package.name.to_string().as_str(), target_dir); if show_artifact_paths { println!("Saved contract artifact to: {}", artifact_path.display()); } diff --git a/noir/noir-repo/tooling/nargo_cli/src/cli/execute_cmd.rs b/noir/noir-repo/tooling/nargo_cli/src/cli/execute_cmd.rs index cf9dc1141a1a..356689d01c93 100644 --- a/noir/noir-repo/tooling/nargo_cli/src/cli/execute_cmd.rs +++ b/noir/noir-repo/tooling/nargo_cli/src/cli/execute_cmd.rs @@ -1,3 +1,5 @@ +use std::path::PathBuf; + use acvm::acir::native_types::WitnessStack; use acvm::FieldElement; use bn254_blackbox_solver::Bn254BlackBoxSolver; @@ -65,13 +67,15 @@ pub(crate) fn run(args: ExecuteCommand, config: NargoConfig) -> Result<(), CliEr let binary_packages = workspace.into_iter().filter(|package| package.is_binary()); for package in binary_packages { let program_artifact_path = workspace.package_build_path(package); - let program: CompiledProgram = read_program_from_file(program_artifact_path)?.into(); + let program: CompiledProgram = + read_program_from_file(program_artifact_path.clone())?.into(); let (return_value, witness_stack) = execute_program_and_decode( program, package, &args.prover_name, args.oracle_resolver.as_deref(), + Some(program_artifact_path.clone()), )?; println!("[{}] Circuit witness successfully solved", package.name); @@ -92,11 +96,13 @@ fn execute_program_and_decode( package: &Package, prover_name: &str, foreign_call_resolver_url: Option<&str>, + program_artifact_path: Option, ) -> Result<(Option, WitnessStack), CliError> { // Parse the initial witness values from Prover.toml let (inputs_map, _) = read_inputs_from_file(&package.root_dir, prover_name, Format::Toml, &program.abi)?; - let witness_stack = execute_program(&program, &inputs_map, foreign_call_resolver_url)?; + let witness_stack = + execute_program(&program, &inputs_map, foreign_call_resolver_url, program_artifact_path)?; // Get the entry point witness for the ABI let main_witness = &witness_stack.peek().expect("Should have at least one witness on the stack").witness; @@ -109,6 +115,7 @@ pub(crate) fn execute_program( compiled_program: &CompiledProgram, inputs_map: &InputMap, foreign_call_resolver_url: Option<&str>, + program_artifact_path: Option, ) -> Result, CliError> { let initial_witness = compiled_program.abi.encode(inputs_map, None)?; @@ -116,7 +123,11 @@ pub(crate) fn execute_program( &compiled_program.program, initial_witness, &Bn254BlackBoxSolver, - &mut DefaultForeignCallExecutor::new(true, foreign_call_resolver_url), + &mut DefaultForeignCallExecutor::new( + true, + foreign_call_resolver_url, + program_artifact_path, + ), ); match solved_witness_stack_err { Ok(solved_witness_stack) => Ok(solved_witness_stack), diff --git a/noir/noir-repo/tooling/nargo_cli/src/cli/test_cmd.rs b/noir/noir-repo/tooling/nargo_cli/src/cli/test_cmd.rs index 27c66c956d90..83d3acd05d05 100644 --- a/noir/noir-repo/tooling/nargo_cli/src/cli/test_cmd.rs +++ b/noir/noir-repo/tooling/nargo_cli/src/cli/test_cmd.rs @@ -1,4 +1,4 @@ -use std::io::Write; +use std::{io::Write, path::PathBuf}; use acvm::{BlackBoxFunctionSolver, FieldElement}; use bn254_blackbox_solver::Bn254BlackBoxSolver; @@ -92,6 +92,7 @@ pub(crate) fn run(args: TestCommand, config: NargoConfig) -> Result<(), CliError pattern, args.show_output, args.oracle_resolver.as_deref(), + Some(workspace.package_build_path(package)), &args.compile_options, ) }) @@ -127,6 +128,7 @@ fn run_tests + Default>( fn_name: FunctionNameMatch, show_output: bool, foreign_call_resolver_url: Option<&str>, + program_artifact_path: Option, compile_options: &CompileOptions, ) -> Result, CliError> { let test_functions = @@ -147,6 +149,7 @@ fn run_tests + Default>( &test_name, show_output, foreign_call_resolver_url, + program_artifact_path.clone(), compile_options, ); @@ -165,6 +168,7 @@ fn run_test + Default>( fn_name: &str, show_output: bool, foreign_call_resolver_url: Option<&str>, + program_artifact_path: Option, compile_options: &CompileOptions, ) -> TestStatus { // This is really hacky but we can't share `Context` or `S` across threads. @@ -201,6 +205,7 @@ fn run_test + Default>( test_function, show_output, foreign_call_resolver_url, + program_artifact_path, compile_options, ) } else { diff --git a/yarn-project/builder/src/contract-interface-gen/codegen.ts b/yarn-project/builder/src/contract-interface-gen/codegen.ts index 4f40886f42af..62ccdaea9ad3 100644 --- a/yarn-project/builder/src/contract-interface-gen/codegen.ts +++ b/yarn-project/builder/src/contract-interface-gen/codegen.ts @@ -2,13 +2,13 @@ import { loadContractArtifact } from '@aztec/types/abi'; import crypto from 'crypto'; -import { existsSync, mkdirSync, readFileSync, readdirSync, statSync, writeFileSync } from 'fs'; +import { access, mkdir, readFile, readdir, stat, writeFile } from 'fs/promises'; import path from 'path'; import { generateTypescriptContractInterface } from './typescript.js'; const cacheFilePath = './codegenCache.json'; -let cache: Record = {}; +let cache: Record = {}; /** Generate code options */ export type GenerateCodeOptions = { force?: boolean }; @@ -16,40 +16,43 @@ export type GenerateCodeOptions = { force?: boolean }; /** * Generates Noir interface or Typescript interface for a folder or single file from a Noir compilation artifact. */ -export function generateCode(outputPath: string, fileOrDirPath: string, opts: GenerateCodeOptions = {}) { +export async function generateCode(outputPath: string, fileOrDirPath: string, opts: GenerateCodeOptions = {}) { readCache(); - const stats = statSync(fileOrDirPath); + const results = []; + const stats = await stat(fileOrDirPath); if (stats.isDirectory()) { - const files = readdirSync(fileOrDirPath, { recursive: true, encoding: 'utf-8' }).filter( + const files = (await readdir(fileOrDirPath, { recursive: true, encoding: 'utf-8' })).filter( file => file.endsWith('.json') && !file.startsWith('debug_'), ); for (const file of files) { const fullPath = path.join(fileOrDirPath, file); - generateFromNoirAbi(outputPath, fullPath, opts); + results.push(await generateFromNoirAbi(outputPath, fullPath, opts)); } } else if (stats.isFile()) { - generateFromNoirAbi(outputPath, fileOrDirPath, opts); + results.push(await generateFromNoirAbi(outputPath, fileOrDirPath, opts)); } - writeCache(); + await writeCache(); + return results; } /** * Generates Noir interface or Typescript interface for a single file Noir compilation artifact. */ -function generateFromNoirAbi(outputPath: string, noirAbiPath: string, opts: GenerateCodeOptions = {}) { - const contractName = path.basename(noirAbiPath); - const currentHash = generateFileHash(noirAbiPath); - - if (isCacheValid(contractName, currentHash) && !opts.force) { - console.log(`${contractName} has not changed. Skipping generation.`); - return; +async function generateFromNoirAbi(outputPath: string, noirAbiPath: string, opts: GenerateCodeOptions = {}) { + const fileName = path.basename(noirAbiPath); + const currentHash = await generateFileHash(noirAbiPath); + let cachedInstance = isCacheValid(fileName, currentHash); + if (cachedInstance && !opts.force) { + console.log(`${fileName} has not changed. Skipping generation.`); + return `${outputPath}/${cachedInstance.contractName}.ts`; } - const contract = JSON.parse(readFileSync(noirAbiPath, 'utf8')); + const file = await readFile(noirAbiPath, 'utf8'); + const contract = JSON.parse(file); const aztecAbi = loadContractArtifact(contract); - mkdirSync(outputPath, { recursive: true }); + await mkdir(outputPath, { recursive: true }); let relativeArtifactPath = path.relative(outputPath, noirAbiPath); if (relativeArtifactPath === path.basename(noirAbiPath)) { @@ -58,34 +61,46 @@ function generateFromNoirAbi(outputPath: string, noirAbiPath: string, opts: Gene } const tsWrapper = generateTypescriptContractInterface(aztecAbi, relativeArtifactPath); - writeFileSync(`${outputPath}/${aztecAbi.name}.ts`, tsWrapper); + const outputFilePath = `${outputPath}/${aztecAbi.name}.ts`; + + await writeFile(outputFilePath, tsWrapper); - updateCache(contractName, currentHash); + updateCache(fileName, aztecAbi.name, currentHash); + return outputFilePath; } -function generateFileHash(filePath: string) { - const fileBuffer = readFileSync(filePath); +async function generateFileHash(filePath: string) { + const fileBuffer = await readFile(filePath); const hashSum = crypto.createHash('sha256'); hashSum.update(fileBuffer); const hex = hashSum.digest('hex'); return hex; } -function readCache(): void { - if (existsSync(cacheFilePath)) { - const cacheRaw = readFileSync(cacheFilePath, 'utf8'); +async function readCache() { + if (await exists(cacheFilePath)) { + const cacheRaw = await readFile(cacheFilePath, 'utf8'); cache = JSON.parse(cacheRaw); } } -function writeCache(): void { - writeFileSync(cacheFilePath, JSON.stringify(cache, null, 2), 'utf8'); +async function writeCache() { + await writeFile(cacheFilePath, JSON.stringify(cache, null, 2), 'utf8'); } -function isCacheValid(contractName: string, currentHash: string): boolean { - return cache[contractName] === currentHash; +function isCacheValid(contractName: string, currentHash: string) { + return cache[contractName]?.hash === currentHash && cache[contractName]; } -function updateCache(contractName: string, hash: string): void { - cache[contractName] = hash; +function updateCache(fileName: string, contractName: string, hash: string): void { + cache[fileName] = { contractName, hash }; +} + +async function exists(filePath: string) { + try { + await access(filePath); + return true; + } catch { + return false; + } } diff --git a/yarn-project/builder/src/index.ts b/yarn-project/builder/src/index.ts index dd19f3411ddf..f38c74f6e87a 100644 --- a/yarn-project/builder/src/index.ts +++ b/yarn-project/builder/src/index.ts @@ -10,7 +10,7 @@ export function injectCommands(program: Command) { .description('Validates and generates an Aztec Contract ABI from Noir ABI.') .action(async (noirAbiPath: string, { outdir, force }) => { const { generateCode } = await import('./contract-interface-gen/codegen.js'); - generateCode(outdir || dirname(noirAbiPath), noirAbiPath, { force }); + await generateCode(outdir || dirname(noirAbiPath), noirAbiPath, { force }); }); return program; } diff --git a/yarn-project/builder/tsconfig.json b/yarn-project/builder/tsconfig.json index a2d81aa678d5..3435c3b70e5f 100644 --- a/yarn-project/builder/tsconfig.json +++ b/yarn-project/builder/tsconfig.json @@ -8,6 +8,9 @@ "references": [ { "path": "../foundation" + }, + { + "path": "../types" } ], "include": ["src", "src/*.json"] diff --git a/yarn-project/txe/package.json b/yarn-project/txe/package.json index 14f89c536216..8c8e45cb0dc7 100644 --- a/yarn-project/txe/package.json +++ b/yarn-project/txe/package.json @@ -64,7 +64,6 @@ "@aztec/foundation": "workspace:^", "@aztec/key-store": "workspace:^", "@aztec/kv-store": "workspace:^", - "@aztec/noir-contracts.js": "workspace:^", "@aztec/pxe": "workspace:^", "@aztec/simulator": "workspace:^", "@aztec/types": "workspace:^", diff --git a/yarn-project/txe/src/index.ts b/yarn-project/txe/src/index.ts index a712ee457cd4..295e3eaa8ae3 100644 --- a/yarn-project/txe/src/index.ts +++ b/yarn-project/txe/src/index.ts @@ -1,7 +1,11 @@ +import { loadContractArtifact } from '@aztec/aztec.js'; import { Fr } from '@aztec/foundation/fields'; import { JsonRpcServer } from '@aztec/foundation/json-rpc/server'; import { type Logger } from '@aztec/foundation/log'; +import { readFile, readdir } from 'fs/promises'; +import { dirname, join } from 'path'; + import { TXEService } from './txe_service/txe_service.js'; import { ForeignCallArray, type ForeignCallResult, fromArray, toForeignCallResult } from './util/encoding.js'; @@ -14,6 +18,7 @@ type MethodNames = { type TXEForeignCallInput = { session_id: number; function: MethodNames | 'reset'; + program_artifact_path: string; inputs: any[]; }; @@ -25,6 +30,7 @@ class TXEDispatcher { session_id: sessionId, function: functionName, inputs, + program_artifact_path, }: TXEForeignCallInput): Promise { this.logger.debug(`Calling ${functionName} on session ${sessionId}`); @@ -42,6 +48,25 @@ class TXEDispatcher { const pathStr = fromArray(inputs[0] as ForeignCallArray) .map(char => String.fromCharCode(char.toNumber())) .join(''); + let inputPath = program_artifact_path; + // Is not the same contract we're testing + if (pathStr != '') { + // Workspace + if (pathStr.includes('@')) { + const [workspace, pkg] = pathStr.split('@'); + const targetPath = join(dirname(inputPath), '../', workspace, './target'); + this.logger.debug(`Looking for compiled artifact in workspace ${targetPath}`); + inputPath = join(targetPath, `${pkg}.json`); + } else { + // Individual contract + const targetPath = join(dirname(inputPath), '../', pathStr, './target'); + this.logger.debug(`Looking for compiled artifact in ${targetPath}`); + [inputPath] = (await readdir(targetPath)).filter(file => file.endsWith('.json')); + } + } + this.logger.debug(`Loading compiled artifact ${inputPath}`); + const artifact = loadContractArtifact(JSON.parse(await readFile(inputPath, 'utf-8'))); + inputs[0] = artifact; } const txeService = TXESessions.get(sessionId); const response = await (txeService as any)[functionName](...inputs); diff --git a/yarn-project/txe/src/txe_service/txe_service.ts b/yarn-project/txe/src/txe_service/txe_service.ts index b4b4d9573efe..e97f49690be3 100644 --- a/yarn-project/txe/src/txe_service/txe_service.ts +++ b/yarn-project/txe/src/txe_service/txe_service.ts @@ -1,3 +1,4 @@ +import { SchnorrAccountContractArtifact } from '@aztec/accounts/schnorr'; import { L2Block, MerkleTreeId, PublicDataWrite } from '@aztec/circuit-types'; import { Fr, @@ -7,10 +8,11 @@ import { PUBLIC_DATA_SUBTREE_HEIGHT, Point, PublicDataTreeLeaf, + computePartialAddress, getContractInstanceFromDeployParams, } from '@aztec/circuits.js'; import { computePublicDataTreeLeafSlot } from '@aztec/circuits.js/hash'; -import { NoteSelector } from '@aztec/foundation/abi'; +import { ContractArtifact, NoteSelector } from '@aztec/foundation/abi'; import { AztecAddress } from '@aztec/foundation/aztec-address'; import { type Logger } from '@aztec/foundation/log'; import { KeyStore } from '@aztec/key-store'; @@ -92,7 +94,7 @@ export class TXEService { } async deploy( - pathStr: string, + artifact: ContractArtifact, initializer: ForeignCallArray, _length: ForeignCallSingle, args: ForeignCallArray, @@ -104,12 +106,10 @@ export class TXEService { const decodedArgs = fromArray(args); const publicKeysHashFr = fromSingle(publicKeysHash); this.logger.debug( - `Deploy ${pathStr} with initializer ${initializerStr}(${decodedArgs}) and public keys hash ${publicKeysHashFr}`, + `Deploy ${artifact.name} with initializer ${initializerStr}(${decodedArgs}) and public keys hash ${publicKeysHashFr}`, ); - const contractModule = await import(pathStr); - // Hacky way of getting the class, the name of the Artifact is always longer - const contractClass = contractModule[Object.keys(contractModule).sort((a, b) => a.length - b.length)[0]]; - const instance = getContractInstanceFromDeployParams(contractClass.artifact, { + + const instance = getContractInstanceFromDeployParams(artifact, { constructorArgs: decodedArgs, skipArgsDecoding: true, salt: Fr.ONE, @@ -118,9 +118,9 @@ export class TXEService { deployer: AztecAddress.ZERO, }); - this.logger.debug(`Deployed ${contractClass.artifact.name} at ${instance.address}`); + this.logger.debug(`Deployed ${artifact.name} at ${instance.address}`); await (this.typedOracle as TXE).addContractInstance(instance); - await (this.typedOracle as TXE).addContractArtifact(contractClass.artifact); + await (this.typedOracle as TXE).addContractArtifact(artifact); return toForeignCallResult([ toArray([ instance.salt, @@ -168,9 +168,26 @@ export class TXEService { ]); } - async addAccount(secret: ForeignCallSingle, partialAddress: ForeignCallSingle) { + async addAccount(secret: ForeignCallSingle) { + const keys = (this.typedOracle as TXE).deriveKeys(fromSingle(secret)); + const args = [keys.publicKeys.masterIncomingViewingPublicKey.x, keys.publicKeys.masterIncomingViewingPublicKey.y]; + const hash = keys.publicKeys.hash(); + let artifact = SchnorrAccountContractArtifact; + const instance = getContractInstanceFromDeployParams(artifact, { + constructorArgs: args, + skipArgsDecoding: true, + salt: Fr.ONE, + publicKeysHash: hash, + constructorArtifact: 'constructor', + deployer: AztecAddress.ZERO, + }); + + this.logger.debug(`Deployed ${artifact.name} at ${instance.address}`); + await (this.typedOracle as TXE).addContractInstance(instance); + await (this.typedOracle as TXE).addContractArtifact(artifact); + const keyStore = (this.typedOracle as TXE).getKeyStore(); - const completeAddress = await keyStore.addAccount(fromSingle(secret), fromSingle(partialAddress)); + const completeAddress = await keyStore.addAccount(fromSingle(secret), computePartialAddress(instance)); const accountStore = (this.typedOracle as TXE).getTXEDatabase(); await accountStore.setAccount(completeAddress.address, completeAddress); this.logger.debug(`Created account ${completeAddress.address}`); diff --git a/yarn-project/txe/tsconfig.json b/yarn-project/txe/tsconfig.json index 7db2bf797787..a57cb2cd9946 100644 --- a/yarn-project/txe/tsconfig.json +++ b/yarn-project/txe/tsconfig.json @@ -27,9 +27,6 @@ { "path": "../kv-store" }, - { - "path": "../noir-contracts.js" - }, { "path": "../pxe" }, diff --git a/yarn-project/yarn.lock b/yarn-project/yarn.lock index ecb58b8557f6..2061be278281 100644 --- a/yarn-project/yarn.lock +++ b/yarn-project/yarn.lock @@ -962,7 +962,6 @@ __metadata: "@aztec/foundation": "workspace:^" "@aztec/key-store": "workspace:^" "@aztec/kv-store": "workspace:^" - "@aztec/noir-contracts.js": "workspace:^" "@aztec/pxe": "workspace:^" "@aztec/simulator": "workspace:^" "@aztec/types": "workspace:^" From 3b828dd647a5f61bb4f6cea025640de8540cb617 Mon Sep 17 00:00:00 2001 From: thunkar Date: Mon, 22 Jul 2024 08:06:59 +0000 Subject: [PATCH 05/14] renaming --- .../tutorials/simple_dapp/2_contract_deployment.md | 2 +- .../tutorials/simple_dapp/3_contract_interaction.md | 2 +- yarn-project/circuits.js/scripts/copy-contracts.sh | 4 ++-- .../protocol-contracts/scripts/copy-contracts.sh | 12 ++++++------ yarn-project/types/scripts/copy-contracts.sh | 2 +- 5 files changed, 11 insertions(+), 11 deletions(-) diff --git a/docs/docs/tutorials/simple_dapp/2_contract_deployment.md b/docs/docs/tutorials/simple_dapp/2_contract_deployment.md index 34d56026835f..49a9e4fe5031 100644 --- a/docs/docs/tutorials/simple_dapp/2_contract_deployment.md +++ b/docs/docs/tutorials/simple_dapp/2_contract_deployment.md @@ -53,7 +53,7 @@ Create a new file `src/deploy.mjs`: import { writeFileSync } from 'fs'; import { Contract, loadContractArtifact, createPXEClient } from '@aztec/aztec.js'; import { getInitialTestAccountsWallets } from '@aztec/accounts/testing'; -import TokenContractJson from "../contracts/token/target/token_contract-Token.json" assert { type: "json" }; +import TokenContractJson from "../contracts/token/target/token_contract.json" assert { type: "json" }; #include_code dapp-deploy yarn-project/end-to-end/src/sample-dapp/deploy.mjs raw diff --git a/docs/docs/tutorials/simple_dapp/3_contract_interaction.md b/docs/docs/tutorials/simple_dapp/3_contract_interaction.md index db1ce5b59640..8cd7c127ec3c 100644 --- a/docs/docs/tutorials/simple_dapp/3_contract_interaction.md +++ b/docs/docs/tutorials/simple_dapp/3_contract_interaction.md @@ -20,7 +20,7 @@ To do this, let's first initialize a new `Contract` instance using `aztec.js` th // src/contracts.mjs import { Contract } from "@aztec/aztec.js"; import { readFileSync } from "fs"; -import TokenContractJson from "../contracts/token/target/token_contract-Token.json" assert { type: "json" }; +import TokenContractJson from "../contracts/token/target/token_contract.json" assert { type: "json" }; ``` And then add the following code for initializing the `Contract` instances: diff --git a/yarn-project/circuits.js/scripts/copy-contracts.sh b/yarn-project/circuits.js/scripts/copy-contracts.sh index 2b9f3838a439..c679671c1efa 100755 --- a/yarn-project/circuits.js/scripts/copy-contracts.sh +++ b/yarn-project/circuits.js/scripts/copy-contracts.sh @@ -2,5 +2,5 @@ set -euo pipefail mkdir -p ./fixtures -cp "../../noir-projects/noir-contracts/target/benchmarking_contract-Benchmarking.json" ./fixtures/Benchmarking.test.json -cp "../../noir-projects/noir-contracts/target/test_contract-Test.json" ./fixtures/Test.test.json +cp "../../noir-projects/noir-contracts/target/benchmarking_contract.json" ./fixtures/Benchmarking.test.json +cp "../../noir-projects/noir-contracts/target/test_contract.json" ./fixtures/Test.test.json diff --git a/yarn-project/protocol-contracts/scripts/copy-contracts.sh b/yarn-project/protocol-contracts/scripts/copy-contracts.sh index 5001bf8c2548..5df6d94e24d7 100755 --- a/yarn-project/protocol-contracts/scripts/copy-contracts.sh +++ b/yarn-project/protocol-contracts/scripts/copy-contracts.sh @@ -3,12 +3,12 @@ set -euo pipefail mkdir -p ./artifacts contracts=( - contract_class_registerer_contract-ContractClassRegisterer - contract_instance_deployer_contract-ContractInstanceDeployer - gas_token_contract-GasToken - key_registry_contract-KeyRegistry - auth_registry_contract-AuthRegistry - multi_call_entrypoint_contract-MultiCallEntrypoint + contract_class_registerer_contract + contract_instance_deployer_contract + gas_token_contract + key_registry_contract + auth_registry_contract + multi_call_entrypoint_contract ) diff --git a/yarn-project/types/scripts/copy-contracts.sh b/yarn-project/types/scripts/copy-contracts.sh index d64da5b967a2..47d12379c9c7 100755 --- a/yarn-project/types/scripts/copy-contracts.sh +++ b/yarn-project/types/scripts/copy-contracts.sh @@ -2,4 +2,4 @@ set -euo pipefail mkdir -p ./fixtures -cp "../../noir-projects/noir-contracts/target/benchmarking_contract-Benchmarking.json" ./fixtures/Benchmarking.test.json +cp "../../noir-projects/noir-contracts/target/benchmarking_contract.json" ./fixtures/Benchmarking.test.json From c8de85ee2c630b231b6c6b55f91a4e0beb894e45 Mon Sep 17 00:00:00 2001 From: thunkar Date: Mon, 22 Jul 2024 13:31:52 +0000 Subject: [PATCH 06/14] better approach --- aztec-up/bin/docker-compose.test.yml | 1 + .../simple_dapp/2_contract_deployment.md | 2 +- .../simple_dapp/3_contract_interaction.md | 2 +- .../aztec/src/test/helpers/cheatcodes.nr | 63 ++++++++++--------- .../src/test/helpers/test_environment.nr | 8 +-- .../aztec-nr/aztec/src/test/helpers/utils.nr | 17 ++--- .../contracts/counter_contract/src/main.nr | 2 +- .../contracts/parent_contract/src/main.nr | 2 +- .../private_token_contract/src/test/utils.nr | 4 +- .../token_contract/src/test/utils.nr | 4 +- .../tooling/acvm_cli/src/cli/execute_cmd.rs | 2 +- .../tooling/debugger/src/foreign_calls.rs | 2 +- noir/noir-repo/tooling/fuzzer/src/lib.rs | 2 +- .../tooling/lsp/src/requests/test_run.rs | 3 +- .../tooling/nargo/src/ops/foreign_calls.rs | 36 +++++++---- noir/noir-repo/tooling/nargo/src/ops/test.rs | 6 +- .../tooling/nargo_cli/src/cli/compile_cmd.rs | 8 ++- .../tooling/nargo_cli/src/cli/execute_cmd.rs | 14 +++-- .../tooling/nargo_cli/src/cli/test_cmd.rs | 15 +++-- .../circuits.js/scripts/copy-contracts.sh | 4 +- .../scripts/copy-contracts.sh | 12 ++-- yarn-project/txe/src/index.ts | 29 +++++---- yarn-project/types/scripts/copy-contracts.sh | 2 +- 23 files changed, 142 insertions(+), 98 deletions(-) diff --git a/aztec-up/bin/docker-compose.test.yml b/aztec-up/bin/docker-compose.test.yml index dca6d9f4435d..4a95518d32fb 100644 --- a/aztec-up/bin/docker-compose.test.yml +++ b/aztec-up/bin/docker-compose.test.yml @@ -6,6 +6,7 @@ services: HOST_WORKDIR: "${PWD}" # Loaded from the user shell to show log files absolute path in host volumes: - ./log:/usr/src/yarn-project/aztec/log:rw + - ${HOME}:${HOME} command: start --txe aztec-nargo: diff --git a/docs/docs/tutorials/simple_dapp/2_contract_deployment.md b/docs/docs/tutorials/simple_dapp/2_contract_deployment.md index 49a9e4fe5031..34d56026835f 100644 --- a/docs/docs/tutorials/simple_dapp/2_contract_deployment.md +++ b/docs/docs/tutorials/simple_dapp/2_contract_deployment.md @@ -53,7 +53,7 @@ Create a new file `src/deploy.mjs`: import { writeFileSync } from 'fs'; import { Contract, loadContractArtifact, createPXEClient } from '@aztec/aztec.js'; import { getInitialTestAccountsWallets } from '@aztec/accounts/testing'; -import TokenContractJson from "../contracts/token/target/token_contract.json" assert { type: "json" }; +import TokenContractJson from "../contracts/token/target/token_contract-Token.json" assert { type: "json" }; #include_code dapp-deploy yarn-project/end-to-end/src/sample-dapp/deploy.mjs raw diff --git a/docs/docs/tutorials/simple_dapp/3_contract_interaction.md b/docs/docs/tutorials/simple_dapp/3_contract_interaction.md index 8cd7c127ec3c..db1ce5b59640 100644 --- a/docs/docs/tutorials/simple_dapp/3_contract_interaction.md +++ b/docs/docs/tutorials/simple_dapp/3_contract_interaction.md @@ -20,7 +20,7 @@ To do this, let's first initialize a new `Contract` instance using `aztec.js` th // src/contracts.mjs import { Contract } from "@aztec/aztec.js"; import { readFileSync } from "fs"; -import TokenContractJson from "../contracts/token/target/token_contract.json" assert { type: "json" }; +import TokenContractJson from "../contracts/token/target/token_contract-Token.json" assert { type: "json" }; ``` And then add the following code for initializing the `Contract` instances: diff --git a/noir-projects/aztec-nr/aztec/src/test/helpers/cheatcodes.nr b/noir-projects/aztec-nr/aztec/src/test/helpers/cheatcodes.nr index 1ebf07f9ccb5..e00332e16000 100644 --- a/noir-projects/aztec-nr/aztec/src/test/helpers/cheatcodes.nr +++ b/noir-projects/aztec-nr/aztec/src/test/helpers/cheatcodes.nr @@ -42,12 +42,18 @@ unconstrained pub fn get_public_context_inputs() -> PublicContextInputs { oracle_get_public_context_inputs() } -unconstrained pub fn deploy(path: str, initializer: str, args: [Field], public_keys_hash: Field) -> ContractInstance { - let instance_fields = oracle_deploy(path, initializer, args, public_keys_hash); +unconstrained pub fn deploy( + path: str, + name: str, + initializer: str

, + args: [Field], + public_keys_hash: Field +) -> ContractInstance { + let instance_fields = oracle_deploy(path, name, initializer, args, public_keys_hash); ContractInstance::deserialize(instance_fields) } -unconstrained pub fn direct_storage_write(contract_address: AztecAddress, storage_slot: Field, fields: [Field; N]) { +unconstrained pub fn direct_storage_write(contract_address: AztecAddress, storage_slot: Field, fields: [Field; N]) { let _hash = direct_storage_write_oracle(contract_address, storage_slot, fields); } @@ -118,77 +124,78 @@ unconstrained pub fn set_fn_selector(selector: FunctionSelector) { } #[oracle(reset)] -fn oracle_reset() {} +unconstrained fn oracle_reset() {} #[oracle(getChainId)] -fn oracle_get_chain_id() -> Field {} +unconstrained fn oracle_get_chain_id() -> Field {} #[oracle(getVersion)] -fn oracle_get_version() -> Field {} +unconstrained fn oracle_get_version() -> Field {} #[oracle(getContractAddress)] -fn oracle_get_contract_address() -> AztecAddress {} +unconstrained fn oracle_get_contract_address() -> AztecAddress {} #[oracle(setContractAddress)] -fn oracle_set_contract_address(address: AztecAddress) {} +unconstrained fn oracle_set_contract_address(address: AztecAddress) {} #[oracle(getBlockNumber)] -fn oracle_get_block_number() -> u32 {} +unconstrained fn oracle_get_block_number() -> u32 {} #[oracle(advanceBlocksBy)] -fn oracle_advance_blocks_by(blocks: u32) {} +unconstrained fn oracle_advance_blocks_by(blocks: u32) {} #[oracle(getPrivateContextInputs)] -fn oracle_get_private_context_inputs(historical_block_number: u32) -> PrivateContextInputs {} +unconstrained fn oracle_get_private_context_inputs(historical_block_number: u32) -> PrivateContextInputs {} #[oracle(getPublicContextInputs)] -fn oracle_get_public_context_inputs() -> PublicContextInputs {} +unconstrained fn oracle_get_public_context_inputs() -> PublicContextInputs {} #[oracle(deploy)] -fn oracle_deploy( +unconstrained fn oracle_deploy( path: str, - initializer: str, + name: str, + initializer: str

, args: [Field], public_keys_hash: Field ) -> [Field; CONTRACT_INSTANCE_LENGTH] {} #[oracle(directStorageWrite)] -fn direct_storage_write_oracle( +unconstrained fn direct_storage_write_oracle( _contract_address: AztecAddress, _storage_slot: Field, _values: [Field; N] ) -> [Field; N] {} #[oracle(createAccount)] -fn oracle_create_account() -> TestAccount {} +unconstrained fn oracle_create_account() -> TestAccount {} #[oracle(addAccount)] -fn oracle_add_account(secret: Field) -> TestAccount {} +unconstrained fn oracle_add_account(secret: Field) -> TestAccount {} #[oracle(deriveKeys)] -fn oracle_derive_keys(secret: Field) -> PublicKeys {} +unconstrained fn oracle_derive_keys(secret: Field) -> PublicKeys {} #[oracle(getMsgSender)] -fn oracle_get_msg_sender() -> AztecAddress {} +unconstrained fn oracle_get_msg_sender() -> AztecAddress {} #[oracle(setMsgSender)] -fn oracle_set_msg_sender(msg_sender: AztecAddress) {} +unconstrained fn oracle_set_msg_sender(msg_sender: AztecAddress) {} #[oracle(getSideEffectsCounter)] -fn oracle_get_side_effects_counter() -> u32 {} +unconstrained fn oracle_get_side_effects_counter() -> u32 {} #[oracle(addAuthWitness)] -fn orable_add_authwit(address: AztecAddress, message_hash: Field) {} +unconstrained fn orable_add_authwit(address: AztecAddress, message_hash: Field) {} #[oracle(assertPublicCallFails)] -fn oracle_assert_public_call_fails( +unconstrained fn oracle_assert_public_call_fails( target_address: AztecAddress, function_selector: FunctionSelector, args: [Field] ) {} #[oracle(assertPrivateCallFails)] -fn oracle_assert_private_call_fails( +unconstrained fn oracle_assert_private_call_fails( target_address: AztecAddress, function_selector: FunctionSelector, argsHash: Field, @@ -198,13 +205,13 @@ fn oracle_assert_private_call_fails( ) {} #[oracle(addNullifiers)] -fn oracle_add_nullifiers(contractAddress: AztecAddress, nullifiers: [Field]) {} +unconstrained fn oracle_add_nullifiers(contractAddress: AztecAddress, nullifiers: [Field]) {} #[oracle(addNoteHashes)] -fn oracle_add_note_hashes(contractAddress: AztecAddress, inner_note_hashes: [Field]) {} +unconstrained fn oracle_add_note_hashes(contractAddress: AztecAddress, inner_note_hashes: [Field]) {} #[oracle(getFunctionSelector)] -fn oracle_get_function_selector() -> FunctionSelector {} +unconstrained fn oracle_get_function_selector() -> FunctionSelector {} #[oracle(setFunctionSelector)] -fn oracle_set_function_selector(selector: FunctionSelector) {} +unconstrained fn oracle_set_function_selector(selector: FunctionSelector) {} diff --git a/noir-projects/aztec-nr/aztec/src/test/helpers/test_environment.nr b/noir-projects/aztec-nr/aztec/src/test/helpers/test_environment.nr index 12ff64f5a646..61c1c7220406 100644 --- a/noir-projects/aztec-nr/aztec/src/test/helpers/test_environment.nr +++ b/noir-projects/aztec-nr/aztec/src/test/helpers/test_environment.nr @@ -105,12 +105,12 @@ impl TestEnvironment { address } - fn deploy(self, path: str) -> Deployer { - Deployer { path, public_keys_hash: 0 } + fn deploy(self, path: str, name: str) -> Deployer { + Deployer { path, name, public_keys_hash: 0 } } - fn deploy_self(self) -> Deployer<0> { - Deployer { path: "", public_keys_hash: 0 } + fn deploy_self(self, name: str) -> Deployer<0, M> { + Deployer { path: "", name, public_keys_hash: 0 } } fn call_private( diff --git a/noir-projects/aztec-nr/aztec/src/test/helpers/utils.nr b/noir-projects/aztec-nr/aztec/src/test/helpers/utils.nr index d6bf6243f6f9..056fa2f80a94 100644 --- a/noir-projects/aztec-nr/aztec/src/test/helpers/utils.nr +++ b/noir-projects/aztec-nr/aztec/src/test/helpers/utils.nr @@ -29,18 +29,20 @@ pub fn apply_side_effects_private(contract_address: AztecAddress, public_inputs: cheatcodes::add_note_hashes(contract_address, note_hashes); } -struct Deployer { +struct Deployer { path: str, + name: str, public_keys_hash: Field } -impl Deployer { - pub fn with_private_initializer( +impl Deployer { + pub fn with_private_initializer( self, call_interface: C - ) -> ContractInstance where C: CallInterface { + ) -> ContractInstance where C: CallInterface { let instance = cheatcodes::deploy( self.path, + self.name, call_interface.get_name(), call_interface.get_args(), self.public_keys_hash @@ -64,12 +66,13 @@ impl Deployer { instance } - pub fn with_public_initializer( + pub fn with_public_initializer( self, call_interface: C - ) -> ContractInstance where C: CallInterface { + ) -> ContractInstance where C: CallInterface { let instance = cheatcodes::deploy( self.path, + self.name, call_interface.get_name(), call_interface.get_args(), self.public_keys_hash @@ -94,7 +97,7 @@ impl Deployer { } pub fn without_initializer(self) -> ContractInstance { - cheatcodes::deploy(self.path, "", &[], self.public_keys_hash) + cheatcodes::deploy(self.path, self.name, "", &[], self.public_keys_hash) } } diff --git a/noir-projects/noir-contracts/contracts/counter_contract/src/main.nr b/noir-projects/noir-contracts/contracts/counter_contract/src/main.nr index 5350a69f7b2a..97e5e40dec34 100644 --- a/noir-projects/noir-contracts/contracts/counter_contract/src/main.nr +++ b/noir-projects/noir-contracts/contracts/counter_contract/src/main.nr @@ -57,7 +57,7 @@ contract Counter { // Deploy contract and initialize let initializer = Counter::interface().initialize(initial_value as u64, owner, outgoing_viewer); - let counter_contract = env.deploy_self().with_private_initializer(initializer); + let counter_contract = env.deploy_self("Counter").with_private_initializer(initializer); let contract_address = counter_contract.to_address(); // docs:start:txe_test_read_notes diff --git a/noir-projects/noir-contracts/contracts/parent_contract/src/main.nr b/noir-projects/noir-contracts/contracts/parent_contract/src/main.nr index 8bce0f09f642..fb6181311417 100644 --- a/noir-projects/noir-contracts/contracts/parent_contract/src/main.nr +++ b/noir-projects/noir-contracts/contracts/parent_contract/src/main.nr @@ -257,7 +257,7 @@ contract Parent { let owner = env.create_account(); // Deploy child contract - let child_contract = env.deploy("./@child_contract").without_initializer(); + let child_contract = env.deploy("./@child_contract", "Child").without_initializer(); let child_contract_address = child_contract.to_address(); cheatcodes::advance_blocks_by(1); diff --git a/noir-projects/noir-contracts/contracts/private_token_contract/src/test/utils.nr b/noir-projects/noir-contracts/contracts/private_token_contract/src/test/utils.nr index fb660d6ef45e..441b0e962412 100644 --- a/noir-projects/noir-contracts/contracts/private_token_contract/src/test/utils.nr +++ b/noir-projects/noir-contracts/contracts/private_token_contract/src/test/utils.nr @@ -15,7 +15,7 @@ pub fn setup(with_account_contracts: bool) -> (&mut TestEnvironment, AztecAddres let owner = env.create_account_contract(1); let recipient = env.create_account_contract(2); // Deploy canonical auth registry - let _auth_registry = env.deploy("@aztec/noir-contracts.js/AuthRegistry").without_initializer(); + let _auth_registry = env.deploy("./@auth_registry_contract", "AuthRegistry").without_initializer(); (owner, recipient) } else { let owner = env.create_account(); @@ -33,7 +33,7 @@ pub fn setup(with_account_contracts: bool) -> (&mut TestEnvironment, AztecAddres "TT00000000000000000000000000000", 18 ); - let token_contract = env.deploy("@aztec/noir-contracts.js/PrivateToken").with_public_initializer(initializer_call_interface); + let token_contract = env.deploy_self("PrivateToken").with_public_initializer(initializer_call_interface); let token_contract_address = token_contract.to_address(); env.advance_block_by(6); (&mut env, token_contract_address, owner, recipient) diff --git a/noir-projects/noir-contracts/contracts/token_contract/src/test/utils.nr b/noir-projects/noir-contracts/contracts/token_contract/src/test/utils.nr index 4435d6e0da67..f914fbf22800 100644 --- a/noir-projects/noir-contracts/contracts/token_contract/src/test/utils.nr +++ b/noir-projects/noir-contracts/contracts/token_contract/src/test/utils.nr @@ -15,7 +15,7 @@ pub fn setup(with_account_contracts: bool) -> (&mut TestEnvironment, AztecAddres let owner = env.create_account_contract(1); let recipient = env.create_account_contract(2); // Deploy canonical auth registry - let _auth_registry = env.deploy("./@auth_registry_contract").without_initializer(); + let _auth_registry = env.deploy("./@auth_registry_contract", "AuthRegistry").without_initializer(); (owner, recipient) } else { let owner = env.create_account(); @@ -33,7 +33,7 @@ pub fn setup(with_account_contracts: bool) -> (&mut TestEnvironment, AztecAddres "TT00000000000000000000000000000", 18 ); - let token_contract = env.deploy_self().with_public_initializer(initializer_call_interface); + let token_contract = env.deploy_self("Token").with_public_initializer(initializer_call_interface); let token_contract_address = token_contract.to_address(); env.advance_block_by(1); (&mut env, token_contract_address, owner, recipient) diff --git a/noir/noir-repo/tooling/acvm_cli/src/cli/execute_cmd.rs b/noir/noir-repo/tooling/acvm_cli/src/cli/execute_cmd.rs index e58d8128f9a2..c453936568ce 100644 --- a/noir/noir-repo/tooling/acvm_cli/src/cli/execute_cmd.rs +++ b/noir/noir-repo/tooling/acvm_cli/src/cli/execute_cmd.rs @@ -73,7 +73,7 @@ pub(crate) fn execute_program_from_witness( &program, inputs_map, &Bn254BlackBoxSolver, - &mut DefaultForeignCallExecutor::new(true, None, None), + &mut DefaultForeignCallExecutor::new(true, None, None, None), ) .map_err(CliError::CircuitExecutionError) } diff --git a/noir/noir-repo/tooling/debugger/src/foreign_calls.rs b/noir/noir-repo/tooling/debugger/src/foreign_calls.rs index 0f428049eaa6..6a773a4b3484 100644 --- a/noir/noir-repo/tooling/debugger/src/foreign_calls.rs +++ b/noir/noir-repo/tooling/debugger/src/foreign_calls.rs @@ -49,7 +49,7 @@ pub struct DefaultDebugForeignCallExecutor { impl DefaultDebugForeignCallExecutor { pub fn new(show_output: bool) -> Self { Self { - executor: DefaultForeignCallExecutor::new(show_output, None, None), + executor: DefaultForeignCallExecutor::new(show_output, None, None, None), debug_vars: DebugVars::default(), } } diff --git a/noir/noir-repo/tooling/fuzzer/src/lib.rs b/noir/noir-repo/tooling/fuzzer/src/lib.rs index d6616bcb1284..35a614663dc7 100644 --- a/noir/noir-repo/tooling/fuzzer/src/lib.rs +++ b/noir/noir-repo/tooling/fuzzer/src/lib.rs @@ -80,7 +80,7 @@ impl FuzzedExecutor { &self.program.bytecode, initial_witness, &StubbedBlackBoxSolver, - &mut DefaultForeignCallExecutor::::new(false, None, None), + &mut DefaultForeignCallExecutor::::new(false, None, None, None), ); // TODO: Add handling for `vm.assume` equivalent diff --git a/noir/noir-repo/tooling/lsp/src/requests/test_run.rs b/noir/noir-repo/tooling/lsp/src/requests/test_run.rs index 114ca499bdaf..532c684020ad 100644 --- a/noir/noir-repo/tooling/lsp/src/requests/test_run.rs +++ b/noir/noir-repo/tooling/lsp/src/requests/test_run.rs @@ -86,7 +86,8 @@ fn on_test_run_request_inner( &test_function, false, None, - workspace_file_manager.path(test_function.file_id()).map(|p| p.to_path_buf()), + Some(workspace.root_dir.clone()), + Some(package.name.to_string()), &CompileOptions::default(), ); let result = match test_result { diff --git a/noir/noir-repo/tooling/nargo/src/ops/foreign_calls.rs b/noir/noir-repo/tooling/nargo/src/ops/foreign_calls.rs index 765216904a6c..30785949a46e 100644 --- a/noir/noir-repo/tooling/nargo/src/ops/foreign_calls.rs +++ b/noir/noir-repo/tooling/nargo/src/ops/foreign_calls.rs @@ -114,8 +114,10 @@ pub struct DefaultForeignCallExecutor { show_output: bool, /// JSON RPC client to resolve foreign calls external_resolver: Option, - /// Path to the file currently in execution. - program_artifact_path: Option, + /// Root path to the program or workspace in execution. + root_path: Option, + /// Name of the package in execution + package_name: Option, } #[derive(Debug, Serialize, Deserialize)] @@ -132,15 +134,19 @@ struct ResolveForeignCallRequest { function_call: ForeignCallWaitInfo, #[serde(skip_serializing_if = "Option::is_none")] - /// Path to the file of the current program execution. - program_artifact_path: Option, + /// Root path to the program or workspace in execution. + root_path: Option, + #[serde(skip_serializing_if = "Option::is_none")] + /// Name of the package in execution + package_name: Option, } impl DefaultForeignCallExecutor { pub fn new( show_output: bool, resolver_url: Option<&str>, - program_artifact_path: Option, + root_path: Option, + package_name: Option, ) -> Self { let oracle_resolver = resolver_url.map(|resolver_url| { let mut transport_builder = @@ -160,7 +166,8 @@ impl DefaultForeignCallExecutor { id: rand::thread_rng().gen(), mocked_responses: Vec::new(), last_mock_id: 0, - program_artifact_path, + root_path, + package_name, } } } @@ -315,10 +322,11 @@ impl Deserialize<'a>> ForeignCallExecutor let encoded_params = vec![build_json_rpc_arg(ResolveForeignCallRequest { session_id: self.id, function_call: foreign_call.clone(), - program_artifact_path: self - .program_artifact_path + root_path: self + .root_path .clone() .map(|path| path.to_str().unwrap().to_string()), + package_name: self.package_name.clone(), })]; let req = @@ -419,7 +427,8 @@ mod tests { fn test_oracle_resolver_echo() { let (server, url) = build_oracle_server(); - let mut executor = DefaultForeignCallExecutor::::new(false, Some(&url), None); + let mut executor = + DefaultForeignCallExecutor::::new(false, Some(&url), None, None); let foreign_call = ForeignCallWaitInfo { function: "echo".to_string(), @@ -436,7 +445,7 @@ mod tests { fn test_oracle_resolver_sum() { let (server, url) = build_oracle_server(); - let mut executor = DefaultForeignCallExecutor::new(false, Some(&url), None); + let mut executor = DefaultForeignCallExecutor::new(false, Some(&url), None, None); let foreign_call = ForeignCallWaitInfo { function: "sum".to_string(), @@ -453,7 +462,8 @@ mod tests { fn foreign_call_executor_id_is_persistent() { let (server, url) = build_oracle_server(); - let mut executor = DefaultForeignCallExecutor::::new(false, Some(&url), None); + let mut executor = + DefaultForeignCallExecutor::::new(false, Some(&url), None, None); let foreign_call = ForeignCallWaitInfo { function: "id".to_string(), inputs: Vec::new() }; @@ -469,9 +479,9 @@ mod tests { let (server, url) = build_oracle_server(); let mut executor_1 = - DefaultForeignCallExecutor::::new(false, Some(&url), None); + DefaultForeignCallExecutor::::new(false, Some(&url), None, None); let mut executor_2 = - DefaultForeignCallExecutor::::new(false, Some(&url), None); + DefaultForeignCallExecutor::::new(false, Some(&url), None, None); let foreign_call = ForeignCallWaitInfo { function: "id".to_string(), inputs: Vec::new() }; diff --git a/noir/noir-repo/tooling/nargo/src/ops/test.rs b/noir/noir-repo/tooling/nargo/src/ops/test.rs index 66015c8053cf..ecfedd3e1185 100644 --- a/noir/noir-repo/tooling/nargo/src/ops/test.rs +++ b/noir/noir-repo/tooling/nargo/src/ops/test.rs @@ -31,7 +31,8 @@ pub fn run_test>( test_function: &TestFunction, show_output: bool, foreign_call_resolver_url: Option<&str>, - program_artifact_path: Option, + root_path: Option, + package_name: Option, config: &CompileOptions, ) -> TestStatus { let compiled_program = compile_no_check(context, config, test_function.get_id(), None, false); @@ -46,7 +47,8 @@ pub fn run_test>( &mut DefaultForeignCallExecutor::new( show_output, foreign_call_resolver_url, - program_artifact_path, + root_path, + package_name, ), ); test_status_program_compile_pass( diff --git a/noir/noir-repo/tooling/nargo_cli/src/cli/compile_cmd.rs b/noir/noir-repo/tooling/nargo_cli/src/cli/compile_cmd.rs index 1034cf9813dd..e83b1728c932 100644 --- a/noir/noir-repo/tooling/nargo_cli/src/cli/compile_cmd.rs +++ b/noir/noir-repo/tooling/nargo_cli/src/cli/compile_cmd.rs @@ -233,8 +233,12 @@ fn save_contract( target_dir: &Path, show_artifact_paths: bool, ) { - let artifact_path = - save_contract_to_file(&contract.into(), package.name.to_string().as_str(), target_dir); + let contract_name = contract.name.clone(); + let artifact_path = save_contract_to_file( + &contract.into(), + &format!("{}-{}", package.name, contract_name), + target_dir, + ); if show_artifact_paths { println!("Saved contract artifact to: {}", artifact_path.display()); } diff --git a/noir/noir-repo/tooling/nargo_cli/src/cli/execute_cmd.rs b/noir/noir-repo/tooling/nargo_cli/src/cli/execute_cmd.rs index 356689d01c93..0f7773d7ab70 100644 --- a/noir/noir-repo/tooling/nargo_cli/src/cli/execute_cmd.rs +++ b/noir/noir-repo/tooling/nargo_cli/src/cli/execute_cmd.rs @@ -75,7 +75,8 @@ pub(crate) fn run(args: ExecuteCommand, config: NargoConfig) -> Result<(), CliEr package, &args.prover_name, args.oracle_resolver.as_deref(), - Some(program_artifact_path.clone()), + Some(workspace.root_dir.clone()), + Some(package.name.to_string()), )?; println!("[{}] Circuit witness successfully solved", package.name); @@ -96,13 +97,14 @@ fn execute_program_and_decode( package: &Package, prover_name: &str, foreign_call_resolver_url: Option<&str>, - program_artifact_path: Option, + root_path: Option, + package_name: Option, ) -> Result<(Option, WitnessStack), CliError> { // Parse the initial witness values from Prover.toml let (inputs_map, _) = read_inputs_from_file(&package.root_dir, prover_name, Format::Toml, &program.abi)?; let witness_stack = - execute_program(&program, &inputs_map, foreign_call_resolver_url, program_artifact_path)?; + execute_program(&program, &inputs_map, foreign_call_resolver_url, root_path, package_name)?; // Get the entry point witness for the ABI let main_witness = &witness_stack.peek().expect("Should have at least one witness on the stack").witness; @@ -115,7 +117,8 @@ pub(crate) fn execute_program( compiled_program: &CompiledProgram, inputs_map: &InputMap, foreign_call_resolver_url: Option<&str>, - program_artifact_path: Option, + root_path: Option, + package_name: Option, ) -> Result, CliError> { let initial_witness = compiled_program.abi.encode(inputs_map, None)?; @@ -126,7 +129,8 @@ pub(crate) fn execute_program( &mut DefaultForeignCallExecutor::new( true, foreign_call_resolver_url, - program_artifact_path, + root_path, + package_name, ), ); match solved_witness_stack_err { diff --git a/noir/noir-repo/tooling/nargo_cli/src/cli/test_cmd.rs b/noir/noir-repo/tooling/nargo_cli/src/cli/test_cmd.rs index 83d3acd05d05..ca3ff165466c 100644 --- a/noir/noir-repo/tooling/nargo_cli/src/cli/test_cmd.rs +++ b/noir/noir-repo/tooling/nargo_cli/src/cli/test_cmd.rs @@ -92,7 +92,8 @@ pub(crate) fn run(args: TestCommand, config: NargoConfig) -> Result<(), CliError pattern, args.show_output, args.oracle_resolver.as_deref(), - Some(workspace.package_build_path(package)), + Some(workspace.root_dir.clone()), + Some(package.name.to_string()), &args.compile_options, ) }) @@ -128,7 +129,8 @@ fn run_tests + Default>( fn_name: FunctionNameMatch, show_output: bool, foreign_call_resolver_url: Option<&str>, - program_artifact_path: Option, + root_path: Option, + package_name: Option, compile_options: &CompileOptions, ) -> Result, CliError> { let test_functions = @@ -149,7 +151,8 @@ fn run_tests + Default>( &test_name, show_output, foreign_call_resolver_url, - program_artifact_path.clone(), + root_path.clone(), + package_name.clone(), compile_options, ); @@ -168,7 +171,8 @@ fn run_test + Default>( fn_name: &str, show_output: bool, foreign_call_resolver_url: Option<&str>, - program_artifact_path: Option, + root_path: Option, + package_name: Option, compile_options: &CompileOptions, ) -> TestStatus { // This is really hacky but we can't share `Context` or `S` across threads. @@ -205,7 +209,8 @@ fn run_test + Default>( test_function, show_output, foreign_call_resolver_url, - program_artifact_path, + root_path, + package_name, compile_options, ) } else { diff --git a/yarn-project/circuits.js/scripts/copy-contracts.sh b/yarn-project/circuits.js/scripts/copy-contracts.sh index c679671c1efa..2b9f3838a439 100755 --- a/yarn-project/circuits.js/scripts/copy-contracts.sh +++ b/yarn-project/circuits.js/scripts/copy-contracts.sh @@ -2,5 +2,5 @@ set -euo pipefail mkdir -p ./fixtures -cp "../../noir-projects/noir-contracts/target/benchmarking_contract.json" ./fixtures/Benchmarking.test.json -cp "../../noir-projects/noir-contracts/target/test_contract.json" ./fixtures/Test.test.json +cp "../../noir-projects/noir-contracts/target/benchmarking_contract-Benchmarking.json" ./fixtures/Benchmarking.test.json +cp "../../noir-projects/noir-contracts/target/test_contract-Test.json" ./fixtures/Test.test.json diff --git a/yarn-project/protocol-contracts/scripts/copy-contracts.sh b/yarn-project/protocol-contracts/scripts/copy-contracts.sh index 5df6d94e24d7..5001bf8c2548 100755 --- a/yarn-project/protocol-contracts/scripts/copy-contracts.sh +++ b/yarn-project/protocol-contracts/scripts/copy-contracts.sh @@ -3,12 +3,12 @@ set -euo pipefail mkdir -p ./artifacts contracts=( - contract_class_registerer_contract - contract_instance_deployer_contract - gas_token_contract - key_registry_contract - auth_registry_contract - multi_call_entrypoint_contract + contract_class_registerer_contract-ContractClassRegisterer + contract_instance_deployer_contract-ContractInstanceDeployer + gas_token_contract-GasToken + key_registry_contract-KeyRegistry + auth_registry_contract-AuthRegistry + multi_call_entrypoint_contract-MultiCallEntrypoint ) diff --git a/yarn-project/txe/src/index.ts b/yarn-project/txe/src/index.ts index 295e3eaa8ae3..ac7046f00b02 100644 --- a/yarn-project/txe/src/index.ts +++ b/yarn-project/txe/src/index.ts @@ -18,7 +18,8 @@ type MethodNames = { type TXEForeignCallInput = { session_id: number; function: MethodNames | 'reset'; - program_artifact_path: string; + root_path: string; + package_name: string; inputs: any[]; }; @@ -30,7 +31,8 @@ class TXEDispatcher { session_id: sessionId, function: functionName, inputs, - program_artifact_path, + root_path, + package_name, }: TXEForeignCallInput): Promise { this.logger.debug(`Calling ${functionName} on session ${sessionId}`); @@ -48,25 +50,30 @@ class TXEDispatcher { const pathStr = fromArray(inputs[0] as ForeignCallArray) .map(char => String.fromCharCode(char.toNumber())) .join(''); - let inputPath = program_artifact_path; + const contractName = fromArray(inputs[1] as ForeignCallArray) + .map(char => String.fromCharCode(char.toNumber())) + .join(''); + let artifactPath = ''; // Is not the same contract we're testing - if (pathStr != '') { + if (!pathStr) { + artifactPath = join(root_path, './target', `${package_name}-${contractName}.json`); + } else { // Workspace if (pathStr.includes('@')) { const [workspace, pkg] = pathStr.split('@'); - const targetPath = join(dirname(inputPath), '../', workspace, './target'); + const targetPath = join(root_path, workspace, './target'); this.logger.debug(`Looking for compiled artifact in workspace ${targetPath}`); - inputPath = join(targetPath, `${pkg}.json`); + artifactPath = join(targetPath, `${pkg}-${contractName}.json`); } else { // Individual contract - const targetPath = join(dirname(inputPath), '../', pathStr, './target'); + const targetPath = join(root_path, pathStr, './target'); this.logger.debug(`Looking for compiled artifact in ${targetPath}`); - [inputPath] = (await readdir(targetPath)).filter(file => file.endsWith('.json')); + [artifactPath] = (await readdir(targetPath)).filter(file => file.endsWith(`-${contractName}.json`)); } } - this.logger.debug(`Loading compiled artifact ${inputPath}`); - const artifact = loadContractArtifact(JSON.parse(await readFile(inputPath, 'utf-8'))); - inputs[0] = artifact; + this.logger.debug(`Loading compiled artifact ${artifactPath}`); + const artifact = loadContractArtifact(JSON.parse(await readFile(artifactPath, 'utf-8'))); + inputs.splice(0, 2, artifact); } const txeService = TXESessions.get(sessionId); const response = await (txeService as any)[functionName](...inputs); diff --git a/yarn-project/types/scripts/copy-contracts.sh b/yarn-project/types/scripts/copy-contracts.sh index 47d12379c9c7..d64da5b967a2 100755 --- a/yarn-project/types/scripts/copy-contracts.sh +++ b/yarn-project/types/scripts/copy-contracts.sh @@ -2,4 +2,4 @@ set -euo pipefail mkdir -p ./fixtures -cp "../../noir-projects/noir-contracts/target/benchmarking_contract.json" ./fixtures/Benchmarking.test.json +cp "../../noir-projects/noir-contracts/target/benchmarking_contract-Benchmarking.json" ./fixtures/Benchmarking.test.json From 91d2185615de77764db12debbf53fd9ee177406c Mon Sep 17 00:00:00 2001 From: thunkar Date: Mon, 22 Jul 2024 13:52:18 +0000 Subject: [PATCH 07/14] clippy --- noir/noir-repo/tooling/nargo/src/ops/test.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/noir/noir-repo/tooling/nargo/src/ops/test.rs b/noir/noir-repo/tooling/nargo/src/ops/test.rs index ecfedd3e1185..b2af5d9780ba 100644 --- a/noir/noir-repo/tooling/nargo/src/ops/test.rs +++ b/noir/noir-repo/tooling/nargo/src/ops/test.rs @@ -25,6 +25,7 @@ impl TestStatus { } } +#[allow(clippy::too_many_arguments)] pub fn run_test>( blackbox_solver: &B, context: &mut Context, From d3a5cf7b223e9430c9268117c2c3a6491e77d714 Mon Sep 17 00:00:00 2001 From: thunkar Date: Mon, 22 Jul 2024 14:06:26 +0000 Subject: [PATCH 08/14] more clippy --- noir/noir-repo/tooling/nargo_cli/src/cli/test_cmd.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/noir/noir-repo/tooling/nargo_cli/src/cli/test_cmd.rs b/noir/noir-repo/tooling/nargo_cli/src/cli/test_cmd.rs index ca3ff165466c..5afa456d4830 100644 --- a/noir/noir-repo/tooling/nargo_cli/src/cli/test_cmd.rs +++ b/noir/noir-repo/tooling/nargo_cli/src/cli/test_cmd.rs @@ -122,6 +122,7 @@ pub(crate) fn run(args: TestCommand, config: NargoConfig) -> Result<(), CliError } } +#[allow(clippy::too_many_arguments)] fn run_tests + Default>( file_manager: &FileManager, parsed_files: &ParsedFiles, @@ -164,6 +165,7 @@ fn run_tests + Default>( Ok(test_report) } +#[allow(clippy::too_many_arguments)] fn run_test + Default>( file_manager: &FileManager, parsed_files: &ParsedFiles, From 9c61ec161442f44267d113647c0d25e7fe2505f2 Mon Sep 17 00:00:00 2001 From: thunkar Date: Mon, 22 Jul 2024 14:21:52 +0000 Subject: [PATCH 09/14] fixed nargo tests --- noir/noir-repo/tooling/nargo_cli/tests/stdlib-tests.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/noir/noir-repo/tooling/nargo_cli/tests/stdlib-tests.rs b/noir/noir-repo/tooling/nargo_cli/tests/stdlib-tests.rs index cc44b269c6a6..f73d5ce781a3 100644 --- a/noir/noir-repo/tooling/nargo_cli/tests/stdlib-tests.rs +++ b/noir/noir-repo/tooling/nargo_cli/tests/stdlib-tests.rs @@ -61,6 +61,8 @@ fn run_stdlib_tests() { &test_function, false, None, + None, + None, &CompileOptions::default(), ) } else { From 7188be87357d366b7b45733ca90f4277aa1aaa64 Mon Sep 17 00:00:00 2001 From: thunkar Date: Mon, 22 Jul 2024 15:04:46 +0000 Subject: [PATCH 10/14] format fixes --- .../builder/src/contract-interface-gen/codegen.ts | 4 ++-- yarn-project/builder/src/index.ts | 2 +- yarn-project/txe/package.json | 1 + yarn-project/txe/src/index.ts | 10 ++++++---- yarn-project/txe/src/txe_service/txe_service.ts | 4 ++-- yarn-project/txe/tsconfig.json | 3 +++ yarn-project/yarn.lock | 1 + 7 files changed, 16 insertions(+), 9 deletions(-) diff --git a/yarn-project/builder/src/contract-interface-gen/codegen.ts b/yarn-project/builder/src/contract-interface-gen/codegen.ts index 62ccdaea9ad3..dea5cb8417a0 100644 --- a/yarn-project/builder/src/contract-interface-gen/codegen.ts +++ b/yarn-project/builder/src/contract-interface-gen/codegen.ts @@ -17,7 +17,7 @@ export type GenerateCodeOptions = { force?: boolean }; * Generates Noir interface or Typescript interface for a folder or single file from a Noir compilation artifact. */ export async function generateCode(outputPath: string, fileOrDirPath: string, opts: GenerateCodeOptions = {}) { - readCache(); + await readCache(); const results = []; const stats = await stat(fileOrDirPath); @@ -42,7 +42,7 @@ export async function generateCode(outputPath: string, fileOrDirPath: string, op async function generateFromNoirAbi(outputPath: string, noirAbiPath: string, opts: GenerateCodeOptions = {}) { const fileName = path.basename(noirAbiPath); const currentHash = await generateFileHash(noirAbiPath); - let cachedInstance = isCacheValid(fileName, currentHash); + const cachedInstance = isCacheValid(fileName, currentHash); if (cachedInstance && !opts.force) { console.log(`${fileName} has not changed. Skipping generation.`); return `${outputPath}/${cachedInstance.contractName}.ts`; diff --git a/yarn-project/builder/src/index.ts b/yarn-project/builder/src/index.ts index f38c74f6e87a..77df374c353e 100644 --- a/yarn-project/builder/src/index.ts +++ b/yarn-project/builder/src/index.ts @@ -1,4 +1,4 @@ -import { Command } from 'commander'; +import { type Command } from 'commander'; import { dirname } from 'path'; export function injectCommands(program: Command) { diff --git a/yarn-project/txe/package.json b/yarn-project/txe/package.json index 8c8e45cb0dc7..fc21460d9264 100644 --- a/yarn-project/txe/package.json +++ b/yarn-project/txe/package.json @@ -57,6 +57,7 @@ ] }, "dependencies": { + "@aztec/accounts": "workspace:^", "@aztec/archiver": "workspace:^", "@aztec/aztec.js": "workspace:^", "@aztec/circuit-types": "workspace:^", diff --git a/yarn-project/txe/src/index.ts b/yarn-project/txe/src/index.ts index ac7046f00b02..2c3c157faf50 100644 --- a/yarn-project/txe/src/index.ts +++ b/yarn-project/txe/src/index.ts @@ -4,10 +4,10 @@ import { JsonRpcServer } from '@aztec/foundation/json-rpc/server'; import { type Logger } from '@aztec/foundation/log'; import { readFile, readdir } from 'fs/promises'; -import { dirname, join } from 'path'; +import { join } from 'path'; import { TXEService } from './txe_service/txe_service.js'; -import { ForeignCallArray, type ForeignCallResult, fromArray, toForeignCallResult } from './util/encoding.js'; +import { type ForeignCallArray, type ForeignCallResult, fromArray, toForeignCallResult } from './util/encoding.js'; const TXESessions = new Map(); @@ -26,14 +26,15 @@ type TXEForeignCallInput = { class TXEDispatcher { constructor(private logger: Logger) {} - // eslint-disable-next-line camelcase + /* eslint-disable camelcase */ async resolve_foreign_call({ session_id: sessionId, function: functionName, inputs, root_path, package_name, - }: TXEForeignCallInput): Promise { + }: /* eslint-enable camelcase */ + TXEForeignCallInput): Promise { this.logger.debug(`Calling ${functionName} on session ${sessionId}`); if (!TXESessions.has(sessionId) && functionName != 'reset') { @@ -56,6 +57,7 @@ class TXEDispatcher { let artifactPath = ''; // Is not the same contract we're testing if (!pathStr) { + // eslint-disable-next-line camelcase artifactPath = join(root_path, './target', `${package_name}-${contractName}.json`); } else { // Workspace diff --git a/yarn-project/txe/src/txe_service/txe_service.ts b/yarn-project/txe/src/txe_service/txe_service.ts index e97f49690be3..a8f772931ca1 100644 --- a/yarn-project/txe/src/txe_service/txe_service.ts +++ b/yarn-project/txe/src/txe_service/txe_service.ts @@ -12,7 +12,7 @@ import { getContractInstanceFromDeployParams, } from '@aztec/circuits.js'; import { computePublicDataTreeLeafSlot } from '@aztec/circuits.js/hash'; -import { ContractArtifact, NoteSelector } from '@aztec/foundation/abi'; +import { type ContractArtifact, NoteSelector } from '@aztec/foundation/abi'; import { AztecAddress } from '@aztec/foundation/aztec-address'; import { type Logger } from '@aztec/foundation/log'; import { KeyStore } from '@aztec/key-store'; @@ -172,7 +172,7 @@ export class TXEService { const keys = (this.typedOracle as TXE).deriveKeys(fromSingle(secret)); const args = [keys.publicKeys.masterIncomingViewingPublicKey.x, keys.publicKeys.masterIncomingViewingPublicKey.y]; const hash = keys.publicKeys.hash(); - let artifact = SchnorrAccountContractArtifact; + const artifact = SchnorrAccountContractArtifact; const instance = getContractInstanceFromDeployParams(artifact, { constructorArgs: args, skipArgsDecoding: true, diff --git a/yarn-project/txe/tsconfig.json b/yarn-project/txe/tsconfig.json index a57cb2cd9946..ec9add87a8b8 100644 --- a/yarn-project/txe/tsconfig.json +++ b/yarn-project/txe/tsconfig.json @@ -6,6 +6,9 @@ "tsBuildInfoFile": ".tsbuildinfo" }, "references": [ + { + "path": "../accounts" + }, { "path": "../archiver" }, diff --git a/yarn-project/yarn.lock b/yarn-project/yarn.lock index 2061be278281..6e272cebcd2d 100644 --- a/yarn-project/yarn.lock +++ b/yarn-project/yarn.lock @@ -955,6 +955,7 @@ __metadata: version: 0.0.0-use.local resolution: "@aztec/txe@workspace:txe" dependencies: + "@aztec/accounts": "workspace:^" "@aztec/archiver": "workspace:^" "@aztec/aztec.js": "workspace:^" "@aztec/circuit-types": "workspace:^" From 87717c0fe8c6c799295e6bf9654ec67175c7760b Mon Sep 17 00:00:00 2001 From: thunkar Date: Mon, 22 Jul 2024 15:52:49 +0000 Subject: [PATCH 11/14] updated docs --- .../guides/smart_contracts/testing_contracts/index.md | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/docs/docs/guides/smart_contracts/testing_contracts/index.md b/docs/docs/guides/smart_contracts/testing_contracts/index.md index acc6bf3baaed..3c7ca0c85996 100644 --- a/docs/docs/guides/smart_contracts/testing_contracts/index.md +++ b/docs/docs/guides/smart_contracts/testing_contracts/index.md @@ -70,7 +70,14 @@ Writing tests in contracts requires importing additional modules from Aztec.nr. ### Deploying contracts ```rust -let deployer = env.deploy("path_to_contract_ts_interface"); + +// Depending on whether we want to deploy the contract we're currently on or an auxiliary one we want to test calls to + +let deployer = env.deploy_self("ContractName"); + +// or + +let deployer = env.deploy("path_to_contract_root_folder_where_nargo_toml_is", "ContractName"); // Now one of these can be called, depending on the contract and their possible initialization options. // Remember a contract can only be initialized once. @@ -89,7 +96,7 @@ let my_contract_instance = deployer.without_initializer(); ``` :::warning -At the moment, TXE uses the generated contract TypeScript interfaces to perform deployments, and they must be provided as either an absolute path, a relative path to TXE's location or a module in an npm direct dependency such as `@aztec/noir-contracts.js`. It is not always necessary to deploy a contract in order to test it, but sometimes it's inevitable (when testing functions that depend on the contract being initialized, or contracts that call others for example) **It is important to keep them up to date**, as TXE cannot recompile them on changes. This will be improved in the future. +It is not always necessary to deploy a contract in order to test it, but sometimes it's inevitable (when testing functions that depend on the contract being initialized, or contracts that call others for example) **It is important to keep them up to date**, as TXE cannot recompile them on changes. Think of it as regenerating the bytecode and ABI so it becomes accessible externally. ::: ### Calling functions From 8260d438500cf81e59c1642f7c2fe3a1296b7c8a Mon Sep 17 00:00:00 2001 From: thunkar Date: Mon, 22 Jul 2024 16:12:29 +0000 Subject: [PATCH 12/14] docs --- .../testing_contracts/index.md | 8 +++++-- docs/docs/migration_notes.md | 23 +++++++++++++++++++ 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/docs/docs/guides/smart_contracts/testing_contracts/index.md b/docs/docs/guides/smart_contracts/testing_contracts/index.md index 3c7ca0c85996..187035425eff 100644 --- a/docs/docs/guides/smart_contracts/testing_contracts/index.md +++ b/docs/docs/guides/smart_contracts/testing_contracts/index.md @@ -71,14 +71,18 @@ Writing tests in contracts requires importing additional modules from Aztec.nr. ```rust -// Depending on whether we want to deploy the contract we're currently on or an auxiliary one we want to test calls to +// Deploy the contract we're currently on let deployer = env.deploy_self("ContractName"); -// or +// Deploy a standalone contract in a path relative to the current one (always from the location of Nargo.toml) let deployer = env.deploy("path_to_contract_root_folder_where_nargo_toml_is", "ContractName"); +// Deploy a contract in a workspace + +let deployer = env.deploy("path_to_workspace_root_folder_where_main_nargo_toml_is@package_name", "ContractName"); + // Now one of these can be called, depending on the contract and their possible initialization options. // Remember a contract can only be initialized once. diff --git a/docs/docs/migration_notes.md b/docs/docs/migration_notes.md index e72821881105..e4b3741d9689 100644 --- a/docs/docs/migration_notes.md +++ b/docs/docs/migration_notes.md @@ -7,6 +7,29 @@ keywords: [sandbox, aztec, notes, migration, updating, upgrading] Aztec is in full-speed development. Literally every version breaks compatibility with the previous ones. This page attempts to target errors and difficulties you might encounter when upgrading, and how to resolve them. ## 0.xx.0 + +# [Aztec sandbox] TXE deployment changes + +The way simulated deployments are done in TXE tests has changed to avoid relying on TS interfaces. It is now possible to do it by directly pointing to a Noir standalone contract or workspace: + +```diff +-let deployer = env.deploy("path_to_contract_ts_interface"); ++let deployer = env.deploy("path_to_contract_root_folder_where_nargo_toml_is", "ContractName"); +``` + +```rust +// Extended syntax for more use cases: + +// The contract we're testing +env.deploy_self("ContractName"); // We have to provide ContractName since nargo it's ready to support multi-contract files + +// A contract from a workspace +env.deploy("../path/to/workspace@package_name", "ContractName"); // This format allows locating the artifact in the root workspace target folder, regardless of internal code organization +``` + +The deploy function returns a `Deployer`, which requires performing a subsequent call to `without_initializer()`, `with_private_initializer()` or `with_public_initializer()` just like before in order to **actually** deploy the contract. + +## 0.46.3 ### [Aztec sandbox] Command refactor and unification + `aztec test` Sandbox commands have been cleaned up and simplified. Doing `aztec-up` now gets you the following top-level commands: From 87eddc82864fd507641a1f635e3b0d1b233c66eb Mon Sep 17 00:00:00 2001 From: thunkar Date: Mon, 22 Jul 2024 16:13:19 +0000 Subject: [PATCH 13/14] better wording --- docs/docs/migration_notes.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/docs/migration_notes.md b/docs/docs/migration_notes.md index e4b3741d9689..023f52c13c38 100644 --- a/docs/docs/migration_notes.md +++ b/docs/docs/migration_notes.md @@ -17,13 +17,13 @@ The way simulated deployments are done in TXE tests has changed to avoid relying +let deployer = env.deploy("path_to_contract_root_folder_where_nargo_toml_is", "ContractName"); ``` -```rust -// Extended syntax for more use cases: +Extended syntax for more use cases: +```rust // The contract we're testing env.deploy_self("ContractName"); // We have to provide ContractName since nargo it's ready to support multi-contract files -// A contract from a workspace +// A contract in a workspace env.deploy("../path/to/workspace@package_name", "ContractName"); // This format allows locating the artifact in the root workspace target folder, regardless of internal code organization ``` From eeb20009b8ad1ca91009a4b0c6dcc5ce0aa762a6 Mon Sep 17 00:00:00 2001 From: thunkar Date: Tue, 23 Jul 2024 07:10:51 +0000 Subject: [PATCH 14/14] refactor --- yarn-project/txe/src/index.ts | 97 ++++++++++++++++++----------------- 1 file changed, 51 insertions(+), 46 deletions(-) diff --git a/yarn-project/txe/src/index.ts b/yarn-project/txe/src/index.ts index 2c3c157faf50..3af1dee03de1 100644 --- a/yarn-project/txe/src/index.ts +++ b/yarn-project/txe/src/index.ts @@ -26,15 +26,42 @@ type TXEForeignCallInput = { class TXEDispatcher { constructor(private logger: Logger) {} - /* eslint-disable camelcase */ - async resolve_foreign_call({ - session_id: sessionId, - function: functionName, - inputs, - root_path, - package_name, - }: /* eslint-enable camelcase */ - TXEForeignCallInput): Promise { + async #processDeployInputs({ inputs, root_path: rootPath, package_name: packageName }: TXEForeignCallInput) { + const pathStr = fromArray(inputs[0] as ForeignCallArray) + .map(char => String.fromCharCode(char.toNumber())) + .join(''); + const contractName = fromArray(inputs[1] as ForeignCallArray) + .map(char => String.fromCharCode(char.toNumber())) + .join(''); + let artifactPath = ''; + // We're deploying the contract under test + // env.deploy_self("contractName") + if (!pathStr) { + artifactPath = join(rootPath, './target', `${packageName}-${contractName}.json`); + } else { + // We're deploying a contract that belongs in a workspace + // env.deploy("../path/to/workspace/root@packageName", "contractName") + if (pathStr.includes('@')) { + const [workspace, pkg] = pathStr.split('@'); + const targetPath = join(rootPath, workspace, './target'); + this.logger.debug(`Looking for compiled artifact in workspace ${targetPath}`); + artifactPath = join(targetPath, `${pkg}-${contractName}.json`); + } else { + // We're deploying a standalone contract + // env.deploy("../path/to/contract/root", "contractName") + const targetPath = join(rootPath, pathStr, './target'); + this.logger.debug(`Looking for compiled artifact in ${targetPath}`); + [artifactPath] = (await readdir(targetPath)).filter(file => file.endsWith(`-${contractName}.json`)); + } + } + this.logger.debug(`Loading compiled artifact ${artifactPath}`); + const artifact = loadContractArtifact(JSON.parse(await readFile(artifactPath, 'utf-8'))); + inputs.splice(0, 2, artifact); + } + + // eslint-disable-next-line camelcase + async resolve_foreign_call(callData: TXEForeignCallInput): Promise { + const { session_id: sessionId, function: functionName, inputs } = callData; this.logger.debug(`Calling ${functionName} on session ${sessionId}`); if (!TXESessions.has(sessionId) && functionName != 'reset') { @@ -42,44 +69,22 @@ class TXEDispatcher { TXESessions.set(sessionId, await TXEService.init(this.logger)); } - if (functionName === 'reset') { - TXESessions.delete(sessionId) && - this.logger.info(`Called reset on session ${sessionId}, yeeting it out of existence`); - return toForeignCallResult([]); - } else { - if (functionName === 'deploy') { - const pathStr = fromArray(inputs[0] as ForeignCallArray) - .map(char => String.fromCharCode(char.toNumber())) - .join(''); - const contractName = fromArray(inputs[1] as ForeignCallArray) - .map(char => String.fromCharCode(char.toNumber())) - .join(''); - let artifactPath = ''; - // Is not the same contract we're testing - if (!pathStr) { - // eslint-disable-next-line camelcase - artifactPath = join(root_path, './target', `${package_name}-${contractName}.json`); - } else { - // Workspace - if (pathStr.includes('@')) { - const [workspace, pkg] = pathStr.split('@'); - const targetPath = join(root_path, workspace, './target'); - this.logger.debug(`Looking for compiled artifact in workspace ${targetPath}`); - artifactPath = join(targetPath, `${pkg}-${contractName}.json`); - } else { - // Individual contract - const targetPath = join(root_path, pathStr, './target'); - this.logger.debug(`Looking for compiled artifact in ${targetPath}`); - [artifactPath] = (await readdir(targetPath)).filter(file => file.endsWith(`-${contractName}.json`)); - } - } - this.logger.debug(`Loading compiled artifact ${artifactPath}`); - const artifact = loadContractArtifact(JSON.parse(await readFile(artifactPath, 'utf-8'))); - inputs.splice(0, 2, artifact); + switch (functionName) { + case 'reset': { + TXESessions.delete(sessionId) && + this.logger.info(`Called reset on session ${sessionId}, yeeting it out of existence`); + return toForeignCallResult([]); + } + case 'deploy': { + // Modify inputs and fall through + await this.#processDeployInputs(callData); + } + // eslint-disable-next-line no-fallthrough + default: { + const txeService = TXESessions.get(sessionId); + const response = await (txeService as any)[functionName](...inputs); + return response; } - const txeService = TXESessions.get(sessionId); - const response = await (txeService as any)[functionName](...inputs); - return response; } } }