First test fixed, Second test created
MODIFY
WalletWasabi.Tests/UnitTests/MempoolMirrorTests/MirrorMempoolTest.cs
WalletWasabi.Tests/UnitTests/MempoolMirrorTests/MirrorMempoolTest.cs
@@ -24,47 +24,68 @@ namespace WalletWasabi.Tests.UnitTests.MempoolMirrorTests
{
var coreNode = await TestNodeBuilder.CreateAsync();
using HostedServices services = new();
+ using MempoolMirror mempoolMirror = new(TimeSpan.FromSeconds(2), coreNode.RpcClient, coreNode.P2pNode);
+ services.Register<MempoolMirror>(mempoolMirror, "Mempool Mirror");
try
{
- using MempoolMirror mempoolMirror = new MempoolMirror(TimeSpan.FromSeconds(1), coreNode.RpcClient, coreNode.P2pNode);
- services.Register<MempoolMirror>(mempoolMirror, "Mempool Mirror");
- await services.StartAllAsync();
+ var rpc = coreNode.RpcClient;
+ var network = rpc.Network;
+ var mempoolInstance = services.Get<MempoolMirror>();
- var network = coreNode.RpcClient.Network;
+ var walletName = "RandomWalletName";
+ await rpc.CreateWalletAsync(walletName);
using var k1 = new Key();
- using var k2 = new Key();
- var blockId = await coreNode.RpcClient.GenerateToAddressAsync(1, k1.PubKey.WitHash.GetAddress(network));
- var block = await coreNode.RpcClient.GetBlockAsync(blockId[0]);
- var coinBaseTx = block.Transactions[0];
-
var spendAmount = new Money(0.0004m, MoneyUnit.BTC);
- var minerFee = new Money(0.000234m, MoneyUnit.BTC);
- var txInAmount = coinBaseTx;
- var changeAmount = txInAmount.Outputs.FirstOrDefault().Value - spendAmount - minerFee;
+ var spendAmount2 = new Money(0.0004m, MoneyUnit.BTC);
+ await coreNode.RpcClient.GenerateAsync(101);
- var tx = Transaction.Create(network);
- tx.Inputs.Add(txInAmount, 0);
- tx.Outputs.Add(spendAmount, k2.PubKey.WitHash.GetAddress(network));
- tx.Outputs.Add(changeAmount, k1.PubKey.WitHash.GetAddress(network));
- tx.Sign(k1.GetBitcoinSecret(network), coinBaseTx.Outputs.AsCoins().First());
- var valid = tx.Check();
-
- await coreNode.RpcClient.GenerateToAddressAsync(101, k1.PubKey.WitHash.GetAddress(network));
-
- var txid = await coreNode.RpcClient.SendRawTransactionAsync(tx);
-
- await services.Get<MempoolMirror>().TriggerAndWaitRoundAsync(TimeSpan.FromMilliseconds(5000));
+ var txid = await coreNode.RpcClient.SendToAddressAsync(k1.GetBitcoinSecret(network).GetAddress(ScriptPubKeyType.Segwit), spendAmount);
+ var txid2 = await coreNode.RpcClient.SendToAddressAsync(k1.GetBitcoinSecret(network).GetAddress(ScriptPubKeyType.Segwit), spendAmount2);
+ await services.StartAllAsync();
+ await mempoolInstance.TriggerAndWaitRoundAsync(TimeSpan.FromSeconds(2));
var localMempool = services.Get<MempoolMirror>().GetMempool();
- Console.WriteLine(localMempool.Count);
+ Assert.Equal(2, localMempool.Count);
Assert.Contains(txid, localMempool.Keys);
- Assert.Single(localMempool);
+ Assert.Contains(txid2, localMempool.Keys);
}
- catch (Exception ex)
+ finally
{
- Logger.LogDebug(ex);
+ await services.StopAllAsync();
+ await coreNode.TryStopAsync();
+ }
+ }
+
+ [Fact]
+ public async Task CanHandleArrivingTxAsync()
+ {
+ var coreNode = await TestNodeBuilder.CreateAsync();
+ using HostedServices services = new();
+ using MempoolMirror mempoolMirror = new(TimeSpan.FromSeconds(2), coreNode.RpcClient, coreNode.P2pNode);
+ services.Register<MempoolMirror>(mempoolMirror, "Mempool Mirror");
+ try
+ {
+ var rpc = coreNode.RpcClient;
+ var network = rpc.Network;
+ await services.StartAllAsync();
+ var mempoolInstance = services.Get<MempoolMirror>();
+
+ var walletName = "RandomWalletName";
+ await rpc.CreateWalletAsync(walletName);
+
+ using var key = new Key();
+ var spendAmount = new Money(0.0004m, MoneyUnit.BTC);
+ await coreNode.RpcClient.GenerateAsync(101);
+
+ var txid = await coreNode.RpcClient.SendToAddressAsync(key.GetBitcoinSecret(network).GetAddress(ScriptPubKeyType.Segwit), spendAmount);
+
+ await mempoolInstance.TriggerAndWaitRoundAsync(TimeSpan.FromSeconds(2));
+ var localMempool = mempoolInstance.GetMempool();
+
+ Assert.Single(localMempool);
+ Assert.Contains(txid, localMempool.Keys);
}
finally
{

MODIFY
WalletWasabi/BitcoinCore/Mempool/MempoolMirror.cs
WalletWasabi/BitcoinCore/Mempool/MempoolMirror.cs
@@ -47,7 +47,8 @@ namespace WalletWasabi.BitcoinCore.Mempool
protected override async Task ActionAsync(CancellationToken cancel)
{
- await MirrorMempoolAsync(cancel).ConfigureAwait(false);
+ int added = await MirrorMempoolAsync(cancel).ConfigureAwait(false);
+ Logger.LogInfo($"{added} transactions were copied from the full node from ActionAsyc");
}
public override Task StopAsync(CancellationToken cancellationToken)

First test fixed, Second test created