aboutsummaryrefslogtreecommitdiff
path: root/src/exchangedb
diff options
context:
space:
mode:
Diffstat (limited to 'src/exchangedb')
-rw-r--r--src/exchangedb/Makefile.am5
-rw-r--r--src/exchangedb/pg_create_shard_tables.c66
-rw-r--r--src/exchangedb/pg_create_shard_tables.h39
-rw-r--r--src/exchangedb/pg_drop_tables.c59
-rw-r--r--src/exchangedb/pg_drop_tables.h38
-rw-r--r--src/exchangedb/pg_insert_aggregation_tracking.c54
-rw-r--r--src/exchangedb/pg_insert_aggregation_tracking.h42
-rw-r--r--src/exchangedb/pg_insert_close_request.c3
-rw-r--r--src/exchangedb/pg_insert_purse_request.c25
-rw-r--r--src/exchangedb/pg_prefligth.c64
-rw-r--r--src/exchangedb/pg_prefligth.h43
-rw-r--r--src/exchangedb/pg_setup_partitions.c26
-rw-r--r--src/exchangedb/pg_setup_partitions.h39
-rw-r--r--src/exchangedb/plugin_exchangedb_postgres.c264
14 files changed, 527 insertions, 240 deletions
diff --git a/src/exchangedb/Makefile.am b/src/exchangedb/Makefile.am
index 55919aebf..1de080060 100644
--- a/src/exchangedb/Makefile.am
+++ b/src/exchangedb/Makefile.am
@@ -69,12 +69,17 @@ endif
libtaler_plugin_exchangedb_postgres_la_SOURCES = \
plugin_exchangedb_common.c plugin_exchangedb_common.h \
plugin_exchangedb_postgres.c pg_helper.h \
+ pg_insert_aggregation_tracking.h pg_insert_aggregation_tracking.c \
pg_do_reserve_open.c pg_do_reserve_open.h \
pg_do_withdraw.h pg_do_withdraw.c \
+ pg_create_shard_tables.h pg_create_shard_tables.c \
+ pg_prefligth.h pg_prefligth.c \
pg_iterate_active_signkeys.h pg_iterate_active_signkeys.c \
pg_commit.h pg_commit.c \
pg_get_coin_transactions.c pg_get_coin_transactions.h \
pg_get_expired_reserves.c pg_get_expired_reserves.h \
+ pg_setup_partitions.h pg_setup_partitions.c \
+ pg_insert_aggregation_tracking.h pg_insert_aggregation_tracking.c \
pg_get_purse_request.c pg_get_purse_request.h \
pg_get_reserve_history.c pg_get_reserve_history.h \
pg_get_unfinished_close_requests.c pg_get_unfinished_close_requests.h \
diff --git a/src/exchangedb/pg_create_shard_tables.c b/src/exchangedb/pg_create_shard_tables.c
new file mode 100644
index 000000000..4fb6940f9
--- /dev/null
+++ b/src/exchangedb/pg_create_shard_tables.c
@@ -0,0 +1,66 @@
+/*
+ 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 exchangedb/pg_create_shard_tables.c
+ * @brief Implementation of the create_shard_tables function for Postgres
+ * @author Christian Grothoff
+ */
+#include "platform.h"
+#include "taler_error_codes.h"
+#include "taler_dbevents.h"
+#include "taler_pq_lib.h"
+#include "pg_create_shard_tables.h"
+#include "pg_helper.h"
+
+
+enum GNUNET_GenericReturnValue
+TEH_PG_create_shard_tables (void *cls,
+ uint32_t idx)
+{
+ struct PostgresClosure *pg = cls;
+ struct GNUNET_PQ_Context *conn;
+ enum GNUNET_GenericReturnValue ret = GNUNET_OK;
+ struct GNUNET_PQ_QueryParam params[] = {
+ GNUNET_PQ_query_param_uint32 (&idx),
+ GNUNET_PQ_query_param_end
+ };
+ struct GNUNET_PQ_ExecuteStatement es[] = {
+ GNUNET_PQ_make_try_execute ("SET search_path TO exchange;"),
+ GNUNET_PQ_EXECUTE_STATEMENT_END
+ };
+
+ struct GNUNET_PQ_PreparedStatement ps[] = {
+ GNUNET_PQ_make_prepare ("create_shard_tables",
+ "SELECT"
+ " setup_shard"
+ " ($1);"),
+ GNUNET_PQ_PREPARED_STATEMENT_END
+ };
+
+ conn = GNUNET_PQ_connect_with_cfg (pg->cfg,
+ "exchangedb-postgres",
+ "shard-",
+ es,
+ ps);
+ if (NULL == conn)
+ return GNUNET_SYSERR;
+ if (0 > GNUNET_PQ_eval_prepared_non_select (conn,
+ "create_shard_tables",
+ params))
+ ret = GNUNET_SYSERR;
+ GNUNET_PQ_disconnect (conn);
+ return ret;
+}
diff --git a/src/exchangedb/pg_create_shard_tables.h b/src/exchangedb/pg_create_shard_tables.h
new file mode 100644
index 000000000..31ab49a4d
--- /dev/null
+++ b/src/exchangedb/pg_create_shard_tables.h
@@ -0,0 +1,39 @@
+/*
+ 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 exchangedb/pg_create_shard_tables.h
+ * @brief implementation of the create_shard_tables function for Postgres
+ * @author Christian Grothoff
+ */
+#ifndef PG_CREATE_SHARD_TABLES_H
+#define PG_CREATE_SHARD_TABLES_H
+
+#include "taler_util.h"
+#include "taler_json_lib.h"
+#include "taler_exchangedb_plugin.h"
+
+/**
+ * Create tables of a shard node with index idx
+ *
+ * @param cls the `struct PostgresClosure` with the plugin-specific state
+ * @param idx the shards index, will be appended as suffix to all tables
+ * @return #GNUNET_OK upon success; #GNUNET_SYSERR upon failure
+ */
+enum GNUNET_GenericReturnValue
+TEH_PG_create_shard_tables (void *cls,
+ uint32_t idx);
+
+#endif
diff --git a/src/exchangedb/pg_drop_tables.c b/src/exchangedb/pg_drop_tables.c
new file mode 100644
index 000000000..4693e1154
--- /dev/null
+++ b/src/exchangedb/pg_drop_tables.c
@@ -0,0 +1,59 @@
+/*
+ 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 exchangedb/pg_drop_tables.c
+ * @brief Implementation of the drop_tables function for Postgres
+ * @author Christian Grothoff
+ */
+#include "platform.h"
+#include "taler_error_codes.h"
+#include "taler_dbevents.h"
+#include "taler_pq_lib.h"
+#include "pg_drop_tables.h"
+#include "pg_helper.h"
+
+
+/**
+ * Drop all Taler tables. This should only be used by testcases.
+ *
+ * @param cls the `struct PostgresClosure` with the plugin-specific state
+ * @return #GNUNET_OK upon success; #GNUNET_SYSERR upon failure
+ */
+enum GNUNET_GenericReturnValue
+TEH_PG_drop_tables (void *cls)
+{
+ struct PostgresClosure *pg = cls;
+ struct GNUNET_PQ_Context *conn;
+ enum GNUNET_GenericReturnValue ret;
+
+ if (NULL != pg->conn)
+ {
+ GNUNET_PQ_disconnect (pg->conn);
+ pg->conn = NULL;
+ pg->init = false;
+ }
+ conn = GNUNET_PQ_connect_with_cfg (pg->cfg,
+ "exchangedb-postgres",
+ NULL,
+ NULL,
+ NULL);
+ if (NULL == conn)
+ return GNUNET_SYSERR;
+ ret = GNUNET_PQ_exec_sql (conn,
+ "drop");
+ GNUNET_PQ_disconnect (conn);
+ return ret;
+}
diff --git a/src/exchangedb/pg_drop_tables.h b/src/exchangedb/pg_drop_tables.h
new file mode 100644
index 000000000..58729d5ec
--- /dev/null
+++ b/src/exchangedb/pg_drop_tables.h
@@ -0,0 +1,38 @@
+/*
+ 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 exchangedb/pg_drop_tables.h
+ * @brief implementation of the drop_tables function for Postgres
+ * @author Christian Grothoff
+ */
+#ifndef PG_DROP_TABLES_H
+#define PG_DROP_TABLES_H
+
+#include "taler_util.h"
+#include "taler_json_lib.h"
+#include "taler_exchangedb_plugin.h"
+
+
+/**
+ * Drop all Taler tables. This should only be used by testcases.
+ *
+ * @param cls the `struct PostgresClosure` with the plugin-specific state
+ * @return #GNUNET_OK upon success; #GNUNET_SYSERR upon failure
+ */
+enum GNUNET_GenericReturnValue
+TEH_PG_drop_tables (void *cls);
+
+#endif
diff --git a/src/exchangedb/pg_insert_aggregation_tracking.c b/src/exchangedb/pg_insert_aggregation_tracking.c
new file mode 100644
index 000000000..01c5928ba
--- /dev/null
+++ b/src/exchangedb/pg_insert_aggregation_tracking.c
@@ -0,0 +1,54 @@
+/*
+ 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 exchangedb/pg_insert_aggregation_tracking.c
+ * @brief Implementation of the insert_aggregation_tracking function for Postgres
+ * @author Christian Grothoff
+ */
+#include "platform.h"
+#include "taler_error_codes.h"
+#include "taler_dbevents.h"
+#include "taler_pq_lib.h"
+#include "pg_insert_aggregation_tracking.h"
+#include "pg_helper.h"
+
+
+enum GNUNET_DB_QueryStatus
+TEH_PG_insert_aggregation_tracking (
+ void *cls,
+ const struct TALER_WireTransferIdentifierRawP *wtid,
+ unsigned long long deposit_serial_id)
+{
+ struct PostgresClosure *pg = cls;
+ uint64_t rid = deposit_serial_id;
+ struct GNUNET_PQ_QueryParam params[] = {
+ GNUNET_PQ_query_param_uint64 (&rid),
+ GNUNET_PQ_query_param_auto_from_type (wtid),
+ GNUNET_PQ_query_param_end
+ };
+
+ PREPARE (pg,
+ "insert_aggregation_tracking",
+ "INSERT INTO aggregation_tracking "
+ "(deposit_serial_id"
+ ",wtid_raw"
+ ") VALUES "
+ "($1, $2);");
+ return GNUNET_PQ_eval_prepared_non_select (pg->conn,
+ "insert_aggregation_tracking",
+ params);
+}
+
diff --git a/src/exchangedb/pg_insert_aggregation_tracking.h b/src/exchangedb/pg_insert_aggregation_tracking.h
new file mode 100644
index 000000000..e67c0e8e7
--- /dev/null
+++ b/src/exchangedb/pg_insert_aggregation_tracking.h
@@ -0,0 +1,42 @@
+/*
+ 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 exchangedb/pg_insert_aggregation_tracking.h
+ * @brief implementation of the insert_aggregation_tracking function for Postgres
+ * @author Christian Grothoff
+ */
+#ifndef PG_INSERT_AGGREGATION_TRACKING_H
+#define PG_INSERT_AGGREGATION_TRACKING_H
+
+#include "taler_util.h"
+#include "taler_json_lib.h"
+#include "taler_exchangedb_plugin.h"
+
+/**
+ * Function called to insert aggregation information into the DB.
+ *
+ * @param cls closure
+ * @param wtid the raw wire transfer identifier we used
+ * @param deposit_serial_id row in the deposits table for which this is aggregation data
+ * @return transaction status code
+ */
+enum GNUNET_DB_QueryStatus
+TEH_PG_insert_aggregation_tracking (
+ void *cls,
+ const struct TALER_WireTransferIdentifierRawP *wtid,
+ unsigned long long deposit_serial_id);
+
+#endif
diff --git a/src/exchangedb/pg_insert_close_request.c b/src/exchangedb/pg_insert_close_request.c
index a62c2cba7..387dafd9a 100644
--- a/src/exchangedb/pg_insert_close_request.c
+++ b/src/exchangedb/pg_insert_close_request.c
@@ -59,7 +59,8 @@ TEH_PG_insert_close_request (
",close_fee_frac"
",payto_uri"
")"
- "VALUES ($1, $2, $3, $4, $5, $6, $7, $8)"
+ "VALUES "
+ "($1, $2, $3, $4, $5, $6, $7, $8)"
" ON CONFLICT DO NOTHING;");
return GNUNET_PQ_eval_prepared_non_select (pg->conn,
"insert_account_close",
diff --git a/src/exchangedb/pg_insert_purse_request.c b/src/exchangedb/pg_insert_purse_request.c
index 88ce36ead..5560bfa94 100644
--- a/src/exchangedb/pg_insert_purse_request.c
+++ b/src/exchangedb/pg_insert_purse_request.c
@@ -63,6 +63,28 @@ TEH_PG_insert_purse_request (
};
*in_conflict = false;
+
+
+ PREPARE ( pg,
+ "insert_purse_request",
+ "INSERT INTO purse_requests"
+ " (purse_pub"
+ " ,merge_pub"
+ " ,purse_creation"
+ " ,purse_expiration"
+ " ,h_contract_terms"
+ " ,age_limit"
+ " ,flags"
+ " ,in_reserve_quota"
+ " ,amount_with_fee_val"
+ " ,amount_with_fee_frac"
+ " ,purse_fee_val"
+
+ " ,purse_fee_frac"
+ " ,purse_sig"
+ " ) VALUES "
+ " ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13)"
+ " ON CONFLICT DO NOTHING;");
qs = GNUNET_PQ_eval_prepared_non_select (pg->conn,
"insert_purse_request",
params);
@@ -107,3 +129,6 @@ TEH_PG_insert_purse_request (
}
+
+
+
diff --git a/src/exchangedb/pg_prefligth.c b/src/exchangedb/pg_prefligth.c
new file mode 100644
index 000000000..9336b6d47
--- /dev/null
+++ b/src/exchangedb/pg_prefligth.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 <http://www.gnu.org/licenses/>
+ */
+/**
+ * @file exchangedb/pg_prefligth.c
+ * @brief Implementation of the prefligth function for Postgres
+ * @author Christian Grothoff
+ */
+#include "platform.h"
+#include "taler_error_codes.h"
+#include "taler_dbevents.h"
+#include "taler_pq_lib.h"
+#include "pg_prefligth.h"
+#include "pg_helper.h"
+
+
+enum GNUNET_GenericReturnValue
+TEH_PG_preflight (void *cls)
+{
+ struct PostgresClosure *pg = cls;
+ struct GNUNET_PQ_ExecuteStatement es[] = {
+ GNUNET_PQ_make_execute ("ROLLBACK"),
+ GNUNET_PQ_EXECUTE_STATEMENT_END
+ };
+
+ if (! pg->init)
+ {
+ if (GNUNET_OK !=
+
+ internal_setup (pg,
+ false))
+ return GNUNET_SYSERR;
+ }
+ if (NULL == pg->transaction_name)
+ return GNUNET_OK; /* all good */
+ if (GNUNET_OK ==
+ GNUNET_PQ_exec_statements (pg->conn,
+ es))
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+ "BUG: Preflight check rolled back transaction `%s'!\n",
+ pg->transaction_name);
+ }
+ else
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+ "BUG: Preflight check failed to rollback transaction `%s'!\n",
+ pg->transaction_name);
+ }
+ pg->transaction_name = NULL;
+ return GNUNET_NO;
+}
diff --git a/src/exchangedb/pg_prefligth.h b/src/exchangedb/pg_prefligth.h
new file mode 100644
index 000000000..719d90959
--- /dev/null
+++ b/src/exchangedb/pg_prefligth.h
@@ -0,0 +1,43 @@
+/*
+ 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 exchangedb/pg_prefligth.h
+ * @brief implementation of the prefligth function for Postgres
+ * @author Christian Grothoff
+ */
+#ifndef PG_PREFLIGTH_H
+#define PG_PREFLIGTH_H
+
+#include "taler_util.h"
+#include "taler_json_lib.h"
+#include "taler_exchangedb_plugin.h"
+
+
+/**
+ * Do a pre-flight check that we are not in an uncommitted transaction.
+ * If we are, try to commit the previous transaction and output a warning.
+ * Does not return anything, as we will continue regardless of the outcome.
+ *
+ * @param cls the `struct PostgresClosure` with the plugin-specific state
+ * @return #GNUNET_OK if everything is fine
+ * #GNUNET_NO if a transaction was rolled back
+ * #GNUNET_SYSERR on hard errors
+ */
+
+
+enum GNUNET_GenericReturnValue
+TEH_PG_preflight (void *cls);
+#endif
diff --git a/src/exchangedb/pg_setup_partitions.c b/src/exchangedb/pg_setup_partitions.c
new file mode 100644
index 000000000..7a472ed1d
--- /dev/null
+++ b/src/exchangedb/pg_setup_partitions.c
@@ -0,0 +1,26 @@
+/*
+ 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 exchangedb/pg_setup_partitions.c
+ * @brief Implementation of the setup_partitions function for Postgres
+ * @author Christian Grothoff
+ */
+#include "platform.h"
+#include "taler_error_codes.h"
+#include "taler_dbevents.h"
+#include "taler_pq_lib.h"
+#include "pg_setup_partitions.h"
+#include "pg_helper.h"
diff --git a/src/exchangedb/pg_setup_partitions.h b/src/exchangedb/pg_setup_partitions.h
new file mode 100644
index 000000000..a3f56ff16
--- /dev/null
+++ b/src/exchangedb/pg_setup_partitions.h
@@ -0,0 +1,39 @@
+/*
+ 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 exchangedb/pg_setup_partitions.h
+ * @brief implementation of the setup_partitions function for Postgres
+ * @author Christian Grothoff
+ */
+#ifndef PG_SETUP_PARTITIONS_H
+#define PG_SETUP_PARTITIONS_H
+
+#include "taler_util.h"
+#include "taler_json_lib.h"
+#include "taler_exchangedb_plugin.h"
+
+/**
+ * Setup partitions of already existing tables
+ *
+ * @param cls the `struct PostgresClosure` with the plugin-specific state
+ * @param num the number of partitions to create for each partitioned table
+ * @return #GNUNET_OK upon success; #GNUNET_SYSERR upon failure
+ */
+enum GNUNET_GenericReturnValue
+TEH_PG_setup_partitions (void *cls,
+ uint32_t num);
+
+#endif
diff --git a/src/exchangedb/plugin_exchangedb_postgres.c b/src/exchangedb/plugin_exchangedb_postgres.c
index 35bd9903e..7bd5fed0e 100644
--- a/src/exchangedb/plugin_exchangedb_postgres.c
+++ b/src/exchangedb/plugin_exchangedb_postgres.c
@@ -63,10 +63,12 @@
/**WHAT I ADD**/
#include "pg_insert_purse_request.h"
#include "pg_iterate_active_signkeys.h"
-
+#include "pg_prefligth.h"
#include "pg_commit.h"
-
-
+#include "pg_create_shard_tables.h"
+#include "pg_insert_aggregation_tracking.h"
+#include "pg_drop_tables.h"
+#include "pg_setup_partitions.h"
/**
* Set to 1 to enable Postgres auto_explain module. This will
* slow down things a _lot_, but also provide extensive logging
@@ -93,38 +95,6 @@
} while (0)
-/**
- * Drop all Taler tables. This should only be used by testcases.
- *
- * @param cls the `struct PostgresClosure` with the plugin-specific state
- * @return #GNUNET_OK upon success; #GNUNET_SYSERR upon failure
- */
-static enum GNUNET_GenericReturnValue
-postgres_drop_tables (void *cls)
-{
- struct PostgresClosure *pg = cls;
- struct GNUNET_PQ_Context *conn;
- enum GNUNET_GenericReturnValue ret;
-
- if (NULL != pg->conn)
- {
- GNUNET_PQ_disconnect (pg->conn);
- pg->conn = NULL;
- pg->init = false;
- }
- conn = GNUNET_PQ_connect_with_cfg (pg->cfg,
- "exchangedb-postgres",
- NULL,
- NULL,
- NULL);
- if (NULL == conn)
- return GNUNET_SYSERR;
- ret = GNUNET_PQ_exec_sql (conn,
- "drop");
- GNUNET_PQ_disconnect (conn);
- return ret;
-}
-
/**
* Create the necessary tables if they are not present
@@ -153,99 +123,6 @@ postgres_create_tables (void *cls)
}
-/**
- * Create tables of a shard node with index idx
- *
- * @param cls the `struct PostgresClosure` with the plugin-specific state
- * @param idx the shards index, will be appended as suffix to all tables
- * @return #GNUNET_OK upon success; #GNUNET_SYSERR upon failure
- */
-static enum GNUNET_GenericReturnValue
-postgres_create_shard_tables (void *cls,
- uint32_t idx)
-{
- struct PostgresClosure *pg = cls;
- struct GNUNET_PQ_Context *conn;
- enum GNUNET_GenericReturnValue ret = GNUNET_OK;
- struct GNUNET_PQ_QueryParam params[] = {
- GNUNET_PQ_query_param_uint32 (&idx),
- GNUNET_PQ_query_param_end
- };
- struct GNUNET_PQ_ExecuteStatement es[] = {
- GNUNET_PQ_make_try_execute ("SET search_path TO exchange;"),
- GNUNET_PQ_EXECUTE_STATEMENT_END
- };
-
- struct GNUNET_PQ_PreparedStatement ps[] = {
- GNUNET_PQ_make_prepare ("create_shard_tables",
- "SELECT"
- " setup_shard"
- " ($1);"),
- GNUNET_PQ_PREPARED_STATEMENT_END
- };
-
- conn = GNUNET_PQ_connect_with_cfg (pg->cfg,
- "exchangedb-postgres",
- "shard-",
- es,
- ps);
- if (NULL == conn)
- return GNUNET_SYSERR;
- if (0 > GNUNET_PQ_eval_prepared_non_select (conn,
- "create_shard_tables",
- params))
- ret = GNUNET_SYSERR;
- GNUNET_PQ_disconnect (conn);
- return ret;
-}
-
-
-/**
- * Setup partitions of already existing tables
- *
- * @param cls the `struct PostgresClosure` with the plugin-specific state
- * @param num the number of partitions to create for each partitioned table
- * @return #GNUNET_OK upon success; #GNUNET_SYSERR upon failure
- */
-static enum GNUNET_GenericReturnValue
-postgres_setup_partitions (void *cls,
- uint32_t num)
-{
- struct PostgresClosure *pg = cls;
- struct GNUNET_PQ_Context *conn;
- enum GNUNET_GenericReturnValue ret = GNUNET_OK;
- struct GNUNET_PQ_QueryParam params[] = {
- GNUNET_PQ_query_param_uint32 (&num),
- GNUNET_PQ_query_param_end
- };
- struct GNUNET_PQ_PreparedStatement ps[] = {
- GNUNET_PQ_make_prepare ("setup_partitions",
- "SELECT"
- " create_partitions"
- " ($1);"),
- GNUNET_PQ_PREPARED_STATEMENT_END
- };
- struct GNUNET_PQ_ExecuteStatement es[] = {
- GNUNET_PQ_make_try_execute ("SET search_path TO exchange;"),
- GNUNET_PQ_EXECUTE_STATEMENT_END
- };
-
- conn = GNUNET_PQ_connect_with_cfg (pg->cfg,
- "exchangedb-postgres",
- NULL,
- es,
- ps);
- if (NULL == conn)
- return GNUNET_SYSERR;
- ret = GNUNET_OK;
- if (0 > GNUNET_PQ_eval_prepared_non_select (conn,
- "setup_partitions",
- params))
- ret = GNUNET_SYSERR;
- GNUNET_PQ_disconnect (conn);
- return ret;
-}
-
/**
* Setup foreign servers (shards) for already existing tables
@@ -591,6 +468,7 @@ prepare_statements (struct PostgresClosure *pg)
",amount_frac"
",master_sig"
") VALUES ($1, $2, $3, $4, $5, $6, $7);"),
+
/* Used in #postgres_profit_drains_get_pending() */
GNUNET_PQ_make_prepare (
"get_ready_profit_drain",
@@ -1458,14 +1336,7 @@ prepare_statements (struct PostgresClosure *pg)
" WHERE dep.coin_pub=$1"
" AND dep.merchant_pub=$3"
" AND dep.h_contract_terms=$2"),
- /* Used in #postgres_insert_aggregation_tracking */
- GNUNET_PQ_make_prepare (
- "insert_aggregation_tracking",
- "INSERT INTO aggregation_tracking "
- "(deposit_serial_id"
- ",wtid_raw"
- ") VALUES "
- "($1, $2);"),
+
/* Used in #postgres_get_wire_fee() */
GNUNET_PQ_make_prepare (
"get_wire_fee",
@@ -2088,26 +1959,7 @@ prepare_statements (struct PostgresClosure *pg)
",contract_sig"
" FROM contracts"
" WHERE purse_pub=$1;"),
- /* Used in #postgres_insert_purse_request() */
- GNUNET_PQ_make_prepare (
- "insert_purse_request",
- "INSERT INTO purse_requests"
- " (purse_pub"
- " ,merge_pub"
- " ,purse_creation"
- " ,purse_expiration"
- " ,h_contract_terms"
- " ,age_limit"
- " ,flags"
- " ,in_reserve_quota"
- " ,amount_with_fee_val"
- " ,amount_with_fee_frac"
- " ,purse_fee_val"
- " ,purse_fee_frac"
- " ,purse_sig"
- " ) VALUES "
- " ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13)"
- " ON CONFLICT DO NOTHING;"),
+
/* Used in #postgres_select_purse_by_merge_pub */
GNUNET_PQ_make_prepare (
"select_purse_by_merge_pub",
@@ -2384,52 +2236,6 @@ internal_setup (struct PostgresClosure *pg,
}
-/**
- * Do a pre-flight check that we are not in an uncommitted transaction.
- * If we are, try to commit the previous transaction and output a warning.
- * Does not return anything, as we will continue regardless of the outcome.
- *
- * @param cls the `struct PostgresClosure` with the plugin-specific state
- * @return #GNUNET_OK if everything is fine
- * #GNUNET_NO if a transaction was rolled back
- * #GNUNET_SYSERR on hard errors
- */
-static enum GNUNET_GenericReturnValue
-postgres_preflight (void *cls)
-{
- struct PostgresClosure *pg = cls;
- struct GNUNET_PQ_ExecuteStatement es[] = {
- GNUNET_PQ_make_execute ("ROLLBACK"),
- GNUNET_PQ_EXECUTE_STATEMENT_END
- };
-
- if (! pg->init)
- {
- if (GNUNET_OK !=
- internal_setup (pg,
- false))
- return GNUNET_SYSERR;
- }
- if (NULL == pg->transaction_name)
- return GNUNET_OK; /* all good */
- if (GNUNET_OK ==
- GNUNET_PQ_exec_statements (pg->conn,
- es))
- {
- GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
- "BUG: Preflight check rolled back transaction `%s'!\n",
- pg->transaction_name);
- }
- else
- {
- GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
- "BUG: Preflight check failed to rollback transaction `%s'!\n",
- pg->transaction_name);
- }
- pg->transaction_name = NULL;
- return GNUNET_NO;
-}
-
/**
* Start a transaction.
@@ -2451,7 +2257,7 @@ postgres_start (void *cls,
GNUNET_assert (NULL != name);
if (GNUNET_SYSERR ==
- postgres_preflight (pg))
+ TEH_PG_preflight (pg))
return GNUNET_SYSERR;
GNUNET_log (GNUNET_ERROR_TYPE_INFO,
"Starting transaction `%s'\n",
@@ -2489,7 +2295,7 @@ postgres_start_read_committed (void *cls,
GNUNET_assert (NULL != name);
if (GNUNET_SYSERR ==
- postgres_preflight (pg))
+ TEH_PG_preflight (pg))
return GNUNET_SYSERR;
GNUNET_log (GNUNET_ERROR_TYPE_INFO,
"Starting READ COMMITTED transaction `%s`\n",
@@ -2528,7 +2334,7 @@ postgres_start_read_only (void *cls,
GNUNET_assert (NULL != name);
if (GNUNET_SYSERR ==
- postgres_preflight (pg))
+ TEH_PG_preflight (pg))
return GNUNET_SYSERR;
GNUNET_log (GNUNET_ERROR_TYPE_INFO,
"Starting READ ONLY transaction `%s`\n",
@@ -5849,33 +5655,6 @@ postgres_lookup_transfer_by_deposit (
}
-/**
- * Function called to insert aggregation information into the DB.
- *
- * @param cls closure
- * @param wtid the raw wire transfer identifier we used
- * @param deposit_serial_id row in the deposits table for which this is aggregation data
- * @return transaction status code
- */
-static enum GNUNET_DB_QueryStatus
-postgres_insert_aggregation_tracking (
- void *cls,
- const struct TALER_WireTransferIdentifierRawP *wtid,
- unsigned long long deposit_serial_id)
-{
- struct PostgresClosure *pg = cls;
- uint64_t rid = deposit_serial_id;
- struct GNUNET_PQ_QueryParam params[] = {
- GNUNET_PQ_query_param_uint64 (&rid),
- GNUNET_PQ_query_param_auto_from_type (wtid),
- GNUNET_PQ_query_param_end
- };
-
- return GNUNET_PQ_eval_prepared_non_select (pg->conn,
- "insert_aggregation_tracking",
- params);
-}
-
/**
* Obtain wire fee from database.
@@ -6597,7 +6376,7 @@ postgres_start_deferred_wire_out (void *cls)
};
if (GNUNET_SYSERR ==
- postgres_preflight (pg))
+ TEH_PG_preflight (pg))
return GNUNET_SYSERR;
if (GNUNET_OK !=
GNUNET_PQ_exec_statements (pg->conn,
@@ -11856,16 +11635,13 @@ libtaler_plugin_exchangedb_postgres_init (void *cls)
plugin = GNUNET_new (struct TALER_EXCHANGEDB_Plugin);
plugin->cls = pg;
- plugin->drop_tables = &postgres_drop_tables;
+
plugin->create_tables = &postgres_create_tables;
- plugin->create_shard_tables = &postgres_create_shard_tables;
- plugin->setup_partitions = &postgres_setup_partitions;
+
plugin->setup_foreign_servers = &postgres_setup_foreign_servers;
plugin->start = &postgres_start;
plugin->start_read_committed = &postgres_start_read_committed;
plugin->start_read_only = &postgres_start_read_only;
-
- plugin->preflight = &postgres_preflight;
plugin->rollback = &postgres_rollback;
plugin->event_listen = &postgres_event_listen;
plugin->event_listen_cancel = &postgres_event_listen_cancel;
@@ -11917,7 +11693,6 @@ libtaler_plugin_exchangedb_postgres_init (void *cls)
plugin->get_refresh_reveal = &postgres_get_refresh_reveal;
plugin->lookup_wire_transfer = &postgres_lookup_wire_transfer;
plugin->lookup_transfer_by_deposit = &postgres_lookup_transfer_by_deposit;
- plugin->insert_aggregation_tracking = &postgres_insert_aggregation_tracking;
plugin->insert_wire_fee = &postgres_insert_wire_fee;
plugin->insert_global_fee = &postgres_insert_global_fee;
plugin->get_wire_fee = &postgres_get_wire_fee;
@@ -12035,6 +11810,7 @@ libtaler_plugin_exchangedb_postgres_init (void *cls)
= &postgres_expire_purse;
plugin->select_purse_by_merge_pub
= &postgres_select_purse_by_merge_pub;
+
plugin->do_purse_deposit
= &postgres_do_purse_deposit;
plugin->set_purse_balance
@@ -12080,6 +11856,8 @@ libtaler_plugin_exchangedb_postgres_init (void *cls)
/* NEW style, sort alphabetically! */
plugin->do_reserve_open
= &TEH_PG_do_reserve_open;
+ plugin->drop_tables
+ = &TEH_PG_drop_tables;
plugin->do_withdraw
= &TEH_PG_do_withdraw;
plugin->free_coin_transaction_list
@@ -12140,6 +11918,14 @@ libtaler_plugin_exchangedb_postgres_init (void *cls)
= &TEH_PG_iterate_active_signkeys;
plugin->commit
= &TEH_PG_commit;
+ plugin->preflight
+ = &TEH_PG_preflight;
+ plugin->create_shard_tables
+ = &TEH_PG_create_shard_tables;
+ plugin->insert_aggregation_tracking
+ = &TEH_PG_insert_aggregation_tracking;
+ plugin->setup_partitions
+ = &TEH_PG_setup_partitions;
return plugin;
}