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

State machine will follow the business logic (continuation) #10059

Merged
merged 17 commits into from Feb 20, 2023

Conversation

turbolay
Copy link
Collaborator

@turbolay turbolay commented Feb 4, 2023

Supersedes #10032
Closes #9957

The point is to NotifyWalletStartedCoinJoin(walletToStart) when ScheduleRestartAutomatically is called, but only once (hence use of WasRestartedAutomatically )

The change with PlebStop is to adapt it to new behavior as it was broken. It makes sure that when a user chose to OverridePlebstop, he won't have to override again until he presses Pause. It should also block when balance becomes lower than the threshold during CJ because of fees (untested).

I was not able to see any issue with this fix, @yahiheb @MarnixCroes can you try to break it?

@turbolay
Copy link
Collaborator Author

turbolay commented Feb 4, 2023

CleanShot 2023-02-03 at 19 27 55@2x

What have I done??

@turbolay turbolay changed the title Improve NoCoinsToMix business logic State machine will follow the business logic (continuation) Feb 4, 2023
@@ -249,6 +251,7 @@ private void ConfigureStateMachine()
.Permit(Trigger.PlebStopChanged, State.Playing)
.Permit(Trigger.WalletStartedCoinJoin, State.Playing)
.Permit(Trigger.WalletStoppedCoinJoin, State.StoppedOrPaused)
.Permit(Trigger.StartError, State.Playing)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same change than #10032 but for PlebStop

walletToStart.LogDebug($"AutoStart was already scheduled, cancel the last task and do not wait.");
trackedAutoStarts[walletToStart].CancellationTokenSource.Cancel();
trackedAutoStarts.Remove(walletToStart, out _);
delayRestart = 0;
Copy link
Collaborator Author

@turbolay turbolay Feb 4, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is to make sure that whenever user tries to restart CoinJoin when it was already scheduled, new arguments are taken into account.

This happens for example when user presses Play in State.PlebStop with only pending coins. A restart task was already scheduled by precedent action with OverridePlebStop as false, so when user presses Play in State.PlebStop we need to cancel the last task to start a new one with OverridePlebStop as true.

I set the delay to 0 as it doesn't really change anything but we could increase it a bit or use a StopWatch if this could be a problem.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would isolate the case. Compare the two trackedAutoStarts OverridePlebStop. If it is different, do the update if not then just debug the message and return.

@turbolay turbolay marked this pull request as ready for review February 4, 2023 08:34
Copy link
Collaborator

@MarnixCroes MarnixCroes left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was not able to see any issue with this fix, @yahiheb @MarnixCroes can you try to break it?

So far, I'm not able to break it.

@molnard
Copy link
Collaborator

molnard commented Feb 6, 2023

CleanShot 2023-02-03 at 19 27 55@2x

What have I done??

I don't know but it is not looking good. Can you fix that?

@pull-request-size pull-request-size bot added size/L and removed size/M labels Feb 7, 2023
@pull-request-size pull-request-size bot added size/M and removed size/L labels Feb 7, 2023
turbolay added a commit to turbolay/WalletWasabi that referenced this pull request Feb 7, 2023
@pull-request-size pull-request-size bot added size/L and removed size/M labels Feb 7, 2023
@pull-request-size pull-request-size bot added size/M and removed size/L labels Feb 7, 2023
Copy link
Collaborator

@yahiheb yahiheb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Once all your coins are private and you get this message Hurray! Your funds are private the play button does nothing on master. With this PR it works.

tACK

yahiheb
yahiheb previously approved these changes Feb 9, 2023
walletToStart.LogDebug($"AutoStart was already scheduled, cancel the last task and do not wait.");
trackedAutoStarts[walletToStart].CancellationTokenSource.Cancel();
trackedAutoStarts.Remove(walletToStart, out _);
delayRestart = 0;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would isolate the case. Compare the two trackedAutoStarts OverridePlebStop. If it is different, do the update if not then just debug the message and return.

WalletWasabi/WabiSabi/Client/CoinJoinManager.cs Outdated Show resolved Hide resolved
walletToStart.LogDebug($"AutoStart was already scheduled.");
return;
walletToStart.LogDebug("AutoStart was already scheduled, cancel the last task and do not wait.");
TryRemoveTrackedAutoStart(trackedAutoStarts, walletToStart);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure that it will be removed correctly after while being sure the old task is not executed in case for eg of an infortunate timing

