pass executionContext as a param

pull/7/head
Iuri Matias 5 years ago
parent 9162f870da
commit 14f3c20278
  1. 3
      remix-debug/src/cmdline/contextManager.js
  2. 5
      remix-debug/src/cmdline/index.js
  3. 29
      remix-lib/src/execution/txListener.js
  4. 38
      remix-lib/src/execution/txRunner.js
  5. 56
      remix-lib/src/universalDapp.js
  6. 2
      remix-simulator/src/methods/txProcess.js

@ -1,13 +1,12 @@
var remixLib = require('remix-lib')
var EventManager = remixLib.EventManager
var executionContext = remixLib.execution.executionContext
var Web3Providers = remixLib.vm.Web3Providers
var DummyProvider = remixLib.vm.DummyProvider
var init = remixLib.init
class ContextManager {
constructor () {
constructor (executionContext) {
this.executionContext = executionContext
this.web3 = this.executionContext.web3()
this.event = new EventManager()

@ -2,10 +2,13 @@ var Web3 = require('web3')
var Debugger = require('../debugger/debugger.js')
var ContextManager = require('./contextManager.js')
var EventManager = require('events')
var remixLib = require('remix-lib')
var executionContext = remixLib.execution.executionContext
class CmdLine {
constructor () {
this.executionContext = executionContext
this.events = new EventManager()
this.lineColumnPos = null
this.rawLocation = null
@ -30,7 +33,7 @@ class CmdLine {
initDebugger (cb) {
const self = this
this.contextManager = new ContextManager()
this.contextManager = new ContextManager(this.executionContext)
this.debugger = new Debugger({
web3: this.contextManager.getWeb3(),

@ -5,7 +5,7 @@ var ethJSUtil = require('ethereumjs-util')
var EventManager = require('../eventManager')
var codeUtil = require('../util')
var executionContext = require('./execution-context')
var defaultExecutionContext = require('./execution-context')
var txFormat = require('./txFormat')
var txHelper = require('./txHelper')
@ -17,8 +17,10 @@ var txHelper = require('./txHelper')
*
*/
class TxListener {
constructor (opt) {
constructor (opt, executionContext) {
this.event = new EventManager()
// has a default for now for backwards compatability
this.executionContext = executionContext || defaultExecutionContext
this._api = opt.api
this._resolvedTransactions = {}
this._resolvedContracts = {}
@ -26,7 +28,7 @@ class TxListener {
this._listenOnNetwork = false
this._loopId = null
this.init()
executionContext.event.register('contextChanged', (context) => {
this.executionContext.event.register('contextChanged', (context) => {
if (this._isListening) {
this.stopListening()
this.startListening()
@ -39,15 +41,16 @@ class TxListener {
// in VM mode
// in web3 mode && listen remix txs only
if (!this._isListening) return // we don't listen
if (this._loopId && executionContext.getProvider() !== 'vm') return // we seems to already listen on a "web3" network
if (this._loopId && this.executionContext.getProvider() !== 'vm') return // we seems to already listen on a "web3" network
var call = {
from: from,
to: to,
input: data,
hash: txResult.transactionHash ? txResult.transactionHash : 'call' + (from || '') + to + data,
isCall: true,
returnValue: executionContext.isVM() ? txResult.result.execResult.returnValue : ethJSUtil.toBuffer(txResult.result),
envMode: executionContext.getProvider()
returnValue: this.executionContext.isVM() ? txResult.result.execResult.returnValue : ethJSUtil.toBuffer(txResult.result),
envMode: this.executionContext.getProvider()
}
addExecutionCosts(txResult, call)
@ -65,12 +68,12 @@ class TxListener {
// in VM mode
// in web3 mode && listen remix txs only
if (!this._isListening) return // we don't listen
if (this._loopId && executionContext.getProvider() !== 'vm') return // we seems to already listen on a "web3" network
executionContext.web3().eth.getTransaction(txResult.transactionHash, (error, tx) => {
if (this._loopId && this.executionContext.getProvider() !== 'vm') return // we seems to already listen on a "web3" network
this.executionContext.web3().eth.getTransaction(txResult.transactionHash, (error, tx) => {
if (error) return console.log(error)
addExecutionCosts(txResult, tx)
tx.envMode = executionContext.getProvider()
tx.envMode = this.executionContext.getProvider()
tx.status = txResult.result.status // 0x0 or 0x1
this._resolve([tx], () => {
})
@ -120,7 +123,7 @@ class TxListener {
startListening () {
this.init()
this._isListening = true
if (this._listenOnNetwork && executionContext.getProvider() !== 'vm') {
if (this._listenOnNetwork && this.executionContext.getProvider() !== 'vm') {
this._startListenOnNetwork()
}
}
@ -142,7 +145,7 @@ class TxListener {
_startListenOnNetwork () {
this._loopId = setInterval(() => {
var currentLoopId = this._loopId
executionContext.web3().eth.getBlockNumber((error, blockNumber) => {
this.executionContext.web3().eth.getBlockNumber((error, blockNumber) => {
if (this._loopId === null) return
if (error) return console.log(error)
if (currentLoopId === this._loopId && (!this.lastBlock || blockNumber > this.lastBlock)) {
@ -163,7 +166,7 @@ class TxListener {
}
_manageBlock (blockNumber) {
executionContext.web3().eth.getBlock(blockNumber, true, (error, result) => {
this.executionContext.web3().eth.getBlock(blockNumber, true, (error, result) => {
if (!error) {
this._newBlock(Object.assign({type: 'web3'}, result))
}
@ -240,7 +243,7 @@ class TxListener {
// first check known contract, resolve against the `runtimeBytecode` if not known
contractName = this._resolvedContracts[tx.to]
if (!contractName) {
executionContext.web3().eth.getCode(tx.to, (error, code) => {
this.executionContext.web3().eth.getCode(tx.to, (error, code) => {
if (error) return cb(error)
if (code) {
var contractName = this._tryResolveContract(code, contracts, false)

@ -3,16 +3,18 @@ var EthJSTX = require('ethereumjs-tx').Transaction
var EthJSBlock = require('ethereumjs-block')
var ethJSUtil = require('ethereumjs-util')
var BN = ethJSUtil.BN
var executionContext = require('./execution-context')
var defaultExecutionContext = require('./execution-context')
var EventManager = require('../eventManager')
class TxRunner {
constructor (vmaccounts, api) {
constructor (vmaccounts, api, executionContext) {
this.event = new EventManager()
// has a default for now for backwards compatability
this.executionContext = executionContext || defaultExecutionContext
this._api = api
this.blockNumber = 0
this.runAsync = true
if (executionContext.isVM()) {
if (this.executionContext.isVM()) {
// this.blockNumber = 1150000 // The VM is running in Homestead mode, which started at this block.
this.blockNumber = 0 // The VM is running in Homestead mode, which started at this block.
this.runAsync = false // We have to run like this cause the VM Event Manager does not support running multiple txs at the same time.
@ -32,18 +34,18 @@ class TxRunner {
}
_executeTx (tx, gasPrice, api, promptCb, callback) {
if (gasPrice) tx.gasPrice = executionContext.web3().utils.toHex(gasPrice)
if (gasPrice) tx.gasPrice = this.executionContext.web3().toHex(gasPrice)
if (api.personalMode()) {
promptCb(
(value) => {
this._sendTransaction(executionContext.web3().personal.sendTransaction, tx, value, callback)
this._sendTransaction(this.executionContext.web3().personal.sendTransaction, tx, value, callback)
},
() => {
return callback('Canceled by user.')
}
)
} else {
this._sendTransaction(executionContext.web3().eth.sendTransaction, tx, null, callback)
this._sendTransaction(this.executionContext.web3().eth.sendTransaction, tx, null, callback)
}
}
@ -83,7 +85,7 @@ class TxRunner {
data = '0x' + data
}
if (!executionContext.isVM()) {
if (!this.executionContext.isVM()) {
self.runInNode(args.from, args.to, data, args.value, args.gasLimit, args.useCall, confirmationCb, gasEstimationForceSend, promptCb, callback)
} else {
try {
@ -101,7 +103,7 @@ class TxRunner {
return callback('Invalid account selected')
}
executionContext.vm().stateManager.getAccount(Buffer.from(from.replace('0x', ''), 'hex'), (err, res) => {
this.executionContext.vm().stateManager.getAccount(Buffer.from(from.replace('0x', ''), 'hex'), (err, res) => {
if (err) {
callback('Account not found')
} else {
@ -132,9 +134,9 @@ class TxRunner {
++self.blockNumber
this.runBlockInVm(tx, block, callback)
} else {
executionContext.vm().stateManager.checkpoint(() => {
this.executionContext.vm().stateManager.checkpoint(() => {
this.runBlockInVm(tx, block, (err, result) => {
executionContext.vm().stateManager.revert(() => {
this.executionContext.vm().stateManager.revert(() => {
callback(err, result)
})
})
@ -145,14 +147,14 @@ class TxRunner {
}
runBlockInVm (tx, block, callback) {
executionContext.vm().runBlock({ block: block, generate: true, skipBlockValidation: true, skipBalance: false }).then(function (results) {
this.executionContext.vm().runBlock({ block: block, generate: true, skipBlockValidation: true, skipBalance: false }).then(function (results) {
let result = results.results[0]
if (result) {
const status = result.execResult.exceptionError ? 0 : 1
result.status = `0x${status}`
}
executionContext.addBlock(block)
executionContext.trackTx('0x' + tx.hash().toString('hex'), block)
this.executionContext.addBlock(block)
this.executionContext.trackTx('0x' + tx.hash().toString('hex'), block)
callback(null, {
result: result,
transactionHash: ethJSUtil.bufferToHex(Buffer.from(tx.hash()))
@ -168,14 +170,14 @@ class TxRunner {
if (useCall) {
tx.gas = gasLimit
return executionContext.web3().eth.call(tx, function (error, result) {
return this.executionContext.web3().eth.call(tx, function (error, result) {
callback(error, {
result: result,
transactionHash: result ? result.transactionHash : null
})
})
}
executionContext.web3().eth.estimateGas(tx, function (err, gasEstimation) {
this.executionContext.web3().eth.estimateGas(tx, function (err, gasEstimation) {
gasEstimationForceSend(err, () => {
// callback is called whenever no error
tx.gas = !gasEstimation ? gasLimit : gasEstimation
@ -197,7 +199,7 @@ class TxRunner {
})
})
}, () => {
var blockGasLimit = executionContext.currentblockGasLimit()
var blockGasLimit = self.executionContext.currentblockGasLimit()
// NOTE: estimateGas very likely will return a large limit if execution of the code failed
// we want to be able to run the code in order to debug and find the cause for the failure
if (err) return callback(err)
@ -218,7 +220,7 @@ class TxRunner {
async function tryTillReceiptAvailable (txhash, done) {
return new Promise((resolve, reject) => {
executionContext.web3().eth.getTransactionReceipt(txhash, async (err, receipt) => {
this.executionContext.web3().eth.getTransactionReceipt(txhash, async (err, receipt) => {
if (err || !receipt) {
// Try again with a bit of delay if error or if result still null
await pause()
@ -232,7 +234,7 @@ async function tryTillReceiptAvailable (txhash, done) {
async function tryTillTxAvailable (txhash, done) {
return new Promise((resolve, reject) => {
executionContext.web3().eth.getTransaction(txhash, async (err, tx) => {
this.executionContext.web3().eth.getTransaction(txhash, async (err, tx) => {
if (err || !tx) {
// Try again with a bit of delay if error or if result still null
await pause()

@ -6,27 +6,29 @@ const { EventEmitter } = require('events')
const TxRunner = require('./execution/txRunner')
const txHelper = require('./execution/txHelper')
const EventManager = require('./eventManager')
const executionContext = require('./execution/execution-context')
const defaultExecutionContext = require('./execution/execution-context')
const { resultToRemixTx } = require('./helpers/txResultHelper')
module.exports = class UniversalDApp {
constructor (config) {
constructor (config, executionContext) {
this.events = new EventEmitter()
this.event = new EventManager()
// has a default for now for backwards compatability
this.executionContext = executionContext || defaultExecutionContext
this.config = config
this.txRunner = new TxRunner({}, {
config: config,
detectNetwork: (cb) => {
executionContext.detectNetwork(cb)
this.executionContext.detectNetwork(cb)
},
personalMode: () => {
return executionContext.getProvider() === 'web3' ? this.config.get('settings/personal-mode') : false
return this.executionContext.getProvider() === 'web3' ? this.config.get('settings/personal-mode') : false
}
})
}, this.executionContext)
this.accounts = {}
executionContext.event.register('contextChanged', this.resetEnvironment.bind(this))
this.executionContext.event.register('contextChanged', this.resetEnvironment.bind(this))
}
// TODO : event should be triggered by Udapp instead of TxListener
@ -39,7 +41,7 @@ module.exports = class UniversalDApp {
resetEnvironment () {
this.accounts = {}
if (executionContext.isVM()) {
if (this.executionContext.isVM()) {
this._addAccount('3cd7232cd6f3fc66a57a6bedc1a8ed6c228fff0a327e169c2bcc5e869ed49511', '0x56BC75E2D63100000')
this._addAccount('2ac6c190b09897cd8987869cc7b918cfea07ee82038d492abce033c75c1b1d0c', '0x56BC75E2D63100000')
this._addAccount('dae9801649ba2d95a21e688b56f77905e5667c44ce868ec83f82e838712a2c7a', '0x56BC75E2D63100000')
@ -52,14 +54,14 @@ module.exports = class UniversalDApp {
config: this.config,
// TODO: to refactor, TxRunner already has access to executionContext
detectNetwork: (cb) => {
executionContext.detectNetwork(cb)
this.executionContext.detectNetwork(cb)
},
personalMode: () => {
return executionContext.getProvider() === 'web3' ? this.config.get('settings/personal-mode') : false
return this.executionContext.getProvider() === 'web3' ? this.config.get('settings/personal-mode') : false
}
})
}, this.executionContext)
this.txRunner.event.register('transactionBroadcasted', (txhash) => {
executionContext.detectNetwork((error, network) => {
this.executionContext.detectNetwork((error, network) => {
if (error || !network) return
this.event.trigger('transactionBroadcasted', [txhash, network.name])
})
@ -76,7 +78,7 @@ module.exports = class UniversalDApp {
*/
createVMAccount (newAccount) {
const { privateKey, balance } = newAccount
if (executionContext.getProvider() !== 'vm') {
if (this.executionContext.getProvider() !== 'vm') {
throw new Error('plugin API does not allow creating a new account through web3 connection. Only vm mode is allowed')
}
this._addAccount(privateKey, balance)
@ -85,12 +87,12 @@ module.exports = class UniversalDApp {
}
newAccount (password, passwordPromptCb, cb) {
if (!executionContext.isVM()) {
if (!this.executionContext.isVM()) {
if (!this.config.get('settings/personal-mode')) {
return cb('Not running in personal mode')
}
passwordPromptCb((passphrase) => {
executionContext.web3().personal.newAccount(passphrase, cb)
this.executionContext.web3().personal.newAccount(passphrase, cb)
})
} else {
let privateKey
@ -104,7 +106,7 @@ module.exports = class UniversalDApp {
/** Add an account to the list of account (only for Javascript VM) */
_addAccount (privateKey, balance) {
if (!executionContext.isVM()) {
if (!this.executionContext.isVM()) {
throw new Error('_addAccount() cannot be called in non-VM mode')
}
@ -113,7 +115,7 @@ module.exports = class UniversalDApp {
const address = privateToAddress(privateKey)
// FIXME: we don't care about the callback, but we should still make this proper
let stateManager = executionContext.vm().stateManager
let stateManager = this.executionContext.vm().stateManager
stateManager.getAccount(address, (error, account) => {
if (error) return console.log(error)
account.balance = balance || '0xf00000000000000001'
@ -129,7 +131,7 @@ module.exports = class UniversalDApp {
/** Return the list of accounts */
getAccounts (cb) {
return new Promise((resolve, reject) => {
const provider = executionContext.getProvider()
const provider = this.executionContext.getProvider()
switch (provider) {
case 'vm': {
if (!this.accounts) {
@ -143,13 +145,13 @@ module.exports = class UniversalDApp {
break
case 'web3': {
if (this.config.get('settings/personal-mode')) {
return executionContext.web3().personal.getListAccounts((error, accounts) => {
return this.executionContext.web3().personal.getListAccounts((error, accounts) => {
if (cb) cb(error, accounts)
if (error) return reject(error)
resolve(accounts)
})
} else {
executionContext.web3().eth.getAccounts((error, accounts) => {
this.executionContext.web3().eth.getAccounts((error, accounts) => {
if (cb) cb(error, accounts)
if (error) return reject(error)
resolve(accounts)
@ -158,7 +160,7 @@ module.exports = class UniversalDApp {
}
break
case 'injected': {
executionContext.web3().eth.getAccounts((error, accounts) => {
this.executionContext.web3().eth.getAccounts((error, accounts) => {
if (cb) cb(error, accounts)
if (error) return reject(error)
resolve(accounts)
@ -172,8 +174,8 @@ module.exports = class UniversalDApp {
getBalance (address, cb) {
address = stripHexPrefix(address)
if (!executionContext.isVM()) {
executionContext.web3().eth.getBalance(address, (err, res) => {
if (!this.executionContext.isVM()) {
this.executionContext.web3().eth.getBalance(address, (err, res) => {
if (err) {
cb(err)
} else {
@ -185,7 +187,7 @@ module.exports = class UniversalDApp {
return cb('No accounts?')
}
executionContext.vm().stateManager.getAccount(Buffer.from(address, 'hex'), (err, res) => {
this.executionContext.vm().stateManager.getAccount(Buffer.from(address, 'hex'), (err, res) => {
if (err) {
cb('Account not found')
} else {
@ -201,7 +203,7 @@ module.exports = class UniversalDApp {
if (error) {
callback(error)
} else {
callback(null, executionContext.web3().utils.fromWei(balance, 'ether'))
callback(null, this.executionContext.web3().fromWei(balance, 'ether'))
}
})
}
@ -240,7 +242,7 @@ module.exports = class UniversalDApp {
}
context () {
return (executionContext.isVM() ? 'memory' : 'blockchain')
return (this.executionContext.isVM() ? 'memory' : 'blockchain')
}
getABI (contract) {
@ -266,7 +268,7 @@ module.exports = class UniversalDApp {
*/
sendTransaction (tx) {
return new Promise((resolve, reject) => {
executionContext.detectNetwork((error, network) => {
this.executionContext.detectNetwork((error, network) => {
if (error) return reject(error)
if (network.name === 'Main' && network.id === '1') {
return reject(new Error('It is not allowed to make this action against mainnet'))
@ -334,7 +336,7 @@ module.exports = class UniversalDApp {
if (err) return next(err)
if (!address) return next('No accounts available')
if (executionContext.isVM() && !self.accounts[address]) {
if (self.executionContext.isVM() && !self.accounts[address]) {
return next('Invalid account selected')
}
next(null, address, value, gasLimit)

@ -65,7 +65,7 @@ function processTx (executionContext, accounts, payload, isCall, callback) {
// let txRunner = new TxRunner(accounts, api)
if (!txRunnerInstance) {
txRunnerInstance = new TxRunner(accounts, api)
txRunnerInstance = new TxRunner(accounts, api, executionContext)
}
txRunnerInstance.vmaccounts = accounts
let { from, to, data, value, gas } = payload.params[0]

Loading…
Cancel
Save