Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some CF issues for CJManager and CJStateVM #10072

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 8 additions & 5 deletions WalletWasabi.Fluent/ViewModels/Wallets/CoinJoinStateViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,6 @@ public partial class CoinJoinStateViewModel : ViewModelBase
private DateTimeOffset _countDownStartTime;
private DateTimeOffset _countDownEndTime;

public bool IsAutoCoinJoinEnabled => WalletVm.CoinJoinSettings.AutoCoinJoin;

public CoinJoinStateViewModel(WalletViewModel walletVm)
{
WalletVm = walletVm;
Expand Down Expand Up @@ -108,11 +106,14 @@ public CoinJoinStateViewModel(WalletViewModel walletVm)
}
});

IsPauseButtonEnabled = this.WhenAnyValue(x => x.IsInCriticalPhase, x => x.PauseSpreading,
IsPauseButtonEnabled = this.WhenAnyValue(
x => x.IsInCriticalPhase,
x => x.PauseSpreading,
(isInCriticalPhase, pauseSpreading) => !isInCriticalPhase && !pauseSpreading);

StopPauseCommand = ReactiveCommand.CreateFromTask(async () =>
await coinJoinManager.StopAsync(wallet, CancellationToken.None), IsPauseButtonEnabled);
StopPauseCommand = ReactiveCommand.CreateFromTask(
async () => await coinJoinManager.StopAsync(wallet, CancellationToken.None),
IsPauseButtonEnabled);

AutoCoinJoinObservable = walletVm.CoinJoinSettings.WhenAnyValue(x => x.AutoCoinJoin);

Expand Down Expand Up @@ -172,6 +173,8 @@ private enum Trigger
AutoCoinJoinOff
}

public bool IsAutoCoinJoinEnabled => WalletVm.CoinJoinSettings.AutoCoinJoin;

public IObservable<bool> AutoCoinJoinObservable { get; }

public IObservable<bool> IsPauseButtonEnabled { get; }
Expand Down
52 changes: 28 additions & 24 deletions WalletWasabi/WabiSabi/Client/CoinJoinManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,6 @@ namespace WalletWasabi.WabiSabi.Client;

