Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: share state manager and side effect tracer for all enqueued calls in tx #9565

Merged
merged 2 commits into from
Nov 6, 2024

Conversation

dbanks12
Copy link
Collaborator

@dbanks12 dbanks12 commented Oct 30, 2024

This PR is a bit of a mashup of things as I made sense of what needs to happen to start tracking side effects and state changes at the transaction level.

Refactor of PublicExecutionResult, PublicExecutor, EnqueuedCallSimulator & EnqueuedCallsProcessor. A lot of this PR is prep for removal of the public kernel and generation of TX-level AVM public inputs & hints.

  1. Pulls the AvmStateManager (needs rename) up to the EnqueuedCallsProcessor and uses it for state checkpointing/forking & rollbacks instead of WorldStateDB. This will let us track side effects at the transaction level and eventually generate AVM circuit hints & public inputs at the transaction level.
  2. Simplifies the return type (public execution result) from PublicExecutor.simulate(). The only consumers of PublicExecutor.simulate() are the TXE and the EnqueuedCallSimulator. The TXE needs side effects, but the EnqueuedCallSimulator shouldn't (although it does until the public kernel is deleted). I provided a minimal return type for each case. EnqueuedCallSimulator doesn't receive side effects in the return type from PublicExecutor.simulate() and instead makes a call to the trace to retrieve VMCircuitPublicInputs. Eventually that call to the trace will happen in the EnqueuedCallsProcessor just once for the entire transaction.
  3. Moves some PublicProcessor tests into EnqueudCallsProcessor tests as they don't really belong at that high a level.
  4. Adds tracing of enqueued calls in side effect trace for future use of enqueued call hints.

Followup work:

  1. Rename AvmStateManager -> PublicStateManager, rename file accordingly, & move to simulator/src/public
  2. Once public kernel is removed and new AVMPublicInputs are introduced, pull the calls to stateManager.trace.toVMCircuitPublicInputs() up to the EnqueuedCallsProcessor & modify that function to return the new AVMPublicInputs
  3. Remove the deprecated PublicFunctionCallResult and the old PublicSideEffectTrace (to be fully replaced by the PublicEnqueuedCallSideEffectTrace which will need a rename). This will require disconnecting the AVM circuit completely until it supports the tx-level AVMPublicInputs.

@dbanks12 dbanks12 requested a review from fcarreiro as a code owner October 30, 2024 01:30
Copy link
Collaborator Author

dbanks12 commented Oct 30, 2024

@dbanks12 dbanks12 changed the title chore: share state manager and side effect tracer for all enqueued calls in tx [WIP] chore: share state manager and side effect tracer for all enqueued calls in tx Oct 30, 2024
@dbanks12 dbanks12 force-pushed the db/tx-level-state-and-trace branch from 75e6b4b to 15de5d9 Compare October 30, 2024 02:00
@dbanks12 dbanks12 removed the request for review from fcarreiro October 30, 2024 02:11
@dbanks12 dbanks12 force-pushed the db/tx-level-state-and-trace branch from 15de5d9 to f3891e6 Compare October 30, 2024 18:59
@dbanks12 dbanks12 force-pushed the db/tx-level-state-and-trace branch 17 times, most recently from 26df580 to 46bd46f Compare November 2, 2024 16:23
Comment on lines 87 to 88

fn validate_counters(self, previous_kernel: PublicKernelCircuitPublicInputs) {
assert_eq(
self.enqueued_call.data.start_side_effect_counter,
previous_kernel.end_side_effect_counter + 1,
"enqueued call must start from the end counter of the previous call",
);
}

// Validates that the start gas injected into the vm circuit matches the remaining gas.
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This constraint was failing because when an enqueued call had no side effects, it would start with start == prev.end. I was trying to figure out the proper way to modify this constraint or modify the AVM/tracing when I realized.... We're going to delete this anyway since side effect counters shouldn't be a critical part of the protocol with everything in the AVM.

global DEFAULT_TEARDOWN_GAS_LIMIT: u32 = 100_000_000;
global DEFAULT_TEARDOWN_GAS_LIMIT: u32 = 12_000_000;
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no reason the teardown limit should be higher than the enqueued call limit

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It maybe doesn't make sense to change this here, but I did so while debugging a test failure and thought... why change it back?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

then maybe you can move the line past MAX_L2_GAS_PER_ENQUEUED_CALL and do

global DEFAULT_TEARDOWN_GAS_LIMIT  = MAX_L2_GAS_PER_ENQUEUED_CALL;

?

@@ -56,93 +54,5 @@ describe('prover/orchestrator/public-functions', () => {
expect(block.number).toEqual(context.blockNumber);
},
);

