iterate_exchange_signkeys.c (5023B)
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_pq_lib.h" 19 #include "pg_helper.h" 20 21 #include "auditor-database/iterate_exchange_signkeys.h" 22 23 24 /** 25 * Hard upper bound on the number of records returned by a single 26 * call, regardless of the limit requested by the client. 27 */ 28 #define MAX_RECORDS 50000 29 30 31 struct ExchangeSignkeysContext 32 { 33 34 /** 35 * Function to call for each bad sig loss. 36 */ 37 TALER_AUDITORDB_ExchangeSignkeysCallback cb; 38 39 /** 40 * Closure for @e cb 41 */ 42 void *cb_cls; 43 44 /** 45 * Plugin context. 46 */ 47 struct TALER_AUDITORDB_PostgresContext *pg; 48 49 /** 50 * Query status to return. 51 */ 52 enum GNUNET_DB_QueryStatus qs; 53 }; 54 55 56 /** 57 * Helper function for #TALER_AUDITORDB_iterate_exchange_signkeys(). 58 * To be called with the results of a SELECT statement 59 * that has returned @a num_results results. 60 * 61 * @param cls closure of type `struct ExchangeSignkeysContext *` 62 * @param result the postgres result 63 * @param num_results the number of results in @a result 64 */ 65 static void 66 exchange_signkeys_cb (void *cls, 67 PGresult *result, 68 unsigned int num_results) 69 { 70 struct ExchangeSignkeysContext *dcc = cls; 71 // struct TALER_AUDITORDB_PostgresContext *pg = dcc->pg; 72 73 for (unsigned int i = 0; i < num_results; i++) 74 { 75 uint64_t serial_id; 76 struct TALER_AUDITORDB_ExchangeSignkeys dc; 77 struct GNUNET_PQ_ResultSpec rs[] = { 78 GNUNET_PQ_result_spec_uint64 ("row_id", 79 &serial_id), 80 GNUNET_PQ_result_spec_auto_from_type ("exchange_pub", 81 &dc.exchange_pub), 82 GNUNET_PQ_result_spec_auto_from_type ("master_sig", 83 &dc.master_sig), 84 GNUNET_PQ_result_spec_absolute_time ("ep_valid_from", 85 &dc.ep_valid_from), 86 GNUNET_PQ_result_spec_absolute_time ("ep_expire_sign", 87 &dc.ep_expire_sign), 88 GNUNET_PQ_result_spec_absolute_time ("ep_expire_legal", 89 &dc.ep_expire_legal), 90 GNUNET_PQ_result_spec_end 91 }; 92 enum GNUNET_GenericReturnValue rval; 93 94 if (GNUNET_OK != 95 GNUNET_PQ_extract_result (result, 96 rs, 97 i)) 98 { 99 GNUNET_break (0); 100 dcc->qs = GNUNET_DB_STATUS_HARD_ERROR; 101 return; 102 } 103 104 dcc->qs = i + 1; 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 TALER_AUDITORDB_iterate_exchange_signkeys ( 118 struct TALER_AUDITORDB_PostgresContext *pg, 119 int64_t limit, 120 uint64_t offset, 121 TALER_AUDITORDB_ExchangeSignkeysCallback cb, 122 void *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_uint64 (&plimit), 129 GNUNET_PQ_query_param_end 130 }; 131 struct ExchangeSignkeysContext dcc = { 132 .cb = cb, 133 .cb_cls = cb_cls, 134 .pg = pg 135 }; 136 enum GNUNET_DB_QueryStatus qs; 137 138 PREPARE (pg, 139 "iterate_exchange_signkeys_desc", 140 "SELECT" 141 " row_id," 142 " exchange_pub," 143 " master_sig," 144 " ep_valid_from," 145 " ep_expire_sign," 146 " ep_expire_legal" 147 " FROM auditor_exchange_signkeys" 148 " WHERE (row_id < $1)" 149 " ORDER BY row_id DESC" 150 " LIMIT $2" 151 ); 152 PREPARE (pg, 153 "iterate_exchange_signkeys_asc", 154 "SELECT" 155 " row_id," 156 " exchange_pub," 157 " master_sig," 158 " ep_valid_from," 159 " ep_expire_sign," 160 " ep_expire_legal" 161 " FROM auditor_exchange_signkeys" 162 " WHERE (row_id > $1)" 163 " ORDER BY row_id ASC" 164 " LIMIT $2" 165 ); 166 qs = GNUNET_PQ_eval_prepared_multi_select ( 167 pg->conn, 168 (limit > 0) 169 ? "iterate_exchange_signkeys_asc" 170 : "iterate_exchange_signkeys_desc", 171 params, 172 &exchange_signkeys_cb, 173 &dcc); 174 if (qs > 0) 175 return dcc.qs; 176 GNUNET_break (GNUNET_DB_STATUS_HARD_ERROR != qs); 177 return qs; 178 }