Merge branch 'manage-zero-chain-height' into testing

This commit is contained in:
sneurlax 2024-02-14 15:23:47 -06:00
commit cfcfde7e04

View file

@ -5,6 +5,7 @@ import 'package:bip47/src/util.dart';
import 'package:bitcoindart/bitcoindart.dart' as bitcoindart;
import 'package:coinlib_flutter/coinlib_flutter.dart' as coinlib;
import 'package:isar/isar.dart';
import 'package:mutex/mutex.dart';
import 'package:stackwallet/electrumx_rpc/cached_electrumx_client.dart';
import 'package:stackwallet/electrumx_rpc/electrumx_chain_height_service.dart';
import 'package:stackwallet/electrumx_rpc/electrumx_client.dart';
@ -824,7 +825,11 @@ mixin ElectrumXInterface<T extends Bip39HDCurrency> on Bip39HDWallet<T> {
}
}
// Mutex to control subscription management access.
static final Mutex _subMutex = Mutex();
Future<void> _manageChainHeightSubscription() async {
await _subMutex.protect(() async {
// Set the timeout period for the chain height subscription.
const timeout = Duration(seconds: 10);
@ -834,7 +839,8 @@ mixin ElectrumXInterface<T extends Bip39HDCurrency> on Bip39HDWallet<T> {
//
// Set up to wait for the first response.
final Completer<int> completer = Completer<int>();
ElectrumxChainHeightService.completers[cryptoCurrency.coin] ??= completer;
ElectrumxChainHeightService.completers[cryptoCurrency.coin] ??=
completer;
// Make sure we only complete once.
final isFirstResponse = _latestHeight == null;
@ -848,9 +854,6 @@ mixin ElectrumXInterface<T extends Bip39HDCurrency> on Bip39HDWallet<T> {
ElectrumxChainHeightService.timeStarted[cryptoCurrency.coin] =
subscriptionCreationTime;
// Doublecheck to avoid race condition.
if (ElectrumxChainHeightService.subscriptions[cryptoCurrency.coin] ==
null) {
// Set stream subscription.
ElectrumxChainHeightService.subscriptions[cryptoCurrency.coin] =
subscription.responseStream.asBroadcastStream().listen((event) {
@ -880,7 +883,6 @@ mixin ElectrumXInterface<T extends Bip39HDCurrency> on Bip39HDWallet<T> {
}
});
}
}
// A subscription already exists.
//
@ -888,7 +890,8 @@ mixin ElectrumXInterface<T extends Bip39HDCurrency> on Bip39HDWallet<T> {
if (ElectrumxChainHeightService
.subscriptions[cryptoCurrency.coin]!.isPaused) {
// If it's paused, resume it.
ElectrumxChainHeightService.subscriptions[cryptoCurrency.coin]!.resume();
ElectrumxChainHeightService.subscriptions[cryptoCurrency.coin]!
.resume();
}
// Causes synchronization to stall.
@ -919,7 +922,8 @@ mixin ElectrumXInterface<T extends Bip39HDCurrency> on Bip39HDWallet<T> {
}
// Check if the subscription has been running for too long.
if (ElectrumxChainHeightService.timeStarted[cryptoCurrency.coin] != null) {
if (ElectrumxChainHeightService.timeStarted[cryptoCurrency.coin] !=
null) {
final timeRunning = DateTime.now().difference(
ElectrumxChainHeightService.timeStarted[cryptoCurrency.coin]!);
// Cancel and retry if we've been waiting too long.
@ -951,6 +955,7 @@ mixin ElectrumXInterface<T extends Bip39HDCurrency> on Bip39HDWallet<T> {
.completers[cryptoCurrency.coin]!.future;
return;
});
}
Future<int> fetchTxCount({required String addressScriptHash}) async {