Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
benesjan committed Jun 4, 2024
1 parent b2f2b92 commit 3996257
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 39 deletions.
4 changes: 2 additions & 2 deletions yarn-project/pxe/src/database/deferred_note_dao.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { randomInt } from '@aztec/foundation/crypto';
import { DeferredNoteDao } from './deferred_note_dao.js';

export const randomDeferredNoteDao = ({
publicKey = Point.random(),
ivpkM = Point.random(),
note = Note.random(),
contractAddress = AztecAddress.random(),
txHash = randomTxHash(),
Expand All @@ -15,7 +15,7 @@ export const randomDeferredNoteDao = ({
dataStartIndexForTx = randomInt(100),
}: Partial<DeferredNoteDao> = {}) => {
return new DeferredNoteDao(
publicKey,
ivpkM,
note,
contractAddress,
storageSlot,
Expand Down
9 changes: 6 additions & 3 deletions yarn-project/pxe/src/database/deferred_note_dao.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@ import { BufferReader, serializeToBuffer } from '@aztec/foundation/serialize';
*/
export class DeferredNoteDao {
constructor(
/** The public key associated with this note */
public publicKey: PublicKey,
/**
* The incoming viewing public key the note was encrypted with.
* @dev Will never be ovpkM because there are no deferred notes for outgoing.
*/
public ivpkM: PublicKey,
/** The note as emitted from the Noir contract. */
public note: Note,
/** The contract address this note is created in. */
Expand All @@ -29,7 +32,7 @@ export class DeferredNoteDao {

toBuffer(): Buffer {
return serializeToBuffer(
this.publicKey,
this.ivpkM,
this.note,
this.contractAddress,
this.storageSlot,
Expand Down
4 changes: 3 additions & 1 deletion yarn-project/pxe/src/database/pxe_database_test_suite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export function describePxeDatabase(getDatabase: () => PxeDatabase) {
});
});

describe('notes', () => {
describe('incoming notes', () => {
let owners: CompleteAddress[];
let contractAddresses: AztecAddress[];
let storageSlots: Fr[];
Expand Down Expand Up @@ -198,6 +198,8 @@ export function describePxeDatabase(getDatabase: () => PxeDatabase) {
});
});

// TODO(#6867): Add tests for outgoing notes

describe('block header', () => {
it('stores and retrieves the block header', async () => {
const header = makeHeader(randomInt(1000), INITIAL_L2_BLOCK_NUM);
Expand Down
14 changes: 6 additions & 8 deletions yarn-project/pxe/src/note_processor/note_processor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import { NoteProcessor } from './note_processor.js';
const TXS_PER_BLOCK = 4;
const NUM_NOTE_HASHES_PER_BLOCK = TXS_PER_BLOCK * MAX_NEW_NOTE_HASHES_PER_TX;

/** A wrapper containing info about a note we want to mock and insert into a block */
/** A wrapper containing info about a note we want to mock and insert into a block. */
class MockNoteRequest {
constructor(
/** Note we want to insert into a block. */
Expand Down Expand Up @@ -125,17 +125,15 @@ describe('Note Processor', () => {
beforeAll(() => {
const ownerSk = Fr.random();
const partialAddress = Fr.random();
account = CompleteAddress.fromSecretKeyAndPartialAddress(ownerSk, partialAddress);

const allOwnerKeys = deriveKeys(ownerSk);
account = CompleteAddress.fromSecretKeyAndPartialAddress(ownerSk, partialAddress);
ownerIvpkM = account.publicKeys.masterIncomingViewingPublicKey;

ownerIvpkM = allOwnerKeys.publicKeys.masterIncomingViewingPublicKey;
ownerIvskM = allOwnerKeys.masterIncomingViewingSecretKey;
({ masterIncomingViewingSecretKey: ownerIvskM, masterOutgoingViewingSecretKey: ownerOvskM } = deriveKeys(ownerSk));

ownerOvskM = allOwnerKeys.masterOutgoingViewingSecretKey;
ownerOvKeys = new KeyValidationRequest(
allOwnerKeys.publicKeys.masterOutgoingViewingPublicKey,
computeOvskApp(allOwnerKeys.masterOutgoingViewingSecretKey, app),
account.publicKeys.masterOutgoingViewingPublicKey,
computeOvskApp(ownerOvskM, app),
);
});

Expand Down
34 changes: 13 additions & 21 deletions yarn-project/pxe/src/note_processor/note_processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -278,35 +278,29 @@ export class NoteProcessor {
* Retry decoding the given deferred notes because we now have the contract code.
*
* @param deferredNoteDaos - notes that we have previously deferred because the contract was not found
* @returns An array of NoteDaos that were successfully decoded.
* @returns An array of incoming notes that were successfully decoded.
*
* @remarks Caller is responsible for making sure that we have the contract for the
* deferred notes provided: we will not retry notes that fail again.
*/
public async decodeDeferredNotes(
deferredNoteDaos: DeferredNoteDao[],
): Promise<{ incomingNotes: IncomingNoteDao[]; outgoingNotes: OutgoingNoteDao[] }> {
public async decodeDeferredNotes(deferredNoteDaos: DeferredNoteDao[]): Promise<IncomingNoteDao[]> {
const excludedIndices: Set<number> = new Set();
const incomingNotes: IncomingNoteDao[] = [];
const outgoingNotes: OutgoingNoteDao[] = [];

for (const deferredNote of deferredNoteDaos) {
const { publicKey, note, contractAddress, storageSlot, noteTypeId, txHash, newNoteHashes, dataStartIndexForTx } =
const { ivpkM, note, contractAddress, storageSlot, noteTypeId, txHash, newNoteHashes, dataStartIndexForTx } =
deferredNote;
const payload = new L1NotePayload(note, contractAddress, storageSlot, noteTypeId);

const isIncoming = publicKey.equals(this.ivpkM);
const isOutgoing = publicKey.equals(this.ovpkM);

if (!isIncoming && !isOutgoing) {
if (!ivpkM.equals(this.ivpkM)) {
// The note is not for this account, so we skip it.
continue;
}

const { incomingNote, outgoingNote } = await produceNoteDaos(
const { incomingNote } = await produceNoteDaos(
this.simulator,
isIncoming ? this.ivpkM : undefined,
isOutgoing ? this.ovpkM : undefined,
this.ivpkM,
undefined,
payload,
txHash,
newNoteHashes,
Expand All @@ -315,16 +309,14 @@ export class NoteProcessor {
this.log,
);

if (incomingNote) {
incomingNotes.push(incomingNote);
this.stats.decrypted++;
}
if (outgoingNote) {
outgoingNotes.push(outgoingNote);
this.stats.decrypted++;
if (!incomingNote) {
throw new Error('Deferred note could not be decoded.');
}

incomingNotes.push(incomingNote);
this.stats.decrypted++;
}

return { incomingNotes, outgoingNotes };
return incomingNotes;
}
}
6 changes: 2 additions & 4 deletions yarn-project/pxe/src/synchronizer/synchronizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -345,20 +345,18 @@ export class Synchronizer {

// keep track of decoded notes
const incomingNotes: IncomingNoteDao[] = [];
const outgoingNotes: OutgoingNoteDao[] = [];
// now process each txHash
for (const deferredNotes of txHashToDeferredNotes.values()) {
// to be safe, try each note processor in case the deferred notes are for different accounts.
for (const processor of this.noteProcessors) {
const notes = await processor.decodeDeferredNotes(deferredNotes);
incomingNotes.push(...notes.incomingNotes);
outgoingNotes.push(...notes.outgoingNotes);
incomingNotes.push(...notes);
}
}

// now drop the deferred notes, and add the decoded notes
await this.db.removeDeferredNotesByContract(contractAddress);
await this.db.addNotes(incomingNotes, outgoingNotes);
await this.db.addNotes(incomingNotes, []);

incomingNotes.forEach(noteDao => {
this.log.debug(
Expand Down

0 comments on commit 3996257

Please sign in to comment.