fix cancellation of multiple suggestion creation call
MODIFY
WalletWasabi.Fluent/ViewModels/Wallets/Send/PrivacySuggestionsFlyoutViewModel.cs
WalletWasabi.Fluent/ViewModels/Wallets/Send/PrivacySuggestionsFlyoutViewModel.cs
@@ -36,9 +36,6 @@ public partial class PrivacySuggestionsFlyoutViewModel : ViewModelBase
public async Task BuildPrivacySuggestionsAsync(Wallet wallet, TransactionInfo info, BitcoinAddress destination, BuildTransactionResult transaction, bool isFixedAmount, CancellationToken cancellationToken)
{
- using CancellationTokenSource timeoutCts = new(TimeSpan.FromSeconds(15));
- using CancellationTokenSource linkedCts = CancellationTokenSource.CreateLinkedTokenSource(timeoutCts.Token, cancellationToken);
-
Suggestions.Clear();
SelectedSuggestion = null;
@@ -57,7 +54,7 @@ public partial class PrivacySuggestionsFlyoutViewModel : ViewModelBase
if (hasChange && !isFixedAmount && !info.IsPayJoin)
{
var suggestions =
- ChangeAvoidanceSuggestionViewModel.GenerateSuggestionsAsync(info, destination, wallet, linkedCts.Token);
+ ChangeAvoidanceSuggestionViewModel.GenerateSuggestionsAsync(info, destination, wallet, cancellationToken);
await foreach (var suggestion in suggestions)
{

MODIFY
WalletWasabi.Fluent/ViewModels/Wallets/Send/TransactionPreviewViewModel.cs
WalletWasabi.Fluent/ViewModels/Wallets/Send/TransactionPreviewViewModel.cs
@@ -32,6 +32,7 @@ public partial class TransactionPreviewViewModel : RoutableViewModel
private readonly BitcoinAddress _destination;
private BuildTransactionResult? _transaction;
private CancellationTokenSource _cancellationTokenSource;
+ private CancellationTokenSource? _suggestionCancellationTokenSource;
[AutoNotify] private string _nextButtonText;
[AutoNotify] private bool _adjustFeeAvailable;
[AutoNotify] private TransactionSummaryViewModel? _displayedTransactionSummary;
@@ -80,7 +81,7 @@ public partial class TransactionPreviewViewModel : RoutableViewModel
_info.ChangelessCoins = ca.TransactionResult.SpentCoins;
UpdateTransaction(CurrentTransactionSummary, ca.TransactionResult);
- await PrivacySuggestions.BuildPrivacySuggestionsAsync(_wallet, _info, _destination, ca.TransactionResult, _isFixedAmount, _cancellationTokenSource.Token);
+ await BuildSuggestionsAsync();
}
else if (x is PocketSuggestionViewModel)
{
@@ -198,7 +199,7 @@ public partial class TransactionPreviewViewModel : RoutableViewModel
{
UpdateTransaction(CurrentTransactionSummary, newTransaction);
- await PrivacySuggestions.BuildPrivacySuggestionsAsync(_wallet, _info, _destination, newTransaction, _isFixedAmount, _cancellationTokenSource.Token);
+ await BuildSuggestionsAsync();
}
}
@@ -413,7 +414,7 @@ public partial class TransactionPreviewViewModel : RoutableViewModel
{
UpdateTransaction(CurrentTransactionSummary, initialTransaction);
- await PrivacySuggestions.BuildPrivacySuggestionsAsync(_wallet, _info, _destination, initialTransaction, _isFixedAmount, _cancellationTokenSource.Token);
+ await BuildSuggestionsAsync();
if (CurrentTransactionSummary.TransactionHasPockets && !await NavigateConfirmLabelsDialogAsync(initialTransaction))
{
@@ -448,6 +449,8 @@ public partial class TransactionPreviewViewModel : RoutableViewModel
_cancellationTokenSource.Cancel();
_cancellationTokenSource.Dispose();
+ _suggestionCancellationTokenSource?.Dispose();
+
_info.ChangelessCoins = Enumerable.Empty<SmartCoin>(); // Clear ChangelessCoins on cancel, so the user can undo the optimization.
}
@@ -542,4 +545,20 @@ public partial class TransactionPreviewViewModel : RoutableViewModel
return transaction;
}
+
+ private async Task BuildSuggestionsAsync()
+ {
+ if (_transaction is null)
+ {
+ return;
+ }
+
+ _suggestionCancellationTokenSource?.Cancel();
+ _suggestionCancellationTokenSource?.Dispose();
+
+ _suggestionCancellationTokenSource = new(TimeSpan.FromSeconds(15));
+ using CancellationTokenSource linkedCts = CancellationTokenSource.CreateLinkedTokenSource(_suggestionCancellationTokenSource.Token, _cancellationTokenSource.Token);
+
+ await PrivacySuggestions.BuildPrivacySuggestionsAsync(_wallet, _info, _destination, _transaction, _isFixedAmount, linkedCts.Token);
+ }
}

fix cancellation of multiple suggestion creation call