From 5baf1fd9dccf3aaa4da635a426a03aea4bccbb54 Mon Sep 17 00:00:00 2001 From: Sina Mahmoodi Date: Thu, 16 May 2019 10:24:03 +0200 Subject: [PATCH 1/2] Change runCall filetype to ts --- lib/{runCall.js => runCall.ts} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename lib/{runCall.js => runCall.ts} (100%) diff --git a/lib/runCall.js b/lib/runCall.ts similarity index 100% rename from lib/runCall.js rename to lib/runCall.ts From aa87896e8b41d51b3c18e5cde289f46717ba10bc Mon Sep 17 00:00:00 2001 From: Sina Mahmoodi Date: Thu, 16 May 2019 10:24:13 +0200 Subject: [PATCH 2/2] Migrate runCall to ts --- lib/index.ts | 6 +++++- lib/runCall.ts | 53 ++++++++++++++++++++++++++++++++++++-------------- 2 files changed, 43 insertions(+), 16 deletions(-) diff --git a/lib/index.ts b/lib/index.ts index 783cccd50c..90999ed4b8 100644 --- a/lib/index.ts +++ b/lib/index.ts @@ -3,6 +3,7 @@ import { StateManager } from './state' import Common from 'ethereumjs-common' import Account from 'ethereumjs-account' import { default as runCode, RunCodeOpts, RunCodeCb } from './runCode' +import { default as runCall, RunCallOpts, RunCallCb } from './runCall' const promisify = require('util.promisify') const AsyncEventEmitter = require('async-eventemitter') const Blockchain = require('ethereumjs-blockchain') @@ -71,10 +72,13 @@ export default class VM extends AsyncEventEmitter { this.runJIT = require('./runJit.js').bind(this) this.runBlock = require('./runBlock.js').bind(this) this.runTx = require('./runTx.js').bind(this) - this.runCall = require('./runCall.js').bind(this) this.runBlockchain = require('./runBlockchain.js').bind(this) } + runCall (opts: RunCallOpts, cb: RunCallCb): void { + runCall.bind(this)(opts, cb) + } + runCode (opts: RunCodeOpts, cb: RunCodeCb): void { runCode.bind(this)(opts, cb) } diff --git a/lib/runCall.ts b/lib/runCall.ts index 518f4b61b5..396abc280f 100644 --- a/lib/runCall.ts +++ b/lib/runCall.ts @@ -1,10 +1,34 @@ -const ethUtil = require('ethereumjs-util') +import BN = require('bn.js') +import { zeros } from 'ethereumjs-util' +import VM from './index' +import { StorageReader } from './state' +import TxContext from './evm/txContext' +import Message from './evm/message' +import { default as Interpreter, InterpreterResult } from './evm/interpreter' const Block = require('ethereumjs-block') -const BN = ethUtil.BN -const { StorageReader } = require('./state') -const TxContext = require('./evm/txContext').default -const Message = require('./evm/message').default -const Interpreter = require('./evm/interpreter').default + +export interface RunCallOpts { + block?: any + storageReader?: StorageReader + gasPrice?: Buffer + origin?: Buffer + caller?: Buffer + gasLimit?: Buffer + to?: Buffer + value?: Buffer + data?: Buffer + code?: Buffer + depth?: number + compiled?: boolean + static?: boolean + salt?: Buffer + selfdestruct?: {[k: string]: boolean} + delegatecall?: boolean +} + +export interface RunCallCb { + (err: Error | null, results: InterpreterResult | null): void +} /** * runs a CALL operation @@ -22,11 +46,11 @@ const Interpreter = require('./evm/interpreter').default * @param opts.value {Buffer} * @param {Function} cb the callback */ -module.exports = function (opts, cb) { +export default function runCall (this: VM, opts: RunCallOpts, cb: RunCallCb): void { const block = opts.block || new Block() const storageReader = opts.storageReader || new StorageReader(this.stateManager) - const txContext = new TxContext(opts.gasPrice, opts.origin || opts.caller) + const txContext = new TxContext(opts.gasPrice || Buffer.alloc(0), opts.origin || opts.caller || zeros(32)) const message = new Message({ caller: opts.caller, gasLimit: opts.gasLimit ? new BN(opts.gasLimit) : new BN(0xffffff), @@ -34,13 +58,12 @@ module.exports = function (opts, cb) { value: opts.value, data: opts.data, code: opts.code, - depth: opts.depth, - isCompiled: opts.compiled, - isStatic: opts.static, - salt: opts.salt, - // opts.suicides is kept for backward compatiblity with pre-EIP6 syntax - selfdestruct: opts.selfdestruct || opts.suicides, - delegatecall: opts.delegatecall + depth: opts.depth || 0, + isCompiled: opts.compiled || false, + isStatic: opts.static || false, + salt: opts.salt || null, + selfdestruct: opts.selfdestruct || {}, + delegatecall: opts.delegatecall || false }) const interpreter = new Interpreter(this, txContext, block, storageReader)