aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2023-04-24 23:29:28 +0200
committerChristian Grothoff <christian@grothoff.org>2023-04-24 23:29:28 +0200
commit70f682236feebb8f3d1d54daed3ef741412f984f (patch)
treeb8eebaf0de86df55ceb18351de7a252b3d571767 /src
parent9cbc189fa1c3416e541b0c3e1637908639aca8ae (diff)
-work on DB plugin logic for taler-merchant-exchange
Diffstat (limited to 'src')
-rw-r--r--src/backend/taler-merchant-exchange.c8
-rw-r--r--src/backenddb/Makefile.am1
-rw-r--r--src/backenddb/merchant-0005.sql7
-rw-r--r--src/backenddb/pg_select_open_transfers.c167
-rw-r--r--src/backenddb/pg_select_open_transfers.h47
-rw-r--r--src/backenddb/plugin_merchantdb_postgres.c3
-rw-r--r--src/include/taler_merchantdb_plugin.h45
7 files changed, 274 insertions, 4 deletions
diff --git a/src/backend/taler-merchant-exchange.c b/src/backend/taler-merchant-exchange.c
index b440902d..ab4bc03f 100644
--- a/src/backend/taler-merchant-exchange.c
+++ b/src/backend/taler-merchant-exchange.c
@@ -565,8 +565,8 @@ exchange_request (void *cls)
* @param exchange_url base URL of the exchange that initiated the transfer
* @param payto_uri account of the merchant that received the transfer
* @param wtid wire transfer subject identifying the aggregation
- * @param execution_time when did the merchant observe the transfer
* @param total total amount that was wired
+ * @param next_attempt when should we next try to interact with the exchange
*/
static void
start_inquiry (
@@ -576,8 +576,8 @@ start_inquiry (
const char *exchange_url,
const char *payto_uri,
const struct TALER_WireTransferIdentifierRawP *wtid,
- struct GNUNET_TIME_Absolute execution_time,
- const struct TALER_Amount *total)
+ const struct TALER_Amount *total,
+ struct GNUNET_TIME_Absolute next_attempt)
{
struct Inquiry *w;
@@ -587,7 +587,6 @@ start_inquiry (
w->instance_id = GNUNET_strdup (instance_id);
w->rowid = rowid;
w->wtid = *wtid;
- w->execution_time = execution_time;
w->total = *total;
GNUNET_CONTAINER_DLL_insert (w_head,
w_tail,
@@ -615,6 +614,7 @@ find_work (void *cls)
// NOTE: SELECT WHERE confirmed AND NOT verified AND NOT failed?;
// FIXME: use LIMIT clause! => When do we try again if LIMIT applied?
qs = db_plugin->select_open_transfers (db_plugin->cls,
+ LIMIT - active,
&start_inquiry,
NULL);
if (qs < 0)
diff --git a/src/backenddb/Makefile.am b/src/backenddb/Makefile.am
index 020b3f7b..c72ddd6f 100644
--- a/src/backenddb/Makefile.am
+++ b/src/backenddb/Makefile.am
@@ -56,6 +56,7 @@ libtaler_plugin_merchantdb_postgres_la_SOURCES = \
pg_select_wirewatch_accounts.h pg_select_wirewatch_accounts.c \
pg_insert_account.h pg_insert_account.c \
pg_update_account.h pg_update_account.c \
+ pg_select_open_transfers.h pg_select_open_transfers.c \
pg_lookup_instances.h pg_lookup_instances.c \
pg_lookup_transfers.h pg_lookup_transfers.c \
plugin_merchantdb_postgres.c pg_helper.h
diff --git a/src/backenddb/merchant-0005.sql b/src/backenddb/merchant-0005.sql
index 3bb24e66..f90ee701 100644
--- a/src/backenddb/merchant-0005.sql
+++ b/src/backenddb/merchant-0005.sql
@@ -36,6 +36,13 @@ COMMENT ON COLUMN merchant_transfers.failed
COMMENT ON COLUMN merchant_transfers.validation_status
IS 'Taler error code describing the state of the validation';
+CREATE INDEX merchant_transfers_by_open
+ ON merchant_transfers
+ (ready_time ASC)
+ WHERE confirmed AND NOT (failed OR verified);
+COMMENT ON INDEX merchant_transfers_by_open
+ IS 'For select_open_transfers';
+
ALTER TABLE merchant_accounts
ADD COLUMN credit_facade_url VARCHAR,
diff --git a/src/backenddb/pg_select_open_transfers.c b/src/backenddb/pg_select_open_transfers.c
new file mode 100644
index 00000000..7113c446
--- /dev/null
+++ b/src/backenddb/pg_select_open_transfers.c
@@ -0,0 +1,167 @@
+/*
+ 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_select_open_transfers.c
+ * @brief Implementation of the select_open_transfers function for Postgres
+ * @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_select_open_transfers.h"
+#include "pg_helper.h"
+
+
+/**
+ * Context used for open_transfers_cb().
+ */
+struct SelectOpenTransfersContext
+{
+ /**
+ * Postgres context.
+ */
+ struct PostgresClosure *pg;
+
+ /**
+ * Function to call with the results.
+ */
+ TALER_MERCHANTDB_OpenTransferCallback cb;
+
+ /**
+ * Closure for @a cb.
+ */
+ void *cb_cls;
+
+ /**
+ * Internal result.
+ */
+ enum GNUNET_DB_QueryStatus qs;
+};
+
+
+/**
+ * Function to be called with the results of a SELECT statement
+ * that has returned @a num_results results about tips.
+ *
+ * @param[in,out] cls of type `struct SelectOpenTransfersContext *`
+ * @param result the postgres result
+ * @param num_results the number of results in @a result
+ */
+static void
+open_transfers_cb (void *cls,
+ PGresult *result,
+ unsigned int num_results)
+{
+ struct SelectOpenTransfersContext *plc = cls;
+ struct PostgresClosure *pg = plc->pg;
+
+ for (unsigned int i = 0; i < num_results; i++)
+ {
+ uint64_t rowid;
+ char *instance_id;
+ char *exchange_url;
+ char *payto_uri;
+ struct TALER_WireTransferIdentifierRawP wtid;
+ struct TALER_Amount total;
+ struct GNUNET_TIME_Absolute next_attempt;
+ struct GNUNET_PQ_ResultSpec rs[] = {
+ GNUNET_PQ_result_spec_uint64 ("credit_serial",
+ &rowid),
+ GNUNET_PQ_result_spec_string ("instance_id",
+ &instance_id),
+ GNUNET_PQ_result_spec_string ("exchange_url",
+ &exchange_url),
+ GNUNET_PQ_result_spec_string ("payto_uri",
+ &payto_uri),
+ GNUNET_PQ_result_spec_auto_from_type ("wtid",
+ &wtid),
+ TALER_PQ_RESULT_SPEC_AMOUNT ("credit_amount",
+ &total),
+ GNUNET_PQ_result_spec_absolute_time ("next_attempt",
+ &next_attempt),
+ GNUNET_PQ_result_spec_end
+ };
+
+ if (GNUNET_OK !=
+ GNUNET_PQ_extract_result (result,
+ rs,
+ i))
+ {
+ GNUNET_break (0);
+ plc->qs = GNUNET_DB_STATUS_HARD_ERROR;
+ return;
+ }
+ plc->cb (plc->cb_cls,
+ rowid,
+ instance_id,
+ exchange_url,
+ payto_uri,
+ &wtid,
+ &total,
+ next_attempt);
+ GNUNET_PQ_cleanup_result (rs);
+ }
+}
+
+
+enum GNUNET_DB_QueryStatus
+TMH_PG_select_open_transfers (void *cls,
+ uint64_t limit,
+ TALER_MERCHANTDB_OpenTransferCallback cb,
+ void *cb_cls)
+{
+ struct PostgresClosure *pg = cls;
+ struct SelectOpenTransfersContext plc = {
+ .pg = pg,
+ .cb = cb,
+ .cb_cls = cb_cls
+ };
+ struct GNUNET_PQ_QueryParam params[] = {
+ GNUNET_PQ_query_param_uint64 (&limit),
+ GNUNET_PQ_query_param_end
+ };
+ enum GNUNET_DB_QueryStatus qs;
+
+ PREPARE (pg,
+ "select_open_transfers",
+ "SELECT"
+ " credit_serial"
+ ",exchange_url"
+ ",wtid"
+ ",credit_amount_val"
+ ",credit_amount_frac"
+ ",payto_uri"
+ " FROM merchant_transfers"
+ " JOIN merchant_instances"
+ " USING (merchant_id)"
+ " JOIN merchant_accounts"
+ " USING (account_serial)"
+ " WHERE confirmed AND"
+ " NOT (failed OR verified)"
+ " ORDER BY next_attempt INC"
+ " LIMIT $1;");
+
+ qs = GNUNET_PQ_eval_prepared_multi_select (
+ pg->conn,
+ "select_open_transfers",
+ params,
+ &open_transfers_cb,
+ &plc);
+ if (0 != plc.qs)
+ return plc.qs;
+ return qs;
+}
diff --git a/src/backenddb/pg_select_open_transfers.h b/src/backenddb/pg_select_open_transfers.h
new file mode 100644
index 00000000..5857ed80
--- /dev/null
+++ b/src/backenddb/pg_select_open_transfers.h
@@ -0,0 +1,47 @@
+/*
+ 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_select_open_transfers.h
+ * @brief implementation of the select_open_transfers function for Postgres
+ * @author Christian Grothoff
+ */
+#ifndef PG_SELECT_OPEN_TRANSFERS_H
+#define PG_SELECT_OPEN_TRANSFERS_H
+
+#include <taler/taler_util.h>
+#include <taler/taler_json_lib.h>
+#include "taler_merchantdb_plugin.h"
+
+
+/**
+ * Retrieve wire transfer details of wire details
+ * that taler-merchant-exchange still needs to
+ * investigate.
+ *
+ * @param cls closure, typically a connection to the db
+ * @param limit maximum number of results to return
+ * @param cb function called with the wire transfer data
+ * @param cb_cls closure for @a cb
+ * @return transaction status
+ */
+enum GNUNET_DB_QueryStatus
+TMH_PG_select_open_transfers (void *cls,
+ uint64_t limit,
+ TALER_MERCHANTDB_OpenTransferCallback cb,
+ void *cb_cls);
+
+
+#endif
diff --git a/src/backenddb/plugin_merchantdb_postgres.c b/src/backenddb/plugin_merchantdb_postgres.c
index e70d8639..6040c9af 100644
--- a/src/backenddb/plugin_merchantdb_postgres.c
+++ b/src/backenddb/plugin_merchantdb_postgres.c
@@ -36,6 +36,7 @@
#include "pg_lookup_transfers.h"
#include "pg_update_wirewatch_progress.h"
#include "pg_select_wirewatch_accounts.h"
+#include "pg_select_open_transfers.h"
/**
@@ -9784,6 +9785,8 @@ libtaler_plugin_merchantdb_postgres_init (void *cls)
plugin->insert_pickup = &postgres_insert_pickup;
plugin->insert_pickup_blind_signature =
&postgres_insert_pickup_blind_signature;
+ plugin->select_open_transfers
+ = &TMH_PG_select_open_transfers;
plugin->lookup_templates = &postgres_lookup_templates;
plugin->lookup_template = &postgres_lookup_template;
plugin->delete_template = &postgres_delete_template;
diff --git a/src/include/taler_merchantdb_plugin.h b/src/include/taler_merchantdb_plugin.h
index f0ae6589..62dcf5e7 100644
--- a/src/include/taler_merchantdb_plugin.h
+++ b/src/include/taler_merchantdb_plugin.h
@@ -712,6 +712,32 @@ typedef void
/**
+ * Function called with information about wire transfers
+ * that taler-merchant-exchange still needs to process.
+ *
+ * @param cls closure
+ * @param rowid row of the transfer in the merchant database
+ * @param instance_id instance that received the transfer
+ * @param exchange_url base URL of the exchange that initiated the transfer
+ * @param payto_uri account of the merchant that received the transfer
+ * @param wtid wire transfer subject identifying the aggregation
+ * @param execution_time when did the merchant observe the transfer
+ * @param total total amount that was wired
+ * @param next_attempt when should we next try to interact with the exchange
+ */
+typedef void
+(*TALER_MERCHANTDB_OpenTransferCallback)(
+ void *cls,
+ uint64_t rowid,
+ const char *instance_id,
+ const char *exchange_url,
+ const char *payto_uri,
+ const struct TALER_WireTransferIdentifierRawP *wtid,
+ const struct TALER_Amount *total,
+ struct GNUNET_TIME_Absolute next_attempt);
+
+
+/**
* Function called with detailed information about a wire transfer and
* the underlying deposits that are being aggregated.
*
@@ -1927,6 +1953,25 @@ struct TALER_MERCHANTDB_Plugin
/**
+ * Retrieve wire transfer details of wire details
+ * that taler-merchant-exchange still needs to
+ * investigate.
+ *
+ * @param cls closure
+ * @param limit maximum number of results to return
+ * @param cb function called with the wire transfer data
+ * @param cb_cls closure for @a cb
+ * @return transaction status
+ */
+ enum GNUNET_DB_QueryStatus
+ (*select_open_transfers)(
+ void *cls,
+ uint64_t limit,
+ TALER_MERCHANTDB_OpenTransferCallback cb,
+ void *cb_cls);
+
+
+ /**
* Insert wire transfer details for a deposit.
*
* @param cls closure