code manager refacto

pull/7/head
yann300 8 years ago
parent cc7f3cc1a0
commit 080dc3322b
  1. 50
      src/code/codeManager.js
  2. 2
      src/code/codeResolver.js
  3. 10
      src/ui/ASMCode.js
  4. 5
      test/codeManager.js

@ -17,7 +17,6 @@ function CodeManager (_traceManager) {
util.extend(this, new EventManager()) util.extend(this, new EventManager())
this.isLoading = false this.isLoading = false
this.traceManager = _traceManager this.traceManager = _traceManager
this.currentAddress = ''
this.codeResolver = codeResolver this.codeResolver = codeResolver
} }
@ -26,63 +25,52 @@ CodeManager.prototype.resolveStep = function (stepIndex, tx) {
this.trigger('resolvingStep') this.trigger('resolvingStep')
var self = this var self = this
if (stepIndex === 0) { if (stepIndex === 0) {
self.ensureCodeLoaded(tx.to, stepIndex, tx) self.getCode(tx.to, stepIndex, tx)
} else { } else {
this.traceManager.getCurrentCalledAddressAt(stepIndex, function (error, address) { this.traceManager.getCurrentCalledAddressAt(stepIndex, function (error, address) {
if (error) { if (error) {
console.log(error) console.log(error)
} else { } else {
self.ensureCodeLoaded(address, stepIndex, tx) self.getCode(address, stepIndex, tx)
} }
}) })
} }
} }
CodeManager.prototype.ensureCodeLoaded = function (address, currentStep, tx) { CodeManager.prototype.getCode = function (address, currentStep, tx) {
var self = this var self = this
if (address !== this.currentAddress) {
if (traceHelper.isContractCreation(address)) { if (traceHelper.isContractCreation(address)) {
var codes = codeResolver.getExecutingCodeFromCache(address)
if (!codes) {
this.traceManager.getContractCreationCode(address, function (error, hexCode) { this.traceManager.getContractCreationCode(address, function (error, hexCode) {
// contract creation // contract creation
if (error) { if (error) {
console.log(error) console.log(error)
} else { } else {
var codes = codeResolver.cacheExecutingCode(address, hexCode) codes = codeResolver.cacheExecutingCode(address, hexCode)
self.trigger('loadingCode', [address]) self.retrieveIndexAndTrigger(address, currentStep, codes.code)
self.getInstructionIndex(address, currentStep, function (error, result) {
if (!error) {
self.trigger('codeChanged', [codes.code, address, result])
self.trigger('indexChanged', [result])
self.currentAddress = address
} else {
console.log(error)
} }
}) })
} else {
self.retrieveIndexAndTrigger(address, currentStep, codes.code)
} }
})
} else { } else {
codeResolver.resolveCode(address, currentStep, tx, function (address, code) { codeResolver.resolveCode(address, function (address, code) {
// resoling code from stack // resoling code from stack
self.trigger('loadingCode', [address]) self.retrieveIndexAndTrigger(address, currentStep, code)
self.getInstructionIndex(address, currentStep, function (error, result) {
if (!error) {
self.trigger('codeChanged', [code, address, result])
self.trigger('indexChanged', [result])
self.currentAddress = address
} else {
console.log(error)
}
})
}) })
} }
} else { }
// only set selected item
this.getInstructionIndex(this.currentAddress, currentStep, function (error, result) { CodeManager.prototype.retrieveIndexAndTrigger = function (address, step, code) {
var self = this
this.getInstructionIndex(address, step, function (error, result) {
if (!error) { if (!error) {
self.trigger('indexChanged', [result]) self.trigger('changed', [code, address, result])
} else {
console.log(error)
} }
}) })
}
} }
CodeManager.prototype.getInstructionIndex = function (address, step, callback) { CodeManager.prototype.getInstructionIndex = function (address, step, callback) {

@ -6,7 +6,7 @@ module.exports = {
codes: {}, // assembly items instructions list by contract addesses codes: {}, // assembly items instructions list by contract addesses
instructionsIndexByBytesOffset: {}, // mapping between bytes offset and instructions index. instructionsIndexByBytesOffset: {}, // mapping between bytes offset and instructions index.
resolveCode: function (address, vmTraceIndex, transaction, callBack) { resolveCode: function (address, callBack) {
var cache = this.getExecutingCodeFromCache(address) var cache = this.getExecutingCodeFromCache(address)
if (cache) { if (cache) {
callBack(address, cache.code) callBack(address, cache.code)

@ -26,21 +26,21 @@ ASMCode.prototype.render = function () {
} }
ASMCode.prototype.init = function () { ASMCode.prototype.init = function () {
this.codeManager.register('indexChanged', this, this.indexChanged) this.codeManager.register('changed', this, this.changed)
this.codeManager.register('codeChanged', this, this.codeChanged)
this.codeManager.register('loadingCode', this, function (address) {})
} }
ASMCode.prototype.indexChanged = function (index) { ASMCode.prototype.indexChanged = function (index) {
document.getElementById('asmitems').value = index document.getElementById('asmitems').value = index
} }
ASMCode.prototype.codeChanged = function (code, address, index) { ASMCode.prototype.changed = function (code, address, index) {
if (this.address !== address) {
this.code = code this.code = code
this.address = address this.address = address
this.renderAssemblyItems() this.renderAssemblyItems()
yo.update(this.view, this.render()) yo.update(this.view, this.render())
document.getElementById('asmitems').value = index }
this.indexChanged(index)
} }
ASMCode.prototype.renderAssemblyItems = function () { ASMCode.prototype.renderAssemblyItems = function () {

@ -40,7 +40,7 @@ function continueTesting (t, codeManager) {
t.test('CodeManager.resolveStep', function (st) { t.test('CodeManager.resolveStep', function (st) {
st.plan(6) st.plan(6)
codeManager.register('indexChanged', this, function (index) { codeManager.register('changed', this, function (code, address, index) {
if (index === undefined || index === null) { if (index === undefined || index === null) {
st.fail(index) st.fail(index)
} else { } else {
@ -48,8 +48,7 @@ function continueTesting (t, codeManager) {
} }
}) })
codeManager.register('codeChanged', this, function (code, address, index) { codeManager.register('changed', this, function (code, address, index) {
console.log(address + ' ' + index + ' ' + code)
if (!code) { if (!code) {
st.fail('no codes') st.fail('no codes')
} else { } else {

Loading…
Cancel
Save