diff --git a/Database/Configuration.cs b/Database/Configuration.cs index 133400da..1678e3d2 100644 --- a/Database/Configuration.cs +++ b/Database/Configuration.cs @@ -14,7 +14,7 @@ namespace Swarmops.Database /// public static int DbVersionExpected { - get { return 55; } + get { return 56; } } diff --git a/Database/Database-HotBitcoinAddresses.cs b/Database/Database-HotBitcoinAddresses.cs index ec449439..55681d0e 100644 --- a/Database/Database-HotBitcoinAddresses.cs +++ b/Database/Database-HotBitcoinAddresses.cs @@ -232,7 +232,7 @@ namespace Swarmops.Database AddParameterWithName(command, "amountSatoshis", amountSatoshis); AddParameterWithName(command, "confirmationCount", confirmationCount); - return Convert.ToInt32(command.ExecuteScalar()); + return Convert.ToInt32(command.ExecuteScalar()); // A negative number means the record was updated, a positive that it was created } } } @@ -254,6 +254,21 @@ namespace Swarmops.Database } } + + public void UpdateHotBitcoinAddressUnspentTotals() + { + using (DbConnection connection = GetMySqlDbConnection()) + { + connection.Open(); + + using (DbCommand command = GetDbCommand("UpdateHotBitcoinAddressUnspentTotals", connection)) + { + command.CommandType = CommandType.StoredProcedure; + command.ExecuteNonQuery(); + } + } + } + #endregion } } diff --git a/Database/Schemata/revisions.txt b/Database/Schemata/revisions.txt index 8a9d7535..86936f4e 100644 --- a/Database/Schemata/revisions.txt +++ b/Database/Schemata/revisions.txt @@ -53,3 +53,4 @@ 0053 - Bugfix for 0052 [RF] 0054 - Fix for Conditional-CreateUTXO procedure with fork in effect [RF] 0055 - Fix for bug introduced with 0054, hash collisions in index [RF] +0056 - Splitting a storedproc into two; debugging a weird multi-row select return [RF] \ No newline at end of file diff --git a/Database/Schemata/upgrade-0056.sql b/Database/Schemata/upgrade-0056.sql new file mode 100644 index 00000000..fdbaa720 --- /dev/null +++ b/Database/Schemata/upgrade-0056.sql @@ -0,0 +1,71 @@ +DROP PROCEDURE IF EXISTS `UpdateHotBitcoinAddressUnspentTotals` + +# + +DROP PROCEDURE IF EXISTS `CreateHotBitcoinAddressUnspentConditional` + +# + + +CREATE PROCEDURE `CreateHotBitcoinAddressUnspentConditional`( + IN hotBitcoinAddressId INT, + IN amountSatoshis BIGINT, + IN transactionHash VARCHAR(128), + IN transactionOutputIndex INT, + IN confirmationCount INT +) +BEGIN + + DECLARE unspentId INT; + + IF ((SELECT COUNT(*) FROM HotBitcoinAddressUnspents + WHERE HotBitcoinAddressUnspents.TransactionHash=transactionHash + AND HotBitcoinAddressUnspents.TransactionOutputIndex=transactionOutputIndex + AND HotBitcoinAddressUnspents.HotBitcoinAddressId=hotBitcoinAddressId) = 0) + THEN + + INSERT INTO HotBitcoinAddressUnspents + (HotBitcoinAddressId,AmountSatoshis,TransactionHash,TransactionOutputIndex,ConfirmationCount) + VALUES + (hotBitcoinAddressId,amountSatoshis,transactionHash,transactionOutputIndex,confirmationCount); + + SELECT LAST_INSERT_ID() AS Identity; + + ELSE + + SELECT HotBitcoinAddressUnspents.HotBitcoinAddressUnspentId INTO unspentId + FROM HotBitcoinAddressUnspents + WHERE HotBitcoinAddressUnspents.TransactionHash=transactionHash + AND HotBitcoinAddressUnspents.TransactionOutputIndex=transactionOutputIndex + AND HotBitcoinAddressId=hotBitcoinAddressId; + + UPDATE HotBitcoinAddressUnspents + SET HotBitcoinAddressUnspents.ConfirmationCount=confirmationCount + WHERE HotBitcoinAddressUnspents.HotBitcoinAddressUnspentId=unspentId; + + SELECT -unspentId AS Identity; + + END IF; + +END + + +# + + +CREATE PROCEDURE `UpdateHotBitcoinAddressUnspentTotals` () +BEGIN + + UPDATE HotBitcoinAddresses + INNER JOIN + ( + SELECT HotBitcoinAddressId, SUM(AmountSatoshis) AS UnspentAmountSum + FROM HotBitcoinAddressUnspents + GROUP BY HotBitcoinAddressId + ) innerSelection ON HotBitcoinAddresses.HotBitcoinAddressId = innerSelection.HotBitcoinAddressId + + SET HotBitcoinAddresses.BalanceSatoshis = innerSelection.UnspentAmountSum + WHERE HotBitcoinAddresses.HotBitcoinAddressId=hotBitcoinAddressId; + +END + diff --git a/Site/Pages/v5/Ledgers/Json-BitcoinHotwalletData.aspx.cs b/Site/Pages/v5/Ledgers/Json-BitcoinHotwalletData.aspx.cs index ee587a81..8342f2ae 100644 --- a/Site/Pages/v5/Ledgers/Json-BitcoinHotwalletData.aspx.cs +++ b/Site/Pages/v5/Ledgers/Json-BitcoinHotwalletData.aspx.cs @@ -26,6 +26,7 @@ namespace Swarmops.Frontend.Pages.v5.Ledgers if (address.Chain == BitcoinChain.Core) { // These shouldn't exist much, so make sure that we have the equivalent Cash address registered + BitcoinUtility.TestUnspents(BitcoinChain.Cash, address.Address); try {