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 MetaMaskProvider = require('web3-provider-engine/zero.js')
|
||||
const ObservableStore = require('obs-store')
|
||||
const ComposedStore = require('obs-store/lib/composed')
|
||||
const extend = require('xtend')
|
||||
const EthQuery = require('eth-query')
|
||||
const RPC_ADDRESS_LIST = require('../config.js').network
|
||||
const DEFAULT_RPC = RPC_ADDRESS_LIST['rinkeby']
|
||||
|
||||
module.exports = class NetworkController extends EventEmitter {
|
||||
constructor (providerOpts) {
|
||||
constructor (config) {
|
||||
super()
|
||||
this.networkStore = new ObservableStore({ network: 'loading' })
|
||||
providerOpts.provider.rpcTarget = this.getRpcAddressForType(providerOpts.provider.type, providerOpts.provider)
|
||||
this.providerStore = new ObservableStore(providerOpts)
|
||||
this.store = new ObservableStore(extend(this.networkStore.getState(), this.providerStore.getState()))
|
||||
this.networkStore = new ObservableStore('loading')
|
||||
config.provider.rpcTarget = this.getRpcAddressForType(config.provider.type, config.provider)
|
||||
this.providerStore = new ObservableStore(config.provider)
|
||||
this.store = new ComposedStore({ provider: this.providerStore, network: this.networkStore })
|
||||
this._providerListeners = {}
|
||||
|
||||
this._providerListners = {}
|
||||
|
||||
this.networkStore.subscribe((state) => this.store.updateState(state))
|
||||
this.providerStore.subscribe((state) => this.store.updateState(state))
|
||||
this.on('networkSwitch', this.lookupNetwork)
|
||||
this.on('networkDidChange', this.lookupNetwork)
|
||||
this.providerStore.subscribe((state) => this.switchNetwork({rpcUrl: state.rpcTarget}))
|
||||
}
|
||||
|
||||
get provider () {
|
||||
@ -28,15 +28,8 @@ module.exports = class NetworkController extends EventEmitter {
|
||||
this._provider = provider
|
||||
}
|
||||
|
||||
getState () {
|
||||
return extend({},
|
||||
this.networkStore.getState(),
|
||||
this.providerStore.getState()
|
||||
)
|
||||
}
|
||||
|
||||
initializeProvider (opts) {
|
||||
this.providerConfig = opts
|
||||
this.providerInit = opts
|
||||
this._provider = MetaMaskProvider(opts)
|
||||
this._proxy = new Proxy(this._provider, {
|
||||
get: (obj, name) => {
|
||||
@ -54,16 +47,18 @@ module.exports = class NetworkController extends EventEmitter {
|
||||
return this.provider
|
||||
}
|
||||
|
||||
switchNetwork (providerConfig) {
|
||||
const newConfig = extend(this.providerConfig, providerConfig)
|
||||
this.providerConfig = newConfig
|
||||
switchNetwork (providerInit) {
|
||||
this.setNetworkState('loading')
|
||||
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
|
||||
Object.keys(this._providerListners).forEach((key) => {
|
||||
this._providerListners[key].forEach((handler) => this._provider.addListener(key, handler))
|
||||
Object.keys(this._providerListeners).forEach((key) => {
|
||||
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 () {
|
||||
return this.networkStore.getState().network
|
||||
return this.networkStore.getState()
|
||||
}
|
||||
|
||||
setNetworkState (network) {
|
||||
return this.networkStore.updateState({ network })
|
||||
return this.networkStore.putState(network)
|
||||
}
|
||||
|
||||
isNetworkLoading () {
|
||||
return this.getNetworkState() === 'loading'
|
||||
}
|
||||
|
||||
lookupNetwork (err) {
|
||||
if (err) this.setNetworkState('loading')
|
||||
|
||||
lookupNetwork () {
|
||||
this.ethQuery.sendAsync({ method: 'net_version' }, (err, network) => {
|
||||
if (err) return this.setNetworkState('loading')
|
||||
log.info('web3.getNetwork returned ' + network)
|
||||
@ -96,37 +89,30 @@ module.exports = class NetworkController extends EventEmitter {
|
||||
|
||||
setRpcTarget (rpcUrl) {
|
||||
this.providerStore.updateState({
|
||||
provider: {
|
||||
type: 'rpc',
|
||||
rpcTarget: rpcUrl,
|
||||
},
|
||||
type: 'rpc',
|
||||
rpcTarget: rpcUrl,
|
||||
})
|
||||
}
|
||||
|
||||
getCurrentRpcAddress () {
|
||||
var provider = this.getProvider()
|
||||
const provider = this.getProviderConfig()
|
||||
if (!provider) return null
|
||||
return this.getRpcAddressForType(provider.type)
|
||||
}
|
||||
|
||||
setProviderType (type) {
|
||||
if (type === this.getProvider().type) return
|
||||
if (type === this.getProviderConfig().type) return
|
||||
const rpcTarget = this.getRpcAddressForType(type)
|
||||
this.networkStore.updateState({network: 'loading'})
|
||||
this.switchNetwork({
|
||||
rpcUrl: rpcTarget,
|
||||
})
|
||||
this.providerStore.updateState({provider: {type, rpcTarget}})
|
||||
this.providerStore.updateState({type, rpcTarget})
|
||||
}
|
||||
|
||||
getProvider () {
|
||||
return this.providerStore.getState().provider
|
||||
getProviderConfig () {
|
||||
return this.providerStore.getState()
|
||||
}
|
||||
|
||||
getRpcAddressForType (type, provider = this.getProvider()) {
|
||||
console.log(`#getRpcAddressForType: ${type}`)
|
||||
if (type in RPC_ADDRESS_LIST) return RPC_ADDRESS_LIST[type]
|
||||
return provider && provider.rpcTarget ? provider.rpcTarget : RPC_ADDRESS_LIST['rinkeby']
|
||||
getRpcAddressForType (type, provider = this.getProviderConfig()) {
|
||||
if (RPC_ADDRESS_LIST[type]) return RPC_ADDRESS_LIST[type]
|
||||
return provider && provider.rpcTarget ? provider.rpcTarget : DEFAULT_RPC
|
||||
}
|
||||
|
||||
_logBlock (block) {
|
||||
@ -135,8 +121,8 @@ module.exports = class NetworkController extends EventEmitter {
|
||||
}
|
||||
|
||||
_on (event, handler) {
|
||||
if (!this._providerListners[event]) this._providerListners[event] = []
|
||||
this._providerListners[event].push(handler)
|
||||
if (!this._providerListeners[event]) this._providerListeners[event] = []
|
||||
this._providerListeners[event].push(handler)
|
||||
this._provider.on(event, handler)
|
||||
}
|
||||
}
|
||||
|
@ -21,9 +21,7 @@ module.exports = class TransactionManager extends EventEmitter {
|
||||
this.blockTracker = opts.blockTracker
|
||||
this.query = opts.ethQuery
|
||||
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.signEthTx = opts.signTransaction
|
||||
this.nonceLock = Semaphore(1)
|
||||
|
||||
@ -39,7 +37,7 @@ module.exports = class TransactionManager extends EventEmitter {
|
||||
}
|
||||
|
||||
getNetwork () {
|
||||
return this.networkStore.getState().network
|
||||
return this.networkStore.getState()
|
||||
}
|
||||
|
||||
getSelectedAddress () {
|
||||
|
@ -3,7 +3,8 @@
|
||||
//
|
||||
|
||||
module.exports = {
|
||||
config: {
|
||||
config: {},
|
||||
NetworkController: {
|
||||
provider: {
|
||||
type: 'rinkeby',
|
||||
},
|
||||
|
@ -107,6 +107,35 @@ ConfigManager.prototype.getSeedWords = function () {
|
||||
var data = this.getData()
|
||||
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 () {
|
||||
var provider = this.getProvider()
|
||||
|
@ -65,7 +65,6 @@ module.exports = class MetamaskController extends EventEmitter {
|
||||
// eth data query tools
|
||||
this.ethQuery = new EthQuery(this.provider)
|
||||
this.ethStore = new EthStore({
|
||||
network: this.networkController.networkStore,
|
||||
provider: this.provider,
|
||||
blockTracker: this.provider,
|
||||
})
|
||||
@ -139,7 +138,7 @@ module.exports = class MetamaskController extends EventEmitter {
|
||||
this.shapeshiftController.store.subscribe((state) => {
|
||||
this.store.updateState({ ShapeShiftController: state })
|
||||
})
|
||||
this.networkController.providerStore.subscribe((state) => {
|
||||
this.networkController.store.subscribe((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 ethUtil = require('ethereumjs-util')
|
||||
const EthTx = require('ethereumjs-tx')
|
||||
const EthQuery = require('eth-query')
|
||||
const ObservableStore = require('obs-store')
|
||||
const clone = require('clone')
|
||||
const sinon = require('sinon')
|
||||
@ -16,9 +17,10 @@ describe('Transaction Controller', function () {
|
||||
|
||||
beforeEach(function () {
|
||||
txController = new TransactionController({
|
||||
networkStore: new ObservableStore({ network: currentNetworkId }),
|
||||
networkStore: new ObservableStore(currentNetworkId),
|
||||
txHistoryLimit: 10,
|
||||
blockTracker: new EventEmitter(),
|
||||
ethQuery: new EthQuery(new EventEmitter()),
|
||||
signTransaction: (ethTx) => new Promise((resolve) => {
|
||||
ethTx.sign(privKey)
|
||||
resolve()
|
||||
|
@ -9,7 +9,11 @@ describe('txUtils', function () {
|
||||
let txUtils
|
||||
|
||||
before(function () {
|
||||
txUtils = new TxUtils()
|
||||
txUtils = new TxUtils(new Proxy({}, {
|
||||
get: (obj, name) => {
|
||||
return () => {}
|
||||
},
|
||||
}))
|
||||
})
|
||||
|
||||
describe('chain Id', function () {
|
||||
|
Loading…
Reference in New Issue
Block a user