Use nameof instead of magic strings (Backend project)
MODIFY
WalletWasabi.Backend/Controllers/ChaumianCoinJoinController.cs
WalletWasabi.Backend/Controllers/ChaumianCoinJoinController.cs
@@ -119,7 +119,7 @@ namespace WalletWasabi.Backend.Controllers
if (round is null || round.Phase != CcjRoundPhase.InputRegistration)
{
- return NotFound("No such running round in InputRegistration. Try another round.");
+ return NotFound($"No such running round in {nameof(CcjRoundPhase.InputRegistration)}. Try another round.");
}
// Do more checks.
@@ -148,7 +148,7 @@ namespace WalletWasabi.Backend.Controllers
// RegTest and TestNet address formats are sometimes the same.
if (Network == Network.Main)
{
- return BadRequest($"Invalid ChangeOutputAddress Network.");
+ return BadRequest($"Invalid {nameof(request.ChangeOutputAddress)} Network.");
}
}
@@ -411,7 +411,8 @@ namespace WalletWasabi.Backend.Controllers
default:
{
TryLogLateRequest(roundId, CcjRoundPhase.ConnectionConfirmation);
- return Gone($"Participation can be only confirmed from InputRegistration or ConnectionConfirmation phase. Current phase: {phase}.");
+ return Gone($"Participation can be only confirmed from {nameof(CcjRoundPhase.InputRegistration)} or " +
+ $"{nameof(CcjRoundPhase.ConnectionConfirmation)} phase. Current phase: {phase}.");
}
}
@@ -473,7 +474,7 @@ namespace WalletWasabi.Backend.Controllers
}
default:
{
- return Gone($"Participation can be only unconfirmed from InputRegistration phase. Current phase: {phase}.");
+ return Gone($"Participation can be only unconfirmed from {nameof(CcjRoundPhase.InputRegistration)} phase. Current phase: {phase}.");
}
}
}
@@ -521,7 +522,7 @@ namespace WalletWasabi.Backend.Controllers
if (phase != CcjRoundPhase.OutputRegistration)
{
TryLogLateRequest(roundId, CcjRoundPhase.OutputRegistration);
- return Conflict($"Output registration can only be done from OutputRegistration phase. Current phase: {phase}.");
+ return Conflict($"Output registration can only be done from {nameof(CcjRoundPhase.OutputRegistration)} phase. Current phase: {phase}.");
}
if (request.OutputAddress.Network != Network)
@@ -529,7 +530,7 @@ namespace WalletWasabi.Backend.Controllers
// RegTest and TestNet address formats are sometimes the same.
if (Network == Network.Main)
{
- return BadRequest($"Invalid OutputAddress Network.");
+ return BadRequest($"Invalid {nameof(request.OutputAddress)} Network.");
}
}
@@ -564,7 +565,7 @@ namespace WalletWasabi.Backend.Controllers
}
catch (Exception ex)
{
- return BadRequest($"Invalid outputAddress is provided. Details: {ex.Message}");
+ return BadRequest($"Invalid {nameof(request.OutputAddress)} is provided. Details: {ex.Message}");
}
int bobCount = round.CountBobs();
@@ -620,7 +621,7 @@ namespace WalletWasabi.Backend.Controllers
default:
{
TryLogLateRequest(roundId, CcjRoundPhase.Signing);
- return Conflict($"CoinJoin can only be requested from Signing phase. Current phase: {phase}.");
+ return Conflict($"CoinJoin can only be requested from {CcjRoundPhase.Signing} phase. Current phase: {phase}.");
}
}
}
@@ -695,7 +696,7 @@ namespace WalletWasabi.Backend.Controllers
// Check duplicates.
if (round.SignedCoinJoin.Inputs[index].HasWitScript())
{
- return BadRequest($"Input is already signed.");
+ return BadRequest("Input is already signed.");
}
// Verify witness.
@@ -710,7 +711,7 @@ namespace WalletWasabi.Backend.Controllers
// 5. Verify if currentIndexedInput is correctly signed, if not, return the specific error.
if (!currentIndexedInput.VerifyScript(registeredCoin, out ScriptError error))
{
- return BadRequest($"Invalid witness is provided. ScriptError: {error}.");
+ return BadRequest($"Invalid witness is provided. {nameof(ScriptError)}: {error}.");
}
// Finally add it to our CJ.
@@ -727,7 +728,7 @@ namespace WalletWasabi.Backend.Controllers
default:
{
TryLogLateRequest(roundId, CcjRoundPhase.Signing);
- return Conflict($"CoinJoin can only be requested from Signing phase. Current phase: {phase}.");
+ return Conflict($"CoinJoin can only be requested from {CcjRoundPhase.Signing} phase. Current phase: {phase}.");
}
}
}
@@ -737,7 +738,7 @@ namespace WalletWasabi.Backend.Controllers
returnFailureResponse = null;
if (string.IsNullOrWhiteSpace(uniqueId) || !ModelState.IsValid)
{
- returnFailureResponse = BadRequest("Invalid uniqueId provided.");
+ returnFailureResponse = BadRequest($"Invalid {nameof(uniqueId)} provided.");
}
Guid aliceGuid = Guid.Empty;
@@ -748,12 +749,12 @@ namespace WalletWasabi.Backend.Controllers
catch (Exception ex)
{
Logger.LogDebug<ChaumianCoinJoinController>(ex);
- returnFailureResponse = BadRequest("Invalid uniqueId provided.");
+ returnFailureResponse = BadRequest($"Invalid {nameof(uniqueId)} provided.");
}
if (aliceGuid == Guid.Empty) // Probably not possible
{
- Logger.LogDebug<ChaumianCoinJoinController>($"Empty uniqueId GID provided in {nameof(GetCoinJoin)} function.");
- returnFailureResponse = BadRequest("Invalid uniqueId provided.");
+ Logger.LogDebug<ChaumianCoinJoinController>($"Empty {nameof(uniqueId)} GID provided in {nameof(GetCoinJoin)} function.");
+ returnFailureResponse = BadRequest($"Invalid {nameof(uniqueId)} provided.");
}
return aliceGuid;

