Skip to content

Commit acd56bf

Browse files
authored
Migrate runCall to typescript (ethereumjs#510)
* Change runCall filetype to ts * Migrate runCall to ts
1 parent aa859a2 commit acd56bf

File tree

3 files changed

+78
-51
lines changed

3 files changed

+78
-51
lines changed

lib/index.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { StateManager } from './state'
33
import Common from 'ethereumjs-common'
44
import Account from 'ethereumjs-account'
55
import { default as runCode, RunCodeOpts, RunCodeCb } from './runCode'
6+
import { default as runCall, RunCallOpts, RunCallCb } from './runCall'
67
const promisify = require('util.promisify')
78
const AsyncEventEmitter = require('async-eventemitter')
89
const Blockchain = require('ethereumjs-blockchain')
@@ -71,10 +72,13 @@ export default class VM extends AsyncEventEmitter {
7172
this.runJIT = require('./runJit.js').bind(this)
7273
this.runBlock = require('./runBlock.js').bind(this)
7374
this.runTx = require('./runTx.js').bind(this)
74-
this.runCall = require('./runCall.js').bind(this)
7575
this.runBlockchain = require('./runBlockchain.js').bind(this)
7676
}
7777

78+
runCall (opts: RunCallOpts, cb: RunCallCb): void {
79+
runCall.bind(this)(opts, cb)
80+
}
81+
7882
runCode (opts: RunCodeOpts, cb: RunCodeCb): void {
7983
runCode.bind(this)(opts, cb)
8084
}

lib/runCall.js

Lines changed: 0 additions & 50 deletions
This file was deleted.

lib/runCall.ts

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import BN = require('bn.js')
2+
import { zeros } from 'ethereumjs-util'
3+
import VM from './index'
4+
import { StorageReader } from './state'
5+
import TxContext from './evm/txContext'
6+
import Message from './evm/message'
7+
import { default as Interpreter, InterpreterResult } from './evm/interpreter'
8+
const Block = require('ethereumjs-block')
9+
10+
export interface RunCallOpts {
11+
block?: any
12+
storageReader?: StorageReader
13+
gasPrice?: Buffer
14+
origin?: Buffer
15+
caller?: Buffer
16+
gasLimit?: Buffer
17+
to?: Buffer
18+
value?: Buffer
19+
data?: Buffer
20+
code?: Buffer
21+
depth?: number
22+
compiled?: boolean
23+
static?: boolean
24+
salt?: Buffer
25+
selfdestruct?: {[k: string]: boolean}
26+
delegatecall?: boolean
27+
}
28+
29+
export interface RunCallCb {
30+
(err: Error | null, results: InterpreterResult | null): void
31+
}
32+
33+
/**
34+
* runs a CALL operation
35+
* @method vm.runCall
36+
* @private
37+
* @param opts
38+
* @param opts.block {Block}
39+
* @param opts.caller {Buffer}
40+
* @param opts.code {Buffer} this is for CALLCODE where the code to load is different than the code from the to account.
41+
* @param opts.data {Buffer}
42+
* @param opts.gasLimit {Buffer | BN.js }
43+
* @param opts.gasPrice {Buffer}
44+
* @param opts.origin {Buffer} []
45+
* @param opts.to {Buffer}
46+
* @param opts.value {Buffer}
47+
* @param {Function} cb the callback
48+
*/
49+
export default function runCall (this: VM, opts: RunCallOpts, cb: RunCallCb): void {
50+
const block = opts.block || new Block()
51+
const storageReader = opts.storageReader || new StorageReader(this.stateManager)
52+
53+
const txContext = new TxContext(opts.gasPrice || Buffer.alloc(0), opts.origin || opts.caller || zeros(32))
54+
const message = new Message({
55+
caller: opts.caller,
56+
gasLimit: opts.gasLimit ? new BN(opts.gasLimit) : new BN(0xffffff),
57+
to: opts.to && opts.to.toString('hex') !== '' ? opts.to : undefined,
58+
value: opts.value,
59+
data: opts.data,
60+
code: opts.code,
61+
depth: opts.depth || 0,
62+
isCompiled: opts.compiled || false,
63+
isStatic: opts.static || false,
64+
salt: opts.salt || null,
65+
selfdestruct: opts.selfdestruct || {},
66+
delegatecall: opts.delegatecall || false
67+
})
68+
69+
const interpreter = new Interpreter(this, txContext, block, storageReader)
70+
interpreter.executeMessage(message)
71+
.then((results) => cb(null, results))
72+
.catch((err) => cb(err, null))
73+
}

0 commit comments

Comments
 (0)