exchange

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

pg_select_recoup_above_serial_id.c (5948B)


      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/pg_select_recoup_above_serial_id.c
     18  * @brief Implementation of the select_recoup_above_serial_id function for Postgres
     19  * @author Christian Grothoff
     20  */
     21 #include "taler/platform.h"
     22 #include "taler/taler_error_codes.h"
     23 #include "taler/taler_dbevents.h"
     24 #include "taler/taler_pq_lib.h"
     25 #include "pg_select_recoup_above_serial_id.h"
     26 #include "pg_helper.h"
     27 
     28 
     29 /**
     30  * Closure for #recoup_serial_helper_cb().
     31  */
     32 struct RecoupSerialContext
     33 {
     34 
     35   /**
     36    * Callback to call.
     37    */
     38   TALER_EXCHANGEDB_RecoupCallback cb;
     39 
     40   /**
     41    * Closure for @e cb.
     42    */
     43   void *cb_cls;
     44 
     45   /**
     46    * Plugin context.
     47    */
     48   struct PostgresClosure *pg;
     49 
     50   /**
     51    * Status code, set to #GNUNET_SYSERR on hard errors.
     52    */
     53   enum GNUNET_GenericReturnValue status;
     54 };
     55 
     56 
     57 /**
     58  * Helper function to be called with the results of a SELECT statement
     59  * that has returned @a num_results results.
     60  *
     61  * @param cls closure of type `struct RecoupSerialContext`
     62  * @param result the postgres result
     63  * @param num_results the number of results in @a result
     64  */
     65 static void
     66 recoup_serial_helper_cb (void *cls,
     67                          PGresult *result,
     68                          unsigned int num_results)
     69 {
     70   struct RecoupSerialContext *psc = cls;
     71   struct PostgresClosure *pg = psc->pg;
     72 
     73   for (unsigned int i = 0; i<num_results; i++)
     74   {
     75     uint64_t rowid;
     76     struct TALER_ReservePublicKeyP reserve_pub;
     77     struct TALER_CoinPublicInfo coin;
     78     struct TALER_CoinSpendSignatureP coin_sig;
     79     union GNUNET_CRYPTO_BlindingSecretP coin_blind;
     80     struct TALER_Amount amount;
     81     struct TALER_DenominationPublicKey denom_pub;
     82     struct GNUNET_TIME_Timestamp timestamp;
     83     struct GNUNET_PQ_ResultSpec rs[] = {
     84       GNUNET_PQ_result_spec_uint64 ("recoup_uuid",
     85                                     &rowid),
     86       GNUNET_PQ_result_spec_timestamp ("recoup_timestamp",
     87                                        &timestamp),
     88       GNUNET_PQ_result_spec_auto_from_type ("reserve_pub",
     89                                             &reserve_pub),
     90       GNUNET_PQ_result_spec_auto_from_type ("coin_pub",
     91                                             &coin.coin_pub),
     92       TALER_PQ_result_spec_denom_pub ("denom_pub",
     93                                       &denom_pub),
     94       GNUNET_PQ_result_spec_auto_from_type ("coin_sig",
     95                                             &coin_sig),
     96       GNUNET_PQ_result_spec_auto_from_type ("coin_blind",
     97                                             &coin_blind),
     98       GNUNET_PQ_result_spec_auto_from_type ("denom_pub_hash",
     99                                             &coin.denom_pub_hash),
    100       GNUNET_PQ_result_spec_allow_null (
    101         GNUNET_PQ_result_spec_auto_from_type ("age_commitment_hash",
    102                                               &coin.h_age_commitment),
    103         &coin.no_age_commitment),
    104       TALER_PQ_result_spec_denom_sig ("denom_sig",
    105                                       &coin.denom_sig),
    106       TALER_PQ_RESULT_SPEC_AMOUNT ("amount",
    107                                    &amount),
    108       GNUNET_PQ_result_spec_end
    109     };
    110     int ret;
    111 
    112     if (GNUNET_OK !=
    113         GNUNET_PQ_extract_result (result,
    114                                   rs,
    115                                   i))
    116     {
    117       GNUNET_break (0);
    118       psc->status = GNUNET_SYSERR;
    119       return;
    120     }
    121     ret = psc->cb (psc->cb_cls,
    122                    rowid,
    123                    timestamp,
    124                    &amount,
    125                    &reserve_pub,
    126                    &coin,
    127                    &denom_pub,
    128                    &coin_sig,
    129                    &coin_blind);
    130     GNUNET_PQ_cleanup_result (rs);
    131     if (GNUNET_OK != ret)
    132       break;
    133   }
    134 }
    135 
    136 
    137 enum GNUNET_DB_QueryStatus
    138 TEH_PG_select_recoup_above_serial_id (
    139   void *cls,
    140   uint64_t serial_id,
    141   TALER_EXCHANGEDB_RecoupCallback cb,
    142   void *cb_cls)
    143 {
    144   struct PostgresClosure *pg = cls;
    145   struct GNUNET_PQ_QueryParam params[] = {
    146     GNUNET_PQ_query_param_uint64 (&serial_id),
    147     GNUNET_PQ_query_param_end
    148   };
    149   struct RecoupSerialContext psc = {
    150     .cb = cb,
    151     .cb_cls = cb_cls,
    152     .pg = pg,
    153     .status = GNUNET_OK
    154   };
    155   enum GNUNET_DB_QueryStatus qs;
    156 
    157   PREPARE (pg,
    158            "recoup_get_incr",
    159            "SELECT"
    160            " recoup_uuid"
    161            ",recoup_timestamp"
    162            ",withdraw.reserve_pub"
    163            ",coins.coin_pub"
    164            ",coin_sig"
    165            ",coin_blind"
    166            ",denoms.denom_pub_hash"
    167            ",coins.denom_sig"
    168            ",coins.age_commitment_hash"
    169            ",denoms.denom_pub"
    170            ",amount"
    171            " FROM recoup"
    172            "    JOIN known_coins coins"
    173            "      USING (coin_pub)"
    174            "    JOIN withdraw"
    175            "      USING (withdraw_id)"
    176            "    JOIN denominations denoms"
    177            "      ON (coins.denominations_serial = denoms.denominations_serial)"
    178            " WHERE recoup_uuid>=$1"
    179            " ORDER BY recoup_uuid ASC;");
    180   qs = GNUNET_PQ_eval_prepared_multi_select (pg->conn,
    181                                              "recoup_get_incr",
    182                                              params,
    183                                              &recoup_serial_helper_cb,
    184                                              &psc);
    185   if (GNUNET_OK != psc.status)
    186     return GNUNET_DB_STATUS_HARD_ERROR;
    187   return qs;
    188 }