Commit 7aa65f86 authored by Iuri Matias's avatar Iuri Matias

remove global web3 object from remix-core

parent f71916c5
...@@ -3,7 +3,7 @@ var remixLib = require('remix-lib') ...@@ -3,7 +3,7 @@ var remixLib = require('remix-lib')
var EventManager = remixLib.EventManager var EventManager = remixLib.EventManager
var traceHelper = remixLib.helpers.trace var traceHelper = remixLib.helpers.trace
var SourceMappingDecoder = remixLib.SourceMappingDecoder var SourceMappingDecoder = remixLib.SourceMappingDecoder
var codeResolver = require('./codeResolver') var CodeResolver = require('./codeResolver')
/* /*
resolve contract code referenced by vmtrace in order to be used by asm listview. resolve contract code referenced by vmtrace in order to be used by asm listview.
...@@ -16,7 +16,7 @@ function CodeManager (_traceManager) { ...@@ -16,7 +16,7 @@ function CodeManager (_traceManager) {
this.event = new EventManager() this.event = new EventManager()
this.isLoading = false this.isLoading = false
this.traceManager = _traceManager this.traceManager = _traceManager
this.codeResolver = codeResolver this.codeResolver = new CodeResolver({web3: this.traceManager.web3})
} }
/** /**
...@@ -57,12 +57,13 @@ CodeManager.prototype.resolveStep = function (stepIndex, tx) { ...@@ -57,12 +57,13 @@ CodeManager.prototype.resolveStep = function (stepIndex, tx) {
* @param {Function} cb - callback function, return the bytecode * @param {Function} cb - callback function, return the bytecode
*/ */
CodeManager.prototype.getCode = function (address, cb) { CodeManager.prototype.getCode = function (address, cb) {
const self = this
if (traceHelper.isContractCreation(address)) { if (traceHelper.isContractCreation(address)) {
var codes = codeResolver.getExecutingCodeFromCache(address) var codes = this.codeResolver.getExecutingCodeFromCache(address)
if (!codes) { if (!codes) {
this.traceManager.getContractCreationCode(address, function (error, hexCode) { this.traceManager.getContractCreationCode(address, function (error, hexCode) {
if (!error) { if (!error) {
codes = codeResolver.cacheExecutingCode(address, hexCode) codes = self.codeResolver.cacheExecutingCode(address, hexCode)
cb(null, codes) cb(null, codes)
} }
}) })
...@@ -70,7 +71,7 @@ CodeManager.prototype.getCode = function (address, cb) { ...@@ -70,7 +71,7 @@ CodeManager.prototype.getCode = function (address, cb) {
cb(null, codes) cb(null, codes)
} }
} else { } else {
codeResolver.resolveCode(address, function (address, code) { this.codeResolver.resolveCode(address, function (address, code) {
cb(null, code) cb(null, code)
}) })
} }
...@@ -111,12 +112,13 @@ CodeManager.prototype.getFunctionFromStep = function (stepIndex, sourceMap, ast) ...@@ -111,12 +112,13 @@ CodeManager.prototype.getFunctionFromStep = function (stepIndex, sourceMap, ast)
* @param {Function} callback - instruction index * @param {Function} callback - instruction index
*/ */
CodeManager.prototype.getInstructionIndex = function (address, step, callback) { CodeManager.prototype.getInstructionIndex = function (address, step, callback) {
const self = this
this.traceManager.getCurrentPC(step, function (error, pc) { this.traceManager.getCurrentPC(step, function (error, pc) {
if (error) { if (error) {
console.log(error) console.log(error)
callback('Cannot retrieve current PC for ' + step, null) callback('Cannot retrieve current PC for ' + step, null)
} else { } else {
var itemIndex = codeResolver.getInstructionIndex(address, pc) var itemIndex = self.codeResolver.getInstructionIndex(address, pc)
callback(null, itemIndex) callback(null, itemIndex)
} }
}) })
...@@ -132,7 +134,7 @@ CodeManager.prototype.getInstructionIndex = function (address, step, callback) { ...@@ -132,7 +134,7 @@ CodeManager.prototype.getInstructionIndex = function (address, step, callback) {
* @return {Object} return the ast node of the function * @return {Object} return the ast node of the function
*/ */
CodeManager.prototype.getFunctionFromPC = function (address, pc, sourceMap, ast) { CodeManager.prototype.getFunctionFromPC = function (address, pc, sourceMap, ast) {
var instIndex = codeResolver.getInstructionIndex(address, pc) var instIndex = this.codeResolver.getInstructionIndex(address, pc)
return SourceMappingDecoder.findNodeAtInstructionIndex('FunctionDefinition', instIndex, sourceMap, ast) return SourceMappingDecoder.findNodeAtInstructionIndex('FunctionDefinition', instIndex, sourceMap, ast)
} }
......
'use strict' 'use strict'
var codeUtils = require('./codeUtils') var codeUtils = require('./codeUtils')
var remixLib = require('remix-lib')
var global = remixLib.global
module.exports = { function CodeResolver (options) {
bytecodeByAddress: {}, // bytes code by contract addesses this.web3 = options.web3
instructionsByAddress: {}, // assembly items instructions list by contract addesses
instructionsIndexByBytesOffset: {}, // mapping between bytes offset and instructions index.
clear: function () { this.bytecodeByAddress = {} // bytes code by contract addesses
this.instructionsByAddress = {} // assembly items instructions list by contract addesses
this.instructionsIndexByBytesOffset = {} // mapping between bytes offset and instructions index.
}
CodeResolver.prototype.clear = function () {
this.bytecodeByAddress = {} this.bytecodeByAddress = {}
this.instructionsByAddress = {} this.instructionsByAddress = {}
this.instructionsIndexByBytesOffset = {} this.instructionsIndexByBytesOffset = {}
}, }
resolveCode: function (address, callBack) { CodeResolver.prototype.resolveCode = function (address, callBack) {
var cache = this.getExecutingCodeFromCache(address) var cache = this.getExecutingCodeFromCache(address)
if (cache) { if (cache) {
callBack(address, cache) callBack(address, cache)
...@@ -25,36 +26,36 @@ module.exports = { ...@@ -25,36 +26,36 @@ module.exports = {
this.loadCode(address, function (code) { this.loadCode(address, function (code) {
callBack(address, self.cacheExecutingCode(address, code)) callBack(address, self.cacheExecutingCode(address, code))
}) })
}, }
loadCode: function (address, callback) { CodeResolver.prototype.loadCode = function (address, callback) {
console.log('loading new code from web3 ' + address) console.log('loading new code from web3 ' + address)
global.web3.eth.getCode(address, function (error, result) { this.web3.eth.getCode(address, function (error, result) {
if (error) { if (error) {
console.log(error) console.log(error)
} else { } else {
callback(result) callback(result)
} }
}) })
}, }
cacheExecutingCode: function (address, hexCode) { CodeResolver.prototype.cacheExecutingCode = function (address, hexCode) {
var codes = this.formatCode(hexCode) var codes = this.formatCode(hexCode)
this.bytecodeByAddress[address] = hexCode this.bytecodeByAddress[address] = hexCode
this.instructionsByAddress[address] = codes.code this.instructionsByAddress[address] = codes.code
this.instructionsIndexByBytesOffset[address] = codes.instructionsIndexByBytesOffset this.instructionsIndexByBytesOffset[address] = codes.instructionsIndexByBytesOffset
return this.getExecutingCodeFromCache(address) return this.getExecutingCodeFromCache(address)
}, }
formatCode: function (hexCode) { CodeResolver.prototype.formatCode = function (hexCode) {
var code = codeUtils.nameOpCodes(new Buffer(hexCode.substring(2), 'hex')) var code = codeUtils.nameOpCodes(new Buffer(hexCode.substring(2), 'hex'))
return { return {
code: code[0], code: code[0],
instructionsIndexByBytesOffset: code[1] instructionsIndexByBytesOffset: code[1]
} }
}, }
getExecutingCodeFromCache: function (address) { CodeResolver.prototype.getExecutingCodeFromCache = function (address) {
if (this.instructionsByAddress[address]) { if (this.instructionsByAddress[address]) {
return { return {
instructions: this.instructionsByAddress[address], instructions: this.instructionsByAddress[address],
...@@ -64,9 +65,10 @@ module.exports = { ...@@ -64,9 +65,10 @@ module.exports = {
} else { } else {
return null return null
} }
}, }
getInstructionIndex: function (address, pc) { CodeResolver.prototype.getInstructionIndex = function (address, pc) {
return this.getExecutingCodeFromCache(address).instructionsIndexByBytesOffset[pc] return this.getExecutingCodeFromCache(address).instructionsIndexByBytesOffset[pc]
}
} }
module.exports = CodeResolver
var remixLib = require('remix-lib')
var global = remixLib.global
module.exports = { module.exports = {
decodeMappingsKeys: decodeMappingsKeys decodeMappingsKeys: decodeMappingsKeys
...@@ -13,12 +11,12 @@ module.exports = { ...@@ -13,12 +11,12 @@ module.exports = {
* @param {Function} callback - calback * @param {Function} callback - calback
* @return {Map} - solidity mapping location (e.g { "<mapping_slot>" : { "<mapping-key1>": preimageOf1 }, { "<mapping-key2>": preimageOf2 }, ... }) * @return {Map} - solidity mapping location (e.g { "<mapping_slot>" : { "<mapping-key1>": preimageOf1 }, { "<mapping-key2>": preimageOf2 }, ... })
*/ */
async function decodeMappingsKeys (storage, callback) { async function decodeMappingsKeys (web3, storage, callback) {
var ret = {} var ret = {}
for (var hashedLoc in storage) { for (var hashedLoc in storage) {
var preimage var preimage
try { try {
preimage = await getPreimage(storage[hashedLoc].key) preimage = await getPreimage(web3, storage[hashedLoc].key)
} catch (e) { } catch (e) {
} }
if (preimage) { if (preimage) {
...@@ -42,9 +40,9 @@ async function decodeMappingsKeys (storage, callback) { ...@@ -42,9 +40,9 @@ async function decodeMappingsKeys (storage, callback) {
* @param {String} key - key to retrieve the preimage of * @param {String} key - key to retrieve the preimage of
* @return {String} - preimage of the given key * @return {String} - preimage of the given key
*/ */
function getPreimage (key) { function getPreimage (web3, key) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
global.web3Debug.debug.preimage(key.indexOf('0x') === 0 ? key : '0x' + key, function (error, preimage) { web3.debug.preimage(key.indexOf('0x') === 0 ? key : '0x' + key, function (error, preimage) {
if (error) { if (error) {
resolve(null) resolve(null)
} else { } else {
......
'use strict' 'use strict'
var remixLib = require('remix-lib') var remixLib = require('remix-lib')
var traceHelper = remixLib.helpers.trace var traceHelper = remixLib.helpers.trace
var global = remixLib.global
var mappingPreimages = require('./mappingPreimages') var mappingPreimages = require('./mappingPreimages')
/** /**
...@@ -9,10 +8,12 @@ var mappingPreimages = require('./mappingPreimages') ...@@ -9,10 +8,12 @@ var mappingPreimages = require('./mappingPreimages')
* (TODO: one instance need to be shared over all the components) * (TODO: one instance need to be shared over all the components)
*/ */
class StorageResolver { class StorageResolver {
constructor () { constructor (options) {
this.storageByAddress = {} this.storageByAddress = {}
this.preimagesMappingByAddress = {} this.preimagesMappingByAddress = {}
this.maxSize = 100 this.maxSize = 100
this.web3 = options.web3
this.zeroSlot = '0x0000000000000000000000000000000000000000000000000000000000000000'
} }
/** /**
...@@ -25,7 +26,7 @@ class StorageResolver { ...@@ -25,7 +26,7 @@ class StorageResolver {
* @param {Function} - callback - contains a map: [hashedKey] = {key, hashedKey, value} * @param {Function} - callback - contains a map: [hashedKey] = {key, hashedKey, value}
*/ */
storageRange (tx, stepIndex, address, callback) { storageRange (tx, stepIndex, address, callback) {
storageRangeInternal(this, zeroSlot, tx, stepIndex, address, callback) this.storageRangeInternal(this, this.zeroSlot, tx, stepIndex, address, callback)
} }
/** /**
...@@ -37,6 +38,7 @@ class StorageResolver { ...@@ -37,6 +38,7 @@ class StorageResolver {
* @return {Function} - callback * @return {Function} - callback
*/ */
initialPreimagesMappings (tx, stepIndex, address, callback) { initialPreimagesMappings (tx, stepIndex, address, callback) {
const self = this
if (this.preimagesMappingByAddress[address]) { if (this.preimagesMappingByAddress[address]) {
return callback(null, this.preimagesMappingByAddress[address]) return callback(null, this.preimagesMappingByAddress[address])
} }
...@@ -44,7 +46,7 @@ class StorageResolver { ...@@ -44,7 +46,7 @@ class StorageResolver {
if (error) { if (error) {
return callback(error) return callback(error)
} }
mappingPreimages.decodeMappingsKeys(storage, (error, mappings) => { mappingPreimages.decodeMappingsKeys(self.web3, storage, (error, mappings) => {
if (error) { if (error) {
callback(error) callback(error)
} else { } else {
...@@ -65,7 +67,7 @@ class StorageResolver { ...@@ -65,7 +67,7 @@ class StorageResolver {
* @param {Function} - callback - {key, hashedKey, value} - * @param {Function} - callback - {key, hashedKey, value} -
*/ */
storageSlot (slot, tx, stepIndex, address, callback) { storageSlot (slot, tx, stepIndex, address, callback) {
storageRangeInternal(this, slot, tx, stepIndex, address, function (error, storage) { this.storageRangeInternal(this, slot, tx, stepIndex, address, function (error, storage) {
if (error) { if (error) {
callback(error) callback(error)
} else { } else {
...@@ -83,70 +85,67 @@ class StorageResolver { ...@@ -83,70 +85,67 @@ class StorageResolver {
isComplete (address) { isComplete (address) {
return this.storageByAddress[address] && this.storageByAddress[address].complete return this.storageByAddress[address] && this.storageByAddress[address].complete
} }
}
/** /**
* retrieve the storage and ensure at least @arg slot is cached. * retrieve the storage and ensure at least @arg slot is cached.
* - If @arg slot is already cached, the storage will be returned from the cache * - If @arg slot is already cached, the storage will be returned from the cache
* even if the next 1000 items are not in the cache. * 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. * - If @arg slot is not cached, the corresponding value will be resolved and the next 1000 slots.
*/ */
function storageRangeInternal (self, slotKey, tx, stepIndex, address, callback) { storageRangeInternal (self, slotKey, tx, stepIndex, address, callback) {
var cached = fromCache(self, address) var cached = this.fromCache(self, address)
if (cached && cached.storage[slotKey]) { // we have the current slot in the cache and maybe the next 1000... if (cached && cached.storage[slotKey]) { // we have the current slot in the cache and maybe the next 1000...
return callback(null, cached.storage) return callback(null, cached.storage)
} }
storageRangeWeb3Call(tx, address, slotKey, self.maxSize, (error, storage, nextKey) => { this.storageRangeWeb3Call(tx, address, slotKey, self.maxSize, (error, storage, nextKey) => {
if (error) { if (error) {
return callback(error) return callback(error)
} }
if (!storage[slotKey] && slotKey !== zeroSlot) { // we don't cache the zero slot (could lead to inconsistency) if (!storage[slotKey] && slotKey !== self.zeroSlot) { // we don't cache the zero slot (could lead to inconsistency)
storage[slotKey] = { storage[slotKey] = {
key: slotKey, key: slotKey,
value: zeroSlot value: self.zeroSlot
} }
} }
toCache(self, address, storage) self.toCache(self, address, storage)
if (slotKey === zeroSlot && !nextKey) { // only working if keys are sorted !! if (slotKey === self.zeroSlot && !nextKey) { // only working if keys are sorted !!
self.storageByAddress[address].complete = true self.storageByAddress[address].complete = true
} }
callback(null, storage) callback(null, storage)
}) })
} }
var zeroSlot = '0x0000000000000000000000000000000000000000000000000000000000000000'
/** /**
* retrieve the storage from the cache. if @arg slot is defined, return only the desired slot, if not return the entire known storage * retrieve the storage from the cache. if @arg slot is defined, return only the desired slot, if not return the entire known storage
* *
* @param {String} address - contract address * @param {String} address - contract address
* @return {String} - either the entire known storage or a single value * @return {String} - either the entire known storage or a single value
*/ */
function fromCache (self, address) { fromCache (self, address) {
if (!self.storageByAddress[address]) { if (!self.storageByAddress[address]) {
return null return null
} }
return self.storageByAddress[address] return self.storageByAddress[address]
} }
/** /**
* store the result of `storageRangeAtInternal` * store the result of `storageRangeAtInternal`
* *
* @param {String} address - contract address * @param {String} address - contract address
* @param {Object} storage - result of `storageRangeAtInternal`, contains {key, hashedKey, value} * @param {Object} storage - result of `storageRangeAtInternal`, contains {key, hashedKey, value}
*/ */
function toCache (self, address, storage) { toCache (self, address, storage) {
if (!self.storageByAddress[address]) { if (!self.storageByAddress[address]) {
self.storageByAddress[address] = {} self.storageByAddress[address] = {}
} }
self.storageByAddress[address].storage = Object.assign(self.storageByAddress[address].storage || {}, storage) self.storageByAddress[address].storage = Object.assign(self.storageByAddress[address].storage || {}, storage)
} }
function storageRangeWeb3Call (tx, address, start, maxSize, callback) { storageRangeWeb3Call (tx, address, start, maxSize, callback) {
if (traceHelper.isContractCreation(address)) { if (traceHelper.isContractCreation(address)) {
callback(null, {}, null) callback(null, {}, null)
} else { } else {
global.web3Debug.debug.storageRangeAt( this.web3.debug.storageRangeAt(
tx.blockHash, tx.transactionIndex === undefined ? tx.hash : tx.transactionIndex, tx.blockHash, tx.transactionIndex === undefined ? tx.hash : tx.transactionIndex,
address, address,
start, start,
...@@ -161,6 +160,7 @@ function storageRangeWeb3Call (tx, address, start, maxSize, callback) { ...@@ -161,6 +160,7 @@ function storageRangeWeb3Call (tx, address, start, maxSize, callback) {
} }
}) })
} }
}
} }
module.exports = StorageResolver module.exports = StorageResolver
...@@ -12,6 +12,7 @@ class StorageViewer { ...@@ -12,6 +12,7 @@ class StorageViewer {
constructor (_context, _storageResolver, _traceManager) { constructor (_context, _storageResolver, _traceManager) {
this.context = _context this.context = _context
this.storageResolver = _storageResolver this.storageResolver = _storageResolver
this.web3 = this.storageResolver.web3
this.initialMappingsLocationPromise = null this.initialMappingsLocationPromise = null
this.currentMappingsLocationPromise = null this.currentMappingsLocationPromise = null
_traceManager.accumulateStorageChanges(this.context.stepIndex, this.context.address, {}, (error, storageChanges) => { _traceManager.accumulateStorageChanges(this.context.stepIndex, this.context.address, {}, (error, storageChanges) => {
...@@ -117,7 +118,7 @@ class StorageViewer { ...@@ -117,7 +118,7 @@ class StorageViewer {
if (this.mappingsLocationChanges) { if (this.mappingsLocationChanges) {
return callback(null, this.mappingsLocationChanges) return callback(null, this.mappingsLocationChanges)
} }
mappingPreimages.decodeMappingsKeys(storageChanges, (error, mappings) => { mappingPreimages.decodeMappingsKeys(this.web3, storageChanges, (error, mappings) => {
if (!error) { if (!error) {
this.mappingsLocationChanges = mappings this.mappingsLocationChanges = mappings
return callback(null, this.mappingsLocationChanges) return callback(null, this.mappingsLocationChanges)
......
...@@ -6,14 +6,14 @@ var TraceStepManager = require('./traceStepManager') ...@@ -6,14 +6,14 @@ var TraceStepManager = require('./traceStepManager')
var remixLib = require('remix-lib') var remixLib = require('remix-lib')
var traceHelper = remixLib.helpers.trace var traceHelper = remixLib.helpers.trace
var util = remixLib.util var util = remixLib.util
var global = remixLib.global
function TraceManager () { function TraceManager (options) {
this.web3 = options.web3
this.isLoading = false this.isLoading = false
this.trace = null this.trace = null
this.traceCache = new TraceCache() this.traceCache = new TraceCache()
this.traceAnalyser = new TraceAnalyser(this.traceCache) this.traceAnalyser = new TraceAnalyser(this.traceCache)
this.traceRetriever = new TraceRetriever() this.traceRetriever = new TraceRetriever({web3: this.web3})
this.traceStepManager = new TraceStepManager(this.traceAnalyser) this.traceStepManager = new TraceStepManager(this.traceAnalyser)
this.tx this.tx
} }
...@@ -22,7 +22,7 @@ function TraceManager () { ...@@ -22,7 +22,7 @@ function TraceManager () {
TraceManager.prototype.resolveTrace = function (tx, callback) { TraceManager.prototype.resolveTrace = function (tx, callback) {
this.tx = tx this.tx = tx
this.init() this.init()
if (!global.web3) callback('web3 not loaded', false) if (!this.web3) callback('web3 not loaded', false)
this.isLoading = true this.isLoading = true
var self = this var self = this
this.traceRetriever.getTrace(tx.hash, function (error, result) { this.traceRetriever.getTrace(tx.hash, function (error, result) {
......
'use strict' 'use strict'
var remixLib = require('remix-lib')
var global = remixLib.global
function TraceRetriever () { function TraceRetriever (options) {
this.web3 = options.web3
} }
TraceRetriever.prototype.getTrace = function (txHash, callback) { TraceRetriever.prototype.getTrace = function (txHash, callback) {
...@@ -12,7 +11,7 @@ TraceRetriever.prototype.getTrace = function (txHash, callback) { ...@@ -12,7 +11,7 @@ TraceRetriever.prototype.getTrace = function (txHash, callback) {
disableStack: false, disableStack: false,
fullStorage: false fullStorage: false
} }
global.web3Debug.debug.traceTransaction(txHash, options, function (error, result) { this.web3.debug.traceTransaction(txHash, options, function (error, result) {
callback(error, result) callback(error, result)
}) })
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment