@@ -54,14 +54,12 @@ const generateRandomLetters = (length: number) => {
54
54
return result ;
55
55
} ;
56
56
57
-
58
57
const updateTransactionIds = ( transactions : Cardano . HydratedTx [ ] ) =>
59
58
transactions . map ( ( tx ) => ( {
60
59
...tx ,
61
60
id : Cardano . TransactionId ( `${ generateRandomLetters ( 64 ) } ` )
62
61
} ) ) ;
63
62
64
-
65
63
describe ( 'TransactionsTracker' , ( ) => {
66
64
const logger = dummyLogger ;
67
65
const historicalTransactionsFetchLimit = 3 ;
@@ -97,7 +95,8 @@ describe('TransactionsTracker', () => {
97
95
} ) ;
98
96
99
97
it ( 'emits empty array if store is empty and ChainHistoryProvider does not return any transactions' , async ( ) => {
100
- chainHistoryProvider . transactionsByAddresses = jest . fn ( )
98
+ chainHistoryProvider . transactionsByAddresses = jest
99
+ . fn ( )
101
100
. mockImplementation ( ( ) => delay ( 50 ) . then ( ( ) => ( { pageResults : [ ] , totalResultCount : 0 } ) ) ) ;
102
101
const provider$ = createAddressTransactionsProvider ( {
103
102
addresses$ : of ( addresses ) ,
@@ -112,27 +111,41 @@ describe('TransactionsTracker', () => {
112
111
expect ( store . setAll ) . toBeCalledTimes ( 0 ) ;
113
112
} ) ;
114
113
115
- it ( 'if store is empty, stores and emits transactions resolved by ChainHistoryProvider' , async ( ) => {
114
+ it ( 'if store is empty, stores and emits last {historicalTransactionsFetchLimit} transactions resolved by ChainHistoryProvider' , async ( ) => {
115
+ const lowerHistoricalTransactionsFetchLimit = 2 ;
116
+
117
+ chainHistoryProvider . transactionsByAddresses = jest . fn ( ) . mockImplementation ( ( ) =>
118
+ delay ( 50 ) . then ( ( ) => ( {
119
+ ...queryTransactionsResult2 ,
120
+ pageResults : [ ...queryTransactionsResult2 . pageResults ]
121
+ } ) )
122
+ ) ;
116
123
const provider$ = createAddressTransactionsProvider ( {
117
124
addresses$ : of ( addresses ) ,
118
125
chainHistoryProvider,
119
- historicalTransactionsFetchLimit,
126
+ historicalTransactionsFetchLimit : lowerHistoricalTransactionsFetchLimit ,
120
127
logger,
121
128
retryBackoffConfig,
122
129
store,
123
130
tipBlockHeight$
124
131
} ) . transactionsSource$ ;
125
- expect ( await firstValueFrom ( provider$ ) ) . toEqual ( queryTransactionsResult . pageResults ) ;
132
+ const lastHistoricalTransactionsFetchLimitTransactions = queryTransactionsResult2 . pageResults . slice (
133
+ - 1 * lowerHistoricalTransactionsFetchLimit
134
+ ) ;
135
+ expect ( await firstValueFrom ( provider$ ) ) . toEqual ( lastHistoricalTransactionsFetchLimitTransactions ) ;
126
136
expect ( store . setAll ) . toBeCalledTimes ( 1 ) ;
127
- expect ( store . setAll ) . toBeCalledWith ( queryTransactionsResult . pageResults ) ;
137
+ expect ( store . setAll ) . toBeCalledWith ( lastHistoricalTransactionsFetchLimitTransactions ) ;
128
138
} ) ;
129
139
130
140
it ( 'emits configured number of latest historical transactions' , async ( ) => {
131
141
const totalTxsCount = PAGE_SIZE + 5 ;
132
142
133
143
const allTransactions = generateTxAlonzo ( totalTxsCount ) ;
134
- chainHistoryProvider . transactionsByAddresses = jest . fn ( ) . mockImplementation (
135
- ( args : TransactionsByAddressesArgs ) => filterAndPaginateTransactions ( allTransactions , args ) ) ;
144
+ chainHistoryProvider . transactionsByAddresses = jest
145
+ . fn ( )
146
+ . mockImplementation ( ( args : TransactionsByAddressesArgs ) =>
147
+ filterAndPaginateTransactions ( allTransactions , args )
148
+ ) ;
136
149
137
150
const provider$ = createAddressTransactionsProvider ( {
138
151
addresses$ : of ( addresses ) ,
@@ -183,7 +196,8 @@ describe('TransactionsTracker', () => {
183
196
await firstValueFrom ( store . setAll ( [ txId1 , txId2 ] ) ) ;
184
197
185
198
// ChainHistory is shorter by 1 tx: [1]
186
- chainHistoryProvider . transactionsByAddresses = jest . fn ( )
199
+ chainHistoryProvider . transactionsByAddresses = jest
200
+ . fn ( )
187
201
// the mismatch will pop the single transaction found in the stored transactions
188
202
. mockImplementationOnce ( ( ) => delay ( 50 ) . then ( ( ) => ( { pageResults : [ ] , totalResultCount : 0 } ) ) )
189
203
// intersection is found, chain is shortened
@@ -221,7 +235,8 @@ describe('TransactionsTracker', () => {
221
235
await firstValueFrom ( store . setAll ( [ txId1 , txId2 ] ) ) ;
222
236
223
237
// ChainHistory has one common and one different: [1, 3]
224
- chainHistoryProvider . transactionsByAddresses = jest . fn ( )
238
+ chainHistoryProvider . transactionsByAddresses = jest
239
+ . fn ( )
225
240
// the mismatch will pop the single transaction found in the stored transactions
226
241
. mockImplementationOnce ( ( ) => delay ( 50 ) . then ( ( ) => ( { pageResults : [ txId3 ] , totalResultCount : 1 } ) ) )
227
242
// intersection is found, and stored history is populated with the new transaction
@@ -263,7 +278,8 @@ describe('TransactionsTracker', () => {
263
278
264
279
it ( 'queries ChainHistoryProvider again with blockRange lower bound from a previous transaction on rollback' , async ( ) => {
265
280
await firstValueFrom ( store . setAll ( queryTransactionsResult . pageResults ) ) ;
266
- chainHistoryProvider . transactionsByAddresses = jest . fn ( )
281
+ chainHistoryProvider . transactionsByAddresses = jest
282
+ . fn ( )
267
283
. mockImplementationOnce ( ( ) => delay ( 50 ) . then ( ( ) => ( { pageResults : [ ] , totalResultCount : 0 } ) ) )
268
284
. mockImplementationOnce ( ( ) => delay ( 50 ) . then ( ( ) => ( { pageResults : [ ] , totalResultCount : 0 } ) ) )
269
285
. mockImplementationOnce ( ( ) =>
@@ -396,7 +412,10 @@ describe('TransactionsTracker', () => {
396
412
397
413
it ( 'ignores duplicate transactions' , async ( ) => {
398
414
// eslint-disable-next-line max-len
399
- const [ txId1 , txId2 , txId3 ] = updateTransactionsBlockNo ( queryTransactionsResult2 . pageResults , Cardano . BlockNo ( 10_050 ) ) ;
415
+ const [ txId1 , txId2 , txId3 ] = updateTransactionsBlockNo (
416
+ queryTransactionsResult2 . pageResults ,
417
+ Cardano . BlockNo ( 10_050 )
418
+ ) ;
400
419
401
420
txId1 . blockHeader . slot = Cardano . Slot ( 10_050 ) ;
402
421
txId2 . blockHeader . slot = Cardano . Slot ( 10_051 ) ;
@@ -428,7 +447,7 @@ describe('TransactionsTracker', () => {
428
447
429
448
expect ( await firstValueFrom ( provider$ . pipe ( bufferCount ( 2 ) ) ) ) . toEqual ( [
430
449
[ txId1 , txId1 , txId2 ] , // from store
431
- [ txId1 , txId2 , txId3 ] // chain history (fixes stored duplicates)
450
+ [ txId1 , txId2 , txId3 ] // chain history (fixes stored duplicates)
432
451
] ) ;
433
452
expect ( rollbacks . length ) . toBe ( 0 ) ;
434
453
expect ( store . setAll ) . toBeCalledTimes ( 2 ) ;
@@ -500,10 +519,19 @@ describe('TransactionsTracker', () => {
500
519
501
520
await firstValueFrom ( store . setAll ( [ txId1 , txId2 , txId3 ] ) ) ;
502
521
503
- chainHistoryProvider . transactionsByAddresses = jest . fn ( ) . mockImplementation ( ( ) => ( {
504
- pageResults : [ txId3OtherBlock , txId1OtherBlock , txId2OtherBlock ] ,
505
- totalResultCount : 3
506
- } ) ) ;
522
+ chainHistoryProvider . transactionsByAddresses = jest
523
+ . fn ( )
524
+ . mockImplementationOnce ( ( ) => ( {
525
+ // asc
526
+ pageResults : [ txId3OtherBlock , txId1OtherBlock , txId2OtherBlock ] ,
527
+ totalResultCount : 3
528
+ } ) )
529
+ // detects a rollback and reverts all local transactions (all in the same block)
530
+ // fetches from scratch - provider is called with 'desc' order
531
+ . mockImplementationOnce ( ( ) => ( {
532
+ pageResults : [ txId2OtherBlock , txId1OtherBlock , txId3OtherBlock ] ,
533
+ totalResultCount : 3
534
+ } ) ) ;
507
535
508
536
const { transactionsSource$ : provider$ , rollback$ } = createAddressTransactionsProvider ( {
509
537
addresses$ : of ( addresses ) ,
@@ -544,11 +572,11 @@ describe('TransactionsTracker', () => {
544
572
545
573
await firstValueFrom ( store . setAll ( [ txId1 , txId2 ] ) ) ;
546
574
547
- chainHistoryProvider . transactionsByAddresses = jest . fn ( ) . mockImplementation (
548
- ( args : TransactionsByAddressesArgs ) => filterAndPaginateTransactions (
549
- [ txId1OtherBlock , txId2OtherBlock , txId3OtherBlock ]
550
- , args )
551
- ) ;
575
+ chainHistoryProvider . transactionsByAddresses = jest
576
+ . fn ( )
577
+ . mockImplementation ( ( args : TransactionsByAddressesArgs ) =>
578
+ filterAndPaginateTransactions ( [ txId1OtherBlock , txId2OtherBlock , txId3OtherBlock ] , args )
579
+ ) ;
552
580
553
581
const { transactionsSource$ : provider$ , rollback$ } = createAddressTransactionsProvider ( {
554
582
addresses$ : of ( addresses ) ,
0 commit comments