exchange

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

iterate_recoup_refreshes_above_serial_id.c (6667B)


      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/iterate_recoup_refreshes_above_serial_id.c
     18  * @brief Implementation of the iterate_recoup_refreshes_above_serial_id function for Postgres
     19  * @author Christian Grothoff
     20  */
     21 #include "taler/taler_pq_lib.h"
     22 #include "exchange-database/iterate_recoup_refreshes_above_serial_id.h"
     23 #include "helper.h"
     24 
     25 
     26 /**
     27  * Closure for #recoup_refresh_serial_helper_cb().
     28  */
     29 struct RecoupRefreshSerialContext
     30 {
     31 
     32   /**
     33    * Callback to call.
     34    */
     35   TALER_EXCHANGEDB_RecoupRefreshCallback 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 RecoupRefreshSerialContext`
     59  * @param result the postgres result
     60  * @param num_results the number of results in @a result
     61  */
     62 static void
     63 recoup_refresh_serial_helper_cb (void *cls,
     64                                  PGresult *result,
     65                                  unsigned int num_results)
     66 {
     67   struct RecoupRefreshSerialContext *psc = cls;
     68   struct TALER_EXCHANGEDB_PostgresContext *pg = psc->pg;
     69 
     70   for (unsigned int i = 0; i<num_results; i++)
     71   {
     72     uint64_t rowid;
     73     struct TALER_CoinSpendPublicKeyP old_coin_pub;
     74     struct TALER_CoinPublicInfo coin;
     75     struct TALER_CoinSpendSignatureP coin_sig;
     76     union GNUNET_CRYPTO_BlindingSecretP coin_blind;
     77     struct TALER_DenominationPublicKey denom_pub;
     78     struct TALER_DenominationHashP old_denom_pub_hash;
     79     struct TALER_Amount amount;
     80     struct GNUNET_TIME_Timestamp timestamp;
     81     struct GNUNET_PQ_ResultSpec rs[] = {
     82       GNUNET_PQ_result_spec_uint64 ("recoup_refresh_uuid",
     83                                     &rowid),
     84       GNUNET_PQ_result_spec_timestamp ("recoup_timestamp",
     85                                        &timestamp),
     86       GNUNET_PQ_result_spec_auto_from_type ("old_coin_pub",
     87                                             &old_coin_pub),
     88       GNUNET_PQ_result_spec_auto_from_type ("old_denom_pub_hash",
     89                                             &old_denom_pub_hash),
     90       GNUNET_PQ_result_spec_auto_from_type ("coin_pub",
     91                                             &coin.coin_pub),
     92       GNUNET_PQ_result_spec_auto_from_type ("coin_sig",
     93                                             &coin_sig),
     94       GNUNET_PQ_result_spec_auto_from_type ("coin_blind",
     95                                             &coin_blind),
     96       TALER_PQ_result_spec_denom_pub ("denom_pub",
     97                                       &denom_pub),
     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     enum GNUNET_GenericReturnValue 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                    &old_coin_pub,
    126                    &old_denom_pub_hash,
    127                    &coin,
    128                    &denom_pub,
    129                    &coin_sig,
    130                    &coin_blind);
    131     GNUNET_PQ_cleanup_result (rs);
    132     if (GNUNET_OK != ret)
    133       break;
    134   }
    135 }
    136 
    137 
    138 enum GNUNET_DB_QueryStatus
    139 TALER_EXCHANGEDB_iterate_recoup_refreshes_above_serial_id (
    140   struct TALER_EXCHANGEDB_PostgresContext *pg,
    141   uint64_t serial_id,
    142   TALER_EXCHANGEDB_RecoupRefreshCallback cb,
    143   void *cb_cls)
    144 {
    145   struct GNUNET_PQ_QueryParam params[] = {
    146     GNUNET_PQ_query_param_uint64 (&serial_id),
    147     GNUNET_PQ_query_param_end
    148   };
    149   struct RecoupRefreshSerialContext 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            "iterate_recoup_refreshes_above_serial_id",
    159            "SELECT"
    160            " rr.recoup_refresh_uuid"
    161            ",rr.recoup_timestamp"
    162            ",rr.coin_sig"
    163            ",rr.coin_blind"
    164            ",rr.amount"
    165            ",new_coins.age_commitment_hash"
    166            ",new_coins.coin_pub AS coin_pub"
    167            ",new_denoms.denom_pub AS denom_pub"
    168            ",new_denoms.denom_pub_hash"
    169            ",new_coins.denom_sig AS denom_sig"
    170            ",old_coins.coin_pub AS old_coin_pub"
    171            ",old_denoms.denom_pub_hash AS old_denom_pub_hash"
    172            " FROM recoup_refresh rr"
    173            " INNER JOIN refresh r"
    174            "   ON (r.refresh_id = rr.refresh_id)"
    175            " INNER JOIN known_coins old_coins"
    176            "   ON (r.old_coin_pub = old_coins.coin_pub)"
    177            " INNER JOIN known_coins new_coins"
    178            "   ON (rr.coin_pub = new_coins.coin_pub)"
    179            " INNER JOIN denominations new_denoms"
    180            "   ON (new_coins.denominations_serial = new_denoms.denominations_serial)"
    181            " INNER JOIN denominations old_denoms"
    182            "   ON (old_coins.denominations_serial = old_denoms.denominations_serial)"
    183            " WHERE rr.recoup_refresh_uuid>=$1"
    184            " ORDER BY rr.recoup_refresh_uuid ASC;");
    185   qs = GNUNET_PQ_eval_prepared_multi_select (
    186     pg->conn,
    187     "iterate_recoup_refreshes_above_serial_id",
    188     params,
    189     &recoup_refresh_serial_helper_cb,
    190     &psc);
    191   if (GNUNET_OK != psc.status)
    192     return GNUNET_DB_STATUS_HARD_ERROR;
    193   return qs;
    194 }