MODIFY
WalletWasabi.Backend/Global.cs
WalletWasabi.Backend/Global.cs
@@ -61,7 +61,7 @@ namespace WalletWasabi.Backend
IndexBuilderService = new IndexBuilderService(RpcClient, TrustedNodeNotifyingBehavior, indexFilePath, utxoSetFilePath);
Coordinator = new CcjCoordinator(RpcClient.Network, TrustedNodeNotifyingBehavior, Path.Combine(DataDir, nameof(CcjCoordinator)), RpcClient, roundConfig);
IndexBuilderService.Synchronize();
- Logger.LogInfo<IndexBuilderService>("IndexBuilderService is successfully initialized and started synchronization.");
+ Logger.LogInfo<IndexBuilderService>($"{nameof(IndexBuilderService)} is successfully initialized and started synchronization.");
await Coordinator.MakeSureTwoRunningRoundsAsync();
Logger.LogInfo<CcjCoordinator>("Chaumian CoinJoin Coordinator is successfully initialized and started two new rounds.");
@@ -137,28 +137,28 @@ namespace WalletWasabi.Backend
TrustedNodeNotifyingBehavior = node.Behaviors.Find<TrustedNodeNotifyingBehavior>();
try
{
- Logger.LogInfo<Node>($"TCP Connection succeeded, handshaking...");
+ Logger.LogInfo<Node>("TCP Connection succeeded, handshaking...");
node.VersionHandshake(Constants.LocalBackendNodeRequirements, handshakeTimeout.Token);
var peerServices = node.PeerVersion.Services;
if (!peerServices.HasFlag(NodeServices.Network) && !peerServices.HasFlag(NodeServices.NODE_NETWORK_LIMITED))
{
- throw new InvalidOperationException($"Wasabi cannot use the local node because it does not provide blocks.");
+ throw new InvalidOperationException("Wasabi cannot use the local node because it does not provide blocks.");
}
- Logger.LogInfo<Node>($"Handshake completed successfully.");
+ Logger.LogInfo<Node>("Handshake completed successfully.");
if (!node.IsConnected)
{
throw new InvalidOperationException($"Wasabi could not complete the handshake with the local node and dropped the connection.{Environment.NewLine}" +
- $"Probably this is because the node does not support retrieving full blocks or segwit serialization.");
+ "Probably this is because the node does not support retrieving full blocks or segwit serialization.");
}
LocalNode = node;
}
catch (OperationCanceledException) when (handshakeTimeout.IsCancellationRequested)
{
Logger.LogWarning<Node>($"Wasabi could not complete the handshake with the local node. Probably Wasabi is not whitelisted by the node.{Environment.NewLine}" +
- $"Use \"whitebind\" in the node configuration. (Typically whitebind=127.0.0.1:8333 if Wasabi and the node are on the same machine and whitelist=1.2.3.4 if they are not.)");
+ "Use \"whitebind\" in the node configuration. (Typically whitebind=127.0.0.1:8333 if Wasabi and the node are on the same machine and whitelist=1.2.3.4 if they are not.)");
throw;
}
}
@@ -173,13 +173,13 @@ namespace WalletWasabi.Backend
var blocks = blockchainInfo.Blocks;
if (blocks == 0 && Config.Network != Network.RegTest)
{
- throw new NotSupportedException("blocks == 0");
+ throw new NotSupportedException($"{nameof(blocks)} == 0");
}
var headers = blockchainInfo.Headers;
if (headers == 0 && Config.Network != Network.RegTest)
{
- throw new NotSupportedException("headers == 0");
+ throw new NotSupportedException($"{nameof(headers)} == 0");
}
if (blocks != headers)
@@ -204,16 +204,16 @@ namespace WalletWasabi.Backend
var generateBlocksResponse = await RpcClient.GenerateAsync(101);
if (generateBlocksResponse is null)
{
- throw new NotSupportedException("Bitcoin Core cannot generate blocks on the RegTest.");
+ throw new NotSupportedException($"Bitcoin Core cannot generate blocks on the {nameof(Network.RegTest)}.");
}
blockchainInfo = await RpcClient.GetBlockchainInfoAsync();
blocks = blockchainInfo.Blocks;
if (blocks == 0)
{
- throw new NotSupportedException("blocks == 0");
+ throw new NotSupportedException($"{nameof(blocks)} == 0");
}
- Logger.LogInfo<RPCClient>($"Generated 101 block on RegTest. Number of blocks {blocks}.");
+ Logger.LogInfo<RPCClient>($"Generated 101 block on {nameof(Network.RegTest)}. Number of blocks {blocks}.");
}
}
}

