From 724bd42e2cba55b5ab42c4ee68431eeb65f2bd05 Mon Sep 17 00:00:00 2001 From: Dan J Miller Date: Wed, 27 Nov 2019 09:28:03 -0330 Subject: [PATCH] Ensures the tx controller + state-manager orders transactions as received (#7484) * Ensures the tx controller + tx-state-manager orders transactions in the order they are received * Handle transaction ordering in cases where tx ids are off by more than 1 in tx-state-manager * Add comment to addUnapprovedTransaction explaining calling _determineTransactionCategory after generateTxMeta * Sort txes by timestamp of creation instead of id --- app/scripts/controllers/transactions/index.js | 11 ++++++++--- .../controllers/transactions/tx-state-manager.js | 7 ++++++- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/app/scripts/controllers/transactions/index.js b/app/scripts/controllers/transactions/index.js index f19dd53f7..31f8cc103 100644 --- a/app/scripts/controllers/transactions/index.js +++ b/app/scripts/controllers/transactions/index.js @@ -192,13 +192,18 @@ class TransactionController extends EventEmitter { throw new Error(`Transaction from address isn't valid for this account`) } txUtils.validateTxParams(normalizedTxParams) - // construct txMeta - const { transactionCategory, getCodeResponse } = await this._determineTransactionCategory(txParams) + /** + `generateTxMeta` adds the default txMeta properties to the passed object. + These include the tx's `id`. As we use the id for determining order of + txes in the tx-state-manager, it is necessary to call the asynchronous + method `this._determineTransactionCategory` after `generateTxMeta`. + */ let txMeta = this.txStateManager.generateTxMeta({ txParams: normalizedTxParams, type: TRANSACTION_TYPE_STANDARD, - transactionCategory, }) + const { transactionCategory, getCodeResponse } = await this._determineTransactionCategory(txParams) + txMeta.transactionCategory = transactionCategory this.addTx(txMeta) this.emit('newUnapprovedTx', txMeta) diff --git a/app/scripts/controllers/transactions/tx-state-manager.js b/app/scripts/controllers/transactions/tx-state-manager.js index fb9359c79..ffbe6f010 100644 --- a/app/scripts/controllers/transactions/tx-state-manager.js +++ b/app/scripts/controllers/transactions/tx-state-manager.js @@ -167,7 +167,12 @@ class TransactionStateManager extends EventEmitter { transactions.splice(index, 1) } } - transactions.push(txMeta) + const newTxIndex = transactions + .findIndex((currentTxMeta) => currentTxMeta.time > txMeta.time) + + newTxIndex === -1 + ? transactions.push(txMeta) + : transactions.splice(newTxIndex, 0, txMeta) this._saveTxList(transactions) return txMeta }