Merge pull request #350 from ethereum/remix-libs-refactor-2
Refactor remix lib - remix-debug - remix-soliditypull/5370/head
commit
1ebfc209dc
@ -1,58 +0,0 @@ |
||||
const remixLib = require('@remix-project/remix-lib') |
||||
|
||||
const EventManager = remixLib.EventManager |
||||
const Web3Providers = remixLib.vm.Web3Providers |
||||
const DummyProvider = remixLib.vm.DummyProvider |
||||
const init = remixLib.init |
||||
|
||||
class ContextManager { |
||||
constructor (executionContext) { |
||||
this.executionContext = executionContext |
||||
this.web3 = this.executionContext.web3() |
||||
this.event = new EventManager() |
||||
} |
||||
|
||||
initProviders () { |
||||
this.web3Providers = new Web3Providers() |
||||
this.addProvider('DUMMYWEB3', new DummyProvider()) |
||||
this.switchProvider('DUMMYWEB3') |
||||
|
||||
this.addProvider('vm', this.executionContext.vm()) |
||||
this.addProvider('injected', this.executionContext.internalWeb3()) |
||||
this.addProvider('web3', this.executionContext.internalWeb3()) |
||||
this.switchProvider(this.executionContext.getProvider()) |
||||
} |
||||
|
||||
getWeb3 () { |
||||
return this.web3 |
||||
} |
||||
|
||||
addProvider (type, obj) { |
||||
this.web3Providers.addProvider(type, obj) |
||||
this.event.trigger('providerAdded', [type]) |
||||
} |
||||
|
||||
switchProvider (type, cb) { |
||||
this.web3Providers.get(type, (error, obj) => { |
||||
if (error) { |
||||
// console.log('provider ' + type + ' not defined')
|
||||
} else { |
||||
this.web3 = obj |
||||
this.executionContext.detectNetwork((error, network) => { |
||||
if (error || !network) { |
||||
this.web3 = obj |
||||
} else { |
||||
var webDebugNode = init.web3DebugNode(network.name) |
||||
this.web3 = (!webDebugNode ? obj : webDebugNode) |
||||
} |
||||
this.event.trigger('providerChanged', [type, this.web3]) |
||||
if (cb) return cb() |
||||
}) |
||||
this.event.trigger('providerChanged', [type, this.web3]) |
||||
} |
||||
}) |
||||
} |
||||
|
||||
} |
||||
|
||||
module.exports = ContextManager |
@ -1,8 +1,8 @@ |
||||
'use strict' |
||||
|
||||
const EventManager = require('../eventManager') |
||||
const traceHelper = require('../helpers/traceHelper') |
||||
const SourceMappingDecoder = require('../sourceMappingDecoder') |
||||
const traceHelper = require('../trace/traceHelper') |
||||
const SourceMappingDecoder = require('../source/sourceMappingDecoder') |
||||
const CodeResolver = require('./codeResolver') |
||||
|
||||
/* |
@ -1,7 +1,8 @@ |
||||
'use strict' |
||||
|
||||
const parseCode = require('./codeUtils').parseCode |
||||
const util = require('../util') |
||||
const remixLib = require('@remix-project/remix-lib') |
||||
const util = remixLib.util |
||||
|
||||
const createExpressions = function (instructions) { |
||||
const expressions = [] |
@ -0,0 +1,70 @@ |
||||
'use strict' |
||||
|
||||
function eventManager () { |
||||
this.registered = {} |
||||
this.anonymous = {} |
||||
} |
||||
|
||||
/* |
||||
* Unregister a listener. |
||||
* Note that if obj is a function. the unregistration will be applied to the dummy obj {}. |
||||
* |
||||
* @param {String} eventName - the event name |
||||
* @param {Object or Func} obj - object that will listen on this event |
||||
* @param {Func} func - function of the listeners that will be executed |
||||
*/ |
||||
eventManager.prototype.unregister = function (eventName, obj, func) { |
||||
if (!this.registered[eventName]) { |
||||
return |
||||
} |
||||
if (obj instanceof Function) { |
||||
func = obj |
||||
obj = this.anonymous |
||||
} |
||||
for (let reg in this.registered[eventName]) { |
||||
if (this.registered[eventName][reg].obj === obj && this.registered[eventName][reg].func === func) { |
||||
this.registered[eventName].splice(reg, 1) |
||||
} |
||||
} |
||||
} |
||||
|
||||
/* |
||||
* Register a new listener. |
||||
* Note that if obj is a function, the function registration will be associated with the dummy object {} |
||||
* |
||||
* @param {String} eventName - the event name |
||||
* @param {Object or Func} obj - object that will listen on this event |
||||
* @param {Func} func - function of the listeners that will be executed |
||||
*/ |
||||
eventManager.prototype.register = function (eventName, obj, func) { |
||||
if (!this.registered[eventName]) { |
||||
this.registered[eventName] = [] |
||||
} |
||||
if (obj instanceof Function) { |
||||
func = obj |
||||
obj = this.anonymous |
||||
} |
||||
this.registered[eventName].push({ |
||||
obj: obj, |
||||
func: func |
||||
}) |
||||
} |
||||
|
||||
/* |
||||
* trigger event. |
||||
* Every listener have their associated function executed |
||||
* |
||||
* @param {String} eventName - the event name |
||||
* @param {Array}j - argument that will be passed to the executed function. |
||||
*/ |
||||
eventManager.prototype.trigger = function (eventName, args) { |
||||
if (!this.registered[eventName]) { |
||||
return |
||||
} |
||||
for (let listener in this.registered[eventName]) { |
||||
const l = this.registered[eventName][listener] |
||||
l.func.apply(l.obj === this.anonymous ? {} : l.obj, args) |
||||
} |
||||
} |
||||
|
||||
module.exports = eventManager |
@ -0,0 +1,76 @@ |
||||
'use strict' |
||||
const Web3 = require('web3') |
||||
|
||||
module.exports = { |
||||
loadWeb3: function (url) { |
||||
if (!url) url = 'http://localhost:8545' |
||||
const web3 = new Web3() |
||||
web3.setProvider(new web3.providers.HttpProvider(url)) |
||||
this.extend(web3) |
||||
return web3 |
||||
}, |
||||
|
||||
extendWeb3: function (web3) { |
||||
this.extend(web3) |
||||
}, |
||||
|
||||
setProvider: function (web3, url) { |
||||
web3.setProvider(new web3.providers.HttpProvider(url)) |
||||
}, |
||||
|
||||
web3DebugNode: function (network) { |
||||
if (web3DebugNodes[network]) { |
||||
return this.loadWeb3(web3DebugNodes[network]) |
||||
} |
||||
return null |
||||
}, |
||||
|
||||
extend: function (web3) { |
||||
if (!web3.extend) { |
||||
return |
||||
} |
||||
// DEBUG
|
||||
const methods = [] |
||||
if (!(web3.debug && web3.debug.preimage)) { |
||||
methods.push(new web3.extend.Method({ |
||||
name: 'preimage', |
||||
call: 'debug_preimage', |
||||
inputFormatter: [null], |
||||
params: 1 |
||||
})) |
||||
} |
||||
|
||||
if (!(web3.debug && web3.debug.traceTransaction)) { |
||||
methods.push(new web3.extend.Method({ |
||||
name: 'traceTransaction', |
||||
call: 'debug_traceTransaction', |
||||
inputFormatter: [null, null], |
||||
params: 2 |
||||
})) |
||||
} |
||||
|
||||
if (!(web3.debug && web3.debug.storageRangeAt)) { |
||||
methods.push(new web3.extend.Method({ |
||||
name: 'storageRangeAt', |
||||
call: 'debug_storageRangeAt', |
||||
inputFormatter: [null, null, null, null, null], |
||||
params: 5 |
||||
})) |
||||
} |
||||
if (methods.length > 0) { |
||||
web3.extend({ |
||||
property: 'debug', |
||||
methods: methods, |
||||
properties: [] |
||||
}) |
||||
} |
||||
} |
||||
} |
||||
|
||||
const web3DebugNodes = { |
||||
'Main': 'https://gethmainnet.komputing.org', |
||||
'Rinkeby': 'https://remix-rinkeby.ethdevops.io', |
||||
'Ropsten': 'https://remix-ropsten.ethdevops.io', |
||||
'Goerli': 'https://remix-goerli.ethdevops.io', |
||||
'Kovan': 'https://remix-kovan.ethdevops.io' |
||||
} |
@ -1,8 +1,9 @@ |
||||
'use strict' |
||||
const EventManager = require('./eventManager') |
||||
const helper = require('./helpers/traceHelper') |
||||
const EventManager = require('../eventManager') |
||||
const helper = require('../trace/traceHelper') |
||||
const SourceMappingDecoder = require('./sourceMappingDecoder') |
||||
const util = require('./util') |
||||
const remixLib = require('@remix-project/remix-lib') |
||||
const util = remixLib.util |
||||
|
||||
/** |
||||
* Process the source code location for the current executing bytecode |
@ -1,5 +1,6 @@ |
||||
'use strict' |
||||
const util = require('./util') |
||||
const remixLib = require('@remix-project/remix-lib') |
||||
const util = remixLib.util |
||||
const AstWalker = require('./astWalker') |
||||
|
||||
/** |
@ -1,5 +1,5 @@ |
||||
'use strict' |
||||
const traceHelper = require('../helpers/traceHelper') |
||||
const traceHelper = require('./traceHelper') |
||||
|
||||
function TraceAnalyser (_cache) { |
||||
this.traceCache = _cache |
@ -1,5 +1,6 @@ |
||||
'use strict' |
||||
const helper = require('../util') |
||||
const remixLib = require('@remix-project/remix-lib') |
||||
const helper = remixLib.util |
||||
|
||||
function TraceCache () { |
||||
this.init() |
@ -1,5 +1,6 @@ |
||||
'use strict' |
||||
const ui = require('./uiHelper') |
||||
const remixLib = require('@remix-project/remix-lib') |
||||
const ui = remixLib.helpers.ui |
||||
|
||||
module.exports = { |
||||
// vmTraceIndex has to point to a CALL, CODECALL, ...
|
@ -1,7 +1,8 @@ |
||||
'use strict' |
||||
|
||||
const traceHelper = require('../helpers/traceHelper') |
||||
const util = require('../util') |
||||
const traceHelper = require('./traceHelper') |
||||
const remixLib = require('@remix-project/remix-lib') |
||||
const util = remixLib.util |
||||
|
||||
function TraceStepManager (_traceAnalyser) { |
||||
this.traceAnalyser = _traceAnalyser |
@ -1,6 +1,6 @@ |
||||
'use strict' |
||||
const tape = require('tape') |
||||
const AstWalker = require('../src/astWalker') |
||||
const AstWalker = require('../src/source/astWalker') |
||||
const node = require('./resources/ast') |
||||
|
||||
tape('ASTWalker', function (t) { |
@ -0,0 +1,26 @@ |
||||
module.exports = { |
||||
compilerInput: compilerInput |
||||
} |
||||
|
||||
function compilerInput (contracts) { |
||||
return JSON.stringify({ |
||||
language: 'Solidity', |
||||
sources: { |
||||
'test.sol': { |
||||
content: contracts |
||||
} |
||||
}, |
||||
settings: { |
||||
optimizer: { |
||||
enabled: false, |
||||
runs: 200 |
||||
}, |
||||
outputSelection: { |
||||
'*': { |
||||
'': [ 'legacyAST', 'ast' ], |
||||
'*': [ 'abi', 'metadata', 'evm.legacyAssembly', 'evm.bytecode', 'evm.deployedBytecode', 'evm.methodIdentifiers', 'evm.gasEstimates' ] |
||||
} |
||||
} |
||||
} |
||||
}) |
||||
} |
@ -1,9 +1,9 @@ |
||||
'use strict' |
||||
const tape = require('tape') |
||||
const sourceMapping = require('./resources/sourceMapping') |
||||
const SourceMappingDecoder = require('../src/sourceMappingDecoder') |
||||
const SourceMappingDecoder = require('../src/source/sourceMappingDecoder') |
||||
const compiler = require('solc') |
||||
const compilerInput = require('../src/helpers/compilerHelper').compilerInput |
||||
const compilerInput = require('./helpers/compilerHelper').compilerInput |
||||
|
||||
tape('SourceMappingDecoder', function (t) { |
||||
t.test('SourceMappingDecoder.findNodeAtInstructionIndex', function (st) { |
@ -1,32 +1,18 @@ |
||||
'use strict' |
||||
const TraceManager = require('../src/trace/traceManager') |
||||
const tape = require('tape') |
||||
const Web3Providers = require('../src/web3Provider/web3Providers') |
||||
const web3Test = require('./resources/testWeb3') |
||||
|
||||
let web3 = null |
||||
|
||||
tape('TraceManager', function (t) { |
||||
let traceManager |
||||
|
||||
t.test('TraceManager.init', function (st) { |
||||
const web3Providers = new Web3Providers() |
||||
web3Providers.addProvider('TEST', web3Test) |
||||
web3Providers.get('TEST', function (error, obj) { |
||||
if (error) { |
||||
const mes = 'provider TEST not defined' |
||||
console.log(mes) |
||||
st.fail(mes) |
||||
} else { |
||||
web3 = obj |
||||
traceManager = new TraceManager({web3: web3}) |
||||
st.end() |
||||
} |
||||
}) |
||||
traceManager = new TraceManager({web3: web3Test}) |
||||
st.end() |
||||
}) |
||||
|
||||
t.test('TraceManager.resolveTrace', function (st) { |
||||
const tx = web3.eth.getTransaction('0x20ef65b8b186ca942fcccd634f37074dde49b541c27994fc7596740ef44cfd51') |
||||
const tx = web3Test.eth.getTransaction('0x20ef65b8b186ca942fcccd634f37074dde49b541c27994fc7596740ef44cfd51') |
||||
traceManager.resolveTrace(tx).then(() => { |
||||
st.end() |
||||
}).catch(() => { |
@ -1,51 +0,0 @@ |
||||
'use strict' |
||||
const init = require('../init') |
||||
const web3Override = {} |
||||
web3Override.eth = {} |
||||
web3Override.debug = {} |
||||
let data = init.readFile(require('path').resolve(__dirname, 'testWeb3.json')) |
||||
data = JSON.parse(data) |
||||
|
||||
web3Override.eth.getCode = function (address, callback) { |
||||
if (callback) { |
||||
callback(null, data.testCodes[address]) |
||||
} else { |
||||
return data.testCodes[address] |
||||
} |
||||
} |
||||
|
||||
web3Override.debug.traceTransaction = function (txHash, options, callback) { |
||||
callback(null, data.testTraces[txHash]) |
||||
} |
||||
|
||||
web3Override.debug.storageRangeAt = function (blockNumber, txIndex, address, start, maxSize, callback) { |
||||
callback(null, { storage: {}, complete: true }) |
||||
} |
||||
|
||||
web3Override.eth.getTransaction = function (txHash, callback) { |
||||
if (callback) { |
||||
callback(null, data.testTxs[txHash]) |
||||
} else { |
||||
return data.testTxs[txHash] |
||||
} |
||||
} |
||||
|
||||
web3Override.eth.getTransactionFromBlock = function (blockNumber, txIndex, callback) { |
||||
if (callback) { |
||||
callback(null, data.testTxsByBlock[blockNumber + '-' + txIndex]) |
||||
} else { |
||||
return data.testTxsByBlock[blockNumber + '-' + txIndex] |
||||
} |
||||
} |
||||
|
||||
web3Override.eth.getBlockNumber = function (callback) { callback('web3 modified testing purposes :)') } |
||||
|
||||
web3Override.eth.setProvider = function (provider) {} |
||||
|
||||
web3Override.eth.providers = { 'HttpProvider': function (url) {} } |
||||
|
||||
web3Override.eth.currentProvider = {'host': 'test provider'} |
||||
|
||||
if (typeof (module) !== 'undefined' && typeof (module.exports) !== 'undefined') { |
||||
module.exports = web3Override |
||||
} |
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
@ -1,11 +1,5 @@ |
||||
require('./astwalker.js') |
||||
require('./eventManager.js') |
||||
require('./sourceMappingDecoder.js') |
||||
require('./util.js') |
||||
require('./txFormat.js') |
||||
require('./txHelper.js') |
||||
|
||||
require('./traceManager.js') |
||||
require('./codeManager.js') |
||||
require('./disassembler.js') |
||||
require('./txResultHelper.js') |
||||
|
@ -0,0 +1,68 @@ |
||||
'use strict' |
||||
|
||||
export default class EventManager { |
||||
registered: any = {} // eslint-disable-line
|
||||
anonymous: any = {} // eslint-disable-line
|
||||
|
||||
/* |
||||
* Unregister a listener. |
||||
* Note that if obj is a function. the unregistration will be applied to the dummy obj {}. |
||||
* |
||||
* @param {String} eventName - the event name |
||||
* @param {Object or Func} obj - object that will listen on this event |
||||
* @param {Func} func - function of the listeners that will be executed |
||||
*/ |
||||
unregister (eventName: any, obj: any, func: any): void { // eslint-disable-line
|
||||
if (!this.registered[eventName]) { |
||||
return |
||||
} |
||||
if (obj instanceof Function) { |
||||
func = obj |
||||
obj = this.anonymous |
||||
} |
||||
for (const reg in this.registered[eventName]) { |
||||
if (this.registered[eventName][reg].obj === obj && this.registered[eventName][reg].func === func) { |
||||
this.registered[eventName].splice(reg, 1) |
||||
} |
||||
} |
||||
} |
||||
|
||||
/* |
||||
* Register a new listener. |
||||
* Note that if obj is a function, the function registration will be associated with the dummy object {} |
||||
* |
||||
* @param {String} eventName - the event name |
||||
* @param {Object or Func} obj - object that will listen on this event |
||||
* @param {Func} func - function of the listeners that will be executed |
||||
*/ |
||||
register (eventName: any, obj: any, func: any): void { // eslint-disable-line
|
||||
if (!this.registered[eventName]) { |
||||
this.registered[eventName] = [] |
||||
} |
||||
if (obj instanceof Function) { |
||||
func = obj |
||||
obj = this.anonymous |
||||
} |
||||
this.registered[eventName].push({ |
||||
obj: obj, |
||||
func: func |
||||
}) |
||||
} |
||||
|
||||
/* |
||||
* trigger event. |
||||
* Every listener have their associated function executed |
||||
* |
||||
* @param {String} eventName - the event name |
||||
* @param {Array}j - argument that will be passed to the executed function. |
||||
*/ |
||||
trigger (eventName: any, args: any): void { // eslint-disable-line
|
||||
if (!this.registered[eventName]) { |
||||
return |
||||
} |
||||
for (const listener in this.registered[eventName]) { |
||||
const l = this.registered[eventName][listener] |
||||
l.func.apply(l.obj === this.anonymous ? {} : l.obj, args) |
||||
} |
||||
} |
||||
} |
Loading…
Reference in new issue