public class CoinJoinManager : BackgroundService
{
private record CoinJoinCommand(IWallet Wallet);
private record StartCoinJoinCommand(IWallet Wallet, bool StopWhenAllMixed, bool OverridePlebStop) : CoinJoinCommand(Wallet);
private record StopCoinJoinCommand(IWallet Wallet) : CoinJoinCommand(Wallet);

public CoinJoinManager(IWalletProvider walletProvider, RoundStateUpdater roundStatusUpdater, IWasabiHttpClientFactory backendHttpClientFactory, IWasabiBackendStatusProvider wasabiBackendStatusProvider, string coordinatorIdentifier)
{
WasabiBackendStatusProvide = wasabiBackendStatusProvider;
Expand All @@ -34,9 +30,9 @@ public CoinJoinManager(IWalletProvider walletProvider, RoundStateUpdater roundSt
RoundStatusUpdater = roundStatusUpdater;
CoordinatorIdentifier = coordinatorIdentifier;
}


public event EventHandler<StatusChangedEventArgs>? StatusChanged;
private IWasabiBackendStatusProvider WasabiBackendStatusProvide { get; }

public IWalletProvider WalletProvider { get; }
public IWasabiHttpClientFactory HttpClientFactory { get; }
public RoundStateUpdater RoundStatusUpdater { get; }
Expand All @@ -49,8 +45,6 @@ public CoinJoinManager(IWalletProvider walletProvider, RoundStateUpdater roundSt
/// </summary>
private ConcurrentDictionary<string, byte> WalletsInSendWorkflow { get; } = new();

public event EventHandler<StatusChangedEventArgs>? StatusChanged;

public CoinJoinClientState HighestCoinJoinClientState { get; private set; }

private Channel<CoinJoinCommand> CommandChannel { get; } = Channel.CreateUnbounded<CoinJoinCommand>();
Expand Down Expand Up @@ -111,7 +105,7 @@ private async Task MonitorWalletsAsync(CancellationToken stoppingToken)
var closedWallets = trackedWallets.Where(x => !mixableWallets.ContainsKey(x.Key)).ToImmutableList();
foreach (var closedWallet in closedWallets.Select(x => x.Value))
{
//closedWallet.Cancel();
// closedWallet.Cancel();
NotifyMixableWalletUnloaded(closedWallet);
trackedWallets.Remove(closedWallet.WalletName);
}
Expand Down Expand Up @@ -299,7 +293,8 @@ private void ScheduleRestartAutomatically(IWallet walletToStart, ConcurrentDicti
var linkedCts = CancellationTokenSource.CreateLinkedTokenSource(stoppingToken);
#pragma warning restore CA2000 // Dispose objects before losing scope

var restartTask = Task.Run(async () =>
var restartTask = Task.Run(
async () =>
{
try
{
Expand All @@ -322,7 +317,8 @@ private void ScheduleRestartAutomatically(IWallet walletToStart, ConcurrentDicti
{
walletToStart.LogInfo($"AutoStart was already handled.");
}
}, linkedCts.Token);
},
linkedCts.Token);

if (!trackedAutoStarts.TryAdd(walletToStart, new TrackedAutoStart(restartTask, linkedCts)))
{
Expand Down Expand Up @@ -353,6 +349,7 @@ private async Task MonitorAndHandlingCoinJoinFinallizationAsync(ConcurrentDictio
NotifyWalletStoppedCoinJoin(finishedCoinJoin.Wallet);
}
}

// Updates the highest coinjoin client state.
var onGoingCoinJoins = trackedCoinJoins.Values.Where(wtd => !wtd.IsCompleted).ToImmutableArray();
var scheduledCoinJoins = trackedAutoStarts.Select(t => t.Key);
Expand Down Expand Up @@ -482,21 +479,25 @@ private async Task MarkDestinationsUsedAsync(ImmutableList<Script> outputs)
private void NotifyMixableWalletLoaded(IWallet openedWallet) =>
StatusChanged.SafeInvoke(this, new LoadedEventArgs(openedWallet));

private void NotifyCoinJoinCompletion(CoinJoinTracker finishedCoinJoin) =>
StatusChanged.SafeInvoke(this, new CompletedEventArgs(
finishedCoinJoin.Wallet,
finishedCoinJoin.CoinJoinTask.Status switch
{
TaskStatus.RanToCompletion when finishedCoinJoin.CoinJoinTask.Result is SuccessfulCoinJoinResult => CompletionStatus.Success,
TaskStatus.Canceled => CompletionStatus.Canceled,
TaskStatus.Faulted => CompletionStatus.Failed,
_ => CompletionStatus.Unknown,
}));
private void NotifyCoinJoinCompletion(CoinJoinTracker finishedCoinJoin)
{
CompletionStatus status = finishedCoinJoin.CoinJoinTask.Status switch
{
TaskStatus.RanToCompletion when finishedCoinJoin.CoinJoinTask.Result is SuccessfulCoinJoinResult => CompletionStatus.Success,
TaskStatus.Canceled => CompletionStatus.Canceled,
TaskStatus.Faulted => CompletionStatus.Failed,
_ => CompletionStatus.Unknown
};
CompletedEventArgs e = new CompletedEventArgs(finishedCoinJoin.Wallet, status);
StatusChanged.SafeInvoke(this, e);
}

private void NotifyCoinJoinStatusChanged(IWallet wallet, CoinJoinProgressEventArgs coinJoinProgressEventArgs) =>
StatusChanged.SafeInvoke(this, new CoinJoinStatusEventArgs(
wallet,
coinJoinProgressEventArgs));
StatusChanged.SafeInvoke(
this,
new CoinJoinStatusEventArgs(
wallet,
coinJoinProgressEventArgs));

private async Task<ImmutableDictionary<string, IWallet>> GetMixableWalletsAsync() =>
(await WalletProvider.GetWalletsAsync().ConfigureAwait(false))
Expand Down Expand Up @@ -547,4 +548,7 @@ private void CoinJoinTracker_WalletCoinJoinProgressChanged(object? sender, CoinJ
}

private record TrackedAutoStart(Task Task, CancellationTokenSource CancellationTokenSource);
private record CoinJoinCommand(IWallet Wallet);
private record StartCoinJoinCommand(IWallet Wallet, bool StopWhenAllMixed, bool OverridePlebStop) : CoinJoinCommand(Wallet);
private record StopCoinJoinCommand(IWallet Wallet) : CoinJoinCommand(Wallet);
}