commit d368476a11b1e3cb905961ab429c3801c29c942e
parent 98ba58b7df9978d71a9affb2859e233cb1fef5c1
Author: Antoine A <>
Date: Mon, 7 Oct 2024 23:32:55 +0200
bank: reduce data duplication
Diffstat:
4 files changed, 20 insertions(+), 47 deletions(-)
diff --git a/bank/src/main/kotlin/tech/libeufin/bank/db/ExchangeDAO.kt b/bank/src/main/kotlin/tech/libeufin/bank/db/ExchangeDAO.kt
@@ -77,13 +77,14 @@ class ExchangeDAO(private val db: Database) {
SELECT
bank_transaction_id
,transaction_date
- ,(amount).val AS amount_val
- ,(amount).frac AS amount_frac
- ,creditor_payto
- ,creditor_name
+ ,(txs.amount).val AS amount_val
+ ,(txs.amount).frac AS amount_frac
+ ,txs.creditor_payto
+ ,txs.creditor_name
,wtid
,exchange_base_url
FROM taler_exchange_outgoing AS tfr
+ JOIN transfer_operations USING (exchange_outgoing_id)
JOIN bank_account_transactions AS txs
ON bank_transaction=txs.bank_transaction_id
WHERE
diff --git a/bank/src/test/kotlin/bench.kt b/bank/src/test/kotlin/bench.kt
@@ -81,9 +81,8 @@ class Bench {
val uuid = UUID.randomUUID()
"$uuid\t$account\t\\\\x$hex\t0\n"
},
- "taler_exchange_outgoing(wtid, exchange_base_url, bank_transaction)" to {
- val hex32 = token32.rand().encodeHex()
- "\\\\x$hex32\t\\url\t${it*2-1}\n"
+ "taler_exchange_outgoing(bank_transaction)" to {
+ "${it*2-1}\n"
},
"transfer_operations(wtid, request_uid, amount, exchange_base_url, exchange_outgoing_id, exchange_id, transfer_date, creditor_payto, status, status_msg)" to {
val hex32 = token32.rand().encodeHex()
diff --git a/database-versioning/libeufin-bank-0009.sql b/database-versioning/libeufin-bank-0009.sql
@@ -64,6 +64,8 @@ COMMENT ON INDEX transfer_operations_account_index IS 'for listing taler transfe
-- Remove unused columns
ALTER TABLE taler_exchange_outgoing
DROP COLUMN request_uid,
- DROP COLUMN creditor_account_id;
+ DROP COLUMN creditor_account_id,
+ DROP COLUMN wtid,
+ DROP COLUMN exchange_base_url;
COMMIT;
diff --git a/database-versioning/libeufin-bank-procedures.sql b/database-versioning/libeufin-bank-procedures.sql
@@ -531,43 +531,6 @@ UPDATE customers SET deleted_at = in_timestamp WHERE customer_id = my_customer_i
END $$;
COMMENT ON FUNCTION account_delete IS 'Deletes an account if the balance is zero';
-CREATE FUNCTION register_outgoing(
- IN in_wtid BYTEA,
- IN in_exchange_base_url TEXT,
- IN in_debtor_account_id INT8,
- IN in_creditor_account_id INT8,
- IN in_debit_row_id INT8,
- IN in_credit_row_id INT8,
- OUT in_tx_row_id INT8
-)
-LANGUAGE plpgsql AS $$
-DECLARE
- local_amount taler_amount;
- local_bank_account_id INT8;
-BEGIN
--- register outgoing transaction
-INSERT
- INTO taler_exchange_outgoing (
- wtid,
- exchange_base_url,
- bank_transaction
-) VALUES (
- in_wtid,
- in_exchange_base_url,
- in_debit_row_id
-) RETURNING exchange_outgoing_id INTO in_tx_row_id;
--- TODO check if not drain
--- update stats
-SELECT (amount).val, (amount).frac, bank_account_id
-INTO local_amount.val, local_amount.frac, local_bank_account_id
-FROM bank_account_transactions WHERE bank_transaction_id=in_debit_row_id;
-CALL stats_register_payment('taler_out', NULL, local_amount, null);
--- notify new transaction
-PERFORM pg_notify('bank_outgoing_tx', in_debtor_account_id || ' ' || in_creditor_account_id || ' ' || in_debit_row_id || ' ' || in_credit_row_id);
-END $$;
-COMMENT ON FUNCTION register_outgoing
- IS 'Register a bank transaction as a taler outgoing transaction and announce it';
-
CREATE PROCEDURE register_incoming(
IN in_tx_row_id INT8,
IN in_type taler_incoming_type,
@@ -716,8 +679,13 @@ IF out_exchange_balance_insufficient THEN
RETURN;
END IF;
-- Register outgoing transaction
-SELECT in_tx_row_id INTO outgoing_id
- FROM register_outgoing(in_wtid, in_exchange_base_url, exchange_account_id, creditor_account_id, debit_row_id, credit_row_id);
+INSERT INTO taler_exchange_outgoing (
+ bank_transaction
+) VALUES (
+ debit_row_id
+) RETURNING exchange_outgoing_id INTO outgoing_id;
+-- Update stats
+CALL stats_register_payment('taler_out', NULL, in_amount, null);
-- Register success
INSERT INTO transfer_operations (
request_uid,
@@ -742,6 +710,9 @@ INSERT INTO transfer_operations (
NULL,
exchange_account_id
) RETURNING transfer_operation_id INTO out_tx_row_id;
+
+-- Notify new transaction
+PERFORM pg_notify('bank_outgoing_tx', exchange_account_id || ' ' || creditor_account_id || ' ' || debit_row_id || ' ' || credit_row_id);
END $$;
COMMENT ON FUNCTION taler_transfer IS 'Create an outgoing taler transaction and register it';