Skip to content

Commit

Permalink
WIP - Minimal refactor with SendParameters
Browse files Browse the repository at this point in the history
  • Loading branch information
ichthus1604 committed Apr 18, 2024
1 parent a7947ec commit 438845d
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 11 deletions.
18 changes: 11 additions & 7 deletions WalletWasabi.Fluent/Models/Transactions/PrivacySuggestionsModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@

namespace WalletWasabi.Fluent.Models.Transactions;

public class PrivacySuggestionsModel
[AutoInterface]
public partial class PrivacySuggestionsModel
{
private const decimal MaximumDifferenceTolerance = 0.25m;
private const int ConsolidationTolerance = 10;
Expand All @@ -31,15 +32,16 @@ public class PrivacySuggestionsModel
/// <summary>Allow at most one suggestion generation run.</summary>
private readonly AsyncLock _asyncLock = new();

private readonly Wallet _wallet;
private readonly CoinJoinManager _cjManager;

private readonly SendParameters _sendParameters;
private readonly Wallet _wallet;
private CancellationTokenSource? _singleRunCancellationTokenSource;
private CancellationTokenSource? _linkedCancellationTokenSource;

public PrivacySuggestionsModel(Wallet wallet)
public PrivacySuggestionsModel(SendParameters sendParameters)
{
_wallet = wallet;
_sendParameters = sendParameters;
_wallet = sendParameters.Wallet;
_cjManager = Services.HostedServices.Get<CoinJoinManager>();
}

Expand Down Expand Up @@ -155,15 +157,17 @@ private IEnumerable<PrivacyItem> VerifyPrivacyLevel(Parameters parameters)
yield break;
}

var availableCoins = _sendParameters.AvailableCoins;

ImmutableList<SmartCoin> coinsToExclude = _cjManager.CoinsInCriticalPhase[_wallet.WalletId];
bool wasCoinjoiningCoinUsed = parameters.Transaction.SpentCoins.Any(coinsToExclude.Contains);

// Only exclude coins if the original transaction doesn't use them either.
var allPrivateCoin = _wallet.Coins.Where(x => x.GetPrivacyLevel(_wallet.AnonScoreTarget) == PrivacyLevel.Private).ToArray();
var allPrivateCoin = availableCoins.Where(x => x.GetPrivacyLevel(_wallet.AnonScoreTarget) == PrivacyLevel.Private).ToArray();

allPrivateCoin = wasCoinjoiningCoinUsed ? allPrivateCoin : allPrivateCoin.Except(coinsToExclude).ToArray();

var onlyKnownByTheRecipientCoins = _wallet.Coins.Where(x => parameters.TransactionInfo.Recipient.Equals(x.GetLabels(_wallet.AnonScoreTarget), StringComparer.OrdinalIgnoreCase)).ToArray();
var onlyKnownByTheRecipientCoins = availableCoins.Where(x => parameters.TransactionInfo.Recipient.Equals(x.GetLabels(_wallet.AnonScoreTarget), StringComparer.OrdinalIgnoreCase)).ToArray();
var allSemiPrivateCoin =
_wallet.Coins.Where(x => x.GetPrivacyLevel(_wallet.AnonScoreTarget) == PrivacyLevel.SemiPrivate)
.Union(onlyKnownByTheRecipientCoins)
Expand Down
6 changes: 6 additions & 0 deletions WalletWasabi.Fluent/Models/Wallets/WalletModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using ReactiveUI;
using WalletWasabi.Fluent.Helpers;
using WalletWasabi.Fluent.Infrastructure;
using WalletWasabi.Fluent.Models.Transactions;
using WalletWasabi.Fluent.ViewModels.Wallets.Labels;
using WalletWasabi.Wallets;

Expand Down Expand Up @@ -121,6 +122,11 @@ public IWalletInfoModel GetWalletInfo()
return new WalletInfoModel(Wallet);
}

public IPrivacySuggestionsModel GetPrivacySuggestionsModel(SendParameters sendParameters)
{
return new PrivacySuggestionsModel(sendParameters);
}

public void Rename(string newWalletName)
{
Services.WalletManager.RenameWallet(Wallet, newWalletName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@
using System.Threading.Tasks;
using WalletWasabi.Blockchain.TransactionBuilding;
using WalletWasabi.Fluent.Models.Transactions;
using WalletWasabi.Fluent.Models.Wallets;
using WalletWasabi.Wallets;

namespace WalletWasabi.Fluent.ViewModels.Wallets.Send;

public partial class PrivacySuggestionsFlyoutViewModel : ViewModelBase
{
private readonly PrivacySuggestionsModel _privacySuggestionsModel;
private readonly IPrivacySuggestionsModel _privacySuggestionsModel;
private readonly Subject<IEnumerable<PrivacyWarning>> _previewWarnings = new();

[AutoNotify] private PrivacySuggestion? _previewSuggestion;
Expand All @@ -27,9 +28,9 @@ public partial class PrivacySuggestionsFlyoutViewModel : ViewModelBase
[AutoNotify] private bool _goodPrivacy;
[AutoNotify] private bool _maxPrivacy;

public PrivacySuggestionsFlyoutViewModel(Wallet wallet)
public PrivacySuggestionsFlyoutViewModel(IWalletModel wallet)
{
_privacySuggestionsModel = new PrivacySuggestionsModel(wallet);
_privacySuggestionsModel = wallet.GetPrivacySuggestionsModel();
}

public ObservableCollection<PrivacyWarning> Warnings { get; } = new();
Expand Down
4 changes: 3 additions & 1 deletion WalletWasabi.Fluent/ViewModels/Wallets/Send/SendViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,9 @@ private async Task OnNextAsync()
await coinJoinManager.WalletEnteredSendingAsync(_wallet);
}

Navigate().To().TransactionPreview(_wallet, _walletModel, transactionInfo);
var sendParameters = _parameters with { TransactionInfo = transactionInfo };

Navigate().To().TransactionPreview(_wallet, _walletModel, sendParameters);
}

private async Task OnAutoPasteAsync()
Expand Down

0 comments on commit 438845d

Please sign in to comment.