Refactor CoreNode
MODIFY
WalletWasabi.Tests/NodeBuilding/CoreNode.cs
WalletWasabi.Tests/NodeBuilding/CoreNode.cs
@@ -18,25 +18,17 @@ namespace WalletWasabi.Tests.NodeBuilding
private readonly NodeBuilder _Builder;
public string Folder { get; }
- public IPEndPoint Endpoint
- {
- get
- {
- return new IPEndPoint(IPAddress.Parse("127.0.0.1"), _ports[0]);
- }
- }
+ public IPEndPoint Endpoint => new IPEndPoint(IPAddress.Parse("127.0.0.1"), _ports[0]);
public string Config { get; }
public NodeConfigParameters ConfigParameters { get; } = new NodeConfigParameters();
- public CoreNode(string folder, NodeBuilder builder)
+ private CoreNode(string folder, NodeBuilder builder)
{
_Builder = builder;
Folder = folder;
State = CoreNodeState.Stopped;
- CleanFolderAsync().GetAwaiter().GetResult();
- Directory.CreateDirectory(folder);
DataDir = Path.Combine(folder, "data");
Directory.CreateDirectory(DataDir);
var pass = Encoders.Hex.EncodeData(RandomUtils.GetBytes(20));
@@ -47,38 +39,18 @@ namespace WalletWasabi.Tests.NodeBuilding
FindPorts(_ports);
}
- private async Task CleanFolderAsync()
+ public static async Task<CoreNode> CreateAsync(string folder, NodeBuilder builder)
{
- await IoHelpers.DeleteRecursivelyWithMagicDustAsync(Folder);
- }
+ await IoHelpers.DeleteRecursivelyWithMagicDustAsync(folder);
+ Directory.CreateDirectory(folder);
- public async Task SyncAsync(CoreNode node, bool keepConnection = false)
- {
- var rpc = CreateRpcClient();
- var rpc1 = node.CreateRpcClient();
- rpc.AddNode(node.Endpoint, true);
- while (await rpc.GetBestBlockHashAsync() != await rpc1.GetBestBlockHashAsync())
- {
- await Task.Delay(200);
- }
- if (!keepConnection)
- {
- rpc.RemoveNode(node.Endpoint);
- }
+ return new CoreNode(folder, builder);
}
public CoreNodeState State { get; private set; }
private readonly int[] _ports;
- public int ProtocolPort
- {
- get
- {
- return _ports[0];
- }
- }
-
private readonly NetworkCredential Creds;
public RPCClient CreateRpcClient()
@@ -86,11 +58,6 @@ namespace WalletWasabi.Tests.NodeBuilding
return new RPCClient(Creds, new Uri("http://127.0.0.1:" + _ports[1] + "/"), Network.RegTest);
}
- public RestClient CreateRESTClient()
- {
- return new RestClient(new Uri("http://127.0.0.1:" + _ports[1] + "/"));
- }
-
public Node CreateNodeClient()
{
return Node.Connect(Network.RegTest, new IPEndPoint(IPAddress.Loopback, _ports[0]));
@@ -168,81 +135,6 @@ namespace WalletWasabi.Tests.NodeBuilding
private List<Transaction> _transactions = new List<Transaction>();
private HashSet<OutPoint> _locked = new HashSet<OutPoint>();
- private readonly Money _fee = Money.Coins(0.0001m);
-
- public Transaction GiveMoney(Script destination, Money amount, bool broadcast = true)
- {
- var rpc = CreateRpcClient();
- var builder = new TransactionBuilder();
- builder.AddKeys(rpc.ListSecrets().OfType<ISecret>().ToArray());
- builder.AddCoins(rpc.ListUnspent().Where(c => !_locked.Contains(c.OutPoint)).Select(c => c.AsCoin()));
- builder.Send(destination, amount);
- builder.SendFees(_fee);
- builder.SetChange(GetFirstSecret(rpc));
- var tx = builder.BuildTransaction(true);
- foreach (var outpoint in tx.Inputs.Select(i => i.PrevOut))
- {
- _locked.Add(outpoint);
- }
- if (broadcast)
- Broadcast(tx);
- else
- _transactions.Add(tx);
- return tx;
- }
-
- public void Rollback(Transaction tx)
- {
- _transactions.Remove(tx);
- foreach (var outpoint in tx.Inputs.Select(i => i.PrevOut))
- {
- _locked.Remove(outpoint);
- }
- }
-
- public void Broadcast(Transaction transaction)
- {
- using (var node = CreateNodeClient())
- {
- node.VersionHandshake();
- node.SendMessageAsync(new InvPayload(transaction));
- node.SendMessageAsync(new TxPayload(transaction));
- node.PingPong();
- }
- }
-
- public void SelectMempoolTransactions()
- {
- var rpc = CreateRpcClient();
- var txs = rpc.GetRawMempool();
-
- var tasks = txs.Select(t => rpc.GetRawTransactionAsync(t)).ToArray();
- Task.WaitAll(tasks);
- _transactions.AddRange(tasks.Select(t => t.GetAwaiter().GetResult()).ToArray());
- }
-
- public void Broadcast(Transaction[] txs)
- {
- foreach (var tx in txs)
- Broadcast(tx);
- }
-
- public void Split(Money amount, int parts)
- {
- var rpc = CreateRpcClient();
- var builder = new TransactionBuilder();
- builder.AddKeys(rpc.ListSecrets().OfType<ISecret>().ToArray());
- builder.AddCoins(rpc.ListUnspent().Select(c => c.AsCoin()));
- var secret = GetFirstSecret(rpc);
- foreach (var part in (amount - _fee).Split(parts))
- {
- builder.Send(secret, part);
- }
- builder.SendFees(_fee);
- builder.SetChange(secret);
- var tx = builder.BuildTransaction(true);
- Broadcast(tx);
- }
private readonly object _l = new object();
@@ -257,26 +149,10 @@ namespace WalletWasabi.Tests.NodeBuilding
}
State = CoreNodeState.Killed;
if (cleanFolder)
- CleanFolderAsync().GetAwaiter().GetResult();
- }
- }
-
- public DateTimeOffset? MockTime
{
- get;
- set;
+ IoHelpers.DeleteRecursivelyWithMagicDustAsync(Folder).GetAwaiter().GetResult();
}
-
- public void SetMinerSecret(BitcoinSecret secret)
- {
- CreateRpcClient().ImportPrivKey(secret);
- MinerSecret = secret;
}
-
- public BitcoinSecret MinerSecret
- {
- get;
- private set;
}
public Block[] Generate(int blockCount)
@@ -289,13 +165,6 @@ namespace WalletWasabi.Tests.NodeBuilding
return tasks.Select(b => b.GetAwaiter().GetResult()).ToArray();
}
- private List<uint256> _toMalleate = new List<uint256>();
-
- public void Malleate(uint256 txId)
- {
- _toMalleate.Add(txId);
- }
-
public void BroadcastBlocks(IEnumerable<Block> blocks)
{
using (var node = CreateNodeClient())
@@ -314,41 +183,5 @@ namespace WalletWasabi.Tests.NodeBuilding
}
node.PingPong();
}
-
- public void MineBlock(Block block)
- {
- block.UpdateMerkleRoot();
- uint nonce = 0;
- while (!block.CheckProofOfWork())
- {
- block.Header.Nonce = ++nonce;
- }
- }
-
- private class TransactionNode
- {
- public TransactionNode(Transaction tx)
- {
- Transaction = tx;
- Hash = tx.GetHash();
- }
-
- public uint256 Hash = null;
- public Transaction Transaction = null;
- public List<TransactionNode> DependsOn = new List<TransactionNode>();
- }
-
- private BitcoinSecret GetFirstSecret(RPCClient rpc)
- {
- if (!(MinerSecret is null))
- return MinerSecret;
- var dest = rpc.ListSecrets().FirstOrDefault();
- if (dest is null)
- {
- var address = rpc.GetNewAddress();
- dest = rpc.DumpPrivKey(address);
- }
- return dest;
- }
}
}

MODIFY
WalletWasabi.Tests/NodeBuilding/NodeBuilder.cs
WalletWasabi.Tests/NodeBuilding/NodeBuilder.cs
@@ -133,7 +133,7 @@ namespace WalletWasabi.Tests.NodeBuilding
catch (DirectoryNotFoundException)
{
}
- var node = new CoreNode(child, this);
+ var node = await CoreNode.CreateAsync(child, this);
Nodes.Add(node);
if (start)
{

Refactor CoreNode