Merge pull request #37 from yann300/calldata

get calldata from memory
pull/7/head
yann300 9 years ago committed by GitHub
commit 5ee7ba8b1e
  1. 2
      src/TxBrowser.js
  2. 46
      src/trace/traceAnalyser.js
  3. 4
      src/trace/traceCache.js
  4. 2
      src/trace/traceManager.js

@ -10,7 +10,7 @@ function TxBrowser (_web3) {
this.web3 = _web3 this.web3 = _web3
this.blockNumber this.blockNumber
this.txNumber = '0x20ef65b8b186ca942fcccd634f37074dde49b541c27994fc7596740ef44cfd51' this.txNumber = '0x71a6d583d16d142c5c3e8903060e8a4ee5a5016348a9448df6c3e63b68076ec4'
this.hash this.hash
this.from this.from
this.to this.to

@ -21,20 +21,40 @@ TraceAnalyser.prototype.analyse = function (trace, tx, callback) {
if (traceHelper.isContractCreation(tx.to)) { if (traceHelper.isContractCreation(tx.to)) {
this.traceCache.pushContractCreation(tx.to, tx.input) this.traceCache.pushContractCreation(tx.to, tx.input)
} }
this.buildCalldata(0, this.trace[0], tx, true)
for (var k = 0; k < this.trace.length; k++) { for (var k = 0; k < this.trace.length; k++) {
var step = this.trace[k] var step = this.trace[k]
this.buildCalldata(k, step)
this.buildMemory(k, step) this.buildMemory(k, step)
this.buildDepth(k, step, callStack) this.buildDepth(k, step, tx, callStack)
context = this.buildStorage(k, step, context) context = this.buildStorage(k, step, context)
} }
callback(null, true) callback(null, true)
} }
TraceAnalyser.prototype.buildCalldata = function (index, step) { TraceAnalyser.prototype.buildCalldata = function (index, step, tx, newContext) {
if (step.calldata) { var calldata = ''
this.traceCache.pushCallDataChanges(index) if (index === 0) {
calldata = tx.input
this.traceCache.pushCallDataChanges(index, calldata)
} else if (!newContext) {
var lastCall = this.traceCache.callsData[this.traceCache.callDataChanges[this.traceCache.callDataChanges.length - 2]]
this.traceCache.pushCallDataChanges(index + 1, lastCall)
} else {
var memory = this.trace[this.traceCache.memoryChanges[this.traceCache.memoryChanges.length - 1]].memory
var callStep = this.trace[index]
var stack = callStep.stack
var offset = ''
var size = ''
if (callStep.op === 'DELEGATECALL') {
offset = 2 * parseInt(stack[stack.length - 3], 16)
size = 2 * parseInt(stack[stack.length - 4], 16)
} else {
offset = 2 * parseInt(stack[stack.length - 4], 16)
size = 2 * parseInt(stack[stack.length - 5], 16)
}
calldata = '0x' + memory.join('').substr(offset, size)
this.traceCache.pushCallDataChanges(index + 1, calldata)
} }
} }
@ -62,7 +82,7 @@ TraceAnalyser.prototype.buildStorage = function (index, step, context) {
return context return context
} }
TraceAnalyser.prototype.buildDepth = function (index, step, callStack) { TraceAnalyser.prototype.buildDepth = function (index, step, tx, callStack) {
if (traceHelper.isCallInstruction(step) && !traceHelper.isCallToPrecompiledContract(index, this.trace)) { if (traceHelper.isCallInstruction(step) && !traceHelper.isCallToPrecompiledContract(index, this.trace)) {
if (traceHelper.isCreateInstruction(step)) { if (traceHelper.isCreateInstruction(step)) {
var contractToken = traceHelper.contractCreationToken(index) var contractToken = traceHelper.contractCreationToken(index)
@ -81,12 +101,16 @@ TraceAnalyser.prototype.buildDepth = function (index, step, callStack) {
this.traceCache.pushCallStack(index + 1, { this.traceCache.pushCallStack(index + 1, {
callStack: callStack.slice(0) callStack: callStack.slice(0)
}) })
this.buildCalldata(index, step, tx, true)
} else if (traceHelper.isReturnInstruction(step)) { } else if (traceHelper.isReturnInstruction(step)) {
callStack.pop() if (index + 1 < this.trace.length) {
this.traceCache.pushCallChanges(step, index + 1) callStack.pop()
this.traceCache.pushCallStack(index + 1, { this.traceCache.pushCallChanges(step, index + 1)
callStack: callStack.slice(0) this.traceCache.pushCallStack(index + 1, {
}) callStack: callStack.slice(0)
})
this.buildCalldata(index, step, tx, false)
}
} }
} }

@ -9,6 +9,7 @@ TraceCache.prototype.init = function () {
this.callChanges = [] this.callChanges = []
this.returnChanges = [] this.returnChanges = []
this.calls = {} this.calls = {}
this.callsData = {}
this.contractCreation = {} this.contractCreation = {}
this.callDataChanges = [] this.callDataChanges = []
@ -18,8 +19,9 @@ TraceCache.prototype.init = function () {
this.callStack = {} // contains all callStack by vmtrace index (we need to rebuild it, callstack is not included in the vmtrace) this.callStack = {} // contains all callStack by vmtrace index (we need to rebuild it, callstack is not included in the vmtrace)
} }
TraceCache.prototype.pushCallDataChanges = function (value) { TraceCache.prototype.pushCallDataChanges = function (value, calldata) {
this.callDataChanges.push(value) this.callDataChanges.push(value)
this.callsData[value] = calldata
} }
TraceCache.prototype.pushMemoryChanges = function (value) { TraceCache.prototype.pushMemoryChanges = function (value) {

@ -101,7 +101,7 @@ TraceManager.prototype.getCallDataAt = function (stepIndex, callback) {
} }
var callDataChange = traceHelper.findLowerBound(stepIndex, this.traceCache.callDataChanges) var callDataChange = traceHelper.findLowerBound(stepIndex, this.traceCache.callDataChanges)
if (callDataChange === undefined) return callback('no calldata found', null) if (callDataChange === undefined) return callback('no calldata found', null)
callback(null, [this.trace[callDataChange].calldata]) callback(null, [this.traceCache.callsData[callDataChange]])
} }
TraceManager.prototype.getCallStackAt = function (stepIndex, callback) { TraceManager.prototype.getCallStackAt = function (stepIndex, callback) {

Loading…
Cancel
Save