Repository navigation
Build simulator from .yul source
#6799
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
james-toussaint
merged 15 commits into
OpenZeppelin:master
from
james-toussaint:yul/call-simulator
Oct 15, 2026
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
1726033
Build simulator from `.yul` source
james-toussaint 512f051
Move to contracts/
james-toussaint 9ee6ebf
Add changelog
james-toussaint 7aa547c
Slither profile skips yul
james-toussaint 6fc0014
Remove compile:yul
james-toussaint 2622586
Add hh yul plugin
james-toussaint ee96677
Compile to dedicated dir
james-toussaint 05f1b90
Compile .yul sources as part of the Hardhat build
Amxx 479be6f
Give Yul artifacts a buildInfoId
Amxx e7c80e1
Use fully qualified name for CallSimulator artifact
Amxx 2ea56a3
up
Amxx f07210d
Update embedded simulator bytecode in SimulateCall
Amxx cb49392
Remove calldata length check from simulator
Amxx 795dddc
Update SimulateCall.sol
Amxx 4a0cc67
Merge branch 'master' into yul/call-simulator
Amxx 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| 'openzeppelin-solidity': patch | ||
| --- | ||
|
|
||
| `SimulateCall`: Changed the deployed call-simulator bytecode, which also changes its deployed address. |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| // SPDX-License-Identifier: MIT | ||
|
|
||
| /// @dev Call simulator deployed via CREATE2 and delegatecalled by {SimulateCall}. | ||
| object "SimulationRelayer" { | ||
| /// @dev Constructor: returns the runtime object as the deployed code. | ||
| code { | ||
| datacopy(0, dataoffset("runtime"), datasize("runtime")) | ||
| return(0, datasize("runtime")) | ||
| } | ||
| object "runtime" { | ||
| /// @dev Runs the target call and inverts the result. | ||
| /// - Reverts on success (undoing state). | ||
| /// - Returns on failure. | ||
| /// Calldata layout: target[0x00:0x14] | value[0x14:0x34] | data[0x34:] | ||
| /// | ||
| /// WARNING: Behavior is undefined if the calldata is shorter than 0x34 bytes. In that case, the length | ||
| /// computation underflows and the `calldatacopy` attempts to expand memory to an unreasonable size, | ||
| /// which in practice runs out of gas. | ||
| code { | ||
| let len := sub(calldatasize(), 0x34) | ||
| calldatacopy(0x00, 0x34, len) | ||
| let success := call(gas(), shr(0x60, calldataload(0x00)), calldataload(0x14), 0x00, len, 0x00, 0x00) | ||
| returndatacopy(0x00, 0x00, returndatasize()) | ||
| if success { revert(0x00, returndatasize()) } | ||
| /* else */ { return(0x00, returndatasize()) } | ||
| } | ||
| } | ||
| } | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| import type { SolidityHooks } from 'hardhat/types/hooks'; | ||
| import fs from 'node:fs'; | ||
| import path from 'node:path'; | ||
| import { compileYul } from '../internal/compile.ts'; | ||
|
|
||
| const isYul = (file: string) => file.endsWith('.yul'); | ||
|
|
||
| // Hardhat only discovers `.sol` files. This hook compiles the `.yul` files found in the sources directories (or | ||
| // explicitly passed to `hardhat build`) alongside them, and emits regular artifacts at `artifacts/<sourceName>/`. | ||
| export default async (): Promise<Partial<SolidityHooks>> => ({ | ||
| build: async (context, rootFilePaths, options, next) => { | ||
| // Yul files are removed from the roots, so the Solidity build system doesn't try to parse them. | ||
| const result = await next( | ||
| context, | ||
| rootFilePaths.filter(file => !isYul(file)), | ||
| options, | ||
| ); | ||
|
|
||
| if (context.solidity.isSuccessfulBuildResult(result) && options?.scope !== 'tests') { | ||
| const { root, artifacts, sources } = context.config.paths; | ||
|
|
||
| // Discover all Yul files in the sources directories and the root file paths. | ||
| // Add any Yul files explicitly passed to the build via rootFilePaths. | ||
| const yulFiles = new Set([ | ||
| ...sources.solidity | ||
| .filter(dir => fs.existsSync(dir)) | ||
| .flatMap(dir => | ||
| fs | ||
| .readdirSync(dir, { recursive: true, encoding: 'utf8' }) | ||
| .filter(isYul) | ||
| .map(file => path.join(dir, file)), | ||
| ), | ||
| ...rootFilePaths.filter(isYul), | ||
| ]); | ||
|
|
||
| for (const file of yulFiles) { | ||
| const sourceName = path.relative(root, file).split(path.sep).join('/'); | ||
| const outDir = path.join(artifacts, sourceName); | ||
| fs.mkdirSync(outDir, { recursive: true }); | ||
| for (const { contractName, buildInfoId, bytecode, deployedBytecode } of await compileYul(context, sourceName)) { | ||
| const artifact = { | ||
| _format: 'hh3-artifact-1', | ||
| contractName, | ||
| sourceName, | ||
| abi: [], | ||
| bytecode, | ||
| linkReferences: {}, | ||
| deployedBytecode, | ||
| deployedLinkReferences: {}, | ||
| buildInfoId, | ||
| }; | ||
| fs.writeFileSync(path.join(outDir, `${contractName}.json`), `${JSON.stringify(artifact, null, 2)}\n`); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return result; | ||
| }, | ||
| }); |
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 |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| import type { HookContext } from 'hardhat/types/hooks'; | ||
| import type { SolidityBuildInfo } from 'hardhat/types/solidity'; | ||
| import { createHash } from 'node:crypto'; | ||
| import { readFile } from 'node:fs/promises'; | ||
| import path from 'node:path'; | ||
|
|
||
| export interface YulContract { | ||
| contractName: string; | ||
| buildInfoId: string; | ||
| bytecode: string; | ||
| deployedBytecode: string; | ||
| } | ||
|
|
||
| // Compile a Yul source with the solc resolved by Hardhat (courtesy from the Hardhat team). | ||
| export async function compileYul(context: HookContext, sourceName: string): Promise<YulContract[]> { | ||
| const { version, settings } = context.config.solidity.profiles.default.compilers[0]; | ||
|
|
||
| const input = { | ||
| language: 'Yul', | ||
| sources: { | ||
| [sourceName]: { content: await readFile(path.join(context.config.paths.root, sourceName), 'utf8') }, | ||
| }, | ||
| settings: { | ||
| optimizer: settings.optimizer, | ||
| evmVersion: settings.evmVersion, | ||
| outputSelection: { '*': { '*': ['evm.bytecode.object', 'evm.deployedBytecode.object'] } }, | ||
| }, | ||
| }; | ||
|
|
||
| // Hardhat requires every artifact to reference a build info, with an id of the form `solc-<major>_<minor>_<patch>-<hex>` | ||
| // (the solc version is parsed from it). No build info file is written: only the id is needed. | ||
| const buildInfoId = `solc-${version.replaceAll('.', '_')}-${createHash('sha256').update(JSON.stringify(input)).digest('hex')}`; | ||
|
|
||
| // `context.solidity.compileBuildInfo` downloads (or reuses from cache) the requested solc version | ||
| const output = await context.solidity.compileBuildInfo( | ||
| { solcVersion: version, input } as unknown as SolidityBuildInfo, | ||
| { | ||
| quiet: true, | ||
| }, | ||
| ); | ||
|
|
||
| const errors = output.errors?.filter(e => e.severity === 'error') ?? []; | ||
| if (errors.length > 0) { | ||
| throw new Error(errors.map(e => e.formattedMessage).join('\n')); | ||
| } | ||
|
|
||
| return Object.entries(output.contracts?.[sourceName] ?? {}).map(([contractName, { evm }]) => { | ||
| if (!evm?.bytecode?.object || !evm?.deployedBytecode?.object) { | ||
| throw new Error(`No bytecode produced for ${sourceName}:${contractName}`); | ||
| } | ||
| return { | ||
| contractName, | ||
| buildInfoId, | ||
| bytecode: `0x${evm.bytecode.object}`, | ||
| deployedBytecode: `0x${evm.deployedBytecode.object}`, | ||
| }; | ||
| }); | ||
| } |
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 |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| import type { HardhatPlugin } from 'hardhat/types/plugins'; | ||
|
|
||
| const hardhatYulPlugin: HardhatPlugin = { | ||
| id: 'hardhat-yul', | ||
| hookHandlers: { | ||
| solidity: () => import('./hook-handlers/solidity.ts'), | ||
| }, | ||
| }; | ||
|
|
||
| export default hardhatYulPlugin; |
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.
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.
Uh oh!
There was an error while loading. Please reload this page.