LCOV - code coverage report
Current view: top level - lib - exchange_api_post-management-wire-fee.c (source / functions) Coverage Total Hit
Test: coverage.info Lines: 62.5 % 88 55
Test Date: 2026-03-10 12:10:57 Functions: 100.0 % 4 4

            Line data    Source code
       1              : /*
       2              :   This file is part of TALER
       3              :   Copyright (C) 2020-2026 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
      15              :   <http://www.gnu.org/licenses/>
      16              : */
      17              : /**
      18              :  * @file lib/exchange_api_post-management-wire-fee.c
      19              :  * @brief functions to set wire fees at an exchange
      20              :  * @author Christian Grothoff
      21              :  */
      22              : #include "taler/platform.h"
      23              : #include "taler/taler_json_lib.h"
      24              : #include <gnunet/gnunet_curl_lib.h>
      25              : #include <microhttpd.h>
      26              : #include "taler/taler_exchange_service.h"
      27              : #include "taler/taler-exchange/post-management-wire-fee.h"
      28              : #include "exchange_api_curl_defaults.h"
      29              : #include "taler/taler_signatures.h"
      30              : #include "taler/taler_curl_lib.h"
      31              : 
      32              : 
      33              : struct TALER_EXCHANGE_PostManagementWireFeesHandle
      34              : {
      35              : 
      36              :   /**
      37              :    * The base URL for this request.
      38              :    */
      39              :   char *base_url;
      40              : 
      41              :   /**
      42              :    * The full URL for this request, set during _start.
      43              :    */
      44              :   char *url;
      45              : 
      46              :   /**
      47              :    * Minor context that holds body and headers.
      48              :    */
      49              :   struct TALER_CURL_PostContext post_ctx;
      50              : 
      51              :   /**
      52              :    * Handle for the request.
      53              :    */
      54              :   struct GNUNET_CURL_Job *job;
      55              : 
      56              :   /**
      57              :    * Function to call with the result.
      58              :    */
      59              :   TALER_EXCHANGE_PostManagementWireFeesCallback cb;
      60              : 
      61              :   /**
      62              :    * Closure for @a cb.
      63              :    */
      64              :   TALER_EXCHANGE_POST_MANAGEMENT_WIRE_FEES_RESULT_CLOSURE *cb_cls;
      65              : 
      66              :   /**
      67              :    * Reference to the execution context.
      68              :    */
      69              :   struct GNUNET_CURL_Context *ctx;
      70              : 
      71              :   /**
      72              :    * Wire method these fees are for.
      73              :    */
      74              :   char *wire_method;
      75              : 
      76              :   /**
      77              :    * Start of validity period.
      78              :    */
      79              :   struct GNUNET_TIME_Timestamp validity_start;
      80              : 
      81              :   /**
      82              :    * End of validity period.
      83              :    */
      84              :   struct GNUNET_TIME_Timestamp validity_end;
      85              : 
      86              :   /**
      87              :    * Wire fees for this time period.
      88              :    */
      89              :   struct TALER_WireFeeSet fees;
      90              : 
      91              :   /**
      92              :    * Signature affirming the wire fees.
      93              :    */
      94              :   struct TALER_MasterSignatureP master_sig;
      95              : 
      96              : };
      97              : 
      98              : 
      99              : /**
     100              :  * Function called when we're done processing the
     101              :  * HTTP POST /management/wire-fees request.
     102              :  *
     103              :  * @param cls the `struct TALER_EXCHANGE_PostManagementWireFeesHandle`
     104              :  * @param response_code HTTP response code, 0 on error
     105              :  * @param response response body, NULL if not in JSON
     106              :  */
     107              : static void
     108           44 : handle_wire_fees_finished (void *cls,
     109              :                            long response_code,
     110              :                            const void *response)
     111              : {
     112           44 :   struct TALER_EXCHANGE_PostManagementWireFeesHandle *pmwfh = cls;
     113           44 :   const json_t *json = response;
     114           44 :   struct TALER_EXCHANGE_PostManagementWireFeesResponse res = {
     115           44 :     .hr.http_status = (unsigned int) response_code,
     116              :     .hr.reply = json
     117              :   };
     118              : 
     119           44 :   pmwfh->job = NULL;
     120           44 :   switch (response_code)
     121              :   {
     122           40 :   case MHD_HTTP_NO_CONTENT:
     123           40 :     break;
     124            2 :   case MHD_HTTP_FORBIDDEN:
     125            2 :     res.hr.ec = TALER_JSON_get_error_code (json);
     126            2 :     res.hr.hint = TALER_JSON_get_error_hint (json);
     127            2 :     break;
     128            0 :   case MHD_HTTP_NOT_FOUND:
     129            0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     130              :                 "Server did not find handler at `%s'. Did you configure the correct exchange base URL?\n",
     131              :                 pmwfh->url);
     132            0 :     if (NULL != json)
     133              :     {
     134            0 :       res.hr.ec = TALER_JSON_get_error_code (json);
     135            0 :       res.hr.hint = TALER_JSON_get_error_hint (json);
     136              :     }
     137              :     else
     138              :     {
     139            0 :       res.hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE;
     140            0 :       res.hr.hint = TALER_ErrorCode_get_hint (res.hr.ec);
     141              :     }
     142            0 :     break;
     143            2 :   case MHD_HTTP_CONFLICT:
     144            2 :     res.hr.ec = TALER_JSON_get_error_code (json);
     145            2 :     res.hr.hint = TALER_JSON_get_error_hint (json);
     146            2 :     break;
     147            0 :   case MHD_HTTP_PRECONDITION_FAILED:
     148            0 :     res.hr.ec = TALER_JSON_get_error_code (json);
     149            0 :     res.hr.hint = TALER_JSON_get_error_hint (json);
     150            0 :     break;
     151            0 :   default:
     152              :     /* unexpected response code */
     153            0 :     GNUNET_break_op (0);
     154            0 :     res.hr.ec = TALER_JSON_get_error_code (json);
     155            0 :     res.hr.hint = TALER_JSON_get_error_hint (json);
     156            0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     157              :                 "Unexpected response code %u/%d for exchange management set wire fee\n",
     158              :                 (unsigned int) response_code,
     159              :                 (int) res.hr.ec);
     160            0 :     break;
     161              :   }
     162           44 :   if (NULL != pmwfh->cb)
     163              :   {
     164           44 :     pmwfh->cb (pmwfh->cb_cls,
     165              :                &res);
     166           44 :     pmwfh->cb = NULL;
     167              :   }
     168           44 :   TALER_EXCHANGE_post_management_wire_fees_cancel (pmwfh);
     169           44 : }
     170              : 
     171              : 
     172              : struct TALER_EXCHANGE_PostManagementWireFeesHandle *
     173           44 : TALER_EXCHANGE_post_management_wire_fees_create (
     174              :   struct GNUNET_CURL_Context *ctx,
     175              :   const char *exchange_base_url,
     176              :   const char *wire_method,
     177              :   struct GNUNET_TIME_Timestamp validity_start,
     178              :   struct GNUNET_TIME_Timestamp validity_end,
     179              :   const struct TALER_WireFeeSet *fees,
     180              :   const struct TALER_MasterSignatureP *master_sig)
     181              : {
     182              :   struct TALER_EXCHANGE_PostManagementWireFeesHandle *pmwfh;
     183              : 
     184           44 :   pmwfh = GNUNET_new (struct TALER_EXCHANGE_PostManagementWireFeesHandle);
     185           44 :   pmwfh->ctx = ctx;
     186           44 :   pmwfh->base_url = GNUNET_strdup (exchange_base_url);
     187           44 :   pmwfh->wire_method = GNUNET_strdup (wire_method);
     188           44 :   pmwfh->validity_start = validity_start;
     189           44 :   pmwfh->validity_end = validity_end;
     190           44 :   pmwfh->fees = *fees;
     191           44 :   pmwfh->master_sig = *master_sig;
     192           44 :   return pmwfh;
     193              : }
     194              : 
     195              : 
     196              : enum TALER_ErrorCode
     197           44 : TALER_EXCHANGE_post_management_wire_fees_start (
     198              :   struct TALER_EXCHANGE_PostManagementWireFeesHandle *pmwfh,
     199              :   TALER_EXCHANGE_PostManagementWireFeesCallback cb,
     200              :   TALER_EXCHANGE_POST_MANAGEMENT_WIRE_FEES_RESULT_CLOSURE *cb_cls)
     201              : {
     202              :   CURL *eh;
     203              :   json_t *body;
     204              : 
     205           44 :   pmwfh->cb = cb;
     206           44 :   pmwfh->cb_cls = cb_cls;
     207           44 :   pmwfh->url = TALER_url_join (pmwfh->base_url,
     208              :                                "management/wire-fee",
     209              :                                NULL);
     210           44 :   if (NULL == pmwfh->url)
     211              :   {
     212            0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     213              :                 "Could not construct request URL.\n");
     214            0 :     return TALER_EC_GENERIC_CONFIGURATION_INVALID;
     215              :   }
     216           44 :   body = GNUNET_JSON_PACK (
     217              :     GNUNET_JSON_pack_string ("wire_method",
     218              :                              pmwfh->wire_method),
     219              :     GNUNET_JSON_pack_data_auto ("master_sig",
     220              :                                 &pmwfh->master_sig),
     221              :     GNUNET_JSON_pack_timestamp ("fee_start",
     222              :                                 pmwfh->validity_start),
     223              :     GNUNET_JSON_pack_timestamp ("fee_end",
     224              :                                 pmwfh->validity_end),
     225              :     TALER_JSON_pack_amount ("closing_fee",
     226              :                             &pmwfh->fees.closing),
     227              :     TALER_JSON_pack_amount ("wire_fee",
     228              :                             &pmwfh->fees.wire));
     229           44 :   eh = TALER_EXCHANGE_curl_easy_get_ (pmwfh->url);
     230           88 :   if ( (NULL == eh) ||
     231              :        (GNUNET_OK !=
     232           44 :         TALER_curl_easy_post (&pmwfh->post_ctx,
     233              :                               eh,
     234              :                               body)) )
     235              :   {
     236            0 :     GNUNET_break (0);
     237            0 :     if (NULL != eh)
     238            0 :       curl_easy_cleanup (eh);
     239            0 :     json_decref (body);
     240            0 :     GNUNET_free (pmwfh->url);
     241            0 :     pmwfh->url = NULL;
     242            0 :     return TALER_EC_GENERIC_INTERNAL_INVARIANT_FAILURE;
     243              :   }
     244           44 :   json_decref (body);
     245           44 :   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
     246              :               "Requesting URL '%s'\n",
     247              :               pmwfh->url);
     248           88 :   pmwfh->job = GNUNET_CURL_job_add2 (pmwfh->ctx,
     249              :                                      eh,
     250           44 :                                      pmwfh->post_ctx.headers,
     251              :                                      &handle_wire_fees_finished,
     252              :                                      pmwfh);
     253           44 :   if (NULL == pmwfh->job)
     254              :   {
     255            0 :     TALER_curl_easy_post_finished (&pmwfh->post_ctx);
     256            0 :     GNUNET_free (pmwfh->url);
     257            0 :     pmwfh->url = NULL;
     258            0 :     return TALER_EC_GENERIC_INTERNAL_INVARIANT_FAILURE;
     259              :   }
     260           44 :   return TALER_EC_NONE;
     261              : }
     262              : 
     263              : 
     264              : void
     265           44 : TALER_EXCHANGE_post_management_wire_fees_cancel (
     266              :   struct TALER_EXCHANGE_PostManagementWireFeesHandle *pmwfh)
     267              : {
     268           44 :   if (NULL != pmwfh->job)
     269              :   {
     270            0 :     GNUNET_CURL_job_cancel (pmwfh->job);
     271            0 :     pmwfh->job = NULL;
     272              :   }
     273           44 :   TALER_curl_easy_post_finished (&pmwfh->post_ctx);
     274           44 :   GNUNET_free (pmwfh->wire_method);
     275           44 :   GNUNET_free (pmwfh->url);
     276           44 :   GNUNET_free (pmwfh->base_url);
     277           44 :   GNUNET_free (pmwfh);
     278           44 : }
     279              : 
     280              : 
     281              : /* end of exchange_api_post-management-wire-fee.c */
        

Generated by: LCOV version 2.0-1