add fusionRoundsCompleted and fusionRoundsFailed to fusion uiState

and update fusiondart ref for minor null assertion->optional
This commit is contained in:
sneurlax 2023-10-19 12:06:26 -05:00
parent 993914636b
commit e88676cf40
3 changed files with 74 additions and 8 deletions

@ -1 +1 @@
Subproject commit b93220d2f133ac076e7ebe7f433bd6f248721e16
Subproject commit cad52f0ee123c30cd881482817b4e078f1472d8b

View file

@ -2,7 +2,14 @@ import 'package:flutter/cupertino.dart';
import 'package:stackwallet/pages_desktop_specific/cashfusion/sub_widgets/fusion_dialog.dart';
class FusionProgressUIState extends ChangeNotifier {
bool _ableToConnect = true; // set to true for now
/// Whether we are able to connect to the server.
bool _ableToConnect = false;
// _ableToConnect setter.
set ableToConnect(bool ableToConnect) {
_ableToConnect = ableToConnect;
notifyListeners();
}
bool get done {
if (!_ableToConnect) {
@ -89,4 +96,33 @@ class FusionProgressUIState extends ChangeNotifier {
_fusionStatus = fusionStatus;
notifyListeners();
}
/// An int storing the number of successfully completed fusion rounds.
int _fusionRoundsCompleted = 0;
int get fusionRoundsCompleted => _fusionRoundsCompleted;
set fusionRoundsCompleted(int fusionRoundsCompleted) {
_fusionRoundsCompleted = fusionRoundsCompleted;
notifyListeners();
}
/// A helper for incrementing the number of successfully completed fusion rounds.
void incrementFusionRoundsCompleted() {
_fusionRoundsCompleted++;
_fusionRoundsFailed = 0; // Reset failed round count on success.
notifyListeners();
}
/// An int storing the number of failed fusion rounds.
int _fusionRoundsFailed = 0;
int get fusionRoundsFailed => _fusionRoundsFailed;
set fusionRoundsFailed(int fusionRoundsFailed) {
_fusionRoundsFailed = fusionRoundsFailed;
notifyListeners();
}
/// A helper for incrementing the number of failed fusion rounds.
void incrementFusionRoundsFailed() {
_fusionRoundsFailed++;
notifyListeners();
}
}

View file

@ -128,7 +128,15 @@ mixin FusionWalletInterface {
// Fusion object.
fusion.Fusion? _mainFusionObject;
bool _stopRequested = false;
int _currentFuseCount = 0;
/// An int storing the number of successfully completed fusion rounds.
int _completedFuseCount = 0;
/// An int storing the number of failed fusion rounds.
int _failedFuseCount = 0;
/// The maximum number of consecutive failed fusion rounds before stopping.
int get maxFailedFuseCount => 5;
/// Initializes the FusionWalletInterface mixin.
///
@ -457,8 +465,11 @@ mixin FusionWalletInterface {
},
);
// reset count and flag
_currentFuseCount = 0;
// Reset internal and UI counts and flag.
_completedFuseCount = 0;
_uiState?.fusionRoundsCompleted = 0;
_failedFuseCount = 0;
_uiState?.fusionRoundsFailed = 0;
_stopRequested = false;
bool shouldFuzeAgain() {
@ -468,13 +479,11 @@ mixin FusionWalletInterface {
} else {
// not continuous
// check to make sure we aren't doing more fusions than requested
return !_stopRequested && _currentFuseCount < fusionInfo.rounds;
return !_stopRequested && _completedFuseCount < fusionInfo.rounds;
}
}
while (shouldFuzeAgain()) {
_currentFuseCount++;
// refresh wallet utxos
await _updateWalletUTXOS();
@ -553,6 +562,27 @@ mixin FusionWalletInterface {
level: LogLevel.Error,
);
// just continue on attempt failure
// Increment the number of failed fusion rounds.
_failedFuseCount++;
// Do the same for the UI state.
_uiState?.incrementFusionRoundsFailed();
// If we fail 5 times in a row, stop trying.
if (_failedFuseCount >= maxFailedFuseCount) {
_stopRequested = true;
}
} finally {
// Increment the number of successfully completed fusion rounds.
_completedFuseCount++;
// Do the same for the UI state. This also resets the failed count (for
// the UI state only).
_uiState?.incrementFusionRoundsCompleted();
// Also reset the failed count here.
_failedFuseCount = 0;
}
}
}