From ad925ccc324ca2b84676a39d63b776ba337da492 Mon Sep 17 00:00:00 2001 From: Christian Grothoff Date: Mon, 10 Apr 2023 23:30:59 +0200 Subject: prepare test_kyc_api to test #7684 --- src/backenddb/plugin_merchantdb_postgres.c | 462 +---------------------------- 1 file changed, 6 insertions(+), 456 deletions(-) (limited to 'src/backenddb/plugin_merchantdb_postgres.c') diff --git a/src/backenddb/plugin_merchantdb_postgres.c b/src/backenddb/plugin_merchantdb_postgres.c index ff7e1dd3..b49bc54a 100644 --- a/src/backenddb/plugin_merchantdb_postgres.c +++ b/src/backenddb/plugin_merchantdb_postgres.c @@ -30,6 +30,8 @@ #include #include "taler_merchantdb_plugin.h" #include "pg_helper.h" +#include "pg_lookup_transfers.h" + /** * How often do we re-try if we run into a DB serialization error? @@ -176,14 +178,7 @@ postgres_preflight (void *cls) } -/** - * Check that the database connection is still up - * and automatically reconnects unless we are - * already inside of a transaction. - * - * @param pg connection to check - */ -static void +void check_connection (struct PostgresClosure *pg) { if (NULL != pg->transaction_name) @@ -4859,253 +4854,6 @@ postgres_lookup_transfer_details ( } -/** - * Closure for #lookup_transfers_cb(). - */ -struct LookupTransfersContext -{ - /** - * Function to call on results. - */ - TALER_MERCHANTDB_TransferCallback cb; - - /** - * Closure for @e cb. - */ - void *cb_cls; - - /** - * Postgres context. - */ - struct PostgresClosure *pg; - - /** - * Transaction status (set). - */ - enum GNUNET_DB_QueryStatus qs; - - /** - * Filter to apply by verification status. - */ - enum TALER_EXCHANGE_YesNoAll verified; -}; - - -/** - * Function to be called with the results of a SELECT statement - * that has returned @a num_results results. - * - * @param cls of type `struct LookupTransfersContext *` - * @param result the postgres result - * @param num_results the number of results in @a result - */ -static void -lookup_transfers_cb (void *cls, - PGresult *result, - unsigned int num_results) -{ - struct LookupTransfersContext *ltc = cls; - struct PostgresClosure *pg = ltc->pg; - - for (unsigned int i = 0; iqs = GNUNET_DB_STATUS_HARD_ERROR; - return; - } - if (0 == verified8) - verified = TALER_EXCHANGE_YNA_NO; - else - verified = TALER_EXCHANGE_YNA_YES; - if ( (ltc->verified == TALER_EXCHANGE_YNA_ALL) || - (ltc->verified == verified) ) - { - ltc->cb (ltc->cb_cls, - &credit_amount, - &wtid, - payto_uri, - exchange_url, - transfer_serial_id, - execution_time, - TALER_EXCHANGE_YNA_YES == verified, - 0 != confirmed8); - } - GNUNET_PQ_cleanup_result (rs); - } - ltc->qs = num_results; -} - - -/** - * Lookup transfers. Note that filtering by @a verified status is done - * outside of SQL, as we already have 8 prepared statements and adding - * a filter on verified would further double the number of statements for - * a likely rather ineffective filter. So we apply that filter in - * #lookup_transfers_cb(). - * - * @param cls closure - * @param instance_id instance to lookup payments for - * @param payto_uri account that we are interested in transfers to - * @param before timestamp for the earliest transfer we care about - * @param after timestamp for the last transfer we care about - * @param limit number of entries to return, negative for descending in execution time, - * positive for ascending in execution time - * @param offset transfer_serial number of the transfer we want to offset from - * @param verified filter transfers by verification status - * @param cb function to call with detailed transfer data - * @param cb_cls closure for @a cb - * @return transaction status - */ -static enum GNUNET_DB_QueryStatus -postgres_lookup_transfers (void *cls, - const char *instance_id, - const char *payto_uri, - struct GNUNET_TIME_Timestamp before, - struct GNUNET_TIME_Timestamp after, - int64_t limit, - uint64_t offset, - enum TALER_EXCHANGE_YesNoAll verified, - TALER_MERCHANTDB_TransferCallback cb, - void *cb_cls) -{ - struct PostgresClosure *pg = cls; - uint64_t plimit = (uint64_t) ((limit < 0) ? -limit : limit); - struct LookupTransfersContext ltc = { - .cb = cb, - .cb_cls = cb_cls, - .pg = pg, - .verified = verified - }; - enum GNUNET_DB_QueryStatus qs; - bool by_time; - - by_time = ( (! GNUNET_TIME_absolute_is_never (before.abs_time)) || - (! GNUNET_TIME_absolute_is_zero (after.abs_time)) ); - check_connection (pg); - if (by_time) - { - if (NULL != payto_uri) - { - struct GNUNET_PQ_QueryParam params[] = { - GNUNET_PQ_query_param_string (instance_id), - GNUNET_PQ_query_param_timestamp (&before), - GNUNET_PQ_query_param_timestamp (&after), - GNUNET_PQ_query_param_uint64 (&offset), - GNUNET_PQ_query_param_uint64 (&plimit), - GNUNET_PQ_query_param_string (payto_uri), - GNUNET_PQ_query_param_end - }; - - qs = GNUNET_PQ_eval_prepared_multi_select ( - pg->conn, - (limit > 0) - ? "lookup_transfers_time_payto_asc" - : "lookup_transfers_time_payto_desc", - params, - &lookup_transfers_cb, - <c); - } - else - { - struct GNUNET_PQ_QueryParam params[] = { - GNUNET_PQ_query_param_string (instance_id), - GNUNET_PQ_query_param_timestamp (&before), - GNUNET_PQ_query_param_timestamp (&after), - GNUNET_PQ_query_param_uint64 (&offset), - GNUNET_PQ_query_param_uint64 (&plimit), - GNUNET_PQ_query_param_end - }; - - qs = GNUNET_PQ_eval_prepared_multi_select ( - pg->conn, - (limit > 0) - ? "lookup_transfers_time_asc" - : "lookup_transfers_time_desc", - params, - &lookup_transfers_cb, - <c); - } - } - else - { - if (NULL != payto_uri) - { - struct GNUNET_PQ_QueryParam params[] = { - GNUNET_PQ_query_param_string (instance_id), - GNUNET_PQ_query_param_uint64 (&offset), - GNUNET_PQ_query_param_uint64 (&plimit), - GNUNET_PQ_query_param_string (payto_uri), - GNUNET_PQ_query_param_end - }; - - qs = GNUNET_PQ_eval_prepared_multi_select ( - pg->conn, - (limit > 0) - ? "lookup_transfers_payto_asc" - : "lookup_transfers_payto_desc", - params, - &lookup_transfers_cb, - <c); - } - else - { - struct GNUNET_PQ_QueryParam params[] = { - GNUNET_PQ_query_param_string (instance_id), - GNUNET_PQ_query_param_uint64 (&offset), - GNUNET_PQ_query_param_uint64 (&plimit), - GNUNET_PQ_query_param_end - }; - - qs = GNUNET_PQ_eval_prepared_multi_select ( - pg->conn, - (limit > 0) - ? "lookup_transfers_asc" - : "lookup_transfers_desc", - params, - &lookup_transfers_cb, - <c); - } - } - if (0 >= qs) - return qs; - return ltc.qs; -} - - /** * Store information about wire fees charged by an exchange, * including signature (so we have proof). @@ -9702,206 +9450,6 @@ postgres_connect (void *cls) " JOIN merchant_transfers USING (credit_serial)" " WHERE merchant_transfers.wtid=$2" " AND merchant_transfers.exchange_url=$1"), - /* for postgres_lookup_transfers() */ - GNUNET_PQ_make_prepare ("lookup_transfers_time_payto_asc", - "SELECT" - " mt.credit_amount_val" - ",mt.credit_amount_frac" - ",wtid" - ",merchant_accounts.payto_uri" - ",exchange_url" - ",credit_serial" - ",merchant_transfer_signatures.execution_time" - ",verified" - ",confirmed" - " FROM merchant_transfers mt" - " JOIN merchant_accounts USING (account_serial)" - " JOIN merchant_transfer_signatures USING (credit_serial)" - " WHERE execution_time < $2" - " AND execution_time >= $3" - " AND credit_serial > $4" - " AND payto_uri = $6" - " AND merchant_serial =" - " (SELECT merchant_serial" - " FROM merchant_instances" - " WHERE merchant_id=$1)" - " ORDER BY credit_serial ASC" - " LIMIT $5"), - /* for postgres_lookup_transfers() */ - GNUNET_PQ_make_prepare ("lookup_transfers_time_asc", - "SELECT" - " mt.credit_amount_val" - ",mt.credit_amount_frac" - ",wtid" - ",merchant_accounts.payto_uri" - ",exchange_url" - ",credit_serial" - ",merchant_transfer_signatures.execution_time" - ",verified" - ",confirmed" - " FROM merchant_transfers mt" - " JOIN merchant_accounts USING (account_serial)" - " JOIN merchant_transfer_signatures USING (credit_serial)" - " WHERE execution_time < $2" - " AND execution_time >= $3" - " AND credit_serial > $4" - " AND merchant_serial =" - " (SELECT merchant_serial" - " FROM merchant_instances" - " WHERE merchant_id=$1)" - " ORDER BY credit_serial ASC" - " LIMIT $5"), - /* for postgres_lookup_transfers() */ - GNUNET_PQ_make_prepare ("lookup_transfers_payto_asc", - "SELECT" - " mt.credit_amount_val" - ",mt.credit_amount_frac" - ",wtid" - ",merchant_accounts.payto_uri" - ",exchange_url" - ",credit_serial" - ",CASE WHEN (merchant_transfer_signatures.execution_time) IS NULL" - " THEN 9223372036854775807" /* largest BIGINT possible */ - " ELSE merchant_transfer_signatures.execution_time" - " END AS execution_time" - ",verified" - ",confirmed" - " FROM merchant_transfers mt" - " JOIN merchant_accounts USING (account_serial)" - " LEFT JOIN merchant_transfer_signatures USING (credit_serial)" - " WHERE credit_serial > $2" - " AND payto_uri = $4" - " AND merchant_serial =" - " (SELECT merchant_serial" - " FROM merchant_instances" - " WHERE merchant_id=$1)" - " ORDER BY credit_serial ASC" - " LIMIT $3"), - /* for postgres_lookup_transfers() */ - GNUNET_PQ_make_prepare ("lookup_transfers_asc", - "SELECT" - " mt.credit_amount_val" - ",mt.credit_amount_frac" - ",wtid" - ",merchant_accounts.payto_uri" - ",exchange_url" - ",credit_serial" - ",CASE WHEN (merchant_transfer_signatures.execution_time) IS NULL" - " THEN 9223372036854775807" /* largest BIGINT possible */ - " ELSE merchant_transfer_signatures.execution_time" - " END AS execution_time" - ",verified" - ",confirmed" - " FROM merchant_transfers mt" - " JOIN merchant_accounts USING (account_serial)" - " LEFT JOIN merchant_transfer_signatures USING (credit_serial)" - " WHERE credit_serial > $2" - " AND merchant_serial =" - " (SELECT merchant_serial" - " FROM merchant_instances" - " WHERE merchant_id=$1)" - " ORDER BY credit_serial ASC" - " LIMIT $3"), - /* for postgres_lookup_transfers() */ - GNUNET_PQ_make_prepare ("lookup_transfers_time_payto_desc", - "SELECT" - " mt.credit_amount_val" - ",mt.credit_amount_frac" - ",wtid" - ",merchant_accounts.payto_uri" - ",exchange_url" - ",credit_serial" - ",merchant_transfer_signatures.execution_time" - ",verified" - ",confirmed" - " FROM merchant_transfers mt" - " JOIN merchant_accounts USING (account_serial)" - " JOIN merchant_transfer_signatures USING (credit_serial)" - " WHERE execution_time < $2" - " AND execution_time >= $3" - " AND credit_serial < $4" - " AND payto_uri = $6" - " AND merchant_serial =" - " (SELECT merchant_serial" - " FROM merchant_instances" - " WHERE merchant_id=$1)" - " ORDER BY credit_serial DESC" - " LIMIT $5"), - /* for postgres_lookup_transfers() */ - GNUNET_PQ_make_prepare ("lookup_transfers_time_desc", - "SELECT" - " mt.credit_amount_val" - ",mt.credit_amount_frac" - ",wtid" - ",merchant_accounts.payto_uri" - ",exchange_url" - ",credit_serial" - ",merchant_transfer_signatures.execution_time" - ",verified" - ",confirmed" - " FROM merchant_transfers mt" - " JOIN merchant_accounts USING (account_serial)" - " JOIN merchant_transfer_signatures USING (credit_serial)" - " WHERE execution_time < $2" - " AND execution_time >= $3" - " AND credit_serial < $4" - " AND merchant_serial =" - " (SELECT merchant_serial" - " FROM merchant_instances" - " WHERE merchant_id=$1)" - " ORDER BY credit_serial DESC" - " LIMIT $5"), - /* for postgres_lookup_transfers() */ - GNUNET_PQ_make_prepare ("lookup_transfers_payto_desc", - "SELECT" - " mt.credit_amount_val" - ",mt.credit_amount_frac" - ",wtid" - ",merchant_accounts.payto_uri" - ",exchange_url" - ",credit_serial" - ",CASE WHEN (merchant_transfer_signatures.execution_time) IS NULL" - " THEN 9223372036854775807" /* largest BIGINT possible */ - " ELSE merchant_transfer_signatures.execution_time" - " END AS execution_time" - ",verified" - ",confirmed" - " FROM merchant_transfers mt" - " JOIN merchant_accounts USING (account_serial)" - " LEFT JOIN merchant_transfer_signatures USING (credit_serial)" - " WHERE credit_serial < $2" - " AND payto_uri = $4" - " AND merchant_serial =" - " (SELECT merchant_serial" - " FROM merchant_instances" - " WHERE merchant_id=$1)" - " ORDER BY credit_serial DESC" - " LIMIT $3"), - /* for postgres_lookup_transfers() */ - GNUNET_PQ_make_prepare ("lookup_transfers_desc", - "SELECT" - " mt.credit_amount_val" - ",mt.credit_amount_frac" - ",wtid" - ",merchant_accounts.payto_uri" - ",exchange_url" - ",credit_serial" - ",CASE WHEN (merchant_transfer_signatures.execution_time) IS NULL" - " THEN 9223372036854775807" /* largest BIGINT possible */ - " ELSE merchant_transfer_signatures.execution_time" - " END AS execution_time" - ",verified" - ",confirmed" - " FROM merchant_transfers mt" - " JOIN merchant_accounts USING (account_serial)" - " LEFT JOIN merchant_transfer_signatures USING (credit_serial)" - " WHERE credit_serial < $2" - " AND merchant_serial =" - " (SELECT merchant_serial" - " FROM merchant_instances" - " WHERE merchant_id=$1)" - " ORDER BY credit_serial DESC" - " LIMIT $3"), /* For postgres_store_wire_fee_by_exchange() */ GNUNET_PQ_make_prepare ("insert_wire_fee", "INSERT INTO merchant_exchange_wire_fees" @@ -10517,6 +10065,7 @@ postgres_connect (void *cls) NULL, es, ps); + pg->prep_gen++; if (NULL == pg->conn) return GNUNET_SYSERR; return GNUNET_OK; @@ -10638,7 +10187,8 @@ libtaler_plugin_merchantdb_postgres_init (void *cls) &postgres_set_transfer_status_to_verified; plugin->lookup_transfer_summary = &postgres_lookup_transfer_summary; plugin->lookup_transfer_details = &postgres_lookup_transfer_details; - plugin->lookup_transfers = &postgres_lookup_transfers; + plugin->lookup_transfers + = &TMH_PG_lookup_transfers; plugin->store_wire_fee_by_exchange = &postgres_store_wire_fee_by_exchange; plugin->insert_reserve = &postgres_insert_reserve; plugin->activate_reserve = &postgres_activate_reserve; -- cgit v1.2.3