traceManager and traceStepManager updated

pull/5370/head
aniket-engg 4 years ago committed by Aniket
parent eb96e84b15
commit 7fc6a94109
  1. 432
      libs/remix-debug/src/trace/traceManager.ts
  2. 77
      libs/remix-debug/src/trace/traceStepManager.ts

@ -7,269 +7,277 @@ const traceHelper = require('./traceHelper')
const remixLib = require('@remix-project/remix-lib') const remixLib = require('@remix-project/remix-lib')
const util = remixLib.util const util = remixLib.util
function TraceManager (options) { export class TraceManager {
this.web3 = options.web3
this.isLoading = false web3
this.trace = null isLoading: boolean
this.traceCache = new TraceCache() trace
this.traceAnalyser = new TraceAnalyser(this.traceCache) traceCache
this.traceStepManager = new TraceStepManager(this.traceAnalyser) traceAnalyser
this.tx traceStepManager
} tx
// init section constructor (options) {
TraceManager.prototype.resolveTrace = async function (tx) { this.web3 = options.web3
this.tx = tx this.isLoading = false
this.init() this.trace = null
if (!this.web3) throw new Error('web3 not loaded') this.traceCache = new TraceCache()
this.isLoading = true this.traceAnalyser = new TraceAnalyser(this.traceCache)
try { this.traceStepManager = new TraceStepManager(this.traceAnalyser)
const result = await this.getTrace(tx.hash) }
if (result.structLogs.length > 0) {
this.trace = result.structLogs
this.traceAnalyser.analyse(result.structLogs, tx) // init section
async resolveTrace (tx) {
this.tx = tx
this.init()
if (!this.web3) throw new Error('web3 not loaded')
this.isLoading = true
try {
const result = await this.getTrace(tx.hash)
if (result['structLogs'].length > 0) {
this.trace = result['structLogs']
this.traceAnalyser.analyse(result['structLogs'], tx)
this.isLoading = false
return true
}
var mes = tx.hash + ' is not a contract invocation or contract creation.'
console.log(mes)
this.isLoading = false
throw new Error(mes)
} catch (error) {
console.log(error)
this.isLoading = false this.isLoading = false
return true throw new Error(error)
} }
var mes = tx.hash + ' is not a contract invocation or contract creation.'
console.log(mes)
this.isLoading = false
throw new Error(mes)
} catch (error) {
console.log(error)
this.isLoading = false
throw new Error(error)
} }
}
TraceManager.prototype.getTrace = function (txHash) { getTrace (txHash) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const options = { const options = {
disableStorage: true, disableStorage: true,
disableMemory: false, disableMemory: false,
disableStack: false, disableStack: false,
fullStorage: false fullStorage: false
} }
this.web3.debug.traceTransaction(txHash, options, function (error, result) { this.web3.debug.traceTransaction(txHash, options, function (error, result) {
if (error) return reject(error) if (error) return reject(error)
resolve(result) resolve(result)
})
}) })
}) }
}
TraceManager.prototype.init = function () {
this.trace = null
this.traceCache.init()
}
// API section
TraceManager.prototype.inRange = function (step) {
return this.isLoaded() && step >= 0 && step < this.trace.length
}
TraceManager.prototype.isLoaded = function () {
return !this.isLoading && this.trace !== null
}
TraceManager.prototype.getLength = function (callback) { init () {
if (!this.trace) { this.trace = null
callback('no trace available', null) this.traceCache.init()
} else {
callback(null, this.trace.length)
} }
}
TraceManager.prototype.accumulateStorageChanges = function (index, address, storageOrigin) { // API section
return this.traceCache.accumulateStorageChanges(index, address, storageOrigin) inRange (step) {
} return this.isLoaded() && step >= 0 && step < this.trace.length
}
TraceManager.prototype.getAddresses = function () { isLoaded () {
return this.traceCache.addresses return !this.isLoading && this.trace !== null
} }
TraceManager.prototype.getCallDataAt = function (stepIndex) { getLength (callback) {
try { if (!this.trace) {
this.checkRequestedStep(stepIndex) callback('no trace available', null)
} catch (check) { } else {
throw new Error(check) callback(null, this.trace.length)
}
} }
const callDataChange = util.findLowerBoundValue(stepIndex, this.traceCache.callDataChanges)
if (callDataChange === null) { accumulateStorageChanges (index, address, storageOrigin) {
throw new Error('no calldata found') return this.traceCache.accumulateStorageChanges(index, address, storageOrigin)
} }
return [this.traceCache.callsData[callDataChange]]
}
TraceManager.prototype.buildCallPath = async function (stepIndex) { getAddresses () {
try { return this.traceCache.addresses
this.checkRequestedStep(stepIndex)
} catch (check) {
throw new Error(check)
} }
const callsPath = util.buildCallPath(stepIndex, this.traceCache.callsTree.call)
if (callsPath === null) throw new Error('no call path built')
return callsPath
}
TraceManager.prototype.getCallStackAt = function (stepIndex) { getCallDataAt (stepIndex) {
try { try {
this.checkRequestedStep(stepIndex) this.checkRequestedStep(stepIndex)
} catch (check) { } catch (check) {
throw new Error(check) throw new Error(check)
}
const callDataChange = util.findLowerBoundValue(stepIndex, this.traceCache.callDataChanges)
if (callDataChange === null) {
throw new Error('no calldata found')
}
return [this.traceCache.callsData[callDataChange]]
} }
const call = util.findCall(stepIndex, this.traceCache.callsTree.call)
if (call === null) { async buildCallPath (stepIndex) {
throw new Error('no callstack found') try {
this.checkRequestedStep(stepIndex)
} catch (check) {
throw new Error(check)
}
const callsPath = util.buildCallPath(stepIndex, this.traceCache.callsTree.call)
if (callsPath === null) throw new Error('no call path built')
return callsPath
} }
return call.callStack
}
TraceManager.prototype.getStackAt = function (stepIndex) { getCallStackAt (stepIndex) {
this.checkRequestedStep(stepIndex) try {
if (this.trace[stepIndex] && this.trace[stepIndex].stack) { // there's always a stack this.checkRequestedStep(stepIndex)
let stack = this.trace[stepIndex].stack.slice(0) } catch (check) {
stack.reverse() throw new Error(check)
return stack }
} else { const call = util.findCall(stepIndex, this.traceCache.callsTree.call)
throw new Error('no stack found') if (call === null) {
throw new Error('no callstack found')
}
return call.callStack
} }
}
TraceManager.prototype.getLastCallChangeSince = function (stepIndex) { getStackAt (stepIndex) {
try {
this.checkRequestedStep(stepIndex) this.checkRequestedStep(stepIndex)
} catch (check) { if (this.trace[stepIndex] && this.trace[stepIndex].stack) { // there's always a stack
throw new Error(check) let stack = this.trace[stepIndex].stack.slice(0)
stack.reverse()
return stack
} else {
throw new Error('no stack found')
}
} }
const callChange = util.findCall(stepIndex, this.traceCache.callsTree.call) getLastCallChangeSince (stepIndex) {
if (callChange === null) { try {
return 0 this.checkRequestedStep(stepIndex)
} } catch (check) {
return callChange throw new Error(check)
} }
TraceManager.prototype.getCurrentCalledAddressAt = function (stepIndex) { const callChange = util.findCall(stepIndex, this.traceCache.callsTree.call)
try { if (callChange === null) {
this.checkRequestedStep(stepIndex) return 0
const resp = this.getLastCallChangeSince(stepIndex)
if (!resp) {
throw new Error('unable to get current called address. ' + stepIndex + ' does not match with a CALL')
} }
return resp.address return callChange
} catch (error) {
throw new Error(error)
} }
}
TraceManager.prototype.getContractCreationCode = function (token) { getCurrentCalledAddressAt (stepIndex) {
if (!this.traceCache.contractCreation[token]) { try {
throw new Error('no contract creation named ' + token) this.checkRequestedStep(stepIndex)
const resp = this.getLastCallChangeSince(stepIndex)
if (!resp) {
throw new Error('unable to get current called address. ' + stepIndex + ' does not match with a CALL')
}
return resp.address
} catch (error) {
throw new Error(error)
}
} }
return this.traceCache.contractCreation[token]
}
TraceManager.prototype.getMemoryAt = function (stepIndex) { getContractCreationCode (token) {
this.checkRequestedStep(stepIndex) if (!this.traceCache.contractCreation[token]) {
const lastChanges = util.findLowerBoundValue(stepIndex, this.traceCache.memoryChanges) throw new Error('no contract creation named ' + token)
if (lastChanges === null) { }
throw new Error('no memory found') return this.traceCache.contractCreation[token]
} }
return this.trace[lastChanges].memory
}
TraceManager.prototype.getCurrentPC = function (stepIndex) { getMemoryAt (stepIndex) {
try {
this.checkRequestedStep(stepIndex) this.checkRequestedStep(stepIndex)
} catch (check) { const lastChanges = util.findLowerBoundValue(stepIndex, this.traceCache.memoryChanges)
throw new Error(check) if (lastChanges === null) {
throw new Error('no memory found')
}
return this.trace[lastChanges].memory
} }
return this.trace[stepIndex].pc
}
TraceManager.prototype.getReturnValue = function (stepIndex) { getCurrentPC (stepIndex) {
try { try {
this.checkRequestedStep(stepIndex) this.checkRequestedStep(stepIndex)
} catch (check) { } catch (check) {
throw new Error(check) throw new Error(check)
}
return this.trace[stepIndex].pc
} }
if (!this.traceCache.returnValues[stepIndex]) {
throw new Error('current step is not a return step') getReturnValue (stepIndex) {
try {
this.checkRequestedStep(stepIndex)
} catch (check) {
throw new Error(check)
}
if (!this.traceCache.returnValues[stepIndex]) {
throw new Error('current step is not a return step')
}
return this.traceCache.returnValues[stepIndex]
} }
return this.traceCache.returnValues[stepIndex]
}
TraceManager.prototype.getCurrentStep = function (stepIndex) { getCurrentStep (stepIndex) {
try { try {
this.checkRequestedStep(stepIndex) this.checkRequestedStep(stepIndex)
} catch (check) { } catch (check) {
throw new Error(check) throw new Error(check)
}
return this.traceCache.steps[stepIndex]
} }
return this.traceCache.steps[stepIndex]
}
TraceManager.prototype.getMemExpand = function (stepIndex) { getMemExpand (stepIndex) {
return (this.getStepProperty(stepIndex, 'memexpand') || '') return (this.getStepProperty(stepIndex, 'memexpand') || '')
} }
TraceManager.prototype.getStepCost = function (stepIndex) { getStepCost (stepIndex) {
return this.getStepProperty(stepIndex, 'gasCost') return this.getStepProperty(stepIndex, 'gasCost')
} }
TraceManager.prototype.getRemainingGas = function (stepIndex) { getRemainingGas (stepIndex) {
return this.getStepProperty(stepIndex, 'gas') return this.getStepProperty(stepIndex, 'gas')
} }
TraceManager.prototype.getStepProperty = function (stepIndex, property) { getStepProperty (stepIndex, property) {
try { try {
this.checkRequestedStep(stepIndex) this.checkRequestedStep(stepIndex)
} catch (check) { } catch (check) {
throw new Error(check) throw new Error(check)
}
return this.trace[stepIndex][property]
} }
return this.trace[stepIndex][property]
}
TraceManager.prototype.isCreationStep = function (stepIndex) { isCreationStep (stepIndex) {
return traceHelper.isCreateInstruction(this.trace[stepIndex]) return traceHelper.isCreateInstruction(this.trace[stepIndex])
} }
// step section // step section
TraceManager.prototype.findStepOverBack = function (currentStep) { findStepOverBack (currentStep) {
return this.traceStepManager.findStepOverBack(currentStep) return this.traceStepManager.findStepOverBack(currentStep)
} }
TraceManager.prototype.findStepOverForward = function (currentStep) { findStepOverForward (currentStep) {
return this.traceStepManager.findStepOverForward(currentStep) return this.traceStepManager.findStepOverForward(currentStep)
} }
TraceManager.prototype.findNextCall = function (currentStep) { findNextCall (currentStep) {
return this.traceStepManager.findNextCall(currentStep) return this.traceStepManager.findNextCall(currentStep)
} }
TraceManager.prototype.findStepOut = function (currentStep) { findStepOut (currentStep) {
return this.traceStepManager.findStepOut(currentStep) return this.traceStepManager.findStepOut(currentStep)
} }
TraceManager.prototype.checkRequestedStep = function (stepIndex) { checkRequestedStep (stepIndex) {
if (!this.trace) { if (!this.trace) {
throw new Error('trace not loaded') throw new Error('trace not loaded')
} else if (stepIndex >= this.trace.length) { } else if (stepIndex >= this.trace.length) {
throw new Error('trace smaller than requested') throw new Error('trace smaller than requested')
}
} }
}
TraceManager.prototype.waterfall = function (calls, stepindex, cb) { waterfall (calls, stepindex, cb) {
let ret = [] let ret = []
let retError = null let retError = null
for (var call in calls) { for (var call in calls) {
calls[call].apply(this, [stepindex, function (error, result) { calls[call].apply(this, [stepindex, function (error, result) {
retError = error retError = error
ret.push({ error: error, value: result }) ret.push({ error: error, value: result })
}]) }])
}
cb(retError, ret)
} }
cb(retError, ret)
} }
module.exports = TraceManager

