From 9548c02009539e17bfc00fbf263636bd9e2f4e9e Mon Sep 17 00:00:00 2001 From: Rick Falkvinge Date: Mon, 4 Mar 2019 07:08:05 +0100 Subject: [PATCH] Hardened against transient broadcast failure --- Common/Common.csproj | 2 + Common/Exceptions/NetworkTimeoutException.cs | 12 +++++ Common/Exceptions/TryAgainException.cs | 12 +++++ Logic/Financial/BitcoinUtility.cs | 50 ++++++++++++------- Logic/Support/BackendServiceOrder.cs | 10 +++- .../ReturnBitcoinEchoUtxoOrder.cs | 12 ++++- 6 files changed, 77 insertions(+), 21 deletions(-) create mode 100644 Common/Exceptions/NetworkTimeoutException.cs create mode 100644 Common/Exceptions/TryAgainException.cs diff --git a/Common/Common.csproj b/Common/Common.csproj index 7e405a4d..7ffb593a 100644 --- a/Common/Common.csproj +++ b/Common/Common.csproj @@ -84,6 +84,8 @@ + + diff --git a/Common/Exceptions/NetworkTimeoutException.cs b/Common/Exceptions/NetworkTimeoutException.cs new file mode 100644 index 00000000..ff83f05b --- /dev/null +++ b/Common/Exceptions/NetworkTimeoutException.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 + { + } +} diff --git a/Common/Exceptions/TryAgainException.cs b/Common/Exceptions/TryAgainException.cs new file mode 100644 index 00000000..e3b29322 --- /dev/null +++ b/Common/Exceptions/TryAgainException.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 TryAgainException: Exception + { + } +} diff --git a/Logic/Financial/BitcoinUtility.cs b/Logic/Financial/BitcoinUtility.cs index 82f66665..b0663f29 100644 --- a/Logic/Financial/BitcoinUtility.cs +++ b/Logic/Financial/BitcoinUtility.cs @@ -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) diff --git a/Logic/Support/BackendServiceOrder.cs b/Logic/Support/BackendServiceOrder.cs index cf66e4e9..3c909a7e 100644 --- a/Logic/Support/BackendServiceOrder.cs +++ b/Logic/Support/BackendServiceOrder.cs @@ -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 { diff --git a/Logic/Support/BackendServices/ReturnBitcoinEchoUtxoOrder.cs b/Logic/Support/BackendServices/ReturnBitcoinEchoUtxoOrder.cs index 315965ba..189e60d7 100644 --- a/Logic/Support/BackendServices/ReturnBitcoinEchoUtxoOrder.cs +++ b/Logic/Support/BackendServices/ReturnBitcoinEchoUtxoOrder.cs @@ -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();