iterate_wire_format_inconsistencies.c (5525B)
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_pq_lib.h" 17 #include "pg_helper.h" 18 #include "auditor-database/iterate_wire_format_inconsistencies.h" 19 20 21 /** 22 * Hard upper bound on the number of records returned by a single 23 * call, regardless of the limit requested by the client. 24 */ 25 #define MAX_RECORDS 50000 26 27 28 struct WireFormatInconsistencyContext 29 { 30 31 /** 32 * Function to call for each bad sig loss. 33 */ 34 TALER_AUDITORDB_WireFormatInconsistencyCallback cb; 35 36 /** 37 * Closure for @e cb 38 */ 39 void *cb_cls; 40 41 /** 42 * Plugin context. 43 */ 44 struct TALER_AUDITORDB_PostgresContext *pg; 45 46 /** 47 * Query status to return. 48 */ 49 enum GNUNET_DB_QueryStatus qs; 50 }; 51 52 53 /** 54 * Helper function for #TALER_AUDITORDB_iterate_wire_format_inconsistencies(). 55 * To be called with the results of a SELECT statement 56 * that has returned @a num_results results. 57 * 58 * @param cls closure of type `struct WireFormatInconsistencyContext *` 59 * @param result the postgres result 60 * @param num_results the number of results in @a result 61 */ 62 static void 63 wire_format_inconsistency_cb (void *cls, 64 PGresult *result, 65 unsigned int num_results) 66 { 67 struct WireFormatInconsistencyContext *dcc = cls; 68 struct TALER_AUDITORDB_PostgresContext *pg = dcc->pg; 69 70 for (unsigned int i = 0; i < num_results; i++) 71 { 72 struct TALER_AUDITORDB_WireFormatInconsistency dc; 73 struct GNUNET_PQ_ResultSpec rs[] = { 74 GNUNET_PQ_result_spec_uint64 ("row_id", 75 &dc.row_id), 76 TALER_PQ_RESULT_SPEC_AMOUNT ("amount", 77 &dc.amount), 78 GNUNET_PQ_result_spec_uint64 ("wire_offset", 79 &dc.wire_offset), 80 GNUNET_PQ_result_spec_string ("diagnostic", 81 &dc.diagnostic), 82 GNUNET_PQ_result_spec_bool ("suppressed", 83 &dc.suppressed), 84 GNUNET_PQ_result_spec_end 85 }; 86 enum GNUNET_GenericReturnValue rval; 87 88 if (GNUNET_OK != 89 GNUNET_PQ_extract_result (result, 90 rs, 91 i)) 92 { 93 GNUNET_break (0); 94 dcc->qs = GNUNET_DB_STATUS_HARD_ERROR; 95 return; 96 } 97 dcc->qs = i + 1; 98 rval = dcc->cb (dcc->cb_cls, 99 &dc); 100 GNUNET_PQ_cleanup_result (rs); 101 if (GNUNET_OK != rval) 102 break; 103 } 104 } 105 106 107 enum GNUNET_DB_QueryStatus 108 TALER_AUDITORDB_iterate_wire_format_inconsistencies (struct 109 TALER_AUDITORDB_PostgresContext 110 * 111 pg, 112 int64_t limit, 113 uint64_t offset, 114 bool return_suppressed, 115 TALER_AUDITORDB_WireFormatInconsistencyCallback 116 cb, 117 void *cb_cls) 118 { 119 uint64_t plimit = GNUNET_MIN ((uint64_t) MAX_RECORDS, 120 TALER_AUDITORDB_abs_limit (limit)); 121 struct GNUNET_PQ_QueryParam params[] = { 122 GNUNET_PQ_query_param_uint64 (&offset), 123 GNUNET_PQ_query_param_bool (return_suppressed), 124 GNUNET_PQ_query_param_uint64 (&plimit), 125 GNUNET_PQ_query_param_end 126 }; 127 struct WireFormatInconsistencyContext dcc = { 128 .cb = cb, 129 .cb_cls = cb_cls, 130 .pg = pg 131 }; 132 enum GNUNET_DB_QueryStatus qs; 133 134 PREPARE (pg, 135 "iterate_wire_format_inconsistencies_inconsistency_get_desc", 136 "SELECT" 137 " row_id," 138 " amount," 139 " wire_offset," 140 " diagnostic," 141 " suppressed" 142 " FROM auditor_wire_format_inconsistency" 143 " WHERE (row_id < $1)" 144 " AND ($2 OR NOT suppressed)" 145 " ORDER BY row_id DESC" 146 " LIMIT $3" 147 ); 148 PREPARE (pg, 149 "iterate_wire_format_inconsistencies_inconsistency_get_asc", 150 "SELECT" 151 " row_id," 152 " amount," 153 " wire_offset," 154 " diagnostic," 155 " suppressed" 156 " FROM auditor_wire_format_inconsistency" 157 " WHERE (row_id > $1)" 158 " AND ($2 OR NOT suppressed)" 159 " ORDER BY row_id ASC" 160 " LIMIT $3" 161 ); 162 qs = GNUNET_PQ_eval_prepared_multi_select ( 163 pg->conn, 164 (limit > 0) 165 ? "iterate_wire_format_inconsistencies_inconsistency_get_asc" 166 : "iterate_wire_format_inconsistencies_inconsistency_get_desc", 167 params, 168 &wire_format_inconsistency_cb, 169 &dcc); 170 if (qs > 0) 171 return dcc.qs; 172 GNUNET_break (GNUNET_DB_STATUS_HARD_ERROR != qs); 173 return qs; 174 }