You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
164 lines
6.1 KiB
164 lines
6.1 KiB
7 years ago
|
'use strict'
|
||
7 years ago
|
|
||
5 years ago
|
const StorageViewer = require('./storage/storageViewer')
|
||
|
const StorageResolver = require('./storage/storageResolver')
|
||
|
|
||
5 years ago
|
const TraceManager = require('./trace/traceManager')
|
||
|
const CodeManager = require('./code/codeManager')
|
||
|
const traceHelper = require('./trace/traceHelper')
|
||
|
const EventManager = require('./eventManager')
|
||
|
|
||
|
const {SolidityProxy, stateDecoder, localDecoder, InternalCallTree} = require('./solidity-decoder')
|
||
|
|
||
7 years ago
|
/**
|
||
|
* Ethdebugger is a wrapper around a few classes that helps debugging a transaction
|
||
|
*
|
||
|
* - Web3Providers - define which environment (web3) the transaction will be retrieved from
|
||
|
* - TraceManager - Load / Analyze the trace and retrieve details of specific test
|
||
|
* - CodeManager - Retrieve loaded byte code and help to resolve AST item from vmtrace index
|
||
|
* - SolidityProxy - Basically used to extract state variable from AST
|
||
|
* - Breakpoint Manager - Used to add / remove / jumpto breakpoint
|
||
|
* - InternalCallTree - Used to retrieved local variables
|
||
7 years ago
|
* - StorageResolver - Help resolving the storage accross different steps
|
||
7 years ago
|
*
|
||
|
* @param {Map} opts - { function compilationResult } //
|
||
|
*/
|
||
|
function Ethdebugger (opts) {
|
||
5 years ago
|
this.compilationResult = opts.compilationResult || function (contractAddress) { return null }
|
||
6 years ago
|
this.web3 = opts.web3
|
||
5 years ago
|
this.opts = opts
|
||
7 years ago
|
|
||
7 years ago
|
this.event = new EventManager()
|
||
|
|
||
|
this.tx
|
||
|
|
||
7 years ago
|
this.traceManager = new TraceManager({web3: this.web3})
|
||
|
this.codeManager = new CodeManager(this.traceManager)
|
||
5 years ago
|
this.solidityProxy = new SolidityProxy({getCurrentCalledAddressAt: this.traceManager.getCurrentCalledAddressAt.bind(this.traceManager), getCode: this.codeManager.getCode.bind(this.codeManager)})
|
||
7 years ago
|
this.storageResolver = null
|
||
|
|
||
5 years ago
|
const includeLocalVariables = true
|
||
|
this.callTree = new InternalCallTree(this.event,
|
||
|
this.traceManager,
|
||
|
this.solidityProxy,
|
||
|
this.codeManager,
|
||
|
{ ...opts, includeLocalVariables})
|
||
7 years ago
|
}
|
||
|
|
||
|
Ethdebugger.prototype.setManagers = function () {
|
||
|
this.traceManager = new TraceManager({web3: this.web3})
|
||
7 years ago
|
this.codeManager = new CodeManager(this.traceManager)
|
||
5 years ago
|
this.solidityProxy = new SolidityProxy({getCurrentCalledAddressAt: this.traceManager.getCurrentCalledAddressAt.bind(this.traceManager), getCode: this.codeManager.getCode.bind(this.codeManager)})
|
||
7 years ago
|
this.storageResolver = null
|
||
5 years ago
|
const includeLocalVariables = true
|
||
7 years ago
|
|
||
5 years ago
|
this.callTree = new InternalCallTree(this.event,
|
||
|
this.traceManager,
|
||
|
this.solidityProxy,
|
||
|
this.codeManager,
|
||
|
{ ...this.opts, includeLocalVariables})
|
||
5 years ago
|
this.event.trigger('managersChanged')
|
||
7 years ago
|
}
|
||
7 years ago
|
|
||
7 years ago
|
Ethdebugger.prototype.resolveStep = function (index) {
|
||
|
this.codeManager.resolveStep(index, this.tx)
|
||
7 years ago
|
}
|
||
|
|
||
7 years ago
|
Ethdebugger.prototype.setCompilationResult = function (compilationResult) {
|
||
5 years ago
|
this.solidityProxy.reset((compilationResult && compilationResult.data) || {})
|
||
7 years ago
|
}
|
||
|
|
||
5 years ago
|
Ethdebugger.prototype.sourceLocationFromVMTraceIndex = async function (address, stepIndex) {
|
||
|
return this.callTree.sourceLocationTracker.getSourceLocationFromVMTraceIndex(address, stepIndex, this.solidityProxy.contracts)
|
||
7 years ago
|
}
|
||
|
|
||
4 years ago
|
Ethdebugger.prototype.getValidSourceLocationFromVMTraceIndex = async function (address, stepIndex) {
|
||
|
return this.callTree.sourceLocationTracker.getValidSourceLocationFromVMTraceIndex(address, stepIndex, this.solidityProxy.contracts)
|
||
|
}
|
||
|
|
||
5 years ago
|
Ethdebugger.prototype.sourceLocationFromInstructionIndex = async function (address, instIndex, callback) {
|
||
|
return this.callTree.sourceLocationTracker.getSourceLocationFromInstructionIndex(address, instIndex, this.solidityProxy.contracts)
|
||
7 years ago
|
}
|
||
|
|
||
7 years ago
|
/* breakpoint */
|
||
7 years ago
|
Ethdebugger.prototype.setBreakpointManager = function (breakpointManager) {
|
||
|
this.breakpointManager = breakpointManager
|
||
|
}
|
||
|
|
||
7 years ago
|
/* decode locals */
|
||
5 years ago
|
Ethdebugger.prototype.extractLocalsAt = function (step) {
|
||
|
return this.callTree.findScope(step)
|
||
7 years ago
|
}
|
||
|
|
||
5 years ago
|
Ethdebugger.prototype.decodeLocalsAt = async function (step, sourceLocation, callback) {
|
||
5 years ago
|
try {
|
||
|
const stack = this.traceManager.getStackAt(step)
|
||
|
const memory = this.traceManager.getMemoryAt(step)
|
||
|
const address = this.traceManager.getCurrentCalledAddressAt(step)
|
||
|
try {
|
||
|
const storageViewer = new StorageViewer({ stepIndex: step, tx: this.tx, address: address }, this.storageResolver, this.traceManager)
|
||
5 years ago
|
const locals = await localDecoder.solidityLocals(step, this.callTree, stack, memory, storageViewer, sourceLocation)
|
||
|
if (locals.error) {
|
||
|
return callback(locals.error)
|
||
|
}
|
||
|
return callback(null, locals)
|
||
5 years ago
|
} catch (e) {
|
||
|
callback(e.message)
|
||
|
}
|
||
|
} catch (error) {
|
||
|
callback(error)
|
||
|
}
|
||
7 years ago
|
}
|
||
|
|
||
7 years ago
|
/* decode state */
|
||
5 years ago
|
Ethdebugger.prototype.extractStateAt = async function (step) {
|
||
|
return this.solidityProxy.extractStateVariablesAt(step)
|
||
7 years ago
|
}
|
||
|
|
||
5 years ago
|
Ethdebugger.prototype.decodeStateAt = async function (step, stateVars, callback) {
|
||
5 years ago
|
try {
|
||
|
const address = this.traceManager.getCurrentCalledAddressAt(step)
|
||
5 years ago
|
const storageViewer = new StorageViewer({stepIndex: step, tx: this.tx, address: address}, this.storageResolver, this.traceManager)
|
||
|
const result = await stateDecoder.decodeState(stateVars, storageViewer)
|
||
|
return result
|
||
5 years ago
|
} catch (error) {
|
||
|
callback(error)
|
||
|
}
|
||
7 years ago
|
}
|
||
|
|
||
|
Ethdebugger.prototype.storageViewAt = function (step, address) {
|
||
5 years ago
|
return new StorageViewer({stepIndex: step, tx: this.tx, address: address}, this.storageResolver, this.traceManager)
|
||
7 years ago
|
}
|
||
7 years ago
|
|
||
6 years ago
|
Ethdebugger.prototype.updateWeb3 = function (web3) {
|
||
|
this.web3 = web3
|
||
6 years ago
|
this.setManagers()
|
||
7 years ago
|
}
|
||
|
|
||
|
Ethdebugger.prototype.unLoad = function () {
|
||
|
this.traceManager.init()
|
||
|
this.codeManager.clear()
|
||
|
this.event.trigger('traceUnloaded')
|
||
|
}
|
||
|
|
||
7 years ago
|
Ethdebugger.prototype.debug = function (tx) {
|
||
7 years ago
|
if (this.traceManager.isLoading) {
|
||
|
return
|
||
|
}
|
||
5 years ago
|
tx.to = tx.to || traceHelper.contractCreationToken('0')
|
||
7 years ago
|
this.tx = tx
|
||
5 years ago
|
|
||
|
this.traceManager.resolveTrace(tx).then(async (result) => {
|
||
|
this.setCompilationResult(await this.compilationResult(tx.to))
|
||
|
this.event.trigger('newTraceLoaded', [this.traceManager.trace])
|
||
|
if (this.breakpointManager && this.breakpointManager.hasBreakpoint()) {
|
||
|
this.breakpointManager.jumpNextBreakpoint(false)
|
||
7 years ago
|
}
|
||
5 years ago
|
this.storageResolver = new StorageResolver({web3: this.traceManager.web3})
|
||
|
}).catch((error) => {
|
||
|
this.statusMessage = error ? error.message : 'Trace not loaded'
|
||
7 years ago
|
})
|
||
|
}
|
||
|
|
||
|
module.exports = Ethdebugger
|