@turbolay
Copy link
Collaborator Author

turbolay commented Feb 9, 2023

I would isolate the case. Compare the two trackedAutoStarts OverridePlebStop. If it is different, do the update if not then just debug the message and return.

@molnard To do that I'd need to access parameters for the last task and hence change record TrackedAutoStart to also take bool StopWhenAllMixed, bool OverridePlebStop as parameters, should I proceed?

@molnard
Copy link
Collaborator

molnard commented Feb 9, 2023

TrackedAutoStart to also take bool StopWhenAllMixed, bool OverridePlebStop as parameters, should I proceed?

That is a clean-cut. Yes.

if (trackedAutoStarts.ContainsKey(walletToStart))
{
walletToStart.LogDebug($"AutoStart was already scheduled.");
return;
if (stopWhenAllMixed == trackedAutoStarts[walletToStart].StopWhenAllMixed && overridePlebStop == trackedAutoStarts[walletToStart].OverridePlebStop)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dictionnary with IWallet as key. Maybe consider changing? I don't know if necessary, neither what we could use.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will address this in a subsequent PR probably using WalletName

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or implement making sure the wallet name will be used when checking equality for dictionaries. ACK for doing it later.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a comment, wallet references are never changing.

Copy link
Collaborator

@molnard molnard left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Purrfect

Cali7GIF

WalletWasabi/WabiSabi/Client/CoinJoinManager.cs Outdated Show resolved Hide resolved
if (trackedAutoStarts.ContainsKey(walletToStart))
{
walletToStart.LogDebug($"AutoStart was already scheduled.");
return;
if (stopWhenAllMixed == trackedAutoStarts[walletToStart].StopWhenAllMixed && overridePlebStop == trackedAutoStarts[walletToStart].OverridePlebStop)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or implement making sure the wallet name will be used when checking equality for dictionaries. ACK for doing it later.

@molnard
Copy link
Collaborator

molnard commented Feb 14, 2023

@brizik please test this with the help of @MarnixCroes.

yahiheb
yahiheb previously approved these changes Feb 14, 2023
Copy link
Collaborator

@yahiheb yahiheb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tACK

@turbolay
Copy link
Collaborator Author

Last 2 commits does not change anything to the behavior:

  • Use if instead of ternary
  • Create the task then add to task list it then start it. There was no any issues before but now that we do not wait sometimes there could be a concurrency issues

@molnard
Copy link
Collaborator

molnard commented Feb 17, 2023

@brizik please test this with the help of @MarnixCroes.

@MarnixCroes can you take a look at this?


#pragma warning disable CA2000 // Dispose objects before losing scope
var linkedCts = CancellationTokenSource.CreateLinkedTokenSource(stoppingToken);
#pragma warning restore CA2000 // Dispose objects before losing scope

var restartTask = Task.Run(
var restartTask = new Task(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why the change here?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I explained in my comment:

Create the task then add to task list it then start it. There was no any issues before but now that we do not wait sometimes there could be a concurrency issues

At the end of the task we remove the task from the list, but the line to add the task into the list was after task run, I was afraid of a potential concurrency problem.
Should I revert?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Collaborator Author

@turbolay turbolay Feb 17, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Between

if (trackedAutoStarts.TryRemove(walletToStart, out _))

and

if (trackedAutoStarts.TryAdd(walletToStart, new TrackedAutoStart(restartTask, stopWhenAllMixed, overridePlebStop, linkedCts)))

because we do not wait anymore in some cases

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACK

Copy link
Collaborator

@MarnixCroes MarnixCroes left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tACK

Copy link
Collaborator

@yahiheb yahiheb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tACK

Copy link
Collaborator

@molnard molnard left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tACK 🥇

@molnard molnard merged commit 1a751c8 into zkSNACKs:master Feb 20, 2023
@turbolay turbolay deleted the cjWhenNoCoinsToMix branch February 26, 2024 04:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

musicbox: "hurray funds private, press Play to start" bug
4 participants