Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
sklppy88 committed Oct 11, 2024
1 parent 322d621 commit 31098a8
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 34 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
18 changes: 12 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,12 @@
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 AztecAddress,
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 +53,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 +67,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 +231,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
10 changes: 7 additions & 3 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 Expand Up @@ -871,7 +871,11 @@ export class PXEService implements PXE {
}

public async isAccountStateSynchronized(account: AztecAddress) {
return await this.synchronizer.isAccountStateSynchronized(account);
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.`);
}
return await this.synchronizer.isAccountStateSynchronized(completeAddress);
}

public getSyncStatus() {
Expand Down
14 changes: 7 additions & 7 deletions 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 All @@ -132,15 +132,15 @@ describe('Synchronizer', () => {

await synchronizer.workNoteProcessorCatchUp();

expect(await synchronizer.isAccountStateSynchronized(completeAddressA.address)).toBe(false);
expect(await synchronizer.isAccountStateSynchronized(completeAddressB.address)).toBe(false);
expect(await synchronizer.isAccountStateSynchronized(completeAddressC.address)).toBe(false);
expect(await synchronizer.isAccountStateSynchronized(completeAddressA)).toBe(false);
expect(await synchronizer.isAccountStateSynchronized(completeAddressB)).toBe(false);
expect(await synchronizer.isAccountStateSynchronized(completeAddressC)).toBe(false);

await synchronizer.workNoteProcessorCatchUp();

expect(await synchronizer.isAccountStateSynchronized(completeAddressA.address)).toBe(true);
expect(await synchronizer.isAccountStateSynchronized(completeAddressB.address)).toBe(true);
expect(await synchronizer.isAccountStateSynchronized(completeAddressC.address)).toBe(true);
expect(await synchronizer.isAccountStateSynchronized(completeAddressA)).toBe(true);
expect(await synchronizer.isAccountStateSynchronized(completeAddressB)).toBe(true);
expect(await synchronizer.isAccountStateSynchronized(completeAddressC)).toBe(true);
});
});

Expand Down
14 changes: 5 additions & 9 deletions yarn-project/pxe/src/synchronizer/synchronizer.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
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, 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 +243,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 @@ -261,12 +261,8 @@ export class Synchronizer {
* retrieved information from contracts might be old/stale (e.g. old token balance).
* @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);
public async isAccountStateSynchronized(account: CompleteAddress) {
const findByAccountAddress = (x: NoteProcessor) => x.account.equals(account);
const processor =
this.noteProcessors.find(findByAccountAddress) ?? this.noteProcessorsToCatchUp.find(findByAccountAddress);
if (!processor) {
Expand Down Expand Up @@ -341,7 +337,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

0 comments on commit 31098a8

Please sign in to comment.