|
| 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