From bf5c6f47cd53bc08386c0631b7509ff5ee255348 Mon Sep 17 00:00:00 2001 From: benesjan Date: Thu, 2 Nov 2023 07:53:54 +0000 Subject: [PATCH] getPublicStorageAt fix --- .../acir-simulator/src/client/view_data_oracle.ts | 8 ++++---- yarn-project/aztec-node/src/aztec-node/server.ts | 11 +++++++---- yarn-project/pxe/src/pxe_service/pxe_service.ts | 4 ++-- yarn-project/types/src/interfaces/aztec-node.ts | 8 ++++++-- yarn-project/types/src/interfaces/pxe.ts | 13 ++++++++----- 5 files changed, 27 insertions(+), 17 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 0194b91ad2b..525f4789628 100644 --- a/yarn-project/acir-simulator/src/client/view_data_oracle.ts +++ b/yarn-project/acir-simulator/src/client/view_data_oracle.ts @@ -136,14 +136,14 @@ export class ViewDataOracle extends TypedOracle { } const values = []; - for (let i = 0; i < Number(numberOfElements); i++) { - const storageSlot = startStorageSlot.value + BigInt(i); + for (let i = 0n; i < numberOfElements; i++) { + const storageSlot = new Fr(startStorageSlot.value + i); const value = await this.aztecNode.getPublicStorageAt(this.contractAddress, storageSlot); if (value === undefined) { - throw new Error(`Oracle storage read undefined: slot=${storageSlot.toString(16)}`); + throw new Error(`Oracle storage read undefined: slot=${storageSlot.toString()}`); } - this.log(`Oracle storage read: slot=${storageSlot.toString(16)} value=${value}`); + this.log(`Oracle storage read: slot=${storageSlot.toString()} 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 756e8878b7e..5d168d79269 100644 --- a/yarn-project/aztec-node/src/aztec-node/server.ts +++ b/yarn-project/aztec-node/src/aztec-node/server.ts @@ -340,15 +340,18 @@ export class AztecNodeService implements AztecNode { } /** - * Gets the storage value at the given contract slot. + * Gets the storage value at the given contract storage slot. + * + * @remarks The storage slot here refers to the slot as it is defined in Noir not the index in the merkle tree. + * Aztec's version of `eth_getStorageAt`. + * * @param contract - Address of the contract to query. * @param slot - Slot to query. * @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: Fr): Promise { const committedDb = await this.#getWorldState(); - const leafIndex = computePublicDataTreeIndex(await CircuitsWasm.get(), contract, new Fr(slot)); + const leafIndex = computePublicDataTreeIndex(await CircuitsWasm.get(), contract, slot); const value = await committedDb.getLeafValue(MerkleTreeId.PUBLIC_DATA_TREE, leafIndex.value); return value ? Fr.fromBuffer(value) : undefined; } diff --git a/yarn-project/pxe/src/pxe_service/pxe_service.ts b/yarn-project/pxe/src/pxe_service/pxe_service.ts index a0af74e275c..8d8a9b69156 100644 --- a/yarn-project/pxe/src/pxe_service/pxe_service.ts +++ b/yarn-project/pxe/src/pxe_service/pxe_service.ts @@ -187,11 +187,11 @@ export class PXEService implements PXE { return (await this.db.getContracts()).map(c => c.completeAddress.address); } - public async getPublicStorageAt(contract: AztecAddress, storageSlot: Fr) { + public async getPublicStorageAt(contract: AztecAddress, slot: Fr) { if ((await this.getContractData(contract)) === undefined) { throw new Error(`Contract ${contract.toString()} is not deployed`); } - return await this.node.getPublicStorageAt(contract, storageSlot.value); + return await this.node.getPublicStorageAt(contract, slot); } public async getNotes(filter: NoteFilter): Promise { diff --git a/yarn-project/types/src/interfaces/aztec-node.ts b/yarn-project/types/src/interfaces/aztec-node.ts index 5a6604213a5..0c00dd480bf 100644 --- a/yarn-project/types/src/interfaces/aztec-node.ts +++ b/yarn-project/types/src/interfaces/aztec-node.ts @@ -127,12 +127,16 @@ export interface AztecNode extends StateInfoProvider { getPendingTxByHash(txHash: TxHash): Promise; /** - * Gets the storage value at the given contract slot. Our version of eth_getStorageAt. + * Gets the storage value at the given contract storage slot. + * + * @remarks The storage slot here refers to the slot as it is defined in Noir not the index in the merkle tree. + * Aztec's version of `eth_getStorageAt`. + * * @param contract - Address of the contract to query. * @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: Fr): 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 4e238ef7b66..5efef5b63ae 100644 --- a/yarn-project/types/src/interfaces/pxe.ts +++ b/yarn-project/types/src/interfaces/pxe.ts @@ -151,14 +151,17 @@ export interface PXE { getTx(txHash: TxHash): Promise; /** - * Retrieves the public storage data at a specified contract address and storage slot. + * Gets the storage value at the given contract storage slot. * - * @param contract - The AztecAddress of the target contract. - * @param storageSlot - The Fr representing the storage slot to be fetched. - * @returns A buffer containing the public storage data at the storage slot. + * @remarks The storage slot here refers to the slot as it is defined in Noir not the index in the merkle tree. + * Aztec's version of `eth_getStorageAt`. + * + * @param contract - Address of the contract to query. + * @param slot - Slot to query. + * @returns Storage value at the given contract slot (or undefined if not found). * @throws If the contract is not deployed. */ - getPublicStorageAt(contract: AztecAddress, storageSlot: Fr): Promise; + getPublicStorageAt(contract: AztecAddress, slot: Fr): Promise; /** * Gets notes of accounts registered in this PXE based on the provided filter.