Commit ee7e8558 authored by yann300's avatar yann300

declare traceManager func in prototype

parent 505d7436
...@@ -3,14 +3,15 @@ var React = require('react') ...@@ -3,14 +3,15 @@ var React = require('react')
var TxBrowser = require('./txBrowser') var TxBrowser = require('./txBrowser')
var StepManager = require('./stepManager') var StepManager = require('./stepManager')
var AssemblyItemsBrowser = require('./vmDebugger') var AssemblyItemsBrowser = require('./vmDebugger')
var traceManager = require('./traceManager') var TraceManager = require('./traceManager')
var style = require('./basicStyles') var style = require('./basicStyles')
module.exports = React.createClass({ module.exports = React.createClass({
getInitialState: function () { getInitialState: function () {
return { return {
currentStepIndex: -1, // index of the selected item in the vmtrace currentStepIndex: -1, // index of the selected item in the vmtrace
tx: null tx: null,
traceManager: null
} }
}, },
...@@ -23,13 +24,15 @@ module.exports = React.createClass({ ...@@ -23,13 +24,15 @@ module.exports = React.createClass({
getChildContext: function () { getChildContext: function () {
return { return {
web3: this.props.web3, web3: this.props.web3,
traceManager: traceManager, traceManager: this.state.traceManager,
tx: this.state.tx tx: this.state.tx
} }
}, },
componentDidMount: function () { componentDidMount: function () {
traceManager.setWeb3(this.props.web3) this.setState({
traceManager: new TraceManager(this.props.web3)
})
}, },
render: function () { render: function () {
...@@ -50,7 +53,7 @@ module.exports = React.createClass({ ...@@ -50,7 +53,7 @@ module.exports = React.createClass({
}, },
startDebugging: function (blockNumber, txIndex, tx) { startDebugging: function (blockNumber, txIndex, tx) {
if (traceManager.isLoading) { if (this.state.traceManager.isLoading) {
return return
} }
console.log('loading trace...') console.log('loading trace...')
...@@ -58,7 +61,7 @@ module.exports = React.createClass({ ...@@ -58,7 +61,7 @@ module.exports = React.createClass({
tx: tx tx: tx
}) })
var self = this var self = this
traceManager.resolveTrace(blockNumber, txIndex, function (success) { this.state.traceManager.resolveTrace(blockNumber, txIndex, function (success) {
console.log('trace loaded ' + success) console.log('trace loaded ' + success)
self.setState({ self.setState({
currentStepIndex: 0 currentStepIndex: 0
......
'use strict' 'use strict'
module.exports = { function TraceManager (_web3) {
isLoading: false, this.web3 = _web3
web3: null,
transaction: null, this.isLoading = false
trace: null, this.trace = null
// vmtrace changes section // vmtrace changes section
depthChanges: [], this.depthChanges = []
callStack: {}, this.callStack = {}
memoryChanges: [], this.memoryChanges = []
callDataChanges: [], this.callDataChanges = []
// storage section // storage section
storageChanges: [], this.storageChanges = []
vmTraceIndexByStorageChange: {}, this.vmTraceIndexByStorageChange = {}
vmTraceChangesRef: [], this.vmTraceChangesRef = []
storages: {}, this.storages = {}
}
// init section
setWeb3: function (web3) {
this.web3 = web3
},
resolveTrace: function (blockNumber, txNumber, callback) { // init section
TraceManager.prototype.resolveTrace = function (blockNumber, txNumber, callback) {
this.isLoading = true this.isLoading = true
this.init() this.init()
if (!this.web3) callback(false) if (!this.web3) callback(false)
...@@ -37,9 +34,9 @@ module.exports = { ...@@ -37,9 +34,9 @@ module.exports = {
} }
this.isLoading = false this.isLoading = false
}) })
}, }
init: function () { TraceManager.prototype.init = function () {
this.trace = null this.trace = null
this.depthChanges = [] this.depthChanges = []
this.memoryChanges = [] this.memoryChanges = []
...@@ -48,9 +45,9 @@ module.exports = { ...@@ -48,9 +45,9 @@ module.exports = {
this.vmTraceIndexByStorageChange = {} this.vmTraceIndexByStorageChange = {}
this.vmTraceChangesRef = [] this.vmTraceChangesRef = []
this.callStack = {} this.callStack = {}
}, }
computeTrace: function (trace) { TraceManager.prototype.computeTrace = function (trace) {
this.trace = trace this.trace = trace
var currentDepth = 0 var currentDepth = 0
var currentStorageAddress var currentStorageAddress
...@@ -66,22 +63,22 @@ module.exports = { ...@@ -66,22 +63,22 @@ module.exports = {
currentDepth = depth currentDepth = depth
} }
} }
}, }
// compute trace section // compute trace section
buildCalldata: function (index, step) { TraceManager.prototype.buildCalldata = function (index, step) {
if (step.calldata) { if (step.calldata) {
this.callDataChanges.push(index) this.callDataChanges.push(index)
} }
}, }
buildMemory: function (index, step) { TraceManager.prototype.buildMemory = function (index, step) {
if (step.memory) { if (step.memory) {
this.memoryChanges.push(index) this.memoryChanges.push(index)
} }
}, }
buildStorage: function (index, step, currentAddress) { TraceManager.prototype.buildStorage = function (index, step, currentAddress) {
var change = false var change = false
if (step.address) { if (step.address) {
// new context // new context
...@@ -109,9 +106,9 @@ module.exports = { ...@@ -109,9 +106,9 @@ module.exports = {
this.vmTraceChangesRef.push(index) this.vmTraceChangesRef.push(index)
} }
return currentAddress return currentAddress
}, }
buildDepth: function (index, step, currentDepth, callStack) { TraceManager.prototype.buildDepth = function (index, step, currentDepth, callStack) {
if (step.depth === undefined) return if (step.depth === undefined) return
if (step.depth > currentDepth) { if (step.depth > currentDepth) {
if (index === 0) { if (index === 0) {
...@@ -132,15 +129,15 @@ module.exports = { ...@@ -132,15 +129,15 @@ module.exports = {
} }
this.depthChanges.push(index) this.depthChanges.push(index)
return step.depth return step.depth
}, }
// API section // API section
getLength: function (callback) { TraceManager.prototype.getLength = function (callback) {
if (!this.trace) callback('no trace available', null) if (!this.trace) callback('no trace available', null)
callback(null, this.trace.length) callback(null, this.trace.length)
}, }
getStorageAt: function (stepIndex, blockNumber, txIndex, callback) { TraceManager.prototype.getStorageAt = function (stepIndex, blockNumber, txIndex, callback) {
var stoChange = this.findLowerBound(stepIndex, this.vmTraceChangesRef) var stoChange = this.findLowerBound(stepIndex, this.vmTraceChangesRef)
if (!stoChange) { if (!stoChange) {
callback('cannot rebuild storage', null) callback('cannot rebuild storage', null)
...@@ -162,21 +159,21 @@ module.exports = { ...@@ -162,21 +159,21 @@ module.exports = {
} }
callback(null, storage) callback(null, storage)
}) })
}, }
getCallDataAt: function (stepIndex, callback) { TraceManager.prototype.getCallDataAt = function (stepIndex, callback) {
var callDataChange = this.findLowerBound(stepIndex, this.callDataChanges) var callDataChange = this.findLowerBound(stepIndex, this.callDataChanges)
if (!callDataChange) return callback('no calldata found', null) if (!callDataChange) return callback('no calldata found', null)
callback(null, [this.trace[callDataChange].calldata]) callback(null, [this.trace[callDataChange].calldata])
}, }
getCallStackAt: function (stepIndex, callback) { TraceManager.prototype.getCallStackAt = function (stepIndex, callback) {
var callStackChange = this.findLowerBound(stepIndex, this.depthChanges) var callStackChange = this.findLowerBound(stepIndex, this.depthChanges)
if (!callStackChange) return callback('no callstack found', null) if (!callStackChange) return callback('no callstack found', null)
callback(null, this.callStack[callStackChange].stack) callback(null, this.callStack[callStackChange].stack)
}, }
getStackAt: function (stepIndex, callback) { TraceManager.prototype.getStackAt = function (stepIndex, callback) {
var stack var stack
if (this.trace[stepIndex].stack) { // there's always a stack if (this.trace[stepIndex].stack) { // there's always a stack
stack = this.trace[stepIndex].stack.slice(0) stack = this.trace[stepIndex].stack.slice(0)
...@@ -185,14 +182,14 @@ module.exports = { ...@@ -185,14 +182,14 @@ module.exports = {
} else { } else {
callback('no stack found', null) callback('no stack found', null)
} }
}, }
getLastDepthIndexChangeSince: function (stepIndex, callback) { TraceManager.prototype.getLastDepthIndexChangeSince = function (stepIndex, callback) {
var depthIndex = this.findLowerBound(stepIndex, this.depthChanges) var depthIndex = this.findLowerBound(stepIndex, this.depthChanges)
callback(null, depthIndex) callback(null, depthIndex)
}, }
getCurrentCalledAddressAt: function (stepIndex, callback) { TraceManager.prototype.getCurrentCalledAddressAt = function (stepIndex, callback) {
var self = this var self = this
this.getLastDepthIndexChangeSince(stepIndex, function (error, addressIndex) { this.getLastDepthIndexChangeSince(stepIndex, function (error, addressIndex) {
if (error) { if (error) {
...@@ -201,62 +198,62 @@ module.exports = { ...@@ -201,62 +198,62 @@ module.exports = {
callback(null, self.resolveAddress(addressIndex)) callback(null, self.resolveAddress(addressIndex))
} }
}) })
}, }
getMemoryAt: function (stepIndex, callback) { TraceManager.prototype.getMemoryAt = function (stepIndex, callback) {
var lastChanges = this.findLowerBound(stepIndex, this.memoryChanges) var lastChanges = this.findLowerBound(stepIndex, this.memoryChanges)
if (!lastChanges) return callback('no memory found', null) if (!lastChanges) return callback('no memory found', null)
callback(null, this.trace[lastChanges].memory) callback(null, this.trace[lastChanges].memory)
}, }
getCurrentPC: function (stepIndex, callback) { TraceManager.prototype.getCurrentPC = function (stepIndex, callback) {
callback(null, this.trace[stepIndex].pc) callback(null, this.trace[stepIndex].pc)
}, }
getCurrentStep: function (stepIndex, callback) { TraceManager.prototype.getCurrentStep = function (stepIndex, callback) {
callback(null, this.trace[stepIndex].steps) callback(null, this.trace[stepIndex].steps)
}, }
getMemExpand: function (stepIndex, callback) { TraceManager.prototype.getMemExpand = function (stepIndex, callback) {
callback(null, this.trace[stepIndex].memexpand ? this.trace[stepIndex].memexpand : '') callback(null, this.trace[stepIndex].memexpand ? this.trace[stepIndex].memexpand : '')
}, }
getStepCost: function (stepIndex, callback) { TraceManager.prototype.getStepCost = function (stepIndex, callback) {
callback(null, this.trace[stepIndex].gascost) callback(null, this.trace[stepIndex].gascost)
}, }
getRemainingGas: function (stepIndex, callback) { TraceManager.prototype.getRemainingGas = function (stepIndex, callback) {
callback(null, this.trace[stepIndex].gas) callback(null, this.trace[stepIndex].gas)
}, }
// step section // step section
isCallInstruction: function (index) { TraceManager.prototype.isCallInstruction = function (index) {
var state = this.trace[index] var state = this.trace[index]
return state.instname === 'CALL' || state.instname === 'CALLCODE' || state.instname === 'CREATE' || state.instname === 'DELEGATECALL' return state.instname === 'CALL' || state.instname === 'CALLCODE' || state.instname === 'CREATE' || state.instname === 'DELEGATECALL'
}, }
isReturnInstruction: function (index) { TraceManager.prototype.isReturnInstruction = function (index) {
var state = this.trace[index] var state = this.trace[index]
return state.instname === 'RETURN' return state.instname === 'RETURN'
}, }
findStepOverBack: function (currentStep) { TraceManager.prototype.findStepOverBack = function (currentStep) {
if (this.isReturnInstruction(currentStep - 1)) { if (this.isReturnInstruction(currentStep - 1)) {
return this.findStepOutBack(currentStep) return this.findStepOutBack(currentStep)
} else { } else {
return currentStep - 1 return currentStep - 1
} }
}, }
findStepOverForward: function (currentStep) { TraceManager.prototype.findStepOverForward = function (currentStep) {
if (this.isCallInstruction(currentStep)) { if (this.isCallInstruction(currentStep)) {
return this.findStepOutForward(currentStep) return this.findStepOutForward(currentStep)
} else { } else {
return currentStep + 1 return currentStep + 1
} }
}, }
findStepOutBack: function (currentStep) { TraceManager.prototype.findStepOutBack = function (currentStep) {
var i = currentStep - 1 var i = currentStep - 1
var depth = 0 var depth = 0
while (--i >= 0) { while (--i >= 0) {
...@@ -271,9 +268,9 @@ module.exports = { ...@@ -271,9 +268,9 @@ module.exports = {
} }
} }
return i return i
}, }
findStepOutForward: function (currentStep) { TraceManager.prototype.findStepOutForward = function (currentStep) {
var i = currentStep var i = currentStep
var depth = 0 var depth = 0
while (++i < this.trace.length) { while (++i < this.trace.length) {
...@@ -288,10 +285,10 @@ module.exports = { ...@@ -288,10 +285,10 @@ module.exports = {
} }
} }
return i + 1 return i + 1
}, }
// util section // util section
findLowerBound: function (target, changes) { TraceManager.prototype.findLowerBound = function (target, changes) {
if (changes.length === 1) { if (changes.length === 1) {
if (changes[0] > target) { if (changes[0] > target) {
// we only a closest maximum, returning 0 // we only a closest maximum, returning 0
...@@ -309,19 +306,19 @@ module.exports = { ...@@ -309,19 +306,19 @@ module.exports = {
} else { } else {
return changes[middle] return changes[middle]
} }
}, }
resolveAddress: function (vmTraceIndex) { TraceManager.prototype.resolveAddress = function (vmTraceIndex) {
var address = this.trace[vmTraceIndex].address var address = this.trace[vmTraceIndex].address
if (vmTraceIndex > 0) { if (vmTraceIndex > 0) {
var stack = this.trace[vmTraceIndex - 1].stack // callcode, delegatecall, ... var stack = this.trace[vmTraceIndex - 1].stack // callcode, delegatecall, ...
address = stack[stack.length - 2] address = stack[stack.length - 2]
} }
return address return address
}, }
// retrieve the storage of an account just after the execution of tx // retrieve the storage of an account just after the execution of tx
retrieveStorage: function (address, blockNumber, txIndex, callBack) { TraceManager.prototype.retrieveStorage = function (address, blockNumber, txIndex, callBack) {
if (this.storages[address]) { if (this.storages[address]) {
callBack(this.storages[address]) callBack(this.storages[address])
} }
...@@ -338,5 +335,6 @@ module.exports = { ...@@ -338,5 +335,6 @@ module.exports = {
} else { } else {
console.log('blockNumber/txIndex are not defined') console.log('blockNumber/txIndex are not defined')
} }
}
} }
module.exports = TraceManager
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