Skip to content

Commit

Permalink
Silo leaf slots in each iteration.
Browse files Browse the repository at this point in the history
  • Loading branch information
LeilaWang committed Apr 14, 2024
1 parent fe1bce6 commit 0aa7f23
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -500,29 +500,9 @@ function patchPublicStorageActionOrdering(
// so the returned result will be a subset of the public kernel output.

const simPublicDataReads = collectPublicDataReads(execResult);
// verify that each read is in the kernel output
for (const read of simPublicDataReads) {
if (!publicDataReads.find(item => item.equals(read))) {
throw new Error(
`Public data reads from simulator do not match those from public kernel.\nFrom simulator: ${simPublicDataReads
.map(p => p.toFriendlyJSON())
.join(', ')}\nFrom public kernel: ${publicDataReads.map(i => i.toFriendlyJSON()).join(', ')}`,
);
}
}

const simPublicDataUpdateRequests = collectPublicDataUpdateRequests(execResult);
for (const update of simPublicDataUpdateRequests) {
if (!publicDataUpdateRequests.find(item => item.equals(update))) {
throw new Error(
`Public data update requests from simulator do not match those from public kernel.\nFrom simulator: ${simPublicDataUpdateRequests
.map(p => p.toFriendlyJSON())
.join(', ')}\nFrom public kernel revertible: ${publicDataUpdateRequests
.map(i => i.toFriendlyJSON())
.join(', ')}`,
);
}
}

// We only want to reorder the items from the public inputs of the
// most recently processed top/enqueued call.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import {
StateReference,
makeEmptyProof,
} from '@aztec/circuits.js';
import { computePublicDataTreeLeafSlot } from '@aztec/circuits.js/hash';
import { fr, makeAztecAddress, makePublicCallRequest, makeSelector } from '@aztec/circuits.js/testing';
import { arrayNonEmptyLength, padArrayEnd } from '@aztec/foundation/collection';
import { openTmpStore } from '@aztec/kv-store/utils';
Expand Down Expand Up @@ -425,11 +424,14 @@ describe('public_processor', () => {

const txEffect = toTxEffect(processed[0]);
expect(arrayNonEmptyLength(txEffect.publicDataWrites, PublicDataWrite.isEmpty)).toEqual(2);
// TODO: Leave slots should be siloed once we do it in the tail circuit.
expect(txEffect.publicDataWrites[0]).toEqual(
new PublicDataWrite(computePublicDataTreeLeafSlot(baseContractAddress, contractSlotA), fr(0x101)),
// new PublicDataWrite(computePublicDataTreeLeafSlot(baseContractAddress, contractSlotA), fr(0x101)),
new PublicDataWrite(contractSlotA, fr(0x101)),
);
expect(txEffect.publicDataWrites[1]).toEqual(
new PublicDataWrite(computePublicDataTreeLeafSlot(baseContractAddress, contractSlotC), fr(0x201)),
// new PublicDataWrite(computePublicDataTreeLeafSlot(baseContractAddress, contractSlotC), fr(0x201)),
new PublicDataWrite(contractSlotC, fr(0x201)),
);
expect(txEffect.encryptedLogs.getTotalLogCount()).toBe(0);
expect(txEffect.unencryptedLogs.getTotalLogCount()).toBe(0);
Expand Down Expand Up @@ -711,14 +713,18 @@ describe('public_processor', () => {

const txEffect = toTxEffect(processed[0]);
expect(arrayNonEmptyLength(txEffect.publicDataWrites, PublicDataWrite.isEmpty)).toEqual(3);
// TODO: Leave slots should be siloed once we do it in the tail circuit.
expect(txEffect.publicDataWrites[0]).toEqual(
new PublicDataWrite(computePublicDataTreeLeafSlot(baseContractAddress, contractSlotC), fr(0x201)),
// new PublicDataWrite(computePublicDataTreeLeafSlot(baseContractAddress, contractSlotC), fr(0x201)),
new PublicDataWrite(contractSlotC, fr(0x201)),
);
expect(txEffect.publicDataWrites[1]).toEqual(
new PublicDataWrite(computePublicDataTreeLeafSlot(baseContractAddress, contractSlotA), fr(0x102)),
// new PublicDataWrite(computePublicDataTreeLeafSlot(baseContractAddress, contractSlotA), fr(0x102)),
new PublicDataWrite(contractSlotA, fr(0x102)),
);
expect(txEffect.publicDataWrites[2]).toEqual(
new PublicDataWrite(computePublicDataTreeLeafSlot(baseContractAddress, contractSlotB), fr(0x151)),
// new PublicDataWrite(computePublicDataTreeLeafSlot(baseContractAddress, contractSlotB), fr(0x151)),
new PublicDataWrite(contractSlotB, fr(0x151)),
);
expect(txEffect.encryptedLogs.getTotalLogCount()).toBe(0);
expect(txEffect.unencryptedLogs.getTotalLogCount()).toBe(0);
Expand Down
25 changes: 5 additions & 20 deletions yarn-project/simulator/src/public/execution.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { type SimulationError, type UnencryptedFunctionL2Logs } from '@aztec/circuit-types';
import {
type AztecAddress,
type ContractStorageRead,
type ContractStorageUpdateRequest,
type Fr,
Expand All @@ -12,7 +11,6 @@ import {
type SideEffect,
type SideEffectLinkedToNoteHash,
} from '@aztec/circuits.js';
import { computePublicDataTreeLeafSlot, computePublicDataTreeValue } from '@aztec/circuits.js/hash';

/**
* The public function execution result.
Expand Down Expand Up @@ -81,10 +79,8 @@ export function isPublicExecutionResult(
*/
export function collectPublicDataReads(execResult: PublicExecutionResult): PublicDataRead[] {
// HACK(#1622): part of temporary hack - may be able to remove this function after public state ordering is fixed
const contractAddress = execResult.execution.callContext.storageContractAddress;

const thisExecPublicDataReads = execResult.contractStorageReads.map(read =>
contractStorageReadToPublicDataRead(read, contractAddress),
contractStorageReadToPublicDataRead(read),
);
const unsorted = [
...thisExecPublicDataReads,
Expand All @@ -101,10 +97,8 @@ export function collectPublicDataReads(execResult: PublicExecutionResult): Publi
*/
export function collectPublicDataUpdateRequests(execResult: PublicExecutionResult): PublicDataUpdateRequest[] {
// HACK(#1622): part of temporary hack - may be able to remove this function after public state ordering is fixed
const contractAddress = execResult.execution.callContext.storageContractAddress;

const thisExecPublicDataUpdateRequests = execResult.contractStorageUpdateRequests.map(update =>
contractStorageUpdateRequestToPublicDataUpdateRequest(update, contractAddress),
contractStorageUpdateRequestToPublicDataUpdateRequest(update),
);
const unsorted = [
...thisExecPublicDataUpdateRequests,
Expand All @@ -119,12 +113,8 @@ export function collectPublicDataUpdateRequests(execResult: PublicExecutionResul
* @param contractAddress - the contract address of the read
* @returns The public data read.
*/
function contractStorageReadToPublicDataRead(read: ContractStorageRead, contractAddress: AztecAddress): PublicDataRead {
return new PublicDataRead(
computePublicDataTreeLeafSlot(contractAddress, read.storageSlot),
computePublicDataTreeValue(read.currentValue),
read.sideEffectCounter!,
);
function contractStorageReadToPublicDataRead(read: ContractStorageRead): PublicDataRead {
return new PublicDataRead(read.storageSlot, read.currentValue, read.sideEffectCounter!);
}

/**
Expand All @@ -135,13 +125,8 @@ function contractStorageReadToPublicDataRead(read: ContractStorageRead, contract
*/
function contractStorageUpdateRequestToPublicDataUpdateRequest(
update: ContractStorageUpdateRequest,
contractAddress: AztecAddress,
): PublicDataUpdateRequest {
return new PublicDataUpdateRequest(
computePublicDataTreeLeafSlot(contractAddress, update.storageSlot),
computePublicDataTreeValue(update.newValue),
update.sideEffectCounter!,
);
return new PublicDataUpdateRequest(update.storageSlot, update.newValue, update.sideEffectCounter!);
}

/**
Expand Down
25 changes: 24 additions & 1 deletion yarn-project/simulator/src/public/public_execution_context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@ import {
} from '@aztec/circuit-types';
import {
CallContext,
ContractStorageRead,
ContractStorageUpdateRequest,
FunctionData,
type FunctionSelector,
GasSettings,
type GlobalVariables,
type Header,
} from '@aztec/circuits.js';
import { computePublicDataTreeLeafSlot, computePublicDataTreeValue } from '@aztec/circuits.js/hash';
import { type AztecAddress } from '@aztec/foundation/aztec-address';
import { EthAddress } from '@aztec/foundation/eth-address';
import { Fr } from '@aztec/foundation/fields';
Expand Down Expand Up @@ -91,7 +94,27 @@ export class PublicExecutionContext extends TypedOracle {
*/
public getStorageActionData() {
const [contractStorageReads, contractStorageUpdateRequests] = this.storageActions.collect();
return { contractStorageReads, contractStorageUpdateRequests };
// TODO: Silo values in public tail circuit.
return {
contractStorageReads: contractStorageReads.map(r =>
r.isEmpty()
? r
: new ContractStorageRead(
computePublicDataTreeLeafSlot(this.execution.callContext.storageContractAddress, r.storageSlot),
computePublicDataTreeValue(r.currentValue),
r.sideEffectCounter,
),
),
contractStorageUpdateRequests: contractStorageUpdateRequests.map(w =>
w.isEmpty()
? w
: new ContractStorageUpdateRequest(
computePublicDataTreeLeafSlot(this.execution.callContext.storageContractAddress, w.storageSlot),
computePublicDataTreeValue(w.newValue),
w.sideEffectCounter,
),
),
};
}

/**
Expand Down

0 comments on commit 0aa7f23

Please sign in to comment.