Refactor SendTransactionAsync
MODIFY
WalletWasabi/Services/WalletService.cs
WalletWasabi/Services/WalletService.cs
@@ -1183,45 +1183,10 @@ namespace WalletWasabi.Services
private static long SendCount = 0;
- public async Task SendTransactionAsync(SmartTransaction transaction)
- {
- try
- {
- Interlocked.Increment(ref SendCount);
- // Broadcast to a random node.
- // Wait until it arrives to at least two other nodes.
- // If something's wrong, fall back broadcasting with backend.
-
- if (Network == Network.RegTest)
- {
- throw new InvalidOperationException($"Transaction broadcasting to nodes does not work in {Network.RegTest}.");
- }
-
- while (true)
+ private async Task BroadcastTransactionAsync(SmartTransaction transaction, Node node)
{
- // As long as we are connected to at least 4 nodes, we can always try again.
- // 3 should be enough, but make it 5 so 2 nodes could disconnect the meantime.
- if (Nodes.ConnectedNodes.Count < 5)
- {
- throw new InvalidOperationException("We are not connected to enough nodes.");
- }
-
- Node node = Nodes.ConnectedNodes.RandomElement();
- if (node == default(Node))
- {
- await Task.Delay(100);
- continue;
- }
-
- if (!node.IsConnected)
- {
- await Task.Delay(100);
- continue;
- }
-
Logger.LogInfo<WalletService>($"Trying to broadcast transaction with random node ({node.RemoteSocketAddress}):{transaction.GetHash()}");
- var addedToBroadcastStore = Mempool.TryAddToBroadcastStore(transaction.Transaction, node.RemoteSocketEndpoint.ToString()); // So we'll reply to INV with this transaction.
- if (!addedToBroadcastStore)
+ if (!Mempool.TryAddToBroadcastStore(transaction.Transaction, node.RemoteSocketEndpoint.ToString())) // So we'll reply to INV with this transaction.
{
Logger.LogWarning<WalletService>($"Transaction {transaction.GetHash()} was already present in the broadcast store.");
}
@@ -1265,8 +1230,34 @@ namespace WalletWasabi.Services
{
Logger.LogWarning<WalletService>($"Expected transaction {transaction.GetHash()} was not found in the broadcast store.");
}
- break;
}
+
+ public async Task SendTransactionAsync(SmartTransaction transaction)
+ {
+ try
+ {
+ Interlocked.Increment(ref SendCount);
+ // Broadcast to a random node.
+ // Wait until it arrives to at least two other nodes.
+ // If something's wrong, fall back broadcasting with backend.
+
+ if (Network == Network.RegTest)
+ {
+ throw new InvalidOperationException($"Transaction broadcasting to nodes does not work in {Network.RegTest}.");
+ }
+
+ Node node = Nodes.ConnectedNodes.RandomElement();
+ while (node == default(Node) || !node.IsConnected || Nodes.ConnectedNodes.Count < 5)
+ {
+ // As long as we are connected to at least 4 nodes, we can always try again.
+ // 3 should be enough, but make it 5 so 2 nodes could disconnect the meantime.
+ if (Nodes.ConnectedNodes.Count < 5)
+ {
+ throw new InvalidOperationException("We are not connected to enough nodes.");
+ }
+ await Task.Delay(100);
+ }
+ await BroadcastTransactionAsync(transaction, node);
}
catch (Exception ex)
{

Refactor SendTransactionAsync