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 9e3e536 commit 6670315
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 38 deletions.
10 changes: 5 additions & 5 deletions yarn-project/pxe/src/database/kv_pxe_database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
} from '@aztec/circuits.js';
import { type ContractArtifact } from '@aztec/foundation/abi';
import { toBufferBE } from '@aztec/foundation/bigint-buffer';
import { Fr, type Point } from '@aztec/foundation/fields';
import { Fr } from '@aztec/foundation/fields';
import {
type AztecArray,
type AztecKVStore,
Expand Down Expand Up @@ -545,12 +545,12 @@ export class KVPxeDatabase implements PxeDatabase {
return Promise.resolve(Array.from(this.#addresses).map(v => CompleteAddress.fromBuffer(v)));
}

getSynchedBlockNumberForPublicKey(publicKey: Point): number | undefined {
return this.#syncedBlockPerPublicKey.get(publicKey.toString());
getSynchedBlockNumberForAccount(account: AztecAddress): number | undefined {
return this.#syncedBlockPerPublicKey.get(account.toString());
}

setSynchedBlockNumberForPublicKey(publicKey: Point, blockNumber: number): Promise<void> {
return this.#syncedBlockPerPublicKey.set(publicKey.toString(), blockNumber);
setSynchedBlockNumberForAccount(account: AztecAddress, blockNumber: number): Promise<void> {
return this.#syncedBlockPerPublicKey.set(account.toString(), blockNumber);
}

async estimateSize(): Promise<number> {
Expand Down
8 changes: 4 additions & 4 deletions yarn-project/pxe/src/database/pxe_database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,16 +169,16 @@ export interface PxeDatabase extends ContractArtifactDatabase, ContractInstanceD

/**
* Updates up to which block number we have processed notes for a given public key.
* @param publicKey - The public key to set the synched block number for.
* @param account - The account to set the synched block number for.
* @param blockNumber - The block number to set.
*/
setSynchedBlockNumberForPublicKey(publicKey: PublicKey, blockNumber: number): Promise<void>;
setSynchedBlockNumberForAccount(account: AztecAddress, blockNumber: number): Promise<void>;

/**
* Get the synched block number for a given public key.
* @param publicKey - The public key to get the synched block number for.
* @param account - The account to get the synched block number for.
*/
getSynchedBlockNumberForPublicKey(publicKey: PublicKey): number | undefined;
getSynchedBlockNumberForAccount(account: AztecAddress): number | undefined;

/**
* Returns the estimated size in bytes of this db.
Expand Down
6 changes: 3 additions & 3 deletions yarn-project/pxe/src/note_processor/note_processor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ describe('Note Processor', () => {
);
});

beforeEach(async () => {
beforeEach(() => {
database = new KVPxeDatabase(openTmpStore());
addNotesSpy = jest.spyOn(database, 'addNotes');

Expand All @@ -179,7 +179,7 @@ describe('Note Processor', () => {
keyStore.getMasterIncomingViewingPublicKey.mockResolvedValue(account.publicKeys.masterIncomingViewingPublicKey);
keyStore.getMasterOutgoingViewingPublicKey.mockResolvedValue(account.publicKeys.masterOutgoingViewingPublicKey);

noteProcessor = await NoteProcessor.create(account, keyStore, database, aztecNode, INITIAL_L2_BLOCK_NUM, simulator);
noteProcessor = NoteProcessor.create(account, keyStore, database, aztecNode, INITIAL_L2_BLOCK_NUM, simulator);

simulator.computeNoteHashAndOptionallyANullifier.mockImplementation((...args) =>
Promise.resolve({
Expand Down Expand Up @@ -358,7 +358,7 @@ describe('Note Processor', () => {
const blocks = mockBlocks([request]);
await noteProcessor.process(blocks);

const newNoteProcessor = await NoteProcessor.create(
const newNoteProcessor = NoteProcessor.create(
account,
keyStore,
database,
Expand Down
37 changes: 16 additions & 21 deletions yarn-project/pxe/src/note_processor/note_processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {
type CompleteAddress,
INITIAL_L2_BLOCK_NUM,
MAX_NOTE_HASHES_PER_TX,
type PublicKey,
computeAddressSecret,
} from '@aztec/circuits.js';
import { type Fr } from '@aztec/foundation/fields';
Expand Down Expand Up @@ -54,10 +53,6 @@ export class NoteProcessor {

private constructor(
public readonly account: CompleteAddress,
/** The public counterpart to the secret key to be used in the decryption of incoming note logs. */
private readonly ivpkM: PublicKey,
/** The public counterpart to the secret key to be used in the decryption of outgoing note logs. */
private readonly ovpkM: PublicKey,
private keyStore: KeyStore,
private db: PxeDatabase,
private node: AztecNode,
Expand All @@ -66,7 +61,7 @@ export class NoteProcessor {
private log: Logger,
) {}

public static async create(
public static create(
account: CompleteAddress,
keyStore: KeyStore,
db: PxeDatabase,
Expand All @@ -75,10 +70,7 @@ export class NoteProcessor {
simulator = getAcirSimulator(db, node, keyStore),
log = createDebugLogger('aztec:note_processor'),
) {
const ivpkM = await keyStore.getMasterIncomingViewingPublicKey(account.address);
const ovpkM = await keyStore.getMasterOutgoingViewingPublicKey(account.address);

return new NoteProcessor(account, ivpkM, ovpkM, keyStore, db, node, startingBlock, simulator, log);
return new NoteProcessor(account, keyStore, db, node, startingBlock, simulator, log);
}

/**
Expand All @@ -101,7 +93,7 @@ export class NoteProcessor {
}

private getSyncedToBlock(): number {
return this.db.getSynchedBlockNumberForPublicKey(this.ivpkM) ?? this.startingBlock - 1;
return this.db.getSynchedBlockNumberForAccount(this.account.address) ?? this.startingBlock - 1;
}

/**
Expand All @@ -120,10 +112,10 @@ export class NoteProcessor {
const deferredIncomingNotes: DeferredNoteDao[] = [];
const deferredOutgoingNotes: DeferredNoteDao[] = [];

const ivskM = await this.keyStore.getMasterSecretKey(this.ivpkM);
const ivskM = await this.keyStore.getMasterSecretKey(this.account.publicKeys.masterIncomingViewingPublicKey);
const addressSecret = computeAddressSecret(this.account.getPreaddress(), ivskM);

const ovskM = await this.keyStore.getMasterSecretKey(this.ovpkM);
const ovskM = await this.keyStore.getMasterSecretKey(this.account.publicKeys.masterOutgoingViewingPublicKey);

// Iterate over both blocks and encrypted logs.
for (const block of blocks) {
Expand Down Expand Up @@ -176,8 +168,8 @@ export class NoteProcessor {
await produceNoteDaos(
this.simulator,
this.db,
incomingNotePayload ? this.ivpkM : undefined,
outgoingNotePayload ? this.ovpkM : undefined,
incomingNotePayload ? this.account.publicKeys.masterIncomingViewingPublicKey : undefined,
outgoingNotePayload ? this.account.publicKeys.masterOutgoingViewingPublicKey : undefined,
payload!,
txEffect.txHash,
noteHashes,
Expand Down Expand Up @@ -224,7 +216,7 @@ export class NoteProcessor {
await this.processDeferredNotes(deferredIncomingNotes, deferredOutgoingNotes);

const syncedToBlock = blocks[blocks.length - 1].number;
await this.db.setSynchedBlockNumberForPublicKey(this.ivpkM, syncedToBlock);
await this.db.setSynchedBlockNumberForAccount(this.account.address, syncedToBlock);

this.log.debug(`Synched block ${syncedToBlock}`);
}
Expand Down Expand Up @@ -258,7 +250,10 @@ export class NoteProcessor {
const nullifiers: Fr[] = blocksAndNotes.flatMap(b =>
b.block.body.txEffects.flatMap(txEffect => txEffect.nullifiers),
);
const removedNotes = await this.db.removeNullifiedNotes(nullifiers, this.ivpkM);
const removedNotes = await this.db.removeNullifiedNotes(
nullifiers,
this.account.publicKeys.masterIncomingViewingPublicKey,
);
removedNotes.forEach(noteDao => {
this.log.verbose(
`Removed note for contract ${noteDao.contractAddress} at slot ${
Expand Down Expand Up @@ -317,8 +312,8 @@ export class NoteProcessor {
for (const deferredNote of deferredNoteDaos) {
const { publicKey, payload, txHash, noteHashes, dataStartIndexForTx, unencryptedLogs } = deferredNote;

const isIncoming = publicKey.equals(this.ivpkM);
const isOutgoing = publicKey.equals(this.ovpkM);
const isIncoming = publicKey.equals(this.account.publicKeys.masterIncomingViewingPublicKey);
const isOutgoing = publicKey.equals(this.account.publicKeys.masterOutgoingViewingPublicKey);

if (!isIncoming && !isOutgoing) {
// The note does not belong to this note processor
Expand All @@ -328,8 +323,8 @@ export class NoteProcessor {
const { incomingNote, outgoingNote } = await produceNoteDaos(
this.simulator,
this.db,
isIncoming ? this.ivpkM : undefined,
isOutgoing ? this.ovpkM : undefined,
isIncoming ? this.account.publicKeys.masterIncomingViewingPublicKey : undefined,
isOutgoing ? this.account.publicKeys.masterOutgoingViewingPublicKey : undefined,
payload,
txHash,
noteHashes,
Expand Down
4 changes: 2 additions & 2 deletions yarn-project/pxe/src/pxe_service/pxe_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ export class PXEService implements PXE {
}

count++;
await this.synchronizer.addAccount(address, this.keyStore, this.config.l2StartingBlock);
this.synchronizer.addAccount(address, this.keyStore, this.config.l2StartingBlock);
}

if (count > 0) {
Expand Down Expand Up @@ -195,7 +195,7 @@ export class PXEService implements PXE {
this.log.info(`Account:\n "${accountCompleteAddress.address.toString()}"\n already registered.`);
return accountCompleteAddress;
} else {
await this.synchronizer.addAccount(accountCompleteAddress, this.keyStore, this.config.l2StartingBlock);
this.synchronizer.addAccount(accountCompleteAddress, this.keyStore, this.config.l2StartingBlock);
this.log.info(`Registered account ${accountCompleteAddress.address.toString()}`);
this.log.debug(`Registered account\n ${accountCompleteAddress.toReadableString()}`);
}
Expand Down
2 changes: 1 addition & 1 deletion yarn-project/pxe/src/synchronizer/synchronizer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ describe('Synchronizer', () => {
const partialAddress = Fr.random();
const completeAddress = await keyStore.addAccount(secretKey, partialAddress);
await database.addCompleteAddress(completeAddress);
await synchronizer.addAccount(completeAddress, keyStore, startingBlockNum);
synchronizer.addAccount(completeAddress, keyStore, startingBlockNum);
return completeAddress;
};

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 @@ -249,14 +249,14 @@ export class Synchronizer {
* @param startingBlock - The block where to start scanning for notes for this accounts.
* @returns A promise that resolves once the account is added to the Synchronizer.
*/
public async addAccount(account: CompleteAddress, keyStore: KeyStore, startingBlock: number) {
public addAccount(account: CompleteAddress, keyStore: KeyStore, startingBlock: number) {
const predicate = (x: NoteProcessor) => x.account.equals(account);
const processor = this.noteProcessors.find(predicate) ?? this.noteProcessorsToCatchUp.find(predicate);
if (processor) {
return;
}

this.noteProcessorsToCatchUp.push(await NoteProcessor.create(account, keyStore, this.db, this.node, startingBlock));
this.noteProcessorsToCatchUp.push(NoteProcessor.create(account, keyStore, this.db, this.node, startingBlock));
}

/**
Expand Down

0 comments on commit 6670315

Please sign in to comment.