Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
benesjan committed Oct 4, 2024
1 parent a23d987 commit 581e7b8
Show file tree
Hide file tree
Showing 9 changed files with 275 additions and 232 deletions.
2 changes: 1 addition & 1 deletion yarn-project/pxe/src/database/incoming_note_dao.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { toBigIntBE } from '@aztec/foundation/bigint-buffer';
import { BufferReader, serializeToBuffer } from '@aztec/foundation/serialize';
import { type NoteData } from '@aztec/simulator';

import { type NoteInfo } from '../note_processor/find_note_index_and_nullifier.js';
import { type NoteInfo } from '../note_processor/utils/index.js';

/**
* A note with contextual data which was decrypted as incoming.
Expand Down
2 changes: 1 addition & 1 deletion yarn-project/pxe/src/database/outgoing_note_dao.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { NoteSelector } from '@aztec/foundation/abi';
import { toBigIntBE } from '@aztec/foundation/bigint-buffer';
import { BufferReader, serializeToBuffer } from '@aztec/foundation/serialize';

import { type NoteInfo } from '../note_processor/find_note_index_and_nullifier.js';
import { type NoteInfo } from '../note_processor/utils/index.js';

/**
* A note with contextual data which was decrypted as outgoing.
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 @@ -12,7 +12,7 @@ import { type IncomingNoteDao } from '../database/incoming_note_dao.js';
import { type PxeDatabase } from '../database/index.js';
import { type OutgoingNoteDao } from '../database/outgoing_note_dao.js';
import { getAcirSimulator } from '../simulator/index.js';
import { produceNoteDaos } from './produce_note_dao.js';
import { produceNoteDaos } from './utils/produce_note_daos.js';

/**
* Contains all the decrypted data in this array so that we can later batch insert it all into the database.
Expand Down
227 changes: 0 additions & 227 deletions yarn-project/pxe/src/note_processor/produce_note_dao.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { L1NotePayload, Note } from '@aztec/circuit-types';
import { type Fr } from '@aztec/foundation/fields';
import { ContractNotFoundError } from '@aztec/simulator';

import { type PxeDatabase } from '../database/pxe_database.js';
import { type PxeDatabase } from '../../database/pxe_database.js';

/**
* Inserts publicly delivered nullable fields into the note payload.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { type L1NotePayload, type TxHash } from '@aztec/circuit-types';
import { Fr } from '@aztec/circuits.js';
import { computeNoteHashNonce, siloNullifier } from '@aztec/circuits.js/hash';
import { Fr } from '@aztec/foundation/fields';
import { type AcirSimulator } from '@aztec/simulator';

export interface NoteInfo {
Expand Down
2 changes: 2 additions & 0 deletions yarn-project/pxe/src/note_processor/utils/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { produceNoteDaos } from './produce_note_daos.js';
export { NoteInfo } from './brute_force_note_info.js';
114 changes: 114 additions & 0 deletions yarn-project/pxe/src/note_processor/utils/produce_note_daos.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
import { type L1NotePayload, type PublicKey, type TxHash, type UnencryptedTxL2Logs } from '@aztec/circuit-types';
import { type Fr } from '@aztec/foundation/fields';
import { type Logger } from '@aztec/foundation/log';
import { type AcirSimulator } from '@aztec/simulator';

import { type DeferredNoteDao } from '../../database/deferred_note_dao.js';
import { IncomingNoteDao } from '../../database/incoming_note_dao.js';
import { OutgoingNoteDao } from '../../database/outgoing_note_dao.js';
import { type PxeDatabase } from '../../database/pxe_database.js';
import { produceNoteDaosForKey } from './produce_note_daos_for_key.js';

/**
* Decodes a note from a transaction that we know was intended for us.
* Throws if we do not yet have the contract corresponding to the note in our database.
* Accepts a set of excluded indices, which are indices that have been assigned a note in the same tx.
* Inserts the index of the note into the excludedIndices set if the note is successfully decoded.
*
* @param simulator - An instance of AcirSimulator.
* @param db - An instance of PxeDatabase.
* @param ivpkM - The public counterpart to the secret key to be used in the decryption of incoming note logs.
* @param ovpkM - The public counterpart to the secret key to be used in the decryption of outgoing note logs.
* @param payload - An instance of l1NotePayload.
* @param txHash - The hash of the transaction that created the note. Equivalent to the first nullifier of the transaction.
* @param noteHashes - New note hashes in this transaction, one of which belongs to this note.
* @param dataStartIndexForTx - The next available leaf index for the note hash tree for this transaction.
* @param excludedIndices - Indices that have been assigned a note in the same tx. Notes in a tx can have the same l1NotePayload, we need to find a different index for each replicate.
* @param logger - An instance of Logger.
* @param unencryptedLogs - Unencrypted logs for the transaction (used to complete partial notes).
* @returns An object containing the incoming, outgoing, and deferred notes.
*/
export async function produceNoteDaos(
simulator: AcirSimulator,
db: PxeDatabase,
ivpkM: PublicKey | undefined,
ovpkM: PublicKey | undefined,
payload: L1NotePayload,
txHash: TxHash,
noteHashes: Fr[],
dataStartIndexForTx: number,
excludedIndices: Set<number>,
logger: Logger,
unencryptedLogs: UnencryptedTxL2Logs,
): Promise<{
incomingNote: IncomingNoteDao | undefined;
outgoingNote: OutgoingNoteDao | undefined;
incomingDeferredNote: DeferredNoteDao | undefined;
outgoingDeferredNote: DeferredNoteDao | undefined;
}> {
// WARNING: This code is full of tech debt and will be refactored once we have final design of partial notes
// delivery.
if (!ivpkM && !ovpkM) {
throw new Error('Both ivpkM and ovpkM are undefined. Cannot create note.');
}

let incomingNote: IncomingNoteDao | undefined;
let outgoingNote: OutgoingNoteDao | undefined;
let incomingDeferredNote: DeferredNoteDao | undefined;
let outgoingDeferredNote: DeferredNoteDao | undefined;

if (ivpkM) {
[incomingNote, incomingDeferredNote] = await produceNoteDaosForKey(
simulator,
db,
ivpkM,
payload,
txHash,
noteHashes,
dataStartIndexForTx,
excludedIndices,
logger,
unencryptedLogs,
IncomingNoteDao.fromPayloadAndNoteInfo,
);
}

if (ovpkM) {
if (incomingNote) {
// Incoming note is defined meaning that this PXE has both the incoming and outgoing keys. We can skip computing
// note hash and note index since we already have them in the incoming note.
outgoingNote = new OutgoingNoteDao(
payload.note,
payload.contractAddress,
payload.storageSlot,
payload.noteTypeId,
txHash,
incomingNote.nonce,
incomingNote.noteHash,
incomingNote.index,
ovpkM,
);
} else {
[outgoingNote, outgoingDeferredNote] = await produceNoteDaosForKey(
simulator,
db,
ovpkM,
payload,
txHash,
noteHashes,
dataStartIndexForTx,
excludedIndices,
logger,
unencryptedLogs,
OutgoingNoteDao.fromPayloadAndNoteInfo,
);
}
}

return {
incomingNote,
outgoingNote,
incomingDeferredNote,
outgoingDeferredNote,
};
}
Loading

0 comments on commit 581e7b8

Please sign in to comment.