use async / await

pull/7/head
yann300 8 years ago
parent b4a7df75a4
commit 891b1bb70b
  1. 10
      .babelrc
  2. 139
      src/util/internalCallTree.js
  3. 18
      test/solidity/localDecoder.js

@ -1,5 +1,13 @@
{ {
"plugins": ["fast-async", "plugins": [["fast-async", {
"runtimePatten": null,
"compiler": {
"promises": true,
"es7": true,
"noRuntime": true,
"wrapAwait": true
}
}],
"check-es2015-constants", "check-es2015-constants",
"transform-es2015-arrow-functions", "transform-es2015-arrow-functions",
"transform-es2015-block-scoped-functions", "transform-es2015-block-scoped-functions",

@ -31,11 +31,12 @@ class InternalCallTree {
if (!this.solidityProxy.loaded()) { if (!this.solidityProxy.loaded()) {
console.log('compilation result not loaded. Cannot build internal call tree') console.log('compilation result not loaded. Cannot build internal call tree')
} else { } else {
buildTree(this, 0, '', (error) => { buildTree(this, 0, '').then((result) => {
if (!error) { if (result.error) {
this.event.trigger('callTreeReady', [this.scopes, this.scopeStarts]) this.event.trigger('callTreeBuildFailed', [result])
} else { } else {
this.event.trigger('callTreeBuildFailed', [error]) console.log('ready')
this.event.trigger('callTreeReady', [this.scopes, this.scopeStarts])
} }
}) })
} }
@ -82,96 +83,72 @@ class InternalCallTree {
} }
} }
/** async function buildTree (tree, step, scopeId) {
* build tree (called recursively)
*
* @param {Object} tree - instance of InternalCallTree
* @param {Int} step - index on the vm trace
* @param {String} scopeId - deepness of the current scope
* @param {Function} cb - callback
*/
function buildTree (tree, step, scopeId, cb) {
let subScope = 1 let subScope = 1
tree.scopeStarts[step] = scopeId tree.scopeStarts[step] = scopeId
tree.scopes[scopeId] = { firstStep: step, locals: {} } tree.scopes[scopeId] = { firstStep: step, locals: {} }
visitStep(tree, step, scopeId, subScope, function (error, result) { while (step < tree.traceManager.trace.length) {
cb(error, result) var sourceLocation
}) try {
} sourceLocation = await extractSourceLocation(tree, step)
} catch (e) {
/** return { outStep: step, error: 'InternalCallTree - Error resolving source location. ' + step + ' ' + e.messager }
* visit a step (called recursively) }
* if (!sourceLocation) {
* @param {Object} tree - instance of InternalCallTree return { outStep: step, error: 'InternalCallTree - No source Location. ' + step }
* @param {Int} step - index on the vm trace }
* @param {String} scopeId - deepness of the current scope if (sourceLocation.jump === 'i') {
* @param {Int} subScope - index of the next scope from current scope var result = await buildTree(tree, step + 1, scopeId === '' ? subScope.toString() : scopeId + '.' + subScope)
* @param {Function} cb - callback if (result.error) {
*/ return result
function visitStep (tree, step, scopeId, subScope, cb) {
setTimeout(() => {
extractSourceLocation(tree, step, (error, sourceLocation) => {
if (error) {
cb(error)
} else { } else {
if (sourceLocation.jump === 'i') { step = result.outStep
buildTree(tree, step + 1, scopeId === '' ? subScope.toString() : scopeId + '.' + subScope, function (error, outStep) { subScope++
}
} else if (sourceLocation.jump === 'o') {
tree.scopes[scopeId].lastStep = step
return { outStep: step + 1 }
} else {
if (tree.includeLocalVariables) {
var variableDeclaration = resolveVariableDeclaration(tree, step, sourceLocation)
if (variableDeclaration && !tree.scopes[scopeId].locals[variableDeclaration.attributes.name]) {
tree.traceManager.getStackAt(step, (error, stack) => {
if (!error) { if (!error) {
visitStep(tree, outStep, scopeId, subScope + 1, cb) tree.solidityProxy.contractNameAt(step, (error, contractName) => { // cached
} else {
cb('error computing jump')
}
})
return
} else if (sourceLocation.jump === 'o') {
tree.scopes[scopeId].lastStep = step
cb(null, step + 1)
return
} else {
if (tree.includeLocalVariables) {
var variableDeclaration = resolveVariableDeclaration(tree, step, sourceLocation)
if (variableDeclaration && !tree.scopes[scopeId].locals[variableDeclaration.attributes.name]) {
tree.traceManager.getStackAt(step, (error, stack) => {
if (!error) { if (!error) {
tree.solidityProxy.contractNameAt(step, (error, contractName) => { // cached var states = tree.solidityProxy.extractStatesDefinitions()
if (!error) { tree.scopes[scopeId].locals[variableDeclaration.attributes.name] = {
var states = tree.solidityProxy.extractStatesDefinitions() name: variableDeclaration.attributes.name,
tree.scopes[scopeId].locals[variableDeclaration.attributes.name] = { type: decodeInfo.parseType(variableDeclaration.attributes.type, states, contractName),
name: variableDeclaration.attributes.name, stackHeight: stack.length
type: decodeInfo.parseType(variableDeclaration.attributes.type, states, contractName), }
stackHeight: stack.length
}
}
})
} }
}) })
} }
} })
step++
}
if (tree.traceManager.inRange(step)) {
visitStep(tree, step, scopeId, subScope, cb)
} else {
cb(null, step)
} }
} }
}) step++
}, 0) }
}
return { outStep: step }
} }
function extractSourceLocation (tree, step, cb) { function extractSourceLocation (tree, step) {
tree.traceManager.getCurrentCalledAddressAt(step, (error, address) => { return new Promise(function (resolve, reject) {
if (!error) { tree.traceManager.getCurrentCalledAddressAt(step, (error, address) => {
tree.sourceLocationTracker.getSourceLocationFromVMTraceIndex(address, step, tree.solidityProxy.contracts, (error, sourceLocation) => { if (!error) {
if (!error) { tree.sourceLocationTracker.getSourceLocationFromVMTraceIndex(address, step, tree.solidityProxy.contracts, (error, sourceLocation) => {
cb(null, sourceLocation) if (!error) {
} else { return resolve(sourceLocation)
cb('InternalCallTree - Cannot retrieve sourcelocation for step ' + step) } else {
} return reject('InternalCallTree - Cannot retrieve sourcelocation for step ' + step)
}) }
} else { })
cb('InternalCallTree - Cannot retrieve address for step ' + step) } else {
} return reject('InternalCallTree - Cannot retrieve address for step ' + step)
}
})
}) })
} }