@ -4,52 +4,55 @@ const traceHelper = require('./traceHelper')
const remixLib = require('@remix-project/remix-lib') const remixLib = require('@remix-project/remix-lib')
const util = remixLib.util const util = remixLib.util
function TraceStepManager (_traceAnalyser) { export class TraceStepManager {
this.traceAnalyser = _traceAnalyser
}
TraceStepManager.prototype.isCallInstruction = function (index) { traceAnalyser
const state = this.traceAnalyser.trace[index]
return traceHelper.isCallInstruction(state) && !traceHelper.isCallToPrecompiledContract(index, this.traceAnalyser.trace)
}
TraceStepManager.prototype.isReturnInstruction = function (index) { constructor (_traceAnalyser) {
const state = this.traceAnalyser.trace[index] this.traceAnalyser = _traceAnalyser
return traceHelper.isReturnInstruction(state) }
}
TraceStepManager.prototype.findStepOverBack = function (currentStep) { isCallInstruction (index) {
if (this.isReturnInstruction(currentStep)) { const state = this.traceAnalyser.trace[index]
const call = util.findCall(currentStep, this.traceAnalyser.traceCache.callsTree.call) return traceHelper.isCallInstruction(state) && !traceHelper.isCallToPrecompiledContract(index, this.traceAnalyser.trace)
return call.start > 0 ? call.start - 1 : 0
} }
return currentStep > 0 ? currentStep - 1 : 0
}
TraceStepManager.prototype.findStepOverForward = function (currentStep) { isReturnInstruction (index) {
if (this.isCallInstruction(currentStep)) { const state = this.traceAnalyser.trace[index]
const call = util.findCall(currentStep + 1, this.traceAnalyser.traceCache.callsTree.call) return traceHelper.isReturnInstruction(state)
return call.return + 1 < this.traceAnalyser.trace.length ? call.return + 1 : this.traceAnalyser.trace.length - 1 }
findStepOverBack (currentStep) {
if (this.isReturnInstruction(currentStep)) {
const call = util.findCall(currentStep, this.traceAnalyser.traceCache.callsTree.call)
return call.start > 0 ? call.start - 1 : 0
}
return currentStep > 0 ? currentStep - 1 : 0
} }
return this.traceAnalyser.trace.length >= currentStep + 1 ? currentStep + 1 : currentStep
}
TraceStepManager.prototype.findNextCall = function (currentStep) { findStepOverForward (currentStep) {
const call = util.findCall(currentStep, this.traceAnalyser.traceCache.callsTree.call) if (this.isCallInstruction(currentStep)) {
const subCalls = Object.keys(call.calls) const call = util.findCall(currentStep + 1, this.traceAnalyser.traceCache.callsTree.call)
if (subCalls.length) { return call.return + 1 < this.traceAnalyser.trace.length ? call.return + 1 : this.traceAnalyser.trace.length - 1
var callStart = util.findLowerBound(currentStep, subCalls) + 1 }
if (subCalls.length > callStart) { return this.traceAnalyser.trace.length >= currentStep + 1 ? currentStep + 1 : currentStep
return subCalls[callStart] - 1 }
findNextCall (currentStep) {
const call = util.findCall(currentStep, this.traceAnalyser.traceCache.callsTree.call)
const subCalls = Object.keys(call.calls)
if (subCalls.length) {
var callStart = util.findLowerBound(currentStep, subCalls) + 1
if (subCalls.length > callStart) {
return parseInt(subCalls[callStart]) - 1
}
return currentStep
} }
return currentStep return currentStep
} }
return currentStep
}
TraceStepManager.prototype.findStepOut = function (currentStep) { findStepOut (currentStep) {
const call = util.findCall(currentStep, this.traceAnalyser.traceCache.callsTree.call) const call = util.findCall(currentStep, this.traceAnalyser.traceCache.callsTree.call)
return call.return return call.return
}
} }
module.exports = TraceStepManager

Loading…
Cancel
Save