Remove NodeBuilder
MODIFY
WalletWasabi.Tests/NodeBuilding/CoreNode.cs
WalletWasabi.Tests/NodeBuilding/CoreNode.cs
@@ -10,6 +10,7 @@ using System.IO;
using System.Linq;
using System.Net;
using System.Net.Sockets;
+using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
using WalletWasabi.Helpers;
@@ -18,21 +19,62 @@ namespace WalletWasabi.Tests.NodeBuilding
{
public class CoreNode
{
- public int P2pPort { get; }
+ public int P2pPort { get; private set; }
- public EndPoint P2pEndPoint { get; }
- public EndPoint RpcEndPoint { get; }
- public RPCClient RpcClient { get; }
- public string Config { get; }
+ public EndPoint P2pEndPoint { get; private set; }
+ public EndPoint RpcEndPoint { get; private set; }
+ public RPCClient RpcClient { get; private set; }
- public CoreNode(string dataDir)
+ private Process Process { get; set; }
+ private string DataDir { get; set; }
+
+ public static async Task<CoreNode> CreateAsync([CallerMemberName]string caller = null)
{
- DataDir = Guard.NotNullOrEmptyOrWhitespace(nameof(dataDir), dataDir);
- IoHelpers.EnsureDirectoryExists(DataDir);
+ var coreNode = new CoreNode();
+ coreNode.DataDir = Path.Combine(Global.Instance.DataDir, caller);
+
+ var configPath = Path.Combine(coreNode.DataDir, "bitcoin.conf");
+ if (File.Exists(configPath))
+ {
+ var foundConfig = await NodeConfigParameters.LoadAsync(configPath);
+ var rpcPortString = foundConfig["regtest.rpcport"];
+ var rpcUser = foundConfig["regtest.rpcuser"];
+ var rpcPassword = foundConfig["regtest.rpcpassword"];
+ var pidFileName = foundConfig["regtest.pid"];
+ var credentials = new NetworkCredential(rpcUser, rpcPassword);
+ try
+ {
+ var rpc = new RPCClient(credentials, new Uri("http://127.0.0.1:" + rpcPortString + "/"), Network.RegTest);
+ await rpc.StopAsync();
+
+ var pidFile = Path.Combine(coreNode.DataDir, "regtest", pidFileName);
+ if (File.Exists(pidFile))
+ {
+ var pid = await File.ReadAllTextAsync(pidFile);
+ using var process = Process.GetProcessById(int.Parse(pid));
+ await process.WaitForExitAsync(CancellationToken.None);
+ }
+ else
+ {
+ var allProcesses = Process.GetProcesses();
+ var bitcoindProcesses = allProcesses.Where(x => x.ProcessName.Contains("bitcoind"));
+ if (bitcoindProcesses.Count() == 1)
+ {
+ var bitcoind = bitcoindProcesses.First();
+ await bitcoind.WaitForExitAsync(CancellationToken.None);
+ }
+ }
+ }
+ catch (Exception)
+ {
+ }
+ }
+
+ await IoHelpers.DeleteRecursivelyWithMagicDustAsync(coreNode.DataDir);
+ IoHelpers.EnsureDirectoryExists(coreNode.DataDir);
var pass = Encoders.Hex.EncodeData(RandomUtils.GetBytes(20));
var creds = new NetworkCredential(pass, pass);
- Config = Path.Combine(DataDir, "bitcoin.conf");
var portArray = new int[2];
var i = 0;
@@ -58,30 +100,13 @@ namespace WalletWasabi.Tests.NodeBuilding
}
}
- P2pPort = portArray[0];
+ coreNode.P2pPort = portArray[0];
var rpcPort = portArray[1];
- P2pEndPoint = new IPEndPoint(IPAddress.Loopback, P2pPort);
- RpcEndPoint = new IPEndPoint(IPAddress.Loopback, rpcPort);
-
- RpcClient = new RPCClient($"{creds.UserName}:{creds.Password}", RpcEndPoint.ToString(rpcPort), Network.RegTest);
- }
-
- public async Task<IEnumerable<Block>> GenerateAsync(int blockCount)
- {
- var blocks = await RpcClient.GenerateAsync(blockCount);
- var rpc = RpcClient.PrepareBatch();
- var tasks = blocks.Select(b => rpc.GetBlockAsync(b));
- rpc.SendBatch();
- return await Task.WhenAll(tasks);
- }
+ coreNode.P2pEndPoint = new IPEndPoint(IPAddress.Loopback, coreNode.P2pPort);
+ coreNode.RpcEndPoint = new IPEndPoint(IPAddress.Loopback, rpcPort);
- public async Task<Node> CreateNodeClientAsync()
- {
- return await Node.ConnectAsync(Network.RegTest, P2pEndPoint);
- }
+ coreNode.RpcClient = new RPCClient($"{creds.UserName}:{creds.Password}", coreNode.RpcEndPoint.ToString(rpcPort), Network.RegTest);
- public async Task StartAsync()
- {
var config = new NodeConfigParameters
{
{"regtest", "1"},
@@ -89,44 +114,58 @@ namespace WalletWasabi.Tests.NodeBuilding
{"regtest.listenonion", "0"},
{"regtest.server", "1"},
{"regtest.txindex", "1"},
- {"regtest.rpcuser", RpcClient.CredentialString.UserPassword.UserName},
- {"regtest.rpcpassword", RpcClient.CredentialString.UserPassword.Password},
- {"regtest.whitebind", "127.0.0.1:" + P2pPort.ToString()},
- {"regtest.rpcport", RpcEndPoint.GetPortOrDefault().ToString()},
+ {"regtest.rpcuser", coreNode.RpcClient.CredentialString.UserPassword.UserName},
+ {"regtest.rpcpassword", coreNode.RpcClient.CredentialString.UserPassword.Password},
+ {"regtest.whitebind", "127.0.0.1:" + coreNode.P2pPort.ToString()},
+ {"regtest.rpcport", coreNode.RpcEndPoint.GetPortOrDefault().ToString()},
{"regtest.printtoconsole", "0"}, // Set it to one if do not mind loud debug logs
{"regtest.keypool", "10"},
{"regtest.pid", "bitcoind.pid"}
};
- await File.WriteAllTextAsync(Config, config.ToString());
- using (await KillerLock.LockAsync())
+
+ await File.WriteAllTextAsync(configPath, config.ToString());
+ using (await coreNode.KillerLock.LockAsync())
{
- Process = Process.Start(new FileInfo(EnvironmentHelpers.GetBinaryPath("BitcoinCore", "bitcoind")).FullName, "-conf=bitcoin.conf" + " -datadir=" + DataDir + " -debug=1");
- string pidFile = Path.Combine(DataDir, "regtest", "bitcoind.pid");
+ coreNode.Process = Process.Start(new FileInfo(EnvironmentHelpers.GetBinaryPath("BitcoinCore", "bitcoind")).FullName, "-conf=bitcoin.conf" + " -datadir=" + coreNode.DataDir + " -debug=1");
+ string pidFile = Path.Combine(coreNode.DataDir, "regtest", "bitcoind.pid");
if (!File.Exists(pidFile))
{
- Directory.CreateDirectory(Path.Combine(DataDir, "regtest"));
- await File.WriteAllTextAsync(pidFile, Process.Id.ToString());
+ Directory.CreateDirectory(Path.Combine(coreNode.DataDir, "regtest"));
+ await File.WriteAllTextAsync(pidFile, coreNode.Process.Id.ToString());
}
}
while (true)
{
try
{
- await RpcClient.GetBlockHashAsync(0);
+ await coreNode.RpcClient.GetBlockHashAsync(0);
break;
}
catch
{
}
- if (Process is null || Process.HasExited)
+ if (coreNode.Process is null || coreNode.Process.HasExited)
{
break;
}
}
+
+ return coreNode;
}
- private Process Process { get; set; }
- private string DataDir { get; }
+ public async Task<Node> CreateNodeClientAsync()
+ {
+ return await Node.ConnectAsync(Network.RegTest, P2pEndPoint);
+ }
+
+ public async Task<IEnumerable<Block>> GenerateAsync(int blockCount)
+ {
+ var blocks = await RpcClient.GenerateAsync(blockCount);
+ var rpc = RpcClient.PrepareBatch();
+ var tasks = blocks.Select(b => rpc.GetBlockAsync(b));
+ rpc.SendBatch();
+ return await Task.WhenAll(tasks);
+ }
private readonly AsyncLock KillerLock = new AsyncLock();

