Isolate BlockNofifier to make it testeable
MODIFY
WalletWasabi.Backend/Global.cs
WalletWasabi.Backend/Global.cs
@@ -103,7 +103,8 @@ namespace WalletWasabi.Backend
// We have to find it, because it's cloned by the node and not perfectly cloned (event handlers cannot be cloned.)
P2pNode = new P2pNode(network, endPoint, new MempoolService(), $"/WasabiCoordinator:{Constants.BackendMajorVersion.ToString()}/");
await P2pNode.ConnectAsync(cancel).ConfigureAwait(false);
- BlockNotifier = new BlockNotifier(TimeSpan.FromSeconds(7), RpcClient, P2pNode);
+ BlockNotifier = new BlockNotifier(TimeSpan.FromSeconds(7), new RpcWrappedClient(RpcClient));
+ P2pNode.BlockInv += (s, e) => BlockNotifier.TriggerRound();
BlockNotifier.Start();
}

MODIFY
WalletWasabi/BitcoinCore/CoreNode.cs
WalletWasabi/BitcoinCore/CoreNode.cs
@@ -191,14 +191,20 @@ namespace WalletWasabi.BitcoinCore
coreNode.P2pNode = new P2pNode(coreNode.Network, coreNode.P2pEndPoint, coreNode.MempoolService, coreNodeParams.UserAgent);
await coreNode.P2pNode.ConnectAsync(cancel).ConfigureAwait(false);
+ coreNode.BlockNotifier = new BlockNotifier(TimeSpan.FromSeconds(7), new RpcWrappedClient(coreNode.RpcClient));
+ coreNode.P2pNode.BlockInv += coreNode.OnP2pBlockInv;
- coreNode.BlockNotifier = new BlockNotifier(TimeSpan.FromSeconds(7), coreNode.RpcClient, coreNode.P2pNode);
coreNode.BlockNotifier.Start();
return coreNode;
}
}
+ private void OnP2pBlockInv(object sender, uint256 e)
+ {
+ BlockNotifier.TriggerRound();
+ }
+
public static async Task<Version> GetVersionAsync(CancellationToken cancel)
{
var arguments = "-version";
@@ -240,7 +246,12 @@ namespace WalletWasabi.BitcoinCore
{
await blockNotifier.StopAsync().ConfigureAwait(false);
}
- P2pNode?.Dispose();
+ var p2pNode = P2pNode;
+ if (p2pNode is { })
+ {
+ p2pNode.BlockInv -= OnP2pBlockInv;
+ p2pNode.Dispose();
+ }
_disposedValue = true;
}
}

MODIFY
WalletWasabi/Blockchain/Blocks/BlockNotifier.cs
WalletWasabi/Blockchain/Blocks/BlockNotifier.cs
@@ -22,22 +22,14 @@ namespace WalletWasabi.Blockchain.Blocks
public event EventHandler<BlockHeader> OnReorg;
- public RPCClient RpcClient { get; set; }
+ public IRPCClient RpcClient { get; set; }
public Network Network => RpcClient.Network;
private List<BlockHeader> ProcessedBlocks { get; }
- public P2pNode P2pNode { get; }
- public BlockNotifier(TimeSpan period, RPCClient rpcClient, P2pNode p2pNode) : base(period, null)
+ public BlockNotifier(TimeSpan period, IRPCClient rpcClient) : base(period, null)
{
RpcClient = Guard.NotNull(nameof(rpcClient), rpcClient);
- P2pNode = p2pNode;
ProcessedBlocks = new List<BlockHeader>();
- P2pNode.BlockInv += P2pNode_BlockInv;
- }
-
- private void P2pNode_BlockInv(object sender, uint256 blockHash)
- {
- TriggerRound();
}
protected override async Task<uint256> ActionAsync(CancellationToken cancel)
@@ -191,7 +183,6 @@ namespace WalletWasabi.Blockchain.Blocks
public new async Task StopAsync()
{
- P2pNode.BlockInv -= P2pNode_BlockInv;
await base.StopAsync().ConfigureAwait(false);
}
}

Isolate BlockNofifier to make it testeable