Skip to content

Commit

Permalink
Merge branch 'master' into pr/12888
Browse files Browse the repository at this point in the history
  • Loading branch information
RolandUI committed May 22, 2024
2 parents 9a192cc + ede4ac5 commit c6762ed
Show file tree
Hide file tree
Showing 283 changed files with 1,534 additions and 943 deletions.
5 changes: 5 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,11 @@ dotnet_diagnostic.CA1847.severity = warning
# CA1859: Use concrete types when possible for improved performance
dotnet_diagnostic.CA1859.severity = none

# CA1865-CA1867: Use 'string.Method(char)' instead of 'string.Method(string)' for string with single char
dotnet_diagnostic.CA1865.severity = warning
dotnet_diagnostic.CA1866.severity = warning
dotnet_diagnostic.CA1867.severity = warning

# CA1868: Unnecessary call to Set.Contains(item)
dotnet_diagnostic.CA1868.severity = warning

Expand Down
2 changes: 1 addition & 1 deletion WalletWasabi.Backend/Controllers/BatchController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public async Task<IActionResult> GetSynchronizeAsync(
var numberOfFilters = Global.Config.Network == Network.Main ? 1000 : 10000;
(Height bestHeight, IEnumerable<FilterModel> filters) = Global.IndexBuilderService.GetFilterLinesExcluding(knownHash, numberOfFilters, out bool found);

var response = new SynchronizeResponse { Filters = Enumerable.Empty<FilterModel>(), BestHeight = bestHeight };
var response = new SynchronizeResponse { Filters = [], BestHeight = bestHeight };

if (!found)
{
Expand Down
8 changes: 0 additions & 8 deletions WalletWasabi.Backend/Controllers/BlockchainController.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,12 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.Memory;
using NBitcoin;
using NBitcoin.Protocol.Payloads;
using NBitcoin.RPC;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Net.Cache;
using System.Threading;
using System.Threading.Tasks;
using WalletWasabi.Backend.Models;
using WalletWasabi.Backend.Models.Responses;
using WalletWasabi.BitcoinCore.Mempool;
using WalletWasabi.BitcoinCore.Rpc;
Expand Down
1 change: 0 additions & 1 deletion WalletWasabi.Backend/Filters/ExceptionTranslateFilter.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System.ComponentModel;
using System.Net;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
Expand Down
66 changes: 59 additions & 7 deletions WalletWasabi.Daemon/Config.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using Microsoft.Extensions.Logging;
using NBitcoin;
using System;
using System.Collections;
Expand Down Expand Up @@ -58,7 +57,7 @@ [ nameof(RegTestCoordinatorUri)] = (
GetNullableStringValue("RegTestCoordinatorUri", PersistentConfig.RegTestCoordinatorUri, cliArgs)),
[ nameof(UseTor)] = (
"All the communications go through the Tor network",
GetBoolValue("UseTor", PersistentConfig.UseTor, cliArgs)),
GetTorModeValue("UseTor", PersistentConfig.UseTor, cliArgs)),
[ nameof(TorFolder)] = (
"Folder where Tor binary is located",
GetNullableStringValue("TorFolder", null, cliArgs)),
Expand Down Expand Up @@ -131,12 +130,18 @@ [ nameof(CoordinatorIdentifier)] = (
};

// Check if any config value is overridden (either by an environment value, or by a CLI argument).
foreach ((_, IValue optionValue) in Data.Values)
foreach (string optionName in Data.Keys)
{
if (optionValue.Overridden)
// It is allowed to override the log level.
if (!string.Equals(optionName, nameof(LogLevel)))
{
IsOverridden = true;
break;
(_, IValue optionValue) = Data[optionName];

if (optionValue.Overridden)
{
IsOverridden = true;
break;
}
}
}

Expand All @@ -154,7 +159,7 @@ [ nameof(CoordinatorIdentifier)] = (
public string? MainNetCoordinatorUri => GetEffectiveValue<NullableStringValue, string?>(nameof(MainNetCoordinatorUri));
public string? TestNetCoordinatorUri => GetEffectiveValue<NullableStringValue, string?>(nameof(TestNetCoordinatorUri));
public string? RegTestCoordinatorUri => GetEffectiveValue<NullableStringValue, string?>(nameof(RegTestCoordinatorUri));
public bool UseTor => GetEffectiveValue<BoolValue, bool>(nameof(UseTor)) && Network != Network.RegTest;
public TorMode UseTor => Network == Network.RegTest ? TorMode.Disabled : GetEffectiveValue<TorModeValue, TorMode>(nameof(UseTor));
public string? TorFolder => GetEffectiveValue<NullableStringValue, string?>(nameof(TorFolder));
public int TorSocksPort => GetEffectiveValue<IntValue, int>(nameof(TorSocksPort));
public int TorControlPort => GetEffectiveValue<IntValue, int>(nameof(TorControlPort));
Expand Down Expand Up @@ -186,6 +191,12 @@ [ nameof(CoordinatorIdentifier)] = (
EnvironmentHelpers.GetDataDir(Path.Combine("WalletWasabi", "Client")),
Environment.GetCommandLineArgs()).EffectiveValue;

/// <summary>Whether a config option was overridden by a command line argument or an environment variable.</summary>
/// <remarks>
/// Changing config options in the UI while a config option is overridden would bring uncertainty if user understands consequences or not,
/// thus it is normally not allowed. However, there are exceptions as what options are taken into account, there is currently
/// one exception: <see cref="LogLevel"/>.
/// </remarks>
public bool IsOverridden { get; }

public EndPoint GetBitcoinP2pEndPoint()
Expand Down Expand Up @@ -369,6 +380,46 @@ private static LogModeArrayValue GetLogModeArrayValue(string key, LogMode[] arra
return new LogModeArrayValue(arrayValues, arrayValues, ValueSource.Disk);
}

private static TorModeValue GetTorModeValue(string key, object value, string[] cliArgs)
{
TorMode computedValue;

string? stringValue = value.ToString();

if (stringValue is null)
{
throw new ArgumentException($"Could not convert '{value}' to a string value.");
}
else if (stringValue.Equals("true", StringComparison.OrdinalIgnoreCase))
{
computedValue = TorMode.Enabled;
}
else if (stringValue.Equals("false", StringComparison.OrdinalIgnoreCase))
{
computedValue = TorMode.Disabled;
}
else if (Enum.TryParse(stringValue, out TorMode parsedTorMode))
{
computedValue = parsedTorMode;
}
else
{
throw new ArgumentException($"Could not convert '{value}' to a valid {nameof(TorMode)} value.");
}

if (GetOverrideValue(key, cliArgs, out string? overrideValue, out ValueSource? valueSource))
{
if (!Enum.TryParse(overrideValue, out TorMode parsedOverrideValue))
{
throw new ArgumentException($"Could not convert overridden value '{overrideValue}' to a valid {nameof(TorMode)} value.");
}

return new TorModeValue(computedValue, parsedOverrideValue, valueSource.Value);
}

return new TorModeValue(computedValue, computedValue, ValueSource.Disk);
}

private static bool GetOverrideValue(string key, string[] cliArgs, [NotNullWhen(true)] out string? overrideValue, [NotNullWhen(true)] out ValueSource? valueSource)
{
// CLI arguments have higher precedence than environment variables.
Expand Down Expand Up @@ -460,6 +511,7 @@ private record StringValue(string Value, string EffectiveValue, ValueSource Valu
private record NullableStringValue(string? Value, string? EffectiveValue, ValueSource ValueSource) : ITypedValue<string?>;
private record StringArrayValue(string[] Value, string[] EffectiveValue, ValueSource ValueSource) : ITypedValue<string[]>;
private record LogModeArrayValue(LogMode[] Value, LogMode[] EffectiveValue, ValueSource ValueSource) : ITypedValue<LogMode[]>;
private record TorModeValue(TorMode Value, TorMode EffectiveValue, ValueSource ValueSource) : ITypedValue<TorMode>;
private record NetworkValue(Network Value, Network EffectiveValue, ValueSource ValueSource) : ITypedValue<Network>;
private record MoneyValue(Money Value, Money EffectiveValue, ValueSource ValueSource) : ITypedValue<Money>;
private record EndPointValue(EndPoint Value, EndPoint EffectiveValue, ValueSource ValueSource) : ITypedValue<EndPoint>;
Expand Down
20 changes: 14 additions & 6 deletions WalletWasabi.Daemon/Global.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
using WalletWasabi.WebClients.BuyAnything;
using WalletWasabi.WebClients.ShopWare;
using WalletWasabi.Wallets.FilterProcessor;
using WalletWasabi.Models;

namespace WalletWasabi.Daemon;

Expand All @@ -54,7 +55,8 @@ public Global(string dataDir, string configFilePath, Config config)
TorSettings = new TorSettings(
DataDir,
distributionFolderPath: EnvironmentHelpers.GetFullBaseDirectory(),
Config.TerminateTorOnExit,
terminateOnExit: Config.TerminateTorOnExit,
torMode: Config.UseTor,
socksPort: config.TorSocksPort,
controlPort: config.TorControlPort,
torFolder: config.TorFolder,
Expand Down Expand Up @@ -102,7 +104,7 @@ public Global(string dataDir, string configFilePath, Config config)
var p2p = new P2pNetwork(
Network,
Config.GetBitcoinP2pEndPoint(),
Config.UseTor ? TorSettings.SocksEndpoint : null,
Config.UseTor != TorMode.Disabled ? TorSettings.SocksEndpoint : null,
Path.Combine(DataDir, "BitcoinP2pNetwork"),
BitcoinStore);
if (!Config.BlockOnlyMode)
Expand Down Expand Up @@ -185,8 +187,9 @@ public Global(string dataDir, string configFilePath, Config config)

private WasabiHttpClientFactory BuildHttpClientFactory(Func<Uri> backendUriGetter) =>
new(
Config.UseTor ? TorSettings.SocksEndpoint : null,
backendUriGetter);
Config.UseTor != TorMode.Disabled ? TorSettings.SocksEndpoint : null,
backendUriGetter,
torControlAvailable: Config.UseTor == TorMode.Enabled);

public async Task InitializeNoWalletAsync(bool initializeSleepInhibitor, TerminateService terminateService, CancellationToken cancellationToken)
{
Expand Down Expand Up @@ -311,7 +314,7 @@ private async Task StartRpcServerAsync(TerminateService terminateService, Cancel

private async Task StartTorProcessManagerAsync(CancellationToken cancellationToken)
{
if (Config.UseTor && Network != Network.RegTest)
if (Config.UseTor != TorMode.Disabled)
{
TorManager = new TorProcessManager(TorSettings);
await TorManager.StartAsync(attempts: 3, cancellationToken).ConfigureAwait(false);
Expand All @@ -333,7 +336,12 @@ private async Task StartTorProcessManagerAsync(CancellationToken cancellationTok
}
}

HostedServices.Register<TorMonitor>(() => new TorMonitor(period: TimeSpan.FromMinutes(1), torProcessManager: TorManager, httpClientFactory: HttpClientFactory), nameof(TorMonitor));
// Do not monitor Tor when Tor is an already running service.
if (TorSettings.TorMode == TorMode.Enabled)
{
HostedServices.Register<TorMonitor>(() => new TorMonitor(period: TimeSpan.FromMinutes(1), torProcessManager: TorManager, httpClientFactory: HttpClientFactory), nameof(TorMonitor));
}

HostedServices.Register<TorStatusChecker>(() => TorStatusChecker, "Tor Network Checker");
}
}
Expand Down
12 changes: 9 additions & 3 deletions WalletWasabi.Daemon/PersistentConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,13 @@ public record PersistentConfig : IConfigNg
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public string? RegTestCoordinatorUri { get; init; }

[DefaultValue(true)]
/// <remarks>
/// For backward compatibility this was changed to an object.
/// Only strings (new) and booleans (old) are supported.
/// </remarks>
[DefaultValue("Enabled")]
[JsonPropertyName("UseTor")]
public bool UseTor { get; init; } = true;
public object UseTor { get; init; } = "Enabled";

[DefaultValue(false)]
[JsonPropertyName("TerminateTorOnExit")]
Expand Down Expand Up @@ -116,6 +120,8 @@ public record PersistentConfig : IConfigNg

public bool DeepEquals(PersistentConfig other)
{
bool useTorIsEqual = UseTor.ToString() == other.UseTor.ToString();

return
Network == other.Network &&
MainNetBackendUri == other.MainNetBackendUri &&
Expand All @@ -124,7 +130,7 @@ public bool DeepEquals(PersistentConfig other)
MainNetCoordinatorUri == other.MainNetCoordinatorUri &&
TestNetCoordinatorUri == other.TestNetCoordinatorUri &&
RegTestCoordinatorUri == other.RegTestCoordinatorUri &&
UseTor == other.UseTor &&
useTorIsEqual &&
TerminateTorOnExit == other.TerminateTorOnExit &&
DownloadNewVersion == other.DownloadNewVersion &&
StartLocalBitcoinCoreOnStartup == other.StartLocalBitcoinCoreOnStartup &&
Expand Down
9 changes: 0 additions & 9 deletions WalletWasabi.Daemon/Program.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,8 @@
using System;
using WalletWasabi.Helpers;
using WalletWasabi.Logging;
using WalletWasabi.Services;
using WalletWasabi.Services.Terminate;
using WalletWasabi.Wallets;
using System.Net.Sockets;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using NBitcoin;
using LogLevel = WalletWasabi.Logging.LogLevel;

namespace WalletWasabi.Daemon;

Expand Down
2 changes: 0 additions & 2 deletions WalletWasabi.Daemon/Rpc/WasabiJsonRpcService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -495,8 +495,6 @@ public void StartCoinjoinSweeping(string? password = null, string? outputWalletN

private async Task StartCoinjoinSweepAsync(Wallet activeWallet, Wallet outputWallet)
{
activeWallet.ConsolidationMode = true;

// If output wallet isn't initialized, then load it.
if (outputWallet.State == WalletState.Uninitialized)
{
Expand Down
1 change: 0 additions & 1 deletion WalletWasabi.Daemon/WasabiAppBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ void ProcessCommands()
ProcessCommands();
await app.TerminateService.ForcefulTerminationRequestedTask.ConfigureAwait(false);
}
}).ConfigureAwait(false);
}
}
Loading

0 comments on commit c6762ed

Please sign in to comment.