diff options
author | Christian Grothoff <christian@grothoff.org> | 2024-01-04 01:03:34 +0100 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2024-01-04 01:03:34 +0100 |
commit | d2d2d773cb2cc23f198d07392bab463beb2e2e04 (patch) | |
tree | 4f8eb381f1f64d47cc3668b40889416ea35fc31f /src/backenddb | |
parent | 881381503f7f51b294c2740e9fcf7f28ef43f973 (diff) | |
download | merchant-d2d2d773cb2cc23f198d07392bab463beb2e2e04.tar.gz merchant-d2d2d773cb2cc23f198d07392bab463beb2e2e04.tar.bz2 merchant-d2d2d773cb2cc23f198d07392bab463beb2e2e04.zip |
remove unnecessary DB interaction in GET private/orders/ID
Diffstat (limited to 'src/backenddb')
-rw-r--r-- | src/backenddb/Makefile.am | 2 | ||||
-rw-r--r-- | src/backenddb/pg_lookup_contract_terms.c | 8 | ||||
-rw-r--r-- | src/backenddb/pg_lookup_contract_terms.h | 2 | ||||
-rw-r--r-- | src/backenddb/pg_lookup_contract_terms3.c | 99 | ||||
-rw-r--r-- | src/backenddb/pg_lookup_contract_terms3.h | 56 | ||||
-rw-r--r-- | src/backenddb/pg_lookup_payment_status.c | 98 | ||||
-rw-r--r-- | src/backenddb/pg_lookup_payment_status.h | 45 | ||||
-rw-r--r-- | src/backenddb/plugin_merchantdb_postgres.c | 8 | ||||
-rw-r--r-- | src/backenddb/test_merchantdb.c | 76 |
9 files changed, 200 insertions, 194 deletions
diff --git a/src/backenddb/Makefile.am b/src/backenddb/Makefile.am index a63a56b1..1818a877 100644 --- a/src/backenddb/Makefile.am +++ b/src/backenddb/Makefile.am @@ -117,6 +117,7 @@ libtaler_plugin_merchantdb_postgres_la_SOURCES = \ pg_insert_order.h pg_insert_order.c \ pg_unlock_inventory.h pg_unlock_inventory.c \ pg_insert_order_lock.h pg_insert_order_lock.c \ + pg_lookup_contract_terms3.h pg_lookup_contract_terms3.c \ pg_lookup_contract_terms2.h pg_lookup_contract_terms2.c \ pg_lookup_contract_terms.h pg_lookup_contract_terms.c \ pg_insert_contract_terms.h pg_insert_contract_terms.c \ @@ -131,7 +132,6 @@ libtaler_plugin_merchantdb_postgres_la_SOURCES = \ pg_refund_coin.h pg_refund_coin.c \ pg_lookup_order_status.h pg_lookup_order_status.c \ pg_lookup_order_status_by_serial.h pg_lookup_order_status_by_serial.c \ - pg_lookup_payment_status.h pg_lookup_payment_status.c \ pg_lookup_deposits_by_order.h pg_lookup_deposits_by_order.c \ pg_lookup_transfer_details_by_order.h pg_lookup_transfer_details_by_order.c \ pg_mark_order_wired.h pg_mark_order_wired.c \ diff --git a/src/backenddb/pg_lookup_contract_terms.c b/src/backenddb/pg_lookup_contract_terms.c index e1f24ac4..9588eef4 100644 --- a/src/backenddb/pg_lookup_contract_terms.c +++ b/src/backenddb/pg_lookup_contract_terms.c @@ -1,6 +1,6 @@ /* This file is part of TALER - Copyright (C) 2022 Taler Systems SA + Copyright (C) 2022-2024 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 @@ -17,6 +17,7 @@ * @file backenddb/pg_lookup_contract_terms.c * @brief Implementation of the lookup_contract_terms function for Postgres * @author Iván Ávalos + * @author Christian Grothoff */ #include "platform.h" #include <taler/taler_error_codes.h> @@ -25,6 +26,7 @@ #include "pg_lookup_contract_terms.h" #include "pg_helper.h" + enum GNUNET_DB_QueryStatus TMH_PG_lookup_contract_terms ( void *cls, @@ -32,7 +34,6 @@ TMH_PG_lookup_contract_terms ( const char *order_id, json_t **contract_terms, uint64_t *order_serial, - bool *paid, struct TALER_ClaimTokenP *claim_token) { struct PostgresClosure *pg = cls; @@ -49,8 +50,6 @@ TMH_PG_lookup_contract_terms ( contract_terms), GNUNET_PQ_result_spec_uint64 ("order_serial", order_serial), - GNUNET_PQ_result_spec_bool ("paid", - paid), GNUNET_PQ_result_spec_auto_from_type ("claim_token", &ct), GNUNET_PQ_result_spec_end @@ -63,7 +62,6 @@ TMH_PG_lookup_contract_terms ( " contract_terms" ",order_serial" ",claim_token" - ",paid" " FROM merchant_contract_terms" " WHERE order_id=$2" " AND merchant_serial=" diff --git a/src/backenddb/pg_lookup_contract_terms.h b/src/backenddb/pg_lookup_contract_terms.h index 2ab1bb16..fa757ed1 100644 --- a/src/backenddb/pg_lookup_contract_terms.h +++ b/src/backenddb/pg_lookup_contract_terms.h @@ -33,7 +33,6 @@ * @param order_id order_id used to lookup. * @param[out] contract_terms where to store the result, NULL to only check for existence * @param[out] order_serial set to the order's serial number - * @param[out] paid set to true if the order is fully paid * @param[out] claim_token set to token to use for access control * @return transaction status */ @@ -44,7 +43,6 @@ TMH_PG_lookup_contract_terms ( const char *order_id, json_t **contract_terms, uint64_t *order_serial, - bool *paid, struct TALER_ClaimTokenP *claim_token); #endif diff --git a/src/backenddb/pg_lookup_contract_terms3.c b/src/backenddb/pg_lookup_contract_terms3.c new file mode 100644 index 00000000..ef955a51 --- /dev/null +++ b/src/backenddb/pg_lookup_contract_terms3.c @@ -0,0 +1,99 @@ +/* + 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 <http://www.gnu.org/licenses/> + */ +/** + * @file backenddb/pg_lookup_contract_terms3.c + * @brief Implementation of the lookup_contract_terms3 function for Postgres + * @author Iván Ávalos + * @author Christian Grothoff + */ +#include "platform.h" +#include <taler/taler_error_codes.h> +#include <taler/taler_dbevents.h> +#include <taler/taler_pq_lib.h> +#include "pg_lookup_contract_terms3.h" +#include "pg_helper.h" + + +enum GNUNET_DB_QueryStatus +TMH_PG_lookup_contract_terms3 ( + void *cls, + const char *instance_id, + const char *order_id, + const char *session_id, + json_t **contract_terms, + uint64_t *order_serial, + bool *paid, + bool *wired, + bool *session_matches, + struct TALER_ClaimTokenP *claim_token) +{ + struct PostgresClosure *pg = cls; + enum GNUNET_DB_QueryStatus qs; + struct TALER_ClaimTokenP ct; + struct GNUNET_PQ_QueryParam params[] = { + GNUNET_PQ_query_param_string (instance_id), + GNUNET_PQ_query_param_string (order_id), + NULL == session_id + ? GNUNET_PQ_query_param_null () + : GNUNET_PQ_query_param_string (session_id), + GNUNET_PQ_query_param_end + }; + struct GNUNET_PQ_ResultSpec rs[] = { + /* contract_terms must be first! */ + TALER_PQ_result_spec_json ("contract_terms", + contract_terms), + GNUNET_PQ_result_spec_uint64 ("order_serial", + order_serial), + GNUNET_PQ_result_spec_bool ("paid", + paid), + GNUNET_PQ_result_spec_bool ("wired", + wired), + GNUNET_PQ_result_spec_allow_null ( + GNUNET_PQ_result_spec_bool ("session_matches", + session_matches), + NULL), + GNUNET_PQ_result_spec_auto_from_type ("claim_token", + &ct), + GNUNET_PQ_result_spec_end + }; + + *session_matches = false; + check_connection (pg); + PREPARE (pg, + "lookup_contract_terms3", + "SELECT" + " contract_terms" + ",order_serial" + ",claim_token" + ",paid" + ",wired" + ",(session_id=$3) AS session_matches" + " FROM merchant_contract_terms" + " WHERE order_id=$2" + " AND merchant_serial=" + " (SELECT merchant_serial" + " FROM merchant_instances" + " WHERE merchant_id=$1)"); + qs = GNUNET_PQ_eval_prepared_singleton_select (pg->conn, + "lookup_contract_terms3", + params, + (NULL != contract_terms) + ? rs + : &rs[1]); + if (NULL != claim_token) + *claim_token = ct; + return qs; +} diff --git a/src/backenddb/pg_lookup_contract_terms3.h b/src/backenddb/pg_lookup_contract_terms3.h new file mode 100644 index 00000000..d1cc78a2 --- /dev/null +++ b/src/backenddb/pg_lookup_contract_terms3.h @@ -0,0 +1,56 @@ +/* + 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 <http://www.gnu.org/licenses/> + */ +/** + * @file backenddb/pg_lookup_contract_terms3.h + * @brief implementation of the lookup_contract_terms3 function for Postgres + * @author Iván Ávalos + */ +#ifndef PG_LOOKUP_CONTRACT_TERMS3_H +#define PG_LOOKUP_CONTRACT_TERMS3_H + +#include <taler/taler_util.h> +#include <taler/taler_json_lib.h> +#include "taler_merchantdb_plugin.h" + +/** + * Retrieve contract terms given its @a order_id + * + * @param cls closure + * @param instance_id instance's identifier + * @param order_id order_id used to lookup. + * @param session_id session_id to compare, can be NULL + * @param[out] contract_terms where to store the result, NULL to only check for existence + * @param[out] order_serial set to the order's serial number + * @param[out] paid set to true if the order is fully paid + * @param[out] wired set to true if the exchange wired the funds + * @param[out] session_matches set to true if @a session_id matches session stored for this contract + * @param[out] claim_token set to token to use for access control + * @return transaction status + */ +enum GNUNET_DB_QueryStatus +TMH_PG_lookup_contract_terms3 ( + void *cls, + const char *instance_id, + const char *order_id, + const char *session_id, + json_t **contract_terms, + uint64_t *order_serial, + bool *paid, + bool *wired, + bool *session_matches, + struct TALER_ClaimTokenP *claim_token); + +#endif diff --git a/src/backenddb/pg_lookup_payment_status.c b/src/backenddb/pg_lookup_payment_status.c deleted file mode 100644 index fc4e5d94..00000000 --- a/src/backenddb/pg_lookup_payment_status.c +++ /dev/null @@ -1,98 +0,0 @@ -/* - 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 <http://www.gnu.org/licenses/> - */ -/** - * @file backenddb/pg_lookup_payment_status.c - * @brief Implementation of the lookup_payment_status function for Postgres - * @author Iván Ávalos - */ -#include "platform.h" -#include <taler/taler_error_codes.h> -#include <taler/taler_dbevents.h> -#include <taler/taler_pq_lib.h> -#include "pg_lookup_payment_status.h" -#include "pg_helper.h" - -enum GNUNET_DB_QueryStatus -TMH_PG_lookup_payment_status (void *cls, - uint64_t order_serial, - const char *session_id, - bool *paid, - bool *wired) -{ - struct PostgresClosure *pg = cls; - uint8_t paid8; - uint8_t wired8; - enum GNUNET_DB_QueryStatus qs; - struct GNUNET_PQ_ResultSpec rs[] = { - GNUNET_PQ_result_spec_auto_from_type ("paid", - &paid8), - GNUNET_PQ_result_spec_auto_from_type ("wired", - &wired8), - GNUNET_PQ_result_spec_end - }; - check_connection (pg); - if (NULL == session_id) - { - struct GNUNET_PQ_QueryParam params[] = { - GNUNET_PQ_query_param_uint64 (&order_serial), - GNUNET_PQ_query_param_end - }; - - PREPARE (pg, - "lookup_payment_status", - "SELECT" - " wired" - ",paid" - " FROM merchant_contract_terms" - " WHERE order_serial=$1"); - qs = GNUNET_PQ_eval_prepared_singleton_select (pg->conn, - "lookup_payment_status", - params, - rs); - } - else - { - struct GNUNET_PQ_QueryParam params[] = { - GNUNET_PQ_query_param_uint64 (&order_serial), - GNUNET_PQ_query_param_string (session_id), - GNUNET_PQ_query_param_end - }; - - PREPARE (pg, - "lookup_payment_status_session_id", - "SELECT" - " wired" - ",paid" - " FROM merchant_contract_terms" - " WHERE order_serial=$1" - " AND session_id=$2"); - qs = GNUNET_PQ_eval_prepared_singleton_select (pg->conn, - "lookup_payment_status_session_id", - params, - rs); - } - if (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT == qs) - { - *paid = (0 != paid8); - *wired = (0 != wired8); - } - else - { - *paid = false; /* just to be safe(r) */ - *wired = false; /* just to be safe(r) */ - } - return qs; -} diff --git a/src/backenddb/pg_lookup_payment_status.h b/src/backenddb/pg_lookup_payment_status.h deleted file mode 100644 index a46ee31e..00000000 --- a/src/backenddb/pg_lookup_payment_status.h +++ /dev/null @@ -1,45 +0,0 @@ -/* - 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 <http://www.gnu.org/licenses/> - */ -/** - * @file backenddb/pg_lookup_payment_status.h - * @brief implementation of the lookup_payment_status function for Postgres - * @author Iván Ávalos - */ -#ifndef PG_LOOKUP_PAYMENT_STATUS_H -#define PG_LOOKUP_PAYMENT_STATUS_H - -#include <taler/taler_util.h> -#include <taler/taler_json_lib.h> -#include "taler_merchantdb_plugin.h" - -/** - * Retrieve payment and wire status for a given @a order_serial and session ID. - * - * @param cls closure - * @param order_serial identifies the order - * @param session_id session for which to check the payment status, NULL for any - * @param[out] paid set to the payment status of the contract - * @param[out] wired set to the wire transfer status of the exchange payment - * @return transaction status - */ -enum GNUNET_DB_QueryStatus -TMH_PG_lookup_payment_status (void *cls, - uint64_t order_serial, - const char *session_id, - bool *paid, - bool *wired); - -#endif diff --git a/src/backenddb/plugin_merchantdb_postgres.c b/src/backenddb/plugin_merchantdb_postgres.c index 30f5c169..0e538ba9 100644 --- a/src/backenddb/plugin_merchantdb_postgres.c +++ b/src/backenddb/plugin_merchantdb_postgres.c @@ -1,6 +1,6 @@ /* This file is part of TALER - (C) 2014--2023 Taler Systems SA + (C) 2014--2024 Taler Systems SA TALER is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software @@ -76,6 +76,7 @@ #include "pg_insert_order.h" #include "pg_unlock_inventory.h" #include "pg_insert_order_lock.h" +#include "pg_lookup_contract_terms3.h" #include "pg_lookup_contract_terms2.h" #include "pg_lookup_contract_terms.h" #include "pg_insert_contract_terms.h" @@ -96,7 +97,6 @@ #include "pg_refund_coin.h" #include "pg_lookup_order_status.h" #include "pg_lookup_order_status_by_serial.h" -#include "pg_lookup_payment_status.h" #include "pg_lookup_deposits_by_order.h" #include "pg_lookup_transfer_details_by_order.h" #include "pg_mark_order_wired.h" @@ -456,6 +456,8 @@ libtaler_plugin_merchantdb_postgres_init (void *cls) = &TMH_PG_lookup_contract_terms; plugin->lookup_contract_terms2 = &TMH_PG_lookup_contract_terms2; + plugin->lookup_contract_terms3 + = &TMH_PG_lookup_contract_terms3; plugin->insert_contract_terms = &TMH_PG_insert_contract_terms; plugin->update_contract_terms @@ -480,8 +482,6 @@ libtaler_plugin_merchantdb_postgres_init (void *cls) = &TMH_PG_lookup_order_status; plugin->lookup_order_status_by_serial = &TMH_PG_lookup_order_status_by_serial; - plugin->lookup_payment_status - = &TMH_PG_lookup_payment_status; plugin->lookup_deposits_by_order = &TMH_PG_lookup_deposits_by_order; plugin->lookup_transfer_details_by_order diff --git a/src/backenddb/test_merchantdb.c b/src/backenddb/test_merchantdb.c index 3cbe0467..d85bccd3 100644 --- a/src/backenddb/test_merchantdb.c +++ b/src/backenddb/test_merchantdb.c @@ -1665,7 +1665,6 @@ test_lookup_contract_terms (const struct InstanceData *instance, { json_t *contract = NULL; uint64_t order_serial; - bool paid; if (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT != plugin->lookup_contract_terms (plugin->cls, @@ -1673,7 +1672,6 @@ test_lookup_contract_terms (const struct InstanceData *instance, order->id, &contract, &order_serial, - &paid, NULL)) { GNUNET_log (GNUNET_ERROR_TYPE_ERROR, @@ -1843,22 +1841,36 @@ test_lookup_order_by_fulfillment (const struct InstanceData *instance, * @return 0 on success, 1 otherwise. */ static int -test_lookup_payment_status (uint64_t order_id, +test_lookup_payment_status (const char *instance_id, + const char *order_id, const char *session_id, bool expected_paid, bool expected_wired) { bool paid; bool wired; + bool matches; + uint64_t os; + TEST_COND_RET_ON_FAIL (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT == - plugin->lookup_payment_status (plugin->cls, - order_id, - session_id, - &paid, - &wired), + plugin->lookup_contract_terms3 (plugin->cls, + instance_id, + order_id, + session_id, + NULL, + &os, + &paid, + &wired, + &matches, + NULL), "Lookup payment status failed\n"); - if ((expected_paid != paid) || - (expected_wired != wired)) + if ( (NULL != session_id) && (! matches) ) + { + paid = false; + wired = false; + } + if ( (expected_paid != paid) || + (expected_wired != wired) ) { GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Lookup payment status failed\n"); @@ -2051,7 +2063,6 @@ run_test_orders (struct TestOrders_Closure *cls) { json_t *lookup_contract = NULL; uint64_t lookup_order_serial; - bool paid; if (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS != plugin->lookup_contract_terms (plugin->cls, @@ -2059,7 +2070,6 @@ run_test_orders (struct TestOrders_Closure *cls) cls->orders[1].id, &lookup_contract, &lookup_order_serial, - &paid, NULL)) { GNUNET_log (GNUNET_ERROR_TYPE_ERROR, @@ -2098,21 +2108,11 @@ run_test_orders (struct TestOrders_Closure *cls) } } /* Test lookup payment status */ - TEST_RET_ON_FAIL (test_lookup_payment_status (serial, + TEST_RET_ON_FAIL (test_lookup_payment_status (cls->instance.instance.id, + cls->orders[0].id, NULL, false, false)); - { - bool paid; - bool wired; - TEST_COND_RET_ON_FAIL (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS == - plugin->lookup_payment_status (plugin->cls, - 256, - NULL, - &paid, - &wired), - "Lookup payment status failed\n"); - } /* Test lookup order status fails for nonexistent order */ { struct TALER_PrivateContractHashP h_contract_terms; @@ -2134,25 +2134,21 @@ run_test_orders (struct TestOrders_Closure *cls) TEST_RET_ON_FAIL (test_mark_contract_paid (&cls->instance, &cls->orders[0], GNUNET_DB_STATUS_SUCCESS_ONE_RESULT)); - TEST_RET_ON_FAIL (test_lookup_payment_status (serial, + TEST_RET_ON_FAIL (test_lookup_payment_status (cls->instance.instance.id, + cls->orders[0].id, NULL, true, false)); - TEST_RET_ON_FAIL (test_lookup_payment_status (serial, + TEST_RET_ON_FAIL (test_lookup_payment_status (cls->instance.instance.id, + cls->orders[0].id, "test_orders_session", true, false)); - { - bool paid; - bool wired; - TEST_COND_RET_ON_FAIL (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS == - plugin->lookup_payment_status (plugin->cls, - serial, - "bad_session", - &paid, - &wired), - "Lookup payment status failed\n"); - } + TEST_RET_ON_FAIL (test_lookup_payment_status (cls->instance.instance.id, + cls->orders[0].id, + "bad_session", + false, + false)); /* Test lookup order by fulfillment */ TEST_RET_ON_FAIL (test_lookup_order_by_fulfillment (&cls->instance, &cls->orders[0], @@ -2187,7 +2183,8 @@ run_test_orders (struct TestOrders_Closure *cls) /* Test marking orders as wired */ TEST_RET_ON_FAIL (test_mark_order_wired (serial, GNUNET_DB_STATUS_SUCCESS_ONE_RESULT)); - TEST_RET_ON_FAIL (test_lookup_payment_status (serial, + TEST_RET_ON_FAIL (test_lookup_payment_status (cls->instance.instance.id, + cls->orders[0].id, NULL, true, true)); @@ -4281,7 +4278,8 @@ run_test_transfers (struct TestTransfers_Closure *cls) &cls->account, &cls->transfers[0], GNUNET_DB_STATUS_SUCCESS_ONE_RESULT)); - TEST_RET_ON_FAIL (test_lookup_payment_status (order_serial, + TEST_RET_ON_FAIL (test_lookup_payment_status (cls->instance.instance.id, + cls->order.id, NULL, false, true)); |