-
Notifications
You must be signed in to change notification settings - Fork 310
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(avm-simulator): avm's nested calls now stay internal and properly track PublicExecutionResult #6165
chore(avm-simulator): avm's nested calls now stay internal and properly track PublicExecutionResult #6165
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,7 +26,7 @@ import { PackedValuesCache } from '../common/packed_values_cache.js'; | |
import { type CommitmentsDB, type PublicContractsDB, type PublicStateDB } from './db.js'; | ||
import { type PublicExecution, type PublicExecutionResult, checkValidStaticCall } from './execution.js'; | ||
import { PublicExecutionContext } from './public_execution_context.js'; | ||
import { convertAvmResults, createAvmExecutionEnvironment, isAvmBytecode } from './transitional_adaptors.js'; | ||
import { convertAvmResultsToPxResult, createAvmExecutionEnvironment, isAvmBytecode } from './transitional_adaptors.js'; | ||
|
||
/** | ||
* Execute a public function and return the execution result. | ||
|
@@ -46,15 +46,22 @@ export async function executePublicFunction( | |
} | ||
|
||
if (isAvmBytecode(bytecode)) { | ||
return await executePublicFunctionAvm(context); | ||
return await executeTopLevelPublicFunctionAvm(context); | ||
} else { | ||
return await executePublicFunctionAcvm(context, bytecode, nested); | ||
} | ||
} | ||
|
||
async function executePublicFunctionAvm(executionContext: PublicExecutionContext): Promise<PublicExecutionResult> { | ||
/** | ||
* Execute a top-level public function call (the first call in an enqueued-call/execution-request) in the AVM. | ||
* Translate the results back to the PublicExecutionResult format. | ||
*/ | ||
async function executeTopLevelPublicFunctionAvm( | ||
executionContext: PublicExecutionContext, | ||
): Promise<PublicExecutionResult> { | ||
const address = executionContext.execution.contractAddress; | ||
const selector = executionContext.execution.functionData.selector; | ||
const startGas = executionContext.availableGas; | ||
const log = createDebugLogger('aztec:simulator:public_execution'); | ||
log.verbose(`[AVM] Executing public external function ${address.toString()}:${selector}.`); | ||
|
||
|
@@ -65,7 +72,12 @@ async function executePublicFunctionAvm(executionContext: PublicExecutionContext | |
executionContext.contractsDb, | ||
executionContext.commitmentsDb, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
[Re: line 55]
Maybe add a comment clarifying that this function is (as opposed to ...Acvm) only called at the top level but not for nested calls. See this comment inline on Graphite. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will do (in the next PR though where that is actually made true) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh I was confused, that's this PR 👍 |
||
); | ||
|
||
// TODO(6207): add sideEffectCounter to persistableState construction | ||
// or modify the PersistableStateManager to manage rollbacks across enqueued-calls and transactions. | ||
const worldStateJournal = new AvmPersistableStateManager(hostStorage); | ||
const startSideEffectCounter = executionContext.execution.callContext.sideEffectCounter; | ||
worldStateJournal.trace.accessCounter = startSideEffectCounter; | ||
|
||
const executionEnv = createAvmExecutionEnvironment( | ||
executionContext.execution, | ||
|
@@ -75,18 +87,21 @@ async function executePublicFunctionAvm(executionContext: PublicExecutionContext | |
executionContext.transactionFee, | ||
); | ||
|
||
const machineState = new AvmMachineState(executionContext.availableGas); | ||
const context = new AvmContext(worldStateJournal, executionEnv, machineState); | ||
const simulator = new AvmSimulator(context); | ||
const machineState = new AvmMachineState(startGas); | ||
const avmContext = new AvmContext(worldStateJournal, executionEnv, machineState); | ||
const simulator = new AvmSimulator(avmContext); | ||
|
||
const result = await simulator.execute(); | ||
const newWorldState = context.persistableState.flush(); | ||
const avmResult = await simulator.execute(); | ||
|
||
log.verbose( | ||
`[AVM] ${address.toString()}:${selector} returned, reverted: ${result.reverted}, reason: ${result.revertReason}.`, | ||
`[AVM] ${address.toString()}:${selector} returned, reverted: ${avmResult.reverted}, reason: ${ | ||
avmResult.revertReason | ||
}.`, | ||
); | ||
|
||
return await convertAvmResults(executionContext, newWorldState, result, machineState); | ||
return Promise.resolve( | ||
convertAvmResultsToPxResult(avmResult, startSideEffectCounter, executionContext.execution, startGas, avmContext), | ||
); | ||
} | ||
|
||
async function executePublicFunctionAcvm( | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IIUC now the ACVM can call the AVM. Do you expect to support that and will it work ok, or should we rather just remove it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think I've really messed with any of the interop stuff in that direction, so it might work 🤷♂️ Maybe let's see how it goes when we start changing contracts to use the AVM and then we can remove it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(i didn't disable the e2e interop tests in that direction)