From a2ec88726491000ca4c15f007daff549933547df Mon Sep 17 00:00:00 2001 From: benesjan Date: Wed, 1 Nov 2023 14:35:49 +0000 Subject: [PATCH 1/4] WIP --- .../acir-simulator/src/client/view_data_oracle.ts | 5 ++--- yarn-project/aztec-node/src/aztec-node/server.ts | 11 ++++++----- yarn-project/aztec.js/src/utils/cheat_codes.ts | 2 +- yarn-project/pxe/src/contract_tree/index.ts | 5 +---- yarn-project/pxe/src/pxe_service/pxe_service.ts | 4 ++-- yarn-project/pxe/src/simulator_oracle/index.ts | 4 ++-- yarn-project/types/src/interfaces/aztec-node.ts | 2 +- yarn-project/types/src/interfaces/pxe.ts | 2 +- yarn-project/types/src/interfaces/state_provider.ts | 2 +- 9 files changed, 17 insertions(+), 20 deletions(-) diff --git a/yarn-project/acir-simulator/src/client/view_data_oracle.ts b/yarn-project/acir-simulator/src/client/view_data_oracle.ts index 53a47c04ff0..0194b91ad2b 100644 --- a/yarn-project/acir-simulator/src/client/view_data_oracle.ts +++ b/yarn-project/acir-simulator/src/client/view_data_oracle.ts @@ -143,9 +143,8 @@ export class ViewDataOracle extends TypedOracle { throw new Error(`Oracle storage read undefined: slot=${storageSlot.toString(16)}`); } - const frValue = Fr.fromBuffer(value); - this.log(`Oracle storage read: slot=${storageSlot.toString(16)} value=${frValue}`); - values.push(frValue); + this.log(`Oracle storage read: slot=${storageSlot.toString(16)} value=${value}`); + values.push(value); } return values; } diff --git a/yarn-project/aztec-node/src/aztec-node/server.ts b/yarn-project/aztec-node/src/aztec-node/server.ts index 8ac7bf6fcd0..756e8878b7e 100644 --- a/yarn-project/aztec-node/src/aztec-node/server.ts +++ b/yarn-project/aztec-node/src/aztec-node/server.ts @@ -291,9 +291,9 @@ export class AztecNodeService implements AztecNode { * @param leafValue - The value to search for * @returns The index of the given leaf in the given tree or undefined if not found. */ - public async findLeafIndex(treeId: MerkleTreeId, leafValue: Buffer): Promise { + public async findLeafIndex(treeId: MerkleTreeId, leafValue: Fr): Promise { const committedDb = await this.#getWorldState(); - return committedDb.findLeafIndex(treeId, leafValue); + return committedDb.findLeafIndex(treeId, leafValue.toBuffer()); } /** @@ -324,7 +324,7 @@ export class AztecNodeService implements AztecNode { */ public async getL1ToL2MessageAndIndex(messageKey: Fr): Promise { // todo: #697 - make this one lookup. - const index = (await this.findLeafIndex(MerkleTreeId.L1_TO_L2_MESSAGES_TREE, messageKey.toBuffer()))!; + const index = (await this.findLeafIndex(MerkleTreeId.L1_TO_L2_MESSAGES_TREE, messageKey))!; const message = await this.l1ToL2MessageSource.getConfirmedL1ToL2Message(messageKey); return Promise.resolve(new L1ToL2MessageAndIndex(index, message)); } @@ -346,10 +346,11 @@ export class AztecNodeService implements AztecNode { * @returns Storage value at the given contract slot (or undefined if not found). * Note: Aztec's version of `eth_getStorageAt`. */ - public async getPublicStorageAt(contract: AztecAddress, slot: bigint): Promise { + public async getPublicStorageAt(contract: AztecAddress, slot: bigint): Promise { const committedDb = await this.#getWorldState(); const leafIndex = computePublicDataTreeIndex(await CircuitsWasm.get(), contract, new Fr(slot)); - return committedDb.getLeafValue(MerkleTreeId.PUBLIC_DATA_TREE, leafIndex.value); + const value = await committedDb.getLeafValue(MerkleTreeId.PUBLIC_DATA_TREE, leafIndex.value); + return value ? Fr.fromBuffer(value) : undefined; } /** diff --git a/yarn-project/aztec.js/src/utils/cheat_codes.ts b/yarn-project/aztec.js/src/utils/cheat_codes.ts index 23f814d8f73..f12c38ea4b7 100644 --- a/yarn-project/aztec.js/src/utils/cheat_codes.ts +++ b/yarn-project/aztec.js/src/utils/cheat_codes.ts @@ -277,7 +277,7 @@ export class AztecCheatCodes { if (storageValue === undefined) { throw new Error(`Storage slot ${slot} not found`); } - return Fr.fromBuffer(storageValue); + return storageValue; } /** diff --git a/yarn-project/pxe/src/contract_tree/index.ts b/yarn-project/pxe/src/contract_tree/index.ts index d0ef99ce806..c0fe69e7226 100644 --- a/yarn-project/pxe/src/contract_tree/index.ts +++ b/yarn-project/pxe/src/contract_tree/index.ts @@ -229,10 +229,7 @@ export class ContractTree { const root = await this.getFunctionTreeRoot(); const newContractData = new NewContractData(completeAddress.address, portalContract, root); const commitment = computeContractLeaf(this.wasm, newContractData); - this.contractIndex = await this.stateInfoProvider.findLeafIndex( - MerkleTreeId.CONTRACT_TREE, - commitment.toBuffer(), - ); + this.contractIndex = await this.stateInfoProvider.findLeafIndex(MerkleTreeId.CONTRACT_TREE, commitment); if (this.contractIndex === undefined) { throw new Error( `Failed to find contract at ${completeAddress.address} with portal ${portalContract} resulting in commitment ${commitment}.`, diff --git a/yarn-project/pxe/src/pxe_service/pxe_service.ts b/yarn-project/pxe/src/pxe_service/pxe_service.ts index 4938f6c02b4..a0af74e275c 100644 --- a/yarn-project/pxe/src/pxe_service/pxe_service.ts +++ b/yarn-project/pxe/src/pxe_service/pxe_service.ts @@ -232,14 +232,14 @@ export class PXEService implements PXE { // TODO(https://github.com/AztecProtocol/aztec-packages/issues/1386) // This can always be `uniqueSiloedNoteHash` once notes added from public also include nonces. const noteHashToLookUp = nonce.isZero() ? siloedNoteHash : uniqueSiloedNoteHash; - const index = await this.node.findLeafIndex(MerkleTreeId.NOTE_HASH_TREE, noteHashToLookUp.toBuffer()); + const index = await this.node.findLeafIndex(MerkleTreeId.NOTE_HASH_TREE, noteHashToLookUp); if (index === undefined) { throw new Error('Note does not exist.'); } const wasm = await CircuitsWasm.get(); const siloedNullifier = siloNullifier(wasm, note.contractAddress, innerNullifier!); - const nullifierIndex = await this.node.findLeafIndex(MerkleTreeId.NULLIFIER_TREE, siloedNullifier.toBuffer()); + const nullifierIndex = await this.node.findLeafIndex(MerkleTreeId.NULLIFIER_TREE, siloedNullifier); if (nullifierIndex !== undefined) { throw new Error('The note has been destroyed.'); } diff --git a/yarn-project/pxe/src/simulator_oracle/index.ts b/yarn-project/pxe/src/simulator_oracle/index.ts index fb2580b9cbe..53642ae9cd2 100644 --- a/yarn-project/pxe/src/simulator_oracle/index.ts +++ b/yarn-project/pxe/src/simulator_oracle/index.ts @@ -116,11 +116,11 @@ export class SimulatorOracle implements DBOracle { * @returns - The index of the commitment. Undefined if it does not exist in the tree. */ async getCommitmentIndex(commitment: Fr) { - return await this.stateInfoProvider.findLeafIndex(MerkleTreeId.NOTE_HASH_TREE, commitment.toBuffer()); + return await this.stateInfoProvider.findLeafIndex(MerkleTreeId.NOTE_HASH_TREE, commitment); } async getNullifierIndex(nullifier: Fr) { - return await this.stateInfoProvider.findLeafIndex(MerkleTreeId.NULLIFIER_TREE, nullifier.toBuffer()); + return await this.stateInfoProvider.findLeafIndex(MerkleTreeId.NULLIFIER_TREE, nullifier); } /** diff --git a/yarn-project/types/src/interfaces/aztec-node.ts b/yarn-project/types/src/interfaces/aztec-node.ts index 287e4df2f53..5a6604213a5 100644 --- a/yarn-project/types/src/interfaces/aztec-node.ts +++ b/yarn-project/types/src/interfaces/aztec-node.ts @@ -132,7 +132,7 @@ export interface AztecNode extends StateInfoProvider { * @param slot - Slot to query. * @returns Storage value at the given contract slot (or undefined if not found). */ - getPublicStorageAt(contract: AztecAddress, slot: bigint): Promise; + getPublicStorageAt(contract: AztecAddress, slot: bigint): Promise; /** * Returns the current committed roots for the data trees. diff --git a/yarn-project/types/src/interfaces/pxe.ts b/yarn-project/types/src/interfaces/pxe.ts index cd9c8497c3d..75f19fd101a 100644 --- a/yarn-project/types/src/interfaces/pxe.ts +++ b/yarn-project/types/src/interfaces/pxe.ts @@ -158,7 +158,7 @@ export interface PXE { * @returns A buffer containing the public storage data at the storage slot. * @throws If the contract is not deployed. */ - getPublicStorageAt(contract: AztecAddress, storageSlot: Fr): Promise; + getPublicStorageAt(contract: AztecAddress, storageSlot: Fr): Promise; /** * Gets notes of accounts registered in this PXE based on the provided filter. diff --git a/yarn-project/types/src/interfaces/state_provider.ts b/yarn-project/types/src/interfaces/state_provider.ts index 4308abbd6c3..a08347cee39 100644 --- a/yarn-project/types/src/interfaces/state_provider.ts +++ b/yarn-project/types/src/interfaces/state_provider.ts @@ -14,7 +14,7 @@ export interface StateInfoProvider { * @param leafValue - The value to search for * @returns The index of the given leaf in the given tree or undefined if not found. */ - findLeafIndex(treeId: MerkleTreeId, leafValue: Buffer): Promise; + findLeafIndex(treeId: MerkleTreeId, leafValue: Fr): Promise; /** * Returns the sibling path for the given index in the contract tree. From 93fc692cae719ad2347f979d58662c63db1d08a0 Mon Sep 17 00:00:00 2001 From: benesjan Date: Wed, 1 Nov 2023 15:12:14 +0000 Subject: [PATCH 2/4] fixes --- yarn-project/acir-simulator/src/public/index.test.ts | 5 ++--- yarn-project/end-to-end/src/e2e_2_pxes.test.ts | 7 +++---- .../end-to-end/src/e2e_account_contracts.test.ts | 4 ++-- yarn-project/end-to-end/src/e2e_nested_contract.test.ts | 4 +--- yarn-project/end-to-end/src/e2e_ordering.test.ts | 9 ++++----- yarn-project/end-to-end/src/guides/dapp_testing.test.ts | 2 +- 6 files changed, 13 insertions(+), 18 deletions(-) diff --git a/yarn-project/acir-simulator/src/public/index.test.ts b/yarn-project/acir-simulator/src/public/index.test.ts index 093c823f6cc..0ac9864e1c2 100644 --- a/yarn-project/acir-simulator/src/public/index.test.ts +++ b/yarn-project/acir-simulator/src/public/index.test.ts @@ -11,7 +11,6 @@ import { FunctionArtifact, FunctionSelector, encodeArguments } from '@aztec/foun import { AztecAddress } from '@aztec/foundation/aztec-address'; import { EthAddress } from '@aztec/foundation/eth-address'; import { Fr } from '@aztec/foundation/fields'; -import { toBigInt } from '@aztec/foundation/serialize'; import { ChildContractArtifact, ParentContractArtifact, @@ -240,8 +239,8 @@ describe('ACIR public execution simulator', () => { const functionData = new FunctionData(parentEntryPointFnSelector, isInternal ?? false, false, false); const args = encodeArguments(parentEntryPointFn, [ - childContractAddress.toField().value, - toBigInt(childValueFnSelector.toBuffer()), + childContractAddress.toField(), + childValueFnSelector.toField(), initialValue, ]); diff --git a/yarn-project/end-to-end/src/e2e_2_pxes.test.ts b/yarn-project/end-to-end/src/e2e_2_pxes.test.ts index 8039eeb8fc3..a429175bb9f 100644 --- a/yarn-project/end-to-end/src/e2e_2_pxes.test.ts +++ b/yarn-project/end-to-end/src/e2e_2_pxes.test.ts @@ -1,7 +1,6 @@ import { AztecAddress, Note, Wallet, computeMessageSecretHash } from '@aztec/aztec.js'; import { DebugLogger } from '@aztec/foundation/log'; import { retryUntil } from '@aztec/foundation/retry'; -import { toBigInt } from '@aztec/foundation/serialize'; import { ChildContract, TokenContract } from '@aztec/noir-contracts/types'; import { EthAddress, Fr, PXEService } from '@aztec/pxe'; import { AztecNode, CompleteAddress, ExtendedNote, PXE, TxStatus } from '@aztec/types'; @@ -175,7 +174,7 @@ describe('e2e_2_pxes', () => { }; const getChildStoredValue = (child: { address: AztecAddress }, pxe: PXE) => - pxe.getPublicStorageAt(child.address, new Fr(1)).then(x => toBigInt(x!)); + pxe.getPublicStorageAt(child.address, new Fr(1)); it('user calls a public function on a contract deployed by a different user using a different PXE', async () => { const childCompleteAddress = await deployChildContractViaServerA(); @@ -191,7 +190,7 @@ describe('e2e_2_pxes', () => { }, ]); - const newValueToSet = 256n; + const newValueToSet = new Fr(256n); const childContractWithWalletB = await ChildContract.at(childCompleteAddress.address, walletB); await childContractWithWalletB.methods.pubIncValue(newValueToSet).send().wait({ interval: 0.1 }); @@ -199,7 +198,7 @@ describe('e2e_2_pxes', () => { await awaitServerSynchronized(pxeA); const storedValue = await getChildStoredValue(childCompleteAddress, pxeB); - expect(storedValue).toBe(newValueToSet); + expect(storedValue).toEqual(newValueToSet); }); it('private state is "zero" when Private eXecution Environment (PXE) does not have the account private key', async () => { diff --git a/yarn-project/end-to-end/src/e2e_account_contracts.test.ts b/yarn-project/end-to-end/src/e2e_account_contracts.test.ts index aaca09892b7..ddb932a4107 100644 --- a/yarn-project/end-to-end/src/e2e_account_contracts.test.ts +++ b/yarn-project/end-to-end/src/e2e_account_contracts.test.ts @@ -9,7 +9,6 @@ import { Wallet, } from '@aztec/aztec.js'; import { CompleteAddress, GrumpkinPrivateKey, GrumpkinScalar } from '@aztec/circuits.js'; -import { toBigInt } from '@aztec/foundation/serialize'; import { ChildContract } from '@aztec/noir-contracts/types'; import { randomBytes } from 'crypto'; @@ -56,7 +55,8 @@ function itShouldBehaveLikeAnAccountContract( const { logger, pxe } = context; logger('Calling public function...'); await child.methods.pubIncValue(42).send().wait({ interval: 0.1 }); - expect(toBigInt((await pxe.getPublicStorageAt(child.address, new Fr(1)))!)).toEqual(42n); + const storedValue = await pxe.getPublicStorageAt(child.address, new Fr(1)); + expect(storedValue!).toEqual(new Fr(42n)); }, 60_000); it('fails to call a function using an invalid signature', async () => { diff --git a/yarn-project/end-to-end/src/e2e_nested_contract.test.ts b/yarn-project/end-to-end/src/e2e_nested_contract.test.ts index 0abd5912844..be484b3b6e8 100644 --- a/yarn-project/end-to-end/src/e2e_nested_contract.test.ts +++ b/yarn-project/end-to-end/src/e2e_nested_contract.test.ts @@ -1,7 +1,6 @@ import { AztecAddress, BatchCall, Fr, Wallet } from '@aztec/aztec.js'; import { toBigIntBE } from '@aztec/foundation/bigint-buffer'; import { DebugLogger } from '@aztec/foundation/log'; -import { toBigInt } from '@aztec/foundation/serialize'; import { ChildContract, ImportTestContract, ParentContract, TestContract } from '@aztec/noir-contracts/types'; import { PXE } from '@aztec/types'; @@ -28,8 +27,7 @@ describe('e2e_nested_contract', () => { childContract = await ChildContract.deploy(wallet).send().deployed(); }, 100_000); - const getChildStoredValue = (child: { address: AztecAddress }) => - pxe.getPublicStorageAt(child.address, new Fr(1)).then(x => toBigInt(x!)); + const getChildStoredValue = (child: { address: AztecAddress }) => pxe.getPublicStorageAt(child.address, new Fr(1)); it('performs nested calls', async () => { await parentContract.methods diff --git a/yarn-project/end-to-end/src/e2e_ordering.test.ts b/yarn-project/end-to-end/src/e2e_ordering.test.ts index d0b9f902840..a28349cbc50 100644 --- a/yarn-project/end-to-end/src/e2e_ordering.test.ts +++ b/yarn-project/end-to-end/src/e2e_ordering.test.ts @@ -3,7 +3,6 @@ import { Wallet } from '@aztec/aztec.js'; import { FunctionSelector } from '@aztec/circuits.js'; import { toBigIntBE } from '@aztec/foundation/bigint-buffer'; import { Fr } from '@aztec/foundation/fields'; -import { toBigInt } from '@aztec/foundation/serialize'; import { ChildContract, ParentContract } from '@aztec/noir-contracts/types'; import { PXE, TxStatus } from '@aztec/types'; @@ -80,8 +79,8 @@ describe('e2e_ordering', () => { await expectLogsFromLastBlockToBe(expectedOrder); // The final value of the child is the last one set - const value = await pxe.getPublicStorageAt(child.address, new Fr(1)).then(x => toBigInt(x!)); - expect(value).toEqual(expectedOrder[1]); // final state should match last value set + const value = await pxe.getPublicStorageAt(child.address, new Fr(1)); + expect(value?.value).toBe(expectedOrder[1]); // final state should match last value set }, ); }); @@ -104,8 +103,8 @@ describe('e2e_ordering', () => { const receipt = await tx.wait(); expect(receipt.status).toBe(TxStatus.MINED); - const value = await pxe.getPublicStorageAt(child.address, new Fr(1)).then(x => toBigInt(x!)); - expect(value).toEqual(expectedOrder[1]); // final state should match last value set + const value = await pxe.getPublicStorageAt(child.address, new Fr(1)); + expect(value?.value).toBe(expectedOrder[1]); // final state should match last value set }, ); diff --git a/yarn-project/end-to-end/src/guides/dapp_testing.test.ts b/yarn-project/end-to-end/src/guides/dapp_testing.test.ts index d4b87f6d81f..57895f26023 100644 --- a/yarn-project/end-to-end/src/guides/dapp_testing.test.ts +++ b/yarn-project/end-to-end/src/guides/dapp_testing.test.ts @@ -166,7 +166,7 @@ describe('guides/dapp/testing', () => { await token.methods.mint_public(owner.getAddress(), 100n).send().wait(); const ownerPublicBalanceSlot = cheats.aztec.computeSlotInMap(6n, owner.getAddress()); const balance = await pxe.getPublicStorageAt(token.address, ownerPublicBalanceSlot); - expect(toBigIntBE(balance!)).toEqual(100n); + expect(balance!.value).toEqual(100n); // docs:end:public-storage }); From 7da8449b42693ab97da50fa732625845063e0c1f Mon Sep 17 00:00:00 2001 From: benesjan Date: Wed, 1 Nov 2023 15:23:41 +0000 Subject: [PATCH 3/4] linter fix --- yarn-project/end-to-end/src/guides/dapp_testing.test.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/yarn-project/end-to-end/src/guides/dapp_testing.test.ts b/yarn-project/end-to-end/src/guides/dapp_testing.test.ts index 57895f26023..315daa60f9b 100644 --- a/yarn-project/end-to-end/src/guides/dapp_testing.test.ts +++ b/yarn-project/end-to-end/src/guides/dapp_testing.test.ts @@ -11,7 +11,6 @@ import { getSandboxAccountsWallets, waitForSandbox, } from '@aztec/aztec.js'; -import { toBigIntBE } from '@aztec/foundation/bigint-buffer'; import { TestContract, TokenContract } from '@aztec/noir-contracts/types'; import { ExtendedNote } from '@aztec/types'; From 95303201ca674b68c94090431cb2f05d5c97b8f9 Mon Sep 17 00:00:00 2001 From: benesjan Date: Wed, 1 Nov 2023 16:02:06 +0000 Subject: [PATCH 4/4] fix --- .../end-to-end/src/e2e_nested_contract.test.ts | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/yarn-project/end-to-end/src/e2e_nested_contract.test.ts b/yarn-project/end-to-end/src/e2e_nested_contract.test.ts index be484b3b6e8..ee8f9e0a738 100644 --- a/yarn-project/end-to-end/src/e2e_nested_contract.test.ts +++ b/yarn-project/end-to-end/src/e2e_nested_contract.test.ts @@ -56,7 +56,7 @@ describe('e2e_nested_contract', () => { .enqueueCallToChild(childContract.address, childContract.methods.pubIncValue.selector.toField(), 42n) .send() .wait(); - expect(await getChildStoredValue(childContract)).toEqual(42n); + expect(await getChildStoredValue(childContract)).toEqual(new Fr(42n)); }, 100_000); it('fails simulation if calling a public function not allowed to be called externally', async () => { @@ -72,7 +72,7 @@ describe('e2e_nested_contract', () => { .enqueueCallToChildTwice(childContract.address, childContract.methods.pubIncValue.selector.value, 42n) .send() .wait(); - expect(await getChildStoredValue(childContract)).toEqual(85n); + expect(await getChildStoredValue(childContract)).toEqual(new Fr(85n)); }, 100_000); it('enqueues a public call with nested public calls', async () => { @@ -80,7 +80,7 @@ describe('e2e_nested_contract', () => { .enqueueCallToPubEntryPoint(childContract.address, childContract.methods.pubIncValue.selector.toField(), 42n) .send() .wait(); - expect(await getChildStoredValue(childContract)).toEqual(42n); + expect(await getChildStoredValue(childContract)).toEqual(new Fr(42n)); }, 100_000); it('enqueues multiple public calls with nested public calls', async () => { @@ -88,7 +88,7 @@ describe('e2e_nested_contract', () => { .enqueueCallsToPubEntryPoint(childContract.address, childContract.methods.pubIncValue.selector.toField(), 42n) .send() .wait(); - expect(await getChildStoredValue(childContract)).toEqual(85n); + expect(await getChildStoredValue(childContract)).toEqual(new Fr(85n)); }, 100_000); // Regression for https://github.com/AztecProtocol/aztec-packages/issues/640 @@ -97,7 +97,7 @@ describe('e2e_nested_contract', () => { .pubEntryPointTwice(childContract.address, childContract.methods.pubIncValue.selector.value, 42n) .send() .wait(); - expect(await getChildStoredValue(childContract)).toEqual(84n); + expect(await getChildStoredValue(childContract)).toEqual(new Fr(84n)); }, 100_000); // Regression for https://github.com/AztecProtocol/aztec-packages/issues/1645 @@ -119,7 +119,7 @@ describe('e2e_nested_contract', () => { ).logs; const processedLogs = extendedLogs.map(extendedLog => toBigIntBE(extendedLog.log.data)); expect(processedLogs).toEqual([20n, 40n]); - expect(await getChildStoredValue(childContract)).toEqual(40n); + expect(await getChildStoredValue(childContract)).toEqual(new Fr(40n)); }); });