MODIFY
WalletWasabi.Backend/InitConfigStartupTask.cs
WalletWasabi.Backend/InitConfigStartupTask.cs
@@ -75,12 +75,12 @@ namespace WalletWasabi.Backend
private static void TaskScheduler_UnobservedTaskException(object sender, UnobservedTaskExceptionEventArgs e)
{
- Logger.LogWarning(e?.Exception, "UnobservedTaskException");
+ Logger.LogWarning(e?.Exception, $"{nameof(TaskScheduler.UnobservedTaskException)}");
}
private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
- Logger.LogWarning(e?.ExceptionObject as Exception, "UnhandledException");
+ Logger.LogWarning(e?.ExceptionObject as Exception, $"{nameof(AppDomain.CurrentDomain.UnhandledException)}");
}
private void Coordinator_CoinJoinBroadcasted(object sender, Transaction tx)

MODIFY
WalletWasabi.Backend/Startup.cs
WalletWasabi.Backend/Startup.cs
@@ -101,27 +101,27 @@ namespace WalletWasabi.Backend
private async Task CleanupAsync(Global global)
{
global.Coordinator?.Dispose();
- Logger.LogInfo<Startup>("Coordinator is disposed.");
+ Logger.LogInfo<Startup>($"{nameof(global.Coordinator)} is disposed.");
if (global.IndexBuilderService != null)
{
await global.IndexBuilderService.StopAsync();
- Logger.LogInfo<Startup>("IndexBuilderService is disposed.");
+ Logger.LogInfo<Startup>($"{nameof(global.IndexBuilderService)} is disposed.");
}
if (global.RoundConfigWatcher != null)
{
await global.RoundConfigWatcher.StopAsync();
- Logger.LogInfo<Startup>("RoundConfigWatcher is disposed.");
+ Logger.LogInfo<Startup>($"{nameof(global.RoundConfigWatcher)} is disposed.");
}
if (global.LocalNode != null)
{
global.DisconnectDisposeNullLocalNode();
- Logger.LogInfo<Startup>("LocalNode is disposed.");
+ Logger.LogInfo<Startup>($"{nameof(global.LocalNode)} is disposed.");
}
- Logger.LogInfo($"Wasabi Backend stopped gracefully.", Logger.InstanceGuid.ToString());
+ Logger.LogInfo("Wasabi Backend stopped gracefully.", Logger.InstanceGuid.ToString());
}
}
}

Use nameof instead of magic strings (Backend project)