From c1ef98833a22f909838747a1f22f98d6299638fd Mon Sep 17 00:00:00 2001 From: julian Date: Sun, 15 Dec 2024 20:17:41 -0600 Subject: [PATCH] reduced pings --- lib/electrumx_rpc/electrumx_client.dart | 5 +-- lib/utilities/constants.dart | 2 +- lib/wallets/wallet/wallet.dart | 47 +++---------------------- 3 files changed, 9 insertions(+), 45 deletions(-) diff --git a/lib/electrumx_rpc/electrumx_client.dart b/lib/electrumx_rpc/electrumx_client.dart index 6737e7b4b..3f8241bca 100644 --- a/lib/electrumx_rpc/electrumx_client.dart +++ b/lib/electrumx_rpc/electrumx_client.dart @@ -399,6 +399,7 @@ class ElectrumXClient { rethrow; } } catch (e) { + Logging.instance.log("$host $e", level: LogLevel.Debug); if (currentFailoverIndex < _failovers.length - 1) { currentFailoverIndex++; return request( @@ -525,10 +526,10 @@ class ElectrumXClient { return await request( requestID: requestID, command: 'server.ping', - requestTimeout: const Duration(seconds: 3), + requestTimeout: const Duration(seconds: 30), retries: retryCount, ).timeout( - const Duration(seconds: 3), + const Duration(seconds: 30), onTimeout: () { Logging.instance.log( "ElectrumxClient.ping timed out with retryCount=$retryCount, host=$_host", diff --git a/lib/utilities/constants.dart b/lib/utilities/constants.dart index 6b64d4a73..163b7d04e 100644 --- a/lib/utilities/constants.dart +++ b/lib/utilities/constants.dart @@ -35,7 +35,7 @@ abstract class Constants { // // true; // true for development, static const int notificationsMax = 0xFFFFFFFF; - static const Duration networkAliveTimerDuration = Duration(seconds: 10); + static const Duration networkAliveTimerDuration = Duration(seconds: 30); // Enable Logger.print statements static const bool disableLogger = false; diff --git a/lib/wallets/wallet/wallet.dart b/lib/wallets/wallet/wallet.dart index 6e5a9c08f..5b8eba8a6 100644 --- a/lib/wallets/wallet/wallet.dart +++ b/lib/wallets/wallet/wallet.dart @@ -415,6 +415,11 @@ abstract class Wallet { } void _periodicPingCheck() async { + if (refreshMutex.isLocked) { + // should be active calls happening so no need to make extra work + return; + } + final bool hasNetwork = await pingCheck(); if (_isConnected != hasNetwork) { @@ -568,25 +573,6 @@ abstract class Wallet { } final start = DateTime.now(); - bool tAlive = true; - Timer? t; - if (this is! SparkInterface) { - t = Timer.periodic(const Duration(seconds: 1), (timer) async { - if (tAlive) { - final pingSuccess = await pingCheck(); - if (!pingSuccess) { - tAlive = false; - } - } else { - timer.cancel(); - } - }); - } - - void _checkAlive() { - if (!tAlive) throw Exception("refresh alive ping failure"); - } - final viewOnly = this is ViewOnlyOptionInterface && (this as ViewOnlyOptionInterface).isViewOnly; @@ -603,57 +589,45 @@ abstract class Wallet { ), ); - _checkAlive(); - // add some small buffer before making calls. // this can probably be removed in the future but was added as a // debugging feature await Future.delayed(const Duration(milliseconds: 300)); - _checkAlive(); // TODO: [prio=low] handle this differently. Extra modification of this file for coin specific functionality should be avoided. final Set codesToCheck = {}; - _checkAlive(); if (this is PaynymInterface && !viewOnly) { // isSegwit does not matter here at all final myCode = await (this as PaynymInterface).getPaymentCode(isSegwit: false); - _checkAlive(); final nym = await PaynymIsApi().nym(myCode.toString()); - _checkAlive(); if (nym.value != null) { for (final follower in nym.value!.followers) { codesToCheck.add(follower.code); } - _checkAlive(); for (final following in nym.value!.following) { codesToCheck.add(following.code); } } - _checkAlive(); } GlobalEventBus.instance.fire(RefreshPercentChangedEvent(0.0, walletId)); _checkAlive(); await updateChainHeight(); - _checkAlive(); if (this is BitcoinFrostWallet) { await (this as BitcoinFrostWallet).lookAhead(); } - _checkAlive(); GlobalEventBus.instance.fire(RefreshPercentChangedEvent(0.1, walletId)); - _checkAlive(); // TODO: [prio=low] handle this differently. Extra modification of this file for coin specific functionality should be avoided. if (this is MultiAddressInterface) { if (info.otherData[WalletInfoKeys.reuseAddress] != true) { await (this as MultiAddressInterface) .checkReceivingAddressForTransactions(); } - _checkAlive(); } GlobalEventBus.instance.fire(RefreshPercentChangedEvent(0.2, walletId)); @@ -672,49 +646,39 @@ abstract class Wallet { // this should be called before updateTransactions() await (this as SparkInterface).refreshSparkData(null); } - _checkAlive(); - GlobalEventBus.instance.fire(RefreshPercentChangedEvent(0.50, walletId)); - _checkAlive(); final fetchFuture = updateTransactions(); _checkAlive(); final utxosRefreshFuture = updateUTXOs(); // if (currentHeight != storedHeight) { GlobalEventBus.instance.fire(RefreshPercentChangedEvent(0.60, walletId)); - _checkAlive(); await utxosRefreshFuture; GlobalEventBus.instance.fire(RefreshPercentChangedEvent(0.70, walletId)); - _checkAlive(); await fetchFuture; // TODO: [prio=low] handle this differently. Extra modification of this file for coin specific functionality should be avoided. if (!viewOnly && this is PaynymInterface && codesToCheck.isNotEmpty) { - _checkAlive(); await (this as PaynymInterface) .checkForNotificationTransactionsTo(codesToCheck); // check utxos again for notification outputs - _checkAlive(); await updateUTXOs(); } _checkAlive(); GlobalEventBus.instance.fire(RefreshPercentChangedEvent(0.80, walletId)); // await getAllTxsToWatch(); - _checkAlive(); // TODO: [prio=low] handle this differently. Extra modification of this file for coin specific functionality should be avoided. if (this is LelantusInterface && !viewOnly) { if (info.otherData[WalletInfoKeys.enableLelantusScanning] as bool? ?? false) { await (this as LelantusInterface).refreshLelantusData(); - _checkAlive(); } } GlobalEventBus.instance.fire(RefreshPercentChangedEvent(0.90, walletId)); - _checkAlive(); await updateBalance(); _checkAlive(); @@ -728,7 +692,6 @@ abstract class Wallet { } catch (error, strace) { completer.completeError(error, strace); } finally { - t?.cancel(); refreshMutex.release(); if (!completer.isCompleted) { completer.completeError(