exchange

Base system with REST service to issue digital coins, run by the payment service provider
Log | Files | Refs | Submodules | README | LICENSE

select_refunds_above_serial_id.c (7457B)


      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_f >= amount_with_fee.fraction);
    141     }
    142     ret = rsc->cb (rsc->cb_cls,
    143                    rowid,
    144                    &denom_pub,
    145                    &refund.coin.coin_pub,
    146                    &refund.details.merchant_pub,
    147                    &refund.details.merchant_sig,
    148                    &refund.details.h_contract_terms,
    149                    refund.details.rtransaction_id,
    150                    full_refund,
    151                    &refund.details.refund_amount);
    152     GNUNET_PQ_cleanup_result (rs);
    153     if (GNUNET_OK != ret)
    154       break;
    155   }
    156 }
    157 
    158 
    159 enum GNUNET_DB_QueryStatus
    160 TALER_EXCHANGEDB_select_refunds_above_serial_id (
    161   struct TALER_EXCHANGEDB_PostgresContext *pg,
    162   uint64_t serial_id,
    163   TALER_EXCHANGEDB_RefundCallback cb,
    164   void *cb_cls)
    165 {
    166   struct GNUNET_PQ_QueryParam params[] = {
    167     GNUNET_PQ_query_param_uint64 (&serial_id),
    168     GNUNET_PQ_query_param_end
    169   };
    170   struct RefundsSerialContext rsc = {
    171     .cb = cb,
    172     .cb_cls = cb_cls,
    173     .pg = pg,
    174     .status = GNUNET_OK
    175   };
    176   enum GNUNET_DB_QueryStatus qs;
    177 
    178   /* Fetch refunds with rowid '\geq' the given parameter */
    179   PREPARE (pg,
    180            "audit_get_refunds_incr",
    181            "SELECT"
    182            " bdep.merchant_pub"
    183            ",ref.merchant_sig"
    184            ",bdep.h_contract_terms"
    185            ",ref.rtransaction_id"
    186            ",denom.denom_pub"
    187            ",kc.coin_pub"
    188            ",ref.amount_with_fee"
    189            ",ref.refund_serial_id"
    190            " FROM refunds ref"
    191            "   JOIN batch_deposits bdep"
    192            "     ON (ref.batch_deposit_serial_id=bdep.batch_deposit_serial_id)"
    193            "   JOIN coin_deposits cdep"
    194            "     ON (ref.coin_pub=cdep.coin_pub AND ref.batch_deposit_serial_id=cdep.batch_deposit_serial_id)"
    195            "   JOIN known_coins kc"
    196            "     ON (cdep.coin_pub=kc.coin_pub)"
    197            "   JOIN denominations denom"
    198            "     ON (kc.denominations_serial=denom.denominations_serial)"
    199            " WHERE ref.refund_serial_id>=$1"
    200            " ORDER BY ref.refund_serial_id ASC;");
    201   PREPARE (pg,
    202            "test_refund_full",
    203            "SELECT"
    204            " CAST(SUM(CAST((ref.amount_with_fee).frac AS INT8)) AS INT8) AS s_f"
    205            ",CAST(SUM((ref.amount_with_fee).val) AS INT8) AS s_v"
    206            ",cdep.amount_with_fee"
    207            " FROM refunds ref"
    208            "   JOIN coin_deposits cdep"
    209            "     ON (ref.coin_pub=cdep.coin_pub AND ref.batch_deposit_serial_id=cdep.batch_deposit_serial_id)"
    210            " WHERE ref.refund_serial_id=$1"
    211            " GROUP BY (cdep.amount_with_fee);");
    212   qs = GNUNET_PQ_eval_prepared_multi_select (pg->conn,
    213                                              "audit_get_refunds_incr",
    214                                              params,
    215                                              &refunds_serial_helper_cb,
    216                                              &rsc);
    217   if (GNUNET_OK != rsc.status)
    218     return GNUNET_DB_STATUS_HARD_ERROR;
    219   return qs;
    220 }