Skip to content

Commit cb1c968

Browse files
committed
refactor: tagging related cleanup
I started working on improving unconstrained tagging as proposed in [this forum post](https://forum.aztec.network/t/on-note-discovery-and-index-coordination/7165) and the initial step in implementing that is the following: 1. store the tagging indexes requested during execution in a cache (just like we do with notes), 2. when the execution is finished take the cache and along with a tx hash dump it into db. Given that the cache will reside in `PrivateExecutionOracle` I now need to orchestrate the sync there. When trying to do that (to be done in a [PR up the stack](#17445)) I realized that the current tagging-related API was cumbersome and this made me do the following in this PR: 1. Have the API accept directionalAppTaggingSecret on the input instead of appTaggingSecret and recipient as now I can just compute it in the `PrivateExecutionOracle` and request info based on that, 2. I moved the tagging functionality from stdlib to PXE as there was no reason to have it in stdlib, 3. I simplified a bunch of random things. This PR is a bit random so if something is not clear LMK.
1 parent bac57ba commit cb1c968

File tree

19 files changed

+449
-299
lines changed

19 files changed

+449
-299
lines changed

yarn-project/pxe/src/contract_function_simulator/execution_data_provider.ts

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@ import type { AztecAddress } from '@aztec/stdlib/aztec-address';
55
import type { L2Block } from '@aztec/stdlib/block';
66
import type { CompleteAddress, ContractInstance } from '@aztec/stdlib/contract';
77
import type { KeyValidationRequest } from '@aztec/stdlib/kernel';
8+
import type { DirectionalAppTaggingSecret } from '@aztec/stdlib/logs';
89
import type { NoteStatus } from '@aztec/stdlib/note';
910
import { type MerkleTreeId, type NullifierMembershipWitness, PublicDataWitness } from '@aztec/stdlib/trees';
1011
import type { BlockHeader, NodeStats } from '@aztec/stdlib/tx';
1112

13+
import type { Tag } from '../tagging/tag.js';
1214
import type { NoteData } from './oracle/interfaces.js';
1315
import type { MessageLoadOracleInputs } from './oracle/message_load_oracle_inputs.js';
1416

@@ -214,13 +216,36 @@ export interface ExecutionDataProvider {
214216
assertCompatibleOracleVersion(version: number): void;
215217

216218
/**
217-
* Returns the next app tag for a given sender and recipient pair.
219+
* Calculates the directional app tagging secret for a given contract, sender and recipient.
218220
* @param contractAddress - The contract address to silo the secret for
219221
* @param sender - The address sending the note
220222
* @param recipient - The address receiving the note
223+
* @returns The directional app tagging secret
224+
*/
225+
calculateDirectionalAppTaggingSecret(
226+
contractAddress: AztecAddress,
227+
sender: AztecAddress,
228+
recipient: AztecAddress,
229+
): Promise<DirectionalAppTaggingSecret>;
230+
231+
/**
232+
* Updates the local index of the shared tagging secret of a (sender, recipient, contract) tuple if a log with
233+
* a larger index is found from the node.
234+
* @param secret - The secret that's unique for (sender, recipient, contract) tuple while the direction
235+
* of sender -> recipient matters.
236+
* @param contractAddress - The address of the contract that the logs are tagged for. Needs to be provided to store
237+
* because the function performs second round of siloing which is necessary because kernels do it as well (they silo
238+
* first field of the private log which corresponds to the tag).
239+
*/
240+
syncTaggedLogsAsSender(secret: DirectionalAppTaggingSecret, contractAddress: AztecAddress): Promise<void>;
241+
242+
/**
243+
* Returns the next app tag for a given directional app tagging secret.
244+
* @param secret - The secret that's unique for (sender, recipient, contract) tuple while
245+
* direction of sender -> recipient matters.
221246
* @returns The computed tag.
222247
*/
223-
getNextAppTagAsSender(contractAddress: AztecAddress, sender: AztecAddress, recipient: AztecAddress): Promise<Fr>;
248+
getNextAppTagAsSender(secret: DirectionalAppTaggingSecret): Promise<Tag>;
224249

225250
/**
226251
* Synchronizes the private logs tagged with scoped addresses and all the senders in the address book. Stores the found

yarn-project/pxe/src/contract_function_simulator/oracle/interfaces.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import type { Note, NoteStatus } from '@aztec/stdlib/note';
99
import { type MerkleTreeId, type NullifierMembershipWitness, PublicDataWitness } from '@aztec/stdlib/trees';
1010
import type { BlockHeader } from '@aztec/stdlib/tx';
1111

12+
import type { Tag } from '../../tagging/tag.js';
1213
import type { UtilityContext } from '../noir-structs/utility_context.js';
1314
import type { MessageLoadOracleInputs } from './message_load_oracle_inputs.js';
1415

@@ -154,6 +155,6 @@ export interface IPrivateExecutionOracle {
154155
privateNotifySetMinRevertibleSideEffectCounter(minRevertibleSideEffectCounter: number): Promise<void>;
155156
privateGetSenderForTags(): Promise<AztecAddress | undefined>;
156157
privateSetSenderForTags(senderForTags: AztecAddress): Promise<void>;
157-
privateGetNextAppTagAsSender(sender: AztecAddress, recipient: AztecAddress): Promise<Fr>;
158+
privateGetNextAppTagAsSender(sender: AztecAddress, recipient: AztecAddress): Promise<Tag>;
158159
utilityEmitOffchainEffect(data: Fr[]): Promise<void>;
159160
}

yarn-project/pxe/src/contract_function_simulator/oracle/oracle.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,7 @@ export class Oracle {
438438
AztecAddress.fromString(sender),
439439
AztecAddress.fromString(recipient),
440440
);
441-
return [toACVMField(tag)];
441+
return [toACVMField(tag.value)];
442442
}
443443

444444
async utilityFetchTaggedLogs([pendingTaggedLogArrayBaseSlot]: ACVMField[]): Promise<ACVMField[]> {

yarn-project/pxe/src/contract_function_simulator/oracle/private_execution.test.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ import {
4747
} from '@aztec/stdlib/hash';
4848
import { KeyValidationRequest } from '@aztec/stdlib/kernel';
4949
import { computeAppNullifierSecretKey, deriveKeys } from '@aztec/stdlib/keys';
50+
import { DirectionalAppTaggingSecret } from '@aztec/stdlib/logs';
5051
import { L1Actor, L1ToL2Message, L2Actor } from '@aztec/stdlib/messaging';
5152
import { Note } from '@aztec/stdlib/note';
5253
import { makeHeader } from '@aztec/stdlib/testing';
@@ -64,6 +65,7 @@ import { jest } from '@jest/globals';
6465
import { Matcher, type MatcherCreator, type MockProxy, mock } from 'jest-mock-extended';
6566
import { toFunctionSelector } from 'viem';
6667

68+
import { Tag } from '../../tagging/tag.js';
6769
import { ContractFunctionSimulator } from '../contract_function_simulator.js';
6870
import type { ExecutionDataProvider } from '../execution_data_provider.js';
6971
import { MessageLoadOracleInputs } from './message_load_oracle_inputs.js';
@@ -301,11 +303,9 @@ describe('Private Execution test suite', () => {
301303
throw new Error(`Unknown address: ${address}. Recipient: ${recipient}, Owner: ${owner}`);
302304
});
303305

304-
executionDataProvider.getNextAppTagAsSender.mockImplementation(
305-
(_contractAddress: AztecAddress, _sender: AztecAddress, _recipient: AztecAddress) => {
306-
return Promise.resolve(Fr.random());
307-
},
308-
);
306+
executionDataProvider.getNextAppTagAsSender.mockImplementation((_secret: DirectionalAppTaggingSecret) => {
307+
return Promise.resolve(Tag.compute({ secret: _secret, index: 0 }));
308+
});
309309
executionDataProvider.getFunctionArtifact.mockImplementation(async (address, selector) => {
310310
const contract = contracts[address.toString()];
311311
if (!contract) {
@@ -331,6 +331,9 @@ describe('Private Execution test suite', () => {
331331
});
332332

333333
executionDataProvider.syncTaggedLogs.mockImplementation((_, __) => Promise.resolve());
334+
executionDataProvider.calculateDirectionalAppTaggingSecret.mockResolvedValue(
335+
DirectionalAppTaggingSecret.fromString('0x1'),
336+
);
334337
executionDataProvider.loadCapsule.mockImplementation((_, __) => Promise.resolve(null));
335338

336339
executionDataProvider.getPublicStorageAt.mockImplementation(

yarn-project/pxe/src/contract_function_simulator/oracle/private_execution_oracle.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import {
2626
type TxContext,
2727
} from '@aztec/stdlib/tx';
2828

29+
import type { Tag } from '../../tagging/tag.js';
2930
import type { ExecutionDataProvider } from '../execution_data_provider.js';
3031
import type { ExecutionNoteCache } from '../execution_note_cache.js';
3132
import type { HashedValuesCache } from '../hashed_values_cache.js';
@@ -191,8 +192,22 @@ export class PrivateExecutionOracle extends UtilityExecutionOracle implements IP
191192
* @param recipient - The address receiving the log
192193
* @returns An app tag to be used in a log.
193194
*/
194-
public async privateGetNextAppTagAsSender(sender: AztecAddress, recipient: AztecAddress): Promise<Fr> {
195-
return await this.executionDataProvider.getNextAppTagAsSender(this.contractAddress, sender, recipient);
195+
public async privateGetNextAppTagAsSender(sender: AztecAddress, recipient: AztecAddress): Promise<Tag> {
196+
const directionalAppTaggingSecret = await this.executionDataProvider.calculateDirectionalAppTaggingSecret(
197+
this.contractAddress,
198+
sender,
199+
recipient,
200+
);
201+
202+
// TODO(benesjan): In a follow-up PR we will load here the index from the ExecutionTaggingIndexCache if present
203+
// and if not we will obtain it from the execution data provider.
204+
205+
this.log.debug(`Syncing tagged logs as sender ${sender} for contract ${this.contractAddress}`, {
206+
directionalAppTaggingSecret,
207+
recipient,
208+
});
209+
await this.executionDataProvider.syncTaggedLogsAsSender(directionalAppTaggingSecret, this.contractAddress);
210+
return this.executionDataProvider.getNextAppTagAsSender(directionalAppTaggingSecret);
196211
}
197212

198213
/**

0 commit comments

Comments
 (0)