LCOV - code coverage report
Current view: top level - exchange - taler-exchange-httpd_management_wire_fees.c (source / functions) Hit Total Coverage
Test: GNU Taler exchange coverage report Lines: 42 54 77.8 %
Date: 2021-08-30 06:43:37 Functions: 2 2 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :   This file is part of TALER
       3             :   Copyright (C) 2020 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-exchange-httpd_management_wire_fees.c
      18             :  * @brief Handle request to add wire fee details
      19             :  * @author Christian Grothoff
      20             :  */
      21             : #include "platform.h"
      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_json_lib.h"
      28             : #include "taler_mhd_lib.h"
      29             : #include "taler_signatures.h"
      30             : #include "taler-exchange-httpd_management.h"
      31             : #include "taler-exchange-httpd_responses.h"
      32             : #include "taler-exchange-httpd_wire.h"
      33             : 
      34             : 
      35             : /**
      36             :  * Closure for the #add_fee transaction.
      37             :  */
      38             : struct AddFeeContext
      39             : {
      40             :   /**
      41             :    * Fee's signature affirming the #TALER_SIGNATURE_MASTER_WIRE_FEES operation.
      42             :    */
      43             :   struct TALER_MasterSignatureP master_sig;
      44             : 
      45             :   /**
      46             :    * Wire method this is about.
      47             :    */
      48             :   const char *wire_method;
      49             : 
      50             :   /**
      51             :    * Starting period.
      52             :    */
      53             :   struct GNUNET_TIME_Absolute start_time;
      54             : 
      55             :   /**
      56             :    * End of period.
      57             :    */
      58             :   struct GNUNET_TIME_Absolute end_time;
      59             : 
      60             :   /**
      61             :    * Wire fee amount.
      62             :    */
      63             :   struct TALER_Amount wire_fee;
      64             : 
      65             :   /**
      66             :    * Closing fee amount.
      67             :    */
      68             :   struct TALER_Amount closing_fee;
      69             : 
      70             : };
      71             : 
      72             : 
      73             : /**
      74             :  * Function implementing database transaction to add a fee.  Runs the
      75             :  * transaction logic; IF it returns a non-error code, the transaction logic
      76             :  * MUST NOT queue a MHD response.  IF it returns an hard error, the
      77             :  * transaction logic MUST queue a MHD response and set @a mhd_ret.  IF it
      78             :  * returns the soft error code, the function MAY be called again to retry and
      79             :  * MUST not queue a MHD response.
      80             :  *
      81             :  * @param cls closure with a `struct AddFeeContext`
      82             :  * @param connection MHD request which triggered the transaction
      83             :  * @param[out] mhd_ret set to MHD response status for @a connection,
      84             :  *             if transaction failed (!)
      85             :  * @return transaction status
      86             :  */
      87             : static enum GNUNET_DB_QueryStatus
      88           9 : add_fee (void *cls,
      89             :          struct MHD_Connection *connection,
      90             :          MHD_RESULT *mhd_ret)
      91             : {
      92           9 :   struct AddFeeContext *afc = cls;
      93             :   enum GNUNET_DB_QueryStatus qs;
      94             :   struct TALER_Amount wire_fee;
      95             :   struct TALER_Amount closing_fee;
      96             : 
      97           9 :   qs = TEH_plugin->lookup_wire_fee_by_time (
      98           9 :     TEH_plugin->cls,
      99             :     afc->wire_method,
     100             :     afc->start_time,
     101             :     afc->end_time,
     102             :     &wire_fee,
     103             :     &closing_fee);
     104           9 :   if (qs < 0)
     105             :   {
     106           0 :     if (GNUNET_DB_STATUS_SOFT_ERROR == qs)
     107           0 :       return qs;
     108           0 :     GNUNET_break (0);
     109           0 :     *mhd_ret = TALER_MHD_reply_with_error (connection,
     110             :                                            MHD_HTTP_INTERNAL_SERVER_ERROR,
     111             :                                            TALER_EC_GENERIC_DB_FETCH_FAILED,
     112             :                                            "lookup wire fee");
     113           0 :     return qs;
     114             :   }
     115           9 :   if (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS != qs)
     116             :   {
     117           2 :     if ( (GNUNET_OK ==
     118           4 :           TALER_amount_is_valid (&wire_fee)) &&
     119             :          (0 ==
     120           2 :           TALER_amount_cmp (&wire_fee,
     121           4 :                             &afc->wire_fee)) &&
     122             :          (0 ==
     123           2 :           TALER_amount_cmp (&closing_fee,
     124           2 :                             &afc->closing_fee)) )
     125             :     {
     126             :       /* this will trigger the 'success' response */
     127           1 :       return GNUNET_DB_STATUS_SUCCESS_NO_RESULTS;
     128             :     }
     129             :     else
     130             :     {
     131           1 :       *mhd_ret = TALER_MHD_reply_with_error (
     132             :         connection,
     133             :         MHD_HTTP_CONFLICT,
     134             :         TALER_EC_EXCHANGE_MANAGEMENT_WIRE_FEE_MISMATCH,
     135             :         NULL);
     136             :     }
     137           1 :     return GNUNET_DB_STATUS_HARD_ERROR;
     138             :   }
     139             : 
     140           7 :   qs = TEH_plugin->insert_wire_fee (
     141           7 :     TEH_plugin->cls,
     142             :     afc->wire_method,
     143             :     afc->start_time,
     144             :     afc->end_time,
     145           7 :     &afc->wire_fee,
     146           7 :     &afc->closing_fee,
     147           7 :     &afc->master_sig);
     148           7 :   if (qs < 0)
     149             :   {
     150           0 :     if (GNUNET_DB_STATUS_SOFT_ERROR == qs)
     151           0 :       return qs;
     152           0 :     GNUNET_break (0);
     153           0 :     *mhd_ret = TALER_MHD_reply_with_error (connection,
     154             :                                            MHD_HTTP_INTERNAL_SERVER_ERROR,
     155             :                                            TALER_EC_GENERIC_DB_STORE_FAILED,
     156             :                                            "insert fee");
     157           0 :     return qs;
     158             :   }
     159           7 :   return qs;
     160             : }
     161             : 
     162             : 
     163             : MHD_RESULT
     164          10 : TEH_handler_management_post_wire_fees (
     165             :   struct MHD_Connection *connection,
     166             :   const json_t *root)
     167             : {
     168             :   struct AddFeeContext afc;
     169             :   struct GNUNET_JSON_Specification spec[] = {
     170          10 :     GNUNET_JSON_spec_fixed_auto ("master_sig",
     171             :                                  &afc.master_sig),
     172          10 :     GNUNET_JSON_spec_string ("wire_method",
     173             :                              &afc.wire_method),
     174          10 :     TALER_JSON_spec_absolute_time ("fee_start",
     175             :                                    &afc.start_time),
     176          10 :     TALER_JSON_spec_absolute_time ("fee_end",
     177             :                                    &afc.end_time),
     178          10 :     TALER_JSON_spec_amount ("closing_fee",
     179             :                             TEH_currency,
     180             :                             &afc.closing_fee),
     181          10 :     TALER_JSON_spec_amount ("wire_fee",
     182             :                             TEH_currency,
     183             :                             &afc.wire_fee),
     184          10 :     GNUNET_JSON_spec_end ()
     185             :   };
     186             : 
     187             :   {
     188             :     enum GNUNET_GenericReturnValue res;
     189             : 
     190          10 :     res = TALER_MHD_parse_json_data (connection,
     191             :                                      root,
     192             :                                      spec);
     193          10 :     if (GNUNET_SYSERR == res)
     194           0 :       return MHD_NO; /* hard failure */
     195          10 :     if (GNUNET_NO == res)
     196           0 :       return MHD_YES; /* failure */
     197             :   }
     198             : 
     199          10 :   if (GNUNET_OK !=
     200          10 :       TALER_exchange_offline_wire_fee_verify (
     201             :         afc.wire_method,
     202             :         afc.start_time,
     203             :         afc.end_time,
     204             :         &afc.wire_fee,
     205             :         &afc.closing_fee,
     206             :         &TEH_master_public_key,
     207             :         &afc.master_sig))
     208             :   {
     209             :     /* signature invalid */
     210           1 :     GNUNET_break_op (0);
     211           1 :     return TALER_MHD_reply_with_error (
     212             :       connection,
     213             :       MHD_HTTP_FORBIDDEN,
     214             :       TALER_EC_EXCHANGE_MANAGEMENT_WIRE_FEE_SIGNATURE_INVALID,
     215             :       NULL);
     216             :   }
     217             : 
     218             :   {
     219             :     enum GNUNET_GenericReturnValue res;
     220             :     MHD_RESULT ret;
     221             : 
     222           9 :     res = TEH_DB_run_transaction (connection,
     223             :                                   "add wire fee",
     224             :                                   &ret,
     225             :                                   &add_fee,
     226             :                                   &afc);
     227           9 :     if (GNUNET_SYSERR == res)
     228           1 :       return ret;
     229             :   }
     230           8 :   TEH_wire_update_state ();
     231           8 :   return TALER_MHD_reply_static (
     232             :     connection,
     233             :     MHD_HTTP_NO_CONTENT,
     234             :     NULL,
     235             :     NULL,
     236             :     0);
     237             : }
     238             : 
     239             : 
     240             : /* end of taler-exchange-httpd_management_wire_fees.c */

Generated by: LCOV version 1.14