diff options
author | Cedric Zwahlen <cedric.zwahlen@students.bfh.ch> | 2024-04-08 23:21:50 +0200 |
---|---|---|
committer | Cedric Zwahlen <cedric.zwahlen@students.bfh.ch> | 2024-04-19 20:47:32 +0200 |
commit | 6749bd0aa6ac33991e271298454382425a865646 (patch) | |
tree | c3b4e88494b8f332e2e14bf1a3d93d5279ad9a98 | |
parent | 99a2d0029ee19a164ae9a22036220cd4e91a9268 (diff) | |
download | exchange-6749bd0aa6ac33991e271298454382425a865646.tar.gz exchange-6749bd0aa6ac33991e271298454382425a865646.tar.bz2 exchange-6749bd0aa6ac33991e271298454382425a865646.zip |
Fix and Improve CRUD files
-rw-r--r-- | src/auditor/Makefile.am | 1 | ||||
-rw-r--r-- | src/auditor/taler-auditor-httpd.c | 22 | ||||
-rw-r--r-- | src/auditor/taler-auditor-httpd_bad-sig-losses-del.c | 2 | ||||
-rw-r--r-- | src/auditor/taler-auditor-httpd_bad-sig-losses-get.c | 31 | ||||
-rw-r--r-- | src/auditor/taler-auditor-httpd_bad-sig-losses-upd.c | 134 | ||||
-rw-r--r-- | src/auditor/taler-auditor-httpd_bad-sig-losses-upd.h | 21 | ||||
-rw-r--r-- | src/auditordb/0002-auditor_bad_sig_losses.sql | 5 | ||||
-rw-r--r-- | src/auditordb/Makefile.am | 1 | ||||
-rw-r--r-- | src/auditordb/pg_get_bad_sig_losses.c | 30 | ||||
-rw-r--r-- | src/auditordb/pg_get_bad_sig_losses.h | 3 | ||||
-rw-r--r-- | src/auditordb/pg_insert_bad_sig_losses.c | 2 | ||||
-rw-r--r-- | src/auditordb/pg_update_bad_sig_losses.c | 34 | ||||
-rw-r--r-- | src/auditordb/pg_update_bad_sig_losses.h | 16 | ||||
-rw-r--r-- | src/auditordb/plugin_auditordb_postgres.c | 4 | ||||
-rw-r--r-- | src/include/taler_auditordb_plugin.h | 19 |
15 files changed, 302 insertions, 23 deletions
diff --git a/src/auditor/Makefile.am b/src/auditor/Makefile.am index 664fb5a67..a7d593725 100644 --- a/src/auditor/Makefile.am +++ b/src/auditor/Makefile.am @@ -192,6 +192,7 @@ taler_auditor_httpd_SOURCES = \ taler-auditor-httpd_bad-sig-losses-put.c taler-auditor-httpd_bad-sig-losses-put.h \ taler-auditor-httpd_bad-sig-losses-get.c taler-auditor-httpd_bad-sig-losses-get.h \ taler-auditor-httpd_bad-sig-losses-del.c taler-auditor-httpd_bad-sig-losses-del.h \ + taler-auditor-httpd_bad-sig-losses-upd.c taler-auditor-httpd_bad-sig-losses-upd.h \ taler-auditor-httpd_closure-lags-put.c taler-auditor-httpd_closure-lags-put.h \ taler-auditor-httpd_closure-lags-get.c taler-auditor-httpd_closure-lags-get.h \ taler-auditor-httpd_closure-lags-del.c taler-auditor-httpd_closure-lags-del.h \ diff --git a/src/auditor/taler-auditor-httpd.c b/src/auditor/taler-auditor-httpd.c index 774cb2a8b..13ecc6d6c 100644 --- a/src/auditor/taler-auditor-httpd.c +++ b/src/auditor/taler-auditor-httpd.c @@ -69,6 +69,7 @@ #include "taler-auditor-httpd_bad-sig-losses-get.h" #include "taler-auditor-httpd_bad-sig-losses-put.h" #include "taler-auditor-httpd_bad-sig-losses-del.h" +#include "taler-auditor-httpd_bad-sig-losses-upd.h" #include "taler-auditor-httpd_closure-lags-get.h" #include "taler-auditor-httpd_closure-lags-put.h" @@ -331,6 +332,27 @@ handle_mhd_request (void *cls, &TAH_RESERVE_BALANCE_INSUFFICIENT_INCONSISTENCY_handler_delete, MHD_HTTP_OK }, + { "/bad-sig-losses", MHD_HTTP_METHOD_GET, + "application/json", + NULL, 0, + &TAH_BAD_SIG_LOSSES_handler_get, + MHD_HTTP_OK }, + { "/bad-sig-losses", MHD_HTTP_METHOD_PUT, + "application/json", + NULL, 0, + &TAH_BAD_SIG_LOSSES_PUT_handler, + MHD_HTTP_OK }, + { "/bad-sig-losses", MHD_HTTP_METHOD_DELETE, + "application/json", + NULL, 0, + &TAH_BAD_SIG_LOSSES_handler_delete, + MHD_HTTP_OK }, + { "/bad-sig-losses", MHD_HTTP_METHOD_PATCH, + "application/json", + NULL, 0, + &TAH_BAD_SIG_LOSSES_handler_update, + MHD_HTTP_OK }, + { "/config", MHD_HTTP_METHOD_GET, "application/json", NULL, 0, &handle_config, MHD_HTTP_OK }, diff --git a/src/auditor/taler-auditor-httpd_bad-sig-losses-del.c b/src/auditor/taler-auditor-httpd_bad-sig-losses-del.c index 042925ef2..aed37e311 100644 --- a/src/auditor/taler-auditor-httpd_bad-sig-losses-del.c +++ b/src/auditor/taler-auditor-httpd_bad-sig-losses-del.c @@ -56,7 +56,7 @@ TAH_BAD_SIG_LOSSES_handler_delete (struct TAH_RequestHandler *rh, qs = TAH_plugin->delete_bad_sig_losses (TAH_plugin->cls, row_id); - if (0 > qs) + if (0 == qs) { // goes in here if there was an error with the transaction GNUNET_break (GNUNET_DB_STATUS_HARD_ERROR == qs); diff --git a/src/auditor/taler-auditor-httpd_bad-sig-losses-get.c b/src/auditor/taler-auditor-httpd_bad-sig-losses-get.c index bc0a9d47d..300a3b61a 100644 --- a/src/auditor/taler-auditor-httpd_bad-sig-losses-get.c +++ b/src/auditor/taler-auditor-httpd_bad-sig-losses-get.c @@ -46,8 +46,7 @@ add_bad_sig_losses (void *cls, obj = GNUNET_JSON_PACK ( - // TODO: fill in - + GNUNET_JSON_pack_uint64 ("row_id", serial_id), GNUNET_JSON_pack_string ("operation", dc->operation), TALER_JSON_pack_amount ("loss", &dc->loss), GNUNET_JSON_pack_data_auto ("operation_specific_pub", @@ -99,20 +98,29 @@ TAH_BAD_SIG_LOSSES_handler_get (struct TAH_RequestHandler *rh, ja = json_array (); GNUNET_break (NULL != ja); - uint64_t row_id = 0; - bool return_suppressed; + int64_t limit = -20; + uint64_t offset; - struct GNUNET_JSON_Specification spec[] = { - GNUNET_JSON_spec_uint64 ("row_id", - &row_id), + TALER_MHD_parse_request_snumber (connection, + "limit", + &limit); + + if (limit < 0) + offset = INT64_MAX; + else + offset = 0; - GNUNET_JSON_spec_bool ("return_suppressed", - &return_suppressed), + TALER_MHD_parse_request_number (connection, + "offset", + &offset); + bool return_suppressed = false; + + struct GNUNET_JSON_Specification spec[] = { + GNUNET_JSON_spec_bool ("return_suppressed", &return_suppressed), GNUNET_JSON_spec_end () }; - // read the input json json_t *json_in; { @@ -145,7 +153,8 @@ TAH_BAD_SIG_LOSSES_handler_get (struct TAH_RequestHandler *rh, qs = TAH_plugin->get_bad_sig_losses ( TAH_plugin->cls, - row_id, + limit, + offset, return_suppressed, &add_bad_sig_losses, ja); diff --git a/src/auditor/taler-auditor-httpd_bad-sig-losses-upd.c b/src/auditor/taler-auditor-httpd_bad-sig-losses-upd.c new file mode 100644 index 000000000..dff5373da --- /dev/null +++ b/src/auditor/taler-auditor-httpd_bad-sig-losses-upd.c @@ -0,0 +1,134 @@ +/* + This file is part of TALER + Copyright (C) 2024 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/> + */ + + +#include "platform.h" +#include <gnunet/gnunet_util_lib.h> +#include <gnunet/gnunet_json_lib.h> +#include <jansson.h> +#include <microhttpd.h> +#include <pthread.h> +#include "taler_json_lib.h" +#include "taler_mhd_lib.h" +#include "taler-auditor-httpd.h" +#include "taler-auditor-httpd_bad-sig-losses-put.h" + +MHD_RESULT +TAH_BAD_SIG_LOSSES_handler_update ( + struct TAH_RequestHandler *rh, + struct MHD_Connection *connection, + void **connection_cls, + const char *upload_data, + size_t *upload_data_size, + const char *const args[]) +{ + enum GNUNET_DB_QueryStatus qs; + + if (GNUNET_SYSERR == + TAH_plugin->preflight (TAH_plugin->cls)) + { + GNUNET_break (0); + return TALER_MHD_reply_with_error (connection, + MHD_HTTP_INTERNAL_SERVER_ERROR, + TALER_EC_GENERIC_DB_SETUP_FAILED, + NULL); + } + + struct TALER_AUDITORDB_Generic_Update gu; + + struct GNUNET_JSON_Specification spec[] = { + + GNUNET_JSON_spec_uint64 ("row_id", &gu.row_id), + GNUNET_JSON_spec_bool ("suppressed", &gu.suppressed), + + GNUNET_JSON_spec_end () + }; + + json_t *json; + + (void) rh; + (void) connection_cls; + (void) upload_data; + (void) upload_data_size; + { + enum GNUNET_GenericReturnValue res; + + res = TALER_MHD_parse_post_json (connection, + connection_cls, + upload_data, + upload_data_size, + &json); + if (GNUNET_SYSERR == res) + return MHD_NO; + if ((GNUNET_NO == res) || + (NULL == json)) + return MHD_YES; + res = TALER_MHD_parse_json_data (connection, + json, + spec); + if (GNUNET_SYSERR == res) + { + json_decref (json); + return MHD_NO; /* hard failure */ + } + if (GNUNET_NO == res) + { + json_decref (json); + return MHD_YES; /* failure */ + } + } + + /* execute transaction */ + qs = TAH_plugin->update_bad_sig_losses (TAH_plugin->cls, &gu); + + GNUNET_JSON_parse_free (spec); + json_decref (json); + + MHD_RESULT ret = MHD_NO; + + switch (qs) + { + case GNUNET_DB_STATUS_HARD_ERROR: + GNUNET_break (0); + ret = TALER_MHD_reply_with_error (connection, + MHD_HTTP_INTERNAL_SERVER_ERROR, + TALER_EC_GENERIC_DB_STORE_FAILED, + "update_account"); + break; + case GNUNET_DB_STATUS_SOFT_ERROR: + GNUNET_break (0); + ret = TALER_MHD_reply_with_error (connection, + MHD_HTTP_INTERNAL_SERVER_ERROR, + TALER_EC_GENERIC_INTERNAL_INVARIANT_FAILURE, + "unexpected serialization problem"); + break; + case GNUNET_DB_STATUS_SUCCESS_NO_RESULTS: + return TALER_MHD_reply_with_error (connection, + MHD_HTTP_NOT_FOUND, + TALER_EC_MERCHANT_GENERIC_ACCOUNT_UNKNOWN, + "no updates executed"); + break; + case GNUNET_DB_STATUS_SUCCESS_ONE_RESULT: + ret = TALER_MHD_reply_static (connection, + MHD_HTTP_NO_CONTENT, + NULL, + NULL, + 0); + break; + } + + return ret; +} diff --git a/src/auditor/taler-auditor-httpd_bad-sig-losses-upd.h b/src/auditor/taler-auditor-httpd_bad-sig-losses-upd.h new file mode 100644 index 000000000..5d4fef4bc --- /dev/null +++ b/src/auditor/taler-auditor-httpd_bad-sig-losses-upd.h @@ -0,0 +1,21 @@ +// +// Created by parallels on 08/04/24. +// + +#ifndef SRC_TALER_AUDITOR_HTTPD_BAD_SIG_LOSSES_UPD_H +#define SRC_TALER_AUDITOR_HTTPD_BAD_SIG_LOSSES_UPD_H + + +#include <microhttpd.h> +#include "taler-auditor-httpd.h" + +MHD_RESULT +TAH_BAD_SIG_LOSSES_handler_update (struct TAH_RequestHandler *rh, + struct MHD_Connection * + connection, + void **connection_cls, + const char *upload_data, + size_t *upload_data_size, + const char *const args[]); + +#endif // SRC_TALER_AUDITOR_HTTPD_BAD_SIG_LOSSES_UPD_H diff --git a/src/auditordb/0002-auditor_bad_sig_losses.sql b/src/auditordb/0002-auditor_bad_sig_losses.sql index ac17a5120..e4e87f398 100644 --- a/src/auditordb/0002-auditor_bad_sig_losses.sql +++ b/src/auditordb/0002-auditor_bad_sig_losses.sql @@ -18,9 +18,10 @@ SET search_path TO auditor; CREATE TABLE IF NOT EXISTS auditor_bad_sig_losses
(
row_id BIGINT GENERATED BY DEFAULT AS IDENTITY UNIQUE PRIMARY KEY,
- operation BYTEA,
+ operation VARCHAR(64),
loss taler_amount,
- operation_specific_pub BYTEA
+ operation_specific_pub BYTEA NOT NULL CHECK (LENGTH(operation_specific_pub)=32),
+ suppressed BOOLEAN NOT NULL DEFAULT FALSE
);
COMMENT ON TABLE auditor_bad_sig_losses
IS 'Report a (serious) inconsistency with losses due to bad signatures';
\ No newline at end of file diff --git a/src/auditordb/Makefile.am b/src/auditordb/Makefile.am index 87501dd5d..79cb8ae36 100644 --- a/src/auditordb/Makefile.am +++ b/src/auditordb/Makefile.am @@ -119,6 +119,7 @@ pg_get_reserve_balance_insufficient_inconsistency.c pg_get_reserve_balance_insuf pg_del_bad_sig_losses.c pg_del_bad_sig_losses.h \ pg_insert_bad_sig_losses.c pg_insert_bad_sig_losses.h \ pg_get_bad_sig_losses.c pg_get_bad_sig_losses.h \ +pg_update_bad_sig_losses.c pg_update_bad_sig_losses.h \ pg_del_auditor_closure_lags.c pg_del_auditor_closure_lags.h \ pg_insert_auditor_closure_lags.c pg_insert_auditor_closure_lags.h \ pg_get_auditor_closure_lags.c pg_get_auditor_closure_lags.h \ diff --git a/src/auditordb/pg_get_bad_sig_losses.c b/src/auditordb/pg_get_bad_sig_losses.c index e2f94dbc2..7c2e8eae0 100644 --- a/src/auditordb/pg_get_bad_sig_losses.c +++ b/src/auditordb/pg_get_bad_sig_losses.c @@ -98,7 +98,8 @@ bad_sig_losses_cb (void *cls, enum GNUNET_DB_QueryStatus TAH_PG_get_bad_sig_losses ( void *cls, - uint64_t start_id, + int64_t limit, + uint64_t offset, bool return_suppressed, // maybe not needed TALER_AUDITORDB_BadSigLossesCallback cb, void *cb_cls) @@ -106,8 +107,9 @@ TAH_PG_get_bad_sig_losses ( struct PostgresClosure *pg = cls; struct GNUNET_PQ_QueryParam params[] = { - GNUNET_PQ_query_param_uint64 (&start_id), + GNUNET_PQ_query_param_uint64 (&offset), GNUNET_PQ_query_param_bool (return_suppressed), + GNUNET_PQ_query_param_int64 (&limit), GNUNET_PQ_query_param_end }; struct BadSigLossesContext dcc = { @@ -118,17 +120,35 @@ TAH_PG_get_bad_sig_losses ( enum GNUNET_DB_QueryStatus qs; PREPARE (pg, - "auditor_bad_sig_losses_get", + "auditor_bad_sig_losses_get_desc", "SELECT" " row_id" ",operation" ",loss" ",operation_specific_pub" " FROM auditor_bad_sig_losses" - " WHERE row_id>$1" + " WHERE (row_id < $1)" + " AND ($2 OR suppressed is false)" + " ORDER BY row_id DESC" + " LIMIT $3" + ); + PREPARE (pg, + "auditor_bad_sig_losses_get_asc", + "SELECT" + " row_id" + ",operation" + ",loss" + ",operation_specific_pub" + " FROM auditor_bad_sig_losses" + " WHERE (row_id > $1)" + " AND ($2 OR suppressed is false)" + " ORDER BY row_id ASC" + " LIMIT $3" ); qs = GNUNET_PQ_eval_prepared_multi_select (pg->conn, - "auditor_bad_sig_losses_get", + (limit > 0) + ? "auditor_bad_sig_losses_get_asc" + : "auditor_bad_sig_losses_get_desc", params, &bad_sig_losses_cb, &dcc); diff --git a/src/auditordb/pg_get_bad_sig_losses.h b/src/auditordb/pg_get_bad_sig_losses.h index 878663cff..4e5bbd98b 100644 --- a/src/auditordb/pg_get_bad_sig_losses.h +++ b/src/auditordb/pg_get_bad_sig_losses.h @@ -23,7 +23,8 @@ enum GNUNET_DB_QueryStatus TAH_PG_get_bad_sig_losses ( void *cls, - uint64_t start_id, + int64_t limit, + uint64_t offset, bool return_suppressed, TALER_AUDITORDB_BadSigLossesCallback cb, void *cb_cls); diff --git a/src/auditordb/pg_insert_bad_sig_losses.c b/src/auditordb/pg_insert_bad_sig_losses.c index 75cc98d37..72e7929f7 100644 --- a/src/auditordb/pg_insert_bad_sig_losses.c +++ b/src/auditordb/pg_insert_bad_sig_losses.c @@ -29,7 +29,7 @@ TAH_PG_insert_bad_sig_losses ( struct PostgresClosure *pg = cls; struct GNUNET_PQ_QueryParam params[] = { - GNUNET_PQ_query_param_auto_from_type (&dc->operation), + GNUNET_PQ_query_param_string (dc->operation), TALER_PQ_query_param_amount (pg->conn, &dc->loss), GNUNET_PQ_query_param_auto_from_type (&dc->operation_specific_pub), diff --git a/src/auditordb/pg_update_bad_sig_losses.c b/src/auditordb/pg_update_bad_sig_losses.c new file mode 100644 index 000000000..2ca92d8e1 --- /dev/null +++ b/src/auditordb/pg_update_bad_sig_losses.c @@ -0,0 +1,34 @@ +// +// Created by parallels on 08/04/24. +// + + +#include "platform.h" +#include "taler_pq_lib.h" +#include "pg_helper.h" + +#include "pg_update_bad_sig_losses.h" + + +enum GNUNET_DB_QueryStatus +TAH_PG_update_bad_sig_losses ( + void *cls, + const struct TALER_AUDITORDB_Generic_Update *gu) +{ + struct PostgresClosure *pg = cls; + struct GNUNET_PQ_QueryParam params[] = { + GNUNET_PQ_query_param_uint64 (&gu->row_id), + GNUNET_PQ_query_param_bool (gu->suppressed), + GNUNET_PQ_query_param_end + }; + + + PREPARE (pg, + "update_bad_sig_losses", + "UPDATE auditor_bad_sig_losses SET" + " suppressed=$2" + " WHERE row_id=$1"); + return GNUNET_PQ_eval_prepared_non_select (pg->conn, + "update_bad_sig_losses", + params); +} diff --git a/src/auditordb/pg_update_bad_sig_losses.h b/src/auditordb/pg_update_bad_sig_losses.h new file mode 100644 index 000000000..affb6a74c --- /dev/null +++ b/src/auditordb/pg_update_bad_sig_losses.h @@ -0,0 +1,16 @@ +// +// Created by parallels on 08/04/24. +// + +#ifndef SRC_PG_UPDATE_BAD_SIG_LOSSES_H +#define SRC_PG_UPDATE_BAD_SIG_LOSSES_H + +#include "taler_util.h" +#include "taler_auditordb_plugin.h" + +enum GNUNET_DB_QueryStatus +TAH_PG_update_bad_sig_losses ( + void *cls, + const struct TALER_AUDITORDB_Generic_Update *dc); + +#endif // SRC_PG_UPDATE_BAD_SIG_LOSSES_H diff --git a/src/auditordb/plugin_auditordb_postgres.c b/src/auditordb/plugin_auditordb_postgres.c index 5661610ff..5060a016c 100644 --- a/src/auditordb/plugin_auditordb_postgres.c +++ b/src/auditordb/plugin_auditordb_postgres.c @@ -60,6 +60,7 @@ #include "pg_get_coin_inconsistency.h" #include "pg_get_row_inconsistency.h" + // MARK: CRUD #include "pg_del_amount_arithmetic_inconsistency.h" @@ -89,6 +90,7 @@ #include "pg_get_bad_sig_losses.h" #include "pg_del_bad_sig_losses.h" #include "pg_insert_bad_sig_losses.h" +#include "pg_update_bad_sig_losses.h" #include "pg_get_denomination_key_validity_withdraw_inconsistency.h" #include "pg_del_denomination_key_validity_withdraw_inconsistency.h" @@ -644,7 +646,7 @@ libtaler_plugin_auditordb_postgres_init (void *cls) plugin->delete_bad_sig_losses = &TAH_PG_del_bad_sig_losses; plugin->insert_bad_sig_losses = &TAH_PG_insert_bad_sig_losses; plugin->get_bad_sig_losses = &TAH_PG_get_bad_sig_losses; - + plugin->update_bad_sig_losses = &TAH_PG_update_bad_sig_losses; plugin->delete_auditor_closure_lags = &TAH_PG_del_auditor_closure_lags; plugin->insert_auditor_closure_lags = &TAH_PG_insert_auditor_closure_lags; diff --git a/src/include/taler_auditordb_plugin.h b/src/include/taler_auditordb_plugin.h index 4dc3a2531..2beb3e0e0 100644 --- a/src/include/taler_auditordb_plugin.h +++ b/src/include/taler_auditordb_plugin.h @@ -198,6 +198,16 @@ struct TALER_AUDITORDB_DepositConfirmation // MARK: CRUD /** + * Information about a row inconsistency + */ +struct TALER_AUDITORDB_Generic_Update +{ + uint64_t row_id; + bool suppressed; + bool ancient; +}; + +/** * Information about an arithmetic inconsistency */ struct TALER_AUDITORDB_AmountArithmeticInconsistency @@ -1066,7 +1076,8 @@ struct TALER_AUDITORDB_Plugin enum GNUNET_DB_QueryStatus (*get_bad_sig_losses)( void *cls, - uint64_t start_id, + int64_t limit, + uint64_t offset, bool return_suppressed, TALER_AUDITORDB_BadSigLossesCallback cb, void *cb_cls); @@ -1232,6 +1243,12 @@ struct TALER_AUDITORDB_Plugin void *cls, const struct TALER_AUDITORDB_RefreshesHanging *dc); + enum GNUNET_DB_QueryStatus + (*update_bad_sig_losses)( + void *cls, + const struct TALER_AUDITORDB_Generic_Update *gu); + + /** * Insert information about a reserve. There must not be an * existing record for the reserve. |