-
-
Notifications
You must be signed in to change notification settings - Fork 57
feat(evm): add evm2 call execution #368
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
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,19 @@ | ||
| --- | ||
| "ox": minor | ||
| --- | ||
|
|
||
| Added `ox/evm`, an EVM backed by `alloy-rs/evm2` compiled to WebAssembly, with read-only transaction execution. | ||
|
|
||
| ```ts | ||
| import { Database, Evm, TxResult } from 'ox/evm' | ||
|
|
||
| const evm = await Evm.create({ | ||
| database: Database.fromMemory({ | ||
| accounts: { '0x0000000000000000000000000000000000000001': { balance: 1n } }, | ||
| }), | ||
| specId: 'osaka', | ||
| }) | ||
|
|
||
| const result = Evm.callTx(evm, { envelope, signer }) | ||
| TxResult.txGasUsed(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
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,35 @@ | ||
| { | ||
| "$schema": "https://developer.microsoft.com/json-schemas/api-extractor/v7/api-extractor.schema.json", | ||
| "mainEntryPointFilePath": "../../dist/evm/index.d.ts", | ||
| "bundledPackages": [], | ||
| "newlineKind": "lf", | ||
| "enumMemberOrder": "preserve", | ||
| "compiler": { | ||
| "tsconfigFilePath": "<projectFolder>/tsconfig.json", | ||
| "overrideTsconfig": {}, | ||
| "skipLibCheck": true | ||
| }, | ||
| "apiReport": { | ||
| "enabled": false | ||
| }, | ||
| "docModel": { | ||
| "enabled": true, | ||
| "apiJsonFilePath": "<projectFolder>/scripts/docgen/evm.api.json", | ||
| "includeForgottenExports": true, | ||
| "projectFolderUrl": "https://github.com/wevm/ox/blob/main" | ||
| }, | ||
| "dtsRollup": { | ||
| "enabled": false | ||
| }, | ||
| "messages": { | ||
| "extractorMessageReporting": { | ||
| "default": { "logLevel": "warning" }, | ||
| "ae-different-release-tags": { "logLevel": "none" }, | ||
| "ae-incompatible-release-tags": { "logLevel": "none" }, | ||
| "ae-internal-mixed-release-tag": { "logLevel": "none" }, | ||
| "ae-missing-release-tag": { "logLevel": "none" }, | ||
| "ae-unresolved-link": { "logLevel": "none" }, | ||
| "ae-forgotten-export": { "logLevel": "none" } | ||
| } | ||
| } | ||
| } |
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,159 @@ | ||
| import type * as Address from '../core/Address.js' | ||
| import * as Bytes from '../core/Bytes.js' | ||
| import * as Errors from '../core/Errors.js' | ||
| import * as Hash from '../core/Hash.js' | ||
| import * as Hex from '../core/Hex.js' | ||
| import type * as internal from './internal/database.js' | ||
|
|
||
| /** | ||
| * State an EVM reads through. | ||
| * | ||
| * A database answers reads and nothing else. Writes accumulate inside the EVM, | ||
| * never reaching back through this boundary, so a source can be a plain object | ||
| * over local state or a view onto a remote node. | ||
| * | ||
| * Reads are synchronous here. Asynchronous sources arrive with a separate type. | ||
| */ | ||
| export type Database = internal.Database | ||
|
|
||
| /** An account, as a database reports it. */ | ||
| export type Account = internal.Account | ||
|
|
||
| /** | ||
| * Creates an in-memory database. | ||
| * | ||
| * Code is supplied inline with each account, so | ||
| * {@link ox#Database.(Database:type)}`.getCodeByHash` is never reached: evm2 | ||
| * files inline code under its hash when it loads the account. | ||
| * | ||
| * @example | ||
| * ```ts twoslash | ||
| * import { Database } from 'ox/evm' | ||
| * | ||
| * const database = Database.fromMemory({ | ||
| * accounts: { | ||
| * '0x0000000000000000000000000000000000000001': { | ||
| * balance: 1n | ||
| * }, | ||
| * '0x0000000000000000000000000000000000000002': { | ||
| * code: '0x5f5ff3' | ||
| * } | ||
| * } | ||
| * }) | ||
| * ``` | ||
| * | ||
| * @param options - Initial state. | ||
| * @returns An in-memory database. | ||
| */ | ||
| export function fromMemory(options: fromMemory.Options = {}): Database { | ||
| type Entry = { | ||
| balance: bigint | ||
| code: Bytes.Bytes | undefined | ||
| codeHash: Hex.Hex | ||
| nonce: bigint | ||
| storage: Map<bigint, bigint> | ||
| } | ||
|
|
||
| const emptyCodeHash = Hash.keccak256('0x') | ||
| const accounts = new Map<string, Entry>() | ||
| for (const [address, account] of Object.entries(options.accounts ?? {})) { | ||
| // Copied, not referenced: `Bytes.from` hands back a caller's own array, and | ||
| // mutating it later would change the code while `codeHash` stayed stale. | ||
| const code = account.code | ||
| ? Uint8Array.from(Bytes.from(account.code)) | ||
| : undefined | ||
| accounts.set(address.toLowerCase(), { | ||
| balance: account.balance ?? 0n, | ||
| code, | ||
| codeHash: code ? Hash.keccak256(Hex.fromBytes(code)) : emptyCodeHash, | ||
| nonce: account.nonce ?? 0n, | ||
| storage: new Map( | ||
| Object.entries(account.storage ?? {}).map(([slot, value]) => [ | ||
| BigInt(slot), | ||
| value, | ||
| ]), | ||
| ), | ||
| }) | ||
| } | ||
|
|
||
| const blockHashes = new Map( | ||
| Object.entries(options.blockHashes ?? {}).map(([number, value]) => [ | ||
| BigInt(number), | ||
| value, | ||
| ]), | ||
| ) | ||
|
|
||
| return { | ||
| getAccount(address) { | ||
| const account = accounts.get(address.toLowerCase()) | ||
| if (!account) return undefined | ||
| return { | ||
| balance: account.balance, | ||
| // Copied on the way out too: handing back the stored array would let a | ||
| // caller change the executed code while `codeHash` stayed committed. | ||
| code: account.code ? Uint8Array.from(account.code) : undefined, | ||
| codeHash: account.codeHash, | ||
| nonce: account.nonce, | ||
| } | ||
| }, | ||
| getBlockHash(number) { | ||
| // evm2 range-checks `BLOCKHASH` before asking, so a number reaching here | ||
| // is one the chain has and this database is missing. | ||
| const blockHash = blockHashes.get(number) | ||
| if (!blockHash) throw new MissingBlockHashError({ number }) | ||
| return blockHash | ||
| }, | ||
| getCodeByHash(codeHash) { | ||
| for (const account of accounts.values()) | ||
| if ( | ||
| account.code && | ||
| account.codeHash.toLowerCase() === codeHash.toLowerCase() | ||
| ) | ||
| return Uint8Array.from(account.code) | ||
| return new Uint8Array() | ||
| }, | ||
| getStorage(address, key) { | ||
| return accounts.get(address.toLowerCase())?.storage.get(key) ?? 0n | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| export declare namespace fromMemory { | ||
| type Options = { | ||
| /** Accounts to seed, keyed by address. */ | ||
| accounts?: | ||
| | Record< | ||
| Address.Address | (string & {}), | ||
| { | ||
| /** Balance in wei. @default 0n */ | ||
| balance?: bigint | undefined | ||
| /** Deployed code. @default '0x' */ | ||
| code?: Hex.Hex | Bytes.Bytes | undefined | ||
| /** Nonce. @default 0n */ | ||
| nonce?: bigint | undefined | ||
| /** Storage slots, keyed by slot. */ | ||
| storage?: Record<string, bigint> | undefined | ||
| } | ||
| > | ||
| | undefined | ||
| /** | ||
| * Historical block hashes, keyed by block number. A `BLOCKHASH` for a | ||
| * height inside the window but absent here fails the read. | ||
| */ | ||
| blockHashes?: Record<string, Hex.Hex> | undefined | ||
| } | ||
| } | ||
|
|
||
| /** Thrown when a `BLOCKHASH` the chain retains was never seeded. */ | ||
| export class MissingBlockHashError extends Errors.BaseError { | ||
| override readonly name = 'Database.MissingBlockHashError' | ||
|
|
||
| constructor({ number }: { number: bigint }) { | ||
| super('The database has no hash for a block the chain retains.', { | ||
| metaMessages: [ | ||
| `Block: ${number}`, | ||
| 'Seed it through `blockHashes` to execute this transaction.', | ||
| ], | ||
| }) | ||
| } | ||
| } |
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,16 @@ | ||
| import type * as Address from '../core/Address.js' | ||
| import type * as Bytes from '../core/Bytes.js' | ||
| import type * as Hex from '../core/Hex.js' | ||
|
|
||
| /** | ||
| * A transaction with its sender already recovered. | ||
| * | ||
| * evm2 takes the signer alongside a signature-stripped envelope rather than | ||
| * recovering it during execution, so the sender is supplied here. | ||
| */ | ||
| export type RecoveredTx = { | ||
| /** EIP-2718 encoded transaction. */ | ||
| envelope: Hex.Hex | Bytes.Bytes | ||
| /** Sender the signature recovers to. */ | ||
| signer: Address.Address | ||
| } |
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.
The changeset continues past its one-sentence summary into a fenced code example, so the published release entry does not follow this repository's requirement that changeset entries consist of a single past-tense sentence. Keep the summary and remove the additional block.
AGENTS.md reference: AGENTS.md:L120-L120
Useful? React with 👍 / 👎.