Refactor configuration
MODIFY
WalletWasabi.Backend/Global.cs
WalletWasabi.Backend/Global.cs
@@ -11,6 +11,7 @@ using System.Net;
using System.Threading.Tasks;
using WalletWasabi.Crypto;
using System.Text;
+using WalletWasabi.ChaumianCoinJoin;
namespace WalletWasabi.Backend
{
@@ -43,26 +44,11 @@ namespace WalletWasabi.Backend
public static CcjRoundConfig RoundConfig { get; private set; }
- public async static Task InitializeAsync(Network network = null, string rpcuser = null, string rpcpassword = null, RPCClient rpc = null)
+ public async static Task InitializeAsync(Config config, CcjRoundConfig roundConfig, RPCClient rpc)
{
- _dataDir = null;
-
- var configFilePath = Path.Combine(DataDir, "Config.json");
- // Initialize Config
- if (network != null || rpcuser != null || rpcpassword != null)
- {
- Config = new Config(network, rpcuser, rpcpassword);
- Config.SetFilePath(configFilePath);
- }
- else
- {
- Config = new Config(configFilePath);
- await Config.LoadOrCreateDefaultFileAsync();
- }
-
- var roundConfigFilePath = Path.Combine(DataDir, "CcjRoundConfig.json");
- RoundConfig = new CcjRoundConfig(roundConfigFilePath);
- await RoundConfig.LoadOrCreateDefaultFileAsync();
+ Config = Guard.NotNull(nameof(config), config);
+ RoundConfig = Guard.NotNull(nameof(roundConfig), roundConfig);
+ RpcClient = Guard.NotNull(nameof(rpc), rpc);
// Initialize RsaKey
string rsaKeyPath = Path.Combine(DataDir, "RsaKey.json");
@@ -78,20 +64,6 @@ namespace WalletWasabi.Backend
Logger.LogInfo($"Created RSA key at: {rsaKeyPath}", nameof(Global));
}
- // Initialize RPC
- if (rpc != null)
- {
- RpcClient = rpc;
- }
- else
- {
- RpcClient = new RPCClient(
- credentials: new RPCCredentialString
- {
- UserPassword = new NetworkCredential(Config.BitcoinRpcUser, Config.BitcoinRpcPassword)
- },
- network: Config.Network);
- }
await AssertRpcNodeFullyInitializedAsync();
// Initialize index building
@@ -101,12 +73,15 @@ namespace WalletWasabi.Backend
IndexBuilderService = new IndexBuilderService(RpcClient, indexFilePath, utxoSetFilePath);
IndexBuilderService.Synchronize();
- Coordinator = new CcjCoordinator();
- await Coordinator.StartNewRoundAsync(RpcClient, RoundConfig.Denomination, (int)RoundConfig.ConfirmationTarget, (decimal)RoundConfig.CoordinatorFeePercent, (int)RoundConfig.AnonymitySet);
+ Coordinator = new CcjCoordinator(RpcClient);
+ await Coordinator.StartNewRoundAsync(roundConfig);
+ if (roundConfig.FilePath != null)
+ {
RoundConfigWatcher = new ConfigWatcher(RoundConfig);
RoundConfigWatcher.Start(TimeSpan.FromSeconds(10), OnConfigChangedAsync); // Every 10 seconds check the config
}
+ }
private static async Task OnConfigChangedAsync()
{
@@ -114,7 +89,7 @@ namespace WalletWasabi.Backend
{
Coordinator.FailAllRoundsInInputRegistration();
- await Coordinator.StartNewRoundAsync(RpcClient, RoundConfig.Denomination, (int)RoundConfig.ConfirmationTarget, (decimal)RoundConfig.CoordinatorFeePercent, (int)RoundConfig.AnonymitySet);
+ await Coordinator.StartNewRoundAsync(RoundConfig);
}
catch (Exception ex)
{

MODIFY
WalletWasabi.Backend/Program.cs
WalletWasabi.Backend/Program.cs
@@ -7,6 +7,9 @@ using WalletWasabi.Logging;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
+using NBitcoin.RPC;
+using System.Net;
+using WalletWasabi.ChaumianCoinJoin;
namespace WalletWasabi.Backend
{
@@ -22,7 +25,22 @@ namespace WalletWasabi.Backend
Logger.SetMinimumLevel(LogLevel.Info);
Logger.SetModes(LogMode.Debug, LogMode.Console, LogMode.File);
- await Global.InitializeAsync();
+ var configFilePath = Path.Combine(Global.DataDir, "Config.json");
+ var config = new Config(configFilePath);
+ await config.LoadOrCreateDefaultFileAsync();
+
+ var roundConfigFilePath = Path.Combine(Global.DataDir, "CcjRoundConfig.json");
+ var roundConfig = new CcjRoundConfig(roundConfigFilePath);
+ await roundConfig.LoadOrCreateDefaultFileAsync();
+
+ var rpc = new RPCClient(
+ credentials: new RPCCredentialString
+ {
+ UserPassword = new NetworkCredential(config.BitcoinRpcUser, config.BitcoinRpcPassword)
+ },
+ network: config.Network);
+
+ await Global.InitializeAsync(config, roundConfig, rpc);
var endPoint = "http://localhost:37127/";

MODIFY
WalletWasabi.Tests/RegTestFixture.cs
WalletWasabi.Tests/RegTestFixture.cs
@@ -7,6 +7,8 @@ using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
+using NBitcoin;
+using WalletWasabi.ChaumianCoinJoin;
namespace WalletWasabi.Tests
{
@@ -30,7 +32,12 @@ namespace WalletWasabi.Tests
var rpc = BackendRegTestNode.CreateRpcClient();
var authString = rpc.Authentication.Split(':');
- Global.InitializeAsync(rpc.Network, authString[0], authString[1], rpc).GetAwaiter().GetResult();
+
+ var config = new Config(rpc.Network, authString[0], authString[1]);
+
+ var roundConfig = new CcjRoundConfig(new Money(0.1m, MoneyUnit.BTC), 144, 0.1m, 100);
+
+ Global.InitializeAsync(config, roundConfig, rpc).GetAwaiter().GetResult();
BackendEndPoint = $"http://localhost:{new Random().Next(37130, 38000)}/";
BackendHost = WebHost.CreateDefaultBuilder()

MODIFY
WalletWasabi/ChaumianCoinJoin/CcjRound.cs
WalletWasabi/ChaumianCoinJoin/CcjRound.cs
@@ -41,15 +41,17 @@ namespace WalletWasabi.ChaumianCoinJoin
public CcjRoundStatus Status { get; private set; }
- public CcjRound(RPCClient rpc, Money denomination, int confirmationTarget, decimal coordinatorFeePercent, int anonymitySet)
+ public CcjRound(RPCClient rpc, CcjRoundConfig config)
{
Interlocked.Increment(ref RoundCount);
RpcClient = Guard.NotNull(nameof(rpc), rpc);
- Denomination = Guard.NotNull(nameof(denomination), denomination);
- ConfirmationTarget = confirmationTarget;
- CoordinatorFee = new Money((coordinatorFeePercent * 0.01m) * decimal.Parse(Denomination.ToString(false, true)), MoneyUnit.BTC);
- AnonymitySet = anonymitySet;
+ Guard.NotNull(nameof(config), config);
+
+ Denomination = config.Denomination;
+ ConfirmationTarget = (int)config.ConfirmationTarget;
+ CoordinatorFee = new Money(((decimal)config.CoordinatorFeePercent * 0.01m) * decimal.Parse(Denomination.ToString(false, true)), MoneyUnit.BTC);
+ AnonymitySet = (int)config.AnonymitySet;
Phase = CcjRoundPhase.InputRegistration;
Status = CcjRoundStatus.NotStarted;

MODIFY
WalletWasabi/Services/CcjCoordinator.cs
WalletWasabi/Services/CcjCoordinator.cs
@@ -8,6 +8,7 @@ using System.Text;
using System.Threading;
using System.Threading.Tasks;
using WalletWasabi.ChaumianCoinJoin;
+using WalletWasabi.Helpers;
using WalletWasabi.Logging;
namespace WalletWasabi.Services
@@ -17,13 +18,17 @@ namespace WalletWasabi.Services
private List<CcjRound> Rounds { get; }
private AsyncLock RoundsListLock { get; }
- public CcjCoordinator()
+ public RPCClient RpcClient { get; }
+
+ public CcjCoordinator(RPCClient rpc)
{
+ RpcClient = Guard.NotNull(nameof(rpc), rpc);
+
Rounds = new List<CcjRound>();
RoundsListLock = new AsyncLock();
}
- public async Task StartNewRoundAsync(RPCClient rpc, Money denomination, int confirmationTarget, decimal coordinatorFeePercent, int anonymitySet)
+ public async Task StartNewRoundAsync(CcjRoundConfig roundConfig)
{
using (await RoundsListLock.LockAsync())
{
@@ -32,7 +37,7 @@ namespace WalletWasabi.Services
throw new InvalidOperationException("Maximum two concurrently running round is allowed the same time.");
}
- var round = new CcjRound(rpc, denomination, confirmationTarget, coordinatorFeePercent, anonymitySet);
+ var round = new CcjRound(RpcClient, roundConfig);
await round.ExecuteNextPhaseAsync();
Rounds.Add(round);
}

Refactor configuration