Skip to content

Commit

Permalink
add 10s timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
sneurlax committed Feb 14, 2024
1 parent fc0d963 commit 7aa0b89
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
8 changes: 6 additions & 2 deletions lib/electrumx_rpc/electrumx_chain_height_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@ import 'package:stackwallet/utilities/enums/coin_enum.dart';

/// Store chain height subscriptions for each coin.
abstract class ElectrumxChainHeightService {
static Map<Coin, StreamSubscription<dynamic>?> subscriptions = {};
// Used to hold chain height subscriptions for each coin as in:
// ElectrumxChainHeightService.subscriptions[cryptoCurrency.coin] = sub;
static Map<Coin, StreamSubscription<dynamic>?> subscriptions = {};

static Map<Coin, Completer<int>?> completers = {};
// Used to hold chain height completers for each coin as in:
// ElectrumxChainHeightService.completers[cryptoCurrency.coin] = completer;
static Map<Coin, Completer<int>?> completers = {};

// Used to hold the time each coin started waiting for chain height as in:
// ElectrumxChainHeightService.timeStarted[cryptoCurrency.coin] = time;
static Map<Coin, DateTime?> timeStarted = {};
}
Original file line number Diff line number Diff line change
Expand Up @@ -825,6 +825,9 @@ mixin ElectrumXInterface<T extends Bip39HDCurrency> on Bip39HDWallet<T> {
}

Future<void> _manageChainHeightSubscription() async {
// Set the timeout period for the chain height subscription.
const timeout = Duration(seconds: 10);

if (ElectrumxChainHeightService.subscriptions[cryptoCurrency.coin] ==
null) {
// No subscription exists for this coin yet, so create one.
Expand All @@ -840,6 +843,11 @@ mixin ElectrumXInterface<T extends Bip39HDCurrency> on Bip39HDWallet<T> {
final subscription =
subscribableElectrumXClient.subscribeToBlockHeaders();

// Set the time the subscription was created.
final subscriptionCreationTime = DateTime.now();
ElectrumxChainHeightService.timeStarted[cryptoCurrency.coin] =
subscriptionCreationTime;

// Set stream subscription.
ElectrumxChainHeightService.subscriptions[cryptoCurrency.coin] =
subscription.responseStream.asBroadcastStream().listen((event) {
Expand Down Expand Up @@ -910,6 +918,25 @@ mixin ElectrumXInterface<T extends Bip39HDCurrency> on Bip39HDWallet<T> {
}
}

// Check if the subscription has been running for too long.
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.
if (timeRunning > timeout) {
// Clear this coin's subscription.
await ElectrumxChainHeightService.subscriptions[cryptoCurrency.coin]!
.cancel();
ElectrumxChainHeightService.subscriptions[cryptoCurrency.coin] = null;

// Clear this coin's completer.
ElectrumxChainHeightService.completers[cryptoCurrency.coin] = null;

// Retry/recurse.
return await _manageChainHeightSubscription();
}
}

// Wait for the first response.
_latestHeight = await ElectrumxChainHeightService
.completers[cryptoCurrency.coin]!.future;
Expand Down

0 comments on commit 7aa0b89

Please sign in to comment.