Commit 91ae8e36 authored by Iuri Matias's avatar Iuri Matias

move resetEnvironment accounts reset to vm provider

parent 2404852e
...@@ -11,7 +11,7 @@ const Personal = require('web3-eth-personal') ...@@ -11,7 +11,7 @@ const Personal = require('web3-eth-personal')
const Web3 = require('web3') const Web3 = require('web3')
const async = require('async') const async = require('async')
const { BN, privateToAddress, isValidPrivate, stripHexPrefix, toChecksumAddress } = require('ethereumjs-util') const { BN, privateToAddress, isValidPrivate, stripHexPrefix } = require('ethereumjs-util')
const crypto = require('crypto') const crypto = require('crypto')
const { EventEmitter } = require('events') const { EventEmitter } = require('events')
...@@ -350,15 +350,8 @@ class Blockchain { ...@@ -350,15 +350,8 @@ class Blockchain {
} }
resetEnvironment () { resetEnvironment () {
this.providers.vm.accounts = {} this.getCurrentProvider().resetEnvironment()
if (this.executionContext.isVM()) { // TODO: most params here can be refactored away in txRunner
this._addAccount('3cd7232cd6f3fc66a57a6bedc1a8ed6c228fff0a327e169c2bcc5e869ed49511', '0x56BC75E2D63100000')
this._addAccount('2ac6c190b09897cd8987869cc7b918cfea07ee82038d492abce033c75c1b1d0c', '0x56BC75E2D63100000')
this._addAccount('dae9801649ba2d95a21e688b56f77905e5667c44ce868ec83f82e838712a2c7a', '0x56BC75E2D63100000')
this._addAccount('d74aa6d18aa79a05f3473dd030a97d3305737cbc8337d940344345c1f6b72eea', '0x56BC75E2D63100000')
this._addAccount('71975fbf7fe448e004ac7ae54cad0a383c3906055a65468714156a07385e96ce', '0x56BC75E2D63100000')
}
// TODO: most params here can be refactored away in txRunner
this.txRunner = new TxRunner(this.providers.vm.accounts, { this.txRunner = new TxRunner(this.providers.vm.accounts, {
// TODO: only used to check value of doNotShowTransactionConfirmationAgain property // TODO: only used to check value of doNotShowTransactionConfirmationAgain property
config: this.config, config: this.config,
...@@ -387,7 +380,7 @@ class Blockchain { ...@@ -387,7 +380,7 @@ class Blockchain {
if (this.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') throw new Error('plugin API does not allow creating a new account through web3 connection. Only vm mode is allowed')
} }
this._addAccount(privateKey, balance) this.providers.vm._addAccount(privateKey, balance)
const privKey = Buffer.from(privateKey, 'hex') const privKey = Buffer.from(privateKey, 'hex')
return '0x' + privateToAddress(privKey).toString('hex') return '0x' + privateToAddress(privKey).toString('hex')
} }
...@@ -398,7 +391,7 @@ class Blockchain { ...@@ -398,7 +391,7 @@ class Blockchain {
do { do {
privateKey = crypto.randomBytes(32) privateKey = crypto.randomBytes(32)
} while (!isValidPrivate(privateKey)) } while (!isValidPrivate(privateKey))
this._addAccount(privateKey, '0x56BC75E2D63100000') this.providers.vm._addAccount(privateKey, '0x56BC75E2D63100000')
return cb(null, '0x' + privateToAddress(privateKey).toString('hex')) return cb(null, '0x' + privateToAddress(privateKey).toString('hex'))
} }
if (!this.config.get('settings/personal-mode')) { if (!this.config.get('settings/personal-mode')) {
...@@ -409,30 +402,6 @@ class Blockchain { ...@@ -409,30 +402,6 @@ class Blockchain {
}) })
} }
/** Add an account to the list of account (only for Javascript VM) */
_addAccount (privateKey, balance) {
if (!this.executionContext.isVM()) {
throw new Error('_addAccount() cannot be called in non-VM mode')
}
if (this.providers.vm.accounts) {
privateKey = Buffer.from(privateKey, 'hex')
const address = privateToAddress(privateKey)
// FIXME: we don't care about the callback, but we should still make this proper
let stateManager = this.executionContext.vm().stateManager
stateManager.getAccount(address, (error, account) => {
if (error) return console.log(error)
account.balance = balance || '0xf00000000000000001'
stateManager.putAccount(address, account, (error) => {
if (error) console.log(error)
})
})
this.providers.vm.accounts[toChecksumAddress('0x' + address.toString('hex'))] = { privateKey, nonce: 0 }
}
}
/** Get the balance of an address, and convert wei to ether */ /** Get the balance of an address, and convert wei to ether */
getBalanceInEther (address, cb) { getBalanceInEther (address, cb) {
address = stripHexPrefix(address) address = stripHexPrefix(address)
......
...@@ -8,6 +8,9 @@ class InjectedProvider { ...@@ -8,6 +8,9 @@ class InjectedProvider {
getAccounts (cb) { getAccounts (cb) {
return this.executionContext.web3().eth.getAccounts(cb) return this.executionContext.web3().eth.getAccounts(cb)
} }
resetEnvironment () {
}
} }
module.exports = InjectedProvider module.exports = InjectedProvider
...@@ -12,6 +12,9 @@ class NodeProvider { ...@@ -12,6 +12,9 @@ class NodeProvider {
} }
return this.executionContext.web3().eth.getAccounts(cb) return this.executionContext.web3().eth.getAccounts(cb)
} }
resetEnvironment () {
}
} }
module.exports = NodeProvider module.exports = NodeProvider
const { privateToAddress, toChecksumAddress } = require('ethereumjs-util')
class VMProvider { class VMProvider {
...@@ -13,6 +14,34 @@ class VMProvider { ...@@ -13,6 +14,34 @@ class VMProvider {
} }
return cb(null, Object.keys(this.accounts)) return cb(null, Object.keys(this.accounts))
} }
resetEnvironment () {
this.accounts = {}
this._addAccount('3cd7232cd6f3fc66a57a6bedc1a8ed6c228fff0a327e169c2bcc5e869ed49511', '0x56BC75E2D63100000')
this._addAccount('2ac6c190b09897cd8987869cc7b918cfea07ee82038d492abce033c75c1b1d0c', '0x56BC75E2D63100000')
this._addAccount('dae9801649ba2d95a21e688b56f77905e5667c44ce868ec83f82e838712a2c7a', '0x56BC75E2D63100000')
this._addAccount('d74aa6d18aa79a05f3473dd030a97d3305737cbc8337d940344345c1f6b72eea', '0x56BC75E2D63100000')
this._addAccount('71975fbf7fe448e004ac7ae54cad0a383c3906055a65468714156a07385e96ce', '0x56BC75E2D63100000')
}
/** Add an account to the list of account (only for Javascript VM) */
_addAccount (privateKey, balance) {
privateKey = Buffer.from(privateKey, 'hex')
const address = privateToAddress(privateKey)
// FIXME: we don't care about the callback, but we should still make this proper
let stateManager = this.executionContext.vm().stateManager
stateManager.getAccount(address, (error, account) => {
if (error) return console.log(error)
account.balance = balance || '0xf00000000000000001'
stateManager.putAccount(address, account, (error) => {
if (error) console.log(error)
})
})
this.accounts[toChecksumAddress('0x' + address.toString('hex'))] = { privateKey, nonce: 0 }
}
} }
module.exports = VMProvider module.exports = VMProvider
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment