Skip to content

Commit

Permalink
Fix Send workflow disruption (WalletWasabi#13429)
Browse files Browse the repository at this point in the history
  • Loading branch information
lontivero authored Sep 20, 2024
1 parent 726b4f2 commit 9a357d7
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 14 deletions.
23 changes: 17 additions & 6 deletions WalletWasabi.Daemon/Rpc/WasabiJsonRpcService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,7 @@ public JsonRpcResultList GetAllKeys()
[JsonRpcMethod("startcoinjoin")]
public void StartCoinJoining(string? password = null, bool stopWhenAllMixed = true, bool overridePlebStop = true)
{
var coinJoinManager = Global.HostedServices.Get<CoinJoinManager>();
var coinJoinManager = GetCoinJoinManager();
var activeWallet = Guard.NotNull(nameof(ActiveWallet), ActiveWallet);

AssertWalletIsLoaded();
Expand All @@ -481,6 +481,7 @@ public void StartCoinJoining(string? password = null, bool stopWhenAllMixed = tr
public void StartCoinjoinSweeping(string? password = null, string? outputWalletName = null)
{
var activeWallet = Guard.NotNull(nameof(ActiveWallet), ActiveWallet);
var coinJoinManager = GetCoinJoinManager();

AssertWalletIsLoaded();
AssertWalletIsLoggedIn(activeWallet, password ?? "");
Expand All @@ -492,25 +493,24 @@ public void StartCoinjoinSweeping(string? password = null, string? outputWalletN

var outputWallet = Global.WalletManager.GetWalletByName(outputWalletName);

StartCoinjoinSweepAsync(activeWallet, outputWallet).ConfigureAwait(false);
StartCoinjoinSweepAsync(coinJoinManager, activeWallet, outputWallet).ConfigureAwait(false);
}

private async Task StartCoinjoinSweepAsync(Wallet activeWallet, Wallet outputWallet)
private async Task StartCoinjoinSweepAsync(CoinJoinManager coinJoinManager, Wallet activeWallet, Wallet outputWallet)
{
// If output wallet isn't initialized, then load it.
if (outputWallet.State == WalletState.Uninitialized)
{
await Global.WalletManager.StartWalletAsync(outputWallet).ConfigureAwait(false);
}

var coinJoinManager = Global.HostedServices.Get<CoinJoinManager>();
await coinJoinManager.StartAsync(activeWallet, outputWallet, stopWhenAllMixed: false, overridePlebStop: true, CancellationToken.None).ConfigureAwait(false);
}

[JsonRpcMethod("stopcoinjoin")]
public void StopCoinJoining()
{
var coinJoinManager = Global.HostedServices.Get<CoinJoinManager>();
var coinJoinManager = GetCoinJoinManager();
var activeWallet = Guard.NotNull(nameof(ActiveWallet), ActiveWallet);

AssertWalletIsLoaded();
Expand Down Expand Up @@ -548,9 +548,20 @@ public Task StopAsync()
throw new InvalidOperationException("This RPC method is special and the handling method should not be called.");
}

private CoinJoinManager GetCoinJoinManager()
{
var coinJoinManager = Global.HostedServices.GetOrDefault<CoinJoinManager>();
if (coinJoinManager is null)
{
throw new InvalidOperationException("No coordinator configured.");
}

return coinJoinManager;
}

private string GetCoinjoinStatus(Wallet wallet)
{
var coinJoinManager = Global.HostedServices.Get<CoinJoinManager>();
var coinJoinManager = GetCoinJoinManager();
var walletCoinjoinClientState = coinJoinManager.GetCoinjoinClientState(wallet.WalletId);
return walletCoinjoinClientState switch
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public partial class PrivacySuggestionsModel
/// <summary>Allow at most one suggestion generation run.</summary>
private readonly AsyncLock _asyncLock = new();

private readonly CoinJoinManager _cjManager;
private readonly CoinJoinManager? _cjManager;
private readonly SendFlowModel _sendFlow;
private readonly Wallet _wallet;
private CancellationTokenSource? _singleRunCancellationTokenSource;
Expand All @@ -42,7 +42,7 @@ public PrivacySuggestionsModel(SendFlowModel sendFlow)
{
_sendFlow = sendFlow;
_wallet = sendFlow.Wallet;
_cjManager = Services.HostedServices.Get<CoinJoinManager>();
_cjManager = Services.HostedServices.GetOrDefault<CoinJoinManager>();
}

/// <summary>
Expand Down Expand Up @@ -159,7 +159,7 @@ private IEnumerable<PrivacyItem> VerifyPrivacyLevel(Parameters parameters)

var availableCoins = _sendFlow.AvailableCoins;

ImmutableList<SmartCoin> coinsToExclude = _cjManager.CoinsInCriticalPhase[_wallet.WalletId];
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.
Expand Down Expand Up @@ -280,7 +280,7 @@ private async Task<List<ChangeAvoidanceSuggestion>> CreateChangeAvoidanceSuggest
ImmutableArray<SmartCoin> coinsToUse = usedPockets.SelectMany(x => x.Coins).ToImmutableArray();

// If the original transaction couldn't avoid the CJing coins, BnB can use them too. Otherwise exclude them.
var coinsInCoinJoin = _cjManager.CoinsInCriticalPhase[_wallet.WalletId];
var coinsInCoinJoin = _cjManager?.CoinsInCriticalPhase[_wallet.WalletId] ?? [];
coinsToUse = spentCoins.Any(coinsInCoinJoin.Contains) ? coinsToUse : coinsToUse.Except(coinsInCoinJoin).ToImmutableArray();

// If the original transaction only using confirmed coins, BnB can use only them too. Otherwise let unconfirmed oins stay in the list.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ private async Task InitializeLabelsAsync()
{
var privateThreshold = _wallet.AnonScoreTarget;

var cjManager = Services.HostedServices.Get<CoinJoinManager>();
var coinsToExclude = cjManager.CoinsInCriticalPhase[_wallet.WalletId].ToList();
var cjManager = Services.HostedServices.GetOrDefault<CoinJoinManager>();
var coinsToExclude = cjManager?.CoinsInCriticalPhase[_wallet.WalletId].ToList() ?? [];

var pockets = _sendFlow.GetPockets();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -475,12 +475,12 @@ private async Task CheckChangePocketAvailableAsync(BuildTransactionResult transa
return;
}

var cjManager = Services.HostedServices.Get<CoinJoinManager>();
var cjManager = Services.HostedServices.GetOrDefault<CoinJoinManager>();

var usedCoins = transaction.SpentCoins;
var pockets = _sendFlow.GetPockets();
var labelSelection = new LabelSelectionViewModel(_wallet.KeyManager, _wallet.Password, _info, isSilent: true);
await labelSelection.ResetAsync(pockets, coinsToExclude: cjManager.CoinsInCriticalPhase[_wallet.WalletId].ToList());
await labelSelection.ResetAsync(pockets, coinsToExclude: cjManager?.CoinsInCriticalPhase[_wallet.WalletId].ToList() ?? []);

_info.IsOtherPocketSelectionPossible = labelSelection.IsOtherSelectionPossible(usedCoins, _info.Recipient);
}
Expand Down

0 comments on commit 9a357d7

Please sign in to comment.