From a05767013c7fade9c62cbb2657abe597cb251034 Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Fri, 13 Dec 2019 19:27:50 -0500 Subject: [PATCH 1/4] remove dependency on global executionContext --- remix-simulator/src/genesis.js | 4 +-- remix-simulator/src/methods/accounts.js | 11 ++++---- remix-simulator/src/methods/blocks.js | 17 ++++++------ remix-simulator/src/methods/filters.js | 24 ++++++++--------- remix-simulator/src/methods/transactions.js | 30 ++++++++++----------- remix-simulator/src/methods/txProcess.js | 3 +-- remix-simulator/src/provider.js | 12 +++++---- 7 files changed, 48 insertions(+), 53 deletions(-) diff --git a/remix-simulator/src/genesis.js b/remix-simulator/src/genesis.js index 7f602e1f36..f008ead194 100644 --- a/remix-simulator/src/genesis.js +++ b/remix-simulator/src/genesis.js @@ -1,10 +1,8 @@ var EthJSBlock = require('ethereumjs-block') -var RemixLib = require('remix-lib') -var executionContext = RemixLib.execution.executionContext var ethJSUtil = require('ethereumjs-util') var BN = ethJSUtil.BN -function generateBlock () { +function generateBlock (executionContext) { var block = new EthJSBlock({ header: { timestamp: (new Date().getTime() / 1000 | 0), diff --git a/remix-simulator/src/methods/accounts.js b/remix-simulator/src/methods/accounts.js index d1cd7d1c15..c7c369ff80 100644 --- a/remix-simulator/src/methods/accounts.js +++ b/remix-simulator/src/methods/accounts.js @@ -1,17 +1,16 @@ -var RemixLib = require('remix-lib') -var executionContext = RemixLib.execution.executionContext var ethJSUtil = require('ethereumjs-util') var BN = ethJSUtil.BN var Web3 = require('web3') -var Accounts = function () { +var Accounts = function (executionContext) { this.web3 = new Web3() + this.executionContext = executionContext // TODO: make it random and/or use remix-libs this.accountsList = [this.web3.eth.accounts.create(['abcd']), this.web3.eth.accounts.create(['ef12']), this.web3.eth.accounts.create(['ef34'])] this.accounts = {} this.accountsKeys = {} - executionContext.init({get: () => { return true }}) + this.executionContext.init({get: () => { return true }}) } Accounts.prototype.init = async function () { @@ -20,7 +19,7 @@ Accounts.prototype.init = async function () { this.accountsKeys[ethJSUtil.toChecksumAddress(account.address)] = account.privateKey this.accounts[ethJSUtil.toChecksumAddress(account.address)] = { privateKey: Buffer.from(account.privateKey.replace('0x', ''), 'hex'), nonce: 0 } - executionContext.vm().stateManager.getAccount(Buffer.from(account.address.replace('0x', ''), 'hex'), (err, account) => { + this.executionContext.vm().stateManager.getAccount(Buffer.from(account.address.replace('0x', ''), 'hex'), (err, account) => { if (err) { throw new Error(err) } @@ -52,7 +51,7 @@ Accounts.prototype.eth_getBalance = function (payload, cb) { let address = payload.params[0] address = ethJSUtil.stripHexPrefix(address) - executionContext.vm().stateManager.getAccount(Buffer.from(address, 'hex'), (err, account) => { + this.executionContext.vm().stateManager.getAccount(Buffer.from(address, 'hex'), (err, account) => { if (err) { return cb(err) } diff --git a/remix-simulator/src/methods/blocks.js b/remix-simulator/src/methods/blocks.js index cc2b15f28a..b8fe4e8f07 100644 --- a/remix-simulator/src/methods/blocks.js +++ b/remix-simulator/src/methods/blocks.js @@ -1,7 +1,6 @@ -var RemixLib = require('remix-lib') -var executionContext = RemixLib.execution.executionContext -var Blocks = function (_options) { +var Blocks = function (executionContext, _options) { + this.executionContext = executionContext const options = _options || {} this.coinbase = options.coinbase || '0x0000000000000000000000000000000000000000' this.blockNumber = 0 @@ -25,10 +24,10 @@ Blocks.prototype.methods = function () { Blocks.prototype.eth_getBlockByNumber = function (payload, cb) { let blockIndex = payload.params[0] if (blockIndex === 'latest') { - blockIndex = executionContext.latestBlockNumber + blockIndex = this.executionContext.latestBlockNumber } - const block = executionContext.blocks[blockIndex] + const block = this.executionContext.blocks[blockIndex] if (!block) { return cb(new Error('block not found')) @@ -65,7 +64,7 @@ function toHex (value) { } Blocks.prototype.eth_getBlockByHash = function (payload, cb) { - var block = executionContext.blocks[payload.params[0]] + var block = this.executionContext.blocks[payload.params[0]] let b = { 'number': toHex(block.header.number), @@ -104,13 +103,13 @@ Blocks.prototype.eth_blockNumber = function (payload, cb) { } Blocks.prototype.eth_getBlockTransactionCountByHash = function (payload, cb) { - var block = executionContext.blocks[payload.params[0]] + var block = this.executionContext.blocks[payload.params[0]] cb(null, block.transactions.length) } Blocks.prototype.eth_getBlockTransactionCountByNumber = function (payload, cb) { - var block = executionContext.blocks[payload.params[0]] + var block = this.executionContext.blocks[payload.params[0]] cb(null, block.transactions.length) } @@ -126,7 +125,7 @@ Blocks.prototype.eth_getUncleCountByBlockNumber = function (payload, cb) { Blocks.prototype.eth_getStorageAt = function (payload, cb) { const [address, position, blockNumber] = payload.params - executionContext.web3().debug.storageRangeAt(blockNumber, 'latest', address.toLowerCase(), position, 1, (err, result) => { + this.executionContext.web3().debug.storageRangeAt(blockNumber, 'latest', address.toLowerCase(), position, 1, (err, result) => { if (err || (result.storage && Object.values(result.storage).length === 0)) { return cb(err, '') } diff --git a/remix-simulator/src/methods/filters.js b/remix-simulator/src/methods/filters.js index ecd5731ce1..7453d615bd 100644 --- a/remix-simulator/src/methods/filters.js +++ b/remix-simulator/src/methods/filters.js @@ -1,8 +1,6 @@ -var RemixLib = require('remix-lib') -var executionContext = RemixLib.execution.executionContext -var Filters = function (_options) { - // const options = _options || {} +var Filters = function (executionContext) { + this.executionContext = executionContext } Filters.prototype.methods = function () { @@ -14,49 +12,49 @@ Filters.prototype.methods = function () { } Filters.prototype.eth_getLogs = function (payload, cb) { - let results = executionContext.logsManager.getLogsFor(payload.params[0]) + let results = this.executionContext.logsManager.getLogsFor(payload.params[0]) cb(null, results) } Filters.prototype.eth_subscribe = function (payload, cb) { - let subscriptionId = executionContext.logsManager.subscribe(payload.params) + let subscriptionId = this.executionContext.logsManager.subscribe(payload.params) cb(null, subscriptionId) } Filters.prototype.eth_unsubscribe = function (payload, cb) { - executionContext.logsManager.unsubscribe(payload.params[0]) + this.executionContext.logsManager.unsubscribe(payload.params[0]) cb(null, true) } Filters.prototype.eth_newFilter = function (payload, cb) { - const filterId = executionContext.logsManager.newFilter('filter', payload.params[0]) + const filterId = this.executionContext.logsManager.newFilter('filter', payload.params[0]) cb(null, filterId) } Filters.prototype.eth_newBlockFilter = function (payload, cb) { - const filterId = executionContext.logsManager.newFilter('block') + const filterId = this.executionContext.logsManager.newFilter('block') cb(null, filterId) } Filters.prototype.eth_newPendingTransactionFilter = function (payload, cb) { - const filterId = executionContext.logsManager.newFilter('pendingTransactions') + const filterId = this.executionContext.logsManager.newFilter('pendingTransactions') cb(null, filterId) } Filters.prototype.eth_uninstallfilter = function (payload, cb) { - const result = executionContext.logsManager.uninstallFilter(payload.params[0]) + const result = this.executionContext.logsManager.uninstallFilter(payload.params[0]) cb(null, result) } Filters.prototype.eth_getFilterChanges = function (payload, cb) { const filterId = payload.params[0] - let results = executionContext.logsManager.getLogsForFilter(filterId) + let results = this.executionContext.logsManager.getLogsForFilter(filterId) cb(null, results) } Filters.prototype.eth_getFilterLogs = function (payload, cb) { const filterId = payload.params[0] - let results = executionContext.logsManager.getLogsForFilter(filterId, true) + let results = this.executionContext.logsManager.getLogsForFilter(filterId, true) cb(null, results) } diff --git a/remix-simulator/src/methods/transactions.js b/remix-simulator/src/methods/transactions.js index d8e038cc61..94a211513a 100644 --- a/remix-simulator/src/methods/transactions.js +++ b/remix-simulator/src/methods/transactions.js @@ -1,11 +1,11 @@ var Web3 = require('web3') -var RemixLib = require('remix-lib') -var executionContext = RemixLib.execution.executionContext var ethJSUtil = require('ethereumjs-util') var processTx = require('./txProcess.js') var BN = ethJSUtil.BN -var Transactions = function () {} +var Transactions = function (executionContext) { + this.executionContext = executionContext +} Transactions.prototype.init = function (accounts) { this.accounts = accounts @@ -30,16 +30,16 @@ Transactions.prototype.eth_sendTransaction = function (payload, cb) { if (payload.params && payload.params.length > 0 && payload.params[0].from) { payload.params[0].from = ethJSUtil.toChecksumAddress(payload.params[0].from) } - processTx(this.accounts, payload, false, cb) + processTx(this.executionContext, this.accounts, payload, false, cb) } Transactions.prototype.eth_getTransactionReceipt = function (payload, cb) { - executionContext.web3().eth.getTransactionReceipt(payload.params[0], (error, receipt) => { + this.executionContext.web3().eth.getTransactionReceipt(payload.params[0], (error, receipt) => { if (error) { return cb(error) } - var txBlock = executionContext.txs[receipt.hash] + var txBlock = this.executionContext.txs[receipt.hash] var r = { 'transactionHash': receipt.hash, @@ -68,7 +68,7 @@ Transactions.prototype.eth_estimateGas = function (payload, cb) { Transactions.prototype.eth_getCode = function (payload, cb) { let address = payload.params[0] - executionContext.web3().eth.getCode(address, (error, result) => { + this.executionContext.web3().eth.getCode(address, (error, result) => { if (error) { console.dir('error getting code') console.dir(error) @@ -88,13 +88,13 @@ Transactions.prototype.eth_call = function (payload, cb) { payload.params[0].value = undefined - processTx(this.accounts, payload, true, cb) + processTx(this.executionContext, this.accounts, payload, true, cb) } Transactions.prototype.eth_getTransactionCount = function (payload, cb) { let address = payload.params[0] - executionContext.vm().stateManager.getAccount(address, (err, account) => { + this.executionContext.vm().stateManager.getAccount(address, (err, account) => { if (err) { return cb(err) } @@ -106,12 +106,12 @@ Transactions.prototype.eth_getTransactionCount = function (payload, cb) { Transactions.prototype.eth_getTransactionByHash = function (payload, cb) { const address = payload.params[0] - executionContext.web3().eth.getTransactionReceipt(address, (error, receipt) => { + this.executionContext.web3().eth.getTransactionReceipt(address, (error, receipt) => { if (error) { return cb(error) } - var txBlock = executionContext.txs[receipt.transactionHash] + var txBlock = this.executionContext.txs[receipt.transactionHash] // TODO: params to add later let r = { @@ -151,10 +151,10 @@ Transactions.prototype.eth_getTransactionByHash = function (payload, cb) { Transactions.prototype.eth_getTransactionByBlockHashAndIndex = function (payload, cb) { const txIndex = payload.params[1] - var txBlock = executionContext.blocks[payload.params[0]] + var txBlock = this.executionContext.blocks[payload.params[0]] const txHash = '0x' + txBlock.transactions[Web3.utils.toDecimal(txIndex)].hash().toString('hex') - executionContext.web3().eth.getTransactionReceipt(txHash, (error, receipt) => { + this.executionContext.web3().eth.getTransactionReceipt(txHash, (error, receipt) => { if (error) { return cb(error) } @@ -193,10 +193,10 @@ Transactions.prototype.eth_getTransactionByBlockHashAndIndex = function (payload Transactions.prototype.eth_getTransactionByBlockNumberAndIndex = function (payload, cb) { const txIndex = payload.params[1] - var txBlock = executionContext.blocks[payload.params[0]] + var txBlock = this.executionContext.blocks[payload.params[0]] const txHash = '0x' + txBlock.transactions[Web3.utils.toDecimal(txIndex)].hash().toString('hex') - executionContext.web3().eth.getTransactionReceipt(txHash, (error, receipt) => { + this.executionContext.web3().eth.getTransactionReceipt(txHash, (error, receipt) => { if (error) { return cb(error) } diff --git a/remix-simulator/src/methods/txProcess.js b/remix-simulator/src/methods/txProcess.js index 3fb6ee890b..816eaf144c 100644 --- a/remix-simulator/src/methods/txProcess.js +++ b/remix-simulator/src/methods/txProcess.js @@ -1,7 +1,6 @@ var RemixLib = require('remix-lib') var TxExecution = RemixLib.execution.txExecution var TxRunner = RemixLib.execution.txRunner -var executionContext = RemixLib.execution.executionContext function runCall (payload, from, to, data, value, gasLimit, txRunner, callbacks, callback) { let finalCallback = function (err, result) { @@ -40,7 +39,7 @@ function createContract (payload, from, data, value, gasLimit, txRunner, callbac let txRunnerInstance -function processTx (accounts, payload, isCall, callback) { +function processTx (executionContext, accounts, payload, isCall, callback) { let api = { logMessage: (msg) => { }, diff --git a/remix-simulator/src/provider.js b/remix-simulator/src/provider.js index 65bf01b618..64433626a2 100644 --- a/remix-simulator/src/provider.js +++ b/remix-simulator/src/provider.js @@ -15,18 +15,20 @@ const generateBlock = require('./genesis.js') var Provider = function (options) { this.options = options || {} + // TODO: init executionContext here + this.executionContext = executionContext this.Accounts = new Accounts() - this.Transactions = new Transactions() + this.Transactions = new Transactions(this.executionContext) this.methods = {} this.methods = merge(this.methods, this.Accounts.methods()) - this.methods = merge(this.methods, (new Blocks(options)).methods()) + this.methods = merge(this.methods, (new Blocks(this.executionContext, options)).methods()) this.methods = merge(this.methods, (new Misc()).methods()) - this.methods = merge(this.methods, (new Filters()).methods()) + this.methods = merge(this.methods, (new Filters(this.executionContext)).methods()) this.methods = merge(this.methods, (new Net()).methods()) this.methods = merge(this.methods, this.Transactions.methods()) - generateBlock() + generateBlock(this.executionContext) this.init() } @@ -67,7 +69,7 @@ Provider.prototype.isConnected = function () { } Provider.prototype.on = function (type, cb) { - executionContext.logsManager.addListener(type, cb) + this.executionContext.logsManager.addListener(type, cb) } module.exports = Provider From ebf8063f1645274fbab7016f3aae45054d36fadb Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Sat, 14 Dec 2019 18:53:03 -0500 Subject: [PATCH 2/4] pass executionContext as a param --- remix-debug/src/cmdline/contextManager.js | 3 +- remix-debug/src/cmdline/index.js | 5 +- remix-lib/src/execution/txListener.js | 29 ++++++------ remix-lib/src/execution/txRunner.js | 38 +++++++-------- remix-lib/src/universalDapp.js | 56 ++++++++++++----------- remix-simulator/src/methods/txProcess.js | 2 +- 6 files changed, 71 insertions(+), 62 deletions(-) diff --git a/remix-debug/src/cmdline/contextManager.js b/remix-debug/src/cmdline/contextManager.js index cb7c475eaa..a5cb6ace88 100644 --- a/remix-debug/src/cmdline/contextManager.js +++ b/remix-debug/src/cmdline/contextManager.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() diff --git a/remix-debug/src/cmdline/index.js b/remix-debug/src/cmdline/index.js index f5734f084c..a9f6a7f730 100644 --- a/remix-debug/src/cmdline/index.js +++ b/remix-debug/src/cmdline/index.js @@ -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(), diff --git a/remix-lib/src/execution/txListener.js b/remix-lib/src/execution/txListener.js index 491f736c94..12a2b42d4a 100644 --- a/remix-lib/src/execution/txListener.js +++ b/remix-lib/src/execution/txListener.js @@ -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) diff --git a/remix-lib/src/execution/txRunner.js b/remix-lib/src/execution/txRunner.js index cdc0dae515..bc02c30367 100644 --- a/remix-lib/src/execution/txRunner.js +++ b/remix-lib/src/execution/txRunner.js @@ -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() diff --git a/remix-lib/src/universalDapp.js b/remix-lib/src/universalDapp.js index 3a76f06cf2..d6cf4eb995 100644 --- a/remix-lib/src/universalDapp.js +++ b/remix-lib/src/universalDapp.js @@ -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) diff --git a/remix-simulator/src/methods/txProcess.js b/remix-simulator/src/methods/txProcess.js index 816eaf144c..1126850643 100644 --- a/remix-simulator/src/methods/txProcess.js +++ b/remix-simulator/src/methods/txProcess.js @@ -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] From 5550a80a57d319711b2c83b3c74653c15e7dc4c2 Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Sun, 15 Dec 2019 17:37:34 -0500 Subject: [PATCH 3/4] fix missing context in accounts methods --- remix-lib/src/execution/txRunner.js | 2 +- remix-simulator/src/provider.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/remix-lib/src/execution/txRunner.js b/remix-lib/src/execution/txRunner.js index bc02c30367..ee933e1af2 100644 --- a/remix-lib/src/execution/txRunner.js +++ b/remix-lib/src/execution/txRunner.js @@ -147,7 +147,7 @@ class TxRunner { } runBlockInVm (tx, block, callback) { - this.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((results) => { let result = results.results[0] if (result) { const status = result.execResult.exceptionError ? 0 : 1 diff --git a/remix-simulator/src/provider.js b/remix-simulator/src/provider.js index 64433626a2..c7929e297d 100644 --- a/remix-simulator/src/provider.js +++ b/remix-simulator/src/provider.js @@ -17,7 +17,7 @@ var Provider = function (options) { this.options = options || {} // TODO: init executionContext here this.executionContext = executionContext - this.Accounts = new Accounts() + this.Accounts = new Accounts(this.executionContext) this.Transactions = new Transactions(this.executionContext) this.methods = {} From 8b3091709989a8c70aeaac829ee42d75d178e4c8 Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Wed, 18 Dec 2019 16:56:31 -0500 Subject: [PATCH 4/4] fix package lock --- package-lock.json | 43 +++++++++++++++++++++++++++++++------------ 1 file changed, 31 insertions(+), 12 deletions(-) diff --git a/package-lock.json b/package-lock.json index 83549b2260..036a188ec5 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,5 +1,5 @@ { - "version": "0.1.21", + "version": "0.1.24", "lockfileVersion": 1, "requires": true, "dependencies": { @@ -1721,7 +1721,8 @@ "ansi-regex": { "version": "2.1.1", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "aproba": { "version": "1.2.0", @@ -1742,12 +1743,14 @@ "balanced-match": { "version": "1.0.0", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "brace-expansion": { "version": "1.1.11", "bundled": true, "dev": true, + "optional": true, "requires": { "balanced-match": "^1.0.0", "concat-map": "0.0.1" @@ -1762,17 +1765,20 @@ "code-point-at": { "version": "1.1.0", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "concat-map": { "version": "0.0.1", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "console-control-strings": { "version": "1.1.0", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "core-util-is": { "version": "1.0.2", @@ -1889,7 +1895,8 @@ "inherits": { "version": "2.0.3", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "ini": { "version": "1.3.5", @@ -1901,6 +1908,7 @@ "version": "1.0.0", "bundled": true, "dev": true, + "optional": true, "requires": { "number-is-nan": "^1.0.0" } @@ -1915,6 +1923,7 @@ "version": "3.0.4", "bundled": true, "dev": true, + "optional": true, "requires": { "brace-expansion": "^1.1.7" } @@ -1922,12 +1931,14 @@ "minimist": { "version": "0.0.8", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "minipass": { "version": "2.3.5", "bundled": true, "dev": true, + "optional": true, "requires": { "safe-buffer": "^5.1.2", "yallist": "^3.0.0" @@ -1946,6 +1957,7 @@ "version": "0.5.1", "bundled": true, "dev": true, + "optional": true, "requires": { "minimist": "0.0.8" } @@ -2026,7 +2038,8 @@ "number-is-nan": { "version": "1.0.1", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "object-assign": { "version": "4.1.1", @@ -2038,6 +2051,7 @@ "version": "1.4.0", "bundled": true, "dev": true, + "optional": true, "requires": { "wrappy": "1" } @@ -2123,7 +2137,8 @@ "safe-buffer": { "version": "5.1.2", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "safer-buffer": { "version": "2.1.2", @@ -2159,6 +2174,7 @@ "version": "1.0.2", "bundled": true, "dev": true, + "optional": true, "requires": { "code-point-at": "^1.0.0", "is-fullwidth-code-point": "^1.0.0", @@ -2178,6 +2194,7 @@ "version": "3.0.1", "bundled": true, "dev": true, + "optional": true, "requires": { "ansi-regex": "^2.0.0" } @@ -2221,12 +2238,14 @@ "wrappy": { "version": "1.0.2", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "yallist": { "version": "3.0.3", "bundled": true, - "dev": true + "dev": true, + "optional": true } } },