Merge branch 'electrum_adapter' into testing

This commit is contained in:
sneurlax 2024-02-15 18:08:00 -06:00
commit 86bd8e2215
3 changed files with 47 additions and 4 deletions

View file

@ -22,22 +22,40 @@ import 'package:string_validator/string_validator.dart';
class CachedElectrumXClient { class CachedElectrumXClient {
final ElectrumXClient electrumXClient; final ElectrumXClient electrumXClient;
final ElectrumClient electrumAdapterClient; ElectrumClient electrumAdapterClient;
final Future<ElectrumClient> Function() electrumAdapterUpdateCallback;
static const minCacheConfirms = 30; static const minCacheConfirms = 30;
const CachedElectrumXClient({ 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<ElectrumClient> 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) {
Logging.instance.log(
"ElectrumAdapterClient is closed, reopening it...",
level: LogLevel.Info,
);
ElectrumClient? _electrumAdapterClient =
await electrumAdapterUpdateCallback.call();
electrumAdapterClient = _electrumAdapterClient;
}
}
Future<Map<String, dynamic>> getAnonymitySet({ Future<Map<String, dynamic>> getAnonymitySet({
required String groupId, required String groupId,
@ -62,6 +80,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 +157,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 +218,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 +263,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 +314,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

@ -996,6 +996,7 @@ mixin ElectrumXInterface<T extends Bip39HDCurrency> on Bip39HDWallet<T> {
electrumXCachedClient = CachedElectrumXClient.from( electrumXCachedClient = CachedElectrumXClient.from(
electrumXClient: electrumXClient, electrumXClient: electrumXClient,
electrumAdapterClient: electrumAdapterClient, electrumAdapterClient: electrumAdapterClient,
electrumAdapterUpdateCallback: updateClient,
); );
subscribableElectrumXClient = SubscribableElectrumXClient.from( subscribableElectrumXClient = SubscribableElectrumXClient.from(
node: newNode, node: newNode,
@ -1307,6 +1308,13 @@ mixin ElectrumXInterface<T extends Bip39HDCurrency> on Bip39HDWallet<T> {
await updateElectrumX(newNode: node); await updateElectrumX(newNode: node);
} }
Future<ElectrumClient> updateClient() async {
Logging.instance.log("Updating electrum node and ElectrumAdapterClient.",
level: LogLevel.Info);
await updateNode();
return electrumAdapterClient;
}
FeeObject? _cachedFees; FeeObject? _cachedFees;
@override @override