Skip to content
This repository has been archived by the owner on Apr 22, 2024. It is now read-only.

VM Subprovider fixes #35

Merged
merged 2 commits into from
Feb 17, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 23 additions & 5 deletions subproviders/vm.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ inherits(VmSubprovider, Subprovider)

function VmSubprovider(opts){
const self = this
self.opts = opts || {};
self.methods = ['eth_call', 'eth_estimateGas']
}

Expand Down Expand Up @@ -68,6 +69,13 @@ VmSubprovider.prototype.runVm = function(payload, cb){

// create vm with state lookup intercepted
var vm = self.vm = new VM()

if (self.opts.debug) {
vm.on('step', function (data) {
console.log(data.opcode.name)
})
}

vm.stateManager._lookupStorageTrie = self._createAccountStorageTrie.bind(self, blockNumber)
vm.stateManager.cache._lookupAccount = self._fetchAccount.bind(self, blockNumber)
var codeStore = new FallbackAsyncStore(function(address, cb){ self._fetchAccountCode(address, blockNumber, cb) })
Expand All @@ -92,18 +100,22 @@ VmSubprovider.prototype.runVm = function(payload, cb){
block: block,
skipNonce: !txParams.nonce,
}, function(err, results) {

if (err) {
// these errors often get gobbled up, so logging for easy debugging
console.error('VmSubprovider encountered an error running "'+payload.method+'":')
console.error(err)
if (isNormalVmError(err.message)) {
return cb(null, { error: err })
} else {
return cb(err)
}
}

if (results.error != null) {
return cb(new Error("VM error: " + results.error));
}

if (results.vm.exception != 1) {
return cb(new Error("VM Exception while executing " + payload.method + ": " + results.vm.exceptionError));
}

cb(null, results)
})

Expand Down Expand Up @@ -145,6 +157,8 @@ VmSubprovider.prototype._fetchAccountStorage = function(address, key, blockNumbe
const self = this
self.emitPayload({ method: 'eth_getStorageAt', params: [address, key, blockNumber] }, function(err, results){
if (err) return cb(err)
if (results.error) return cb(results.error.message)

cb(null, results.result)
})
}
Expand All @@ -153,6 +167,7 @@ VmSubprovider.prototype._fetchAccountBalance = function(address, blockNumber, cb
const self = this
self.emitPayload({ method: 'eth_getBalance', params: [address, blockNumber] }, function(err, results){
if (err) return cb(err)
if (results.error) return cb(results.error.message)
cb(null, results.result)
})
}
Expand All @@ -161,6 +176,7 @@ VmSubprovider.prototype._fetchAccountNonce = function(address, blockNumber, cb){
const self = this
self.emitPayload({ method: 'eth_getTransactionCount', params: [address, blockNumber] }, function(err, results){
if (err) return cb(err)
if (results.error) return cb(results.error.message);
cb(null, results.result)
})
}
Expand All @@ -169,6 +185,7 @@ VmSubprovider.prototype._fetchAccountCode = function(address, blockNumber, cb){
const self = this
self.emitPayload({ method: 'eth_getCode', params: [address, blockNumber] }, function(err, results){
if (err) return cb(err)
if (results.error) return cb(results.error.message);
cb(null, results.result)
})
}
Expand Down Expand Up @@ -234,8 +251,9 @@ FallbackAsyncStore.prototype.get = function(address, cb){
self.fetch(addressHex, function(err, value){
// console.log('FallbackAsyncStore - fetch return', arguments)
if (err) return cb(err)
value = ethUtil.toBuffer(value);
self.cache[addressHex] = value
cb(null, ethUtil.toBuffer(value))
cb(null, value)
})
}
}
Expand Down