select_refunds_above_serial_id.c (7516B)
1 /* 2 This file is part of TALER 3 Copyright (C) 2022 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 * @file exchangedb/select_refunds_above_serial_id.c 18 * @brief Implementation of the select_refunds_above_serial_id function for Postgres 19 * @author Christian Grothoff 20 */ 21 #include "taler/taler_error_codes.h" 22 #include "taler/taler_pq_lib.h" 23 #include "exchange-database/select_refunds_above_serial_id.h" 24 #include "helper.h" 25 26 /** 27 * Closure for #refunds_serial_helper_cb(). 28 */ 29 struct RefundsSerialContext 30 { 31 32 /** 33 * Callback to call. 34 */ 35 TALER_EXCHANGEDB_RefundCallback cb; 36 37 /** 38 * Closure for @e cb. 39 */ 40 void *cb_cls; 41 42 /** 43 * Plugin context. 44 */ 45 struct TALER_EXCHANGEDB_PostgresContext *pg; 46 47 /** 48 * Status code, set to #GNUNET_SYSERR on hard errors. 49 */ 50 enum GNUNET_GenericReturnValue status; 51 }; 52 53 54 /** 55 * Helper function 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 RefundsSerialContext` 59 * @param result the postgres result 60 * @param num_results the number of results in @a result 61 */ 62 static void 63 refunds_serial_helper_cb (void *cls, 64 PGresult *result, 65 unsigned int num_results) 66 { 67 struct RefundsSerialContext *rsc = cls; 68 struct TALER_EXCHANGEDB_PostgresContext *pg = rsc->pg; 69 70 for (unsigned int i = 0; i<num_results; i++) 71 { 72 struct TALER_EXCHANGEDB_Refund refund; 73 struct TALER_DenominationPublicKey denom_pub; 74 uint64_t rowid; 75 bool full_refund; 76 struct GNUNET_PQ_ResultSpec rs[] = { 77 GNUNET_PQ_result_spec_auto_from_type ("merchant_pub", 78 &refund.details.merchant_pub), 79 GNUNET_PQ_result_spec_auto_from_type ("merchant_sig", 80 &refund.details.merchant_sig), 81 GNUNET_PQ_result_spec_auto_from_type ("h_contract_terms", 82 &refund.details.h_contract_terms), 83 GNUNET_PQ_result_spec_uint64 ("rtransaction_id", 84 &refund.details.rtransaction_id), 85 TALER_PQ_result_spec_denom_pub ("denom_pub", 86 &denom_pub), 87 GNUNET_PQ_result_spec_auto_from_type ("coin_pub", 88 &refund.coin.coin_pub), 89 TALER_PQ_RESULT_SPEC_AMOUNT ("amount_with_fee", 90 &refund.details.refund_amount), 91 GNUNET_PQ_result_spec_uint64 ("refund_serial_id", 92 &rowid), 93 GNUNET_PQ_result_spec_end 94 }; 95 enum GNUNET_GenericReturnValue ret; 96 97 if (GNUNET_OK != 98 GNUNET_PQ_extract_result (result, 99 rs, 100 i)) 101 { 102 GNUNET_break (0); 103 rsc->status = GNUNET_SYSERR; 104 return; 105 } 106 { 107 struct GNUNET_PQ_QueryParam params[] = { 108 GNUNET_PQ_query_param_uint64 (&rowid), 109 GNUNET_PQ_query_param_end 110 }; 111 struct TALER_Amount amount_with_fee; 112 uint64_t s_f; 113 uint64_t s_v; 114 struct GNUNET_PQ_ResultSpec rs2[] = { 115 GNUNET_PQ_result_spec_uint64 ("s_v", 116 &s_v), 117 GNUNET_PQ_result_spec_uint64 ("s_f", 118 &s_f), 119 TALER_PQ_RESULT_SPEC_AMOUNT ("amount_with_fee", 120 &amount_with_fee), 121 GNUNET_PQ_result_spec_end 122 }; 123 enum GNUNET_DB_QueryStatus qs; 124 125 qs = GNUNET_PQ_eval_prepared_singleton_select ( 126 pg->conn, 127 "test_refund_full", 128 params, 129 rs2); 130 if (qs <= 0) 131 { 132 GNUNET_break (0); 133 rsc->status = GNUNET_SYSERR; 134 return; 135 } 136 /* normalize */ 137 s_v += s_f / TALER_AMOUNT_FRAC_BASE; 138 s_f %= TALER_AMOUNT_FRAC_BASE; 139 full_refund = (s_v > amount_with_fee.value) || 140 ( (s_v == amount_with_fee.value) && 141 (s_f >= amount_with_fee.fraction) ); 142 } 143 ret = rsc->cb (rsc->cb_cls, 144 rowid, 145 &denom_pub, 146 &refund.coin.coin_pub, 147 &refund.details.merchant_pub, 148 &refund.details.merchant_sig, 149 &refund.details.h_contract_terms, 150 refund.details.rtransaction_id, 151 full_refund, 152 &refund.details.refund_amount); 153 GNUNET_PQ_cleanup_result (rs); 154 if (GNUNET_OK != ret) 155 break; 156 } 157 } 158 159 160 enum GNUNET_DB_QueryStatus 161 TALER_EXCHANGEDB_select_refunds_above_serial_id ( 162 struct TALER_EXCHANGEDB_PostgresContext *pg, 163 uint64_t serial_id, 164 TALER_EXCHANGEDB_RefundCallback cb, 165 void *cb_cls) 166 { 167 struct GNUNET_PQ_QueryParam params[] = { 168 GNUNET_PQ_query_param_uint64 (&serial_id), 169 GNUNET_PQ_query_param_end 170 }; 171 struct RefundsSerialContext rsc = { 172 .cb = cb, 173 .cb_cls = cb_cls, 174 .pg = pg, 175 .status = GNUNET_OK 176 }; 177 enum GNUNET_DB_QueryStatus qs; 178 179 /* Fetch refunds with rowid '\geq' the given parameter */ 180 PREPARE (pg, 181 "audit_get_refunds_incr", 182 "SELECT" 183 " bdep.merchant_pub" 184 ",ref.merchant_sig" 185 ",bdep.h_contract_terms" 186 ",ref.rtransaction_id" 187 ",denom.denom_pub" 188 ",kc.coin_pub" 189 ",ref.amount_with_fee" 190 ",ref.refund_serial_id" 191 " FROM refunds ref" 192 " JOIN batch_deposits bdep" 193 " ON (ref.batch_deposit_serial_id=bdep.batch_deposit_serial_id)" 194 " JOIN coin_deposits cdep" 195 " ON (ref.coin_pub=cdep.coin_pub AND ref.batch_deposit_serial_id=cdep.batch_deposit_serial_id)" 196 " JOIN known_coins kc" 197 " ON (cdep.coin_pub=kc.coin_pub)" 198 " JOIN denominations denom" 199 " ON (kc.denominations_serial=denom.denominations_serial)" 200 " WHERE ref.refund_serial_id>=$1" 201 " ORDER BY ref.refund_serial_id ASC;"); 202 PREPARE (pg, 203 "test_refund_full", 204 "SELECT" 205 " CAST(SUM(CAST((ref.amount_with_fee).frac AS INT8)) AS INT8) AS s_f" 206 ",CAST(SUM((ref.amount_with_fee).val) AS INT8) AS s_v" 207 ",cdep.amount_with_fee" 208 " FROM refunds ref" 209 " JOIN coin_deposits cdep" 210 " ON (ref.coin_pub=cdep.coin_pub AND ref.batch_deposit_serial_id=cdep.batch_deposit_serial_id)" 211 " WHERE ref.refund_serial_id=$1" 212 " GROUP BY (cdep.amount_with_fee);"); 213 qs = GNUNET_PQ_eval_prepared_multi_select (pg->conn, 214 "audit_get_refunds_incr", 215 params, 216 &refunds_serial_helper_cb, 217 &rsc); 218 if (GNUNET_OK != rsc.status) 219 return GNUNET_DB_STATUS_HARD_ERROR; 220 return qs; 221 }