it('nested public calls', async () => {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

test doesn't make sense any more because we aren't generating proofs per nested public call

describe('AVM simulator: transpiled Noir contracts', () => {
it('bulk testing', async () => {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Made some debugging easier so i didn't have to run the bb-prover tests. This is probably good to have here anyway.

@@ -1 +1,2 @@
export * from './avm_simulator.js';
export * from './journal/index.js';
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the journal needs to be renamed StateManager and moved to the src/public/ folder. Will do so in a follow-up PR.

// TODO(5818): make private once no longer accessed in executor
public readonly publicStorage: PublicStorage,
public readonly trace: PublicSideEffectTraceInterface,
Copy link
Collaborator Author

@dbanks12 dbanks12 Nov 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Made public for now.... Otherwise I need to keep adding passthrough functions to the trace. Eventually, the only place outside the AVM where the trace will be accessed will be in the enqueued calls processor where the trace instance is first constructed. At that point we should be able to make this private again since the instance is directly accessible there.

@dbanks12 dbanks12 force-pushed the db/tx-level-state-and-trace branch 2 times, most recently from 5b22d9f to 58bb2b7 Compare November 5, 2024 19:26
@@ -219,11 +220,36 @@ export class TXE implements TypedOracle {
return this.txeDatabase.addAuthWitness(authWitness.requestHash, authWitness.witness);
}

async addNullifiers(contractAddress: AztecAddress, nullifiers: Fr[]) {
async addPublicDataWrites(writes: PublicDataUpdateRequest[]) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

public executor no longer commits to worldStateDB, so we need to manually perform tree insertions for public data writes here

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TY David! LGTM, provided the current TXE test suite passes (which it does!)

Seems like we don't have to build the CombinedConstantData in its entirety now, is this going to stay that way? If it is, I'll leave just the GlobalVariables struct in executePublicFunction in an upcoming refactor PR, should clean it up quite nicely.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm I think it will stay that way, at least for the TXE's interface to the executor

@dbanks12 dbanks12 force-pushed the db/tx-level-state-and-trace branch 2 times, most recently from 84c628a to 8227f99 Compare November 5, 2024 21:04
@dbanks12 dbanks12 force-pushed the db/tx-level-state-and-trace branch from 8227f99 to c5f6d3a Compare November 6, 2024 00:49
@dbanks12 dbanks12 changed the title [WIP] chore: share state manager and side effect tracer for all enqueued calls in tx chore: share state manager and side effect tracer for all enqueued calls in tx Nov 6, 2024
@dbanks12 dbanks12 force-pushed the db/tx-level-state-and-trace branch from c5f6d3a to 9bf1802 Compare November 6, 2024 01:06
@dbanks12 dbanks12 force-pushed the db/tx-level-state-and-trace branch from 9bf1802 to c4b624d Compare November 6, 2024 01:36
Copy link
Collaborator

@LeilaWang LeilaWang left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! 🚀

Copy link
Contributor

@fcarreiro fcarreiro left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is a good step forwards.

I still don't have a lot of clarity on how things will end up looking so I think the best (for me) is to see where we land and then once we think we have the final version, I can judge it better :)

yarn-project/simulator/src/mocks/fixtures.ts Show resolved Hide resolved
@@ -265,17 +331,28 @@ export class EnqueuedCallsProcessor {
);
throw enqueuedCallResult.revertReason;
}
await txStateManager.mergeStateForEnqueuedCall(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note to self: I'm wondering why we can't have a generic accept/reject and we need a special one for enqueued call?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because this merge function traces the enqueued call under the hood, adding it to hints & vector that ultimately becomes public inputs

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right now, this is how we handle all "merges of forked state". We merge the forked state, and we trace the thing that caused the fork. It's not pretty....

Maybe the merge and trace should be separate functions? So we first do mergeForkedState which is always the same no matter what. And then we call a tailored trace function?

yarn-project/simulator/src/public/executor.ts Show resolved Hide resolved
global DEFAULT_TEARDOWN_GAS_LIMIT: u32 = 100_000_000;
global DEFAULT_TEARDOWN_GAS_LIMIT: u32 = 12_000_000;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

then maybe you can move the line past MAX_L2_GAS_PER_ENQUEUED_CALL and do

global DEFAULT_TEARDOWN_GAS_LIMIT  = MAX_L2_GAS_PER_ENQUEUED_CALL;

?

@dbanks12 dbanks12 merged commit e3756fd into master Nov 6, 2024
68 checks passed
@dbanks12 dbanks12 deleted the db/tx-level-state-and-trace branch November 6, 2024 16:00
@dbanks12
Copy link
Collaborator Author

dbanks12 commented Nov 6, 2024

Resolving comments in #9785

@dbanks12
Copy link
Collaborator Author

dbanks12 commented Nov 6, 2024

then maybe you can move the line past MAX_L2_GAS_PER_ENQUEUED_CALL and do
global DEFAULT_TEARDOWN_GAS_LIMIT = MAX_L2_GAS_PER_ENQUEUED_CALL;

I could, but I think that the max-per-enqueued call might go away and be replaced by a limit for the whole TX (minus teardown)

@LeilaWang LeilaWang linked an issue Nov 12, 2024 that may be closed by this pull request
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Remove public kernel inner.
4 participants