From a4642d5856d37fb761fc32cad74e4b90b927ac06 Mon Sep 17 00:00:00 2001 From: yann300 Date: Mon, 24 Sep 2018 14:54:58 +0200 Subject: [PATCH 1/2] Update txRunner.js --- remix-lib/src/execution/txRunner.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/remix-lib/src/execution/txRunner.js b/remix-lib/src/execution/txRunner.js index 08f037a7d..f13ec0a67 100644 --- a/remix-lib/src/execution/txRunner.js +++ b/remix-lib/src/execution/txRunner.js @@ -128,7 +128,7 @@ class TxRunner { executionContext.vm().stateManager.checkpoint(() => {}) } - executionContext.vm().runTx({block: block, tx: tx, skipBalance: true, skipNonce: true}, function (err, result) { + executionContext.vm().runTx({block: block, tx: tx, skipBalance: false, skipNonce: false}, function (err, result) { if (useCall) { executionContext.vm().stateManager.revert(function () {}) } From a8641220b1f9082dbc97464689e568e02cf5e59b Mon Sep 17 00:00:00 2001 From: yann300 Date: Mon, 25 Mar 2019 10:55:56 +0100 Subject: [PATCH 2/2] remix-simulator put default balance --- remix-simulator/src/methods/accounts.js | 14 ++++++----- remix-simulator/src/provider.js | 32 ++++++++++++++++++++++++- remix-tests/src/run.js | 6 +++-- remix-tests/src/runTestSources.js | 10 ++++---- remix-tests/tests/testRunner.js | 6 +++-- 5 files changed, 53 insertions(+), 15 deletions(-) diff --git a/remix-simulator/src/methods/accounts.js b/remix-simulator/src/methods/accounts.js index d23f57222..e7e9e1c66 100644 --- a/remix-simulator/src/methods/accounts.js +++ b/remix-simulator/src/methods/accounts.js @@ -5,13 +5,15 @@ var Accounts = function () { // TODO: make it random and/or use remix-libs this.accounts = [this.web3.eth.accounts.create(['abcd']), this.web3.eth.accounts.create(['ef12']), this.web3.eth.accounts.create(['ef34'])] - this.accounts[this.accounts[0].address.toLowerCase()] = this.accounts[0] - this.accounts[this.accounts[1].address.toLowerCase()] = this.accounts[1] - this.accounts[this.accounts[2].address.toLowerCase()] = this.accounts[2] + let setAccounts = (i) => { + const account = this.accounts[i] + this.accounts[account.address.toLowerCase()] = account + this.accounts[account.address.toLowerCase()].privateKey = Buffer.from(this.accounts[account.address.toLowerCase()].privateKey.slice(2), 'hex') + } - this.accounts[this.accounts[0].address.toLowerCase()].privateKey = Buffer.from(this.accounts[this.accounts[0].address.toLowerCase()].privateKey.slice(2), 'hex') - this.accounts[this.accounts[1].address.toLowerCase()].privateKey = Buffer.from(this.accounts[this.accounts[1].address.toLowerCase()].privateKey.slice(2), 'hex') - this.accounts[this.accounts[2].address.toLowerCase()].privateKey = Buffer.from(this.accounts[this.accounts[2].address.toLowerCase()].privateKey.slice(2), 'hex') + setAccounts(0) + setAccounts(1) + setAccounts(2) } Accounts.prototype.methods = function () { diff --git a/remix-simulator/src/provider.js b/remix-simulator/src/provider.js index 1f4e5d9b8..d37028515 100644 --- a/remix-simulator/src/provider.js +++ b/remix-simulator/src/provider.js @@ -1,6 +1,8 @@ const log = require('./utils/logs.js') const merge = require('merge') +var remixLib = require('remix-lib') +var executionContext = remixLib.execution.executionContext const Accounts = require('./methods/accounts.js') const Blocks = require('./methods/blocks.js') const Misc = require('./methods/misc.js') @@ -9,8 +11,9 @@ const Transactions = require('./methods/transactions.js') const Whisper = require('./methods/whisper.js') var Provider = function () { - this.Accounts = new Accounts() + executionContext.setContext('vm', null, () => {}, (msg) => { console.log(msg) }) + this.Accounts = new Accounts() this.methods = {} this.methods = merge(this.methods, this.Accounts.methods()) this.methods = merge(this.methods, (new Blocks()).methods()) @@ -25,6 +28,7 @@ Provider.prototype.sendAsync = function (payload, callback) { let method = this.methods[payload.method] if (method) { + console.log(payload) return method.call(method, payload, (err, result) => { if (err) { return callback(err) @@ -44,4 +48,30 @@ Provider.prototype.isConnected = function () { return true } +Provider.prototype.init = async function () { + const accounts = this.Accounts.accounts + let setAccounts = (i) => { + return new Promise((resolve, reject) => { + const account = accounts[i] + + let stateManager = executionContext.vm().stateManager + const address = account.address + stateManager.getAccount(account.address, (error, account) => { + if (error) return reject(error) + account.balance = '0xf00000000000000001' + console.log('available address : ', address) + stateManager.putAccount(address, account, function cb (error, result) { + console.log('setBalance', address) + if (error) return reject(error) + console.log(result) + resolve() + }) + }) + }) + } + await setAccounts(0) + await setAccounts(1) + await setAccounts(2) +} + module.exports = Provider diff --git a/remix-tests/src/run.js b/remix-tests/src/run.js index e0aefb97f..e8b30d06e 100644 --- a/remix-tests/src/run.js +++ b/remix-tests/src/run.js @@ -35,7 +35,7 @@ commander.command('help').description('output usage information').action(functio // get current version commander .option('-v, --verbose ', 'run with verbosity', mapVerbosity) - .action(function (filename) { + .action(async function (filename) { // Console message console.log(('\n\tšŸ‘ :: Running remix-tests - Unit testing for solidity :: šŸ‘\t\n').white) // set logger verbosity @@ -45,7 +45,9 @@ commander } let web3 = new Web3() // web3.setProvider(new web3.providers.HttpProvider('http://localhost:8545')) - web3.setProvider(new Provider()) + const provider = new Provider() + await provider.init() + web3.setProvider(provider) // web3.setProvider(new web3.providers.WebsocketProvider('ws://localhost:8546')) if (!fs.existsSync(filename)) { diff --git a/remix-tests/src/runTestSources.js b/remix-tests/src/runTestSources.js index e91bd66a9..ed72d2c68 100644 --- a/remix-tests/src/runTestSources.js +++ b/remix-tests/src/runTestSources.js @@ -8,15 +8,17 @@ let TestRunner = require('./testRunner.js') const Web3 = require('web3') const Provider = require('remix-simulator').Provider -var createWeb3Provider = function () { +var createWeb3Provider = async function () { let web3 = new Web3() - web3.setProvider(new Provider()) + const provider = new Provider() + await provider.init() + web3.setProvider(provider) return web3 } -const runTestSources = function (contractSources, testCallback, resultCallback, finalCallback, importFileCb, opts) { +const runTestSources = async function (contractSources, testCallback, resultCallback, finalCallback, importFileCb, opts) { opts = opts || {} - let web3 = opts.web3 || createWeb3Provider() + let web3 = opts.web3 || await createWeb3Provider() let accounts = opts.accounts || null async.waterfall([ function getAccountList (next) { diff --git a/remix-tests/tests/testRunner.js b/remix-tests/tests/testRunner.js index 092052fb7..13ee05646 100644 --- a/remix-tests/tests/testRunner.js +++ b/remix-tests/tests/testRunner.js @@ -7,9 +7,11 @@ let Deployer = require('../src/deployer.js') let TestRunner = require('../src/testRunner.js') const Provider = require('remix-simulator').Provider -function compileAndDeploy (filename, callback) { +async function compileAndDeploy (filename, callback) { let web3 = new Web3() - web3.setProvider(new Provider()) + const provider = new Provider() + await provider.init() + web3.setProvider(provider) let compilationData let accounts async.waterfall([