From 7363438279822fb6d608e5c8375c22a3fffd5ca9 Mon Sep 17 00:00:00 2001 From: likho Date: Wed, 14 Feb 2024 17:46:01 +0200 Subject: [PATCH] Refator _manageChainHeightSubscription so we are not calling the listener multiple times --- .../electrumx_interface.dart | 77 +++++++++++-------- 1 file changed, 43 insertions(+), 34 deletions(-) diff --git a/lib/wallets/wallet/wallet_mixin_interfaces/electrumx_interface.dart b/lib/wallets/wallet/wallet_mixin_interfaces/electrumx_interface.dart index 6a044ff15..dd6005560 100644 --- a/lib/wallets/wallet/wallet_mixin_interfaces/electrumx_interface.dart +++ b/lib/wallets/wallet/wallet_mixin_interfaces/electrumx_interface.dart @@ -807,43 +807,12 @@ mixin ElectrumXInterface on Bip39HDWallet { // Don't set a stream subscription if one already exists. if (ElectrumxChainHeightService.subscriptions[cryptoCurrency.coin] == null) { - final Completer completer = Completer(); - - // Make sure we only complete once. - final isFirstResponse = _latestHeight == null; - - // Subscribe to block headers. - final subscription = - subscribableElectrumXClient.subscribeToBlockHeaders(); - - // set stream subscription - ElectrumxChainHeightService.subscriptions[cryptoCurrency.coin] = - subscription.responseStream.asBroadcastStream().listen((event) { - final response = event; - if (response != null && - response is Map && - response.containsKey('height')) { - final int chainHeight = response['height'] as int; - // print("Current chain height: $chainHeight"); - - _latestHeight = chainHeight; - - if (isFirstResponse && !completer.isCompleted) { - // Return the chain height. - completer.complete(chainHeight); - } - } else { - Logging.instance.log( - "blockchain.headers.subscribe returned malformed response\n" - "Response: $response", - level: LogLevel.Error); - } - }); - - return _latestHeight ?? await completer.future; + return _manageChainHeightSubscription(); } // Don't set a stream subscription if one already exists. else { + //IF there's already a wallet for a coin the chain height might not be + // stored for current wallet // Check if the stream subscription is paused. if (ElectrumxChainHeightService .subscriptions[cryptoCurrency.coin]!.isPaused) { @@ -851,6 +820,10 @@ mixin ElectrumXInterface on Bip39HDWallet { ElectrumxChainHeightService.subscriptions[cryptoCurrency.coin]! .resume(); } + if (_latestHeight == null) { + //Get the chain height + return _manageChainHeightSubscription(); + } // Causes synchronization to stall. // // Check if the stream subscription is active by pinging it. @@ -883,6 +856,42 @@ mixin ElectrumXInterface on Bip39HDWallet { } } + Future _manageChainHeightSubscription() async { + final Completer completer = Completer(); + // Make sure we only complete once. + final isFirstResponse = _latestHeight == null; + + // Subscribe to block headers. + final subscription = + subscribableElectrumXClient.subscribeToBlockHeaders(); + + // set stream subscription + ElectrumxChainHeightService.subscriptions[cryptoCurrency.coin] = + subscription.responseStream.asBroadcastStream().listen((event) { + final response = event; + if (response != null && + response is Map && + response.containsKey('height')) { + final int chainHeight = response['height'] as int; + // print("Current chain height: $chainHeight"); + + _latestHeight = chainHeight; + + if (isFirstResponse && !completer.isCompleted) { + // Return the chain height. + completer.complete(chainHeight); + } + } else { + Logging.instance.log( + "blockchain.headers.subscribe returned malformed response\n" + "Response: $response", + level: LogLevel.Error); + } + }); + + return _latestHeight ?? await completer.future; + } + Future fetchTxCount({required String addressScriptHash}) async { final transactions = await electrumXClient.getHistory(scripthash: addressScriptHash);