diff --git a/src/storage/storageResolver.js b/src/storage/storageResolver.js index a288bb46e4..eb2becddf1 100644 --- a/src/storage/storageResolver.js +++ b/src/storage/storageResolver.js @@ -3,8 +3,7 @@ var traceHelper = require('../helpers/traceHelper') var util = require('../helpers/global') class StorageResolver { - constructor (_traceManager) { - this.traceManager = _traceManager + constructor () { this.storageByAddress = {} this.maxSize = 100 } @@ -15,10 +14,12 @@ class StorageResolver { * * @param {Object} - tx - transaction * @param {Int} - stepIndex - Index of the stop in the vm trace + * @param {Object} - storageChanges - contains storage changes by hashde key + * @param {String} - address - lookup address * @param {Function} - callback - contains a map: [hashedKey] = {key, hashedKey, value} */ - storageRange (tx, stepIndex, callback) { - storageRangeInternal(this, zeroSlot, tx, stepIndex, true, callback) + storageRange (tx, stepIndex, storageChanges, address, callback) { + storageRangeInternal(this, zeroSlot, tx, stepIndex, true, storageChanges, address, callback) } /** @@ -27,10 +28,12 @@ class StorageResolver { * @param {String} - slot - slot key * @param {Object} - tx - transaction * @param {Int} - stepIndex - Index of the stop in the vm trace + * @param {Object} - storageChanges - contains storage changes by hashde key + * @param {String} - address - lookup address * @param {Function} - callback - {key, hashedKey, value} - */ - storageSlot (slot, tx, stepIndex, callback) { - storageRangeInternal(this, slot, tx, stepIndex, false, function (error, storage) { + storageSlot (slot, tx, stepIndex, storageChanges, address, callback) { + storageRangeInternal(this, slot, tx, stepIndex, false, storageChanges, address, function (error, storage) { if (error) { callback(error) } else { @@ -56,39 +59,29 @@ class StorageResolver { * even if the next 1000 items are not in the cache. * - If @arg slot is not cached, the corresponding value will be resolved and the next 1000 slots. */ -function storageRangeInternal (self, slotKey, tx, stepIndex, fullStorage, callback) { - resolveAddress(self, stepIndex, (error, address) => { +function storageRangeInternal (self, slotKey, tx, stepIndex, fullStorage, storageChanges, address, callback) { + if (!fullStorage && storageChanges[slotKey]) { // don't need the full storage just returning the value from the storageChanges + return callback(null, storageChanges) + } + var cached = fromCache(self, address) + if (cached && cached.storage[slotKey]) { // we have the current slot in the cache and maybe the next 1000... + return callback(null, Object.assign({}, cached.storage, storageChanges)) + } + storageRangeWeb3Call(tx, address, slotKey, self.maxSize, (error, storage, complete) => { if (error) { return callback(error) } - self.traceManager.accumulateStorageChanges(stepIndex, address, {}, (error, storageChanges) => { - if (error) { - return callback(error) - } - if (!fullStorage && storageChanges[slotKey]) { // don't need the full storage just returning the value from the storageChanges - return callback(null, storageChanges) - } - var cached = fromCache(self, address) - if (cached && cached.storage[slotKey]) { // we have the current slot in the cache and maybe the next 1000... - return callback(null, Object.assign(cached.storage, storageChanges)) + if (!storage[slotKey]) { + storage[slotKey] = { + key: slotKey, + value: zeroSlot } - storageRangeWeb3Call(tx, address, slotKey, self.maxSize, (error, storage, complete) => { - if (error) { - return callback(error) - } - if (!storage[slotKey]) { - storage[slotKey] = { - key: slotKey, - value: zeroSlot - } - } - toCache(self, address, storage) - if (slotKey === zeroSlot && Object.keys(storage).length < self.maxSize) { - self.storageByAddress[address].complete = true - } - callback(null, Object.assign(storage, storageChanges)) - }) - }) + } + toCache(self, address, storage) + if (slotKey === zeroSlot && Object.keys(storage).length < self.maxSize) { + self.storageByAddress[address].complete = true + } + callback(null, Object.assign(storage, storageChanges)) }) } @@ -141,14 +134,4 @@ function storageRangeWeb3Call (tx, address, start, maxSize, callback) { } } -function resolveAddress (self, stepIndex, callback) { - self.traceManager.getCurrentCalledAddressAt(stepIndex, (error, result) => { - if (error) { - callback(error) - } else { - callback(null, result) - } - }) -} - module.exports = StorageResolver diff --git a/src/storage/storageViewer.js b/src/storage/storageViewer.js index a20b138d71..37b8463949 100644 --- a/src/storage/storageViewer.js +++ b/src/storage/storageViewer.js @@ -2,9 +2,16 @@ var helper = require('../helpers/util') class StorageViewer { - constructor (_context, _storageResolver) { + constructor (_context, _storageResolver, _traceManager) { this.context = _context this.storageResolver = _storageResolver + _traceManager.accumulateStorageChanges(this.context.stepIndex, this.context.address, {}, (error, storageChanges) => { + if (!error) { + this.storageChanges = storageChanges + } else { + console.log(error) + } + }) } /** @@ -14,27 +21,21 @@ class StorageViewer { * @param {Function} - callback - contains a map: [hashedKey] = {key, hashedKey, value} */ storageRange (callback) { - this.storageResolver.storageRange(this.context.tx, this.context.stepIndex, callback) + this.storageResolver.storageRange(this.context.tx, this.context.stepIndex, this.storageChanges, this.context.address, callback) } /** * return a slot value for the current context (address and vm trace index) - * + * @param {String} - slot - slot key (not hashed key!) * @param {Function} - callback - {key, hashedKey, value} - */ storageSlot (slot, callback) { - this.storageResolver.storageSlot(slot, this.context.tx, this.context.stepIndex, (error, result) => { - if (error || !result || !result[slot]) { - var hashed = helper.sha3_32(slot) - this.storageResolver.storageSlot(hashed, this.context.tx, this.context.stepIndex, (error, result) => { - if (error) { - callback(error) - } else { - callback(null, result) - } - }) + var hashed = helper.sha3_32(slot) + this.storageResolver.storageSlot(hashed, this.context.tx, this.context.stepIndex, this.storageChanges, this.context.address, (error, result) => { + if (error) { + callback(error) } else { - return callback(null, result) + callback(null, result) } }) } diff --git a/src/ui/FullStoragesChanges.js b/src/ui/FullStoragesChanges.js index f0784812a6..9c965f146c 100644 --- a/src/ui/FullStoragesChanges.js +++ b/src/ui/FullStoragesChanges.js @@ -51,8 +51,9 @@ FullStoragesChanges.prototype.init = function () { var address = self.addresses[k] var storageViewer = new StorageViewer({ stepIndex: self.parent.currentStepIndex, - tx: self.parent.tx - }, self.storageResolver) + tx: self.parent.tx, + address: address + }, self.storageResolver, self.traceManager) storageViewer.storageRange(function (error, result) { if (!error) { storageJSON[address] = result diff --git a/src/ui/SolidityLocals.js b/src/ui/SolidityLocals.js index 2d5c386b57..66c37b0728 100644 --- a/src/ui/SolidityLocals.js +++ b/src/ui/SolidityLocals.js @@ -39,7 +39,8 @@ class SolidityLocals { } this.traceManager.waterfall([ this.traceManager.getStackAt, - this.traceManager.getMemoryAt], + this.traceManager.getMemoryAt, + this.traceManager.getCurrentCalledAddressAt], this.parent.currentStepIndex, (error, result) => { if (!error) { @@ -48,8 +49,9 @@ class SolidityLocals { try { var storageViewer = new StorageViewer({ stepIndex: this.parent.currentStepIndex, - tx: this.parent.tx - }, this.storageResolver) + tx: this.parent.tx, + address: result[2].value + }, this.storageResolver, this.traceManager) localDecoder.solidityLocals(this.parent.currentStepIndex, this.internalTreeCall, stack, memory, storageViewer, sourceLocation).then((locals) => { if (!result.error) { this.basicPanel.update(locals) @@ -58,6 +60,8 @@ class SolidityLocals { } catch (e) { warningDiv.innerHTML = e.message } + } else { + console.log(error) } }) }) diff --git a/src/ui/SolidityState.js b/src/ui/SolidityState.js index 9c06c244da..ed684f1b01 100644 --- a/src/ui/SolidityState.js +++ b/src/ui/SolidityState.js @@ -49,7 +49,7 @@ SolidityState.prototype.init = function () { return } - self.traceManager.getCurrentCalledAddressAt(self.parent.currentStepIndex, (error, result) => { + self.traceManager.getCurrentCalledAddressAt(self.parent.currentStepIndex, (error, address) => { if (error) { self.basicPanel.update({}) console.log(error) @@ -61,8 +61,9 @@ SolidityState.prototype.init = function () { } else { var storageViewer = new StorageViewer({ stepIndex: self.parent.currentStepIndex, - tx: self.parent.tx - }, self.storageResolver) + tx: self.parent.tx, + address: address + }, self.storageResolver, self.traceManager) stateDecoder.decodeState(stateVars, storageViewer).then((result) => { if (!result.error) { self.basicPanel.update(result) diff --git a/src/ui/StoragePanel.js b/src/ui/StoragePanel.js index 560b8e31a5..67cb11e854 100644 --- a/src/ui/StoragePanel.js +++ b/src/ui/StoragePanel.js @@ -24,18 +24,19 @@ StoragePanel.prototype.init = function () { if (self.parent.currentStepIndex !== index) return if (!self.storageResolver) return - var storageViewer = new StorageViewer({ - stepIndex: self.parent.currentStepIndex, - tx: self.parent.tx - }, self.storageResolver) + this.traceManager.getCurrentCalledAddressAt(index, (error, address) => { + if (!error) { + var storageViewer = new StorageViewer({ + stepIndex: self.parent.currentStepIndex, + tx: self.parent.tx, + address: address + }, self.storageResolver, self.traceManager) - storageViewer.storageRange((error, storage) => { - if (error) { - console.log(error) - self.basicPanel.update({}) - } else if (self.parent.currentStepIndex === index) { - this.traceManager.getCurrentCalledAddressAt(index, (error, address) => { - if (!error) { + storageViewer.storageRange((error, storage) => { + if (error) { + console.log(error) + self.basicPanel.update({}) + } else if (self.parent.currentStepIndex === index) { var header = storageViewer.isComplete(address) ? 'completely loaded' : 'partially loaded...' self.basicPanel.update(storage, header) } diff --git a/src/ui/VmDebugger.js b/src/ui/VmDebugger.js index 04ddf954e0..c22aea4136 100644 --- a/src/ui/VmDebugger.js +++ b/src/ui/VmDebugger.js @@ -44,7 +44,7 @@ function VmDebugger (_parent, _traceManager, _codeManager, _solidityProxy, _call this.view var self = this _parent.event.register('newTraceLoaded', this, function () { - var storageResolver = new StorageResolver(_traceManager) + var storageResolver = new StorageResolver() self.storagePanel.storageResolver = storageResolver self.solidityState.storageResolver = storageResolver self.solidityLocals.storageResolver = storageResolver