diff --git a/lib/wallets/wallet/wallet_mixin_interfaces/electrumx_interface.dart b/lib/wallets/wallet/wallet_mixin_interfaces/electrumx_interface.dart index 676cb87fe..cdc5dc9ae 100644 --- a/lib/wallets/wallet/wallet_mixin_interfaces/electrumx_interface.dart +++ b/lib/wallets/wallet/wallet_mixin_interfaces/electrumx_interface.dart @@ -848,71 +848,74 @@ mixin ElectrumXInterface on Bip39HDWallet { ElectrumxChainHeightService.timeStarted[cryptoCurrency.coin] = subscriptionCreationTime; - // 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"); + // Doublecheck to avoid race condition. + if (ElectrumxChainHeightService.subscriptions[cryptoCurrency.coin] == + null) { + // 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; + _latestHeight = chainHeight; - if (isFirstResponse) { - // If the completer is not completed, complete it. - if (!ElectrumxChainHeightService - .completers[cryptoCurrency.coin]!.isCompleted) { - // Complete the completer, returning the chain height. - ElectrumxChainHeightService.completers[cryptoCurrency.coin]! - .complete(chainHeight); + if (isFirstResponse) { + // If the completer is not completed, complete it. + if (!ElectrumxChainHeightService + .completers[cryptoCurrency.coin]!.isCompleted) { + // Complete the completer, returning the chain height. + ElectrumxChainHeightService.completers[cryptoCurrency.coin]! + .complete(chainHeight); + } } + } else { + Logging.instance.log( + "blockchain.headers.subscribe returned malformed response\n" + "Response: $response", + level: LogLevel.Error); } - } else { - Logging.instance.log( - "blockchain.headers.subscribe returned malformed response\n" - "Response: $response", - level: LogLevel.Error); - } - }); - } else { - // A subscription already exists. - // - // Resume the stream subscription if it's paused. - if (ElectrumxChainHeightService - .subscriptions[cryptoCurrency.coin]!.isPaused) { - // If it's paused, resume it. - ElectrumxChainHeightService.subscriptions[cryptoCurrency.coin]! - .resume(); + }); } + } - // Causes synchronization to stall. - // // Check if the stream subscription is active by pinging it. - // if (!(await subscribableElectrumXClient.ping())) { - // // If it's not active, reconnect it. - // final node = await getCurrentElectrumXNode(); - // - // await subscribableElectrumXClient.connect( - // host: node.address, port: node.port); - // - // // Wait for first response. - // return completer.future; - // } + // A subscription already exists. + // + // Resume the stream subscription if it's paused. + if (ElectrumxChainHeightService + .subscriptions[cryptoCurrency.coin]!.isPaused) { + // If it's paused, resume it. + ElectrumxChainHeightService.subscriptions[cryptoCurrency.coin]!.resume(); + } - // If there's no completer set for this coin, something's gone wrong. - // - // The completer is always set before the subscription, so this should - // never happen. - if (ElectrumxChainHeightService.completers[cryptoCurrency.coin] == null) { - // Clear this coin's subscription. - await ElectrumxChainHeightService.subscriptions[cryptoCurrency.coin]! - .cancel(); - ElectrumxChainHeightService.subscriptions[cryptoCurrency.coin] = null; + // Causes synchronization to stall. + // // Check if the stream subscription is active by pinging it. + // if (!(await subscribableElectrumXClient.ping())) { + // // If it's not active, reconnect it. + // final node = await getCurrentElectrumXNode(); + // + // await subscribableElectrumXClient.connect( + // host: node.address, port: node.port); + // + // // Wait for first response. + // return completer.future; + // } - // Retry/recurse. - return await _manageChainHeightSubscription(); - } + // If there's no completer set for this coin, something's gone wrong. + // + // The completer is always set before the subscription, so this should + // never happen. + if (ElectrumxChainHeightService.completers[cryptoCurrency.coin] == null) { + // Clear this coin's subscription. + await ElectrumxChainHeightService.subscriptions[cryptoCurrency.coin]! + .cancel(); + ElectrumxChainHeightService.subscriptions[cryptoCurrency.coin] = null; + + // Retry/recurse. + return await _manageChainHeightSubscription(); } // Check if the subscription has been running for too long.