Skip to content

Commit

Permalink
refactor: typing contents of MessageLoadOracleInputs (#4351)
Browse files Browse the repository at this point in the history
To be able to use `SiblingPath` in `MessageLoadOracleInputs` I had to
move it to circuits-types from types. Since this results in a lot of
changes I decided to make it a separate PR from the one down the stack.
  • Loading branch information
benesjan authored Feb 1, 2024
1 parent b650706 commit 433babd
Show file tree
Hide file tree
Showing 33 changed files with 80 additions and 63 deletions.
25 changes: 15 additions & 10 deletions yarn-project/acir-simulator/src/acvm/oracle/typed_oracle.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
import {
CompleteAddress,
L1ToL2Message,
MerkleTreeId,
Note,
NoteStatus,
NullifierMembershipWitness,
PublicDataWitness,
PublicKey,
SiblingPath,
UnencryptedL2Log,
} from '@aztec/circuit-types';
import { GrumpkinPrivateKey, Header, PrivateCallStackItem, PublicCallRequest } from '@aztec/circuits.js';
import {
GrumpkinPrivateKey,
Header,
L1_TO_L2_MSG_TREE_HEIGHT,
PrivateCallStackItem,
PublicCallRequest,
} from '@aztec/circuits.js';
import { FunctionSelector } from '@aztec/foundation/abi';
import { AztecAddress } from '@aztec/foundation/aztec-address';
import { EthAddress } from '@aztec/foundation/eth-address';
Expand Down Expand Up @@ -48,21 +56,18 @@ export interface NoteData {
index?: bigint;
}

export class MessageLoadOracleInputs {
export class MessageLoadOracleInputs<N extends number> {
constructor(
/**
* An collapsed array of fields containing all of the l1 to l2 message components.
* `l1ToL2Message.toFieldArray()` -\> [sender, chainId, recipient, version, content, secretHash, deadline, fee]
*/
public message: Fr[],
/** The message. */
public message: L1ToL2Message,
/** The index of the message commitment in the merkle tree. */
public index: bigint,
/** The path in the merkle tree to the message. */
public siblingPath: Fr[],
public siblingPath: SiblingPath<N>,
) {}

toFields(): Fr[] {
return [...this.message, new Fr(this.index), ...this.siblingPath];
return [...this.message.toFieldArray(), new Fr(this.index), ...this.siblingPath.toFieldArray()];
}
}

Expand Down Expand Up @@ -154,7 +159,7 @@ export abstract class TypedOracle {
throw new Error('Not available.');
}

getL1ToL2Message(_msgKey: Fr): Promise<MessageLoadOracleInputs> {
getL1ToL2Message(_msgKey: Fr): Promise<MessageLoadOracleInputs<typeof L1_TO_L2_MSG_TREE_HEIGHT>> {
throw new Error('Not available.');
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -544,13 +544,7 @@ describe('Private Execution test suite', () => {
const mockOracles = async () => {
const tree = await insertLeaves([messageKey ?? preimage.hash()], 'l1ToL2Messages');
oracle.getL1ToL2Message.mockImplementation(async () => {
return Promise.resolve(
new MessageLoadOracleInputs(
preimage.toFieldArray(),
0n,
(await tree.getSiblingPath(0n, false)).toFieldArray(),
),
);
return Promise.resolve(new MessageLoadOracleInputs(preimage, 0n, await tree.getSiblingPath(0n, false)));
});
};

Expand Down
4 changes: 2 additions & 2 deletions yarn-project/acir-simulator/src/public/db.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { EthAddress, FunctionSelector } from '@aztec/circuits.js';
import { EthAddress, FunctionSelector, L1_TO_L2_MSG_TREE_HEIGHT } from '@aztec/circuits.js';
import { AztecAddress } from '@aztec/foundation/aztec-address';
import { Fr } from '@aztec/foundation/fields';

Expand Down Expand Up @@ -74,7 +74,7 @@ export interface CommitmentsDB {
* @param msgKey - The message Key.
* @returns - The l1 to l2 message object
*/
getL1ToL2Message(msgKey: Fr): Promise<MessageLoadOracleInputs>;
getL1ToL2Message(msgKey: Fr): Promise<MessageLoadOracleInputs<typeof L1_TO_L2_MSG_TREE_HEIGHT>>;

/**
* Gets the index of a commitment in the note hash tree.
Expand Down
14 changes: 9 additions & 5 deletions yarn-project/acir-simulator/src/public/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { L1ToL2Message } from '@aztec/circuit-types';
import { L1ToL2Message, SiblingPath } from '@aztec/circuit-types';
import {
AppendOnlyTreeSnapshot,
CallContext,
Expand Down Expand Up @@ -455,13 +455,17 @@ describe('ACIR public execution simulator', () => {
publicContracts.getBytecode.mockResolvedValue(Buffer.from(mintPublicArtifact.bytecode, 'base64'));
publicState.storageRead.mockResolvedValue(Fr.ZERO);

const siblingPath = Array(L1_TO_L2_MSG_TREE_HEIGHT).fill(Fr.random());
const siblingPathBuffers = Array(L1_TO_L2_MSG_TREE_HEIGHT)
.fill(Fr.random())
.map(f => f.toBuffer());
const siblingPath = new SiblingPath(L1_TO_L2_MSG_TREE_HEIGHT, siblingPathBuffers);

let root = messageKey ?? preimage.hash();
for (const sibling of siblingPath) {
root = Fr.fromBuffer(pedersenHash([root.toBuffer(), sibling.toBuffer()]));
for (const sibling of siblingPathBuffers) {
root = Fr.fromBuffer(pedersenHash([root.toBuffer(), sibling]));
}
commitmentsDb.getL1ToL2Message.mockImplementation(() => {
return Promise.resolve(new MessageLoadOracleInputs(preimage.toFieldArray(), 0n, siblingPath));
return Promise.resolve(new MessageLoadOracleInputs(preimage, 0n, siblingPath));
});

return new AppendOnlyTreeSnapshot(
Expand Down
2 changes: 1 addition & 1 deletion yarn-project/aztec-node/src/aztec-node/http_rpc_server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
L2BlockL2Logs,
L2Tx,
LogId,
SiblingPath,
Tx,
TxHash,
} from '@aztec/circuit-types';
Expand All @@ -16,7 +17,6 @@ import { AztecAddress } from '@aztec/foundation/aztec-address';
import { EthAddress } from '@aztec/foundation/eth-address';
import { Fr } from '@aztec/foundation/fields';
import { JsonRpcServer } from '@aztec/foundation/json-rpc/server';
import { SiblingPath } from '@aztec/types/membership';

/**
* Wrap an AztecNode instance with a JSON RPC HTTP server.
Expand Down
2 changes: 1 addition & 1 deletion yarn-project/aztec-node/src/aztec-node/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
NullifierMembershipWitness,
PublicDataWitness,
SequencerConfig,
SiblingPath,
Tx,
TxHash,
} from '@aztec/circuit-types';
Expand Down Expand Up @@ -46,7 +47,6 @@ import {
SequencerClient,
getGlobalVariableBuilder,
} from '@aztec/sequencer-client';
import { SiblingPath } from '@aztec/types/membership';
import {
MerkleTrees,
ServerWorldStateSynchronizer,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import { AztecAddress } from '@aztec/foundation/aztec-address';
import { EthAddress } from '@aztec/foundation/eth-address';
import { Fr } from '@aztec/foundation/fields';
import { createJsonRpcClient, defaultFetch } from '@aztec/foundation/json-rpc/client';
import { SiblingPath } from '@aztec/types/membership';

import { ContractData, ExtendedContractData } from '../../contract_data.js';
import { AztecNode } from '../../interfaces/index.js';
import { L1ToL2MessageAndIndex } from '../../l1_to_l2_message.js';
import { L2Block } from '../../l2_block.js';
import { L2Tx } from '../../l2_tx.js';
import { ExtendedUnencryptedL2Log, L2BlockL2Logs, LogId } from '../../logs/index.js';
import { SiblingPath } from '../../sibling_path/index.js';
import { Tx, TxHash } from '../../tx/index.js';

/**
Expand Down
1 change: 1 addition & 0 deletions yarn-project/circuit-types/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export * from './merkle_tree_id.js';
export * from './mocks.js';
export * from './public_data_write.js';
export * from './simulation_error.js';
export * from './sibling_path/index.js';
export * from './tx/index.js';
export * from './tx_execution_request.js';
export * from './packed_arguments.js';
Expand Down
3 changes: 2 additions & 1 deletion yarn-project/circuit-types/src/interfaces/nullifier_tree.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Fr, NULLIFIER_TREE_HEIGHT, NullifierLeafPreimage } from '@aztec/circuits.js';
import { SiblingPath } from '@aztec/types/membership';

import { SiblingPath } from '../sibling_path/index.js';

/**
* Nullifier membership witness.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Fr, PUBLIC_DATA_TREE_HEIGHT, PublicDataTreeLeafPreimage } from '@aztec/circuits.js';
import { SiblingPath } from '@aztec/types/membership';

import { SiblingPath } from '../sibling_path/index.js';

/**
* Public data witness.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import {
NULLIFIER_TREE_HEIGHT,
PUBLIC_DATA_TREE_HEIGHT,
} from '@aztec/circuits.js';
import { SiblingPath } from '@aztec/types/membership';

import { L1ToL2MessageAndIndex } from '../l1_to_l2_message.js';
import { L2Block } from '../l2_block.js';
import { MerkleTreeId } from '../merkle_tree_id.js';
import { SiblingPath } from '../sibling_path/index.js';
import { NullifierMembershipWitness } from './nullifier_tree.js';
import { PublicDataWitness } from './public_data_tree.js';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ import {
deserializeArrayFromVector,
serializeBufferArrayToVector,
} from '@aztec/foundation/serialize';

import { Hasher } from '../interfaces/index.js';
import { Hasher } from '@aztec/types/interfaces';

/**
* Contains functionality to compute and serialize/deserialize a sibling path.
Expand Down
2 changes: 1 addition & 1 deletion yarn-project/merkle-tree/src/interfaces/indexed_tree.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { SiblingPath } from '@aztec/circuit-types';
import { IndexedTreeLeaf, IndexedTreeLeafPreimage } from '@aztec/foundation/trees';
import { SiblingPath } from '@aztec/types/membership';

import { AppendOnlyTree } from './append_only_tree.js';

Expand Down
2 changes: 1 addition & 1 deletion yarn-project/merkle-tree/src/interfaces/merkle_tree.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { SiblingPath } from '@aztec/types/membership';
import { SiblingPath } from '@aztec/circuit-types';

/**
* Defines the interface for a source of sibling paths.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { SiblingPath } from '@aztec/circuit-types';
import { AztecKVStore, AztecMap } from '@aztec/kv-store';
import { Hasher } from '@aztec/types/interfaces';
import { SiblingPath } from '@aztec/types/membership';

import { AppendOnlyTree } from '../interfaces/append_only_tree.js';
import { TreeBase } from '../tree_base.js';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { SiblingPath } from '@aztec/circuit-types';
import { AztecKVStore, AztecMap } from '@aztec/kv-store';
import { SiblingPath } from '@aztec/types/membership';

import { TreeBase } from '../tree_base.js';
import { TreeSnapshot, TreeSnapshotBuilder } from './snapshot_builder.js';
Expand Down
2 changes: 1 addition & 1 deletion yarn-project/merkle-tree/src/snapshots/snapshot_builder.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { SiblingPath } from '@aztec/circuit-types';
import { IndexedTreeLeafPreimage } from '@aztec/foundation/trees';
import { SiblingPath } from '@aztec/types/membership';

/**
* An interface for a tree that can record snapshots of its contents.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { SiblingPath } from '@aztec/circuit-types';
import { createDebugLogger } from '@aztec/foundation/log';
import { AztecKVStore, AztecLmdbStore } from '@aztec/kv-store';
import { Hasher } from '@aztec/types/interfaces';
import { SiblingPath } from '@aztec/types/membership';

import { randomBytes } from 'crypto';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { SiblingPath } from '@aztec/circuit-types';
import { TreeInsertionStats } from '@aztec/circuit-types/stats';
import { toBufferBE } from '@aztec/foundation/bigint-buffer';
import { Timer } from '@aztec/foundation/timer';
import { IndexedTreeLeaf, IndexedTreeLeafPreimage } from '@aztec/foundation/trees';
import { AztecKVStore, AztecMap } from '@aztec/kv-store';
import { Hasher } from '@aztec/types/interfaces';
import { SiblingPath } from '@aztec/types/membership';

import { BatchInsertionResult, IndexedTree, LowLeafWitnessData, PreimageFactory } from '../interfaces/indexed_tree.js';
import { IndexedTreeSnapshotBuilder } from '../snapshots/indexed_tree_snapshot.js';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { SiblingPath } from '@aztec/circuit-types';
import {
Fr,
NullifierLeaf,
Expand All @@ -8,7 +9,6 @@ import {
import { toBufferBE } from '@aztec/foundation/bigint-buffer';
import { AztecKVStore, AztecLmdbStore } from '@aztec/kv-store';
import { Hasher } from '@aztec/types/interfaces';
import { SiblingPath } from '@aztec/types/membership';

import { INITIAL_LEAF, MerkleTree, Pedersen, loadTree, newTree } from '../../index.js';
import { treeTestSuite } from '../../test/test_suite.js';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { SiblingPath } from '@aztec/circuit-types';
import { AztecKVStore, AztecLmdbStore } from '@aztec/kv-store';
import { Hasher } from '@aztec/types/interfaces';
import { SiblingPath } from '@aztec/types/membership';

import { randomBytes } from 'crypto';

Expand Down
2 changes: 1 addition & 1 deletion yarn-project/merkle-tree/src/test/test_suite.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { SiblingPath } from '@aztec/circuit-types';
import { AztecKVStore, AztecLmdbStore } from '@aztec/kv-store';
import { Hasher } from '@aztec/types/interfaces';
import { SiblingPath } from '@aztec/types/membership';

import { Pedersen } from '../index.js';
import { AppendOnlyTree } from '../interfaces/append_only_tree.js';
Expand Down
2 changes: 1 addition & 1 deletion yarn-project/merkle-tree/src/tree_base.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { SiblingPath } from '@aztec/circuit-types';
import { toBigIntLE, toBufferLE } from '@aztec/foundation/bigint-buffer';
import { DebugLogger, createDebugLogger } from '@aztec/foundation/log';
import { AztecKVStore, AztecMap, AztecSingleton } from '@aztec/kv-store';
import { Hasher } from '@aztec/types/interfaces';
import { SiblingPath } from '@aztec/types/membership';

import { HasherWithStats } from './hasher_with_stats.js';
import { MerkleTree } from './interfaces/merkle_tree.js';
Expand Down
16 changes: 12 additions & 4 deletions yarn-project/pxe/src/simulator_oracle/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,15 @@ import {
PublicDataWitness,
StateInfoProvider,
} from '@aztec/circuit-types';
import { AztecAddress, CompleteAddress, EthAddress, Fr, FunctionSelector, Header } from '@aztec/circuits.js';
import {
AztecAddress,
CompleteAddress,
EthAddress,
Fr,
FunctionSelector,
Header,
L1_TO_L2_MSG_TREE_HEIGHT,
} from '@aztec/circuits.js';
import { FunctionArtifactWithDebugMetadata } from '@aztec/foundation/abi';
import { createDebugLogger } from '@aztec/foundation/log';

Expand Down Expand Up @@ -118,12 +126,12 @@ export class SimulatorOracle implements DBOracle {
* @returns A promise that resolves to the message data, a sibling path and the
* index of the message in the l1ToL2MessageTree
*/
async getL1ToL2Message(msgKey: Fr): Promise<MessageLoadOracleInputs> {
async getL1ToL2Message(msgKey: Fr): Promise<MessageLoadOracleInputs<typeof L1_TO_L2_MSG_TREE_HEIGHT>> {
const messageAndIndex = await this.stateInfoProvider.getL1ToL2MessageAndIndex(msgKey);
const message = messageAndIndex.message.toFieldArray();
const message = messageAndIndex.message;
const index = messageAndIndex.index;
const siblingPath = await this.stateInfoProvider.getL1ToL2MessageSiblingPath('latest', index);
return new MessageLoadOracleInputs(message, index, siblingPath.toFieldArray());
return new MessageLoadOracleInputs(message, index, siblingPath);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
ExtendedContractData,
FunctionCall,
FunctionL2Logs,
SiblingPath,
SimulationError,
Tx,
TxL2Logs,
Expand Down Expand Up @@ -35,7 +36,6 @@ import {
} from '@aztec/circuits.js/factories';
import { makeTuple } from '@aztec/foundation/array';
import { padArrayEnd, times } from '@aztec/foundation/collection';
import { SiblingPath } from '@aztec/types/membership';
import { MerkleTreeOperations, TreeInfo } from '@aztec/world-state';

import { MockProxy, mock } from 'jest-mock-extended';
Expand Down
18 changes: 14 additions & 4 deletions yarn-project/sequencer-client/src/simulator/public_executor.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import { CommitmentsDB, MessageLoadOracleInputs, PublicContractsDB, PublicStateDB } from '@aztec/acir-simulator';
import { ContractDataSource, ExtendedContractData, L1ToL2MessageSource, MerkleTreeId, Tx } from '@aztec/circuit-types';
import { AztecAddress, EthAddress, Fr, FunctionSelector, PublicDataTreeLeafPreimage } from '@aztec/circuits.js';
import {
AztecAddress,
EthAddress,
Fr,
FunctionSelector,
L1_TO_L2_MSG_TREE_HEIGHT,
PublicDataTreeLeafPreimage,
} from '@aztec/circuits.js';
import { computePublicDataTreeLeafSlot } from '@aztec/circuits.js/abis';
import { MerkleTreeOperations } from '@aztec/world-state';

Expand Down Expand Up @@ -144,13 +151,16 @@ export class WorldStatePublicDB implements PublicStateDB {
export class WorldStateDB implements CommitmentsDB {
constructor(private db: MerkleTreeOperations, private l1ToL2MessageSource: L1ToL2MessageSource) {}

public async getL1ToL2Message(messageKey: Fr): Promise<MessageLoadOracleInputs> {
public async getL1ToL2Message(messageKey: Fr): Promise<MessageLoadOracleInputs<typeof L1_TO_L2_MSG_TREE_HEIGHT>> {
// todo: #697 - make this one lookup.
const message = await this.l1ToL2MessageSource.getConfirmedL1ToL2Message(messageKey);
const index = (await this.db.findLeafIndex(MerkleTreeId.L1_TO_L2_MESSAGE_TREE, messageKey.toBuffer()))!;
const siblingPath = await this.db.getSiblingPath(MerkleTreeId.L1_TO_L2_MESSAGE_TREE, index);
const siblingPath = await this.db.getSiblingPath<typeof L1_TO_L2_MSG_TREE_HEIGHT>(
MerkleTreeId.L1_TO_L2_MESSAGE_TREE,
index,
);

return new MessageLoadOracleInputs(message.toFieldArray(), index, siblingPath.toFieldArray());
return new MessageLoadOracleInputs<typeof L1_TO_L2_MSG_TREE_HEIGHT>(message, index, siblingPath);
}

public async getCommitmentIndex(commitment: Fr): Promise<bigint | undefined> {
Expand Down
1 change: 0 additions & 1 deletion yarn-project/types/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
export * from './interfaces/index.js';
export * from './sibling-path/index.js';
Loading

0 comments on commit 433babd

Please sign in to comment.