check electrumAdapterClient in CachedElectrumXClient, if closed, reopen

using a callback
This commit is contained in:
sneurlax 2024-02-15 15:43:47 -06:00
parent cbcac9bcce
commit a52f45a4ae
3 changed files with 33 additions and 2 deletions

View file

@ -23,21 +23,34 @@ import 'package:string_validator/string_validator.dart';
class CachedElectrumXClient {
final ElectrumXClient electrumXClient;
final ElectrumClient electrumAdapterClient;
final Future<void> Function() electrumAdapterUpdateCallback;
static const minCacheConfirms = 30;
const CachedElectrumXClient({
required this.electrumXClient,
required this.electrumAdapterClient,
required this.electrumAdapterUpdateCallback,
});
factory CachedElectrumXClient.from({
required ElectrumXClient electrumXClient,
required ElectrumClient electrumAdapterClient,
required Future<void> Function() electrumAdapterUpdateCallback,
}) =>
CachedElectrumXClient(
electrumXClient: electrumXClient,
electrumAdapterClient: electrumAdapterClient);
electrumAdapterClient: electrumAdapterClient,
electrumAdapterUpdateCallback: electrumAdapterUpdateCallback,
);
/// If the client is closed, use the callback to update it.
_checkElectrumAdapterClient() async {
if (electrumAdapterClient.peer.isClosed) {
await electrumAdapterUpdateCallback?.call();
// throw Exception("ElectrumAdapterClient is closed");
}
}
Future<Map<String, dynamic>> getAnonymitySet({
required String groupId,
@ -62,6 +75,8 @@ class CachedElectrumXClient {
set = Map<String, dynamic>.from(cachedSet);
}
await _checkElectrumAdapterClient();
final newSet = await (electrumAdapterClient as FiroElectrumClient)
.getLelantusAnonymitySet(
groupId: groupId,
@ -137,6 +152,8 @@ class CachedElectrumXClient {
set = Map<String, dynamic>.from(cachedSet);
}
await _checkElectrumAdapterClient();
final newSet = await (electrumAdapterClient as FiroElectrumClient)
.getSparkAnonymitySet(
coinGroupId: groupId,
@ -196,6 +213,8 @@ class CachedElectrumXClient {
final cachedTx = box.get(txHash) as Map?;
if (cachedTx == null) {
await _checkElectrumAdapterClient();
final Map<String, dynamic> result =
await electrumAdapterClient.getTransaction(txHash);
@ -239,6 +258,8 @@ class CachedElectrumXClient {
cachedSerials.length - 100, // 100 being some arbitrary buffer
);
await _checkElectrumAdapterClient();
final serials = await (electrumAdapterClient as FiroElectrumClient)
.getLelantusUsedCoinSerials(
startNumber: startNumber,
@ -288,6 +309,8 @@ class CachedElectrumXClient {
cachedTags.length - 100, // 100 being some arbitrary buffer
);
await _checkElectrumAdapterClient();
final tags =
await (electrumAdapterClient as FiroElectrumClient).getUsedCoinsTags(
startNumber: startNumber,

View file

@ -290,6 +290,13 @@ class ElectrumXClient {
// );
// }
// If the current ElectrumAdapterClient is closed, create a new one.
if (_electrumAdapterClient != null &&
_electrumAdapterClient!.peer.isClosed) {
_electrumAdapterChannel = null;
_electrumAdapterClient = null;
}
if (currentFailoverIndex == -1) {
_electrumAdapterChannel ??= await electrum_adapter.connect(
host,

View file

@ -983,6 +983,7 @@ mixin ElectrumXInterface<T extends Bip39HDCurrency> on Bip39HDWallet<T> {
electrumXCachedClient = CachedElectrumXClient.from(
electrumXClient: electrumXClient,
electrumAdapterClient: electrumAdapterClient,
electrumAdapterUpdateCallback: updateNode,
);
subscribableElectrumXClient = SubscribableElectrumXClient.from(
node: newNode,