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

Subdivide runState, refactor loop #498

Merged
merged 11 commits into from
Apr 25, 2019
13 changes: 2 additions & 11 deletions lib/evm/interpreter.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,17 +67,8 @@ module.exports = class Interpreter {
const opts = {
storageReader: this._storageReader,
block: this._block,
gasPrice: this._tx.gasPrice,
origin: this._tx.origin,
code: message.code,
data: message.data,
gasLimit: message.gasLimit,
address: message.to,
caller: message.caller,
value: message.value,
depth: message.depth,
selfdestruct: message.selfdestruct,
isStatic: message.isStatic
txContext: this._tx,
message
}

// Run code
Expand Down
31 changes: 15 additions & 16 deletions lib/evm/loop.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,8 @@ module.exports = class Loop {
}

async run (opts) {
// opts.suicides is kept for backward compatiblity with pre-EIP6 syntax
if (opts.selfdestruct || opts.suicides) {
this._result.selfdestruct = opts.selfdestruct || opts.suicides
if (opts.message.selfdestruct) {
this._result.selfdestruct = opts.message.selfdestruct
}

// Initialize internal vm state
Expand All @@ -54,7 +53,7 @@ module.exports = class Loop {
}
}

return this.parseVmResults(runState, err, opts.gasLimit)
return this.parseVmResults(runState, err, opts.message.gasLimit)
}

canContinueExecution (runState) {
Expand All @@ -64,16 +63,16 @@ module.exports = class Loop {

async initRunState (opts) {
const runState = {
code: opts.code,
code: opts.message.code,
stopped: false,
programCounter: opts.pc | 0,
opCode: undefined,
opName: undefined,
gasLeft: new BN(opts.gasLimit),
gasLeft: new BN(opts.message.gasLimit),
memory: new Memory(),
memoryWordCount: new BN(0),
stack: new Stack(),
validJumps: this._getValidJumpDests(opts.code),
validJumps: this._getValidJumpDests(opts.message.code),
highestMemCost: new BN(0),
lastReturned: [],
// TODO: Replace with EEI methods
Expand All @@ -84,15 +83,15 @@ module.exports = class Loop {

const env = {
blockchain: this._vm.blockchain, // Only used in BLOCKHASH
address: opts.address || utils.zeros(32),
caller: opts.caller || utils.zeros(32),
callData: opts.data || Buffer.from([0]),
callValue: opts.value || new BN(0),
code: opts.code,
isStatic: opts.isStatic || false,
depth: opts.depth || 0,
gasPrice: opts.gasPrice,
origin: opts.origin || opts.caller || utils.zeros(32),
address: opts.message.to || utils.zeros(32),
caller: opts.message.caller || utils.zeros(32),
callData: opts.message.data || Buffer.from([0]),
callValue: opts.message.value || new BN(0),
code: opts.message.code,
isStatic: opts.message.isStatic || false,
depth: opts.message.depth || 0,
gasPrice: opts.txContext.gasPrice,
origin: opts.txContext.origin || opts.message.caller || utils.zeros(32),
block: opts.block || new Block()
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cleaner to have this encapsulated in message and txContext, good. 👍


Expand Down
24 changes: 21 additions & 3 deletions lib/runCode.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ item length then you must use utils.pad(<item>, 32) first.
const Block = require('ethereumjs-block')
const Loop = require('./evm/loop')
const TxContext = require('./evm/txContext').default
const Message = require('./evm/message').default
const Interpreter = require('./evm/interpreter')
const { StorageReader } = require('./state')

Expand Down Expand Up @@ -57,16 +58,33 @@ module.exports = function (opts, cb) {
opts.storageReader = new StorageReader(this.stateManager)
}

// Backwards compatibility
if (!opts.txContext) {
opts.txContext = new TxContext(opts.gasPrice, opts.origin || opts.caller)
}
if (!opts.message) {
opts.message = new Message({
code: opts.code,
data: opts.data,
gasLimit: opts.gasLimit,
to: opts.address,
caller: opts.caller,
value: opts.value,
depth: opts.depth,
selfdestruct: opts.selfdestruct,
isStatic: opts.isStatic
})
}

let interpreter = opts.interpreter
if (!interpreter) {
const txContext = new TxContext(opts.gasPrice, opts.origin || opts.caller)
interpreter = new Interpreter(this, txContext, opts.block, opts.storageReader)
interpreter = new Interpreter(this, opts.txContext, opts.block, opts.storageReader)
}

opts.txRunState = interpreter.txRunState

const loop = new Loop(this, interpreter)
loop.run(opts)
.then((res) => cb(res.err, res.results))
.then((res) => cb(res.exceptionError, res))
.catch((err) => cb(err, null))
}