iterate_reserve_balance_summary_wrong_inconsistencies.c (6313B)
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 \ 19 "auditor-database/iterate_reserve_balance_summary_wrong_inconsistencies.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 ReserveBalanceSummaryWrongInconsistencyContext 30 { 31 32 /** 33 * Function to call for each bad sig loss. 34 */ 35 TALER_AUDITORDB_ReserveBalanceSummaryWrongInconsistencyCallback 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_reserve_balance_summary_wrong_inconsistencies(). 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 ReserveBalanceSummaryWrongInconsistencyContext *` 60 * @param result the postgres result 61 * @param num_results the number of results in @a result 62 */ 63 static void 64 reserve_balance_summary_wrong_inconsistency_cb (void *cls, 65 PGresult *result, 66 unsigned int num_results) 67 { 68 struct ReserveBalanceSummaryWrongInconsistencyContext *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_ReserveBalanceSummaryWrongInconsistency dc; 74 struct GNUNET_PQ_ResultSpec rs[] = { 75 GNUNET_PQ_result_spec_uint64 ("row_id", 76 &dc.row_id), 77 GNUNET_PQ_result_spec_auto_from_type ("reserve_pub", 78 &dc.reserve_pub), 79 TALER_PQ_RESULT_SPEC_AMOUNT ("exchange_amount", 80 &dc.exchange_amount), 81 TALER_PQ_RESULT_SPEC_AMOUNT ("auditor_amount", 82 &dc.auditor_amount), 83 GNUNET_PQ_result_spec_bool ("suppressed", 84 &dc.suppressed), 85 GNUNET_PQ_result_spec_end 86 }; 87 enum GNUNET_GenericReturnValue rval; 88 89 if (GNUNET_OK != 90 GNUNET_PQ_extract_result (result, 91 rs, 92 i)) 93 { 94 GNUNET_break (0); 95 dcc->qs = GNUNET_DB_STATUS_HARD_ERROR; 96 return; 97 } 98 dcc->qs = i + 1; 99 rval = dcc->cb (dcc->cb_cls, 100 &dc); 101 GNUNET_PQ_cleanup_result (rs); 102 if (GNUNET_OK != rval) 103 break; 104 } 105 } 106 107 108 enum GNUNET_DB_QueryStatus 109 TALER_AUDITORDB_iterate_reserve_balance_summary_wrong_inconsistencies (struct 110 TALER_AUDITORDB_PostgresContext 111 *pg, 112 int64_t 113 limit, 114 uint64_t 115 offset 116 , 117 bool 118 return_suppressed, 119 TALER_AUDITORDB_ReserveBalanceSummaryWrongInconsistencyCallback 120 cb, 121 void * 122 cb_cls) 123 { 124 uint64_t plimit = GNUNET_MIN ((uint64_t) MAX_RECORDS, 125 TALER_AUDITORDB_abs_limit (limit)); 126 struct GNUNET_PQ_QueryParam params[] = { 127 GNUNET_PQ_query_param_uint64 (&offset), 128 GNUNET_PQ_query_param_bool (return_suppressed), 129 GNUNET_PQ_query_param_uint64 (&plimit), 130 GNUNET_PQ_query_param_end 131 }; 132 struct ReserveBalanceSummaryWrongInconsistencyContext dcc = { 133 .cb = cb, 134 .cb_cls = cb_cls, 135 .pg = pg 136 }; 137 enum GNUNET_DB_QueryStatus qs; 138 139 PREPARE (pg, 140 "iterate_reserve_balance_summary_wrong_inconsistencies_desc", 141 "SELECT" 142 " row_id" 143 ",reserve_pub" 144 ",exchange_amount" 145 ",auditor_amount" 146 ",suppressed" 147 " FROM auditor_reserve_balance_summary_wrong_inconsistency" 148 " WHERE (row_id < $1)" 149 " AND ($2 OR NOT suppressed)" 150 " ORDER BY row_id DESC" 151 " LIMIT $3" 152 ); 153 PREPARE (pg, 154 "iterate_reserve_balance_summary_wrong_inconsistencies_asc", 155 "SELECT" 156 " row_id" 157 ",reserve_pub" 158 ",exchange_amount" 159 ",auditor_amount" 160 ",suppressed" 161 " FROM auditor_reserve_balance_summary_wrong_inconsistency" 162 " WHERE (row_id > $1)" 163 " AND ($2 OR NOT suppressed)" 164 " ORDER BY row_id ASC" 165 " LIMIT $3" 166 ); 167 qs = GNUNET_PQ_eval_prepared_multi_select ( 168 pg->conn, 169 (limit > 0) 170 ? 171 "iterate_reserve_balance_summary_wrong_inconsistencies_asc" 172 : 173 "iterate_reserve_balance_summary_wrong_inconsistencies_desc", 174 params, 175 &reserve_balance_summary_wrong_inconsistency_cb, 176 &dcc); 177 178 if (qs > 0) 179 return dcc.qs; 180 GNUNET_break (GNUNET_DB_STATUS_HARD_ERROR != qs); 181 return qs; 182 }