From 36ade8bf9337a70bfdb05f2442716d451c46945c Mon Sep 17 00:00:00 2001 From: Dan Miller Date: Mon, 18 Nov 2019 12:43:15 -0330 Subject: [PATCH 1/4] Ensures the tx controller + tx-state-manager orders transactions in the order they are received --- app/scripts/controllers/transactions/index.js | 4 ++-- app/scripts/controllers/transactions/tx-state-manager.js | 7 ++++++- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/app/scripts/controllers/transactions/index.js b/app/scripts/controllers/transactions/index.js index f19dd53f7985..9c1fc2a4e3e0 100644 --- a/app/scripts/controllers/transactions/index.js +++ b/app/scripts/controllers/transactions/index.js @@ -193,12 +193,12 @@ class TransactionController extends EventEmitter { } txUtils.validateTxParams(normalizedTxParams) // construct txMeta - const { transactionCategory, getCodeResponse } = await this._determineTransactionCategory(txParams) 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 fb9359c79174..7b5ee21bfe15 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((metaTx) => { + return metaTx.id === txMeta.id + 1 + }) + newTxIndex === -1 + ? transactions.push(txMeta) + : transactions.splice(newTxIndex, 0, txMeta) this._saveTxList(transactions) return txMeta } From ccd7e625782603bfb8f8c8d31c62b152d59094c1 Mon Sep 17 00:00:00 2001 From: Dan Miller Date: Tue, 26 Nov 2019 13:50:13 -0330 Subject: [PATCH 2/4] Handle transaction ordering in cases where tx ids are off by more than 1 in tx-state-manager --- app/scripts/controllers/transactions/tx-state-manager.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/app/scripts/controllers/transactions/tx-state-manager.js b/app/scripts/controllers/transactions/tx-state-manager.js index 7b5ee21bfe15..8282d665bdbe 100644 --- a/app/scripts/controllers/transactions/tx-state-manager.js +++ b/app/scripts/controllers/transactions/tx-state-manager.js @@ -167,9 +167,9 @@ class TransactionStateManager extends EventEmitter { transactions.splice(index, 1) } } - const newTxIndex = transactions.findIndex((metaTx) => { - return metaTx.id === txMeta.id + 1 - }) + const newTxIndex = transactions + .findIndex((currentTxMeta) => currentTxMeta.id > txMeta.id) + newTxIndex === -1 ? transactions.push(txMeta) : transactions.splice(newTxIndex, 0, txMeta) From 47bd579c8047d7e24c78085ee3f2d2b15fb9b0e3 Mon Sep 17 00:00:00 2001 From: Dan Miller Date: Tue, 26 Nov 2019 13:55:47 -0330 Subject: [PATCH 3/4] Add comment to addUnapprovedTransaction explaining calling _determineTransactionCategory after generateTxMeta --- app/scripts/controllers/transactions/index.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/app/scripts/controllers/transactions/index.js b/app/scripts/controllers/transactions/index.js index 9c1fc2a4e3e0..31f8cc103972 100644 --- a/app/scripts/controllers/transactions/index.js +++ b/app/scripts/controllers/transactions/index.js @@ -192,7 +192,12 @@ class TransactionController extends EventEmitter { throw new Error(`Transaction from address isn't valid for this account`) } txUtils.validateTxParams(normalizedTxParams) - // construct txMeta + /** + `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, From 81f0afe92ae18adaa6a935fd6f2e574ed395218d Mon Sep 17 00:00:00 2001 From: Dan Miller Date: Tue, 26 Nov 2019 17:24:28 -0330 Subject: [PATCH 4/4] Sort txes by timestamp of creation instead of id --- app/scripts/controllers/transactions/tx-state-manager.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/scripts/controllers/transactions/tx-state-manager.js b/app/scripts/controllers/transactions/tx-state-manager.js index 8282d665bdbe..ffbe6f0107b0 100644 --- a/app/scripts/controllers/transactions/tx-state-manager.js +++ b/app/scripts/controllers/transactions/tx-state-manager.js @@ -168,7 +168,7 @@ class TransactionStateManager extends EventEmitter { } } const newTxIndex = transactions - .findIndex((currentTxMeta) => currentTxMeta.id > txMeta.id) + .findIndex((currentTxMeta) => currentTxMeta.time > txMeta.time) newTxIndex === -1 ? transactions.push(txMeta)