commit
f4ef3c5863
@ -1,72 +1,74 @@ |
|||||||
'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.bytecodeByAddress = {} |
this.instructionsByAddress = {} // assembly items instructions list by contract addesses
|
||||||
this.instructionsByAddress = {} |
this.instructionsIndexByBytesOffset = {} // mapping between bytes offset and instructions index.
|
||||||
this.instructionsIndexByBytesOffset = {} |
} |
||||||
}, |
|
||||||
|
|
||||||
resolveCode: function (address, callBack) { |
|
||||||
var cache = this.getExecutingCodeFromCache(address) |
|
||||||
if (cache) { |
|
||||||
callBack(address, cache) |
|
||||||
return |
|
||||||
} |
|
||||||
|
|
||||||
var self = this |
|
||||||
this.loadCode(address, function (code) { |
|
||||||
callBack(address, self.cacheExecutingCode(address, code)) |
|
||||||
}) |
|
||||||
}, |
|
||||||
|
|
||||||
loadCode: function (address, callback) { |
CodeResolver.prototype.clear = function () { |
||||||
console.log('loading new code from web3 ' + address) |
this.bytecodeByAddress = {} |
||||||
global.web3.eth.getCode(address, function (error, result) { |
this.instructionsByAddress = {} |
||||||
if (error) { |
this.instructionsIndexByBytesOffset = {} |
||||||
console.log(error) |
} |
||||||
} else { |
|
||||||
callback(result) |
|
||||||
} |
|
||||||
}) |
|
||||||
}, |
|
||||||
|
|
||||||
cacheExecutingCode: function (address, hexCode) { |
CodeResolver.prototype.resolveCode = function (address, callBack) { |
||||||
var codes = this.formatCode(hexCode) |
var cache = this.getExecutingCodeFromCache(address) |
||||||
this.bytecodeByAddress[address] = hexCode |
if (cache) { |
||||||
this.instructionsByAddress[address] = codes.code |
callBack(address, cache) |
||||||
this.instructionsIndexByBytesOffset[address] = codes.instructionsIndexByBytesOffset |
return |
||||||
return this.getExecutingCodeFromCache(address) |
} |
||||||
}, |
|
||||||
|
|
||||||
formatCode: function (hexCode) { |
var self = this |
||||||
var code = codeUtils.nameOpCodes(new Buffer(hexCode.substring(2), 'hex')) |
this.loadCode(address, function (code) { |
||||||
return { |
callBack(address, self.cacheExecutingCode(address, code)) |
||||||
code: code[0], |
}) |
||||||
instructionsIndexByBytesOffset: code[1] |
} |
||||||
} |
|
||||||
}, |
|
||||||
|
|
||||||
getExecutingCodeFromCache: function (address) { |
CodeResolver.prototype.loadCode = function (address, callback) { |
||||||
if (this.instructionsByAddress[address]) { |
console.log('loading new code from web3 ' + address) |
||||||
return { |
this.web3.eth.getCode(address, function (error, result) { |
||||||
instructions: this.instructionsByAddress[address], |
if (error) { |
||||||
instructionsIndexByBytesOffset: this.instructionsIndexByBytesOffset[address], |
console.log(error) |
||||||
bytecode: this.bytecodeByAddress[address] |
|
||||||
} |
|
||||||
} else { |
} else { |
||||||
return null |
callback(result) |
||||||
} |
} |
||||||
}, |
}) |
||||||
|
} |
||||||
|
|
||||||
getInstructionIndex: function (address, pc) { |
CodeResolver.prototype.cacheExecutingCode = function (address, hexCode) { |
||||||
return this.getExecutingCodeFromCache(address).instructionsIndexByBytesOffset[pc] |
var codes = this.formatCode(hexCode) |
||||||
|
this.bytecodeByAddress[address] = hexCode |
||||||
|
this.instructionsByAddress[address] = codes.code |
||||||
|
this.instructionsIndexByBytesOffset[address] = codes.instructionsIndexByBytesOffset |
||||||
|
return this.getExecutingCodeFromCache(address) |
||||||
|
} |
||||||
|
|
||||||
|
CodeResolver.prototype.formatCode = function (hexCode) { |
||||||
|
var code = codeUtils.nameOpCodes(new Buffer(hexCode.substring(2), 'hex')) |
||||||
|
return { |
||||||
|
code: code[0], |
||||||
|
instructionsIndexByBytesOffset: code[1] |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
CodeResolver.prototype.getExecutingCodeFromCache = function (address) { |
||||||
|
if (this.instructionsByAddress[address]) { |
||||||
|
return { |
||||||
|
instructions: this.instructionsByAddress[address], |
||||||
|
instructionsIndexByBytesOffset: this.instructionsIndexByBytesOffset[address], |
||||||
|
bytecode: this.bytecodeByAddress[address] |
||||||
|
} |
||||||
|
} else { |
||||||
|
return null |
||||||
} |
} |
||||||
} |
} |
||||||
|
|
||||||
|
CodeResolver.prototype.getInstructionIndex = function (address, pc) { |
||||||
|
return this.getExecutingCodeFromCache(address).instructionsIndexByBytesOffset[pc] |
||||||
|
} |
||||||
|
|
||||||
|
module.exports = CodeResolver |
||||||
|
@ -1,4 +0,0 @@ |
|||||||
module.exports = { |
|
||||||
web3: null, |
|
||||||
web3Debug: null // this node should support the debug endpoint
|
|
||||||
} |
|
Loading…
Reference in new issue