mirror of
https://github.com/kremalicious/metamask-extension.git
synced 2024-12-23 09:52:26 +01:00
Make updateTransactionSendFlowHistory background method idempotent (#15585)
This commit is contained in:
parent
3fb7de5768
commit
3154e5e19c
@ -673,14 +673,22 @@ export default class TransactionController extends EventEmitter {
|
|||||||
* state is unapproved. Returns the updated transaction.
|
* state is unapproved. Returns the updated transaction.
|
||||||
*
|
*
|
||||||
* @param {string} txId - transaction id
|
* @param {string} txId - transaction id
|
||||||
|
* @param {number} currentSendFlowHistoryLength - sendFlowHistory entries currently
|
||||||
* @param {Array<{ entry: string, timestamp: number }>} sendFlowHistory -
|
* @param {Array<{ entry: string, timestamp: number }>} sendFlowHistory -
|
||||||
* history to add to the sendFlowHistory property of txMeta.
|
* history to add to the sendFlowHistory property of txMeta.
|
||||||
* @returns {TransactionMeta} the txMeta of the updated transaction
|
* @returns {TransactionMeta} the txMeta of the updated transaction
|
||||||
*/
|
*/
|
||||||
updateTransactionSendFlowHistory(txId, sendFlowHistory) {
|
updateTransactionSendFlowHistory(
|
||||||
|
txId,
|
||||||
|
currentSendFlowHistoryLength,
|
||||||
|
sendFlowHistory,
|
||||||
|
) {
|
||||||
this._throwErrorIfNotUnapprovedTx(txId, 'updateTransactionSendFlowHistory');
|
this._throwErrorIfNotUnapprovedTx(txId, 'updateTransactionSendFlowHistory');
|
||||||
const txMeta = this._getTransaction(txId);
|
const txMeta = this._getTransaction(txId);
|
||||||
|
|
||||||
|
if (
|
||||||
|
currentSendFlowHistoryLength === (txMeta?.sendFlowHistory?.length || 0)
|
||||||
|
) {
|
||||||
// only update what is defined
|
// only update what is defined
|
||||||
const note = `Update sendFlowHistory for ${txId}`;
|
const note = `Update sendFlowHistory for ${txId}`;
|
||||||
|
|
||||||
@ -694,6 +702,7 @@ export default class TransactionController extends EventEmitter {
|
|||||||
},
|
},
|
||||||
note,
|
note,
|
||||||
);
|
);
|
||||||
|
}
|
||||||
return this._getTransaction(txId);
|
return this._getTransaction(txId);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
import { strict as assert } from 'assert';
|
import { strict as assert } from 'assert';
|
||||||
import sinon from 'sinon';
|
import sinon from 'sinon';
|
||||||
import proxyquire from 'proxyquire';
|
import proxyquire from 'proxyquire';
|
||||||
|
import { ORIGIN_METAMASK } from '../../shared/constants/app';
|
||||||
|
|
||||||
const Ganache = require('../../test/e2e/ganache');
|
const Ganache = require('../../test/e2e/ganache');
|
||||||
|
|
||||||
@ -209,4 +210,32 @@ describe('MetaMaskController', function () {
|
|||||||
assert.equal(rpcList1Length, rpcList2Length);
|
assert.equal(rpcList1Length, rpcList2Length);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('#updateTransactionSendFlowHistory', function () {
|
||||||
|
it('two sequential calls with same history give same result', async function () {
|
||||||
|
const recipientAddress = '0xc42edfcc21ed14dda456aa0756c153f7985d8813';
|
||||||
|
|
||||||
|
await metamaskController.createNewVaultAndKeychain('test@123');
|
||||||
|
const accounts = await metamaskController.keyringController.getAccounts();
|
||||||
|
const txMeta = await metamaskController.getApi().addUnapprovedTransaction(
|
||||||
|
{
|
||||||
|
from: accounts[0],
|
||||||
|
to: recipientAddress,
|
||||||
|
},
|
||||||
|
ORIGIN_METAMASK,
|
||||||
|
);
|
||||||
|
|
||||||
|
const [transaction1, transaction2] = await Promise.all([
|
||||||
|
metamaskController
|
||||||
|
.getApi()
|
||||||
|
.updateTransactionSendFlowHistory(txMeta.id, 2, ['foo1', 'foo2']),
|
||||||
|
Promise.resolve(1).then(() =>
|
||||||
|
metamaskController
|
||||||
|
.getApi()
|
||||||
|
.updateTransactionSendFlowHistory(txMeta.id, 2, ['foo1', 'foo2']),
|
||||||
|
),
|
||||||
|
]);
|
||||||
|
assert.deepEqual(transaction1, transaction2);
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
@ -2269,6 +2269,7 @@ export function signTransaction() {
|
|||||||
await dispatch(
|
await dispatch(
|
||||||
updateTransactionSendFlowHistory(
|
updateTransactionSendFlowHistory(
|
||||||
draftTransaction.id,
|
draftTransaction.id,
|
||||||
|
unapprovedTx.sendFlowHistory?.length || 0,
|
||||||
draftTransaction.history,
|
draftTransaction.history,
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
@ -97,7 +97,7 @@ setBackgroundConnection({
|
|||||||
addUnapprovedTransaction: jest.fn((_v, _w, _x, _y, _z, cb) => {
|
addUnapprovedTransaction: jest.fn((_v, _w, _x, _y, _z, cb) => {
|
||||||
cb(null);
|
cb(null);
|
||||||
}),
|
}),
|
||||||
updateTransactionSendFlowHistory: jest.fn((_x, _y, cb) => cb(null)),
|
updateTransactionSendFlowHistory: jest.fn((_x, _y, _z, cb) => cb(null)),
|
||||||
});
|
});
|
||||||
|
|
||||||
const getTestUUIDTx = (state) => state.draftTransactions['test-uuid'];
|
const getTestUUIDTx = (state) => state.draftTransactions['test-uuid'];
|
||||||
|
@ -761,17 +761,22 @@ export function updateEditableParams(txId, editableParams) {
|
|||||||
* Appends new send flow history to a transaction
|
* Appends new send flow history to a transaction
|
||||||
*
|
*
|
||||||
* @param {string} txId - the id of the transaction to update
|
* @param {string} txId - the id of the transaction to update
|
||||||
|
* @param {number} currentSendFlowHistoryLength - sendFlowHistory entries currently
|
||||||
* @param {Array<{event: string, timestamp: number}>} sendFlowHistory - the new send flow history to append to the
|
* @param {Array<{event: string, timestamp: number}>} sendFlowHistory - the new send flow history to append to the
|
||||||
* transaction
|
* transaction
|
||||||
* @returns {import('../../shared/constants/transaction').TransactionMeta}
|
* @returns {import('../../shared/constants/transaction').TransactionMeta}
|
||||||
*/
|
*/
|
||||||
export function updateTransactionSendFlowHistory(txId, sendFlowHistory) {
|
export function updateTransactionSendFlowHistory(
|
||||||
|
txId,
|
||||||
|
currentSendFlowHistoryLength,
|
||||||
|
sendFlowHistory,
|
||||||
|
) {
|
||||||
return async (dispatch) => {
|
return async (dispatch) => {
|
||||||
let updatedTransaction;
|
let updatedTransaction;
|
||||||
try {
|
try {
|
||||||
updatedTransaction = await submitRequestToBackground(
|
updatedTransaction = await submitRequestToBackground(
|
||||||
'updateTransactionSendFlowHistory',
|
'updateTransactionSendFlowHistory',
|
||||||
[txId, sendFlowHistory],
|
[txId, currentSendFlowHistoryLength, sendFlowHistory],
|
||||||
);
|
);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
dispatch(txError(error));
|
dispatch(txError(error));
|
||||||
|
Loading…
Reference in New Issue
Block a user