From ec3ee872769d3fbced9a87d5b4cfc3e2cca1dd3b Mon Sep 17 00:00:00 2001 From: Rick Falkvinge Date: Thu, 7 Jan 2016 01:45:53 +0100 Subject: [PATCH] Made input selection for Bitcoin payments more flexible --- Logic/Financial/BitcoinTransactionInput.cs | 7 +++++ Logic/Financial/BitcoinUtility.cs | 33 +++++++++++++++++++- Logic/Financial/HotBitcoinAddressUnspent.cs | 18 +++++++++++ Logic/Financial/HotBitcoinAddressUnspents.cs | 14 +-------- Logic/Financial/HotBitcoinAddresses.cs | 14 +++++++++ Logic/Financial/Payouts.cs | 7 +++-- Site5/Master-v5.master.cs | 4 +++ 7 files changed, 80 insertions(+), 17 deletions(-) diff --git a/Logic/Financial/BitcoinTransactionInput.cs b/Logic/Financial/BitcoinTransactionInput.cs index 73ecd8fe..94c5fece 100644 --- a/Logic/Financial/BitcoinTransactionInput.cs +++ b/Logic/Financial/BitcoinTransactionInput.cs @@ -26,6 +26,13 @@ namespace Swarmops.Logic.Financial public class BitcoinTransactionInputs: List { + public static BitcoinTransactionInputs FromSingle (BitcoinTransactionInput single) + { + BitcoinTransactionInputs result = new BitcoinTransactionInputs(); + result.Add (single); + return result; + } + public BitcoinSecret[] PrivateKeys { get diff --git a/Logic/Financial/BitcoinUtility.cs b/Logic/Financial/BitcoinUtility.cs index 3b83b6ab..817b69ce 100644 --- a/Logic/Financial/BitcoinUtility.cs +++ b/Logic/Financial/BitcoinUtility.cs @@ -9,6 +9,7 @@ using System.Net; using System.Text; using System.Threading.Tasks; using System.Web; +using System.Web.Configuration; using NBitcoin; using NBitcoin.BouncyCastle.Asn1.Ocsp; using Newtonsoft.Json.Linq; @@ -133,6 +134,8 @@ namespace Swarmops.Logic.Financial return coinList.ToArray(); } + // TODO: Condense TestUnspents into ONE call for MULTIPLE addresses (separated by | for Unspents according to API docs) + static public bool TestUnspents (string address) { // This function queries the Blockchain API for the unspent coin. @@ -207,8 +210,36 @@ namespace Swarmops.Logic.Financial // TODO: What's the most efficient way to do this? HotBitcoinAddresses addresses = HotBitcoinAddresses.ForOrganization (organization); + HotBitcoinAddressUnspents unspents = addresses.Unspents; - return addresses.FindAmount (satoshis); // Will throw on a number of circumstances + BitcoinTransactionInputs result = new BitcoinTransactionInputs(); + + // Lazy checking: if there's one single input that covers the entire amount, use it + + BitcoinTransactionInput lowestSingleSufficientInput = null; + + foreach (HotBitcoinAddressUnspent unspent in unspents) + { + if (unspent.AmountSatoshis >= satoshis) + { + if (lowestSingleSufficientInput == null || + lowestSingleSufficientInput.AmountSatoshis > unspent.AmountSatoshis) + { + lowestSingleSufficientInput = unspent.AsInput; + } + } + } + + if (lowestSingleSufficientInput != null) + { + // There is a single input that will cover the entire amount, so return it + + return BitcoinTransactionInputs.FromSingle (lowestSingleSufficientInput); + } + + throw new NotEnoughFundsException(); + + // Slightly more complex checking (TODO) } diff --git a/Logic/Financial/HotBitcoinAddressUnspent.cs b/Logic/Financial/HotBitcoinAddressUnspent.cs index ffff3d3d..4e6a2375 100644 --- a/Logic/Financial/HotBitcoinAddressUnspent.cs +++ b/Logic/Financial/HotBitcoinAddressUnspent.cs @@ -25,6 +25,24 @@ namespace Swarmops.Logic.Financial return FromBasic (SwarmDb.GetDatabaseForReading().GetHotBitcoinAddressUnspent (hotBitcoinAddressUnspentId)); } + public BitcoinTransactionInput AsInput + { + get + { + HotBitcoinAddress address = this.Address; + + return new BitcoinTransactionInput() + { + AmountSatoshis = this.AmountSatoshis, + BitcoinAddress = address.Address, + PrivateKey = address.PrivateKey, + TransactionHash = this.TransactionHash, + TransactionOutputIndex = this.TransactionOutputIndex, + HotBitcoinAddressUnspentId = this.Identity + }; + } + } + public HotBitcoinAddress Address { get { return HotBitcoinAddress.FromIdentity (base.HotBitcoinAddressId); } diff --git a/Logic/Financial/HotBitcoinAddressUnspents.cs b/Logic/Financial/HotBitcoinAddressUnspents.cs index 8c736ea1..e3cac210 100644 --- a/Logic/Financial/HotBitcoinAddressUnspents.cs +++ b/Logic/Financial/HotBitcoinAddressUnspents.cs @@ -28,19 +28,7 @@ namespace Swarmops.Logic.Financial BitcoinTransactionInputs result = new BitcoinTransactionInputs(); foreach (HotBitcoinAddressUnspent unspent in this) { - HotBitcoinAddress address = unspent.Address; - - BitcoinTransactionInput input = new BitcoinTransactionInput() - { - AmountSatoshis = unspent.AmountSatoshis, - BitcoinAddress = address.Address, - PrivateKey = address.PrivateKey, - TransactionHash = unspent.TransactionHash, - TransactionOutputIndex = unspent.TransactionOutputIndex, - HotBitcoinAddressUnspentId = unspent.Identity - }; - - result.Add (input); + result.Add (unspent.AsInput); } return result; diff --git a/Logic/Financial/HotBitcoinAddresses.cs b/Logic/Financial/HotBitcoinAddresses.cs index cb2123ed..ad80f263 100644 --- a/Logic/Financial/HotBitcoinAddresses.cs +++ b/Logic/Financial/HotBitcoinAddresses.cs @@ -73,5 +73,19 @@ namespace Swarmops.Logic.Financial { get { return this.Sum (item => item.BalanceSatoshis); } } + + public HotBitcoinAddressUnspents Unspents + { + get + { + HotBitcoinAddressUnspents unspents = new HotBitcoinAddressUnspents(); + foreach (HotBitcoinAddress address in this) + { + unspents.AddRange (address.Unspents); + } + + return unspents; + } + } } } diff --git a/Logic/Financial/Payouts.cs b/Logic/Financial/Payouts.cs index c990d417..cc4f4894 100644 --- a/Logic/Financial/Payouts.cs +++ b/Logic/Financial/Payouts.cs @@ -609,10 +609,11 @@ namespace Swarmops.Logic.Financial // We now have our desired payments. The next step is to find enough inputs to reach the required amount (plus fees; we're working a little blind here still). BitcoinTransactionInputs inputs = null; + Int64 satoshisMaximumAnticipatedFees = BitcoinUtility.FeeSatoshisPerThousandBytes * 20; // assume max 20k transaction size try { - inputs = BitcoinUtility.GetInputsForAmount(organization, satoshisTotal + BitcoinUtility.FeeSatoshisPerThousandBytes * 20); // assume max 20k transaction size + inputs = BitcoinUtility.GetInputsForAmount(organization, satoshisTotal + satoshisMaximumAnticipatedFees); } catch (NotEnoughFundsException) { @@ -632,11 +633,11 @@ namespace Swarmops.Logic.Financial Int64 satoshisAvailable = HotBitcoinAddresses.ForOrganization (organization).BalanceSatoshisTotal; secondaryStrings["AmountMissingMicrocoinsFloat"] = - ((satoshisTotal - satoshisAvailable)/100.0).ToString ("N2"); + ((satoshisTotal - satoshisAvailable + satoshisMaximumAnticipatedFees) / 100.0).ToString("N2"); if (organization.Currency.IsBitcoin) { - secondaryStrings["AmountNeededFloat"] = (satoshisTotal/100.0).ToString ("N2"); + secondaryStrings["AmountNeededFloat"] = ((satoshisTotal + satoshisMaximumAnticipatedFees) / 100.0).ToString("N2"); secondaryStrings["AmountWalletFloat"] = (satoshisAvailable/100.0).ToString ("N2"); } else diff --git a/Site5/Master-v5.master.cs b/Site5/Master-v5.master.cs index e09c6f5f..cb84c736 100644 --- a/Site5/Master-v5.master.cs +++ b/Site5/Master-v5.master.cs @@ -38,6 +38,10 @@ namespace Swarmops // BEGIN TEST CODE + // HotBitcoinAddresses testAddresses = HotBitcoinAddresses.ForOrganization (Organization.FromIdentity (2)); + // testAddresses.UpdateBalances(); + + // Payouts.PerformAutomated(); // END TEST CODE