DELETE
WalletWasabi.Tests/NodeBuilding/NodeBuilder.cs
-using NBitcoin;
-using NBitcoin.RPC;
-using Nito.AsyncEx;
-using System;
-using System.Collections.Generic;
-using System.Diagnostics;
-using System.IO;
-using System.IO.Compression;
-using System.Linq;
-using System.Net;
-using System.Net.Http;
-using System.Runtime.CompilerServices;
-using System.Runtime.InteropServices;
-using System.Threading;
-using System.Threading.Tasks;
-using WalletWasabi.Helpers;
-using WalletWasabi.Logging;
-using WalletWasabi.Tests.XunitConfiguration;
-
-namespace WalletWasabi.Tests.NodeBuilding
-{
- public static class NodeBuilder
- {
- public static async Task<CoreNode> CreateNodeAsync([CallerMemberName]string caller = null)
- {
- var dataDir = Path.Combine(Global.Instance.DataDir, caller);
-
- var cfgPath = Path.Combine(dataDir, "bitcoin.conf");
- if (File.Exists(cfgPath))
- {
- var config = await NodeConfigParameters.LoadAsync(cfgPath);
- var rpcPort = config["regtest.rpcport"];
- var rpcUser = config["regtest.rpcuser"];
- var rpcPassword = config["regtest.rpcpassword"];
- var pidFileName = config["regtest.pid"];
- var credentials = new NetworkCredential(rpcUser, rpcPassword);
- try
- {
- var rpc = new RPCClient(credentials, new Uri("http://127.0.0.1:" + rpcPort + "/"), Network.RegTest);
- await rpc.StopAsync();
-
- var pidFile = Path.Combine(dataDir, "regtest", pidFileName);
- if (File.Exists(pidFile))
- {
- var pid = await File.ReadAllTextAsync(pidFile);
- using var process = Process.GetProcessById(int.Parse(pid));
- await process.WaitForExitAsync(CancellationToken.None);
- }
- else
- {
- var allProcesses = Process.GetProcesses();
- var bitcoindProcesses = allProcesses.Where(x => x.ProcessName.Contains("bitcoind"));
- if (bitcoindProcesses.Count() == 1)
- {
- var bitcoind = bitcoindProcesses.First();
- await bitcoind.WaitForExitAsync(CancellationToken.None);
- }
- }
- }
- catch (Exception)
- {
- }
- }
-
- await IoHelpers.DeleteRecursivelyWithMagicDustAsync(dataDir);
- var node = new CoreNode(dataDir);
- await node.StartAsync();
- return node;
- }
- }
-}

MODIFY
WalletWasabi.Tests/XunitConfiguration/RegTestFixture.cs
WalletWasabi.Tests/XunitConfiguration/RegTestFixture.cs
@@ -26,7 +26,7 @@ namespace WalletWasabi.Tests.XunitConfiguration
public RegTestFixture()
{
- BackendRegTestNode = NodeBuilder.CreateNodeAsync(EnvironmentHelpers.GetMethodName()).GetAwaiter().GetResult();
+ BackendRegTestNode = CoreNode.CreateAsync(EnvironmentHelpers.GetMethodName()).GetAwaiter().GetResult();
var testnetBackendDir = EnvironmentHelpers.GetDataDir(Path.Combine("WalletWasabi", "Tests", "Backend"));
IoHelpers.DeleteRecursivelyWithMagicDustAsync(testnetBackendDir).GetAwaiter().GetResult();

Remove NodeBuilder