get_coin_inconsistency.c (5233B)
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 17 18 #include "taler/taler_error_codes.h" 19 #include "taler/taler_pq_lib.h" 20 #include "pg_helper.h" 21 #include "auditor-database/get_coin_inconsistency.h" 22 23 /** 24 * Closure for #deposit_confirmation_cb(). 25 */ 26 struct CoinInconsistencyContext 27 { 28 29 /** 30 * Function to call for each deposit confirmation. 31 */ 32 TALER_AUDITORDB_CoinInconsistencyCallback cb; 33 34 /** 35 * Closure for @e cb 36 */ 37 void *cb_cls; 38 39 /** 40 * Plugin context. 41 */ 42 struct TALER_AUDITORDB_PostgresContext *pg; 43 44 /** 45 * Query status to return. 46 */ 47 enum GNUNET_DB_QueryStatus qs; 48 }; 49 50 51 /** 52 * Helper function for #TALER_AUDITORDB_get_deposit_confirmations(). 53 * To be called with the results of a SELECT statement 54 * that has returned @a num_results results. 55 * 56 * @param cls closure of type `struct DepositConfirmationContext *` 57 * @param result the postgres result 58 * @param num_results the number of results in @a result 59 */ 60 static void 61 coin_inconsistency_cb (void *cls, 62 PGresult *result, 63 unsigned int num_results) 64 { 65 struct CoinInconsistencyContext *dcc = cls; 66 struct TALER_AUDITORDB_PostgresContext *pg = dcc->pg; 67 68 for (unsigned int i = 0; i < num_results; i++) 69 { 70 uint64_t serial_id; 71 72 struct TALER_AUDITORDB_CoinInconsistency dc; 73 74 struct GNUNET_PQ_ResultSpec rs[] = { 75 GNUNET_PQ_result_spec_uint64 ("row_id", 76 &serial_id), 77 GNUNET_PQ_result_spec_string ("operation", 78 &dc.operation), 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_auto_from_type ("coin_pub", &dc.coin_pub), 84 GNUNET_PQ_result_spec_bool ("profitable", 85 &dc.profitable), 86 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 101 dcc->qs = i + 1; 102 103 104 rval = dcc->cb (dcc->cb_cls, 105 serial_id, 106 &dc); 107 GNUNET_PQ_cleanup_result (rs); 108 if (GNUNET_OK != rval) 109 break; 110 } 111 } 112 113 114 enum GNUNET_DB_QueryStatus 115 TALER_AUDITORDB_get_coin_inconsistency ( 116 struct TALER_AUDITORDB_PostgresContext *pg, 117 int64_t limit, 118 uint64_t offset, 119 bool return_suppressed, // maybe not needed 120 TALER_AUDITORDB_CoinInconsistencyCallback cb, 121 void *cb_cls) 122 { 123 124 uint64_t plimit = (uint64_t) ((limit < 0) ? -limit : limit); 125 struct GNUNET_PQ_QueryParam params[] = { 126 GNUNET_PQ_query_param_uint64 (&offset), 127 GNUNET_PQ_query_param_bool (return_suppressed), 128 GNUNET_PQ_query_param_uint64 (&plimit), 129 GNUNET_PQ_query_param_end 130 }; 131 struct CoinInconsistencyContext dcc = { 132 .cb = cb, 133 .cb_cls = cb_cls, 134 .pg = pg 135 }; 136 enum GNUNET_DB_QueryStatus qs; 137 138 139 PREPARE (pg, 140 "auditor_coin_inconsistency_select_desc", 141 "SELECT" 142 " row_id" 143 ",operation" 144 ",exchange_amount" 145 ",auditor_amount" 146 ",coin_pub" 147 ",profitable" 148 " FROM auditor_coin_inconsistency" 149 " WHERE (row_id < $1)" 150 " AND ($2 OR suppressed is false)" 151 " ORDER BY row_id DESC" 152 " LIMIT $3" 153 ); 154 PREPARE (pg, 155 "auditor_coin_inconsistency_select_asc", 156 "SELECT" 157 " row_id" 158 ",operation" 159 ",exchange_amount" 160 ",auditor_amount" 161 ",coin_pub" 162 ",profitable" 163 " FROM auditor_coin_inconsistency" 164 " WHERE (row_id > $1)" 165 " AND ($2 OR suppressed is false)" 166 " ORDER BY row_id ASC" 167 " LIMIT $3" 168 ); 169 qs = GNUNET_PQ_eval_prepared_multi_select (pg->conn, 170 (limit > 0) ? 171 "auditor_coin_inconsistency_select_asc" 172 : 173 "auditor_coin_inconsistency_select_desc", 174 params, 175 &coin_inconsistency_cb, 176 &dcc); 177 178 179 if (qs > 0) 180 return dcc.qs; 181 GNUNET_break (GNUNET_DB_STATUS_HARD_ERROR != qs); 182 return qs; 183 }