Hardened against transient broadcast failure

This commit is contained in:
Rick Falkvinge
2019-03-04 07:08:05 +01:00
parent 75c0e86785
commit 9548c02009
6 changed files with 77 additions and 21 deletions
+2
View File
@@ -84,6 +84,8 @@
<Compile Include="Enums\RoleTypeClass.cs" />
<Compile Include="Exceptions\ConcurrencyException.cs" />
<Compile Include="Exceptions\DatabaseConcurrencyException.cs" />
<Compile Include="Exceptions\NetworkTimeoutException.cs" />
<Compile Include="Exceptions\TryAgainException.cs" />
<Compile Include="ExtensionMethods\Bitmap.cs" />
<Compile Include="ExtensionMethods\Color.cs" />
<Compile Include="ExtensionMethods\DateTime.cs" />
@@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Swarmops.Common.Exceptions
{
public class NetworkTimeoutException: Exception
{
}
}
+12
View File
@@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Swarmops.Common.Exceptions
{
public class TryAgainException: Exception
{
}
}
+31 -19
View File
@@ -17,6 +17,7 @@ using NBitcoin;
using Newtonsoft.Json.Linq;
using Swarmops.Common;
using Swarmops.Common.Enums;
using Swarmops.Common.Exceptions;
using Swarmops.Database;
using Swarmops.Logic.Communications;
using Swarmops.Logic.Communications.Payload;
@@ -582,33 +583,44 @@ namespace Swarmops.Logic.Financial
public static void BroadcastTransaction (Transaction transaction, BitcoinChain chain)
{
using (WebClient client = new WebClient())
try
{
switch (chain)
using (WebClient client = new WebClient())
{
case BitcoinChain.Core:
switch (chain)
{
case BitcoinChain.Core:
client.UploadValues("https://blockchain.info/pushtx?cors=true",
new NameValueCollection()
{
{"tx", transaction.ToHex()}
});
break; // no txid given
client.UploadValues("https://blockchain.info/pushtx?cors=true",
new NameValueCollection()
{
{"tx", transaction.ToHex()}
});
break; // no txid given
case BitcoinChain.Cash:
case BitcoinChain.Cash:
client.UploadValues("https://bch-insight.bitpay.com/api/tx/send",
new NameValueCollection()
{
{"rawtx", transaction.ToHex()}
});
break;
client.UploadValues("https://bch-insight.bitpay.com/api/tx/send",
new NameValueCollection()
{
{"rawtx", transaction.ToHex()}
});
break;
default:
throw new NotImplementedException("Unknown chain: " + chain);
default:
throw new NotImplementedException("Unknown chain: " + chain);
}
// TODO: Exception handling
}
// TODO: Exception handling
}
catch (WebException exception)
{
if (exception.Message.Contains("has timed out"))
{
throw new NetworkTimeoutException();
}
}
}
public static BitcoinTransactionInputs GetInputsForAmount (Organization organization, BitcoinChain chain, Int64 satoshis)
+9 -1
View File
@@ -71,12 +71,20 @@ namespace Swarmops.Logic.Support
// Run the Run() function
bool transientError = false;
try
{
Console.WriteLine(" - Executing BSO: " + className);
order.Run();
}
catch (TryAgainException)
{
transientError = true;
// Don't close this backend service order - it will retry
}
catch (Exception exception)
{
if (!order.HasWorkerThread)
@@ -88,7 +96,7 @@ namespace Swarmops.Logic.Support
}
finally
{
if (!order.HasWorkerThread)
if (!order.HasWorkerThread && !transientError)
{
try
{
@@ -6,6 +6,7 @@ using System.Threading.Tasks;
using NBitcoin;
using Swarmops.Basic.Types.Financial;
using Swarmops.Common.Enums;
using Swarmops.Common.Exceptions;
using Swarmops.Logic.Financial;
using Satoshis = NBitcoin.Money;
@@ -66,7 +67,16 @@ namespace Swarmops.Logic.Support.BackendServices
Transaction tx = txBuilder.BuildTransaction(true, SigHash.ForkId | SigHash.All);
BitcoinUtility.BroadcastTransaction(tx, BitcoinChain.Cash);
try
{
BitcoinUtility.BroadcastTransaction(tx, BitcoinChain.Cash);
}
catch (NetworkTimeoutException)
{
// This is a transient error, so retry
throw new TryAgainException();
}
utxoToReturn.Delete();
utxoAddress.UpdateTotal();