remove dependency on global executionContext

pull/7/head
Iuri Matias 5 years ago
parent c908b28662
commit 9162f870da
  1. 4
      remix-simulator/src/genesis.js
  2. 11
      remix-simulator/src/methods/accounts.js
  3. 17
      remix-simulator/src/methods/blocks.js
  4. 24
      remix-simulator/src/methods/filters.js
  5. 30
      remix-simulator/src/methods/transactions.js
  6. 3
      remix-simulator/src/methods/txProcess.js
  7. 12
      remix-simulator/src/provider.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),

@ -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)
}

@ -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, '')
}

@ -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)
}

@ -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)
}

@ -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) => {
},

@ -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

Loading…
Cancel
Save