Skip to content

Commit

Permalink
[refactor] Client Config: Pass path around to prepare for extractio…
Browse files Browse the repository at this point in the history
…n from `Config` (WalletWasabi#11860)

* `MoneyBtcJsonConverter`: Simplify
* Pass config path better
* `ConfigWatcher`: Remove `Guard.NotNull` as it is not needed
  • Loading branch information
kiminuo authored Nov 4, 2023
1 parent a59f3b1 commit 5a26918
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 18 deletions.
4 changes: 3 additions & 1 deletion WalletWasabi.Daemon/Global.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,10 @@ public class Global
/// <remarks>Use this variable as a guard to prevent touching <see cref="StoppingCts"/> that might have already been disposed.</remarks>
private volatile bool _disposeRequested;

public Global(string dataDir, Config config)
public Global(string dataDir, string configFilePath, Config config)
{
DataDir = dataDir;
ConfigFilePath = configFilePath;
Config = config;
TorSettings = new TorSettings(DataDir, distributionFolderPath: EnvironmentHelpers.GetFullBaseDirectory(), Config.TerminateTorOnExit, Environment.ProcessId);

Expand Down Expand Up @@ -131,6 +132,7 @@ public Global(string dataDir, Config config)
public WasabiHttpClientFactory CoordinatorHttpClientFactory { get; }

public LegalChecker LegalChecker { get; private set; }
public string ConfigFilePath { get; }
public Config Config { get; }
public WasabiSynchronizer Synchronizer { get; private set; }
public WalletManager WalletManager { get; }
Expand Down
12 changes: 7 additions & 5 deletions WalletWasabi.Daemon/WasabiAppBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
Expand All @@ -22,14 +21,19 @@ public class WasabiApplication
{
public WasabiAppBuilder AppConfig { get; }
public Global? Global { get; private set; }
public string ConfigFilePath { get; }
public Config Config { get; }
public SingleInstanceChecker SingleInstanceChecker { get; }
public TerminateService TerminateService { get; }

public WasabiApplication(WasabiAppBuilder wasabiAppBuilder)
{
AppConfig = wasabiAppBuilder;

ConfigFilePath = Path.Combine(Config.DataDir, "Config.json");
Directory.CreateDirectory(Config.DataDir);
Config = new Config(LoadOrCreateConfigs(), wasabiAppBuilder.Arguments);

SetupLogger();
Logger.LogDebug($"Wasabi was started with these argument(s): {string.Join(" ", AppConfig.Arguments.DefaultIfEmpty("none"))}.");
SingleInstanceChecker = new(Config.Network);
Expand Down Expand Up @@ -101,13 +105,11 @@ private void BeforeStopping()
}

private Global CreateGlobal()
=> new(Config.DataDir, Config);
=> new(Config.DataDir, ConfigFilePath, Config);

private PersistentConfig LoadOrCreateConfigs()
{
Directory.CreateDirectory(Config.DataDir);

PersistentConfig persistentConfig = new(Path.Combine(Config.DataDir, "Config.json"));
PersistentConfig persistentConfig = new(ConfigFilePath);
persistentConfig.LoadFile(createIfMissing: true);

if (persistentConfig.MigrateOldDefaultBackendUris())
Expand Down
2 changes: 1 addition & 1 deletion WalletWasabi.Fluent/App.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ private static IClientConfig CreateConfig()

private static IApplicationSettings CreateApplicationSettings()
{
return new ApplicationSettings(Services.PersistentConfig, Services.Config, Services.UiConfig);
return new ApplicationSettings(Services.PersistentConfigFilePath, Services.PersistentConfig, Services.Config, Services.UiConfig);
}

private static ITransactionBroadcasterModel CreateBroadcaster(Network network)
Expand Down
2 changes: 1 addition & 1 deletion WalletWasabi.Fluent/Models/ClientConfig/ClientConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class ClientConfigModel : IClientConfig

public string WalletsBackupDir => Services.WalletManager.WalletDirectories.WalletsBackupDir;

public string ConfigFilePath => Services.PersistentConfig.FilePath;
public string ConfigFilePath => Services.PersistentConfigFilePath;

public string TorLogFilePath => Services.TorSettings.LogFilePath;

Expand Down
8 changes: 5 additions & 3 deletions WalletWasabi.Fluent/Models/UI/ApplicationSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public partial class ApplicationSettings : ReactiveObject
private static readonly object ConfigLock = new();

private readonly Subject<bool> _isRestartNeeded = new();
private readonly string _persistentConfigFilePath;
private readonly PersistentConfig _startupConfig;
private readonly PersistentConfig _persistentConfig;
private readonly Config _config;
Expand Down Expand Up @@ -61,9 +62,10 @@ public partial class ApplicationSettings : ReactiveObject
// Non-persistent
[AutoNotify] private bool _doUpdateOnClose;

public ApplicationSettings(PersistentConfig persistentConfig, Config config, UiConfig uiConfig)
public ApplicationSettings(string persistentConfigFilePath, PersistentConfig persistentConfig, Config config, UiConfig uiConfig)
{
_startupConfig = new PersistentConfig(persistentConfig.FilePath);
_persistentConfigFilePath = persistentConfigFilePath;
_startupConfig = new PersistentConfig(persistentConfigFilePath);
_startupConfig.LoadFile();

_persistentConfig = persistentConfig;
Expand Down Expand Up @@ -149,7 +151,7 @@ public ApplicationSettings(PersistentConfig persistentConfig, Config config, UiC
.Where(value => value && string.IsNullOrEmpty(LocalBitcoinCoreDataDir))
.Subscribe(_ => LocalBitcoinCoreDataDir = EnvironmentHelpers.GetDefaultBitcoinCoreDataDirOrEmptyString());

// Apply RunOnSystenStartup
// Apply RunOnSystemStartup
this.WhenAnyValue(x => x.RunOnSystemStartup)
.DoAsync(async _ => await StartupHelper.ModifyStartupSettingAsync(RunOnSystemStartup))
.Subscribe();
Expand Down
3 changes: 3 additions & 0 deletions WalletWasabi.Fluent/Services.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ public static class Services

public static LegalChecker LegalChecker { get; private set; } = null!;

public static string PersistentConfigFilePath { get; private set; } = null!;

public static PersistentConfig PersistentConfig { get; private set; } = null!;

public static WasabiSynchronizer Synchronizer { get; private set; } = null!;
Expand Down Expand Up @@ -72,6 +74,7 @@ public static void Initialize(Global global, UiConfig uiConfig, SingleInstanceCh
BitcoinStore = global.BitcoinStore;
HttpClientFactory = global.HttpClientFactory;
LegalChecker = global.LegalChecker;
PersistentConfigFilePath = global.ConfigFilePath;
PersistentConfig = global.Config.PersistentConfig;
Synchronizer = global.Synchronizer;
WalletManager = global.WalletManager;
Expand Down
6 changes: 2 additions & 4 deletions WalletWasabi/JsonConverters/Bitcoin/MoneyBtcJsonConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,8 @@ public class MoneyBtcJsonConverter : JsonConverter<Money>
{
return null;
}
else
{
return Money.Parse(stringValue);
}

return Money.Parse(stringValue);
}

/// <inheritdoc />
Expand Down
5 changes: 2 additions & 3 deletions WalletWasabi/Services/ConfigWatcher.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System.Threading;
using System.Threading.Tasks;
using WalletWasabi.Bases;
using WalletWasabi.Helpers;
using WalletWasabi.Interfaces;

namespace WalletWasabi.Services;
Expand All @@ -10,8 +9,8 @@ public class ConfigWatcher : PeriodicRunner
{
public ConfigWatcher(TimeSpan period, IConfig config, Action executeWhenChanged) : base(period)
{
Config = Guard.NotNull(nameof(config), config);
ExecuteWhenChanged = Guard.NotNull(nameof(executeWhenChanged), executeWhenChanged);
Config = config;
ExecuteWhenChanged = executeWhenChanged;
config.AssertFilePathSet();
}

Expand Down

0 comments on commit 5a26918

Please sign in to comment.