From 51199f2de4c20f291b27d72c4333a238163811e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Bene=C5=A1?= Date: Thu, 20 Apr 2023 19:44:22 +0200 Subject: [PATCH] feat: getStorageAt function (#317) --- .../aztec-node/src/aztec-node/aztec-node.ts | 23 +++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/yarn-project/aztec-node/src/aztec-node/aztec-node.ts b/yarn-project/aztec-node/src/aztec-node/aztec-node.ts index 4cf78477af5..c0a348e0008 100644 --- a/yarn-project/aztec-node/src/aztec-node/aztec-node.ts +++ b/yarn-project/aztec-node/src/aztec-node/aztec-node.ts @@ -1,16 +1,23 @@ import { Archiver } from '@aztec/archiver'; -import { AztecAddress } from '@aztec/foundation'; +import { AztecAddress, Fr } from '@aztec/foundation'; import { ContractData, L2Block, L2BlockSource } from '@aztec/types'; import { SiblingPath } from '@aztec/merkle-tree'; import { P2P, P2PClient } from '@aztec/p2p'; import { SequencerClient } from '@aztec/sequencer-client'; import { Tx, TxHash } from '@aztec/types'; import { UnverifiedData, UnverifiedDataSource } from '@aztec/types'; -import { MerkleTreeId, MerkleTrees, ServerWorldStateSynchroniser, WorldStateSynchroniser } from '@aztec/world-state'; +import { + MerkleTreeId, + MerkleTrees, + ServerWorldStateSynchroniser, + WorldStateSynchroniser, + computePublicDataTreeLeafIndex, +} from '@aztec/world-state'; import { default as levelup } from 'levelup'; import { default as memdown, MemDown } from 'memdown'; import { AztecNodeConfig } from './config.js'; import { CircuitsWasm } from '@aztec/circuits.js'; +import { PrimitivesWasm } from '@aztec/barretenberg.js/wasm'; export const createMemDown = () => (memdown as any)() as MemDown; @@ -144,4 +151,16 @@ export class AztecNode { public getDataTreePath(leafIndex: bigint): Promise { return this.merkleTreeDB.getSiblingPath(MerkleTreeId.PRIVATE_DATA_TREE, leafIndex, false); } + + /** + * Gets the storage value at the given contract slot. + * @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 getStorageAt(contract: AztecAddress, slot: bigint): Promise { + const leafIndex = computePublicDataTreeLeafIndex(contract, new Fr(slot), await PrimitivesWasm.get()); + return this.merkleTreeDB.getLeafValue(MerkleTreeId.PUBLIC_DATA_TREE, leafIndex, false); + } }