Skip to content

Commit

Permalink
feat: collapse interfaces for single implementation (#2599)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
LHerskind authored Oct 2, 2023
1 parent 9830fbf commit 860f340
Show file tree
Hide file tree
Showing 15 changed files with 82 additions and 153 deletions.
10 changes: 1 addition & 9 deletions yarn-project/aztec-node/src/aztec-node/http_rpc_server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
34 changes: 7 additions & 27 deletions yarn-project/aztec-node/src/aztec-node/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<bigint | undefined> {
public async findLeafIndex(treeId: MerkleTreeId, leafValue: Buffer): Promise<bigint | undefined> {
const committedDb = await this.#getWorldState();
return committedDb.findLeafIndex(MerkleTreeId.CONTRACT_TREE, leafValue);
return committedDb.findLeafIndex(treeId, leafValue);
}

/**
Expand All @@ -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<bigint | undefined> {
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.
Expand All @@ -301,9 +292,8 @@ export class AztecNodeService implements AztecNode {
*/
public async getL1ToL2MessageAndIndex(messageKey: Fr): Promise<L1ToL2MessageAndIndex> {
// 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 });
}

Expand All @@ -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<bigint | undefined> {
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.
Expand Down
6 changes: 3 additions & 3 deletions yarn-project/pxe/src/contract_data_oracle/index.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -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.
Expand Down Expand Up @@ -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;
Expand Down
11 changes: 7 additions & 4 deletions yarn-project/pxe/src/contract_tree/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand Down Expand Up @@ -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<typeof CONTRACT_TREE_HEIGHT>(
CONTRACT_TREE_HEIGHT,
index,
Expand Down Expand Up @@ -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}.`,
Expand Down
2 changes: 1 addition & 1 deletion yarn-project/pxe/src/note_processor/note_processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
) {}

Expand Down
7 changes: 4 additions & 3 deletions yarn-project/pxe/src/pxe_service/pxe_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import {
L2BlockL2Logs,
L2Tx,
LogType,
MerkleTreeId,
NodeInfo,
NotePreimage,
PXE,
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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.');
}
Expand Down
11 changes: 4 additions & 7 deletions yarn-project/pxe/src/simulator/index.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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);
}
11 changes: 5 additions & 6 deletions yarn-project/pxe/src/simulator_oracle/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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<GrumpkinPrivateKey> {
Expand Down Expand Up @@ -86,10 +85,10 @@ export class SimulatorOracle implements DBOracle {
* index of the message in the l1ToL2MessagesTree
*/
async getL1ToL2Message(msgKey: Fr): Promise<MessageLoadOracleInputs> {
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(),
Expand All @@ -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());
}

/**
Expand Down
11 changes: 2 additions & 9 deletions yarn-project/types/src/interfaces/aztec-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
22 changes: 0 additions & 22 deletions yarn-project/types/src/interfaces/contract_commitment_provider.ts

This file was deleted.

22 changes: 0 additions & 22 deletions yarn-project/types/src/interfaces/data_commitment_provider.ts

This file was deleted.

4 changes: 1 addition & 3 deletions yarn-project/types/src/interfaces/index.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down
24 changes: 0 additions & 24 deletions yarn-project/types/src/interfaces/l1_l2_message_provider.ts

This file was deleted.

13 changes: 0 additions & 13 deletions yarn-project/types/src/interfaces/nullifier_provider.ts

This file was deleted.

Loading

0 comments on commit 860f340

Please sign in to comment.