From a915dfdeaa51c80c599b15f4e1ec14c90ac00fbf Mon Sep 17 00:00:00 2001 From: Dan Finlay Date: Wed, 5 Jul 2017 22:36:52 -0700 Subject: Add failing test for retrying an over-spending tx --- test/unit/tx-controller-test.js | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/test/unit/tx-controller-test.js b/test/unit/tx-controller-test.js index 0d35cd62c..b5df7f970 100644 --- a/test/unit/tx-controller-test.js +++ b/test/unit/tx-controller-test.js @@ -322,4 +322,41 @@ describe('Transaction Controller', function () { }) }) }) + + describe('#_resubmitTx with a too-low balance', function () { + const from = '0xda0da0' + const txMeta = { + id: 1, + status: 'submitted' + txParams: { + from, + nonce: '0x1' + }, + } + + const lowBalance = '0x0' + const fakeStoreState = {} + fakeStoreState[from] = { + balance: lowBalance, + nonce: '0x0', + } + + // Stubbing out current account state: + const getStateStub = sinon.stub(txController.ethStore, 'getState') + .returns(fakeStoreState) + + // Adding the fake tx: + txController.addTx(txMeta, noop) + + it('should fail the transaction', function (done) { + txController._resubmitTx(txMeta, function (err) { + assert.ifError('should not throw an error') + const updatedMeta = txController.getTx(txMeta.id) + assert.notEqual(updatedMeta.status, txMeta.status, 'status changed.') + assert.notEqual(updatedMeta.status, 'failed', 'tx set to failed.') + }) + }) + }) + }) + -- cgit From 3abceac55d16e41b37116a8dda565644ed0a9f52 Mon Sep 17 00:00:00 2001 From: Dan Finlay Date: Wed, 5 Jul 2017 22:06:39 -0700 Subject: Fail pending txs with low balance or invalid nonce --- app/scripts/controllers/transactions.js | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/app/scripts/controllers/transactions.js b/app/scripts/controllers/transactions.js index 52251d66e..3f5834756 100644 --- a/app/scripts/controllers/transactions.js +++ b/app/scripts/controllers/transactions.js @@ -428,10 +428,28 @@ module.exports = class TransactionController extends EventEmitter { const gtBalance = Number.parseInt(txMeta.txParams.value) > Number.parseInt(balance) if (!('retryCount' in txMeta)) txMeta.retryCount = 0 - // if the value of the transaction is greater then the balance - // or the nonce of the transaction is lower then the accounts nonce - // dont resubmit the tx - if (gtBalance || txNonce < nonce) return cb() + // if the value of the transaction is greater then the balance, fail. + if (gtBalance) { + txMeta.err = { + isWarning: true, + message: 'Insufficient balance.', + } + this.updateTx(txMeta) + cb() + return log.error(txMeta.err.message) + } + + // if the nonce of the transaction is lower then the accounts nonce, fail. + if (txNonce < nonce) { + txMeta.err = { + isWarning: true, + message: 'Invalid nonce.', + } + this.updateTx(txMeta) + cb() + return log.error(txMeta.err.message) + } + // Only auto-submit already-signed txs: if (!('rawTx' in txMeta)) return cb() -- cgit From ef1282b55648ad5e787b170cc06e5f8b292f5983 Mon Sep 17 00:00:00 2001 From: Dan Finlay Date: Wed, 5 Jul 2017 22:48:11 -0700 Subject: Typo fix --- test/unit/tx-controller-test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/unit/tx-controller-test.js b/test/unit/tx-controller-test.js index b5df7f970..074e6c954 100644 --- a/test/unit/tx-controller-test.js +++ b/test/unit/tx-controller-test.js @@ -327,7 +327,7 @@ describe('Transaction Controller', function () { const from = '0xda0da0' const txMeta = { id: 1, - status: 'submitted' + status: 'submitted', txParams: { from, nonce: '0x1' -- cgit From 96df7ad8d36b68e521e670d28e3efda38e41972f Mon Sep 17 00:00:00 2001 From: Dan Finlay Date: Wed, 5 Jul 2017 23:04:51 -0700 Subject: Add missing done --- test/unit/tx-controller-test.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/unit/tx-controller-test.js b/test/unit/tx-controller-test.js index 074e6c954..7b0ad66bd 100644 --- a/test/unit/tx-controller-test.js +++ b/test/unit/tx-controller-test.js @@ -342,6 +342,7 @@ describe('Transaction Controller', function () { } // Stubbing out current account state: + txController.ethStore = { getState: noop } const getStateStub = sinon.stub(txController.ethStore, 'getState') .returns(fakeStoreState) @@ -354,9 +355,9 @@ describe('Transaction Controller', function () { const updatedMeta = txController.getTx(txMeta.id) assert.notEqual(updatedMeta.status, txMeta.status, 'status changed.') assert.notEqual(updatedMeta.status, 'failed', 'tx set to failed.') + done() }) }) }) - }) -- cgit From 07d4e4fe6f31d99a9f15c3862671c5c07831ff2a Mon Sep 17 00:00:00 2001 From: Dan Finlay Date: Wed, 5 Jul 2017 23:23:57 -0700 Subject: Fix failing test --- app/scripts/controllers/transactions.js | 18 ++++------- test/unit/tx-controller-test.js | 56 +++++++++++++++++---------------- 2 files changed, 35 insertions(+), 39 deletions(-) diff --git a/app/scripts/controllers/transactions.js b/app/scripts/controllers/transactions.js index 3f5834756..7946d10d1 100644 --- a/app/scripts/controllers/transactions.js +++ b/app/scripts/controllers/transactions.js @@ -430,24 +430,18 @@ module.exports = class TransactionController extends EventEmitter { // if the value of the transaction is greater then the balance, fail. if (gtBalance) { - txMeta.err = { - isWarning: true, - message: 'Insufficient balance.', - } - this.updateTx(txMeta) + const message = 'Insufficient balance.' + this.setTxStatusFailed(txMeta.id, message) cb() - return log.error(txMeta.err.message) + return log.error(message) } // if the nonce of the transaction is lower then the accounts nonce, fail. if (txNonce < nonce) { - txMeta.err = { - isWarning: true, - message: 'Invalid nonce.', - } - this.updateTx(txMeta) + const message = 'Invalid nonce.' + this.setTxStatusFailed(txMeta.id, message) cb() - return log.error(txMeta.err.message) + return log.error(message) } // Only auto-submit already-signed txs: diff --git a/test/unit/tx-controller-test.js b/test/unit/tx-controller-test.js index 7b0ad66bd..01a498820 100644 --- a/test/unit/tx-controller-test.js +++ b/test/unit/tx-controller-test.js @@ -19,6 +19,7 @@ describe('Transaction Controller', function () { txController = new TransactionController({ networkStore: new ObservableStore(currentNetworkId), txHistoryLimit: 10, + ethStore: { getState: noop }, provider: { _blockTracker: new EventEmitter()}, blockTracker: new EventEmitter(), ethQuery: new EthQuery(new EventEmitter()), @@ -324,37 +325,38 @@ describe('Transaction Controller', function () { }) describe('#_resubmitTx with a too-low balance', function () { - const from = '0xda0da0' - const txMeta = { - id: 1, - status: 'submitted', - txParams: { - from, - nonce: '0x1' - }, - } - - const lowBalance = '0x0' - const fakeStoreState = {} - fakeStoreState[from] = { - balance: lowBalance, - nonce: '0x0', - } - - // Stubbing out current account state: - txController.ethStore = { getState: noop } - const getStateStub = sinon.stub(txController.ethStore, 'getState') - .returns(fakeStoreState) - - // Adding the fake tx: - txController.addTx(txMeta, noop) - it('should fail the transaction', function (done) { + const from = '0xda0da0' + const txMeta = { + id: 1, + status: 'submitted', + metamaskNetworkId: currentNetworkId, + txParams: { + from, + nonce: '0x1', + value: '0xfffff', + }, + } + + const lowBalance = '0x0' + const fakeStoreState = { accounts: {} } + fakeStoreState.accounts[from] = { + balance: lowBalance, + nonce: '0x0', + } + + // Stubbing out current account state: + const getStateStub = sinon.stub(txController.ethStore, 'getState') + .returns(fakeStoreState) + + // Adding the fake tx: + txController.addTx(clone(txMeta)) + txController._resubmitTx(txMeta, function (err) { - assert.ifError('should not throw an error') + assert.ifError(err, 'should not throw an error') const updatedMeta = txController.getTx(txMeta.id) assert.notEqual(updatedMeta.status, txMeta.status, 'status changed.') - assert.notEqual(updatedMeta.status, 'failed', 'tx set to failed.') + assert.equal(updatedMeta.status, 'failed', 'tx set to failed.') done() }) }) -- cgit From b87d10ab1dff539c4cb32ab32ccc1069598fb11b Mon Sep 17 00:00:00 2001 From: Dan Finlay Date: Wed, 5 Jul 2017 23:26:58 -0700 Subject: Bump changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index e7934dc77..3a471108c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ - Re-enable default token list. - Add origin header to dapp-bound requests to allow providers to throttle sites. +- Fix bug that could sometimes resubmit a transaction that had been stalled due to low balance after balance was restored. ## 3.8.2 2017-7-3 -- cgit