code manager refacto

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

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

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

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

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

Loading…
Cancel
Save