aboutsummaryrefslogtreecommitdiff
path: root/src/auditordb
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2022-11-02 17:51:42 +0100
committerChristian Grothoff <christian@grothoff.org>2022-11-02 17:51:42 +0100
commita51517f64c83c23545ebd8a935baa311f0de635a (patch)
tree8431dcad0c9aa743468b551828be021a1378f4a6 /src/auditordb
parent5c0b8e32406a7e000396003e9428a1cf9af3e919 (diff)
-implement missing functions
Diffstat (limited to 'src/auditordb')
-rw-r--r--src/auditordb/auditor-0001.sql18
-rw-r--r--src/auditordb/pg_delete_purse_info.c16
-rw-r--r--src/auditordb/pg_get_purse_info.c29
-rw-r--r--src/auditordb/pg_get_purse_summary.c26
-rw-r--r--src/auditordb/pg_insert_purse_info.c22
-rw-r--r--src/auditordb/pg_insert_purse_summary.c20
-rw-r--r--src/auditordb/pg_select_purse_expired.c115
-rw-r--r--src/auditordb/pg_update_purse_info.c19
-rw-r--r--src/auditordb/pg_update_purse_summary.c18
9 files changed, 281 insertions, 2 deletions
diff --git a/src/auditordb/auditor-0001.sql b/src/auditordb/auditor-0001.sql
index 7bd5531e8..a19655d42 100644
--- a/src/auditordb/auditor-0001.sql
+++ b/src/auditordb/auditor-0001.sql
@@ -170,8 +170,8 @@ CREATE INDEX IF NOT EXISTS auditor_reserves_by_reserve_pub
CREATE TABLE IF NOT EXISTS auditor_purses
(purse_pub BYTEA NOT NULL CHECK(LENGTH(purse_pub)=32)
,master_pub BYTEA NOT NULL CONSTRAINT master_pub_ref REFERENCES auditor_exchanges(master_pub) ON DELETE CASCADE
- ,balance_val INT8 NOT NULL
- ,balance_frac INT4 NOT NULL
+ ,balance_val INT8 NOT NULL DEFAULT(0)
+ ,balance_frac INT4 NOT NULL DEFAULT(0)
,target_val INT8 NOT NULL
,target_frac INT4 NOT NULL
,expiration_date INT8 NOT NULL
@@ -185,6 +185,20 @@ CREATE INDEX IF NOT EXISTS auditor_purses_by_purse_pub
(purse_pub);
+CREATE TABLE IF NOT EXISTS auditor_purse_summary
+ (master_pub BYTEA NOT NULL CONSTRAINT master_pub_ref REFERENCES auditor_exchanges(master_pub) ON DELETE CASCADE
+ ,balance_val INT8 NOT NULL
+ ,balance_frac INT4 NOT NULL
+ ,open_purses INT8 NOT NULL
+ );
+COMMENT ON TABLE auditor_purse_summary
+ IS 'sum of the balances in open purses';
+
+CREATE INDEX IF NOT EXISTS auditor_purses_by_purse_pub
+ ON auditor_purses
+ (purse_pub);
+
+
CREATE TABLE IF NOT EXISTS auditor_reserve_balance
(master_pub BYTEA NOT NULL CONSTRAINT master_pub_ref REFERENCES auditor_exchanges(master_pub) ON DELETE CASCADE
,reserve_balance_val INT8 NOT NULL
diff --git a/src/auditordb/pg_delete_purse_info.c b/src/auditordb/pg_delete_purse_info.c
index 2b22218be..cd390d0ca 100644
--- a/src/auditordb/pg_delete_purse_info.c
+++ b/src/auditordb/pg_delete_purse_info.c
@@ -25,10 +25,26 @@
#include "pg_delete_purse_info.h"
#include "pg_helper.h"
+
enum GNUNET_DB_QueryStatus
TAH_PG_delete_purse_info (
void *cls,
const struct TALER_PurseContractPublicKeyP *purse_pub,
const struct TALER_MasterPublicKeyP *master_pub)
{
+ struct PostgresClosure *pg = cls;
+ struct GNUNET_PQ_QueryParam params[] = {
+ GNUNET_PQ_query_param_auto_from_type (purse_pub),
+ GNUNET_PQ_query_param_auto_from_type (master_pub),
+ GNUNET_PQ_query_param_end
+ };
+
+ PREPARE (pg,
+ "auditor_purses_delete",
+ "DELETE FROM auditor_purses "
+ " WHERE purse_pub=$1"
+ " AND master_pub=$2;");
+ return GNUNET_PQ_eval_prepared_non_select (pg->conn,
+ "auditor_purses_delete",
+ params);
}
diff --git a/src/auditordb/pg_get_purse_info.c b/src/auditordb/pg_get_purse_info.c
index 4c469978a..aa0f3027d 100644
--- a/src/auditordb/pg_get_purse_info.c
+++ b/src/auditordb/pg_get_purse_info.c
@@ -34,4 +34,33 @@ TAH_PG_get_purse_info (
struct TALER_Amount *balance,
struct GNUNET_TIME_Timestamp *expiration_date)
{
+ struct PostgresClosure *pg = cls;
+ struct GNUNET_PQ_QueryParam params[] = {
+ GNUNET_PQ_query_param_auto_from_type (purse_pub),
+ GNUNET_PQ_query_param_auto_from_type (master_pub),
+ GNUNET_PQ_query_param_end
+ };
+ struct GNUNET_PQ_ResultSpec rs[] = {
+ TALER_PQ_RESULT_SPEC_AMOUNT ("balance",
+ balance),
+ GNUNET_PQ_result_spec_timestamp ("expiration_date",
+ expiration_date),
+ GNUNET_PQ_result_spec_uint64 ("auditor_purses_rowid",
+ rowid),
+ GNUNET_PQ_result_spec_end
+ };
+
+ PREPARE (pg,
+ "auditor_get_purse_info",
+ "SELECT"
+ " expiration_date"
+ ",balance_val"
+ ",balance_frac"
+ " FROM auditor_purses"
+ " WHERE purse_pub=$1"
+ " AND master_pub=$2;");
+ return GNUNET_PQ_eval_prepared_singleton_select (pg->conn,
+ "auditor_get_purse_info",
+ params,
+ rs);
}
diff --git a/src/auditordb/pg_get_purse_summary.c b/src/auditordb/pg_get_purse_summary.c
index 5a5e087dd..1c5e8a36f 100644
--- a/src/auditordb/pg_get_purse_summary.c
+++ b/src/auditordb/pg_get_purse_summary.c
@@ -25,9 +25,35 @@
#include "pg_get_purse_summary.h"
#include "pg_helper.h"
+
enum GNUNET_DB_QueryStatus
TAH_PG_get_purse_summary (void *cls,
const struct TALER_MasterPublicKeyP *master_pub,
struct TALER_AUDITORDB_PurseBalance *sum)
{
+ struct PostgresClosure *pg = cls;
+ struct GNUNET_PQ_QueryParam params[] = {
+ GNUNET_PQ_query_param_auto_from_type (master_pub),
+ GNUNET_PQ_query_param_end
+ };
+ struct GNUNET_PQ_ResultSpec rs[] = {
+ TALER_PQ_RESULT_SPEC_AMOUNT ("balance",
+ &sum->balance),
+ GNUNET_PQ_result_spec_uint64 ("open_purses",
+ &sum->open_purses),
+ GNUNET_PQ_result_spec_end
+ };
+
+ PREPARE (pg,
+ "auditor_get_purse_summary",
+ "SELECT"
+ " open_purses"
+ ",balance_val"
+ ",balance_frac"
+ " FROM auditor_purse_summary"
+ " WHERE master_pub=$1;");
+ return GNUNET_PQ_eval_prepared_singleton_select (pg->conn,
+ "auditor_get_purse_summary",
+ params,
+ rs);
}
diff --git a/src/auditordb/pg_insert_purse_info.c b/src/auditordb/pg_insert_purse_info.c
index bec57c851..f82f9e1de 100644
--- a/src/auditordb/pg_insert_purse_info.c
+++ b/src/auditordb/pg_insert_purse_info.c
@@ -25,6 +25,7 @@
#include "pg_insert_purse_info.h"
#include "pg_helper.h"
+
enum GNUNET_DB_QueryStatus
TAH_PG_insert_purse_info (
void *cls,
@@ -33,4 +34,25 @@ TAH_PG_insert_purse_info (
const struct TALER_Amount *balance,
struct GNUNET_TIME_Timestamp expiration_date)
{
+ struct PostgresClosure *pg = cls;
+ struct GNUNET_PQ_QueryParam params[] = {
+ GNUNET_PQ_query_param_auto_from_type (purse_pub),
+ GNUNET_PQ_query_param_auto_from_type (master_pub),
+ TALER_PQ_query_param_amount (balance),
+ GNUNET_PQ_query_param_timestamp (&expiration_date),
+ GNUNET_PQ_query_param_end
+ };
+
+ PREPARE (pg,
+ "auditor_purses_insert",
+ "INSERT INTO auditor_purses "
+ "(purse_pub"
+ ",master_pub"
+ ",target_val"
+ ",target_frac"
+ ",expiration_date"
+ ") VALUES ($1,$2,$3,$4,$5);");
+ return GNUNET_PQ_eval_prepared_non_select (pg->conn,
+ "auditor_purses_insert",
+ params);
}
diff --git a/src/auditordb/pg_insert_purse_summary.c b/src/auditordb/pg_insert_purse_summary.c
index 6a1a5b950..f058815f4 100644
--- a/src/auditordb/pg_insert_purse_summary.c
+++ b/src/auditordb/pg_insert_purse_summary.c
@@ -25,10 +25,30 @@
#include "pg_insert_purse_summary.h"
#include "pg_helper.h"
+
enum GNUNET_DB_QueryStatus
TAH_PG_insert_purse_summary (
void *cls,
const struct TALER_MasterPublicKeyP *master_pub,
const struct TALER_AUDITORDB_PurseBalance *sum)
{
+ struct PostgresClosure *pg = cls;
+ struct GNUNET_PQ_QueryParam params[] = {
+ GNUNET_PQ_query_param_auto_from_type (master_pub),
+ TALER_PQ_query_param_amount (&sum->balance),
+ GNUNET_PQ_query_param_uint64 (&sum->open_purses),
+ GNUNET_PQ_query_param_end
+ };
+
+ PREPARE (pg,
+ "auditor_purse_summary_insert",
+ "INSERT INTO auditor_purse_summary "
+ "(master_pub"
+ ",balance_val"
+ ",balance_frac"
+ ",open_purses"
+ ") VALUES ($1,$2,$3,$4);");
+ return GNUNET_PQ_eval_prepared_non_select (pg->conn,
+ "auditor_purse_summary_insert",
+ params);
}
diff --git a/src/auditordb/pg_select_purse_expired.c b/src/auditordb/pg_select_purse_expired.c
index 7a9ba915d..49a8c5a97 100644
--- a/src/auditordb/pg_select_purse_expired.c
+++ b/src/auditordb/pg_select_purse_expired.c
@@ -25,6 +25,87 @@
#include "pg_select_purse_expired.h"
#include "pg_helper.h"
+
+/**
+ * Closure for #purse_expired_cb().
+ */
+struct PurseExpiredContext
+{
+
+ /**
+ * Function to call for each expired purse.
+ */
+ TALER_AUDITORDB_ExpiredPurseCallback cb;
+
+ /**
+ * Closure for @e cb
+ */
+ void *cb_cls;
+
+ /**
+ * Plugin context.
+ */
+ struct PostgresClosure *pg;
+
+ /**
+ * Query status to return.
+ */
+ enum GNUNET_DB_QueryStatus qs;
+};
+
+
+/**
+ * Helper function for #TAH_PG_select_purse_expired().
+ * To be called with the results of a SELECT statement
+ * that has returned @a num_results results.
+ *
+ * @param cls closure of type `struct PurseExpiredContext *`
+ * @param result the postgres result
+ * @param num_results the number of results in @a result
+ */
+static void
+purse_expired_cb (void *cls,
+ PGresult *result,
+ unsigned int num_results)
+{
+ struct PurseExpiredContext *eic = cls;
+ struct PostgresClosure *pg = eic->pg;
+
+ for (unsigned int i = 0; i < num_results; i++)
+ {
+ struct TALER_PurseContractPublicKeyP purse_pub;
+ struct GNUNET_TIME_Timestamp expiration_date;
+ struct TALER_Amount balance;
+ struct GNUNET_PQ_ResultSpec rs[] = {
+ GNUNET_PQ_result_spec_auto_from_type ("purse_pub",
+ &purse_pub),
+ TALER_PQ_RESULT_SPEC_AMOUNT ("balance",
+ &balance),
+ GNUNET_PQ_result_spec_timestamp ("expiration_date",
+ &expiration_date),
+ GNUNET_PQ_result_spec_end
+ };
+
+ if (GNUNET_OK !=
+ GNUNET_PQ_extract_result (result,
+ rs,
+ i))
+ {
+ GNUNET_break (0);
+ eic->qs = GNUNET_DB_STATUS_HARD_ERROR;
+ return;
+ }
+ eic->qs = i + 1;
+ if (GNUNET_OK !=
+ eic->cb (eic->cb_cls,
+ &purse_pub,
+ &balance,
+ expiration_date))
+ break;
+ }
+}
+
+
enum GNUNET_DB_QueryStatus
TAH_PG_select_purse_expired (
void *cls,
@@ -32,4 +113,38 @@ TAH_PG_select_purse_expired (
TALER_AUDITORDB_ExpiredPurseCallback cb,
void *cb_cls)
{
+ struct PostgresClosure *pg = cls;
+ struct GNUNET_TIME_Timestamp now
+ = GNUNET_TIME_timestamp_get ();
+ struct GNUNET_PQ_QueryParam params[] = {
+ GNUNET_PQ_query_param_auto_from_type (master_pub),
+ GNUNET_PQ_query_param_timestamp (&now),
+ GNUNET_PQ_query_param_end
+ };
+ struct PurseExpiredContext eic = {
+ .cb = cb,
+ .cb_cls = cb_cls,
+ .pg = pg
+ };
+ enum GNUNET_DB_QueryStatus qs;
+
+ PREPARE (pg,
+ "auditor_select_expired_purses",
+ "SELECT"
+ " purse_pub"
+ ",expiration_date"
+ ",balance_val"
+ ",balance_frac"
+ " FROM auditor_purses"
+ " WHERE master_pub=$1"
+ " AND expiration_date<$2;");
+ qs = GNUNET_PQ_eval_prepared_multi_select (pg->conn,
+ "auditor_select_expired_purses",
+ params,
+ &purse_expired_cb,
+ &eic);
+ if (qs > 0)
+ return eic.qs;
+ GNUNET_break (GNUNET_DB_STATUS_HARD_ERROR != qs);
+ return qs;
}
diff --git a/src/auditordb/pg_update_purse_info.c b/src/auditordb/pg_update_purse_info.c
index 0af0c913a..87c507b45 100644
--- a/src/auditordb/pg_update_purse_info.c
+++ b/src/auditordb/pg_update_purse_info.c
@@ -33,4 +33,23 @@ TAH_PG_update_purse_info (
const struct TALER_MasterPublicKeyP *master_pub,
const struct TALER_Amount *balance)
{
+
+ struct PostgresClosure *pg = cls;
+ struct GNUNET_PQ_QueryParam params[] = {
+ GNUNET_PQ_query_param_auto_from_type (purse_pub),
+ GNUNET_PQ_query_param_auto_from_type (master_pub),
+ TALER_PQ_query_param_amount (balance),
+ GNUNET_PQ_query_param_end
+ };
+
+ PREPARE (pg,
+ "auditor_purses_update",
+ "UPDATE auditor_purses SET "
+ " balance_val=$3"
+ ",balance_frac=$4"
+ " WHERE purse_pub=$1"
+ " AND master_pub=$2;");
+ return GNUNET_PQ_eval_prepared_non_select (pg->conn,
+ "auditor_purses_update",
+ params);
}
diff --git a/src/auditordb/pg_update_purse_summary.c b/src/auditordb/pg_update_purse_summary.c
index b03f0f4fd..45afe2c8c 100644
--- a/src/auditordb/pg_update_purse_summary.c
+++ b/src/auditordb/pg_update_purse_summary.c
@@ -32,4 +32,22 @@ TAH_PG_update_purse_summary (
const struct TALER_MasterPublicKeyP *master_pub,
const struct TALER_AUDITORDB_PurseBalance *sum)
{
+ struct PostgresClosure *pg = cls;
+ struct GNUNET_PQ_QueryParam params[] = {
+ GNUNET_PQ_query_param_auto_from_type (master_pub),
+ TALER_PQ_query_param_amount (&sum->balance),
+ GNUNET_PQ_query_param_uint64 (&sum->open_purses),
+ GNUNET_PQ_query_param_end
+ };
+
+ PREPARE (pg,
+ "auditor_purse_summary_update",
+ "UPDATE auditor_purse_summary SET"
+ " balance_val=$2"
+ ",balance_frac=$3"
+ ",open_purses=$4"
+ " WHERE master_pub=$1;");
+ return GNUNET_PQ_eval_prepared_non_select (pg->conn,
+ "auditor_purse_summary_update",
+ params);
}