Make indexbuilder notified for new block.
MODIFY
WalletWasabi.Backend/Global.cs
WalletWasabi.Backend/Global.cs
@@ -143,10 +143,10 @@ namespace WalletWasabi.Backend
IsRelay = true
};
- TrustedNodeNotifyingBehavior = new TrustedNodeNotifyingBehavior();
- nodeConnectionParameters.TemplateBehaviors.Add(TrustedNodeNotifyingBehavior);
-
+ nodeConnectionParameters.TemplateBehaviors.Add(new TrustedNodeNotifyingBehavior());
var node = Node.Connect(network, iPEndPoint, nodeConnectionParameters);
+ // We have to find it, because it's cloned by the node and not perfectly cloned (event handlers cannot be cloned.)
+ TrustedNodeNotifyingBehavior = node.Behaviors.Find<TrustedNodeNotifyingBehavior>();
try
{
Logger.LogInfo<Node>($"TCP Connection succeeded, handshaking...");

MODIFY
WalletWasabi.Tests/RegTests.cs
WalletWasabi.Tests/RegTests.cs
@@ -82,7 +82,7 @@ namespace WalletWasabi.Tests
Global.Coordinator.UtxoReferee.Clear();
var network = Global.RpcClient.Network;
- var serviceConfiguration = new ServiceConfiguration(2, 2, 21, 50, new IPEndPoint(IPAddress.Loopback, network.DefaultPort));
+ var serviceConfiguration = new ServiceConfiguration(2, 2, 21, 50, RegTestFixture.BackendRegTestNode.Endpoint);
return ("password", Global.RpcClient, network, Global.Coordinator, serviceConfiguration);
}

MODIFY
WalletWasabi.Tests/XunitConfiguration/RegTestFixture.cs
WalletWasabi.Tests/XunitConfiguration/RegTestFixture.cs
@@ -30,7 +30,7 @@ namespace WalletWasabi.Tests.XunitConfiguration
var rpc = BackendRegTestNode.CreateRpcClient();
- var config = new Config(rpc.Network, rpc.Authentication, IPAddress.Loopback.ToString(), IPAddress.Loopback.ToString(), IPAddress.Loopback.ToString(), Network.Main.DefaultPort, Network.TestNet.DefaultPort, Network.RegTest.DefaultPort);
+ var config = new Config(rpc.Network, rpc.Authentication, IPAddress.Loopback.ToString(), IPAddress.Loopback.ToString(), BackendRegTestNode.Endpoint.Address.ToString(), Network.Main.DefaultPort, Network.TestNet.DefaultPort, BackendRegTestNode.Endpoint.Port);
var roundConfig = new CcjRoundConfig(Money.Coins(0.1m), 144, 0.1m, 100, 120, 60, 60, 60, 1, 24, true, 11);

MODIFY
WalletWasabi/Services/IndexBuilderService.cs
WalletWasabi/Services/IndexBuilderService.cs
@@ -179,12 +179,29 @@ namespace WalletWasabi.Services
}
}
}
+
+ TrustedNodeNotifyingBehavior.BlockInv += TrustedNodeNotifyingBehavior_BlockInv;
}
+ private long _runner;
+
public void Synchronize()
{
Task.Run(async () =>
{
+ if (Interlocked.Read(ref _runner) >= 2) return;
+
+ Interlocked.Increment(ref _runner);
+ while (Interlocked.Read(ref _runner) != 1)
+ {
+ await Task.Delay(100);
+ }
+
+ if (Interlocked.Read(ref _running) >= 2)
+ {
+ return;
+ }
+
try
{
Interlocked.Exchange(ref _running, 1);
@@ -227,10 +244,9 @@ namespace WalletWasabi.Services
// Double it to make sure not to accidentally miss any notification.
if (isCoreSynchornized && blockCount == heightToRequest - 1)
{
- //TrustedNodeNotifyingBehavior.Block += TrustedNodeNotifyingBehavior_Block;
- //// Mark the initial index building process stopping. (finally block will mark it is stopped.)
- //Interlocked.Exchange(ref _initialIndexBuilding, 2);
- //return;
+ // Mark the process notstarted, so it can be started again and finally block can mark it is stopped.
+ Interlocked.Exchange(ref _running, 0);
+ return;
}
}
@@ -238,16 +254,7 @@ namespace WalletWasabi.Services
isImmature = true;
}
- Block block = null;
- try
- {
- block = await RpcClient.GetBlockAsync(heightToRequest);
- }
- catch (RPCException) // if the block didn't come yet
- {
- await Task.Delay(1000);
- continue;
- }
+ Block block = await RpcClient.GetBlockAsync(heightToRequest);
// Reorg check, except if we're requesting the starting height, because then the "currentHash" wouldn't exist.
@@ -364,10 +371,24 @@ namespace WalletWasabi.Services
{
Interlocked.Exchange(ref _running, 3);
}
+ Interlocked.Decrement(ref _runner);
}
});
}
+ private void TrustedNodeNotifyingBehavior_BlockInv(object sender, uint256 e)
+ {
+ try
+ {
+ // Run sync every time a block notification arrives. Synchronizer will stop when it finishes.
+ Synchronize();
+ }
+ catch (Exception ex)
+ {
+ Logger.LogError<IndexBuilderService>(ex);
+ }
+ }
+
private async Task ReorgOneAsync()
{
// 1. Rollback index
@@ -448,6 +469,11 @@ namespace WalletWasabi.Services
public async Task StopAsync()
{
+ if (TrustedNodeNotifyingBehavior != null)
+ {
+ TrustedNodeNotifyingBehavior.BlockInv -= TrustedNodeNotifyingBehavior_BlockInv;
+ }
+
if (IsRunning)
{
Interlocked.Exchange(ref _running, 2);

MODIFY
WalletWasabi/Services/MemPoolBehavior.cs
WalletWasabi/Services/MemPoolBehavior.cs
@@ -78,7 +78,7 @@ namespace WalletWasabi.Services
payload.Inventory.Add(inv);
}
- if (node.IsConnected)
+ if (payload.Inventory.Any() && node.IsConnected)
{
// ask for the whole transaction
await node.SendMessageAsync(payload);

MODIFY
WalletWasabi/Services/TrustedNodeNotifyingBehavior.cs
WalletWasabi/Services/TrustedNodeNotifyingBehavior.cs
@@ -54,7 +54,7 @@ namespace WalletWasabi.Services
}
}
- if (node.IsConnected)
+ if (payload.Inventory.Any() && node.IsConnected)
{
// ask for the whole transaction
await node.SendMessageAsync(payload);
@@ -82,6 +82,7 @@ namespace WalletWasabi.Services
public override object Clone()
{
+ // Note that, this isn't clone! So this must be used after we are connected to a node, because it'll have as many clones as nodes.
return new TrustedNodeNotifyingBehavior();
}
}

Make indexbuilder notified for new block.