Simplify round verification
The repetitive code was extracted to a method.
Nullable `Round?` is not necessary anymore.
MODIFY
WalletWasabi/WabiSabi/Backend/Rounds/Arena.cs
WalletWasabi/WabiSabi/Backend/Rounds/Arena.cs
@@ -335,10 +335,7 @@ namespace WalletWasabi.WabiSabi.Backend.Rounds
using (await AsyncLock.LockAsync(cancellationToken).ConfigureAwait(false))
{
- if (Rounds.FirstOrDefault(x => x.Id == request.RoundId) is not { } round)
- {
- throw new WabiSabiProtocolException(WabiSabiProtocolErrorCode.RoundNotFound);
- }
+ var round = GetRound(request.RoundId);
var registeredCoins = Rounds.Where(x => !(x.Phase == Phase.Ended && !x.WasTransactionBroadcast))
.SelectMany(r => r.Alices.Select(a => a.Coin));
@@ -414,10 +411,7 @@ namespace WalletWasabi.WabiSabi.Backend.Rounds
{
using (await AsyncLock.LockAsync(cancellationToken).ConfigureAwait(false))
{
- if (Rounds.FirstOrDefault(r => r.Id == request.RoundId) is not Round round)
- {
- throw new WabiSabiProtocolException(WabiSabiProtocolErrorCode.RoundNotFound, $"Round ({request.RoundId}) not found.");
- }
+ var round = GetRound(request.RoundId);
if (round.Alices.Find(a => a.Id == request.AliceId) is not Alice alice)
{
@@ -432,10 +426,8 @@ namespace WalletWasabi.WabiSabi.Backend.Rounds
{
using (await AsyncLock.LockAsync(cancellationToken).ConfigureAwait(false))
{
- if (Rounds.FirstOrDefault(x => x.Id == request.RoundId) is not Round round)
- {
- throw new WabiSabiProtocolException(WabiSabiProtocolErrorCode.RoundNotFound, $"Round ({request.RoundId}) not found.");
- }
+ var round = GetRound(request.RoundId);
+
if (round.Phase != Phase.InputRegistration)
{
throw new WabiSabiProtocolException(WabiSabiProtocolErrorCode.WrongPhase, $"Round ({request.RoundId}): Wrong phase ({round.Phase}).");
@@ -446,18 +438,14 @@ namespace WalletWasabi.WabiSabi.Backend.Rounds
public async Task<ConnectionConfirmationResponse> ConfirmConnectionAsync(ConnectionConfirmationRequest request, CancellationToken cancellationToken)
{
- Round? round;
+ Round round;
Alice? alice;
var realAmountCredentialRequests = request.RealAmountCredentialRequests;
var realVsizeCredentialRequests = request.RealVsizeCredentialRequests;
using (await AsyncLock.LockAsync(cancellationToken).ConfigureAwait(false))
{
- round = Rounds.FirstOrDefault(x => x.Id == request.RoundId);
- if (round is null)
- {
- throw new WabiSabiProtocolException(WabiSabiProtocolErrorCode.RoundNotFound, $"Round ({request.RoundId}) not found.");
- }
+ round = GetRound(request.RoundId);
if (round.Phase is not (Phase.InputRegistration or Phase.ConnectionConfirmation))
{
@@ -545,10 +533,7 @@ namespace WalletWasabi.WabiSabi.Backend.Rounds
{
using (await AsyncLock.LockAsync(cancellationToken).ConfigureAwait(false))
{
- if (Rounds.FirstOrDefault(x => x.Id == request.RoundId) is not Round round)
- {
- throw new WabiSabiProtocolException(WabiSabiProtocolErrorCode.RoundNotFound, $"Round ({request.RoundId}) not found.");
- }
+ var round = GetRound(request.RoundId);
var credentialAmount = -request.AmountCredentialRequests.Delta;
@@ -584,10 +569,7 @@ namespace WalletWasabi.WabiSabi.Backend.Rounds
{
using (await AsyncLock.LockAsync(cancellationToken).ConfigureAwait(false))
{
- if (Rounds.FirstOrDefault(x => x.Id == request.RoundId) is not Round round)
- {
- throw new WabiSabiProtocolException(WabiSabiProtocolErrorCode.RoundNotFound, $"Round ({request.RoundId}) not found.");
- }
+ var round = GetRound(request.RoundId);
if (round.Phase != Phase.TransactionSigning)
{
@@ -607,14 +589,10 @@ namespace WalletWasabi.WabiSabi.Backend.Rounds
public async Task<ReissueCredentialResponse> ReissuanceAsync(ReissueCredentialRequest request, CancellationToken cancellationToken)
{
- Round? round;
+ Round round;
using (await AsyncLock.LockAsync(cancellationToken).ConfigureAwait(false))
{
- round = Rounds.FirstOrDefault(x => x.Id == request.RoundId);
- if (round is null)
- {
- throw new WabiSabiProtocolException(WabiSabiProtocolErrorCode.RoundNotFound, $"Round ({request.RoundId}) not found.");
- }
+ round = GetRound(request.RoundId);
if (round.Phase is not (Phase.ConnectionConfirmation or Phase.OutputRegistration))
{
@@ -680,5 +658,9 @@ namespace WalletWasabi.WabiSabi.Backend.Rounds
Random.Dispose();
base.Dispose();
}
+
+ private Round GetRound(uint256 roundId) =>
+ Rounds.FirstOrDefault(x => x.Id == roundId)
+ ?? throw new WabiSabiProtocolException(WabiSabiProtocolErrorCode.RoundNotFound, $"Round ({roundId}) not found.");
}
}

Simplify round verification

The repetitive code was extracted to a method.
Nullable `Round?` is not necessary anymore.