Skip to content

Commit

Permalink
Merge pull request #1545 from input-output-hk/perf/cache-utxo
Browse files Browse the repository at this point in the history
perf(wallet): cache mapped addresses and utxo
  • Loading branch information
mkazlauskas authored Dec 10, 2024
2 parents 6f6e003 + e7bd2b3 commit a444ebe
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 9 deletions.
5 changes: 4 additions & 1 deletion packages/wallet/src/Wallets/BaseWallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ import {
map,
mergeMap,
of,
shareReplay,
switchMap,
take,
tap,
Expand Down Expand Up @@ -474,7 +475,9 @@ export class BaseWallet implements ObservableWallet {
);

const addresses$ = this.addresses$.pipe(
map((addresses) => addresses.map((groupedAddress) => groupedAddress.address))
map((addresses) => addresses.map((groupedAddress) => groupedAddress.address)),
distinctUntilChanged(deepEquals),
shareReplay(1)
);
this.#failedFromReemitter$ = new Subject<FailedTx>();
this.transactions = createTransactionsTracker({
Expand Down
13 changes: 9 additions & 4 deletions packages/wallet/src/services/UtxoTracker.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Cardano, UtxoProvider } from '@cardano-sdk/core';
import { Logger } from 'ts-log';
import { NEVER, Observable, combineLatest, concat, distinctUntilChanged, map, of, switchMap } from 'rxjs';
import { NEVER, Observable, combineLatest, concat, distinctUntilChanged, map, of, shareReplay, switchMap } from 'rxjs';
import { PersistentCollectionTrackerSubject, txInEquals, utxoEquals } from './util';
import { RetryBackoffConfig } from 'backoff-rxjs';
import { TxInFlight, UtxoTracker } from './types';
Expand Down Expand Up @@ -121,7 +121,9 @@ export const createUtxoTracker = (
logger.debug('Found duplicate UTxO in', utxo);
}
return uniqueUtxo;
})
}),
distinctUntilChanged((previous, current) => utxoEquals(previous, current)),
shareReplay(1)
);
const available$ = combineLatest([total$, unspendableUtxoSource$]).pipe(
// filter to utxo that are not included in in-flight transactions or unspendable
Expand All @@ -131,7 +133,9 @@ export const createUtxoTracker = (
txInIsUnspendable && logger.debug('Exclude unspendable UTXO from availble$', utxoTxIn);
return !txInIsUnspendable;
})
)
),
distinctUntilChanged((previous, current) => utxoEquals(previous, current)),
shareReplay(1)
);

return {
Expand All @@ -150,7 +154,8 @@ export const createUtxoTracker = (
map(([unspendableUtxo, utxo]) =>
unspendableUtxo.filter(([unspendable]) => utxo.some(([utxoTxIn]) => txInEquals(utxoTxIn, unspendable)))
),
distinctUntilChanged((previous, current) => utxoEquals(previous, current))
distinctUntilChanged((previous, current) => utxoEquals(previous, current)),
shareReplay(1)
)
};
};
6 changes: 2 additions & 4 deletions packages/wallet/test/services/UtxoTracker.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,10 +267,8 @@ describe('createUtxoTracker', () => {
);
expectObservable(utxoTracker.total$).toBe('-a--b---|', { a: utxo, b: utxo2 });
expectObservable(utxoTracker.unspendable$).toBe('-a--b---|', { a: [utxo[0]], b: [] });
expectObservable(utxoTracker.available$).toBe('-a--b---|', {
a: utxo2,
b: utxo2
});
// utxo2 = utxo-unspendable
expectObservable(utxoTracker.available$).toBe('-a------|', { a: utxo2 });
});
});
});

0 comments on commit a444ebe

Please sign in to comment.