Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
sklppy88 committed Oct 28, 2024
1 parent f3d4bce commit c7e29fb
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 36 deletions.
2 changes: 1 addition & 1 deletion yarn-project/pxe/src/database/deferred_note_dao.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { BufferReader, serializeToBuffer } from '@aztec/foundation/serialize';
*/
export class DeferredNoteDao {
constructor(
/** IvpkM or OvpkM (depending on if incoming or outgoing) the note was encrypted with. */
/** Address Point or OvpkM (depending on if incoming or outgoing) the note was encrypted with. */
public publicKey: PublicKey,
/** The note payload delivered via L1. */
public payload: L1NotePayload,
Expand Down
4 changes: 2 additions & 2 deletions yarn-project/pxe/src/database/incoming_note_dao.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export const randomIncomingNoteDao = ({
noteHash = Fr.random(),
siloedNullifier = Fr.random(),
index = Fr.random().toBigInt(),
ivpkM = Point.random(),
addressPoint = Point.random(),
}: Partial<IncomingNoteDao> = {}) => {
return new IncomingNoteDao(
note,
Expand All @@ -26,7 +26,7 @@ export const randomIncomingNoteDao = ({
noteHash,
siloedNullifier,
index,
ivpkM,
addressPoint,
);
};

Expand Down
8 changes: 4 additions & 4 deletions yarn-project/pxe/src/database/incoming_note_dao.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,15 @@ export class IncomingNoteDao implements NoteData {
/** The location of the relevant note in the note hash tree. */
public index: bigint,
/** The public key with which the note was encrypted. */
public ivpkM: PublicKey,
public addressPoint: PublicKey,
) {}

static fromPayloadAndNoteInfo(
note: Note,
payload: L1NotePayload,
noteInfo: NoteInfo,
dataStartIndexForTx: number,
ivpkM: PublicKey,
addressPoint: PublicKey,
) {
const noteHashIndexInTheWholeTree = BigInt(dataStartIndexForTx + noteInfo.noteHashIndex);
return new IncomingNoteDao(
Expand All @@ -58,7 +58,7 @@ export class IncomingNoteDao implements NoteData {
noteInfo.noteHash,
noteInfo.siloedNullifier,
noteHashIndexInTheWholeTree,
ivpkM,
addressPoint,
);
}

Expand All @@ -73,7 +73,7 @@ export class IncomingNoteDao implements NoteData {
this.noteHash,
this.siloedNullifier,
this.index,
this.ivpkM,
this.addressPoint,
]);
}
static fromBuffer(buffer: Buffer | BufferReader) {
Expand Down
3 changes: 2 additions & 1 deletion yarn-project/pxe/src/database/kv_pxe_database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
import {
AztecAddress,
CompleteAddress,
computePoint,
type ContractInstanceWithAddress,
Header,
SerializableContractInstance,
Expand Down Expand Up @@ -263,7 +264,7 @@ export class KVPxeDatabase implements PxeDatabase {

getIncomingNotes(filter: IncomingNotesFilter): Promise<IncomingNoteDao[]> {
const publicKey: PublicKey | undefined = filter.owner
? this.#getCompleteAddress(filter.owner)?.publicKeys.masterIncomingViewingPublicKey
? computePoint(filter.owner)
: undefined;

filter.status = filter.status ?? NoteStatus.ACTIVE;
Expand Down
4 changes: 2 additions & 2 deletions yarn-project/pxe/src/database/outgoing_note_dao.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export class OutgoingNoteDao {
payload: L1NotePayload,
noteInfo: NoteInfo,
dataStartIndexForTx: number,
ivpkM: PublicKey,
ovpkM: PublicKey,
) {
const noteHashIndexInTheWholeTree = BigInt(dataStartIndexForTx + noteInfo.noteHashIndex);
return new OutgoingNoteDao(
Expand All @@ -51,7 +51,7 @@ export class OutgoingNoteDao {
noteInfo.nonce,
noteInfo.noteHash,
noteHashIndexInTheWholeTree,
ivpkM,
ovpkM,
);
}

Expand Down
15 changes: 8 additions & 7 deletions yarn-project/pxe/src/database/pxe_database_test_suite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { type IncomingNotesFilter, NoteStatus, type OutgoingNotesFilter, randomT
import {
AztecAddress,
CompleteAddress,
computePoint,
INITIAL_L2_BLOCK_NUM,
PublicKeys,
SerializableContractInstance,
Expand Down Expand Up @@ -101,7 +102,7 @@ export function describePxeDatabase(getDatabase: () => PxeDatabase) {

[
() => ({ owner: owners[0].address }),
() => notes.filter(note => note.ivpkM.equals(owners[0].publicKeys.masterIncomingViewingPublicKey)),
() => notes.filter(note => note.addressPoint.equals(owners[0].publicKeys.masterIncomingViewingPublicKey)),
],

[
Expand All @@ -123,7 +124,7 @@ export function describePxeDatabase(getDatabase: () => PxeDatabase) {
randomIncomingNoteDao({
contractAddress: contractAddresses[i % contractAddresses.length],
storageSlot: storageSlots[i % storageSlots.length],
ivpkM: owners[i % owners.length].publicKeys.masterIncomingViewingPublicKey,
addressPoint: owners[i % owners.length].publicKeys.masterIncomingViewingPublicKey,
index: BigInt(i),
}),
);
Expand Down Expand Up @@ -156,7 +157,7 @@ export function describePxeDatabase(getDatabase: () => PxeDatabase) {
// Nullify all notes and use the same filter as other test cases
for (const owner of owners) {
const notesToNullify = notes.filter(note =>
note.ivpkM.equals(owner.publicKeys.masterIncomingViewingPublicKey),
note.addressPoint.equals(computePoint(owner.address)),
);
const nullifiers = notesToNullify.map(note => note.siloedNullifier);
await expect(
Expand All @@ -173,10 +174,10 @@ export function describePxeDatabase(getDatabase: () => PxeDatabase) {
await database.addNotes(notes, []);

const notesToNullify = notes.filter(note =>
note.ivpkM.equals(owners[0].publicKeys.masterIncomingViewingPublicKey),
note.addressPoint.equals(computePoint(owners[0].address)),
);
const nullifiers = notesToNullify.map(note => note.siloedNullifier);
await expect(database.removeNullifiedNotes(nullifiers, notesToNullify[0].ivpkM)).resolves.toEqual(
await expect(database.removeNullifiedNotes(nullifiers, notesToNullify[0].addressPoint)).resolves.toEqual(
notesToNullify,
);

Expand All @@ -191,10 +192,10 @@ export function describePxeDatabase(getDatabase: () => PxeDatabase) {
await database.addNotes(notes, []);

const notesToNullify = notes.filter(note =>
note.ivpkM.equals(owners[0].publicKeys.masterIncomingViewingPublicKey),
note.addressPoint.equals(computePoint(owners[0].address)),
);
const nullifiers = notesToNullify.map(note => note.siloedNullifier);
await expect(database.removeNullifiedNotes(nullifiers, notesToNullify[0].ivpkM)).resolves.toEqual(
await expect(database.removeNullifiedNotes(nullifiers, notesToNullify[0].addressPoint)).resolves.toEqual(
notesToNullify,
);

Expand Down
12 changes: 6 additions & 6 deletions yarn-project/pxe/src/note_processor/utils/produce_note_daos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { produceNoteDaosForKey } from './produce_note_daos_for_key.js';
*
* @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 addressPoint - 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.
Expand All @@ -31,7 +31,7 @@ import { produceNoteDaosForKey } from './produce_note_daos_for_key.js';
export async function produceNoteDaos(
simulator: AcirSimulator,
db: PxeDatabase,
ivpkM: PublicKey | undefined,
addressPoint: PublicKey | undefined,
ovpkM: PublicKey | undefined,
payload: L1NotePayload,
txHash: TxHash,
Expand All @@ -46,20 +46,20 @@ export async function produceNoteDaos(
incomingDeferredNote: DeferredNoteDao | undefined;
outgoingDeferredNote: DeferredNoteDao | undefined;
}> {
if (!ivpkM && !ovpkM) {
throw new Error('Both ivpkM and ovpkM are undefined. Cannot create note.');
if (!addressPoint && !ovpkM) {
throw new Error('Both addressPoint 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) {
if (addressPoint) {
[incomingNote, incomingDeferredNote] = await produceNoteDaosForKey(
simulator,
db,
ivpkM,
addressPoint,
payload,
txHash,
noteHashes,
Expand Down
22 changes: 11 additions & 11 deletions yarn-project/pxe/src/pxe_service/pxe_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import {
computeAddressSecret,
computeContractAddressFromInstance,
computeContractClassId,
computePoint,
getContractClassFromArtifact,
} from '@aztec/circuits.js';
import { computeNoteHashNonce, siloNullifier } from '@aztec/circuits.js/hash';
Expand Down Expand Up @@ -124,14 +125,13 @@ export class PXEService implements PXE {

private async restoreNoteProcessors() {
const accounts = await this.keyStore.getAccounts();
const publicKeys = accounts.map(async account => await this.keyStore.getMasterIncomingViewingPublicKey(account));
const publicKeysSet = new Set(publicKeys.map(k => k.toString()));
const accountsSet = new Set(accounts.map(k => k.toString()));

const registeredAddresses = await this.db.getCompleteAddresses();

let count = 0;
for (const address of registeredAddresses) {
if (!publicKeysSet.has(address.publicKeys.masterIncomingViewingPublicKey.toString())) {
if (!accountsSet.has(address.address.toString())) {
continue;
}

Expand Down Expand Up @@ -307,10 +307,10 @@ export class PXEService implements PXE {
let owner = filter.owner;
if (owner === undefined) {
const completeAddresses = (await this.db.getCompleteAddresses()).find(address =>
address.publicKeys.masterIncomingViewingPublicKey.equals(dao.ivpkM),
computePoint(address.address).equals(dao.addressPoint),
);
if (completeAddresses === undefined) {
throw new Error(`Cannot find complete address for IvpkM ${dao.ivpkM.toString()}`);
throw new Error(`Cannot find complete address for addressPoint ${dao.addressPoint.toString()}`);
}
owner = completeAddresses.address;
}
Expand Down Expand Up @@ -405,18 +405,18 @@ export class PXEService implements PXE {
noteHash,
siloedNullifier,
index,
owner.publicKeys.masterIncomingViewingPublicKey,
computePoint(owner.address),
),
scope,
);
}
}

public async addNullifiedNote(note: ExtendedNote) {
const owner = await this.db.getCompleteAddress(note.owner);
if (!owner) {
throw new Error(`Unknown account: ${note.owner.toString()}`);
}
// const owner = await this.db.getCompleteAddress(note.owner);
// if (!owner) {
// throw new Error(`Unknown account: ${note.owner.toString()}`);
// }

const nonces = await this.#getNoteNonces(note);
if (nonces.length === 0) {
Expand Down Expand Up @@ -453,7 +453,7 @@ export class PXEService implements PXE {
noteHash,
Fr.ZERO, // We are not able to derive
index,
owner.publicKeys.masterIncomingViewingPublicKey,
computePoint(note.owner),
),
);
}
Expand Down
4 changes: 2 additions & 2 deletions yarn-project/pxe/src/synchronizer/synchronizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -373,9 +373,9 @@ export class Synchronizer {
// now group the decoded incoming notes by public key
const publicKeyToIncomingNotes: Map<PublicKey, IncomingNoteDao[]> = new Map();
for (const noteDao of notes) {
const notesForPublicKey = publicKeyToIncomingNotes.get(noteDao.ivpkM) ?? [];
const notesForPublicKey = publicKeyToIncomingNotes.get(noteDao.addressPoint) ?? [];
notesForPublicKey.push(noteDao);
publicKeyToIncomingNotes.set(noteDao.ivpkM, notesForPublicKey);
publicKeyToIncomingNotes.set(noteDao.addressPoint, notesForPublicKey);
}

// now for each group, look for the nullifiers in the nullifier tree
Expand Down

0 comments on commit c7e29fb

Please sign in to comment.