From f4c8eb6a9c460a9a9b8ea1c4358e1ce87097a8f1 Mon Sep 17 00:00:00 2001 From: Christian Grothoff Date: Mon, 3 Oct 2022 12:46:30 +0200 Subject: -skeletons for new PG functions --- src/exchangedb/Makefile.am | 4 +- src/exchangedb/pg_do_reserve_open.c | 68 +++++++++++ src/exchangedb/pg_do_reserve_open.h | 55 +++++++++ src/exchangedb/pg_helper.h | 149 ++++++++++++++++++++++++ src/exchangedb/pg_insert_close_request.c | 64 ++++++++++ src/exchangedb/pg_insert_close_request.h | 52 +++++++++ src/exchangedb/pg_insert_reserve_open_deposit.c | 64 ++++++++++ src/exchangedb/pg_insert_reserve_open_deposit.h | 52 +++++++++ src/exchangedb/pg_iterate_kyc_reference.c | 58 +++++++++ src/exchangedb/pg_iterate_kyc_reference.h | 46 ++++++++ src/exchangedb/pg_iterate_reserve_close_info.c | 63 ++++++++++ src/exchangedb/pg_iterate_reserve_close_info.h | 50 ++++++++ src/exchangedb/pg_select_reserve_close_info.c | 62 ++++++++++ src/exchangedb/pg_select_reserve_close_info.h | 49 ++++++++ src/exchangedb/plugin_exchangedb_postgres.c | 137 +--------------------- 15 files changed, 841 insertions(+), 132 deletions(-) create mode 100644 src/exchangedb/pg_do_reserve_open.c create mode 100644 src/exchangedb/pg_do_reserve_open.h create mode 100644 src/exchangedb/pg_helper.h create mode 100644 src/exchangedb/pg_insert_close_request.c create mode 100644 src/exchangedb/pg_insert_close_request.h create mode 100644 src/exchangedb/pg_insert_reserve_open_deposit.c create mode 100644 src/exchangedb/pg_insert_reserve_open_deposit.h create mode 100644 src/exchangedb/pg_iterate_kyc_reference.c create mode 100644 src/exchangedb/pg_iterate_kyc_reference.h create mode 100644 src/exchangedb/pg_iterate_reserve_close_info.c create mode 100644 src/exchangedb/pg_iterate_reserve_close_info.h create mode 100644 src/exchangedb/pg_select_reserve_close_info.c create mode 100644 src/exchangedb/pg_select_reserve_close_info.h diff --git a/src/exchangedb/Makefile.am b/src/exchangedb/Makefile.am index 01ce13dec..3ea5a0d94 100644 --- a/src/exchangedb/Makefile.am +++ b/src/exchangedb/Makefile.am @@ -68,7 +68,9 @@ plugin_LTLIBRARIES = \ endif libtaler_plugin_exchangedb_postgres_la_SOURCES = \ - plugin_exchangedb_postgres.c + plugin_exchangedb_postgres.c pg_helper.h \ + pg_insert_close_request.c pg_insert_close_request.h \ + pg_insert_reserve_open_deposit.c pg_insert_reserve_open_deposit.h libtaler_plugin_exchangedb_postgres_la_LIBADD = \ $(LTLIBINTL) libtaler_plugin_exchangedb_postgres_la_LDFLAGS = \ diff --git a/src/exchangedb/pg_do_reserve_open.c b/src/exchangedb/pg_do_reserve_open.c new file mode 100644 index 000000000..e4f72845f --- /dev/null +++ b/src/exchangedb/pg_do_reserve_open.c @@ -0,0 +1,68 @@ +/* + This file is part of TALER + Copyright (C) 2022 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 + Foundation; either version 3, or (at your option) any later version. + + TALER is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along with + TALER; see the file COPYING. If not, see + */ +/** + * @file pg_do_reserve_open.c + * @brief Low-level (statement-level) Postgres database access for the exchange + * @author Christian Grothoff + */ +#include "platform.h" +#include "taler_error_codes.h" +#include "taler_dbevents.h" +#include "taler_pq_lib.h" +#include "pg_do_reserve_open.h" +#include "pg_helper.h" + + +enum GNUNET_DB_QueryStatus +TEH_PG_do_reserve_open ( + void *cls, + const struct TALER_ReservePublicKeyP *reserve_pub, + const struct TALER_Amount *total_paid, + uint32_t min_purse_limit, + const struct TALER_ReserveSignatureP *reserve_sig, + struct GNUNET_TIME_Timestamp desired_expiration, + struct GNUNET_TIME_Timestamp now, + const struct TALER_Amount *open_fee, + struct TALER_Amount *open_cost, + const struct GNUNET_TIME_Timestamp *final_expiration) +{ + struct PostgresClosure *pg = cls; + // FIXME: everything from here is cut&paste + struct GNUNET_PQ_QueryParam params[] = { + GNUNET_PQ_query_param_auto_from_type (&cpi->coin_pub), + GNUNET_PQ_query_param_uint64 (&known_coin_id), + GNUNET_PQ_query_param_auto_from_type (coin_sig), + GNUNET_PQ_query_param_auto_from_type (reserve_sig), + TALER_PQ_query_param_amount (coin_total), + GNUNET_PQ_query_param_end + }; + struct GNUNET_PQ_ResultSpec rs[] = { + GNUNET_PQ_result_spec_bool ("insufficient_funds", + insufficient_funds), + GNUNET_PQ_result_spec_end + }; + + PREPARE (pg, + "insert_reserve_open_deposit", + "SELECT " + " insufficient_funds" + " FROM exchange_do_reserve_open_deposit" + " ($1,$2,$3,$4,$5,$6);"); + return GNUNET_PQ_eval_prepared_singleton_select (pg->conn, + "insert_reserve_open_deposit", + params, + rs); +} diff --git a/src/exchangedb/pg_do_reserve_open.h b/src/exchangedb/pg_do_reserve_open.h new file mode 100644 index 000000000..6e4c91fcd --- /dev/null +++ b/src/exchangedb/pg_do_reserve_open.h @@ -0,0 +1,55 @@ +/* + This file is part of TALER + Copyright (C) 2022 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 + Foundation; either version 3, or (at your option) any later version. + + TALER is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along with + TALER; see the file COPYING. If not, see + */ +/** + * @file pg_do_reserve_open.h + * @brief implementation of the do_reserve_open function + * @author Christian Grothoff + */ +#ifndef PG_DO_RESERVE_OPEN_H +#define PG_DO_RESERVE_OPEN_H + +#include "taler_util.h" +#include "taler_json_lib.h" +#include "taler_exchangedb_plugin.h" + + +/** + * Insert reserve close operation into database. + * + * @param cls closure + * @param reserve_pub which reserve is this about? + * @param execution_date when did we perform the transfer? + * @param receiver_account to which account do we transfer, in payto://-format + * @param wtid identifier for the wire transfer + * @param amount_with_fee amount we charged to the reserve + * @param closing_fee how high is the closing fee + * @return transaction status code + */ +enum GNUNET_DB_QueryStatus +TEH_PG_do_reserve_open ( + void *cls, + const struct TALER_ReservePublicKeyP *reserve_pub, + const struct TALER_Amount *total_paid, + uint32_t min_purse_limit, + const struct TALER_ReserveSignatureP *reserve_sig, + struct GNUNET_TIME_Timestamp desired_expiration, + struct GNUNET_TIME_Timestamp now, + const struct TALER_Amount *open_fee, + struct TALER_Amount *open_cost, + const struct GNUNET_TIME_Timestamp *final_expiration); + + +#endif diff --git a/src/exchangedb/pg_helper.h b/src/exchangedb/pg_helper.h new file mode 100644 index 000000000..e0a4be49d --- /dev/null +++ b/src/exchangedb/pg_helper.h @@ -0,0 +1,149 @@ +/* + This file is part of TALER + Copyright (C) 2022 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 + Foundation; either version 3, or (at your option) any later version. + + TALER is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along with + TALER; see the file COPYING. If not, see + */ +/** + * @file pg_helper.h + * @brief shared internal definitions for postgres DB plugin + * @author Christian Grothoff + */ +#ifndef PG_HELPER_H +#define PG_HELPER_H + + +/** + * Type of the "cls" argument given to each of the functions in + * our API. + */ +struct PostgresClosure +{ + + /** + * Our configuration. + */ + const struct GNUNET_CONFIGURATION_Handle *cfg; + + /** + * Directory with SQL statements to run to create tables. + */ + char *sql_dir; + + /** + * After how long should idle reserves be closed? + */ + struct GNUNET_TIME_Relative idle_reserve_expiration_time; + + /** + * After how long should reserves that have seen withdraw operations + * be garbage collected? + */ + struct GNUNET_TIME_Relative legal_reserve_expiration_time; + + /** + * What delay should we introduce before ready transactions + * are actually aggregated? + */ + struct GNUNET_TIME_Relative aggregator_shift; + + /** + * Which currency should we assume all amounts to be in? + */ + char *currency; + + /** + * Our base URL. + */ + char *exchange_url; + + /** + * Postgres connection handle. + */ + struct GNUNET_PQ_Context *conn; + + /** + * Name of the current transaction, for debugging. + */ + const char *transaction_name; + + /** + * Counts how often we have established a fresh @e conn + * to the database. Used to re-prepare statements. + */ + unsigned long long prep_gen; + + /** + * Did we initialize the prepared statements + * for this session? (To be replaced with @e prep_gen.) + */ + bool init; + +}; + + +/** + * Prepares SQL statement @a sql under @a name for + * connection @a pg once. + * Returns with #GNUNET_DB_STATUS_HARD_ERROR on failure. + * + * @param pg a `struct PostgresClosure` + * @param name name to prepare the statement under + * @param sql actual SQL text + */ +#define PREPARE(pg,name,sql) \ + do { \ + static unsigned long long prep_cnt; \ + \ + if (prep_cnt < pg->prep_gen) \ + { \ + struct GNUNET_PQ_PreparedStatement ps[] = { \ + GNUNET_PQ_make_prepare (name, sql, 0), \ + GNUNET_PQ_PREPARED_STATEMENT_END \ + }; \ + \ + if (GNUNET_OK != \ + GNUNET_PQ_prepare_statements (pg->conn, \ + ps)) \ + { \ + GNUNET_break (0); \ + return GNUNET_DB_STATUS_HARD_ERROR; \ + } \ + prep_cnt = pg->prep_gen; \ + } \ + } while (0) + + +/** + * Wrapper macro to add the currency from the plugin's state + * when fetching amounts from the database. + * + * @param field name of the database field to fetch amount from + * @param[out] amountp pointer to amount to set + */ +#define TALER_PQ_RESULT_SPEC_AMOUNT(field,amountp) TALER_PQ_result_spec_amount ( \ + field,pg->currency,amountp) + + +/** + * Wrapper macro to add the currency from the plugin's state + * when fetching amounts from the database. NBO variant. + * + * @param field name of the database field to fetch amount from + * @param[out] amountp pointer to amount to set + */ +#define TALER_PQ_RESULT_SPEC_AMOUNT_NBO(field, \ + amountp) TALER_PQ_result_spec_amount_nbo ( \ + field,pg->currency,amountp) + + +#endif diff --git a/src/exchangedb/pg_insert_close_request.c b/src/exchangedb/pg_insert_close_request.c new file mode 100644 index 000000000..3622149a7 --- /dev/null +++ b/src/exchangedb/pg_insert_close_request.c @@ -0,0 +1,64 @@ +/* + This file is part of TALER + Copyright (C) 2022 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 + Foundation; either version 3, or (at your option) any later version. + + TALER is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along with + TALER; see the file COPYING. If not, see + */ +/** + * @file pg_insert_close_request.c + * @brief Low-level (statement-level) Postgres database access for the exchange + * @author Christian Grothoff + */ +#include "platform.h" +#include "taler_error_codes.h" +#include "taler_dbevents.h" +#include "taler_pq_lib.h" +#include "pg_insert_close_request.h" +#include "pg_helper.h" + + +enum GNUNET_DB_QueryStatus +TEH_PG_insert_close_request ( + void *cls, + const struct TALER_ReservePublicKeyP *reserve_pub, + const char *payto_uri, + const struct TALER_ReserveSignatureP *reserve_sig, + struct GNUNET_TIME_Timestamp request_timestamp, + const struct TALER_Amount *closing_fee, + struct TALER_Amount *final_balance) +{ + struct PostgresClosure *pg = cls; + // FIXME: deal with payto_uri and closing_fee!! + struct GNUNET_PQ_QueryParam params[] = { + GNUNET_PQ_query_param_auto_from_type (reserve_pub), + GNUNET_PQ_query_param_timestamp (&request_timestamp), + GNUNET_PQ_query_param_auto_from_type (reserve_sig), + GNUNET_PQ_query_param_end + }; + struct GNUNET_PQ_ResultSpec rs[] = { + TALER_PQ_RESULT_SPEC_AMOUNT ("out_final_balance", + final_balance), + GNUNET_PQ_result_spec_end + }; + + PREPARE (pg, + "call_account_close", + "SELECT " + " out_final_balance_val" + ",out_final_balance_frac" + " FROM exchange_do_close_request" + " ($1, $2, $3)"); + return GNUNET_PQ_eval_prepared_singleton_select (pg->conn, + "call_account_close", + params, + rs); +} diff --git a/src/exchangedb/pg_insert_close_request.h b/src/exchangedb/pg_insert_close_request.h new file mode 100644 index 000000000..09404094f --- /dev/null +++ b/src/exchangedb/pg_insert_close_request.h @@ -0,0 +1,52 @@ +/* + This file is part of TALER + Copyright (C) 2022 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 + Foundation; either version 3, or (at your option) any later version. + + TALER is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along with + TALER; see the file COPYING. If not, see + */ +/** + * @file pg_insert_close_request.h + * @brief implementation of the insert_close_request function + * @author Christian Grothoff + */ +#ifndef PG_INSERT_CLOSE_REQUEST_H +#define PG_INSERT_CLOSE_REQUEST_H + +#include "taler_util.h" +#include "taler_json_lib.h" +#include "taler_exchangedb_plugin.h" + + +/** + * Function called to initiate closure of an account. + * + * @param cls the @e cls of this struct with the plugin-specific state + * @param reserve_pub public key of the account to close + * @param payto_uri where to wire the funds + * @param reserve_sig signature affiming that the account is to be closed + * @param request_timestamp time of the close request (client-side?) + * @param closing_fee closing fee to charge + * @param[out] final_balance set to the final balance in the account that will be wired back to the origin account + * @return transaction status code + */ +enum GNUNET_DB_QueryStatus +TEH_PG_insert_close_request ( + void *cls, + const struct TALER_ReservePublicKeyP *reserve_pub, + const char *payto_uri, + const struct TALER_ReserveSignatureP *reserve_sig, + struct GNUNET_TIME_Timestamp request_timestamp, + const struct TALER_Amount *closing_fee, + struct TALER_Amount *final_balance); + + +#endif diff --git a/src/exchangedb/pg_insert_reserve_open_deposit.c b/src/exchangedb/pg_insert_reserve_open_deposit.c new file mode 100644 index 000000000..c767bfeee --- /dev/null +++ b/src/exchangedb/pg_insert_reserve_open_deposit.c @@ -0,0 +1,64 @@ +/* + This file is part of TALER + Copyright (C) 2022 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 + Foundation; either version 3, or (at your option) any later version. + + TALER is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along with + TALER; see the file COPYING. If not, see + */ +/** + * @file pg_insert_reserve_open_deposit.c + * @brief Low-level (statement-level) Postgres database access for the exchange + * @author Christian Grothoff + */ +#include "platform.h" +#include "taler_error_codes.h" +#include "taler_dbevents.h" +#include "taler_pq_lib.h" +#include "pg_insert_reserve_open_deposit.h" +#include "pg_helper.h" + + +enum GNUNET_DB_QueryStatus +TEH_PG_insert_reserve_open_deposit ( + void *cls, + const struct TALER_CoinPublicInfo *cpi, + const struct TALER_CoinSpendSignatureP *coin_sig, + uint64_t known_coin_id, + const struct TALER_Amount *coin_total, + const struct TALER_ReserveSignatureP *reserve_sig, + bool *insufficient_funds) +{ + struct PostgresClosure *pg = cls; + struct GNUNET_PQ_QueryParam params[] = { + GNUNET_PQ_query_param_auto_from_type (&cpi->coin_pub), + GNUNET_PQ_query_param_uint64 (&known_coin_id), + GNUNET_PQ_query_param_auto_from_type (coin_sig), + GNUNET_PQ_query_param_auto_from_type (reserve_sig), + TALER_PQ_query_param_amount (coin_total), + GNUNET_PQ_query_param_end + }; + struct GNUNET_PQ_ResultSpec rs[] = { + GNUNET_PQ_result_spec_bool ("insufficient_funds", + insufficient_funds), + GNUNET_PQ_result_spec_end + }; + + PREPARE (pg, + "insert_reserve_open_deposit", + "SELECT " + " insufficient_funds" + " FROM exchange_do_reserve_open_deposit" + " ($1,$2,$3,$4,$5,$6);"); + return GNUNET_PQ_eval_prepared_singleton_select (pg->conn, + "insert_reserve_open_deposit", + params, + rs); +} diff --git a/src/exchangedb/pg_insert_reserve_open_deposit.h b/src/exchangedb/pg_insert_reserve_open_deposit.h new file mode 100644 index 000000000..335fda52d --- /dev/null +++ b/src/exchangedb/pg_insert_reserve_open_deposit.h @@ -0,0 +1,52 @@ +/* + This file is part of TALER + Copyright (C) 2022 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 + Foundation; either version 3, or (at your option) any later version. + + TALER is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along with + TALER; see the file COPYING. If not, see + */ +/** + * @file pg_insert_reserve_open_deposit.h + * @brief implementation of the insert_reserve_open_deposit function + * @author Christian Grothoff + */ +#ifndef PG_INSERT_RESERVE_OPEN_DEPOSIT_H +#define PG_INSERT_RESERVE_OPEN_DEPOSIT_H + +#include "taler_util.h" +#include "taler_json_lib.h" +#include "taler_exchangedb_plugin.h" + + +/** + * Insert reserve open coin deposit data into database. + * Subtracts the @a coin_total from the coin's balance. + * + * @param cls closure + * @param cpi public information about the coin + * @param coin_sig signature with @e coin_pub of type #TALER_SIGNATURE_WALLET_RESERVE_OPEN_DEPOSIT + * @param known_coin_id ID of the coin in the known_coins table + * @param coin_total amount to be spent of the coin (including deposit fee) + * @param reserve_sig signature by the reserve affirming the open operation + * @param[out] insufficient_funds set to true if the coin's balance is insufficient, otherwise to false + * @return transaction status code, 0 if operation is already in the DB + */ +enum GNUNET_DB_QueryStatus +TEH_PG_insert_reserve_open_deposit ( + void *cls, + const struct TALER_CoinPublicInfo *cpi, + const struct TALER_CoinSpendSignatureP *coin_sig, + uint64_t known_coin_id, + const struct TALER_Amount *coin_total, + const struct TALER_ReserveSignatureP *reserve_sig, + bool *insufficient_funds); + +#endif diff --git a/src/exchangedb/pg_iterate_kyc_reference.c b/src/exchangedb/pg_iterate_kyc_reference.c new file mode 100644 index 000000000..e24a22bf2 --- /dev/null +++ b/src/exchangedb/pg_iterate_kyc_reference.c @@ -0,0 +1,58 @@ +/* + This file is part of TALER + Copyright (C) 2022 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 + Foundation; either version 3, or (at your option) any later version. + + TALER is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along with + TALER; see the file COPYING. If not, see + */ +/** + * @file pg_iterate_kyc_reference.c + * @brief Low-level (statement-level) Postgres database access for the exchange + * @author Christian Grothoff + */ +#include "platform.h" +#include "taler_error_codes.h" +#include "taler_dbevents.h" +#include "taler_pq_lib.h" +#include "pg_iterate_kyc_reference.h" +#include "pg_helper.h" + + +enum GNUNET_DB_QueryStatus +TEH_PG_iterate_kyc_reference ( + void *cls, + const struct TALER_PaytoHashP *h_payto, + TALER_EXCHANGEDB_LegitimizationProcessCallback lpc, + void *lpc_cls) +{ + struct PostgresClosure *pg = cls; + struct GNUNET_PQ_QueryParam params[] = { + GNUNET_PQ_query_param_auto_from_type (h_payto), + GNUNET_PQ_query_param_end + }; + // FIXME: everything from here is copy*paste + struct GNUNET_PQ_ResultSpec rs[] = { + GNUNET_PQ_result_spec_bool ("insufficient_funds", + insufficient_funds), + GNUNET_PQ_result_spec_end + }; + + PREPARE (pg, + "iterate_kyc_reference", + "SELECT " + " insufficient_funds" + " FROM exchange_do_reserve_open_deposit" + " ($1,$2,$3,$4,$5,$6);"); + return GNUNET_PQ_eval_prepared_singleton_select (pg->conn, + "iterate_kyc_reference", + params, + rs); +} diff --git a/src/exchangedb/pg_iterate_kyc_reference.h b/src/exchangedb/pg_iterate_kyc_reference.h new file mode 100644 index 000000000..0242fdcf1 --- /dev/null +++ b/src/exchangedb/pg_iterate_kyc_reference.h @@ -0,0 +1,46 @@ +/* + This file is part of TALER + Copyright (C) 2022 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 + Foundation; either version 3, or (at your option) any later version. + + TALER is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along with + TALER; see the file COPYING. If not, see + */ +/** + * @file pg_iterate_kyc_reference.h + * @brief implementation of the iterate_kyc_reference function + * @author Christian Grothoff + */ +#ifndef PG_ITERATE_KYC_REFERENCE_H +#define PG_ITERATE_KYC_REFERENCE_H + +#include "taler_util.h" +#include "taler_json_lib.h" +#include "taler_exchangedb_plugin.h" + + +/** + * Call us on KYC legitimization processes satisfied and not expired for the + * given account. + * + * @param cls the @e cls of this struct with the plugin-specific state + * @param h_payto account identifier + * @param lpc function to call for each satisfied KYC legitimization process + * @param lpc_cls closure for @a lpc + * @return transaction status code + */ +enum GNUNET_DB_QueryStatus +TEH_PG_iterate_kyc_reference ( + void *cls, + const struct TALER_PaytoHashP *h_payto, + TALER_EXCHANGEDB_LegitimizationProcessCallback lpc, + void *lpc_cls); + +#endif diff --git a/src/exchangedb/pg_iterate_reserve_close_info.c b/src/exchangedb/pg_iterate_reserve_close_info.c new file mode 100644 index 000000000..e9a5f6648 --- /dev/null +++ b/src/exchangedb/pg_iterate_reserve_close_info.c @@ -0,0 +1,63 @@ +/* + This file is part of TALER + Copyright (C) 2022 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 + Foundation; either version 3, or (at your option) any later version. + + TALER is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along with + TALER; see the file COPYING. If not, see + */ +/** + * @file pg_iterate_reserve_close_info.c + * @brief Low-level (statement-level) Postgres database access for the exchange + * @author Christian Grothoff + */ +#include "platform.h" +#include "taler_error_codes.h" +#include "taler_dbevents.h" +#include "taler_pq_lib.h" +#include "pg_insert_reserve_open_deposit.h" +#include "pg_helper.h" + + +enum GNUNET_DB_QueryStatus +TEH_PG_iterate_reserve_close_info ( + void *cls, + const struct TALER_PaytoHashP *h_payto, + struct GNUNET_TIME_Absolute time_limit, + TALER_EXCHANGEDB_KycAmountCallback kac, + void *kac_cls) +{ + struct PostgresClosure *pg = cls; + // FIXME: everything from here is copy&paste + struct GNUNET_PQ_QueryParam params[] = { + GNUNET_PQ_query_param_auto_from_type (&cpi->coin_pub), + GNUNET_PQ_query_param_uint64 (&known_coin_id), + GNUNET_PQ_query_param_auto_from_type (coin_sig), + GNUNET_PQ_query_param_auto_from_type (reserve_sig), + TALER_PQ_query_param_amount (coin_total), + GNUNET_PQ_query_param_end + }; + struct GNUNET_PQ_ResultSpec rs[] = { + GNUNET_PQ_result_spec_bool ("insufficient_funds", + insufficient_funds), + GNUNET_PQ_result_spec_end + }; + + PREPARE (pg, + "insert_reserve_open_deposit", + "SELECT " + " insufficient_funds" + " FROM exchange_do_reserve_open_deposit" + " ($1,$2,$3,$4,$5,$6);"); + return GNUNET_PQ_eval_prepared_singleton_select (pg->conn, + "insert_reserve_open_deposit", + params, + rs); +} diff --git a/src/exchangedb/pg_iterate_reserve_close_info.h b/src/exchangedb/pg_iterate_reserve_close_info.h new file mode 100644 index 000000000..a7e7c8d4a --- /dev/null +++ b/src/exchangedb/pg_iterate_reserve_close_info.h @@ -0,0 +1,50 @@ +/* + This file is part of TALER + Copyright (C) 2022 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 + Foundation; either version 3, or (at your option) any later version. + + TALER is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along with + TALER; see the file COPYING. If not, see + */ +/** + * @file pg_iterate_reserve_close_info.h + * @brief implementation of the iterate_reserve_close_info function + * @author Christian Grothoff + */ +#ifndef PG_ITERATE_RESERVE_CLOSE_INFO_H +#define PG_ITERATE_RESERVE_CLOSE_INFO_H + +#include "taler_util.h" +#include "taler_json_lib.h" +#include "taler_exchangedb_plugin.h" + + +/** + * Select information needed for KYC checks on reserve close: historic + * reserve closures going to the same account. + * + * @param cls closure + * @param h_payto which target account is this about? + * @param h_payto account identifier + * @param time_limit oldest transaction that could be relevant + * @param kac function to call for each applicable amount, in reverse chronological order (or until @a kac aborts by returning anything except #GNUNET_OK). + * @param kac_cls closure for @a kac + * @return transaction status code, @a kac aborting with #GNUNET_NO is not an error + */ +enum GNUNET_DB_QueryStatus +TEH_PG_iterate_reserve_close_info ( + void *cls, + const struct TALER_PaytoHashP *h_payto, + struct GNUNET_TIME_Absolute time_limit, + TALER_EXCHANGEDB_KycAmountCallback kac, + void *kac_cls); + + +#endif diff --git a/src/exchangedb/pg_select_reserve_close_info.c b/src/exchangedb/pg_select_reserve_close_info.c new file mode 100644 index 000000000..472ec27c9 --- /dev/null +++ b/src/exchangedb/pg_select_reserve_close_info.c @@ -0,0 +1,62 @@ +/* + This file is part of TALER + Copyright (C) 2022 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 + Foundation; either version 3, or (at your option) any later version. + + TALER is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along with + TALER; see the file COPYING. If not, see + */ +/** + * @file pg_select_reserve_close_info.c + * @brief Low-level (statement-level) Postgres database access for the exchange + * @author Christian Grothoff + */ +#include "platform.h" +#include "taler_error_codes.h" +#include "taler_dbevents.h" +#include "taler_pq_lib.h" +#include "pg_insert_reserve_open_deposit.h" +#include "pg_helper.h" + + +enum GNUNET_DB_QueryStatus +TEH_PG_select_reserve_close_info ( + void *cls, + const struct TALER_ReservePublicKeyP *reserve_pub, + struct TALER_Amount *balance, + char **payto_uri) +{ + struct PostgresClosure *pg = cls; + // FIXME: everything from here is copy*paste! + struct GNUNET_PQ_QueryParam params[] = { + GNUNET_PQ_query_param_auto_from_type (&cpi->coin_pub), + GNUNET_PQ_query_param_uint64 (&known_coin_id), + GNUNET_PQ_query_param_auto_from_type (coin_sig), + GNUNET_PQ_query_param_auto_from_type (reserve_sig), + TALER_PQ_query_param_amount (coin_total), + GNUNET_PQ_query_param_end + }; + struct GNUNET_PQ_ResultSpec rs[] = { + GNUNET_PQ_result_spec_bool ("insufficient_funds", + insufficient_funds), + GNUNET_PQ_result_spec_end + }; + + PREPARE (pg, + "insert_reserve_open_deposit", + "SELECT " + " insufficient_funds" + " FROM exchange_do_reserve_open_deposit" + " ($1,$2,$3,$4,$5,$6);"); + return GNUNET_PQ_eval_prepared_singleton_select (pg->conn, + "insert_reserve_open_deposit", + params, + rs); +} diff --git a/src/exchangedb/pg_select_reserve_close_info.h b/src/exchangedb/pg_select_reserve_close_info.h new file mode 100644 index 000000000..2b90ffd05 --- /dev/null +++ b/src/exchangedb/pg_select_reserve_close_info.h @@ -0,0 +1,49 @@ +/* + This file is part of TALER + Copyright (C) 2022 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 + Foundation; either version 3, or (at your option) any later version. + + TALER is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along with + TALER; see the file COPYING. If not, see + */ +/** + * @file pg_select_reserve_close_info.h + * @brief implementation of the select_reserve_close_info function + * @author Christian Grothoff + */ +#ifndef PG_SELECT_RESERVE_CLOSE_INFO_H +#define PG_SELECT_RESERVE_CLOSE_INFO_H + +#include "taler_util.h" +#include "taler_json_lib.h" +#include "taler_exchangedb_plugin.h" + + +/** + * Select information needed to see if we can close + * a reserve. + * + * @param cls closure + * @param reserve_pub which reserve is this about? + * @param[out] balance current reserve balance + * @param[out] payto_uri set to URL of account that + * originally funded the reserve; + * could be set to NULL if not known + * @return transaction status code, 0 if reserve unknown + */ +enum GNUNET_DB_QueryStatus +TEH_PG_select_reserve_close_info ( + void *cls, + const struct TALER_ReservePublicKeyP *reserve_pub, + struct TALER_Amount *balance, + char **payto_uri); + + +#endif diff --git a/src/exchangedb/plugin_exchangedb_postgres.c b/src/exchangedb/plugin_exchangedb_postgres.c index e3da5216b..341364f9f 100644 --- a/src/exchangedb/plugin_exchangedb_postgres.c +++ b/src/exchangedb/plugin_exchangedb_postgres.c @@ -29,6 +29,9 @@ #include "taler_util.h" #include "taler_json_lib.h" #include "taler_exchangedb_plugin.h" +#include "pg_helper.h" +#include "pg_insert_close_request.h" +#include "pg_insert_reserve_open_deposit.h" #include #include #include @@ -42,26 +45,6 @@ */ #define AUTO_EXPLAIN 1 -/** - * Wrapper macro to add the currency from the plugin's state - * when fetching amounts from the database. - * - * @param field name of the database field to fetch amount from - * @param[out] amountp pointer to amount to set - */ -#define TALER_PQ_RESULT_SPEC_AMOUNT(field,amountp) TALER_PQ_result_spec_amount ( \ - field,pg->currency,amountp) - -/** - * Wrapper macro to add the currency from the plugin's state - * when fetching amounts from the database. NBO variant. - * - * @param field name of the database field to fetch amount from - * @param[out] amountp pointer to amount to set - */ -#define TALER_PQ_RESULT_SPEC_AMOUNT_NBO(field, \ - amountp) TALER_PQ_result_spec_amount_nbo ( \ - field,pg->currency,amountp) /** * Log a really unexpected PQ error with all the details we can get hold of. @@ -81,69 +64,6 @@ } while (0) -/** - * Type of the "cls" argument given to each of the functions in - * our API. - */ -struct PostgresClosure -{ - - /** - * Our configuration. - */ - const struct GNUNET_CONFIGURATION_Handle *cfg; - - /** - * Directory with SQL statements to run to create tables. - */ - char *sql_dir; - - /** - * After how long should idle reserves be closed? - */ - struct GNUNET_TIME_Relative idle_reserve_expiration_time; - - /** - * After how long should reserves that have seen withdraw operations - * be garbage collected? - */ - struct GNUNET_TIME_Relative legal_reserve_expiration_time; - - /** - * What delay should we introduce before ready transactions - * are actually aggregated? - */ - struct GNUNET_TIME_Relative aggregator_shift; - - /** - * Which currency should we assume all amounts to be in? - */ - char *currency; - - /** - * Our base URL. - */ - char *exchange_url; - - /** - * Postgres connection handle. - */ - struct GNUNET_PQ_Context *conn; - - /** - * Name of the current transaction, for debugging. - */ - const char *transaction_name; - - /** - * Did we initialize the prepared statements - * for this session? - */ - bool init; - -}; - - /** * Drop all Taler tables. This should only be used by testcases. * @@ -4467,15 +4387,7 @@ prepare_statements (struct PostgresClosure *pg) " FROM exchange_do_history_request" " ($1, $2, $3, $4, $5)", 5), - /* Used in #postgres_insert_close_request() */ - GNUNET_PQ_make_prepare ( - "call_account_close", - "SELECT " - " out_final_balance_val" - ",out_final_balance_frac" - " FROM exchange_do_close_request" - " ($1, $2, $3)", - 3), + /* Used in #postgres_insert_kyc_requirement_for_account() */ GNUNET_PQ_make_prepare ( "insert_legitimization_requirement", @@ -4674,6 +4586,7 @@ internal_setup (struct PostgresClosure *pg, NULL); if (NULL == db_conn) return GNUNET_SYSERR; + pg->prep_gen++; pg->conn = db_conn; } if (NULL == pg->transaction_name) @@ -16236,44 +16149,6 @@ postgres_insert_history_request ( } -/** - * Function called to initiate closure of an account. - * - * @param cls the @e cls of this struct with the plugin-specific state - * @param reserve_pub public key of the account to close - * @param reserve_sig signature affiming that the account is to be closed - * @param request_timestamp time of the close request (client-side?) - * @param[out] final_balance set to the final balance in the account that will be wired back to the origin account - * @return transaction status code - */ -static enum GNUNET_DB_QueryStatus -postgres_insert_close_request ( - void *cls, - const struct TALER_ReservePublicKeyP *reserve_pub, - const struct TALER_ReserveSignatureP *reserve_sig, - struct GNUNET_TIME_Timestamp request_timestamp, - struct TALER_Amount *final_balance) -{ - struct PostgresClosure *pg = cls; - struct GNUNET_PQ_QueryParam params[] = { - GNUNET_PQ_query_param_auto_from_type (reserve_pub), - GNUNET_PQ_query_param_timestamp (&request_timestamp), - GNUNET_PQ_query_param_auto_from_type (reserve_sig), - GNUNET_PQ_query_param_end - }; - struct GNUNET_PQ_ResultSpec rs[] = { - TALER_PQ_RESULT_SPEC_AMOUNT ("out_final_balance", - final_balance), - GNUNET_PQ_result_spec_end - }; - - return GNUNET_PQ_eval_prepared_singleton_select (pg->conn, - "call_account_close", - params, - rs); -} - - /** * Function called to persist a request to drain profits. * @@ -17390,7 +17265,7 @@ libtaler_plugin_exchangedb_postgres_init (void *cls) plugin->insert_history_request = &postgres_insert_history_request; plugin->insert_close_request - = &postgres_insert_close_request; + = &TEH_PG_insert_close_request; plugin->insert_drain_profit = &postgres_insert_drain_profit; plugin->profit_drains_get_pending -- cgit v1.2.3