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_refresh_above_serial_id.c (7384B)


      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_refresh_above_serial_id.c
     18  * @brief Implementation of the select_recoup_refresh_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_refresh_above_serial_id.h"
     26 #include "pg_helper.h"
     27 
     28 
     29 /**
     30  * Closure for #recoup_refresh_serial_helper_cb().
     31  */
     32 struct RecoupRefreshSerialContext
     33 {
     34 
     35   /**
     36    * Callback to call.
     37    */
     38   TALER_EXCHANGEDB_RecoupRefreshCallback 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 RecoupRefreshSerialContext`
     62  * @param result the postgres result
     63  * @param num_results the number of results in @a result
     64  */
     65 static void
     66 recoup_refresh_serial_helper_cb (void *cls,
     67                                  PGresult *result,
     68                                  unsigned int num_results)
     69 {
     70   struct RecoupRefreshSerialContext *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_CoinSpendPublicKeyP old_coin_pub;
     77     struct TALER_CoinPublicInfo coin;
     78     struct TALER_CoinSpendSignatureP coin_sig;
     79     union GNUNET_CRYPTO_BlindingSecretP coin_blind;
     80     struct TALER_DenominationPublicKey denom_pub;
     81     struct TALER_DenominationHashP old_denom_pub_hash;
     82     struct TALER_Amount amount;
     83     struct TALER_BlindedCoinHashP h_blind_ev;
     84     struct GNUNET_TIME_Timestamp timestamp;
     85     struct GNUNET_PQ_ResultSpec rs[] = {
     86       GNUNET_PQ_result_spec_uint64 ("recoup_refresh_uuid",
     87                                     &rowid),
     88       GNUNET_PQ_result_spec_timestamp ("recoup_timestamp",
     89                                        &timestamp),
     90       GNUNET_PQ_result_spec_auto_from_type ("old_coin_pub",
     91                                             &old_coin_pub),
     92       GNUNET_PQ_result_spec_auto_from_type ("old_denom_pub_hash",
     93                                             &old_denom_pub_hash),
     94       GNUNET_PQ_result_spec_auto_from_type ("coin_pub",
     95                                             &coin.coin_pub),
     96       GNUNET_PQ_result_spec_auto_from_type ("coin_sig",
     97                                             &coin_sig),
     98       GNUNET_PQ_result_spec_auto_from_type ("coin_blind",
     99                                             &coin_blind),
    100       TALER_PQ_result_spec_denom_pub ("denom_pub",
    101                                       &denom_pub),
    102       GNUNET_PQ_result_spec_auto_from_type ("h_blind_ev",
    103                                             &h_blind_ev),
    104       GNUNET_PQ_result_spec_auto_from_type ("denom_pub_hash",
    105                                             &coin.denom_pub_hash),
    106       GNUNET_PQ_result_spec_allow_null (
    107         GNUNET_PQ_result_spec_auto_from_type ("age_commitment_hash",
    108                                               &coin.h_age_commitment),
    109         &coin.no_age_commitment),
    110       TALER_PQ_result_spec_denom_sig ("denom_sig",
    111                                       &coin.denom_sig),
    112       TALER_PQ_RESULT_SPEC_AMOUNT ("amount",
    113                                    &amount),
    114       GNUNET_PQ_result_spec_end
    115     };
    116     enum GNUNET_GenericReturnValue ret;
    117 
    118     if (GNUNET_OK !=
    119         GNUNET_PQ_extract_result (result,
    120                                   rs,
    121                                   i))
    122     {
    123       GNUNET_break (0);
    124       psc->status = GNUNET_SYSERR;
    125       return;
    126     }
    127     ret = psc->cb (psc->cb_cls,
    128                    rowid,
    129                    timestamp,
    130                    &amount,
    131                    &old_coin_pub,
    132                    &old_denom_pub_hash,
    133                    &coin,
    134                    &denom_pub,
    135                    &coin_sig,
    136                    &coin_blind);
    137     GNUNET_PQ_cleanup_result (rs);
    138     if (GNUNET_OK != ret)
    139       break;
    140   }
    141 }
    142 
    143 
    144 enum GNUNET_DB_QueryStatus
    145 TEH_PG_select_recoup_refresh_above_serial_id (
    146   void *cls,
    147   uint64_t serial_id,
    148   TALER_EXCHANGEDB_RecoupRefreshCallback cb,
    149   void *cb_cls)
    150 {
    151   struct PostgresClosure *pg = cls;
    152   struct GNUNET_PQ_QueryParam params[] = {
    153     GNUNET_PQ_query_param_uint64 (&serial_id),
    154     GNUNET_PQ_query_param_end
    155   };
    156   struct RecoupRefreshSerialContext psc = {
    157     .cb = cb,
    158     .cb_cls = cb_cls,
    159     .pg = pg,
    160     .status = GNUNET_OK
    161   };
    162   enum GNUNET_DB_QueryStatus qs;
    163 
    164   PREPARE (pg,
    165            "select_recoup_refresh_above_serial_id",
    166            "SELECT"
    167            " rr.recoup_refresh_uuid"
    168            ",rr.recoup_timestamp"
    169            ",rr.coin_sig"
    170            ",rr.coin_blind"
    171            ",rr.amount"
    172            ",rrc.h_coin_ev AS h_blind_ev" // FIXME:-#9828 r.rc? r.selected_h? Old logic wanted a TALER_BlindedCoinHash, which we now need to derive (from rr.coin_blind)
    173            ",new_coins.age_commitment_hash"
    174            ",new_coins.coin_pub AS coin_pub"
    175            ",new_denoms.denom_pub AS denom_pub"
    176            ",new_denoms.denom_pub_hash"
    177            ",new_coins.denom_sig AS denom_sig"
    178            ",old_coins.coin_pub AS old_coin_pub"
    179            ",old_denoms.denom_pub_hash AS old_denom_pub_hash"
    180            " FROM recoup_refresh rr"
    181            " INNER JOIN refresh_revealed_coins rrc" // FIXME-#9828: no such table anymore!
    182            // but we have 'refresh_id" which is an FK into 'refresh'!
    183            "   USING (rrc_serial)"
    184            " INNER JOIN refresh r"
    185            // but we have 'refresh_id" which is an FK into 'refresh'!
    186            "   USING (refresh_id)"
    187            " INNER JOIN known_coins old_coins"
    188            "   ON (r.old_coin_pub = old_coins.coin_pub)"
    189            " INNER JOIN known_coins new_coins"
    190            "   ON (rr.coin_pub == new_coins.coin_pub)"
    191            " INNER JOIN refresh_commitments rfc"
    192            "   ON (rrc.melt_serial_id = rfc.melt_serial_id)"
    193            " INNER JOIN denominations new_denoms"
    194            "   ON (new_coins.denominations_serial = new_denoms.denominations_serial)"
    195            " INNER JOIN denominations old_denoms"
    196            "   ON (old_coins.denominations_serial = old_denoms.denominations_serial)"
    197            " WHERE recoup_refresh_uuid>=$1"
    198            " ORDER BY recoup_refresh_uuid ASC;");
    199   qs = GNUNET_PQ_eval_prepared_multi_select (
    200     pg->conn,
    201     "select_recoup_refresh_above_serial_id",
    202     params,
    203     &recoup_refresh_serial_helper_cb,
    204     &psc);
    205   if (GNUNET_OK != psc.status)
    206     return GNUNET_DB_STATUS_HARD_ERROR;
    207   return qs;
    208 }