Skip to content

Commit

Permalink
feat: storing outgoing
Browse files Browse the repository at this point in the history
  • Loading branch information
benesjan committed Jun 12, 2024
1 parent 42f8af5 commit 2c35e29
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 14 deletions.
15 changes: 14 additions & 1 deletion yarn-project/pxe/src/database/outgoing_note_dao.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,22 @@ export const randomOutgoingNoteDao = ({
txHash = randomTxHash(),
storageSlot = Fr.random(),
noteTypeId = Fr.random(),
nonce = Fr.random(),
innerNoteHash = Fr.random(),
index = Fr.random().toBigInt(),
ovpkM = Point.random(),
}: Partial<OutgoingNoteDao> = {}) => {
return new OutgoingNoteDao(note, contractAddress, storageSlot, noteTypeId, txHash, ovpkM);
return new OutgoingNoteDao(
note,
contractAddress,
storageSlot,
noteTypeId,
txHash,
nonce,
innerNoteHash,
index,
ovpkM,
);
};

describe('Outgoing Note DAO', () => {
Expand Down
28 changes: 27 additions & 1 deletion yarn-project/pxe/src/database/outgoing_note_dao.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Note, TxHash } from '@aztec/circuit-types';
import { AztecAddress, Fr, Point, type PublicKey } from '@aztec/circuits.js';
import { toBigIntBE } from '@aztec/foundation/bigint-buffer';
import { BufferReader, serializeToBuffer } from '@aztec/foundation/serialize';

/**
Expand All @@ -17,6 +18,15 @@ export class OutgoingNoteDao {
public noteTypeId: Fr,
/** The hash of the tx the note was created in. */
public txHash: TxHash,
/** The nonce of the note. */
public nonce: Fr,
/**
* Inner note hash of the note. This is customizable by the app circuit.
* We can use this value to compute siloedNoteHash and uniqueSiloedNoteHash.
*/
public innerNoteHash: Fr,
/** The location of the relevant note in the note hash tree. */
public index: bigint,
/** The public key with which the note was encrypted. */
public ovpkM: PublicKey,
) {}
Expand All @@ -28,6 +38,9 @@ export class OutgoingNoteDao {
this.storageSlot,
this.noteTypeId,
this.txHash.buffer,
this.nonce,
this.innerNoteHash,
this.index,
this.ovpkM,
]);
}
Expand All @@ -39,9 +52,22 @@ export class OutgoingNoteDao {
const storageSlot = Fr.fromBuffer(reader);
const noteTypeId = Fr.fromBuffer(reader);
const txHash = new TxHash(reader.readBytes(TxHash.SIZE));
const nonce = Fr.fromBuffer(reader);
const innerNoteHash = Fr.fromBuffer(reader);
const index = toBigIntBE(reader.readBytes(32));
const publicKey = Point.fromBuffer(reader);

return new OutgoingNoteDao(note, contractAddress, storageSlot, noteTypeId, txHash, publicKey);
return new OutgoingNoteDao(
note,
contractAddress,
storageSlot,
noteTypeId,
txHash,
nonce,
innerNoteHash,
index,
publicKey,
);
}

toString() {
Expand Down
56 changes: 44 additions & 12 deletions yarn-project/pxe/src/note_processor/produce_note_dao.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,6 @@ export async function produceNoteDaos(
// are not attempting to derive a nullifier.
let incomingDeferredNote: DeferredNoteDao | undefined;

if (ovpkM) {
outgoingNote = new OutgoingNoteDao(
payload.note,
payload.contractAddress,
payload.storageSlot,
payload.noteTypeId,
txHash,
ovpkM,
);
}

try {
if (ivpkM) {
const { noteHashIndex, nonce, innerNoteHash, siloedNullifier } = await findNoteIndexAndNullifier(
Expand All @@ -69,6 +58,7 @@ export async function produceNoteDaos(
txHash,
payload,
excludedIndices,
true, // For incoming we always compute a nullifier.
);
const index = BigInt(dataStartIndexForTx + noteHashIndex);
excludedIndices?.add(noteHashIndex);
Expand Down Expand Up @@ -107,6 +97,46 @@ export async function produceNoteDaos(
}
}

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.innerNoteHash,
incomingNote.index,
ovpkM,
);
} else {
// TODO(benesjan): what about excluded indices here?
const { noteHashIndex, nonce, innerNoteHash } = await findNoteIndexAndNullifier(
simulator,
newNoteHashes,
txHash,
payload,
excludedIndices,
true, // For outgoing we do not compute a nullifier.
);
const index = BigInt(dataStartIndexForTx + noteHashIndex);
outgoingNote = new OutgoingNoteDao(
payload.note,
payload.contractAddress,
payload.storageSlot,
payload.noteTypeId,
txHash,
nonce,
innerNoteHash,
index,
ovpkM,
);
}
}

return {
incomingNote,
outgoingNote,
Expand All @@ -124,6 +154,7 @@ export async function produceNoteDaos(
* @param l1NotePayload - The note payload.
* @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 computeNullifier - A flag indicating whether to compute the nullifier or just return 0.
* @returns Nonce, index, inner hash and siloed nullifier for a given note.
* @throws If cannot find the nonce for the note.
*/
Expand All @@ -133,6 +164,7 @@ async function findNoteIndexAndNullifier(
txHash: TxHash,
{ contractAddress, storageSlot, noteTypeId, note }: L1NotePayload,
excludedIndices: Set<number>,
computeNullifier: boolean,
) {
let noteHashIndex = 0;
let nonce: Fr | undefined;
Expand All @@ -157,7 +189,7 @@ async function findNoteIndexAndNullifier(
expectedNonce,
storageSlot,
noteTypeId,
true,
computeNullifier,
note,
));

Expand Down

0 comments on commit 2c35e29

Please sign in to comment.