exchange

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

pg_select_purse_deposits_above_serial_id.c (6710B)


      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 pg_select_purse_deposits_above_serial_id.c
     18  * @brief Implementation of the select_purse_deposits_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_purse_deposits_above_serial_id.h"
     26 #include "pg_helper.h"
     27 
     28 /**
     29  * Closure for #purse_deposit_serial_helper_cb().
     30  */
     31 struct PurseDepositSerialContext
     32 {
     33 
     34   /**
     35    * Callback to call.
     36    */
     37   TALER_EXCHANGEDB_PurseDepositCallback cb;
     38 
     39   /**
     40    * Closure for @e cb.
     41    */
     42   void *cb_cls;
     43 
     44   /**
     45    * Plugin context.
     46    */
     47   struct PostgresClosure *pg;
     48 
     49   /**
     50    * Status code, set to #GNUNET_SYSERR on hard errors.
     51    */
     52   enum GNUNET_GenericReturnValue status;
     53 };
     54 
     55 
     56 /**
     57  * Helper function to be called with the results of a SELECT statement
     58  * that has returned @a num_results results.
     59  *
     60  * @param cls closure of type `struct DepositSerialContext`
     61  * @param result the postgres result
     62  * @param num_results the number of results in @a result
     63  */
     64 static void
     65 purse_deposit_serial_helper_cb (void *cls,
     66                                 PGresult *result,
     67                                 unsigned int num_results)
     68 {
     69   struct PurseDepositSerialContext *dsc = cls;
     70   struct PostgresClosure *pg = dsc->pg;
     71 
     72   for (unsigned int i = 0; i<num_results; i++)
     73   {
     74     struct TALER_EXCHANGEDB_PurseDeposit deposit = {
     75       .exchange_base_url = NULL
     76     };
     77     struct TALER_DenominationPublicKey denom_pub;
     78     uint64_t rowid;
     79     uint32_t flags32;
     80     struct TALER_ReservePublicKeyP reserve_pub;
     81     bool not_merged = false;
     82     struct TALER_Amount purse_balance;
     83     struct TALER_Amount purse_total;
     84     struct GNUNET_PQ_ResultSpec rs[] = {
     85       TALER_PQ_RESULT_SPEC_AMOUNT ("amount_with_fee",
     86                                    &deposit.amount),
     87       TALER_PQ_RESULT_SPEC_AMOUNT ("balance",
     88                                    &purse_balance),
     89       TALER_PQ_RESULT_SPEC_AMOUNT ("total",
     90                                    &purse_total),
     91       TALER_PQ_RESULT_SPEC_AMOUNT ("fee_deposit",
     92                                    &deposit.deposit_fee),
     93       GNUNET_PQ_result_spec_allow_null (
     94         GNUNET_PQ_result_spec_string ("partner_base_url",
     95                                       &deposit.exchange_base_url),
     96         NULL),
     97       GNUNET_PQ_result_spec_allow_null (
     98         GNUNET_PQ_result_spec_auto_from_type ("reserve_pub",
     99                                               &reserve_pub),
    100         &not_merged),
    101       TALER_PQ_result_spec_denom_pub ("denom_pub",
    102                                       &denom_pub),
    103       GNUNET_PQ_result_spec_auto_from_type ("purse_pub",
    104                                             &deposit.purse_pub),
    105       GNUNET_PQ_result_spec_auto_from_type ("coin_sig",
    106                                             &deposit.coin_sig),
    107       GNUNET_PQ_result_spec_uint32 ("flags",
    108                                     &flags32),
    109       GNUNET_PQ_result_spec_auto_from_type ("coin_pub",
    110                                             &deposit.coin_pub),
    111       GNUNET_PQ_result_spec_allow_null (
    112         GNUNET_PQ_result_spec_auto_from_type ("age_commitment_hash",
    113                                               &deposit.h_age_commitment),
    114         &deposit.no_age_commitment),
    115       GNUNET_PQ_result_spec_uint64 ("purse_deposit_serial_id",
    116                                     &rowid),
    117       GNUNET_PQ_result_spec_end
    118     };
    119     enum GNUNET_GenericReturnValue ret;
    120 
    121     memset (&deposit,
    122             0,
    123             sizeof (deposit));
    124     if (GNUNET_OK !=
    125         GNUNET_PQ_extract_result (result,
    126                                   rs,
    127                                   i))
    128     {
    129       GNUNET_break (0);
    130       dsc->status = GNUNET_SYSERR;
    131       return;
    132     }
    133     ret = dsc->cb (dsc->cb_cls,
    134                    rowid,
    135                    &deposit,
    136                    not_merged ? NULL : &reserve_pub,
    137                    (enum TALER_WalletAccountMergeFlags) flags32,
    138                    &purse_balance,
    139                    &purse_total,
    140                    &denom_pub);
    141     GNUNET_PQ_cleanup_result (rs);
    142     if (GNUNET_OK != ret)
    143       break;
    144   }
    145 }
    146 
    147 
    148 enum GNUNET_DB_QueryStatus
    149 TEH_PG_select_purse_deposits_above_serial_id (
    150   void *cls,
    151   uint64_t serial_id,
    152   TALER_EXCHANGEDB_PurseDepositCallback cb,
    153   void *cb_cls)
    154 {
    155   struct PostgresClosure *pg = cls;
    156   struct GNUNET_PQ_QueryParam params[] = {
    157     GNUNET_PQ_query_param_uint64 (&serial_id),
    158     GNUNET_PQ_query_param_end
    159   };
    160   struct PurseDepositSerialContext dsc = {
    161     .cb = cb,
    162     .cb_cls = cb_cls,
    163     .pg = pg,
    164     .status = GNUNET_OK
    165   };
    166   enum GNUNET_DB_QueryStatus qs;
    167 
    168   PREPARE (pg,
    169            "audit_get_purse_deposits_incr",
    170            "SELECT"
    171            " pd.amount_with_fee"
    172            ",pr.amount_with_fee AS total"
    173            ",pr.balance"
    174            ",pr.flags"
    175            ",pd.purse_pub"
    176            ",pd.coin_sig"
    177            ",partner_base_url"
    178            ",denom.denom_pub"
    179            ",denom.fee_deposit"
    180            ",pm.reserve_pub"
    181            ",kc.coin_pub"
    182            ",kc.age_commitment_hash"
    183            ",pd.purse_deposit_serial_id"
    184            " FROM purse_deposits pd"
    185            " LEFT JOIN partners USING (partner_serial_id)"
    186            " LEFT JOIN purse_merges pm USING (purse_pub)"
    187            " JOIN purse_requests pr USING (purse_pub)"
    188            " JOIN known_coins kc USING (coin_pub)"
    189            " JOIN denominations denom USING (denominations_serial)"
    190            " WHERE ("
    191            "  (purse_deposit_serial_id>=$1)"
    192            " )"
    193            " ORDER BY purse_deposit_serial_id ASC;");
    194   qs = GNUNET_PQ_eval_prepared_multi_select (pg->conn,
    195                                              "audit_get_purse_deposits_incr",
    196                                              params,
    197                                              &purse_deposit_serial_helper_cb,
    198                                              &dsc);
    199   if (GNUNET_OK != dsc.status)
    200     return GNUNET_DB_STATUS_HARD_ERROR;
    201   return qs;
    202 }