From b5d6452454de8d12340e5902914fba9f420865dc Mon Sep 17 00:00:00 2001 From: Frankie Date: Thu, 13 Dec 2018 09:14:46 -1000 Subject: [PATCH] Disallow loading as metamaskNetworkId (#5924) * transactions - throw an error if a transaction is generated while the network is loading * add tests for failing when netId is loading --- .../controllers/transactions/tx-state-manager.js | 4 +++- .../controllers/transactions/tx-controller-test.js | 13 +++++++++++-- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/app/scripts/controllers/transactions/tx-state-manager.js b/app/scripts/controllers/transactions/tx-state-manager.js index 72d869fa8..420191d9c 100644 --- a/app/scripts/controllers/transactions/tx-state-manager.js +++ b/app/scripts/controllers/transactions/tx-state-manager.js @@ -45,11 +45,13 @@ class TransactionStateManager extends EventEmitter { @returns {txMeta} the default txMeta object */ generateTxMeta (opts) { + const netId = this.getNetwork() + if (netId === 'loading') throw new Error('MetaMask is having trouble connecting to the network') return extend({ id: createId(), time: (new Date()).getTime(), status: 'unapproved', - metamaskNetworkId: this.getNetwork(), + metamaskNetworkId: netId, loadingDefaults: true, }, opts) } diff --git a/test/unit/app/controllers/transactions/tx-controller-test.js b/test/unit/app/controllers/transactions/tx-controller-test.js index 74161e26c..9000cd364 100644 --- a/test/unit/app/controllers/transactions/tx-controller-test.js +++ b/test/unit/app/controllers/transactions/tx-controller-test.js @@ -12,7 +12,7 @@ const { createTestProviderTools, getTestAccounts } = require('../../../../stub/p const noop = () => true const currentNetworkId = 42 - +const netStore = new ObservableStore(currentNetworkId) describe('Transaction Controller', function () { let txController, provider, providerResultStub, fromAccount @@ -32,7 +32,7 @@ describe('Transaction Controller', function () { txController = new TransactionController({ provider, getGasPrice: function () { return '0xee6b2800' }, - networkStore: new ObservableStore(currentNetworkId), + networkStore: netStore, txHistoryLimit: 10, blockTracker: blockTrackerStub, signTransaction: (ethTx) => new Promise((resolve) => { @@ -227,6 +227,15 @@ describe('Transaction Controller', function () { txController.addUnapprovedTransaction({ from: selectedAddress, to: '0x0d1d4e623D10F9FBA5Db95830F7d3839406C6AF2' }) .catch(done) }) + + it('should fail if netId is loading', function (done) { + txController.networkStore = new ObservableStore('loading') + txController.addUnapprovedTransaction({ from: selectedAddress, to: '0x0d1d4e623D10F9FBA5Db95830F7d3839406C6AF2' }) + .catch((err) => { + if (err.message === 'MetaMask is having trouble connecting to the network') done() + else done(err) + }) + }) }) describe('#addTxGasDefaults', function () {