Skip to content

Commit

Permalink
refactor: modify note processors and synchronizers to use complete ad…
Browse files Browse the repository at this point in the history
…dress (#9152)

I am modifying the note processors and synchronizers to use a complete
address, instead of purely using the aztecaddress. This is because in
the new address scheme, to decrypt, we will require the preaddress (a
future member of the complete address), as well as the ivsk to calculate
the "secret" of the address point. This is just scaffolding to be able
to make the decrypting changes in the future.

The note processors specifically need this, but the synchronizer
previously did not have it, but it was fetching the complete address
anyways from the pxe db.
  • Loading branch information
sklppy88 authored Oct 14, 2024
1 parent 1c7e627 commit 730d90f
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 29 deletions.
11 changes: 2 additions & 9 deletions yarn-project/pxe/src/note_processor/note_processor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,14 +162,7 @@ describe('Note Processor', () => {
keyStore.getMasterIncomingViewingPublicKey.mockResolvedValue(account.publicKeys.masterIncomingViewingPublicKey);
keyStore.getMasterOutgoingViewingPublicKey.mockResolvedValue(account.publicKeys.masterOutgoingViewingPublicKey);

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

simulator.computeNoteHashAndOptionallyANullifier.mockImplementation((...args) =>
Promise.resolve({
Expand Down Expand Up @@ -342,7 +335,7 @@ describe('Note Processor', () => {
await noteProcessor.process(blocks);

const newNoteProcessor = await NoteProcessor.create(
account.address,
account,
keyStore,
database,
aztecNode,
Expand Down
12 changes: 6 additions & 6 deletions yarn-project/pxe/src/note_processor/note_processor.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { type AztecNode, L1NotePayload, type L2Block } from '@aztec/circuit-types';
import { type NoteProcessorStats } from '@aztec/circuit-types/stats';
import { type AztecAddress, INITIAL_L2_BLOCK_NUM, MAX_NOTE_HASHES_PER_TX, type PublicKey } from '@aztec/circuits.js';
import { type CompleteAddress, INITIAL_L2_BLOCK_NUM, MAX_NOTE_HASHES_PER_TX, type PublicKey } from '@aztec/circuits.js';
import { type Fr } from '@aztec/foundation/fields';
import { type Logger, createDebugLogger } from '@aztec/foundation/log';
import { Timer } from '@aztec/foundation/timer';
Expand Down Expand Up @@ -47,7 +47,7 @@ export class NoteProcessor {
};

private constructor(
public readonly account: AztecAddress,
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. */
Expand All @@ -61,16 +61,16 @@ export class NoteProcessor {
) {}

public static async create(
account: AztecAddress,
account: CompleteAddress,
keyStore: KeyStore,
db: PxeDatabase,
node: AztecNode,
startingBlock: number = INITIAL_L2_BLOCK_NUM,
simulator = getAcirSimulator(db, node, keyStore),
log = createDebugLogger('aztec:note_processor'),
) {
const ivpkM = await keyStore.getMasterIncomingViewingPublicKey(account);
const ovpkM = await keyStore.getMasterOutgoingViewingPublicKey(account);
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);
}
Expand Down Expand Up @@ -225,7 +225,7 @@ export class NoteProcessor {
const incomingNotes = blocksAndNotes.flatMap(b => b.incomingNotes);
const outgoingNotes = blocksAndNotes.flatMap(b => b.outgoingNotes);
if (incomingNotes.length || outgoingNotes.length) {
await this.db.addNotes(incomingNotes, outgoingNotes, this.account);
await this.db.addNotes(incomingNotes, outgoingNotes, this.account.address);
incomingNotes.forEach(noteDao => {
this.log.verbose(
`Added incoming note for contract ${noteDao.contractAddress} at slot ${
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.address, this.keyStore, this.config.l2StartingBlock);
await 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.address, this.keyStore, this.config.l2StartingBlock);
await 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.address, keyStore, startingBlockNum);
await synchronizer.addAccount(completeAddress, keyStore, startingBlockNum);
return completeAddress;
};

Expand Down
22 changes: 12 additions & 10 deletions yarn-project/pxe/src/synchronizer/synchronizer.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import { type AztecNode, type L2Block, MerkleTreeId, type TxHash } from '@aztec/circuit-types';
import { type NoteProcessorCaughtUpStats } from '@aztec/circuit-types/stats';
import { type AztecAddress, type Fr, INITIAL_L2_BLOCK_NUM, type PublicKey } from '@aztec/circuits.js';
import {
type AztecAddress,
type CompleteAddress,
type Fr,
INITIAL_L2_BLOCK_NUM,
type PublicKey,
} from '@aztec/circuits.js';
import { type DebugLogger, createDebugLogger } from '@aztec/foundation/log';
import { type SerialQueue } from '@aztec/foundation/queue';
import { RunningPromise } from '@aztec/foundation/running-promise';
Expand Down Expand Up @@ -243,7 +249,7 @@ 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: AztecAddress, keyStore: KeyStore, startingBlock: number) {
public async 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) {
Expand All @@ -262,11 +268,7 @@ export class Synchronizer {
* @throws If checking a sync status of account which is not registered.
*/
public async isAccountStateSynchronized(account: AztecAddress) {
const completeAddress = await this.db.getCompleteAddress(account);
if (!completeAddress) {
throw new Error(`Checking if account is synched is not possible for ${account} because it is not registered.`);
}
const findByAccountAddress = (x: NoteProcessor) => x.account.equals(completeAddress.address);
const findByAccountAddress = (x: NoteProcessor) => x.account.address.equals(account);
const processor =
this.noteProcessors.find(findByAccountAddress) ?? this.noteProcessorsToCatchUp.find(findByAccountAddress);
if (!processor) {
Expand Down Expand Up @@ -300,7 +302,7 @@ export class Synchronizer {
const lastBlockNumber = this.getSynchedBlockNumber();
return {
blocks: lastBlockNumber,
notes: Object.fromEntries(this.noteProcessors.map(n => [n.account.toString(), n.status.syncedToBlock])),
notes: Object.fromEntries(this.noteProcessors.map(n => [n.account.address.toString(), n.status.syncedToBlock])),
};
}

Expand All @@ -309,7 +311,7 @@ export class Synchronizer {
* @returns The note processor stats for notes for each public key being tracked.
*/
public getSyncStats() {
return Object.fromEntries(this.noteProcessors.map(n => [n.account.toString(), n.stats]));
return Object.fromEntries(this.noteProcessors.map(n => [n.account.address.toString(), n.stats]));
}

/**
Expand Down Expand Up @@ -341,7 +343,7 @@ export class Synchronizer {
const { incomingNotes: inNotes, outgoingNotes: outNotes } = await processor.decodeDeferredNotes(deferredNotes);
incomingNotes.push(...inNotes);

await this.db.addNotes(inNotes, outNotes, processor.account);
await this.db.addNotes(inNotes, outNotes, processor.account.address);

inNotes.forEach(noteDao => {
this.log.debug(
Expand Down
2 changes: 1 addition & 1 deletion yarn-project/world-state/package.local.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
"test": "NODE_NO_WARNINGS=1 node --experimental-vm-modules ../node_modules/.bin/jest --passWithNoTests",
"clean": "rm -rf ./dest ./build .tsbuildinfo"
}
}
}

0 comments on commit 730d90f

Please sign in to comment.