taler-auditor-httpd_get-monitoring-deposit-confirmations.c (6400B)
1 /* 2 This file is part of TALER 3 Copyright (C) 2014-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 Affero 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 Affero General Public License for more details. 12 13 You should have received a copy of the GNU Affero General Public License along with 14 TALER; see the file COPYING. If not, see <http://www.gnu.org/licenses/> 15 */ 16 /** 17 * @file taler-auditor-httpd_get-monitoring-deposit-confirmations.c 18 * @brief Handle /deposit-confirmation requests; return list of deposit confirmations from merchant 19 * that were not received from the exchange, by auditor. 20 * @author Nic Eigel 21 */ 22 #include <gnunet/gnunet_util_lib.h> 23 #include <gnunet/gnunet_json_lib.h> 24 #include <jansson.h> 25 #include <microhttpd.h> 26 #include <pthread.h> 27 #include "taler/taler_json_lib.h" 28 #include "taler/taler_mhd_lib.h" 29 #include "taler-auditor-httpd.h" 30 #include "taler-auditor-httpd_get-monitoring-deposit-confirmations.h" 31 #include "auditor-database/get_deposit_confirmations.h" 32 #include "auditor-database/preflight.h" 33 34 35 /** 36 * Add deposit confirmation to the list. 37 * 38 * @param[in,out] cls a `json_t *` array to extend 39 * @param dc struct of deposit confirmation 40 * @return #GNUNET_OK to continue to iterate, #GNUNET_SYSERR to stop iterating 41 */ 42 static enum GNUNET_GenericReturnValue 43 add_deposit_confirmation ( 44 void *cls, 45 const struct TALER_AUDITORDB_DepositConfirmation *dc) 46 { 47 json_t *list = cls; 48 json_t *obj; 49 json_t *coin_pubs_json = json_array (); 50 json_t *coin_sigs_json = json_array (); 51 52 GNUNET_assert (NULL != coin_pubs_json); 53 GNUNET_assert (NULL != coin_sigs_json); 54 for (unsigned int i = 0; i < dc->num_coins; i++) 55 { 56 json_t *pub; 57 json_t *sig; 58 59 pub = GNUNET_JSON_from_data_auto (&dc->coin_pubs[i]); 60 GNUNET_assert (0 == 61 json_array_append_new (coin_pubs_json, 62 pub)); 63 sig = GNUNET_JSON_from_data_auto (&dc->coin_sigs[i]); 64 GNUNET_assert (0 == 65 json_array_append_new (coin_sigs_json, 66 sig)); 67 } 68 69 obj = GNUNET_JSON_PACK ( 70 GNUNET_JSON_pack_int64 ("deposit_confirmation_serial_id", 71 dc->row_id), 72 GNUNET_JSON_pack_data_auto ("h_contract_terms", 73 &dc->h_contract_terms), 74 GNUNET_JSON_pack_data_auto ("h_policy", 75 &dc->h_policy), 76 GNUNET_JSON_pack_data_auto ("h_wire", 77 &dc->h_wire), 78 GNUNET_JSON_pack_timestamp ("exchange_timestamp", 79 dc->exchange_timestamp), 80 GNUNET_JSON_pack_timestamp ("refund_deadline", 81 dc->refund_deadline), 82 GNUNET_JSON_pack_timestamp ("wire_deadline", 83 dc->wire_deadline), 84 TALER_JSON_pack_amount ("total_without_fee", 85 &dc->total_without_fee), 86 GNUNET_JSON_pack_array_steal ("coin_pubs", 87 coin_pubs_json), 88 GNUNET_JSON_pack_array_steal ("coin_sigs", 89 coin_sigs_json), 90 GNUNET_JSON_pack_data_auto ("merchant_pub", 91 &dc->merchant), 92 GNUNET_JSON_pack_data_auto ("exchange_sig", 93 &dc->exchange_sig), 94 GNUNET_JSON_pack_data_auto ("exchange_pub", 95 &dc->exchange_pub), 96 GNUNET_JSON_pack_data_auto ("master_sig", 97 &dc->master_sig), 98 GNUNET_JSON_pack_bool ("suppressed", 99 dc->suppressed) 100 ); 101 GNUNET_break (0 == 102 json_array_append_new (list, 103 obj)); 104 return GNUNET_OK; 105 } 106 107 108 MHD_RESULT 109 TAH_get_monitoring_deposit_confirmations ( 110 struct TAH_RequestHandler *rh, 111 struct MHD_Connection *connection, 112 void **connection_cls, 113 const char *upload_data, 114 size_t *upload_data_size, 115 const char *const args[]) 116 { 117 json_t *ja; 118 enum GNUNET_DB_QueryStatus qs; 119 bool return_suppressed = false; 120 int64_t limit = -20; 121 uint64_t offset; 122 123 (void) rh; 124 (void) connection_cls; 125 (void) upload_data; 126 (void) upload_data_size; 127 if (GNUNET_SYSERR == 128 TALER_AUDITORDB_preflight (TAH_apg)) 129 { 130 GNUNET_break (0); 131 return TALER_MHD_reply_with_error (connection, 132 MHD_HTTP_INTERNAL_SERVER_ERROR, 133 TALER_EC_GENERIC_DB_SETUP_FAILED, 134 NULL); 135 } 136 TALER_MHD_parse_request_snumber (connection, 137 "limit", 138 &limit); 139 if (limit < 0) 140 offset = INT64_MAX; 141 else 142 offset = 0; 143 TALER_MHD_parse_request_number (connection, 144 "offset", 145 &offset); 146 { 147 const char *ret_s = MHD_lookup_connection_value (connection, 148 MHD_GET_ARGUMENT_KIND, 149 "return_suppressed"); 150 if ( (NULL != ret_s) && 151 (0 == strcmp (ret_s, "true")) ) 152 { 153 return_suppressed = true; 154 } 155 } 156 157 ja = json_array (); 158 GNUNET_break (NULL != ja); 159 qs = TALER_AUDITORDB_get_deposit_confirmations ( 160 TAH_apg, 161 limit, 162 offset, 163 return_suppressed, 164 &add_deposit_confirmation, 165 ja); 166 if (0 > qs) 167 { 168 GNUNET_break (GNUNET_DB_STATUS_HARD_ERROR == qs); 169 json_decref (ja); 170 TALER_LOG_WARNING ( 171 "Failed to handle GET /deposit-confirmation in database\n"); 172 return TALER_MHD_reply_with_error (connection, 173 MHD_HTTP_INTERNAL_SERVER_ERROR, 174 TALER_EC_GENERIC_DB_FETCH_FAILED, 175 "deposit-confirmation"); 176 } 177 return TALER_MHD_REPLY_JSON_PACK ( 178 connection, 179 MHD_HTTP_OK, 180 GNUNET_JSON_pack_array_steal ("deposit_confirmation", 181 ja)); 182 } 183 184 185 /* end of taler-auditor-httpd_deposit-confirmation-get.c */