forked from WalletWasabi/WalletWasabi
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CoinJoiClient.CreateRegisterAndConfirmCoinsAsync refactor
Moving the RegisterInputAsync function from the method. Contributes to WalletWasabi#12790 (comment)
- Loading branch information
Showing
2 changed files
with
226 additions
and
159 deletions.
There are no files selected for viewing
82 changes: 82 additions & 0 deletions
82
WalletWasabi/WabiSabi/Client/CoinJoin/Client/CoinConfirmationState.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
using System.Threading; | ||
using WalletWasabi.WabiSabi.Client.RoundStateAwaiters; | ||
using WalletWasabi.WabiSabi.Models; | ||
|
||
namespace WalletWasabi.WabiSabi.Client.CoinJoin.Client; | ||
|
||
/* | ||
* Used by CoinJoinClient.RegisterInputAsync to store the common data/tokens during the coin confirmation phase. | ||
*/ | ||
|
||
internal class CoinConfirmationState : IDisposable | ||
{ | ||
public CoinConfirmationState(RoundState roundState, CancellationToken cancel, TimeSpan extraPhaseTimeoutMargin) | ||
{ | ||
RoundState = roundState; | ||
Cancel = cancel; | ||
LastUnexpectedRoundPhaseException = null; | ||
|
||
var remainingInputRegTime = roundState.InputRegistrationEnd - DateTimeOffset.UtcNow; | ||
|
||
StrictInputRegTimeoutCts = new(remainingInputRegTime); | ||
InputRegTimeoutCts = new(remainingInputRegTime + extraPhaseTimeoutMargin); | ||
ConnConfTimeoutCts = new(remainingInputRegTime + roundState.CoinjoinState.Parameters.ConnectionConfirmationTimeout + extraPhaseTimeoutMargin); | ||
RegistrationsCts = new(); | ||
ConfirmationsCts = new(); | ||
|
||
LinkedUnregisterCts = CancellationTokenSource.CreateLinkedTokenSource(StrictInputRegTimeoutCts.Token, RegistrationsCts.Token); | ||
LinkedRegistrationsCts = CancellationTokenSource.CreateLinkedTokenSource(InputRegTimeoutCts.Token, RegistrationsCts.Token, cancel); | ||
LinkedConfirmationsCts = CancellationTokenSource.CreateLinkedTokenSource(ConnConfTimeoutCts.Token, ConfirmationsCts.Token, cancel); | ||
TimeoutAndGlobalCts = CancellationTokenSource.CreateLinkedTokenSource(InputRegTimeoutCts.Token, ConnConfTimeoutCts.Token, cancel); | ||
} | ||
|
||
public int EventInvocedAlready(int set) | ||
{ | ||
return Interlocked.Exchange(ref _eventInvokedAlready, 1); | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
Dispose(disposing: true); | ||
GC.SuppressFinalize(this); | ||
} | ||
|
||
protected virtual void Dispose(bool disposing) | ||
{ | ||
if (!_disposed) | ||
{ | ||
if (disposing) | ||
{ | ||
TimeoutAndGlobalCts.Dispose(); | ||
LinkedConfirmationsCts.Dispose(); | ||
LinkedRegistrationsCts.Dispose(); | ||
LinkedUnregisterCts.Dispose(); | ||
ConfirmationsCts.Dispose(); | ||
RegistrationsCts.Dispose(); | ||
ConnConfTimeoutCts.Dispose(); | ||
InputRegTimeoutCts.Dispose(); | ||
StrictInputRegTimeoutCts.Dispose(); | ||
} | ||
_disposed = true; | ||
} | ||
} | ||
|
||
private bool _disposed = false; | ||
private int _eventInvokedAlready = 0; | ||
|
||
public RoundState RoundState { get; init; } | ||
public CancellationToken Cancel { get; init; } | ||
|
||
public UnexpectedRoundPhaseException? LastUnexpectedRoundPhaseException { get; set; } | ||
|
||
public CancellationTokenSource StrictInputRegTimeoutCts { get; init; } | ||
public CancellationTokenSource InputRegTimeoutCts { get; init; } | ||
public CancellationTokenSource ConnConfTimeoutCts { get; init; } | ||
public CancellationTokenSource RegistrationsCts { get; init; } | ||
public CancellationTokenSource ConfirmationsCts { get; init; } | ||
|
||
public CancellationTokenSource LinkedUnregisterCts { get; init; } | ||
public CancellationTokenSource LinkedRegistrationsCts { get; init; } | ||
public CancellationTokenSource LinkedConfirmationsCts { get; init; } | ||
public CancellationTokenSource TimeoutAndGlobalCts { get; init; } | ||
} |
Oops, something went wrong.