Add TrustedNodeNotifyingBehavior
MODIFY
WalletWasabi.Backend/Global.cs
WalletWasabi.Backend/Global.cs
@@ -33,6 +33,8 @@ namespace WalletWasabi.Backend
public static Node LocalNode { get; private set; }
+ public static TrustedNodeNotifyingBehavior TrustedNodeNotifyingBehavior { get; private set; }
+
public static IndexBuilderService IndexBuilderService { get; private set; }
public static CcjCoordinator Coordinator { get; private set; }
@@ -53,13 +55,13 @@ namespace WalletWasabi.Backend
await AssertRpcNodeFullyInitializedAsync();
// Make sure P2P works.
- InitializeP2pNode(config.Network, config.GetBitcoinCoreEndPoint());
+ InitializeP2p(config.Network, config.GetBitcoinCoreEndPoint());
// Initialize index building
var indexBuilderServiceDir = Path.Combine(DataDir, nameof(IndexBuilderService));
var indexFilePath = Path.Combine(indexBuilderServiceDir, $"Index{RpcClient.Network}.dat");
var utxoSetFilePath = Path.Combine(indexBuilderServiceDir, $"UtxoSet{RpcClient.Network}.dat");
- IndexBuilderService = new IndexBuilderService(RpcClient, indexFilePath, utxoSetFilePath);
+ IndexBuilderService = new IndexBuilderService(RpcClient, TrustedNodeNotifyingBehavior, indexFilePath, utxoSetFilePath);
Coordinator = new CcjCoordinator(RpcClient.Network, Path.Combine(DataDir, nameof(CcjCoordinator)), RpcClient, roundConfig);
IndexBuilderService.NewBlock += IndexBuilderService_NewBlockAsync;
IndexBuilderService.Synchronize();
@@ -128,7 +130,7 @@ namespace WalletWasabi.Backend
}
}
- private static void InitializeP2pNode(Network network, IPEndPoint iPEndPoint)
+ private static void InitializeP2p(Network network, IPEndPoint iPEndPoint)
{
Guard.NotNull(nameof(network), network);
Guard.NotNull(nameof(iPEndPoint), iPEndPoint);
@@ -142,6 +144,9 @@ namespace WalletWasabi.Backend
IsRelay = true
};
+ TrustedNodeNotifyingBehavior = new TrustedNodeNotifyingBehavior();
+ nodeConnectionParameters.TemplateBehaviors.Add(TrustedNodeNotifyingBehavior);
+
var node = Node.Connect(network, iPEndPoint, nodeConnectionParameters);
try
{

MODIFY
WalletWasabi.Tests/RegTests.cs
WalletWasabi.Tests/RegTests.cs
@@ -163,7 +163,7 @@ namespace WalletWasabi.Tests
var indexFilePath = Path.Combine(indexBuilderServiceDir, $"Index{rpc.Network}.dat");
var utxoSetFilePath = Path.Combine(indexBuilderServiceDir, $"UtxoSet{rpc.Network}.dat");
- var indexBuilderService = new IndexBuilderService(rpc, indexFilePath, utxoSetFilePath);
+ var indexBuilderService = new IndexBuilderService(rpc, Global.TrustedNodeNotifyingBehavior, indexFilePath, utxoSetFilePath);
try
{
indexBuilderService.Synchronize();

MODIFY
WalletWasabi/Services/IndexBuilderService.cs
WalletWasabi/Services/IndexBuilderService.cs
@@ -11,12 +11,14 @@ using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
+using NBitcoin.Protocol;
namespace WalletWasabi.Services
{
public class IndexBuilderService
{
public RPCClient RpcClient { get; }
+ public TrustedNodeNotifyingBehavior TrustedNodeNotifyingBehavior { get; }
public string IndexFilePath { get; }
public string Bech32UtxoSetFilePath { get; }
@@ -126,9 +128,10 @@ namespace WalletWasabi.Services
public bool IsRunning => Interlocked.Read(ref _running) == 1;
public bool IsStopping => Interlocked.Read(ref _running) == 2;
- public IndexBuilderService(RPCClient rpc, string indexFilePath, string bech32UtxoSetFilePath)
+ public IndexBuilderService(RPCClient rpc, TrustedNodeNotifyingBehavior trustedNodeNotifyingBehavior, string indexFilePath, string bech32UtxoSetFilePath)
{
RpcClient = Guard.NotNull(nameof(rpc), rpc);
+ TrustedNodeNotifyingBehavior = Guard.NotNull(nameof(trustedNodeNotifyingBehavior), trustedNodeNotifyingBehavior);
IndexFilePath = Guard.NotNullOrEmptyOrWhitespace(nameof(indexFilePath), indexFilePath);
Bech32UtxoSetFilePath = Guard.NotNullOrEmptyOrWhitespace(nameof(bech32UtxoSetFilePath), bech32UtxoSetFilePath);
@@ -178,6 +181,8 @@ namespace WalletWasabi.Services
}
}
}
+
+ TrustedNodeNotifyingBehavior.Block += TrustedNodeNotifyingBehavior_Block;
}
public void Synchronize()
@@ -222,6 +227,13 @@ namespace WalletWasabi.Services
}
catch (RPCException) // if the block didn't come yet
{
+ if (blockCount == height - 1)
+ {
+ // Start listening to P2P from here on.
+
+ return;
+ }
+
await Task.Delay(1000);
continue;
}
@@ -368,6 +380,16 @@ namespace WalletWasabi.Services
});
}
+ private void TrustedNodeNotifyingBehavior_Block(object sender, Block e)
+ {
+ try
+ {
+ }
+ catch (Exception ex)
+ {
+ }
+ }
+
public (Height bestHeight, IEnumerable<FilterModel> filters) GetFilterLinesExcluding(uint256 bestKnownBlockHash, int count, out bool found)
{
using (IndexLock.Lock())
@@ -406,6 +428,11 @@ namespace WalletWasabi.Services
public async Task StopAsync()
{
+ if (TrustedNodeNotifyingBehavior != null)
+ {
+ TrustedNodeNotifyingBehavior.Block -= TrustedNodeNotifyingBehavior_Block;
+ }
+
if (IsRunning)
{
Interlocked.Exchange(ref _running, 2);

Add TrustedNodeNotifyingBehavior