1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-12-23 09:52:26 +01:00

Using current time in place of block timestamp for completion time metric (#11483)

This commit is contained in:
ryanml 2021-07-12 10:14:54 -07:00
parent c87dce9ce5
commit 0df928efa9
2 changed files with 30 additions and 21 deletions

View File

@ -669,15 +669,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') {
@ -1105,20 +1102,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) {

View File

@ -1386,4 +1386,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');
});
});
});