-
Notifications
You must be signed in to change notification settings - Fork 595
refactor: drop artifact field from SimulationOverrides #22957
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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 hidden or 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 hidden or 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 hidden or 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
118 changes: 49 additions & 69 deletions
118
yarn-project/pxe/src/contract_function_simulator/proxied_contract_data_source.ts
This file contains hidden or 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 |
|---|---|---|
| @@ -1,89 +1,69 @@ | ||
| import { FunctionSelector } from '@aztec/stdlib/abi'; | ||
| import { AztecAddress } from '@aztec/stdlib/aztec-address'; | ||
| import type { AztecAddress } from '@aztec/stdlib/aztec-address'; | ||
| import type { ContractOverrides } from '@aztec/stdlib/tx'; | ||
|
|
||
| import type { ContractStore } from '../storage/contract_store/contract_store.js'; | ||
|
|
||
| /* | ||
| * Proxy generator for a ContractStore that allows overriding contract instances and artifacts, so | ||
| * the contract function simulator can execute different bytecode on certain addresses. An example use case | ||
| * would be overriding your own account contract so that valid signatures don't have to be provided while simulating. | ||
| * Proxy generator for a ContractStore that allows overriding contract instances at given addresses, | ||
|
dbanks12 marked this conversation as resolved.
|
||
| * so the contract function simulator can execute different bytecode on certain addresses. An example | ||
| * use case would be overriding your own account contract so that valid signatures don't have to be | ||
| * provided while simulating. | ||
| * | ||
| * Function artifact lookups for an overridden address are routed via the override-instance's | ||
| * `currentContractClassId` rather than the underlying ContractStore's address→class mapping — | ||
| * `ContractStore.getFunctionArtifact` calls `this.getContractInstance` internally, which the proxy | ||
| * cannot intercept (the binding sees the raw target, not the proxy). The target class must be | ||
| * registered ahead of time via `pxe.registerContractClass(...)`. | ||
| */ | ||
| export class ProxiedContractStoreFactory { | ||
| static create(contractStore: ContractStore, overrides?: ContractOverrides) { | ||
| if (!overrides) { | ||
| return contractStore; | ||
| } | ||
|
|
||
| const findFunctionInArtifact = async ( | ||
| artifact: { name: string; functions: { name: string; parameters: any[] }[] }, | ||
| selector: FunctionSelector, | ||
| contractAddress: AztecAddress, | ||
| ) => { | ||
| for (const fn of artifact.functions) { | ||
| const fnSelector = await FunctionSelector.fromNameAndParameters(fn.name, fn.parameters); | ||
| if (fnSelector.equals(selector)) { | ||
| return { ...fn, contractName: artifact.name } as any; | ||
| } | ||
| } | ||
| throw new Error( | ||
| `Function with selector ${selector} not found in stub artifact for overridden contract at ${contractAddress}.`, | ||
| ); | ||
| }; | ||
|
Comment on lines
+25
to
+39
Contributor
Author
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. Claude pulled this out into a helper which made me think.... This logic ,which was previously inline in the switch statement below, is nearly identical to a helper in |
||
|
|
||
| return new Proxy(contractStore, { | ||
| get(target, prop: keyof ContractStore) { | ||
| switch (prop) { | ||
| case 'getContractInstance': { | ||
| return async (address: AztecAddress) => { | ||
| if (overrides[address.toString()]) { | ||
| const { instance } = overrides[address.toString()]!; | ||
| instance.address = address; | ||
| const realInstance = await target.getContractInstance(address); | ||
| if (!realInstance) { | ||
| throw new Error(`Contract instance not found for address: ${address}`); | ||
| } | ||
| instance.currentContractClassId = realInstance.currentContractClassId; | ||
| instance.originalContractClassId = realInstance.originalContractClassId; | ||
| instance.initializationHash = realInstance.initializationHash; | ||
| return instance; | ||
| } else { | ||
| return target.getContractInstance(address); | ||
| } | ||
| }; | ||
| } | ||
| case 'getFunctionArtifact': { | ||
| return async (contractAddress: AztecAddress, selector: FunctionSelector) => { | ||
| if (overrides[contractAddress.toString()]) { | ||
| const { artifact } = overrides[contractAddress.toString()]!; | ||
| const functions = artifact.functions; | ||
| for (let i = 0; i < functions.length; i++) { | ||
| const fn = functions[i]; | ||
| const fnSelector = await FunctionSelector.fromNameAndParameters(fn.name, fn.parameters); | ||
| if (fnSelector.equals(selector)) { | ||
| return fn; | ||
| } | ||
| } | ||
| throw new Error( | ||
| `Function with selector ${selector} not found in stub artifact for overridden contract at ${contractAddress}. The stub does not implement this function.`, | ||
| ); | ||
| } else { | ||
| return target.getFunctionArtifact(contractAddress, selector); | ||
| } | ||
| }; | ||
| } | ||
| case 'getFunctionArtifactWithDebugMetadata': { | ||
| return async (contractAddress: AztecAddress, selector: FunctionSelector) => { | ||
| if (overrides[contractAddress.toString()]) { | ||
| const { artifact } = overrides[contractAddress.toString()]!; | ||
| const functions = artifact.functions; | ||
| for (let i = 0; i < functions.length; i++) { | ||
| const fn = functions[i]; | ||
| const fnSelector = await FunctionSelector.fromNameAndParameters(fn.name, fn.parameters); | ||
| if (fnSelector.equals(selector)) { | ||
| return fn; | ||
| } | ||
| } | ||
| throw new Error( | ||
| `Function with selector ${selector} not found in stub artifact for overridden contract at ${contractAddress}. The stub does not implement this function.`, | ||
| ); | ||
| } else { | ||
| return target.getFunctionArtifactWithDebugMetadata(contractAddress, selector); | ||
| } | ||
| }; | ||
| } | ||
| default: { | ||
| const value = Reflect.get(target, prop); | ||
| if (typeof value === 'function') { | ||
| return value.bind(target); | ||
| if (prop === 'getContractInstance') { | ||
| return (address: AztecAddress) => { | ||
| const override = overrides[address.toString()]; | ||
| return override ? Promise.resolve(override.instance) : target.getContractInstance(address); | ||
| }; | ||
| } | ||
| if (prop === 'getFunctionArtifact' || prop === 'getFunctionArtifactWithDebugMetadata') { | ||
| return async (contractAddress: AztecAddress, selector: FunctionSelector) => { | ||
| const override = overrides[contractAddress.toString()]; | ||
| if (!override) { | ||
| return (target[prop] as Function).call(target, contractAddress, selector); | ||
| } | ||
| const artifact = await target.getContractArtifact(override.instance.currentContractClassId); | ||
| if (!artifact) { | ||
| throw new Error( | ||
| `No artifact registered for override class ${override.instance.currentContractClassId} ` + | ||
| `at ${contractAddress}. Register it via pxe.registerContractClass(...) before simulating.`, | ||
| ); | ||
| } | ||
| return value; | ||
| } | ||
| return findFunctionInArtifact(artifact, selector, contractAddress); | ||
| }; | ||
| } | ||
| const value = Reflect.get(target, prop); | ||
| return typeof value === 'function' ? value.bind(target) : value; | ||
| }, | ||
| }) satisfies ContractStore; | ||
| } | ||
|
|
||
This file contains hidden or 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
Oops, something went wrong.
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.
Identical to cli-wallet's fn.... Could consider pulling into a helper somewhere, but not sure it's worth it.