From 451feed4fb355b81acedeba05ebc62468c3c7316 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Bene=C5=A1?= Date: Wed, 13 Nov 2024 11:04:55 -0300 Subject: [PATCH] fix: TXE fixes (#9930) --- yarn-project/txe/package.json | 2 +- yarn-project/txe/src/oracle/txe_oracle.ts | 106 +++++++++++----------- 2 files changed, 55 insertions(+), 53 deletions(-) diff --git a/yarn-project/txe/package.json b/yarn-project/txe/package.json index e109bd49925..47e82bd7428 100644 --- a/yarn-project/txe/package.json +++ b/yarn-project/txe/package.json @@ -18,7 +18,7 @@ "formatting": "run -T prettier --check ./src && run -T eslint ./src", "formatting:fix": "run -T eslint --fix ./src && run -T prettier -w ./src", "test": "NODE_NO_WARNINGS=1 node --experimental-vm-modules ../node_modules/.bin/jest --passWithNoTests", - "dev": "DEBUG='aztec:*' LOG_LEVEL=debug node ./dest/bin/index.js", + "dev": "DEBUG='aztec:*,-aztec:avm_simulator:*' LOG_LEVEL=debug node ./dest/bin/index.js", "start": "node ./dest/bin/index.js" }, "inherits": [ diff --git a/yarn-project/txe/src/oracle/txe_oracle.ts b/yarn-project/txe/src/oracle/txe_oracle.ts index 3eeb80cf35d..122749e1254 100644 --- a/yarn-project/txe/src/oracle/txe_oracle.ts +++ b/yarn-project/txe/src/oracle/txe_oracle.ts @@ -26,6 +26,7 @@ import { NULLIFIER_SUBTREE_HEIGHT, type NULLIFIER_TREE_HEIGHT, type NullifierLeafPreimage, + PRIVATE_CONTEXT_INPUTS_LENGTH, PUBLIC_DATA_SUBTREE_HEIGHT, type PUBLIC_DATA_TREE_HEIGHT, PUBLIC_DISPATCH_SELECTOR, @@ -348,16 +349,16 @@ export class TXE implements TypedOracle { } async getPublicDataTreeWitness(blockNumber: number, leafSlot: Fr): Promise { - const committedDb = new MerkleTreeSnapshotOperationsFacade(this.trees, blockNumber); - const lowLeafResult = await committedDb.getPreviousValueIndex(MerkleTreeId.PUBLIC_DATA_TREE, leafSlot.toBigInt()); + const db = await this.#getTreesAt(blockNumber); + const lowLeafResult = await db.getPreviousValueIndex(MerkleTreeId.PUBLIC_DATA_TREE, leafSlot.toBigInt()); if (!lowLeafResult) { return undefined; } else { - const preimage = (await committedDb.getLeafPreimage( + const preimage = (await db.getLeafPreimage( MerkleTreeId.PUBLIC_DATA_TREE, lowLeafResult.index, )) as PublicDataTreeLeafPreimage; - const path = await committedDb.getSiblingPath( + const path = await db.getSiblingPath( MerkleTreeId.PUBLIC_DATA_TREE, lowLeafResult.index, ); @@ -563,55 +564,53 @@ export class TXE implements TypedOracle { const initialWitness = await this.getInitialWitness(artifact, argsHash, sideEffectCounter, isStaticCall); const acvmCallback = new Oracle(this); const timer = new Timer(); - try { - const acirExecutionResult = await acvm(acir, initialWitness, acvmCallback).catch((err: Error) => { - err.message = resolveAssertionMessageFromError(err, artifact); - - const execError = new ExecutionError( - err.message, - { - contractAddress: targetContractAddress, - functionSelector, - }, - extractCallStack(err, artifact.debug), - { cause: err }, - ); - this.logger.debug(`Error executing private function ${targetContractAddress}:${functionSelector}`); - throw createSimulationError(execError); - }); - const duration = timer.ms(); - const publicInputs = extractPrivateCircuitPublicInputs(artifact, acirExecutionResult.partialWitness); - - const initialWitnessSize = witnessMapToFields(initialWitness).length * Fr.SIZE_IN_BYTES; - this.logger.debug(`Ran external function ${targetContractAddress.toString()}:${functionSelector}`, { - circuitName: 'app-circuit', - duration, - eventName: 'circuit-witness-generation', - inputSize: initialWitnessSize, - outputSize: publicInputs.toBuffer().length, - appCircuitName: 'noname', - } satisfies CircuitWitnessGenerationStats); - - // Apply side effects - const endSideEffectCounter = publicInputs.endSideEffectCounter; - this.sideEffectCounter = endSideEffectCounter.toNumber() + 1; - - await this.addNullifiers( - targetContractAddress, - publicInputs.nullifiers.filter(nullifier => !nullifier.isEmpty()).map(nullifier => nullifier.value), + const acirExecutionResult = await acvm(acir, initialWitness, acvmCallback).catch((err: Error) => { + err.message = resolveAssertionMessageFromError(err, artifact); + + const execError = new ExecutionError( + err.message, + { + contractAddress: targetContractAddress, + functionSelector, + }, + extractCallStack(err, artifact.debug), + { cause: err }, ); + this.logger.debug(`Error executing private function ${targetContractAddress}:${functionSelector}`); + throw createSimulationError(execError); + }); + const duration = timer.ms(); + const publicInputs = extractPrivateCircuitPublicInputs(artifact, acirExecutionResult.partialWitness); + + const initialWitnessSize = witnessMapToFields(initialWitness).length * Fr.SIZE_IN_BYTES; + this.logger.debug(`Ran external function ${targetContractAddress.toString()}:${functionSelector}`, { + circuitName: 'app-circuit', + duration, + eventName: 'circuit-witness-generation', + inputSize: initialWitnessSize, + outputSize: publicInputs.toBuffer().length, + appCircuitName: 'noname', + } satisfies CircuitWitnessGenerationStats); - await this.addNoteHashes( - targetContractAddress, - publicInputs.noteHashes.filter(noteHash => !noteHash.isEmpty()).map(noteHash => noteHash.value), - ); + // Apply side effects + const endSideEffectCounter = publicInputs.endSideEffectCounter; + this.sideEffectCounter = endSideEffectCounter.toNumber() + 1; - return { endSideEffectCounter, returnsHash: publicInputs.returnsHash }; - } finally { - this.setContractAddress(currentContractAddress); - this.setMsgSender(currentMessageSender); - this.setFunctionSelector(currentFunctionSelector); - } + await this.addNullifiers( + targetContractAddress, + publicInputs.nullifiers.filter(nullifier => !nullifier.isEmpty()).map(nullifier => nullifier.value), + ); + + await this.addNoteHashes( + targetContractAddress, + publicInputs.noteHashes.filter(noteHash => !noteHash.isEmpty()).map(noteHash => noteHash.value), + ); + + this.setContractAddress(currentContractAddress); + this.setMsgSender(currentMessageSender); + this.setFunctionSelector(currentFunctionSelector); + + return { endSideEffectCounter, returnsHash: publicInputs.returnsHash }; } async getInitialWitness(abi: FunctionAbi, argsHash: Fr, sideEffectCounter: number, isStaticCall: boolean) { @@ -628,9 +627,12 @@ export class TXE implements TypedOracle { sideEffectCounter, isStaticCall, ); + const privateContextInputsAsFields = privateContextInputs.toFields(); + if (privateContextInputsAsFields.length !== PRIVATE_CONTEXT_INPUTS_LENGTH) { + throw new Error('Invalid private context inputs size'); + } - const fields = [...privateContextInputs.toFields(), ...args]; - + const fields = [...privateContextInputsAsFields, ...args]; return toACVMWitness(0, fields); }