Skip to content

Commit

Permalink
feat!: API inconsistency fix (#3190)
Browse files Browse the repository at this point in the history
Fixes
[inconsistency](#3173 (comment))
spotted by Sean yesterday.

@Maddiaa0 I decided to change the slot to `Fr` instead of `bigint`
because in this case it actually refers to a slot as it is defined in
Noir and not an actually index in a tree.
  • Loading branch information
benesjan authored Nov 2, 2023
1 parent 7b0d076 commit 272eda1
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 17 deletions.
8 changes: 4 additions & 4 deletions yarn-project/acir-simulator/src/client/view_data_oracle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
11 changes: 7 additions & 4 deletions yarn-project/aztec-node/src/aztec-node/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Fr | undefined> {
public async getPublicStorageAt(contract: AztecAddress, slot: Fr): Promise<Fr | undefined> {
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;
}
Expand Down
4 changes: 2 additions & 2 deletions yarn-project/pxe/src/pxe_service/pxe_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<ExtendedNote[]> {
Expand Down
8 changes: 6 additions & 2 deletions yarn-project/types/src/interfaces/aztec-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,12 +127,16 @@ export interface AztecNode extends StateInfoProvider {
getPendingTxByHash(txHash: TxHash): Promise<Tx | undefined>;

/**
* 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<Fr | undefined>;
getPublicStorageAt(contract: AztecAddress, slot: Fr): Promise<Fr | undefined>;

/**
* Returns the current committed roots for the data trees.
Expand Down
13 changes: 8 additions & 5 deletions yarn-project/types/src/interfaces/pxe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,14 +151,17 @@ export interface PXE {
getTx(txHash: TxHash): Promise<L2Tx | undefined>;

/**
* 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<Fr | undefined>;
getPublicStorageAt(contract: AztecAddress, slot: Fr): Promise<Fr | undefined>;

/**
* Gets notes of accounts registered in this PXE based on the provided filter.
Expand Down

0 comments on commit 272eda1

Please sign in to comment.