mirror of
https://github.com/cypherstack/stack_wallet.git
synced 2025-01-10 20:54:33 +00:00
avoid race condition
This commit is contained in:
parent
98c095b568
commit
e979a352fb
1 changed files with 60 additions and 57 deletions
|
@ -848,71 +848,74 @@ mixin ElectrumXInterface<T extends Bip39HDCurrency> on Bip39HDWallet<T> {
|
|||
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.
|
||||
|
|
Loading…
Reference in a new issue