iterate_bad_sig_losses.c (5530B)
1 /* 2 This file is part of TALER 3 Copyright (C) 2024 Taler Systems SA 4 5 TALER is free software; you can redistribute it and/or modify it under the 6 terms of the GNU General Public License as published by the Free Software 7 Foundation; either version 3, or (at your option) any later version. 8 9 TALER is distributed in the hope that it will be useful, but WITHOUT ANY 10 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR 11 A PARTICULAR PURPOSE. See the GNU General Public License for more details. 12 13 You should have received a copy of the GNU General Public License along with 14 TALER; see the file COPYING. If not, see <http://www.gnu.org/licenses/> 15 */ 16 #include "taler/taler_error_codes.h" 17 #include "taler/taler_pq_lib.h" 18 #include "pg_helper.h" 19 #include "auditor-database/iterate_bad_sig_losses.h" 20 21 22 /** 23 * Hard upper bound on the number of records returned by a single 24 * call, regardless of the limit requested by the client. 25 */ 26 #define MAX_RECORDS 50000 27 28 29 struct BadSigLossesContext 30 { 31 32 /** 33 * Function to call for each bad sig loss. 34 */ 35 TALER_AUDITORDB_BadSigLossesCallback cb; 36 37 /** 38 * Closure for @e cb 39 */ 40 void *cb_cls; 41 42 /** 43 * Plugin context. 44 */ 45 struct TALER_AUDITORDB_PostgresContext *pg; 46 47 /** 48 * Query status to return. 49 */ 50 enum GNUNET_DB_QueryStatus qs; 51 }; 52 53 54 /** 55 * Helper function for #TALER_AUDITORDB_iterate_bad_sig_losses(). 56 * To be called with the results of a SELECT statement 57 * that has returned @a num_results results. 58 * 59 * @param cls closure of type `struct BadSigLossesContext *` 60 * @param result the postgres result 61 * @param num_results the number of results in @a result 62 */ 63 static void 64 bad_sig_losses_cb (void *cls, 65 PGresult *result, 66 unsigned int num_results) 67 { 68 struct BadSigLossesContext *dcc = cls; 69 struct TALER_AUDITORDB_PostgresContext *pg = dcc->pg; 70 71 for (unsigned int i = 0; i < num_results; i++) 72 { 73 struct TALER_AUDITORDB_BadSigLosses dc; 74 struct GNUNET_PQ_ResultSpec rs[] = { 75 GNUNET_PQ_result_spec_uint64 ("row_id", 76 &dc.row_id), 77 GNUNET_PQ_result_spec_uint64 ("problem_row_id", 78 &dc.problem_row_id), 79 GNUNET_PQ_result_spec_string ("operation", 80 &dc.operation), 81 TALER_PQ_RESULT_SPEC_AMOUNT ("loss", 82 &dc.loss), 83 GNUNET_PQ_result_spec_auto_from_type ("operation_specific_pub", 84 &dc.operation_specific_pub), 85 GNUNET_PQ_result_spec_bool ("suppressed", 86 &dc.suppressed), 87 GNUNET_PQ_result_spec_end 88 }; 89 enum GNUNET_GenericReturnValue rval; 90 91 if (GNUNET_OK != 92 GNUNET_PQ_extract_result (result, 93 rs, 94 i)) 95 { 96 GNUNET_break (0); 97 dcc->qs = GNUNET_DB_STATUS_HARD_ERROR; 98 return; 99 } 100 dcc->qs = i + 1; 101 rval = dcc->cb (dcc->cb_cls, 102 &dc); 103 GNUNET_PQ_cleanup_result (rs); 104 if (GNUNET_OK != rval) 105 break; 106 } 107 } 108 109 110 enum GNUNET_DB_QueryStatus 111 TALER_AUDITORDB_iterate_bad_sig_losses ( 112 struct TALER_AUDITORDB_PostgresContext *pg, 113 int64_t limit, 114 uint64_t offset, 115 bool return_suppressed, 116 const struct GNUNET_CRYPTO_EddsaPublicKey *op_spec_pub, 117 const char *op, 118 TALER_AUDITORDB_BadSigLossesCallback cb, 119 void *cb_cls) 120 { 121 uint64_t plimit = GNUNET_MIN ((uint64_t) MAX_RECORDS, 122 TALER_AUDITORDB_abs_limit (limit)); 123 struct GNUNET_PQ_QueryParam params[] = { 124 GNUNET_PQ_query_param_uint64 (&offset), 125 GNUNET_PQ_query_param_bool (return_suppressed), 126 GNUNET_PQ_query_param_uint64 (&plimit), 127 NULL == op_spec_pub 128 ? GNUNET_PQ_query_param_null () 129 : GNUNET_PQ_query_param_auto_from_type (op_spec_pub), 130 NULL == op 131 ? GNUNET_PQ_query_param_null () 132 : GNUNET_PQ_query_param_string (op), 133 GNUNET_PQ_query_param_end 134 }; 135 struct BadSigLossesContext dcc = { 136 .cb = cb, 137 .cb_cls = cb_cls, 138 .pg = pg 139 }; 140 enum GNUNET_DB_QueryStatus qs; 141 142 PREPARE (pg, 143 "iterate_bad_sig_losses_desc", 144 "SELECT" 145 " row_id" 146 ",problem_row_id" 147 ",operation" 148 ",loss" 149 ",operation_specific_pub" 150 ",suppressed" 151 " FROM auditor_bad_sig_losses" 152 " WHERE (row_id < $1)" 153 " AND ($2 OR NOT suppressed)" 154 " AND ($4::BYTEA IS NULL OR operation_specific_pub = $4)" 155 " AND ($5::TEXT IS NULL OR operation = $5)" 156 " ORDER BY row_id DESC" 157 " LIMIT $3" 158 ); 159 PREPARE (pg, 160 "iterate_bad_sig_losses_asc", 161 "SELECT" 162 " row_id" 163 ",problem_row_id" 164 ",operation" 165 ",loss" 166 ",operation_specific_pub" 167 ",suppressed" 168 " FROM auditor_bad_sig_losses" 169 " WHERE (row_id > $1)" 170 " AND ($2 OR NOT suppressed)" 171 " AND ($4::BYTEA IS NULL OR operation_specific_pub = $4)" 172 " AND ($5::TEXT IS NULL OR operation = $5)" 173 " ORDER BY row_id ASC" 174 " LIMIT $3" 175 ); 176 qs = GNUNET_PQ_eval_prepared_multi_select ( 177 pg->conn, 178 (limit > 0) 179 ? "iterate_bad_sig_losses_asc" 180 : "iterate_bad_sig_losses_desc", 181 params, 182 &bad_sig_losses_cb, 183 &dcc); 184 if (qs > 0) 185 return dcc.qs; 186 GNUNET_break (GNUNET_DB_STATUS_HARD_ERROR != qs); 187 return qs; 188 }