iterate_aml_decisions.c (9083B)
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 * @file exchangedb/iterate_aml_decisions.c 18 * @brief Implementation of the iterate_aml_decisions function for Postgres 19 * @author Christian Grothoff 20 */ 21 #include "taler/taler_pq_lib.h" 22 #include "taler/taler_kyclogic_lib.h" 23 #include "exchange-database/iterate_aml_decisions.h" 24 #include "helper.h" 25 26 27 /** 28 * Hard upper bound on the number of records returned by a single 29 * call, regardless of the limit requested by the client. 30 */ 31 #define MAX_RECORDS 50000 32 33 34 /** 35 * Closure for #handle_aml_result. 36 */ 37 struct AmlProcessResultContext 38 { 39 /** 40 * Function to call on each result. 41 */ 42 TALER_EXCHANGEDB_AmlDecisionCallback cb; 43 44 /** 45 * Closure for @e cb. 46 */ 47 void *cb_cls; 48 49 /** 50 * Plugin context. 51 */ 52 struct TALER_EXCHANGEDB_PostgresContext *pg; 53 54 /** 55 * Set to #GNUNET_SYSERR on serious errors. 56 */ 57 enum GNUNET_GenericReturnValue status; 58 }; 59 60 61 /** 62 * Function to be called with the results of a SELECT statement 63 * that has returned @a num_results results. Helper function 64 * for #TALER_EXCHANGEDB_iterate_aml_decisions(). 65 * 66 * @param cls closure of type `struct AmlProcessResultContext *` 67 * @param result the postgres result 68 * @param num_results the number of results in @a result 69 */ 70 static void 71 handle_aml_result (void *cls, 72 PGresult *result, 73 unsigned int num_results) 74 { 75 struct AmlProcessResultContext *ctx = cls; 76 77 for (unsigned int i = 0; i<num_results; i++) 78 { 79 struct TALER_NormalizedPaytoHashP h_payto; 80 uint64_t rowid; 81 char *justification = NULL; 82 struct GNUNET_TIME_Timestamp decision_time; 83 struct GNUNET_TIME_Absolute expiration_time; 84 json_t *jproperties = NULL; 85 bool is_wallet; 86 bool to_investigate; 87 bool is_active; 88 json_t *account_rules = NULL; 89 json_t *default_rules = NULL; 90 struct TALER_FullPayto payto; 91 struct GNUNET_PQ_ResultSpec rs[] = { 92 GNUNET_PQ_result_spec_uint64 ("outcome_serial_id", 93 &rowid), 94 GNUNET_PQ_result_spec_auto_from_type ("h_payto", 95 &h_payto), 96 GNUNET_PQ_result_spec_bool ("is_wallet", 97 &is_wallet), 98 GNUNET_PQ_result_spec_allow_null ( 99 GNUNET_PQ_result_spec_string ("justification", 100 &justification), 101 NULL), 102 GNUNET_PQ_result_spec_timestamp ("decision_time", 103 &decision_time), 104 GNUNET_PQ_result_spec_absolute_time ("expiration_time", 105 &expiration_time), 106 GNUNET_PQ_result_spec_allow_null ( 107 TALER_PQ_result_spec_json ("jproperties", 108 &jproperties), 109 NULL), 110 GNUNET_PQ_result_spec_allow_null ( 111 TALER_PQ_result_spec_json ("jnew_rules", 112 &account_rules), 113 NULL), 114 GNUNET_PQ_result_spec_bool ("to_investigate", 115 &to_investigate), 116 GNUNET_PQ_result_spec_bool ("is_active", 117 &is_active), 118 GNUNET_PQ_result_spec_string ("payto_uri", 119 &payto.full_payto), 120 GNUNET_PQ_result_spec_end 121 }; 122 123 if (GNUNET_OK != 124 GNUNET_PQ_extract_result (result, 125 rs, 126 i)) 127 { 128 GNUNET_break (0); 129 ctx->status = GNUNET_SYSERR; 130 return; 131 } 132 if (GNUNET_TIME_absolute_is_past (expiration_time)) 133 is_active = false; 134 if (NULL == account_rules) 135 { 136 /* A NULL rule set means the account is on the exchange's default 137 rules (see exchange_do_insert_successor_measure), not that it has 138 no limits. Report the defaults so the caller always gets a valid 139 rule set. */ 140 default_rules = TALER_KYCLOGIC_get_default_legi_rules (is_wallet); 141 account_rules = default_rules; 142 } 143 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 144 "Returning AML decisions for `%s' (%s)\n", 145 TALER_B2S (&h_payto), 146 is_wallet 147 ? "wallet" 148 : "account"); 149 ctx->cb (ctx->cb_cls, 150 rowid, 151 justification, 152 &h_payto, 153 decision_time, 154 expiration_time, 155 jproperties, 156 to_investigate, 157 is_active, 158 is_wallet, 159 payto, 160 account_rules); 161 json_decref (default_rules); 162 GNUNET_PQ_cleanup_result (rs); 163 } 164 } 165 166 167 enum GNUNET_DB_QueryStatus 168 TALER_EXCHANGEDB_iterate_aml_decisions ( 169 struct TALER_EXCHANGEDB_PostgresContext *pg, 170 const struct TALER_NormalizedPaytoHashP *h_payto, 171 enum TALER_EXCHANGE_YesNoAll investigation_only, 172 enum TALER_EXCHANGE_YesNoAll active_only, 173 uint64_t offset, 174 int64_t limit, 175 TALER_EXCHANGEDB_AmlDecisionCallback cb, 176 void *cb_cls) 177 { 178 uint64_t ulimit = GNUNET_MIN ((uint64_t) MAX_RECORDS, 179 TALER_EXCHANGEDB_abs_limit (limit)); 180 struct GNUNET_PQ_QueryParam params[] = { 181 GNUNET_PQ_query_param_bool (NULL == h_payto), 182 NULL == h_payto 183 ? GNUNET_PQ_query_param_null () 184 : GNUNET_PQ_query_param_auto_from_type (h_payto), 185 GNUNET_PQ_query_param_bool ((TALER_EXCHANGE_YNA_ALL == 186 investigation_only)), 187 GNUNET_PQ_query_param_bool ((TALER_EXCHANGE_YNA_YES == 188 investigation_only)), 189 GNUNET_PQ_query_param_bool ((TALER_EXCHANGE_YNA_ALL == 190 active_only)), 191 GNUNET_PQ_query_param_bool ((TALER_EXCHANGE_YNA_YES == 192 active_only)), 193 GNUNET_PQ_query_param_uint64 (&offset), 194 GNUNET_PQ_query_param_uint64 (&ulimit), 195 GNUNET_PQ_query_param_end 196 }; 197 struct AmlProcessResultContext ctx = { 198 .cb = cb, 199 .cb_cls = cb_cls, 200 .pg = pg, 201 .status = GNUNET_OK 202 }; 203 enum GNUNET_DB_QueryStatus qs; 204 const char *stmt = (limit > 0) 205 ? "iterate_aml_decisions_inc" 206 : "iterate_aml_decisions_dec"; 207 208 PREPARE (pg, 209 "iterate_aml_decisions_inc", 210 "SELECT" 211 " lo.outcome_serial_id" 212 ",lo.h_payto" 213 ",ah.justification" 214 ",lo.decision_time" 215 ",lo.expiration_time" 216 ",lo.jproperties::TEXT" 217 ",lo.to_investigate" 218 ",lo.is_active" 219 ",lo.jnew_rules::TEXT" 220 ",kt.is_wallet" 221 ",wt.payto_uri" 222 " FROM legitimization_outcomes lo" 223 " JOIN kyc_targets kt" 224 " ON (lo.h_payto = kt.h_normalized_payto)" 225 " JOIN wire_targets wt" 226 " ON (lo.h_payto = wt.h_normalized_payto)" 227 " LEFT JOIN aml_history ah" 228 " USING (outcome_serial_id)" 229 " WHERE (outcome_serial_id > $7)" 230 " AND ($1 OR (lo.h_payto = $2))" 231 " AND ($3 OR (lo.to_investigate = $4))" 232 " AND ($5 OR (lo.is_active = $6))" 233 " ORDER BY lo.outcome_serial_id ASC" 234 " LIMIT $8"); 235 PREPARE (pg, 236 "iterate_aml_decisions_dec", 237 "SELECT" 238 " lo.outcome_serial_id" 239 ",lo.h_payto" 240 ",ah.justification" 241 ",lo.decision_time" 242 ",lo.expiration_time" 243 ",lo.jproperties::TEXT" 244 ",lo.to_investigate" 245 ",lo.is_active" 246 ",lo.jnew_rules::TEXT" 247 ",kt.is_wallet" 248 ",wt.payto_uri" 249 " FROM legitimization_outcomes lo" 250 " JOIN kyc_targets kt" 251 " ON (lo.h_payto = kt.h_normalized_payto)" 252 " JOIN wire_targets wt" 253 " ON (lo.h_payto = wt.h_normalized_payto)" 254 " LEFT JOIN aml_history ah" 255 " USING (outcome_serial_id)" 256 " WHERE lo.outcome_serial_id < $7" 257 " AND ($1 OR (lo.h_payto = $2))" 258 " AND ($3 OR (lo.to_investigate = $4))" 259 " AND ($5 OR (lo.is_active = $6))" 260 " ORDER BY lo.outcome_serial_id DESC" 261 " LIMIT $8"); 262 qs = GNUNET_PQ_eval_prepared_multi_select (pg->conn, 263 stmt, 264 params, 265 &handle_aml_result, 266 &ctx); 267 if (GNUNET_OK != ctx.status) 268 return GNUNET_DB_STATUS_HARD_ERROR; 269 return qs; 270 }