aboutsummaryrefslogtreecommitdiff
path: root/src/backenddb
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/backenddb
parent9cbc189fa1c3416e541b0c3e1637908639aca8ae (diff)
-work on DB plugin logic for taler-merchant-exchange
Diffstat (limited to 'src/backenddb')
-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
5 files changed, 225 insertions, 0 deletions
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;