From 44dd7a1aa6490dafd798b243f08988aa0e3ac27c Mon Sep 17 00:00:00 2001 From: Rick Falkvinge Date: Mon, 20 Nov 2017 12:26:59 +0100 Subject: [PATCH] Db0054 - fix to Create-UTXO procedure with Cash --- Database/Configuration.cs | 2 +- Database/Schemata/revisions.txt | 3 +- Database/Schemata/upgrade-0054.sql | 58 ++++++++++++++++++++++++++++++ 3 files changed, 61 insertions(+), 2 deletions(-) create mode 100644 Database/Schemata/upgrade-0054.sql diff --git a/Database/Configuration.cs b/Database/Configuration.cs index 87daa322..49039f8a 100644 --- a/Database/Configuration.cs +++ b/Database/Configuration.cs @@ -14,7 +14,7 @@ namespace Swarmops.Database /// public static int DbVersionExpected { - get { return 53; } + get { return 54; } } diff --git a/Database/Schemata/revisions.txt b/Database/Schemata/revisions.txt index c26fc6bb..5a25c3ef 100644 --- a/Database/Schemata/revisions.txt +++ b/Database/Schemata/revisions.txt @@ -50,4 +50,5 @@ 0050 - Added VatReports.OpenTransaction, .CloseTransaction 0051 - Fix for CreateFinancialTransactionStub with Amount field gone (caused in 0043) [RF] 0052 - Added database support for multiple Bitcoin chain forks [RF] -0053 - Bugfix for 0052 [RF] \ No newline at end of file +0053 - Bugfix for 0052 [RF] +0054 - Fix for Conditional-CreateUTXO procedure with fork in effect [RF] diff --git a/Database/Schemata/upgrade-0054.sql b/Database/Schemata/upgrade-0054.sql new file mode 100644 index 00000000..6e71635f --- /dev/null +++ b/Database/Schemata/upgrade-0054.sql @@ -0,0 +1,58 @@ +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() INTO unspentId; + + 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; + + END IF; + + 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; + + SELECT unspentId AS Identity; + +END \ No newline at end of file