mirror of
https://github.com/kremalicious/metamask-extension.git
synced 2024-12-23 09:52:26 +01:00
Fix for tests
This commit is contained in:
parent
529304c005
commit
243eeff7cb
@ -1,23 +1,23 @@
|
|||||||
const EventEmitter = require('events')
|
const EventEmitter = require('events')
|
||||||
const MetaMaskProvider = require('web3-provider-engine/zero.js')
|
const MetaMaskProvider = require('web3-provider-engine/zero.js')
|
||||||
const ObservableStore = require('obs-store')
|
const ObservableStore = require('obs-store')
|
||||||
|
const ComposedStore = require('obs-store/lib/composed')
|
||||||
const extend = require('xtend')
|
const extend = require('xtend')
|
||||||
const EthQuery = require('eth-query')
|
const EthQuery = require('eth-query')
|
||||||
const RPC_ADDRESS_LIST = require('../config.js').network
|
const RPC_ADDRESS_LIST = require('../config.js').network
|
||||||
|
const DEFAULT_RPC = RPC_ADDRESS_LIST['rinkeby']
|
||||||
|
|
||||||
module.exports = class NetworkController extends EventEmitter {
|
module.exports = class NetworkController extends EventEmitter {
|
||||||
constructor (providerOpts) {
|
constructor (config) {
|
||||||
super()
|
super()
|
||||||
this.networkStore = new ObservableStore({ network: 'loading' })
|
this.networkStore = new ObservableStore('loading')
|
||||||
providerOpts.provider.rpcTarget = this.getRpcAddressForType(providerOpts.provider.type, providerOpts.provider)
|
config.provider.rpcTarget = this.getRpcAddressForType(config.provider.type, config.provider)
|
||||||
this.providerStore = new ObservableStore(providerOpts)
|
this.providerStore = new ObservableStore(config.provider)
|
||||||
this.store = new ObservableStore(extend(this.networkStore.getState(), this.providerStore.getState()))
|
this.store = new ComposedStore({ provider: this.providerStore, network: this.networkStore })
|
||||||
|
this._providerListeners = {}
|
||||||
|
|
||||||
this._providerListners = {}
|
this.on('networkDidChange', this.lookupNetwork)
|
||||||
|
this.providerStore.subscribe((state) => this.switchNetwork({rpcUrl: state.rpcTarget}))
|
||||||
this.networkStore.subscribe((state) => this.store.updateState(state))
|
|
||||||
this.providerStore.subscribe((state) => this.store.updateState(state))
|
|
||||||
this.on('networkSwitch', this.lookupNetwork)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
get provider () {
|
get provider () {
|
||||||
@ -28,15 +28,8 @@ module.exports = class NetworkController extends EventEmitter {
|
|||||||
this._provider = provider
|
this._provider = provider
|
||||||
}
|
}
|
||||||
|
|
||||||
getState () {
|
|
||||||
return extend({},
|
|
||||||
this.networkStore.getState(),
|
|
||||||
this.providerStore.getState()
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
initializeProvider (opts) {
|
initializeProvider (opts) {
|
||||||
this.providerConfig = opts
|
this.providerInit = opts
|
||||||
this._provider = MetaMaskProvider(opts)
|
this._provider = MetaMaskProvider(opts)
|
||||||
this._proxy = new Proxy(this._provider, {
|
this._proxy = new Proxy(this._provider, {
|
||||||
get: (obj, name) => {
|
get: (obj, name) => {
|
||||||
@ -54,16 +47,18 @@ module.exports = class NetworkController extends EventEmitter {
|
|||||||
return this.provider
|
return this.provider
|
||||||
}
|
}
|
||||||
|
|
||||||
switchNetwork (providerConfig) {
|
switchNetwork (providerInit) {
|
||||||
const newConfig = extend(this.providerConfig, providerConfig)
|
this.setNetworkState('loading')
|
||||||
this.providerConfig = newConfig
|
const newInit = extend(this.providerInit, providerInit)
|
||||||
|
this.providerInit = newInit
|
||||||
|
|
||||||
this.provider = MetaMaskProvider(newConfig)
|
this._provider.removeAllListeners()
|
||||||
|
this.provider = MetaMaskProvider(newInit)
|
||||||
// apply the listners created by other controllers
|
// apply the listners created by other controllers
|
||||||
Object.keys(this._providerListners).forEach((key) => {
|
Object.keys(this._providerListeners).forEach((key) => {
|
||||||
this._providerListners[key].forEach((handler) => this._provider.addListener(key, handler))
|
this._providerListeners[key].forEach((handler) => this._provider.addListener(key, handler))
|
||||||
})
|
})
|
||||||
this.emit('networkSwitch', this.provider)
|
this.emit('networkDidChange')
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -73,20 +68,18 @@ module.exports = class NetworkController extends EventEmitter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
getNetworkState () {
|
getNetworkState () {
|
||||||
return this.networkStore.getState().network
|
return this.networkStore.getState()
|
||||||
}
|
}
|
||||||
|
|
||||||
setNetworkState (network) {
|
setNetworkState (network) {
|
||||||
return this.networkStore.updateState({ network })
|
return this.networkStore.putState(network)
|
||||||
}
|
}
|
||||||
|
|
||||||
isNetworkLoading () {
|
isNetworkLoading () {
|
||||||
return this.getNetworkState() === 'loading'
|
return this.getNetworkState() === 'loading'
|
||||||
}
|
}
|
||||||
|
|
||||||
lookupNetwork (err) {
|
lookupNetwork () {
|
||||||
if (err) this.setNetworkState('loading')
|
|
||||||
|
|
||||||
this.ethQuery.sendAsync({ method: 'net_version' }, (err, network) => {
|
this.ethQuery.sendAsync({ method: 'net_version' }, (err, network) => {
|
||||||
if (err) return this.setNetworkState('loading')
|
if (err) return this.setNetworkState('loading')
|
||||||
log.info('web3.getNetwork returned ' + network)
|
log.info('web3.getNetwork returned ' + network)
|
||||||
@ -96,37 +89,30 @@ module.exports = class NetworkController extends EventEmitter {
|
|||||||
|
|
||||||
setRpcTarget (rpcUrl) {
|
setRpcTarget (rpcUrl) {
|
||||||
this.providerStore.updateState({
|
this.providerStore.updateState({
|
||||||
provider: {
|
type: 'rpc',
|
||||||
type: 'rpc',
|
rpcTarget: rpcUrl,
|
||||||
rpcTarget: rpcUrl,
|
|
||||||
},
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
getCurrentRpcAddress () {
|
getCurrentRpcAddress () {
|
||||||
var provider = this.getProvider()
|
const provider = this.getProviderConfig()
|
||||||
if (!provider) return null
|
if (!provider) return null
|
||||||
return this.getRpcAddressForType(provider.type)
|
return this.getRpcAddressForType(provider.type)
|
||||||
}
|
}
|
||||||
|
|
||||||
setProviderType (type) {
|
setProviderType (type) {
|
||||||
if (type === this.getProvider().type) return
|
if (type === this.getProviderConfig().type) return
|
||||||
const rpcTarget = this.getRpcAddressForType(type)
|
const rpcTarget = this.getRpcAddressForType(type)
|
||||||
this.networkStore.updateState({network: 'loading'})
|
this.providerStore.updateState({type, rpcTarget})
|
||||||
this.switchNetwork({
|
|
||||||
rpcUrl: rpcTarget,
|
|
||||||
})
|
|
||||||
this.providerStore.updateState({provider: {type, rpcTarget}})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
getProvider () {
|
getProviderConfig () {
|
||||||
return this.providerStore.getState().provider
|
return this.providerStore.getState()
|
||||||
}
|
}
|
||||||
|
|
||||||
getRpcAddressForType (type, provider = this.getProvider()) {
|
getRpcAddressForType (type, provider = this.getProviderConfig()) {
|
||||||
console.log(`#getRpcAddressForType: ${type}`)
|
if (RPC_ADDRESS_LIST[type]) return RPC_ADDRESS_LIST[type]
|
||||||
if (type in RPC_ADDRESS_LIST) return RPC_ADDRESS_LIST[type]
|
return provider && provider.rpcTarget ? provider.rpcTarget : DEFAULT_RPC
|
||||||
return provider && provider.rpcTarget ? provider.rpcTarget : RPC_ADDRESS_LIST['rinkeby']
|
|
||||||
}
|
}
|
||||||
|
|
||||||
_logBlock (block) {
|
_logBlock (block) {
|
||||||
@ -135,8 +121,8 @@ module.exports = class NetworkController extends EventEmitter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
_on (event, handler) {
|
_on (event, handler) {
|
||||||
if (!this._providerListners[event]) this._providerListners[event] = []
|
if (!this._providerListeners[event]) this._providerListeners[event] = []
|
||||||
this._providerListners[event].push(handler)
|
this._providerListeners[event].push(handler)
|
||||||
this._provider.on(event, handler)
|
this._provider.on(event, handler)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -21,9 +21,7 @@ module.exports = class TransactionManager extends EventEmitter {
|
|||||||
this.blockTracker = opts.blockTracker
|
this.blockTracker = opts.blockTracker
|
||||||
this.query = opts.ethQuery
|
this.query = opts.ethQuery
|
||||||
this.txProviderUtils = new TxProviderUtil(this.query)
|
this.txProviderUtils = new TxProviderUtil(this.query)
|
||||||
this.networkStore.subscribe((_) => this.blockTracker.on('block', this.checkForTxInBlock.bind(this)))
|
|
||||||
this.blockTracker.on('block', this.checkForTxInBlock.bind(this))
|
this.blockTracker.on('block', this.checkForTxInBlock.bind(this))
|
||||||
|
|
||||||
this.signEthTx = opts.signTransaction
|
this.signEthTx = opts.signTransaction
|
||||||
this.nonceLock = Semaphore(1)
|
this.nonceLock = Semaphore(1)
|
||||||
|
|
||||||
@ -39,7 +37,7 @@ module.exports = class TransactionManager extends EventEmitter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
getNetwork () {
|
getNetwork () {
|
||||||
return this.networkStore.getState().network
|
return this.networkStore.getState()
|
||||||
}
|
}
|
||||||
|
|
||||||
getSelectedAddress () {
|
getSelectedAddress () {
|
||||||
|
@ -3,7 +3,8 @@
|
|||||||
//
|
//
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
config: {
|
config: {},
|
||||||
|
NetworkController: {
|
||||||
provider: {
|
provider: {
|
||||||
type: 'rinkeby',
|
type: 'rinkeby',
|
||||||
},
|
},
|
||||||
|
@ -107,6 +107,35 @@ ConfigManager.prototype.getSeedWords = function () {
|
|||||||
var data = this.getData()
|
var data = this.getData()
|
||||||
return data.seedWords
|
return data.seedWords
|
||||||
}
|
}
|
||||||
|
ConfigManager.prototype.setRpcTarget = function (rpcUrl) {
|
||||||
|
var config = this.getConfig()
|
||||||
|
config.provider = {
|
||||||
|
type: 'rpc',
|
||||||
|
rpcTarget: rpcUrl,
|
||||||
|
}
|
||||||
|
this.setConfig(config)
|
||||||
|
}
|
||||||
|
|
||||||
|
ConfigManager.prototype.setProviderType = function (type) {
|
||||||
|
var config = this.getConfig()
|
||||||
|
config.provider = {
|
||||||
|
type: type,
|
||||||
|
}
|
||||||
|
this.setConfig(config)
|
||||||
|
}
|
||||||
|
|
||||||
|
ConfigManager.prototype.useEtherscanProvider = function () {
|
||||||
|
var config = this.getConfig()
|
||||||
|
config.provider = {
|
||||||
|
type: 'etherscan',
|
||||||
|
}
|
||||||
|
this.setConfig(config)
|
||||||
|
}
|
||||||
|
|
||||||
|
ConfigManager.prototype.getProvider = function () {
|
||||||
|
var config = this.getConfig()
|
||||||
|
return config.provider
|
||||||
|
}
|
||||||
|
|
||||||
ConfigManager.prototype.getCurrentRpcAddress = function () {
|
ConfigManager.prototype.getCurrentRpcAddress = function () {
|
||||||
var provider = this.getProvider()
|
var provider = this.getProvider()
|
||||||
|
@ -65,7 +65,6 @@ module.exports = class MetamaskController extends EventEmitter {
|
|||||||
// eth data query tools
|
// eth data query tools
|
||||||
this.ethQuery = new EthQuery(this.provider)
|
this.ethQuery = new EthQuery(this.provider)
|
||||||
this.ethStore = new EthStore({
|
this.ethStore = new EthStore({
|
||||||
network: this.networkController.networkStore,
|
|
||||||
provider: this.provider,
|
provider: this.provider,
|
||||||
blockTracker: this.provider,
|
blockTracker: this.provider,
|
||||||
})
|
})
|
||||||
@ -139,7 +138,7 @@ module.exports = class MetamaskController extends EventEmitter {
|
|||||||
this.shapeshiftController.store.subscribe((state) => {
|
this.shapeshiftController.store.subscribe((state) => {
|
||||||
this.store.updateState({ ShapeShiftController: state })
|
this.store.updateState({ ShapeShiftController: state })
|
||||||
})
|
})
|
||||||
this.networkController.providerStore.subscribe((state) => {
|
this.networkController.store.subscribe((state) => {
|
||||||
this.store.updateState({ NetworkController: state })
|
this.store.updateState({ NetworkController: state })
|
||||||
})
|
})
|
||||||
|
|
||||||
|
74
test/unit/network-contoller-test.js
Normal file
74
test/unit/network-contoller-test.js
Normal file
@ -0,0 +1,74 @@
|
|||||||
|
const EventEmitter = require('events')
|
||||||
|
const assert = require('assert')
|
||||||
|
const NetworkController = require('../../app/scripts/controllers/network')
|
||||||
|
|
||||||
|
describe('# Network Controller', function () {
|
||||||
|
let networkController
|
||||||
|
|
||||||
|
beforeEach(function () {
|
||||||
|
networkController = new NetworkController({
|
||||||
|
provider: {
|
||||||
|
type: 'rinkeby',
|
||||||
|
},
|
||||||
|
})
|
||||||
|
// stub out provider
|
||||||
|
networkController._provider = new EventEmitter()
|
||||||
|
networkController.providerInit = {
|
||||||
|
getAccounts: () => {},
|
||||||
|
}
|
||||||
|
|
||||||
|
networkController.ethQuery = new Proxy({}, {
|
||||||
|
get: (obj, name) => {
|
||||||
|
return () => {}
|
||||||
|
},
|
||||||
|
})
|
||||||
|
})
|
||||||
|
describe('network', function () {
|
||||||
|
describe('#provider', function() {
|
||||||
|
it('provider should be updatable without reassignment', function () {
|
||||||
|
networkController.initializeProvider(networkController.providerInit)
|
||||||
|
const provider = networkController.provider
|
||||||
|
networkController._provider = {test: true}
|
||||||
|
assert.ok(provider.test)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
describe('#getNetworkState', function () {
|
||||||
|
it('should return loading when new', function () {
|
||||||
|
let networkState = networkController.getNetworkState()
|
||||||
|
assert.equal(networkState, 'loading', 'network is loading')
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('#setNetworkState', function () {
|
||||||
|
it('should update the network', function () {
|
||||||
|
networkController.setNetworkState(1)
|
||||||
|
let networkState = networkController.getNetworkState()
|
||||||
|
assert.equal(networkState, 1, 'network is 1')
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('#getRpcAddressForType', function () {
|
||||||
|
it('should return the right rpc address', function () {
|
||||||
|
let rpcTarget = networkController.getRpcAddressForType('mainnet')
|
||||||
|
assert.equal(rpcTarget, 'https://mainnet.infura.io/metamask', 'returns the right rpcAddress')
|
||||||
|
})
|
||||||
|
})
|
||||||
|
describe('#setProviderType', function () {
|
||||||
|
it('should update provider.type', function () {
|
||||||
|
networkController.setProviderType('mainnet')
|
||||||
|
const type = networkController.getProviderConfig().type
|
||||||
|
assert.equal(type, 'mainnet', 'provider type is updated')
|
||||||
|
})
|
||||||
|
it('should set the network to loading', function () {
|
||||||
|
networkController.setProviderType('mainnet')
|
||||||
|
const loading = networkController.isNetworkLoading()
|
||||||
|
assert.ok(loading, 'network is loading')
|
||||||
|
})
|
||||||
|
it('should set the right rpcTarget', function () {
|
||||||
|
networkController.setProviderType('mainnet')
|
||||||
|
const rpcTarget = networkController.getProviderConfig().rpcTarget
|
||||||
|
assert.equal(rpcTarget, 'https://mainnet.infura.io/metamask', 'returns the right rpcAddress')
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
@ -2,6 +2,7 @@ const assert = require('assert')
|
|||||||
const EventEmitter = require('events')
|
const EventEmitter = require('events')
|
||||||
const ethUtil = require('ethereumjs-util')
|
const ethUtil = require('ethereumjs-util')
|
||||||
const EthTx = require('ethereumjs-tx')
|
const EthTx = require('ethereumjs-tx')
|
||||||
|
const EthQuery = require('eth-query')
|
||||||
const ObservableStore = require('obs-store')
|
const ObservableStore = require('obs-store')
|
||||||
const clone = require('clone')
|
const clone = require('clone')
|
||||||
const sinon = require('sinon')
|
const sinon = require('sinon')
|
||||||
@ -16,9 +17,10 @@ describe('Transaction Controller', function () {
|
|||||||
|
|
||||||
beforeEach(function () {
|
beforeEach(function () {
|
||||||
txController = new TransactionController({
|
txController = new TransactionController({
|
||||||
networkStore: new ObservableStore({ network: currentNetworkId }),
|
networkStore: new ObservableStore(currentNetworkId),
|
||||||
txHistoryLimit: 10,
|
txHistoryLimit: 10,
|
||||||
blockTracker: new EventEmitter(),
|
blockTracker: new EventEmitter(),
|
||||||
|
ethQuery: new EthQuery(new EventEmitter()),
|
||||||
signTransaction: (ethTx) => new Promise((resolve) => {
|
signTransaction: (ethTx) => new Promise((resolve) => {
|
||||||
ethTx.sign(privKey)
|
ethTx.sign(privKey)
|
||||||
resolve()
|
resolve()
|
||||||
|
@ -9,7 +9,11 @@ describe('txUtils', function () {
|
|||||||
let txUtils
|
let txUtils
|
||||||
|
|
||||||
before(function () {
|
before(function () {
|
||||||
txUtils = new TxUtils()
|
txUtils = new TxUtils(new Proxy({}, {
|
||||||
|
get: (obj, name) => {
|
||||||
|
return () => {}
|
||||||
|
},
|
||||||
|
}))
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('chain Id', function () {
|
describe('chain Id', function () {
|
||||||
|
Loading…
Reference in New Issue
Block a user