@ -2,20 +2,20 @@
var tape = require('tape') var tape = require('tape')
var compiler = require('solc') var compiler = require('solc')
var intLocal = require('./contracts/intLocal') var intLocal = require('./contracts/intLocal')
var TraceManager = require('../../src/trace/traceManager') var TraceManager = require('../../babelify-src/trace/traceManager')
var CodeManager = require('../../src/code/codeManager') var CodeManager = require('../../babelify-src/code/codeManager')
var VM = require('ethereumjs-vm') var VM = require('ethereumjs-vm')
var Tx = require('ethereumjs-tx') var Tx = require('ethereumjs-tx')
var Block = require('ethereumjs-block') var Block = require('ethereumjs-block')
var BN = require('ethereumjs-util').BN var BN = require('ethereumjs-util').BN
var utileth = require('ethereumjs-util') var utileth = require('ethereumjs-util')
var Web3Providers = require('../../src/web3Provider/web3Providers') var Web3Providers = require('../../babelify-src/web3Provider/web3Providers')
var traceHelper = require('../../src/helpers/traceHelper') var traceHelper = require('../../babelify-src/helpers/traceHelper')
var util = require('../../src/helpers/global') var util = require('../../babelify-src/helpers/global')
var SolidityProxy = require('../../src/solidity/solidityProxy') var SolidityProxy = require('../../babelify-src/solidity/solidityProxy')
var InternalCallTree = require('../../src/util/internalCallTree') var InternalCallTree = require('../../babelify-src/util/internalCallTree')
var EventManager = require('../../src/lib/eventManager') var EventManager = require('../../babelify-src/lib/eventManager')
var localDecoder = require('../../src/solidity/localDecoder') var localDecoder = require('../../babelify-src/solidity/localDecoder')
tape('solidity', function (t) { tape('solidity', function (t) {
t.test('local decoder', function (st) { t.test('local decoder', function (st) {

Loading…
Cancel
Save