Commit 03f9f717 authored by Iuri Matias's avatar Iuri Matias

refactor codeResolver

parent c055231b
...@@ -16,7 +16,16 @@ function CodeManager (_traceManager) { ...@@ -16,7 +16,16 @@ 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 = new CodeResolver({web3: this.traceManager.web3}) this.codeResolver = new CodeResolver({getCode: async (address) => {
return new Promise((resolve, reject) => {
this.traceManager.web3.eth.getCode(address, (error, code) => {
if (error) {
return reject(error)
}
return resolve(code)
})
})
}})
} }
/** /**
......
'use strict' 'use strict'
const codeUtils = require('./codeUtils') const codeUtils = require('./codeUtils')
function CodeResolver (options) { function CodeResolver ({getCode}) {
this.web3 = options.web3 this.getCode = getCode
this.bytecodeByAddress = {} // bytes code by contract addesses this.bytecodeByAddress = {} // bytes code by contract addesses
this.instructionsByAddress = {} // assembly items instructions list by contract addesses this.instructionsByAddress = {} // assembly items instructions list by contract addesses
...@@ -16,20 +16,13 @@ CodeResolver.prototype.clear = function () { ...@@ -16,20 +16,13 @@ CodeResolver.prototype.clear = function () {
} }
CodeResolver.prototype.resolveCode = async function (address) { CodeResolver.prototype.resolveCode = async function (address) {
return new Promise((resolve, reject) => {
const cache = this.getExecutingCodeFromCache(address) const cache = this.getExecutingCodeFromCache(address)
if (cache) { if (cache) {
return resolve(cache) return cache
} }
this.web3.eth.getCode(address, (error, code) => { const code = await this.getCode(address)
if (error) { return this.cacheExecutingCode(address, code)
// return console.log(error)
return reject(error)
}
return resolve(this.cacheExecutingCode(address, code))
})
})
} }
CodeResolver.prototype.cacheExecutingCode = function (address, hexCode) { CodeResolver.prototype.cacheExecutingCode = function (address, hexCode) {
...@@ -41,11 +34,8 @@ CodeResolver.prototype.cacheExecutingCode = function (address, hexCode) { ...@@ -41,11 +34,8 @@ CodeResolver.prototype.cacheExecutingCode = function (address, hexCode) {
} }
CodeResolver.prototype.formatCode = function (hexCode) { CodeResolver.prototype.formatCode = function (hexCode) {
const code = codeUtils.nameOpCodes(Buffer.from(hexCode.substring(2), 'hex')) const [code, instructionsIndexByBytesOffset] = codeUtils.nameOpCodes(Buffer.from(hexCode.substring(2), 'hex'))
return { return {code, instructionsIndexByBytesOffset}
code: code[0],
instructionsIndexByBytesOffset: code[1]
}
} }
CodeResolver.prototype.getExecutingCodeFromCache = function (address) { CodeResolver.prototype.getExecutingCodeFromCache = function (address) {
......
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