From 0d8f1c2b95ef72e6c8e5d7773456b615b9c7f9ce Mon Sep 17 00:00:00 2001 From: sneurlax Date: Mon, 5 Feb 2024 12:09:45 -0600 Subject: [PATCH] add chain height service in order to hold one subscription per coin --- lib/electrumx_rpc/electrumx_chain_height_service.dart | 10 ++++++++++ .../wallet_mixin_interfaces/electrumx_interface.dart | 7 ++++--- 2 files changed, 14 insertions(+), 3 deletions(-) create mode 100644 lib/electrumx_rpc/electrumx_chain_height_service.dart diff --git a/lib/electrumx_rpc/electrumx_chain_height_service.dart b/lib/electrumx_rpc/electrumx_chain_height_service.dart new file mode 100644 index 000000000..126a0df72 --- /dev/null +++ b/lib/electrumx_rpc/electrumx_chain_height_service.dart @@ -0,0 +1,10 @@ +import 'dart:async'; + +import 'package:stackwallet/utilities/enums/coin_enum.dart'; + +/// Store chain height subscriptions for each coin. +abstract class ElectrumxChainHeightService { + static Map?> subscriptions = {}; + // Used to hold chain height subscriptions for each coin as in: + // ElectrumxChainHeightService.subscriptions[cryptoCurrency.coin] = sub; +} diff --git a/lib/wallets/wallet/wallet_mixin_interfaces/electrumx_interface.dart b/lib/wallets/wallet/wallet_mixin_interfaces/electrumx_interface.dart index 71c822702..eb1db613b 100644 --- a/lib/wallets/wallet/wallet_mixin_interfaces/electrumx_interface.dart +++ b/lib/wallets/wallet/wallet_mixin_interfaces/electrumx_interface.dart @@ -6,6 +6,7 @@ import 'package:bitcoindart/bitcoindart.dart' as bitcoindart; import 'package:coinlib_flutter/coinlib_flutter.dart' as coinlib; import 'package:isar/isar.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'; import 'package:stackwallet/electrumx_rpc/subscribable_electrumx_client.dart'; import 'package:stackwallet/models/isar/models/blockchain_data/v2/input_v2.dart'; @@ -36,7 +37,6 @@ mixin ElectrumXInterface on Bip39HDWallet { int? get maximumFeerate => null; int? _latestHeight; - StreamSubscription? _heightSubscription; static const _kServerBatchCutoffVersion = [1, 6]; List? _serverVersion; @@ -807,7 +807,8 @@ mixin ElectrumXInterface on Bip39HDWallet { try { // Don't set a stream subscription if one already exists. - if (_heightSubscription != null) { + if (ElectrumxChainHeightService.subscriptions[cryptoCurrency.coin] != + null) { if (_latestHeight != null) { return _latestHeight!; } else { @@ -824,7 +825,7 @@ mixin ElectrumXInterface on Bip39HDWallet { subscribableElectrumXClient.subscribeToBlockHeaders(); // set stream subscription - _heightSubscription = + ElectrumxChainHeightService.subscriptions[cryptoCurrency.coin] = subscription.responseStream.asBroadcastStream().listen((event) { final response = event; if (response != null &&