Skip to content

Commit

Permalink
vm/evm: add AsyncEventEmitter type
Browse files Browse the repository at this point in the history
vm/evm: fixup TransientStorage/EEI creation
  • Loading branch information
jochem-brouwer committed Jun 9, 2022
1 parent 83a9746 commit 339c581
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 16 deletions.
1 change: 1 addition & 0 deletions packages/vm/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
"@types/node": "^16.11.7",
"@types/node-dir": "^0.0.34",
"@types/tape": "^4.13.2",
"@types/async-eventemitter": "^0.2.1",
"benchmark": "^2.1.4",
"eslint": "^6.8.0",
"karma": "^6.3.2",
Expand Down
9 changes: 2 additions & 7 deletions packages/vm/src/eei/eei.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,9 @@ export default class EEI {
_transientStorage: TransientStorage
_blockchain: Blockchain

constructor(
stateManager: StateManager,
common: Common,
transientStorage: TransientStorage,
blockchain: Blockchain
) {
constructor(stateManager: StateManager, common: Common, blockchain: Blockchain) {
this._common = common
this._transientStorage = transientStorage
this._transientStorage = new TransientStorage()
this._blockchain = blockchain
this.state = new VmState({ common, stateManager })
}
Expand Down
7 changes: 4 additions & 3 deletions packages/vm/src/evm/evm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { promisify } from 'util'
import { Block } from '@ethereumjs/block'
import Blockchain from '@ethereumjs/blockchain'
import Common, { Chain, Hardfork } from '@ethereumjs/common'
const AsyncEventEmitter = require('async-eventemitter')
import AsyncEventEmitter = require('async-eventemitter')
import { debug as createDebugLogger } from 'debug'
import {
Account,
Expand All @@ -26,6 +26,7 @@ import { CustomPrecompile, getActivePrecompiles, PrecompileFunc } from './precom
import { TransientStorage } from '../state'
import {
CustomOpcode,
EVMEvents,
/*ExternalInterface,*/
/*ExternalInterfaceFactory,*/
Log,
Expand Down Expand Up @@ -232,7 +233,7 @@ export function VmErrorResult(error: VmError, gasUsed: bigint): ExecResult {
* and storing them to state (or discarding changes in case of exceptions).
* @ignore
*/
export default class EVM extends AsyncEventEmitter {
export default class EVM extends AsyncEventEmitter<EVMEvents> {
_tx?: TxContext
_block?: Block
/**
Expand Down Expand Up @@ -355,7 +356,7 @@ export default class EVM extends AsyncEventEmitter {

// We cache this promisified function as it's called from the main execution loop, and
// promisifying each time has a huge performance impact.
this._emit = promisify(this.emit.bind(this))
this._emit = <(topic: string, data: any) => Promise<void>>promisify(this.emit.bind(this))
}

async init(): Promise<void> {
Expand Down
10 changes: 9 additions & 1 deletion packages/vm/src/evm/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Block } from '@ethereumjs/block'
import { Account, Address } from 'ethereumjs-util'
import EVM from './evm'
import EVM, { NewContractEvent } from './evm'
import { InterpreterStep } from './interpreter'
import Message from './message'
import { OpHandler } from './opcodes'
import { AsyncDynamicGasHandler, SyncDynamicGasHandler } from './opcodes/gas'
Expand Down Expand Up @@ -292,3 +293,10 @@ type EVMResult = {
export interface EVMInterface {
runMessage(environment: EVMEnvironment): Promise<EVMResult>
}

export type EVMEvents = {
newContract: (data: NewContractEvent, resolve?: (result: any) => void) => void
beforeMessage: (data: Message, resolve?: (result: any) => void) => void
afterTx: (data: EVMResult, resolve?: (result: any) => void) => void
step: (data: InterpreterStep, resolve?: (result: any) => void) => void
}
9 changes: 5 additions & 4 deletions packages/vm/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ import { default as runBlock, RunBlockOpts, RunBlockResult } from './runBlock'
import { default as buildBlock, BuildBlockOpts, BlockBuilder } from './buildBlock'
import EVM from './evm/evm'
import runBlockchain from './runBlockchain'
const AsyncEventEmitter = require('async-eventemitter')
import AsyncEventEmitter = require('async-eventemitter')
import { promisify } from 'util'
import { getActivePrecompiles } from './evm/precompiles'
import EEI from './eei/eei'
import { VMEvents } from './types'

/**
* Options for instantiating a {@link VM}.
Expand Down Expand Up @@ -126,7 +127,7 @@ export interface VMOpts {
*
* This class is an AsyncEventEmitter, please consult the README to learn how to use it.
*/
export default class VM extends AsyncEventEmitter {
export default class VM extends AsyncEventEmitter<VMEvents> {
/**
* The StateManager used by the VM
*/
Expand Down Expand Up @@ -252,7 +253,7 @@ export default class VM extends AsyncEventEmitter {
if (opts.eei) {
this.eei = opts.eei
} else {
this.eei = new EEI(this.stateManager, this._common, new TransientStorage(), this.blockchain)
this.eei = new EEI(this.stateManager, this._common, this.blockchain)
}

// TODO tests
Expand Down Expand Up @@ -282,7 +283,7 @@ export default class VM extends AsyncEventEmitter {

// We cache this promisified function as it's called from the main execution loop, and
// promisifying each time has a huge performance impact.
this._emit = promisify(this.emit.bind(this))
this._emit = <(topic: string, data: any) => Promise<void>>promisify(this.emit.bind(this))
}

async init(): Promise<void> {
Expand Down
2 changes: 1 addition & 1 deletion packages/vm/src/runTx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -511,7 +511,7 @@ async function _runTx(this: VM, opts: RunTxOpts): Promise<RunTxResult> {

await state.cleanupTouchedAccounts()
state.clearOriginalStorageCache()
if (this._common.isActivatedEIP(1153)) this.evm._transientStorage.clear()
if (this._common.isActivatedEIP(1153)) this.eei._transientStorage.clear()

// Generate the tx receipt
const gasUsed = opts.blockGasUsed !== undefined ? opts.blockGasUsed : block.header.gasUsed
Expand Down
11 changes: 11 additions & 0 deletions packages/vm/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { Block } from '@ethereumjs/block'
import { TypedTransaction } from '@ethereumjs/tx'
import { Log } from './evm/types'
import { AfterBlockEvent } from './runBlock'
import { AfterTxEvent } from './runTx'

export type TxReceipt = PreByzantiumTxReceipt | PostByzantiumTxReceipt

Expand Down Expand Up @@ -41,3 +45,10 @@ export interface PostByzantiumTxReceipt extends BaseTxReceipt {
*/
status: 0 | 1
}

export type VMEvents = {
beforeBlock: (data: Block, resolve?: (result: any) => void) => void
afterBlock: (data: AfterBlockEvent, resolve?: (result: any) => void) => void
beforeTx: (data: TypedTransaction, resolve?: (result: any) => void) => void
afterTx: (data: AfterTxEvent, resolve?: (result: any) => void) => void
}

0 comments on commit 339c581

Please sign in to comment.