diff options
Diffstat (limited to 'src/exchangedb/exchange_do_refund.sql')
-rw-r--r-- | src/exchangedb/exchange_do_refund.sql | 102 |
1 files changed, 48 insertions, 54 deletions
diff --git a/src/exchangedb/exchange_do_refund.sql b/src/exchangedb/exchange_do_refund.sql index ceaabfe16..a95746127 100644 --- a/src/exchangedb/exchange_do_refund.sql +++ b/src/exchangedb/exchange_do_refund.sql @@ -1,6 +1,6 @@ -- -- This file is part of TALER --- Copyright (C) 2014--2022 Taler Systems SA +-- Copyright (C) 2014--2023 Taler Systems SA -- -- TALER is free software; you can redistribute it and/or modify it under the -- terms of the GNU General Public License as published by the Free Software @@ -15,12 +15,9 @@ -- CREATE OR REPLACE FUNCTION exchange_do_refund( - IN in_amount_with_fee_val INT8, - IN in_amount_with_fee_frac INT4, - IN in_amount_val INT8, - IN in_amount_frac INT4, - IN in_deposit_fee_val INT8, - IN in_deposit_fee_frac INT4, + IN in_amount_with_fee taler_amount, + IN in_amount taler_amount, + IN in_deposit_fee taler_amount, IN in_h_contract_terms BYTEA, IN in_rtransaction_id INT8, IN in_deposit_shard INT8, @@ -35,15 +32,15 @@ CREATE OR REPLACE FUNCTION exchange_do_refund( LANGUAGE plpgsql AS $$ DECLARE - dsi INT8; -- ID of deposit being refunded + bdsi INT8; -- ID of deposit being refunded DECLARE tmp_val INT8; -- total amount refunded DECLARE - tmp_frac INT8; -- total amount refunded + tmp_frac INT8; -- total amount refunded, large fraction to deal with overflows! DECLARE - deposit_val INT8; -- amount that was originally deposited + tmp taler_amount; -- total amount refunded, normalized DECLARE - deposit_frac INT8; -- amount that was originally deposited + deposit taler_amount; -- amount that was originally deposited BEGIN -- Shards: SELECT deposits (coin_pub, shard, h_contract_terms, merchant_pub) -- INSERT refunds (by coin_pub, rtransaction_id) ON CONFLICT DO NOTHING @@ -51,17 +48,19 @@ BEGIN -- UPDATE known_coins (by coin_pub) SELECT - deposit_serial_id - ,amount_with_fee_val - ,amount_with_fee_frac - ,done -INTO - dsi - ,deposit_val - ,deposit_frac + bdep.batch_deposit_serial_id + ,(cdep.amount_with_fee).val + ,(cdep.amount_with_fee).frac + ,bdep.done + INTO + bdsi + ,deposit.val + ,deposit.frac ,out_gone -FROM exchange.deposits - WHERE coin_pub=in_coin_pub + FROM batch_deposits bdep + JOIN coin_deposits cdep + USING (batch_deposit_serial_id) + WHERE cdep.coin_pub=in_coin_pub AND shard=in_deposit_shard AND merchant_pub=in_merchant_pub AND h_contract_terms=in_h_contract_terms; @@ -76,21 +75,20 @@ THEN RETURN; END IF; -INSERT INTO exchange.refunds - (deposit_serial_id +INSERT INTO refunds + (batch_deposit_serial_id ,coin_pub ,merchant_sig ,rtransaction_id - ,amount_with_fee_val - ,amount_with_fee_frac + ,amount_with_fee ) VALUES - (dsi + (bdsi ,in_coin_pub ,in_merchant_sig ,in_rtransaction_id - ,in_amount_with_fee_val - ,in_amount_with_fee_frac) + ,in_amount_with_fee + ) ON CONFLICT DO NOTHING; IF NOT FOUND @@ -103,10 +101,9 @@ THEN PERFORM FROM exchange.refunds WHERE coin_pub=in_coin_pub - AND deposit_serial_id=dsi + AND batch_deposit_serial_id=bdsi AND rtransaction_id=in_rtransaction_id - AND amount_with_fee_val=in_amount_with_fee_val - AND amount_with_fee_frac=in_amount_with_fee_frac; + AND amount_with_fee=in_amount_with_fee; IF NOT FOUND THEN @@ -136,14 +133,14 @@ END IF; -- Check refund balance invariant. SELECT - SUM(amount_with_fee_val) -- overflow here is not plausible - ,SUM(CAST(amount_with_fee_frac AS INT8)) -- compute using 64 bits + SUM((refs.amount_with_fee).val) -- overflow here is not plausible + ,SUM(CAST((refs.amount_with_fee).frac AS INT8)) -- compute using 64 bits INTO tmp_val ,tmp_frac - FROM exchange.refunds + FROM refunds refs WHERE coin_pub=in_coin_pub - AND deposit_serial_id=dsi; + AND batch_deposit_serial_id=bdsi; IF tmp_val IS NULL THEN RAISE NOTICE 'failed to sum up existing refunds'; @@ -154,15 +151,15 @@ THEN END IF; -- Normalize result before continuing -tmp_val = tmp_val + tmp_frac / 100000000; -tmp_frac = tmp_frac % 100000000; +tmp.val = tmp_val + tmp_frac / 100000000; +tmp.frac = tmp_frac % 100000000; -- Actually check if the deposits are sufficient for the refund. Verbosely. ;-) -IF (tmp_val < deposit_val) +IF (tmp.val < deposit.val) THEN out_refund_ok=TRUE; ELSE - IF (tmp_val = deposit_val) AND (tmp_frac <= deposit_frac) + IF (tmp.val = deposit.val) AND (tmp.frac <= deposit.frac) THEN out_refund_ok=TRUE; ELSE @@ -170,42 +167,39 @@ ELSE END IF; END IF; -IF (tmp_val = deposit_val) AND (tmp_frac = deposit_frac) +IF (tmp.val = deposit.val) AND (tmp.frac = deposit.frac) THEN -- Refunds have reached the full value of the original -- deposit. Also refund the deposit fee. - in_amount_frac = in_amount_frac + in_deposit_fee_frac; - in_amount_val = in_amount_val + in_deposit_fee_val; + in_amount.frac = in_amount.frac + in_deposit_fee.frac; + in_amount.val = in_amount.val + in_deposit_fee.val; -- Normalize result before continuing - in_amount_val = in_amount_val + in_amount_frac / 100000000; - in_amount_frac = in_amount_frac % 100000000; + in_amount.val = in_amount.val + in_amount.frac / 100000000; + in_amount.frac = in_amount.frac % 100000000; END IF; -- Update balance of the coin. -UPDATE known_coins +UPDATE known_coins kc SET - remaining_frac=remaining_frac+in_amount_frac + remaining.frac=(kc.remaining).frac+in_amount.frac - CASE - WHEN remaining_frac+in_amount_frac >= 100000000 + WHEN (kc.remaining).frac+in_amount.frac >= 100000000 THEN 100000000 ELSE 0 END, - remaining_val=remaining_val+in_amount_val + remaining.val=(kc.remaining).val+in_amount.val + CASE - WHEN remaining_frac+in_amount_frac >= 100000000 + WHEN (kc.remaining).frac+in_amount.frac >= 100000000 THEN 1 ELSE 0 END WHERE coin_pub=in_coin_pub; - out_conflict=FALSE; out_not_found=FALSE; END $$; --- COMMENT ON FUNCTION exchange_do_refund(INT8, INT4, BYTEA, BOOLEAN, BOOLEAN) --- IS 'Executes a refund operation, checking that the corresponding deposit was sufficient to cover the refunded amount'; - - +COMMENT ON FUNCTION exchange_do_refund(taler_amount, taler_amount, taler_amount, BYTEA, INT8, INT8, INT8, BYTEA, BYTEA, BYTEA) + IS 'Executes a refund operation, checking that the corresponding deposit was sufficient to cover the refunded amount'; |