Skip to content

Commit b0e8a44

Browse files
committed
Rename max history size constant and expose it.
This will be useful in writing a migration to eliminate huge transaction histories.
1 parent e0b19a1 commit b0e8a44

File tree

4 files changed

+44
-19
lines changed

4 files changed

+44
-19
lines changed

packages/transaction-controller/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1212
- Add `DISPLAYED_TRANSACTION_HISTORY_PATHS` constant, representing the transaction history paths that may be used for display ([#4555](https://github.com/MetaMask/core/pull/4555))
1313
- This was exported so that it might be used to ensure display logic and internal history logic remains in-sync.
1414
- Any paths listed here will have their timestamps preserved. Unlisted paths may be compressed by the controller to minimize history size, losing the timestamp.
15+
- Add `MAX_TRANSACTION_HISTORY_LENGTH` constant, representing the expected maximum size of the `history` property for a given transaction ([#4555](https://github.com/MetaMask/core/pull/4555))
16+
- Note that this is not strictly enforced, the length may exceed this number of all entries are "displayed" entries, but we expect this to be extremely improbable in practice.
1517

1618
### Fixed
1719

packages/transaction-controller/src/index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,10 @@ export {
7070
WalletDevice,
7171
} from './types';
7272
export type { EtherscanTransactionMeta } from './utils/etherscan';
73-
export { DISPLAYED_TRANSACTION_HISTORY_PATHS } from './utils/history';
73+
export {
74+
DISPLAYED_TRANSACTION_HISTORY_PATHS,
75+
MAX_TRANSACTION_HISTORY_LENGTH,
76+
} from './utils/history';
7477
export { determineTransactionType } from './utils/transaction-type';
7578
export { mergeGasFeeEstimates } from './utils/gas-flow';
7679
export {

packages/transaction-controller/src/utils/history.test.ts

Lines changed: 36 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@ import {
88
type TransactionMeta,
99
type TransactionHistoryEntry,
1010
} from '../types';
11-
import { MAX_HISTORY_LENGTH, updateTransactionHistory } from './history';
11+
import {
12+
MAX_TRANSACTION_HISTORY_LENGTH,
13+
updateTransactionHistory,
14+
} from './history';
1215

1316
describe('History', () => {
1417
describe('updateTransactionHistory', () => {
@@ -80,7 +83,7 @@ describe('History', () => {
8083
partialTransaction: {
8184
history: generateMockHistory({
8285
originalTransaction,
83-
length: MAX_HISTORY_LENGTH,
86+
length: MAX_TRANSACTION_HISTORY_LENGTH,
8487
}),
8588
// This is the changed value
8689
txParams: { from: generateAddress(123) },
@@ -94,10 +97,12 @@ describe('History', () => {
9497
note: 'Mock non-displayed change',
9598
op: 'replace',
9699
path: '/txParams/from',
97-
value: generateAddress(MAX_HISTORY_LENGTH - 1),
100+
value: generateAddress(MAX_TRANSACTION_HISTORY_LENGTH - 1),
98101
},
99102
]);
100-
expect(mockTransaction.history).toHaveLength(MAX_HISTORY_LENGTH);
103+
expect(mockTransaction.history).toHaveLength(
104+
MAX_TRANSACTION_HISTORY_LENGTH,
105+
);
101106

102107
const updatedTransaction = updateTransactionHistory(
103108
mockTransaction,
@@ -132,21 +137,25 @@ describe('History', () => {
132137
],
133138
],
134139
});
135-
expect(updatedTransaction.history).toHaveLength(MAX_HISTORY_LENGTH);
140+
expect(updatedTransaction.history).toHaveLength(
141+
MAX_TRANSACTION_HISTORY_LENGTH,
142+
);
136143
});
137144
});
138145

139146
describe('when history is past max size with a single non-displayed entry at the end', () => {
140147
it('merges a non-displayed entry when adding a new entry after max history size is reached', () => {
141148
const originalTransaction = createMinimalMockTransaction();
142149
// This matches the last gas price change in the mock history
143-
const mockTransactionGasPrice = toHex(MAX_HISTORY_LENGTH - 1);
150+
const mockTransactionGasPrice = toHex(
151+
MAX_TRANSACTION_HISTORY_LENGTH - 1,
152+
);
144153
const mockTransaction = createMockTransaction({
145154
partialTransaction: {
146155
history: generateMockHistory({
147-
numberOfDisplayEntries: MAX_HISTORY_LENGTH - 1,
156+
numberOfDisplayEntries: MAX_TRANSACTION_HISTORY_LENGTH - 1,
148157
originalTransaction,
149-
length: MAX_HISTORY_LENGTH,
158+
length: MAX_TRANSACTION_HISTORY_LENGTH,
150159
}),
151160
txParams: {
152161
// This is the changed value
@@ -168,7 +177,9 @@ describe('History', () => {
168177
},
169178
]);
170179
const mockTransactionClone = cloneDeep(mockTransaction);
171-
expect(mockTransaction.history).toHaveLength(MAX_HISTORY_LENGTH);
180+
expect(mockTransaction.history).toHaveLength(
181+
MAX_TRANSACTION_HISTORY_LENGTH,
182+
);
172183

173184
const updatedTransaction = updateTransactionHistory(
174185
mockTransaction,
@@ -179,7 +190,10 @@ describe('History', () => {
179190
expect(updatedTransaction).toStrictEqual({
180191
...mockTransaction,
181192
history: [
182-
...mockTransactionClone.history.slice(0, MAX_HISTORY_LENGTH - 1),
193+
...mockTransactionClone.history.slice(
194+
0,
195+
MAX_TRANSACTION_HISTORY_LENGTH - 1,
196+
),
183197
// This is the new merged entry:
184198
[
185199
{
@@ -197,7 +211,9 @@ describe('History', () => {
197211
],
198212
],
199213
});
200-
expect(updatedTransaction.history).toHaveLength(MAX_HISTORY_LENGTH);
214+
expect(updatedTransaction.history).toHaveLength(
215+
MAX_TRANSACTION_HISTORY_LENGTH,
216+
);
201217
});
202218
});
203219

@@ -207,9 +223,9 @@ describe('History', () => {
207223
const mockTransaction = createMockTransaction({
208224
partialTransaction: {
209225
history: generateMockHistory({
210-
numberOfDisplayEntries: MAX_HISTORY_LENGTH - 1,
226+
numberOfDisplayEntries: MAX_TRANSACTION_HISTORY_LENGTH - 1,
211227
originalTransaction,
212-
length: MAX_HISTORY_LENGTH,
228+
length: MAX_TRANSACTION_HISTORY_LENGTH,
213229
}),
214230
txParams: {
215231
from: originalTransaction.txParams.from,
@@ -226,10 +242,12 @@ describe('History', () => {
226242
note: 'Mock displayed change',
227243
op: 'replace',
228244
path: '/txParams/gasPrice',
229-
value: toHex(MAX_HISTORY_LENGTH - 1),
245+
value: toHex(MAX_TRANSACTION_HISTORY_LENGTH - 1),
230246
},
231247
]);
232-
expect(mockTransaction.history).toHaveLength(MAX_HISTORY_LENGTH);
248+
expect(mockTransaction.history).toHaveLength(
249+
MAX_TRANSACTION_HISTORY_LENGTH,
250+
);
233251

234252
const updatedTransaction = updateTransactionHistory(
235253
mockTransaction,
@@ -253,7 +271,9 @@ describe('History', () => {
253271
],
254272
],
255273
});
256-
expect(updatedTransaction.history).toHaveLength(MAX_HISTORY_LENGTH + 1);
274+
expect(updatedTransaction.history).toHaveLength(
275+
MAX_TRANSACTION_HISTORY_LENGTH + 1,
276+
);
257277
});
258278
});
259279
});

packages/transaction-controller/src/utils/history.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import type {
1010
/**
1111
* The maximum allowed length of the `transaction.history` property.
1212
*/
13-
export const MAX_HISTORY_LENGTH = 100;
13+
export const MAX_TRANSACTION_HISTORY_LENGTH = 100;
1414

1515
/**
1616
* A list of trarnsaction history paths that may be used for display. These entries will not be
@@ -75,7 +75,7 @@ export function updateTransactionHistory(
7575
newHistoryEntry,
7676
] as TransactionHistory;
7777

78-
if (updatedHistory.length > MAX_HISTORY_LENGTH) {
78+
if (updatedHistory.length > MAX_TRANSACTION_HISTORY_LENGTH) {
7979
updatedHistory = compressTransactionHistory(updatedHistory);
8080
}
8181

0 commit comments

Comments
 (0)