From 860f3403980f872d6361acc8f6cc31d95d26a635 Mon Sep 17 00:00:00 2001 From: Lasse Herskind <16536249+LHerskind@users.noreply.github.com> Date: Mon, 2 Oct 2023 09:39:05 +0100 Subject: [PATCH] feat: collapse interfaces for single implementation (#2599) There is an absurd amount of interfaces for things that are just the same implementation but thrown into different (and sometimes same) things. It makes it more difficult to contribute and figure out what is going on in the code, and don't really give us a benefit when it is values that are expected to be tightly coupled anyway. If the need for multiple implementations arise lets make the interfaces then. - Creates `StateInfoProvider` which combines: - `contract_commitment_provider.ts` - `data_commitment_provider.ts` - `l1_l2_message_provider.ts` - `nullifier_provider.ts` - This have functions for retrieving index and sibling paths as we had before. - Replaces the multiple `findXLeafIndex` functions we had with a single `findLeafIndex` that takes the tree id as input. - The sibling path functions are not collapsed hence this don't play well with the `remote` from `comlink` that we use. --- .../src/aztec-node/http_rpc_server.ts | 10 +--- .../aztec-node/src/aztec-node/server.ts | 34 +++----------- .../pxe/src/contract_data_oracle/index.ts | 6 +-- yarn-project/pxe/src/contract_tree/index.ts | 11 +++-- .../pxe/src/note_processor/note_processor.ts | 2 +- .../pxe/src/pxe_service/pxe_service.ts | 7 +-- yarn-project/pxe/src/simulator/index.ts | 11 ++--- .../pxe/src/simulator_oracle/index.ts | 11 ++--- .../types/src/interfaces/aztec-node.ts | 11 +---- .../contract_commitment_provider.ts | 22 --------- .../interfaces/data_commitment_provider.ts | 22 --------- yarn-project/types/src/interfaces/index.ts | 4 +- .../src/interfaces/l1_l2_message_provider.ts | 24 ---------- .../src/interfaces/nullifier_provider.ts | 13 ----- .../types/src/interfaces/state_provider.ts | 47 +++++++++++++++++++ 15 files changed, 82 insertions(+), 153 deletions(-) delete mode 100644 yarn-project/types/src/interfaces/contract_commitment_provider.ts delete mode 100644 yarn-project/types/src/interfaces/data_commitment_provider.ts delete mode 100644 yarn-project/types/src/interfaces/l1_l2_message_provider.ts delete mode 100644 yarn-project/types/src/interfaces/nullifier_provider.ts create mode 100644 yarn-project/types/src/interfaces/state_provider.ts diff --git a/yarn-project/aztec-node/src/aztec-node/http_rpc_server.ts b/yarn-project/aztec-node/src/aztec-node/http_rpc_server.ts index e1fb672952b..cd740d9c44f 100644 --- a/yarn-project/aztec-node/src/aztec-node/http_rpc_server.ts +++ b/yarn-project/aztec-node/src/aztec-node/http_rpc_server.ts @@ -17,15 +17,7 @@ export function createAztecNodeRpcServer(node: AztecNode) { { Tx, L2BlockL2Logs }, false, // disable methods not part of the AztecNode interface - [ - 'start', - 'stop', - 'findContractIndex', - 'findCommitmentIndex', - 'getDataTreePath', - 'getL1ToL2MessageAndIndex', - 'getL1ToL2MessagesTreePath', - ], + ['start', 'stop', 'getDataTreePath', 'getL1ToL2MessageAndIndex', 'getL1ToL2MessagesTreePath'], ); return rpc; } diff --git a/yarn-project/aztec-node/src/aztec-node/server.ts b/yarn-project/aztec-node/src/aztec-node/server.ts index ce7a98a5f22..f1e3f3ad607 100644 --- a/yarn-project/aztec-node/src/aztec-node/server.ts +++ b/yarn-project/aztec-node/src/aztec-node/server.ts @@ -254,13 +254,14 @@ export class AztecNodeService implements AztecNode { } /** - * Find the index of the given contract. - * @param leafValue - The value to search for. - * @returns The index of the given leaf in the contracts tree or undefined if not found. + * Find the index of the given leaf in the given tree. + * @param treeId - The tree to search in. + * @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 findContractIndex(leafValue: Buffer): Promise { + public async findLeafIndex(treeId: MerkleTreeId, leafValue: Buffer): Promise { const committedDb = await this.#getWorldState(); - return committedDb.findLeafIndex(MerkleTreeId.CONTRACT_TREE, leafValue); + return committedDb.findLeafIndex(treeId, leafValue); } /** @@ -273,16 +274,6 @@ export class AztecNodeService implements AztecNode { return committedDb.getSiblingPath(MerkleTreeId.CONTRACT_TREE, leafIndex); } - /** - * Find the index of the given commitment. - * @param leafValue - The value to search for. - * @returns The index of the given leaf in the private data tree or undefined if not found. - */ - public async findCommitmentIndex(leafValue: Buffer): Promise { - const committedDb = await this.#getWorldState(); - return committedDb.findLeafIndex(MerkleTreeId.PRIVATE_DATA_TREE, leafValue); - } - /** * Returns the sibling path for the given index in the data tree. * @param leafIndex - The index of the leaf for which the sibling path is required. @@ -301,9 +292,8 @@ export class AztecNodeService implements AztecNode { */ public async getL1ToL2MessageAndIndex(messageKey: Fr): Promise { // todo: #697 - make this one lookup. - const committedDb = await this.#getWorldState(); + const index = (await this.findLeafIndex(MerkleTreeId.L1_TO_L2_MESSAGES_TREE, messageKey.toBuffer()))!; const message = await this.l1ToL2MessageSource.getConfirmedL1ToL2Message(messageKey); - const index = (await committedDb.findLeafIndex(MerkleTreeId.L1_TO_L2_MESSAGES_TREE, messageKey.toBuffer()))!; return Promise.resolve({ message, index }); } @@ -317,16 +307,6 @@ export class AztecNodeService implements AztecNode { return committedDb.getSiblingPath(MerkleTreeId.L1_TO_L2_MESSAGES_TREE, leafIndex); } - /** - * Find the index of the given nullifier. - * @param nullifier - The nullifier to search for. - * @returns The index of the given leaf in the nullifier tree or undefined if not found. - */ - public async findNullifierIndex(nullifier: Fr): Promise { - const committedDb = await this.#getWorldState(); - return committedDb.findLeafIndex(MerkleTreeId.NULLIFIER_TREE, nullifier.toBuffer()); - } - /** * Gets the storage value at the given contract slot. * @param contract - Address of the contract to query. diff --git a/yarn-project/pxe/src/contract_data_oracle/index.ts b/yarn-project/pxe/src/contract_data_oracle/index.ts index 0984e3dcc70..419fce0319f 100644 --- a/yarn-project/pxe/src/contract_data_oracle/index.ts +++ b/yarn-project/pxe/src/contract_data_oracle/index.ts @@ -1,6 +1,6 @@ import { AztecAddress, CircuitsWasm, MembershipWitness, VK_TREE_HEIGHT } from '@aztec/circuits.js'; import { FunctionDebugMetadata, FunctionSelector, getFunctionDebugMetadata } from '@aztec/foundation/abi'; -import { ContractCommitmentProvider, ContractDatabase } from '@aztec/types'; +import { ContractDatabase, StateInfoProvider } from '@aztec/types'; import { ContractTree } from '../contract_tree/index.js'; @@ -14,7 +14,7 @@ import { ContractTree } from '../contract_tree/index.js'; export class ContractDataOracle { private trees: ContractTree[] = []; - constructor(private db: ContractDatabase, private contractCommitmentProvider: ContractCommitmentProvider) {} + constructor(private db: ContractDatabase, private stateProvider: StateInfoProvider) {} /** * Retrieve the portal contract address associated with the given contract address. @@ -143,7 +143,7 @@ export class ContractDataOracle { } const wasm = await CircuitsWasm.get(); - tree = new ContractTree(contract, this.contractCommitmentProvider, wasm); + tree = new ContractTree(contract, this.stateProvider, wasm); this.trees.push(tree); } return tree; diff --git a/yarn-project/pxe/src/contract_tree/index.ts b/yarn-project/pxe/src/contract_tree/index.ts index 45b2bcc95d7..e187e87f7c3 100644 --- a/yarn-project/pxe/src/contract_tree/index.ts +++ b/yarn-project/pxe/src/contract_tree/index.ts @@ -24,7 +24,7 @@ import { } from '@aztec/circuits.js/abis'; import { ContractAbi, FunctionSelector } from '@aztec/foundation/abi'; import { assertLength } from '@aztec/foundation/serialize'; -import { AztecNode, ContractCommitmentProvider, ContractDao, PublicKey } from '@aztec/types'; +import { AztecNode, ContractDao, MerkleTreeId, PublicKey, StateInfoProvider } from '@aztec/types'; /** * The ContractTree class represents a Merkle tree of functions for a particular contract. @@ -43,7 +43,7 @@ export class ContractTree { * The contract data object containing the ABI and contract address. */ public readonly contract: ContractDao, - private contractCommitmentProvider: ContractCommitmentProvider, + private stateInfoProvider: StateInfoProvider, private wasm: CircuitsWasm, /** * Data associated with the contract constructor for a new contract. @@ -154,7 +154,7 @@ export class ContractTree { public async getContractMembershipWitness() { const index = await this.getContractIndex(); - const siblingPath = await this.contractCommitmentProvider.getContractPath(index); + const siblingPath = await this.stateInfoProvider.getContractPath(index); return new MembershipWitness( CONTRACT_TREE_HEIGHT, index, @@ -229,7 +229,10 @@ 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.contractCommitmentProvider.findContractIndex(commitment.toBuffer()); + this.contractIndex = await this.stateInfoProvider.findLeafIndex( + MerkleTreeId.CONTRACT_TREE, + commitment.toBuffer(), + ); 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/note_processor/note_processor.ts b/yarn-project/pxe/src/note_processor/note_processor.ts index f8a7c115eb1..5c2e8d97bbd 100644 --- a/yarn-project/pxe/src/note_processor/note_processor.ts +++ b/yarn-project/pxe/src/note_processor/note_processor.ts @@ -40,7 +40,7 @@ export class NoteProcessor { private keyStore: KeyStore, private db: Database, private node: AztecNode, - private simulator = getAcirSimulator(db, node, node, node, keyStore), + private simulator = getAcirSimulator(db, node, keyStore), private log = createDebugLogger('aztec:aztec_note_processor'), ) {} diff --git a/yarn-project/pxe/src/pxe_service/pxe_service.ts b/yarn-project/pxe/src/pxe_service/pxe_service.ts index de673813585..88efb0e5b30 100644 --- a/yarn-project/pxe/src/pxe_service/pxe_service.ts +++ b/yarn-project/pxe/src/pxe_service/pxe_service.ts @@ -38,6 +38,7 @@ import { L2BlockL2Logs, L2Tx, LogType, + MerkleTreeId, NodeInfo, NotePreimage, PXE, @@ -82,7 +83,7 @@ export class PXEService implements PXE { this.log = createDebugLogger(logSuffix ? `aztec:pxe_service_${logSuffix}` : `aztec:pxe_service`); this.synchronizer = new Synchronizer(node, db, logSuffix); this.contractDataOracle = new ContractDataOracle(db, node); - this.simulator = getAcirSimulator(db, node, node, node, keyStore, this.contractDataOracle); + this.simulator = getAcirSimulator(db, node, keyStore, this.contractDataOracle); this.sandboxVersion = getPackageInfo().version; } @@ -212,14 +213,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.findCommitmentIndex(noteHashToLookUp.toBuffer()); + const index = await this.node.findLeafIndex(MerkleTreeId.PRIVATE_DATA_TREE, noteHashToLookUp.toBuffer()); if (index === undefined) { throw new Error('Note does not exist.'); } const wasm = await CircuitsWasm.get(); const siloedNullifier = siloNullifier(wasm, contractAddress, innerNullifier!); - const nullifierIndex = await this.node.findNullifierIndex(siloedNullifier); + const nullifierIndex = await this.node.findLeafIndex(MerkleTreeId.NULLIFIER_TREE, siloedNullifier.toBuffer()); if (nullifierIndex !== undefined) { throw new Error('The note has been destroyed.'); } diff --git a/yarn-project/pxe/src/simulator/index.ts b/yarn-project/pxe/src/simulator/index.ts index 82d99879cad..0ffc34a35ef 100644 --- a/yarn-project/pxe/src/simulator/index.ts +++ b/yarn-project/pxe/src/simulator/index.ts @@ -1,5 +1,5 @@ import { AcirSimulator } from '@aztec/acir-simulator'; -import { ContractCommitmentProvider, DataCommitmentProvider, KeyStore, L1ToL2MessageProvider } from '@aztec/types'; +import { KeyStore, StateInfoProvider } from '@aztec/types'; import { ContractDataOracle } from '../contract_data_oracle/index.js'; import { Database } from '../database/database.js'; @@ -10,18 +10,15 @@ import { SimulatorOracle } from '../simulator_oracle/index.js'; */ export function getAcirSimulator( db: Database, - contractCommitmentProvider: ContractCommitmentProvider, - l1ToL2MessageProvider: L1ToL2MessageProvider, - dataCommitmentProvider: DataCommitmentProvider, + stateInfoProvider: StateInfoProvider, keyStore: KeyStore, contractDataOracle?: ContractDataOracle, ) { const simulatorOracle = new SimulatorOracle( - contractDataOracle ?? new ContractDataOracle(db, contractCommitmentProvider), + contractDataOracle ?? new ContractDataOracle(db, stateInfoProvider), db, keyStore, - l1ToL2MessageProvider, - dataCommitmentProvider, + stateInfoProvider, ); return new AcirSimulator(simulatorOracle); } diff --git a/yarn-project/pxe/src/simulator_oracle/index.ts b/yarn-project/pxe/src/simulator_oracle/index.ts index 7aae22c6b4d..4194765854e 100644 --- a/yarn-project/pxe/src/simulator_oracle/index.ts +++ b/yarn-project/pxe/src/simulator_oracle/index.ts @@ -9,7 +9,7 @@ import { HistoricBlockData, PublicKey, } from '@aztec/circuits.js'; -import { DataCommitmentProvider, KeyStore, L1ToL2MessageProvider } from '@aztec/types'; +import { KeyStore, MerkleTreeId, StateInfoProvider } from '@aztec/types'; import { ContractDataOracle } from '../contract_data_oracle/index.js'; import { Database } from '../database/index.js'; @@ -22,8 +22,7 @@ export class SimulatorOracle implements DBOracle { private contractDataOracle: ContractDataOracle, private db: Database, private keyStore: KeyStore, - private l1ToL2MessageProvider: L1ToL2MessageProvider, - private dataTreeProvider: DataCommitmentProvider, + private stateInfoProvider: StateInfoProvider, ) {} getSecretKey(_contractAddress: AztecAddress, pubKey: PublicKey): Promise { @@ -86,10 +85,10 @@ export class SimulatorOracle implements DBOracle { * index of the message in the l1ToL2MessagesTree */ async getL1ToL2Message(msgKey: Fr): Promise { - const messageAndIndex = await this.l1ToL2MessageProvider.getL1ToL2MessageAndIndex(msgKey); + const messageAndIndex = await this.stateInfoProvider.getL1ToL2MessageAndIndex(msgKey); const message = messageAndIndex.message.toFieldArray(); const index = messageAndIndex.index; - const siblingPath = await this.l1ToL2MessageProvider.getL1ToL2MessagesTreePath(index); + const siblingPath = await this.stateInfoProvider.getL1ToL2MessagesTreePath(index); return { message, siblingPath: siblingPath.toFieldArray(), @@ -103,7 +102,7 @@ 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.dataTreeProvider.findCommitmentIndex(commitment.toBuffer()); + return await this.stateInfoProvider.findLeafIndex(MerkleTreeId.PRIVATE_DATA_TREE, commitment.toBuffer()); } /** diff --git a/yarn-project/types/src/interfaces/aztec-node.ts b/yarn-project/types/src/interfaces/aztec-node.ts index c5184e7e920..231e1ea9446 100644 --- a/yarn-project/types/src/interfaces/aztec-node.ts +++ b/yarn-project/types/src/interfaces/aztec-node.ts @@ -4,30 +4,23 @@ import { AztecAddress } from '@aztec/foundation/aztec-address'; import { Fr } from '@aztec/foundation/fields'; import { - ContractCommitmentProvider, ContractData, - DataCommitmentProvider, ExtendedContractData, - L1ToL2MessageProvider, L2Block, L2BlockL2Logs, L2Tx, LogType, MerkleTreeId, + StateInfoProvider, Tx, TxHash, } from '../index.js'; -import { NullifierProvider } from './nullifier_provider.js'; /** * The aztec node. * We will probably implement the additional interfaces by means other than Aztec Node as it's currently a privacy leak */ -export interface AztecNode - extends DataCommitmentProvider, - L1ToL2MessageProvider, - ContractCommitmentProvider, - NullifierProvider { +export interface AztecNode extends StateInfoProvider { /** * Method to determine if the node is ready to accept transactions. * @returns - Flag indicating the readiness for tx submission. diff --git a/yarn-project/types/src/interfaces/contract_commitment_provider.ts b/yarn-project/types/src/interfaces/contract_commitment_provider.ts deleted file mode 100644 index b7539d2b41a..00000000000 --- a/yarn-project/types/src/interfaces/contract_commitment_provider.ts +++ /dev/null @@ -1,22 +0,0 @@ -import { CONTRACT_TREE_HEIGHT } from '@aztec/circuits.js'; - -import { SiblingPath } from '../sibling_path.js'; - -/** - * Interface providing methods for retrieving information about a contract's location in the contract tree. - */ -export interface ContractCommitmentProvider { - /** - * Find the index of the given contract. - * @param leafValue - The value to search for. - * @returns The index of the given leaf in the contracts tree or undefined if not found. - */ - findContractIndex(leafValue: Buffer): Promise; - - /** - * Returns the sibling path for the given index in the contract tree. - * @param leafIndex - The index of the leaf for which the sibling path is required. - * @returns The sibling path for the leaf index. - */ - getContractPath(leafIndex: bigint): Promise>; -} diff --git a/yarn-project/types/src/interfaces/data_commitment_provider.ts b/yarn-project/types/src/interfaces/data_commitment_provider.ts deleted file mode 100644 index bedfa72c1b4..00000000000 --- a/yarn-project/types/src/interfaces/data_commitment_provider.ts +++ /dev/null @@ -1,22 +0,0 @@ -import { PRIVATE_DATA_TREE_HEIGHT } from '@aztec/circuits.js'; - -import { SiblingPath } from '../sibling_path.js'; - -/** - * Interface for providing information about commitments within the data tree. - */ -export interface DataCommitmentProvider { - /** - * Find the index of the given commitment. - * @param leafValue - The value to search for. - * @returns The index of the given leaf of undefined if not found. - */ - findCommitmentIndex(leafValue: Buffer): Promise; - - /** - * Returns the sibling path for the given index in the data tree. - * @param leafIndex - The index of the leaf for which the sibling path is required. - * @returns The sibling path for the leaf index. - */ - getDataTreePath(leafIndex: bigint): Promise>; -} diff --git a/yarn-project/types/src/interfaces/index.ts b/yarn-project/types/src/interfaces/index.ts index cde71ea5604..390f25be66b 100644 --- a/yarn-project/types/src/interfaces/index.ts +++ b/yarn-project/types/src/interfaces/index.ts @@ -1,7 +1,5 @@ -export * from './contract_commitment_provider.js'; +export * from './state_provider.js'; export * from './hasher.js'; -export * from './data_commitment_provider.js'; -export * from './l1_l2_message_provider.js'; export * from './aztec-node.js'; export * from './pxe.js'; export * from './deployed-contract.js'; diff --git a/yarn-project/types/src/interfaces/l1_l2_message_provider.ts b/yarn-project/types/src/interfaces/l1_l2_message_provider.ts deleted file mode 100644 index 5e350737deb..00000000000 --- a/yarn-project/types/src/interfaces/l1_l2_message_provider.ts +++ /dev/null @@ -1,24 +0,0 @@ -import { Fr, L1_TO_L2_MSG_TREE_HEIGHT } from '@aztec/circuits.js'; - -import { L1ToL2MessageAndIndex } from '../l1_to_l2_message.js'; -import { SiblingPath } from '../sibling_path.js'; - -/** - * Interface for providing information about L1 to L2 messages within the tree. - */ -export interface L1ToL2MessageProvider { - /** - * Gets a confirmed/consumed L1 to L2 message for the given message key (throws if not found). - * and its index in the merkle tree - * @param messageKey - The message key. - * @returns The map containing the message and index. - */ - getL1ToL2MessageAndIndex(messageKey: Fr): Promise; - - /** - * Returns the sibling path for a leaf in the committed l1 to l2 data tree. - * @param leafIndex - Index of the leaf in the tree. - * @returns The sibling path. - */ - getL1ToL2MessagesTreePath(leafIndex: bigint): Promise>; -} diff --git a/yarn-project/types/src/interfaces/nullifier_provider.ts b/yarn-project/types/src/interfaces/nullifier_provider.ts deleted file mode 100644 index 4db85ce16f3..00000000000 --- a/yarn-project/types/src/interfaces/nullifier_provider.ts +++ /dev/null @@ -1,13 +0,0 @@ -import { Fr } from '@aztec/foundation/fields'; - -/** - * Interface for providing information about nullifiers within the nullifier tree. - */ -export interface NullifierProvider { - /** - * Find the index of the given nullifier. - * @param nullifier - The nullifier to search for. - * @returns The index of the given leaf of undefined if not found. - */ - findNullifierIndex(nullifier: Fr): Promise; -} diff --git a/yarn-project/types/src/interfaces/state_provider.ts b/yarn-project/types/src/interfaces/state_provider.ts new file mode 100644 index 00000000000..07d6b787dc1 --- /dev/null +++ b/yarn-project/types/src/interfaces/state_provider.ts @@ -0,0 +1,47 @@ +import { CONTRACT_TREE_HEIGHT, Fr, L1_TO_L2_MSG_TREE_HEIGHT, PRIVATE_DATA_TREE_HEIGHT } from '@aztec/circuits.js'; + +import { L1ToL2MessageAndIndex } from '../l1_to_l2_message.js'; +import { MerkleTreeId } from '../merkle_tree_id.js'; +import { SiblingPath } from '../sibling_path.js'; + +/** + * Interface providing methods for retrieving information about content of the state trees. + */ +export interface StateInfoProvider { + /** + * Find the index of the given leaf in the given tree. + * @param treeId - The tree to search in. + * @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; + + /** + * Returns the sibling path for the given index in the contract tree. + * @param leafIndex - The index of the leaf for which the sibling path is required. + * @returns The sibling path for the leaf index. + */ + getContractPath(leafIndex: bigint): Promise>; + + /** + * Returns the sibling path for the given index in the data tree. + * @param leafIndex - The index of the leaf for which the sibling path is required. + * @returns The sibling path for the leaf index. + */ + getDataTreePath(leafIndex: bigint): Promise>; + + /** + * Gets a confirmed/consumed L1 to L2 message for the given message key (throws if not found). + * and its index in the merkle tree + * @param messageKey - The message key. + * @returns The map containing the message and index. + */ + getL1ToL2MessageAndIndex(messageKey: Fr): Promise; + + /** + * Returns the sibling path for a leaf in the committed l1 to l2 data tree. + * @param leafIndex - Index of the leaf in the tree. + * @returns The sibling path. + */ + getL1ToL2MessagesTreePath(leafIndex: bigint): Promise>; +}