From bf4cdb0b5c4ac647e7653161b486366896a14c99 Mon Sep 17 00:00:00 2001 From: ryanml Date: Mon, 12 Jul 2021 10:14:54 -0700 Subject: [PATCH] Using current time in place of block timestamp for completion time metric (#11483) --- app/scripts/controllers/transactions/index.js | 27 +++++-------------- .../controllers/transactions/index.test.js | 24 +++++++++++++++++ 2 files changed, 30 insertions(+), 21 deletions(-) diff --git a/app/scripts/controllers/transactions/index.js b/app/scripts/controllers/transactions/index.js index 7af9f9912..adebd4204 100644 --- a/app/scripts/controllers/transactions/index.js +++ b/app/scripts/controllers/transactions/index.js @@ -820,15 +820,12 @@ export default class TransactionController extends EventEmitter { this._markNonceDuplicatesDropped(txId); const { submittedTime } = txMeta; - const { blockNumber } = txReceipt; const metricsParams = { gas_used: gasUsed }; - const completionTime = await this._getTransactionCompletionTime( - blockNumber, - submittedTime, - ); - if (completionTime) { - metricsParams.completion_time = completionTime; + if (submittedTime) { + metricsParams.completion_time = this._getTransactionCompletionTime( + submittedTime, + ); } if (txReceipt.status === '0x0') { @@ -1256,20 +1253,8 @@ export default class TransactionController extends EventEmitter { }); } - async _getTransactionCompletionTime(blockNumber, submittedTime) { - const transactionBlock = await this.query.getBlockByNumber( - blockNumber.toString(16), - false, - ); - - if (!transactionBlock) { - return ''; - } - - return new BigNumber(transactionBlock.timestamp, 10) - .minus(submittedTime / 1000) - .round() - .toString(10); + _getTransactionCompletionTime(submittedTime) { + return Math.round((Date.now() - submittedTime) / 1000).toString(); } _failTransaction(txId, error) { diff --git a/app/scripts/controllers/transactions/index.test.js b/app/scripts/controllers/transactions/index.test.js index 6e0d6421c..2f4c96f19 100644 --- a/app/scripts/controllers/transactions/index.test.js +++ b/app/scripts/controllers/transactions/index.test.js @@ -1391,4 +1391,28 @@ describe('Transaction Controller', function () { ); }); }); + + describe('#_getTransactionCompletionTime', function () { + let nowStub; + + beforeEach(function () { + nowStub = sinon.stub(Date, 'now').returns(1625782016341); + }); + + afterEach(function () { + nowStub.restore(); + }); + + it('calculates completion time (one)', function () { + const submittedTime = 1625781997397; + const result = txController._getTransactionCompletionTime(submittedTime); + assert.equal(result, '19'); + }); + + it('calculates completion time (two)', function () { + const submittedTime = 1625781995397; + const result = txController._getTransactionCompletionTime(submittedTime); + assert.equal(result, '21'); + }); + }); });