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 { class CachedElectrumXClient {
final ElectrumXClient electrumXClient; final ElectrumXClient electrumXClient;
final ElectrumClient electrumAdapterClient; final ElectrumClient electrumAdapterClient;
final Future<void> Function() electrumAdapterUpdateCallback;
static const minCacheConfirms = 30; static const minCacheConfirms = 30;
const CachedElectrumXClient({ const CachedElectrumXClient({
required this.electrumXClient, required this.electrumXClient,
required this.electrumAdapterClient, required this.electrumAdapterClient,
required this.electrumAdapterUpdateCallback,
}); });
factory CachedElectrumXClient.from({ factory CachedElectrumXClient.from({
required ElectrumXClient electrumXClient, required ElectrumXClient electrumXClient,
required ElectrumClient electrumAdapterClient, required ElectrumClient electrumAdapterClient,
required Future<void> Function() electrumAdapterUpdateCallback,
}) => }) =>
CachedElectrumXClient( CachedElectrumXClient(
electrumXClient: electrumXClient, 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({ Future<Map<String, dynamic>> getAnonymitySet({
required String groupId, required String groupId,
@ -62,6 +75,8 @@ class CachedElectrumXClient {
set = Map<String, dynamic>.from(cachedSet); set = Map<String, dynamic>.from(cachedSet);
} }
await _checkElectrumAdapterClient();
final newSet = await (electrumAdapterClient as FiroElectrumClient) final newSet = await (electrumAdapterClient as FiroElectrumClient)
.getLelantusAnonymitySet( .getLelantusAnonymitySet(
groupId: groupId, groupId: groupId,
@ -137,6 +152,8 @@ class CachedElectrumXClient {
set = Map<String, dynamic>.from(cachedSet); set = Map<String, dynamic>.from(cachedSet);
} }
await _checkElectrumAdapterClient();
final newSet = await (electrumAdapterClient as FiroElectrumClient) final newSet = await (electrumAdapterClient as FiroElectrumClient)
.getSparkAnonymitySet( .getSparkAnonymitySet(
coinGroupId: groupId, coinGroupId: groupId,
@ -196,6 +213,8 @@ class CachedElectrumXClient {
final cachedTx = box.get(txHash) as Map?; final cachedTx = box.get(txHash) as Map?;
if (cachedTx == null) { if (cachedTx == null) {
await _checkElectrumAdapterClient();
final Map<String, dynamic> result = final Map<String, dynamic> result =
await electrumAdapterClient.getTransaction(txHash); await electrumAdapterClient.getTransaction(txHash);
@ -239,6 +258,8 @@ class CachedElectrumXClient {
cachedSerials.length - 100, // 100 being some arbitrary buffer cachedSerials.length - 100, // 100 being some arbitrary buffer
); );
await _checkElectrumAdapterClient();
final serials = await (electrumAdapterClient as FiroElectrumClient) final serials = await (electrumAdapterClient as FiroElectrumClient)
.getLelantusUsedCoinSerials( .getLelantusUsedCoinSerials(
startNumber: startNumber, startNumber: startNumber,
@ -288,6 +309,8 @@ class CachedElectrumXClient {
cachedTags.length - 100, // 100 being some arbitrary buffer cachedTags.length - 100, // 100 being some arbitrary buffer
); );
await _checkElectrumAdapterClient();
final tags = final tags =
await (electrumAdapterClient as FiroElectrumClient).getUsedCoinsTags( await (electrumAdapterClient as FiroElectrumClient).getUsedCoinsTags(
startNumber: startNumber, 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) { if (currentFailoverIndex == -1) {
_electrumAdapterChannel ??= await electrum_adapter.connect( _electrumAdapterChannel ??= await electrum_adapter.connect(
host, host,

View file

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