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