-
Notifications
You must be signed in to change notification settings - Fork 284
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
Recursive public fns in ACIR public simulator #467
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
8d95d37
Refactor public executor and allow for nested calls
spalladino 0461967
Pad nested call return values and pad hex strings to even length
spalladino 1c5bc17
Test nested public call
spalladino 0a84a64
Fix mock bytecode hash
spalladino 18e9566
Simplify executor API
spalladino 327290c
Nicer interface for child contract function
spalladino 0b69e0a
Missed build artifact
spalladino File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
import { AztecAddress, CallContext, EthAddress, Fr, FunctionData, TxRequest } from '@aztec/circuits.js'; | ||
import { padArrayEnd } from '@aztec/foundation/collection'; | ||
import { createDebugLogger } from '@aztec/foundation/log'; | ||
import { select_return_flattened as selectPublicWitnessFlattened } from '@noir-lang/noir_util_wasm'; | ||
import { acvm, frToAztecAddress, frToSelector, fromACVMField, toACVMField, toACVMWitness } from '../acvm/index.js'; | ||
import { PublicContractsDB, PublicStateDB } from './db.js'; | ||
import { PublicExecution, PublicExecutionResult } from './execution.js'; | ||
import { StateActionsCollector } from './state_actions.js'; | ||
|
||
// Copied from crate::abi at noir-contracts/src/contracts/noir-aztec3/src/abi.nr | ||
const NOIR_MAX_RETURN_VALUES = 4; | ||
|
||
/** | ||
* Handles execution of public functions. | ||
*/ | ||
export class PublicExecutor { | ||
constructor( | ||
private readonly stateDb: PublicStateDB, | ||
private readonly contractsDb: PublicContractsDB, | ||
|
||
private log = createDebugLogger('aztec:simulator:public-executor'), | ||
) {} | ||
|
||
/** | ||
* Executes a public execution request. | ||
* @param execution - The execution to run. | ||
* @returns The result of the run plus all nested runs. | ||
*/ | ||
public async execute(execution: PublicExecution): Promise<PublicExecutionResult> { | ||
const selectorHex = execution.functionData.functionSelector.toString('hex'); | ||
this.log(`Executing public external function ${execution.contractAddress.toShortString()}:${selectorHex}`); | ||
|
||
const selector = execution.functionData.functionSelector; | ||
const acir = await this.contractsDb.getBytecode(execution.contractAddress, selector); | ||
if (!acir) throw new Error(`Bytecode not found for ${execution.contractAddress.toShortString()}:${selectorHex}`); | ||
|
||
const initialWitness = getInitialWitness(execution.args, execution.callContext); | ||
const stateActions = new StateActionsCollector(this.stateDb, execution.contractAddress); | ||
const nestedExecutions: PublicExecutionResult[] = []; | ||
|
||
const notAvailable = () => Promise.reject(`Built-in not available for public execution simulation`); | ||
|
||
const { partialWitness } = await acvm(acir, initialWitness, { | ||
getSecretKey: notAvailable, | ||
getNotes2: notAvailable, | ||
getRandomField: notAvailable, | ||
notifyCreatedNote: notAvailable, | ||
notifyNullifiedNote: notAvailable, | ||
callPrivateFunction: notAvailable, | ||
viewNotesPage: notAvailable, | ||
storageRead: async ([slot]) => { | ||
const storageSlot = fromACVMField(slot); | ||
const value = await stateActions.read(storageSlot); | ||
this.log(`Oracle storage read: slot=${storageSlot.toShortString()} value=${value.toString()}`); | ||
return [toACVMField(value)]; | ||
}, | ||
storageWrite: async ([slot, value]) => { | ||
const storageSlot = fromACVMField(slot); | ||
const newValue = fromACVMField(value); | ||
await stateActions.write(storageSlot, newValue); | ||
this.log(`Oracle storage write: slot=${storageSlot.toShortString()} value=${value.toString()}`); | ||
return [toACVMField(newValue)]; | ||
}, | ||
callPublicFunction: async ([address, functionSelector, ...args]) => { | ||
this.log(`Public function call: addr=${address} selector=${functionSelector} args=${args.join(',')}`); | ||
const childExecutionResult = await this.callPublicFunction( | ||
frToAztecAddress(fromACVMField(address)), | ||
frToSelector(fromACVMField(functionSelector)), | ||
args.map(f => fromACVMField(f)), | ||
execution.callContext, | ||
); | ||
|
||
nestedExecutions.push(childExecutionResult); | ||
this.log(`Returning from nested call: ret=${childExecutionResult.returnValues.join(', ')}`); | ||
return padArrayEnd(childExecutionResult.returnValues, Fr.ZERO, NOIR_MAX_RETURN_VALUES).map(fr => fr.toString()); | ||
}, | ||
}); | ||
|
||
const returnValues = selectPublicWitnessFlattened(acir, partialWitness).map(fromACVMField); | ||
const [stateReads, stateTransitions] = stateActions.collect(); | ||
|
||
return { | ||
stateReads, | ||
stateTransitions, | ||
returnValues, | ||
nestedExecutions, | ||
}; | ||
} | ||
|
||
/** | ||
* Creates a PublicExecution out of a TxRequest to a public function. | ||
* @param input - The TxRequest calling a public function. | ||
* @returns A PublicExecution object that can be run via execute. | ||
*/ | ||
public async getPublicExecution(input: TxRequest): Promise<PublicExecution> { | ||
const contractAddress = input.to; | ||
const portalContractAddress = (await this.contractsDb.getPortalContractAddress(contractAddress)) ?? EthAddress.ZERO; | ||
const callContext: CallContext = new CallContext(input.from, input.to, portalContractAddress, false, false, false); | ||
|
||
return { callContext, contractAddress, functionData: input.functionData, args: input.args }; | ||
} | ||
|
||
private async callPublicFunction( | ||
targetContractAddress: AztecAddress, | ||
targetFunctionSelector: Buffer, | ||
targetArgs: Fr[], | ||
callerContext: CallContext, | ||
) { | ||
const portalAddress = (await this.contractsDb.getPortalContractAddress(targetContractAddress)) ?? EthAddress.ZERO; | ||
const functionData = new FunctionData(targetFunctionSelector, false, false); | ||
|
||
const callContext = CallContext.from({ | ||
msgSender: callerContext.storageContractAddress, | ||
portalContractAddress: portalAddress, | ||
storageContractAddress: targetContractAddress, | ||
isContractDeployment: false, | ||
isDelegateCall: false, | ||
isStaticCall: false, | ||
}); | ||
|
||
const nestedExecution: PublicExecution = { | ||
args: targetArgs, | ||
contractAddress: targetContractAddress, | ||
functionData, | ||
callContext, | ||
}; | ||
|
||
return this.execute(nestedExecution); | ||
} | ||
} | ||
|
||
/** | ||
* Generates the initial witness for a public function. | ||
* @param args - The arguments to the function. | ||
* @param callContext - The call context of the function. | ||
* @param witnessStartIndex - The index where to start inserting the parameters. | ||
* @returns The initial witness. | ||
*/ | ||
function getInitialWitness(args: Fr[], callContext: CallContext, witnessStartIndex = 1) { | ||
return toACVMWitness(witnessStartIndex, [ | ||
callContext.isContractDeployment, | ||
callContext.isDelegateCall, | ||
callContext.isStaticCall, | ||
callContext.msgSender, | ||
callContext.portalContractAddress, | ||
callContext.storageContractAddress, | ||
...args, | ||
]); | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Just a suggestion but I feel like at this point we could type the callback as a Record with key string and value functions with type
(params: ACVMField[]) => Promise<ACVMField[]>
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.
Unfortunately not all functions have the same signature: we have return types
ACVMField[]
and[ACVMField]
. We could useACVMField[]
for all, but I think it's nice to hace an extra check. And using record with key string means we don't get type checks for the function names. I think I'd stick with the verbose approach for now.