Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
sklppy88 committed Aug 1, 2024
1 parent f9679a8 commit 63c45ea
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 15 deletions.
21 changes: 12 additions & 9 deletions yarn-project/pxe/src/pxe_service/pxe_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -503,9 +503,9 @@ export class PXEService implements PXE {
return await this.node.getBlock(blockNumber);
}

public proveTx(txRequest: TxExecutionRequest, simulatePublic: boolean): Promise<Tx> {
public proveTx(txRequest: TxExecutionRequest, simulatePublic: boolean, scopes?: AztecAddress[]): Promise<Tx> {
return this.jobQueue.put(async () => {
const simulatedTx = await this.#simulateAndProve(txRequest, this.proofCreator, undefined);
const simulatedTx = await this.#simulateAndProve(txRequest, this.proofCreator, undefined, scopes);
if (simulatePublic) {
simulatedTx.publicOutput = await this.#simulatePublicCalls(simulatedTx.tx);
}
Expand All @@ -518,9 +518,10 @@ export class PXEService implements PXE {
txRequest: TxExecutionRequest,
simulatePublic: boolean,
msgSender: AztecAddress | undefined = undefined,
scopes?: AztecAddress[]
): Promise<SimulatedTx> {
return await this.jobQueue.put(async () => {
const simulatedTx = await this.#simulateAndProve(txRequest, this.fakeProofCreator, msgSender);
const simulatedTx = await this.#simulateAndProve(txRequest, this.fakeProofCreator, msgSender, scopes);
if (simulatePublic) {
simulatedTx.publicOutput = await this.#simulatePublicCalls(simulatedTx.tx);
}
Expand Down Expand Up @@ -551,12 +552,13 @@ export class PXEService implements PXE {
args: any[],
to: AztecAddress,
_from?: AztecAddress,
scopes?: AztecAddress[]
): Promise<DecodedReturn> {
// all simulations must be serialized w.r.t. the synchronizer
return await this.jobQueue.put(async () => {
// TODO - Should check if `from` has the permission to call the view function.
const functionCall = await this.#getFunctionCall(functionName, args, to);
const executionResult = await this.#simulateUnconstrained(functionCall);
const executionResult = await this.#simulateUnconstrained(functionCall, scopes);

// TODO - Return typed result based on the function artifact.
return executionResult;
Expand Down Expand Up @@ -668,14 +670,14 @@ export class PXEService implements PXE {
};
}

async #simulate(txRequest: TxExecutionRequest, msgSender?: AztecAddress): Promise<ExecutionResult> {
async #simulate(txRequest: TxExecutionRequest, msgSender?: AztecAddress, scopes?: AztecAddress[]): Promise<ExecutionResult> {
// TODO - Pause syncing while simulating.

const { contractAddress, functionArtifact } = await this.#getSimulationParameters(txRequest);

this.log.debug('Executing simulator...');
try {
const result = await this.simulator.run(txRequest, functionArtifact, contractAddress, msgSender);
const result = await this.simulator.run(txRequest, functionArtifact, contractAddress, msgSender, scopes);
this.log.verbose(`Simulation completed for ${contractAddress.toString()}:${functionArtifact.name}`);
return result;
} catch (err) {
Expand All @@ -694,12 +696,12 @@ export class PXEService implements PXE {
* @param execRequest - The transaction request object containing the target contract and function data.
* @returns The simulation result containing the outputs of the unconstrained function.
*/
async #simulateUnconstrained(execRequest: FunctionCall) {
async #simulateUnconstrained(execRequest: FunctionCall, scopes?: AztecAddress[]) {
const { contractAddress, functionArtifact } = await this.#getSimulationParameters(execRequest);

this.log.debug('Executing unconstrained simulator...');
try {
const result = await this.simulator.runUnconstrained(execRequest, functionArtifact, contractAddress);
const result = await this.simulator.runUnconstrained(execRequest, functionArtifact, contractAddress, scopes);
this.log.verbose(`Unconstrained simulation for ${contractAddress}.${functionArtifact.name} completed`);

return result;
Expand Down Expand Up @@ -765,9 +767,10 @@ export class PXEService implements PXE {
txExecutionRequest: TxExecutionRequest,
proofCreator: PrivateKernelProver,
msgSender?: AztecAddress,
scopes?: AztecAddress[],
): Promise<SimulatedTx> {
// Get values that allow us to reconstruct the block hash
const executionResult = await this.#simulate(txExecutionRequest, msgSender);
const executionResult = await this.#simulate(txExecutionRequest, msgSender, scopes);

const kernelOracle = new KernelOracle(this.contractDataOracle, this.keyStore, this.node);
const kernelProver = new KernelProver(kernelOracle, proofCreator);
Expand Down
3 changes: 2 additions & 1 deletion yarn-project/pxe/src/simulator_oracle/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,12 @@ export class SimulatorOracle implements DBOracle {
return capsule;
}

async getNotes(contractAddress: AztecAddress, storageSlot: Fr, status: NoteStatus) {
async getNotes(contractAddress: AztecAddress, storageSlot: Fr, status: NoteStatus, scopes?: AztecAddress[]) {
const noteDaos = await this.db.getIncomingNotes({
contractAddress,
storageSlot,
status,
accounts: scopes
});
return noteDaos.map(({ contractAddress, storageSlot, nonce, note, slottedNoteHash, siloedNullifier, index }) => ({
contractAddress,
Expand Down
5 changes: 3 additions & 2 deletions yarn-project/simulator/src/client/client_execution_context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,9 @@ export class ClientExecutionContext extends ViewDataOracle {
private node: AztecNode,
protected sideEffectCounter: number = 0,
log = createDebugLogger('aztec:simulator:client_execution_context'),
scopes?: AztecAddress[],
) {
super(contractAddress, authWitnesses, db, node, log);
super(contractAddress, authWitnesses, db, node, log, scopes);
}

// We still need this function until we can get user-defined ordering of structs for fn arguments
Expand Down Expand Up @@ -251,7 +252,7 @@ export class ClientExecutionContext extends ViewDataOracle {
const pendingNotes = this.noteCache.getNotes(this.callContext.storageContractAddress, storageSlot);

const pendingNullifiers = this.noteCache.getNullifiers(this.callContext.storageContractAddress);
const dbNotes = await this.db.getNotes(this.callContext.storageContractAddress, storageSlot, status);
const dbNotes = await this.db.getNotes(this.callContext.storageContractAddress, storageSlot, status, this.scopes);
const dbNotesFiltered = dbNotes.filter(n => !pendingNullifiers.has((n.siloedNullifier as Fr).value));

const notes = pickNotes<NoteData>([...dbNotesFiltered, ...pendingNotes], {
Expand Down
2 changes: 1 addition & 1 deletion yarn-project/simulator/src/client/db_oracle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ export interface DBOracle extends CommitmentsDB {
* @param status - The status of notes to fetch.
* @returns A Promise that resolves to an array of note data.
*/
getNotes(contractAddress: AztecAddress, storageSlot: Fr, status: NoteStatus): Promise<NoteData[]>;
getNotes(contractAddress: AztecAddress, storageSlot: Fr, status: NoteStatus, scopes?: AztecAddress[]): Promise<NoteData[]>;

/**
* Retrieve the artifact information of a specific function within a contract.
Expand Down
6 changes: 5 additions & 1 deletion yarn-project/simulator/src/client/simulator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export class AcirSimulator {
entryPointArtifact: FunctionArtifact,
contractAddress: AztecAddress,
msgSender = AztecAddress.fromField(Fr.MAX_FIELD_VALUE),
scopes?: AztecAddress[],
): Promise<ExecutionResult> {
if (entryPointArtifact.functionType !== FunctionType.PRIVATE) {
throw new Error(`Cannot run ${entryPointArtifact.functionType} function as private`);
Expand Down Expand Up @@ -80,6 +81,8 @@ export class AcirSimulator {
this.db,
this.node,
startSideEffectCounter,
undefined,
scopes
);

try {
Expand All @@ -106,12 +109,13 @@ export class AcirSimulator {
request: FunctionCall,
entryPointArtifact: FunctionArtifact,
contractAddress: AztecAddress,
scopes?: AztecAddress[]
) {
if (entryPointArtifact.functionType !== FunctionType.UNCONSTRAINED) {
throw new Error(`Cannot run ${entryPointArtifact.functionType} function as unconstrained`);
}

const context = new ViewDataOracle(contractAddress, [], this.db, this.node);
const context = new ViewDataOracle(contractAddress, [], this.db, this.node, undefined, scopes);

try {
return await executeUnconstrainedFunction(
Expand Down
3 changes: 2 additions & 1 deletion yarn-project/simulator/src/client/view_data_oracle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export class ViewDataOracle extends TypedOracle {
protected readonly db: DBOracle,
protected readonly aztecNode: AztecNode,
protected log = createDebugLogger('aztec:simulator:client_view_context'),
protected readonly scopes?: AztecAddress[]
) {
super();
}
Expand Down Expand Up @@ -219,7 +220,7 @@ export class ViewDataOracle extends TypedOracle {
offset: number,
status: NoteStatus,
): Promise<NoteData[]> {
const dbNotes = await this.db.getNotes(this.contractAddress, storageSlot, status);
const dbNotes = await this.db.getNotes(this.contractAddress, storageSlot, status, this.scopes);
return pickNotes<NoteData>(dbNotes, {
selects: selectByIndexes.slice(0, numSelects).map((index, i) => ({
selector: { index, offset: selectByOffsets[i], length: selectByLengths[i] },
Expand Down

0 comments on commit 63c45ea

Please sign in to comment.