Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
sklppy88 committed Nov 20, 2024
1 parent 2587ad5 commit 5435af5
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 35 deletions.
22 changes: 6 additions & 16 deletions yarn-project/pxe/src/database/kv_pxe_database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -669,26 +669,16 @@ export class KVPxeDatabase implements PxeDatabase {
return incomingNotesSize + outgoingNotesSize + treeRootsSize + authWitsSize + addressesSize;
}

async incrementTaggingSecretsIndexesAsSender(appTaggingSecrets: Fr[]): Promise<void> {
await this.#incrementTaggingSecretsIndexes(appTaggingSecrets, this.#taggingSecretIndexesForSenders);
async setTaggingSecretsIndexesAsSender(indexedSecrets: IndexedTaggingSecret[]): Promise<void> {
await this.#setTaggingSecretsIndexes(indexedSecrets, this.#taggingSecretIndexesForSenders);
}

async #incrementTaggingSecretsIndexes(appTaggingSecrets: Fr[], storageMap: AztecMap<string, number>): Promise<void> {
const indexes = await this.#getTaggingSecretsIndexes(appTaggingSecrets, storageMap);
await this.db.transaction(() => {
indexes.forEach((taggingSecretIndex, listIndex) => {
const nextIndex = taggingSecretIndex + 1;
void storageMap.set(appTaggingSecrets[listIndex].toString(), nextIndex);
});
});
async setTaggingSecretsIndexesAsRecipient(indexedSecrets: IndexedTaggingSecret[]): Promise<void> {
await this.#setTaggingSecretsIndexes(indexedSecrets, this.#taggingSecretIndexesForRecipients);
}

async setTaggingSecretsIndexesAsRecipient(indexedSecrets: IndexedTaggingSecret[]): Promise<void> {
await this.db.transaction(() => {
indexedSecrets.forEach(indexedSecret => {
void this.#taggingSecretIndexesForRecipients.set(indexedSecret.secret.toString(), indexedSecret.index);
});
});
#setTaggingSecretsIndexes(indexedSecrets: IndexedTaggingSecret[], storageMap: AztecMap<string, number>): Promise<Promise<void>[]> {
return this.db.transaction(() => indexedSecrets.map(indexedSecret => storageMap.set(indexedSecret.secret.toString(), indexedSecret.index)));
}

async getTaggingSecretsIndexesAsRecipient(appTaggingSecrets: Fr[]) {
Expand Down
7 changes: 1 addition & 6 deletions yarn-project/pxe/src/database/pxe_database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,12 +201,7 @@ export interface PxeDatabase extends ContractArtifactDatabase, ContractInstanceD
*/
getTaggingSecretsIndexesAsSender(appTaggingSecrets: Fr[]): Promise<number[]>;

/**
* Increments the index for the provided app siloed tagging secrets in the senders database
* To be used when the generated tags have been used as sender
* @param appTaggingSecrets - The app siloed tagging secrets.
*/
incrementTaggingSecretsIndexesAsSender(appTaggingSecrets: Fr[]): Promise<void>;
setTaggingSecretsIndexesAsSender(indexedTaggingSecrets: IndexedTaggingSecret[]): Promise<void>;

/**
* Sets the index for the provided app siloed tagging secrets
Expand Down
84 changes: 71 additions & 13 deletions yarn-project/pxe/src/simulator_oracle/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -269,8 +269,11 @@ export class SimulatorOracle implements DBOracle {
sender: AztecAddress,
recipient: AztecAddress,
): Promise<IndexedTaggingSecret> {
await this.syncTaggedLogsAsSender(contractAddress, sender, recipient);

const secret = await this.#calculateTaggingSecret(contractAddress, sender, recipient);
const [index] = await this.db.getTaggingSecretsIndexesAsSender([secret]);

return new IndexedTaggingSecret(secret, index);
}

Expand All @@ -290,7 +293,9 @@ export class SimulatorOracle implements DBOracle {
this.log.verbose(
`Incrementing secret ${secret} as sender ${sender} for recipient: ${recipient} at contract: ${contractName}(${contractAddress})`,
);
await this.db.incrementTaggingSecretsIndexesAsSender([secret]);

const [index] = await this.db.getTaggingSecretsIndexesAsSender([secret]);
await this.db.setTaggingSecretsIndexesAsSender([new IndexedTaggingSecret(secret, index)]);
}

async #calculateTaggingSecret(contractAddress: AztecAddress, sender: AztecAddress, recipient: AztecAddress) {
Expand Down Expand Up @@ -330,6 +335,21 @@ export class SimulatorOracle implements DBOracle {
return appTaggingSecrets.map((secret, i) => new IndexedTaggingSecret(secret, indexes[i]));
}

async #getAppTaggingSecretAsSender(
contractAddress: AztecAddress,
sender: AztecAddress,
recipient: AztecAddress,
): Promise<IndexedTaggingSecret> {
const senderCompleteAddress = await this.getCompleteAddress(sender);
const senderIvsk = await this.keyStore.getMasterIncomingViewingSecretKey(sender);

const sharedSecret = computeTaggingSecret(senderCompleteAddress, senderIvsk, recipient);
const appTaggingSecret = poseidon2Hash([sharedSecret.x, sharedSecret.y, contractAddress]);
const [index] = await this.db.getTaggingSecretsIndexesAsSender([appTaggingSecret]);

return new IndexedTaggingSecret(appTaggingSecret, index);
}

/**
* Synchronizes the logs tagged with scoped addresses and all the senders in the addressbook.
* Returns the unsynched logs and updates the indexes of the secrets used to tag them until there are no more logs to sync.
Expand All @@ -343,6 +363,34 @@ export class SimulatorOracle implements DBOracle {
scopes?: AztecAddress[],
): Promise<Map<string, TxScopedL2Log[]>> {
const recipients = scopes ? scopes : await this.keyStore.getAccounts();

const result = await this.#syncTaggedLogs(contractAddress, recipients);

for (const [key, value] of result) {
result.set(
key,
value.filter(log => log.blockNumber <= maxBlockNumber),
);
}

return result;
}

public async syncTaggedLogsAsSender(
contractAddress: AztecAddress,
sender: AztecAddress,
recipient: AztecAddress,
): Promise<Map<string, TxScopedL2Log[]>> {
const result = await this.#syncTaggedLogs(contractAddress, [recipient], sender);

return result;
}

async #syncTaggedLogs(
contractAddress: AztecAddress,
recipients: AztecAddress[],
asSender?: AztecAddress,
): Promise<Map<string, TxScopedL2Log[]>> {
const result = new Map<string, TxScopedL2Log[]>();
const contractName = await this.contractDataOracle.getDebugContractName(contractAddress);
for (const recipient of recipients) {
Expand All @@ -354,7 +402,12 @@ export class SimulatorOracle implements DBOracle {
// length, since we don't really know the note they correspond to until we decrypt them.

// 1. Get all the secrets for the recipient and sender pairs (#9365)
const appTaggingSecrets = await this.#getAppTaggingSecretsForContacts(contractAddress, recipient);
const appTaggingSecrets: IndexedTaggingSecret[] = [];
if (asSender === undefined) {
appTaggingSecrets.push(...(await this.#getAppTaggingSecretsForContacts(contractAddress, recipient)));
} else {
appTaggingSecrets.push(await this.#getAppTaggingSecretAsSender(contractAddress, asSender, recipient));
}

// 1.1 Set up a sliding window with an offset. Chances are the sender might have messed up
// and inadvertedly incremented their index without use getting any logs (for example, in case
Expand Down Expand Up @@ -432,22 +485,27 @@ export class SimulatorOracle implements DBOracle {
newTaggingSecrets.push(newTaggingSecret);
}
});
await this.db.setTaggingSecretsIndexesAsRecipient(
Object.keys(secretsToIncrement).map(
secret => new IndexedTaggingSecret(Fr.fromString(secret), secretsToIncrement[secret]),
),
);
if (asSender === undefined) {
await this.db.setTaggingSecretsIndexesAsRecipient(
Object.keys(secretsToIncrement).map(
secret => new IndexedTaggingSecret(Fr.fromString(secret), secretsToIncrement[secret]),
),
);
} else {
await this.db.setTaggingSecretsIndexesAsSender(
Object.keys(secretsToIncrement).map(
secret => new IndexedTaggingSecret(Fr.fromString(secret), secretsToIncrement[secret]),
),
)
}

currentTagggingSecrets = newTaggingSecrets;
}

result.set(
recipient.toString(),
// Remove logs with a block number higher than the max block number
// Duplicates are likely to happen due to the sliding window, so we also filter them out
logs.filter(
(log, index, self) =>
log.blockNumber <= maxBlockNumber && index === self.findIndex(otherLog => otherLog.equals(log)),
),
// Duplicates are likely to happen due to the sliding window, so we filter them out
logs.filter((log, index, self) => index === self.findIndex(otherLog => otherLog.equals(log))),
);
}
return result;
Expand Down

0 comments on commit 5435af5

Please sign in to comment.