Skip to content
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

Migrate runCall to typescript #510

Merged
merged 2 commits into from
May 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 5 additions & 1 deletion lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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)
}
Expand Down
50 changes: 0 additions & 50 deletions lib/runCall.js

This file was deleted.

73 changes: 73 additions & 0 deletions lib/runCall.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
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')

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
* @method vm.runCall
* @private
* @param opts
* @param opts.block {Block}
* @param opts.caller {Buffer}
* @param opts.code {Buffer} this is for CALLCODE where the code to load is different than the code from the to account.
* @param opts.data {Buffer}
* @param opts.gasLimit {Buffer | BN.js }
* @param opts.gasPrice {Buffer}
* @param opts.origin {Buffer} []
* @param opts.to {Buffer}
* @param opts.value {Buffer}
* @param {Function} cb the callback
*/
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 || 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),
to: opts.to && opts.to.toString('hex') !== '' ? opts.to : undefined,
value: opts.value,
data: opts.data,
code: opts.code,
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)
interpreter.executeMessage(message)
.then((results) => cb(null, results))
.catch((err) => cb(err, null))
}