Skip to content

Commit

Permalink
vm: refactor runCallOpts and runCodeOpts to types and re-add their do…
Browse files Browse the repository at this point in the history
…cs. Order imports in evm.ts
  • Loading branch information
gabrocheleau committed May 16, 2022
1 parent 1a8f1a9 commit ee7831f
Show file tree
Hide file tree
Showing 3 changed files with 162 additions and 93 deletions.
110 changes: 18 additions & 92 deletions packages/vm/src/evm/evm.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import { debug as createDebugLogger } from 'debug'
import { promisify } from 'util'

import { Block } from '@ethereumjs/block'
import Blockchain from '@ethereumjs/blockchain'
import Common, { Chain, Hardfork } from '@ethereumjs/common'
import { DefaultStateManager } from '@ethereumjs/statemanager'
const AsyncEventEmitter = require('async-eventemitter')
import { debug as createDebugLogger } from 'debug'
import {
Account,
Address,
Expand All @@ -10,26 +16,19 @@ import {
MAX_INTEGER,
short,
} from 'ethereumjs-util'
import { Block } from '@ethereumjs/block'
import { Hardfork } from '@ethereumjs/common'
import { SecureTrie as Trie } from 'merkle-patricia-tree'

import EEI from './eei'
import { ERROR, VmError } from '../exceptions'
import { CustomPrecompile, getActivePrecompiles, PrecompileFunc } from './precompiles'
import { default as Interpreter, InterpreterOpts, RunState } from './interpreter'
import Message, { MessageWithTo } from './message'
import EEI from './eei'
// eslint-disable-next-line
import * as eof from './opcodes/eof'
import { CustomOpcode, Log, TxContext } from './types'
import { default as Interpreter, InterpreterOpts, RunState } from './interpreter'
import Common, { Chain } from '@ethereumjs/common'
import { promisify } from 'util'
import { getOpcodesForHF, OpcodeList, OpHandler } from './opcodes'
import { AsyncDynamicGasHandler, SyncDynamicGasHandler } from './opcodes/gas'
import Blockchain from '@ethereumjs/blockchain'
import { SecureTrie as Trie } from 'merkle-patricia-tree'
import { CustomPrecompile, getActivePrecompiles, PrecompileFunc } from './precompiles'
import { TransientStorage } from '../state'
import { CustomOpcode, Log, RunCallOpts, RunCodeOpts, TxContext } from './types'
import { VmState } from '../vmState'
import { DefaultStateManager } from '@ethereumjs/statemanager'

const debug = createDebugLogger('vm:evm')
const debugGas = createDebugLogger('vm:evm:gas')
Expand Down Expand Up @@ -177,87 +176,12 @@ export interface ExecResult {
selfdestruct?: { [k: string]: Buffer }
}

/**
* Options for running a call (or create) operation
*/
export interface RunCallOpts {
block?: Block
gasPrice?: bigint
origin?: Address
caller?: Address
gasLimit?: bigint
to?: Address
value?: bigint
data?: Buffer
/**
* This is for CALLCODE where the code to load is different than the code from the `opts.to` address.
*/
code?: Buffer
depth?: number
compiled?: boolean
static?: boolean
salt?: Buffer
selfdestruct?: { [k: string]: boolean } | { [k: string]: Buffer }
delegatecall?: boolean
skipBalance?: boolean
message?: Message
}

export interface NewContractEvent {
address: Address
// The deployment code
code: Buffer
}

/**
* Options for the {@link runCode} method.
*/
export interface RunCodeOpts {
/**
* The `@ethereumjs/block` the `tx` belongs to. If omitted a default blank block will be used.
*/
block?: Block
evm?: EVM
txContext?: TxContext
gasPrice?: bigint
/**
* The address where the call originated from. Defaults to the zero address.
*/
origin?: Address
message?: MessageWithTo
/**
* The address that ran this code (`msg.sender`). Defaults to the zero address.
*/
caller?: Address
/**
* The EVM code to run
*/
code?: Buffer
/**
* The input data
*/
data?: Buffer
/**
* Gas limit
*/
gasLimit: bigint
/**
* The value in ether that is being sent to `opt.address`. Defaults to `0`
*/
value?: bigint
depth?: number
isStatic?: boolean
selfdestruct?: { [k: string]: boolean }
/**
* The address of the account that is executing this code (`address(this)`). Defaults to the zero address.
*/
address?: Address
/**
* The initial program counter. Defaults to `0`
*/
pc?: number
}

export function OOGResult(gasLimit: bigint): ExecResult {
return {
returnValue: Buffer.alloc(0),
Expand Down Expand Up @@ -839,8 +763,8 @@ export default class EVM extends AsyncEventEmitter {
data: opts.data,
code: opts.code,
depth: opts.depth,
isCompiled: opts.compiled,
isStatic: opts.static,
isCompiled: opts.isCompiled,
isStatic: opts.isStatic,
salt: opts.salt,
selfdestruct: opts.selfdestruct ?? {},
delegatecall: opts.delegatecall,
Expand Down Expand Up @@ -929,14 +853,16 @@ export default class EVM extends AsyncEventEmitter {
}

/**
* @ignore
* Bound to the global VM and therefore
* shouldn't be used directly from the evm class
* @hidden
*/
async runCode(opts: RunCodeOpts): Promise<ExecResult> {
const block = opts.block ?? Block.fromBlockData({}, { common: this._common })
this._block = block

// Backwards compatibility
const txContext: TxContext = opts.txContext ?? {
const txContext: TxContext = {
gasPrice: opts.gasPrice ?? BigInt(0),
origin: opts.origin ?? opts.caller ?? Address.zero(),
}
Expand Down
143 changes: 143 additions & 0 deletions packages/vm/src/evm/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { Block } from '@ethereumjs/block'
import { Address } from 'ethereumjs-util'
import EVM from './evm'
import Message, { MessageWithTo } from './message'
import { OpHandler } from './opcodes'
import { AsyncDynamicGasHandler, SyncDynamicGasHandler } from './opcodes/gas'

Expand All @@ -21,6 +24,146 @@ export type AddOpcode = {

export type CustomOpcode = AddOpcode | DeleteOpcode

/**
* Options for running a call (or create) operation
*/
export interface RunCallOpts {
/**
* The `@ethereumjs/block` the `tx` belongs to. If omitted a default blank block will be used.
*/
block?: Block
/**
* The gas price for the call. Defaults to `0`
*/
gasPrice?: bigint
/**
* The address where the call originated from. Defaults to the zero address.
*/
origin?: Address
/**
* The address that ran this code (`msg.sender`). Defaults to the zero address.
*/
caller?: Address
/**
* The gas limit for the call. Defaults to `0xffffff`
*/
gasLimit?: bigint
/**
* The to address. Defaults to the zero address.
*/
to?: Address
/**
* The value in ether that is being sent to `opts.to`. Defaults to `0`
*/
value?: bigint
/**
* The data for the call.
*/
data?: Buffer
/**
* This is for CALLCODE where the code to load is different than the code from the `opts.to` address.
*/
code?: Buffer
/**
* The call depth. Defaults to `0`
*/
depth?: number
/**
* If the code location is a precompile.
*/
isCompiled?: boolean
/**
* If the call should be executed statically. Defaults to false.
*/
isStatic?: boolean
/**
* An optional salt to pass to CREATE2.
*/
salt?: Buffer
/**
* Addresses to selfdestruct. Defaults to none.
*/
selfdestruct?: { [k: string]: boolean }
/**
* Skip balance checks if true. Adds transaction value to balance to ensure execution doesn't fail.
*/
skipBalance?: boolean
/**
* If the call is a DELEGATECALL. Defaults to false.
*/
delegatecall?: boolean
/**
* Optionally pass in an already-built message.
*/
message?: Message
}

/**
* Options for the runCode method.
*/
export interface RunCodeOpts {
/**
* The `@ethereumjs/block` the `tx` belongs to. If omitted a default blank block will be used.
*/
block?: Block
/**
* Pass a custom {@link EVM} to use. If omitted the default {@link EVM} will be used.
*/
evm?: EVM
/**
* The gas price for the call. Defaults to `0`
*/
gasPrice?: bigint
/**
* The address where the call originated from. Defaults to the zero address.
*/
origin?: Address
/**
* The address that ran this code (`msg.sender`). Defaults to the zero address.
*/
caller?: Address
/**
* The EVM code to run.
*/
code?: Buffer
/**
* The input data.
*/
data?: Buffer
/**
* The gas limit for the call.
*/
gasLimit: bigint
/**
* The value in ether that is being sent to `opts.address`. Defaults to `0`
*/
value?: bigint
/**
* The call depth. Defaults to `0`
*/
depth?: number
/**
* If the call should be executed statically. Defaults to false.
*/
isStatic?: boolean
/**
* Addresses to selfdestruct. Defaults to none.
*/
selfdestruct?: { [k: string]: boolean }
/**
* The address of the account that is executing this code (`address(this)`). Defaults to the zero address.
*/
address?: Address
/**
* The initial program counter. Defaults to `0`
*/
pc?: number
/**
* Optionally pass in an already-built message
*/
message?: MessageWithTo
}

/**
* Tx context for vm execution
*/
Expand Down
2 changes: 1 addition & 1 deletion packages/vm/tests/api/runCall.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Account, Address, MAX_UINT64, padToEven } from 'ethereumjs-util'
import Common, { Chain, Hardfork } from '@ethereumjs/common'
import VM from '../../src'
import { ERROR } from '../../src/exceptions'
import { RunCallOpts } from '../../src/evm/evm'
import { RunCallOpts } from '../../src/evm/types'

// Non-protected Create2Address generator. Does not check if buffers have the right padding.
function create2address(sourceAddress: Address, codeHash: Buffer, salt: Buffer): Address {
Expand Down

0 comments on commit ee7831f

Please sign in to comment.