exchange

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

pg_select_coin_deposits_above_serial_id.c (7080B)


      1 /*
      2    This file is part of TALER
      3    Copyright (C) 2022-2023 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_coin_deposits_above_serial_id.c
     18  * @brief Implementation of the select_coin_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_coin_deposits_above_serial_id.h"
     26 #include "pg_helper.h"
     27 
     28 /**
     29  * Closure for #deposit_serial_helper_cb().
     30  */
     31 struct CoinDepositSerialContext
     32 {
     33 
     34   /**
     35    * Callback to call.
     36    */
     37   TALER_EXCHANGEDB_DepositCallback 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 CoinDepositSerialContext`
     61  * @param result the postgres result
     62  * @param num_results the number of results in @a result
     63  */
     64 static void
     65 coin_deposit_serial_helper_cb (void *cls,
     66                                PGresult *result,
     67                                unsigned int num_results)
     68 {
     69   struct CoinDepositSerialContext *dsc = cls;
     70   struct PostgresClosure *pg = dsc->pg;
     71 
     72   for (unsigned int i = 0; i<num_results; i++)
     73   {
     74     struct TALER_EXCHANGEDB_Deposit deposit;
     75     struct GNUNET_TIME_Timestamp exchange_timestamp;
     76     struct TALER_DenominationPublicKey denom_pub;
     77     bool done;
     78     uint64_t rowid;
     79     struct GNUNET_PQ_ResultSpec rs[] = {
     80       TALER_PQ_RESULT_SPEC_AMOUNT ("amount_with_fee",
     81                                    &deposit.amount_with_fee),
     82       GNUNET_PQ_result_spec_timestamp ("wallet_timestamp",
     83                                        &deposit.timestamp),
     84       GNUNET_PQ_result_spec_timestamp ("exchange_timestamp",
     85                                        &exchange_timestamp),
     86       GNUNET_PQ_result_spec_auto_from_type ("merchant_pub",
     87                                             &deposit.merchant_pub),
     88       TALER_PQ_result_spec_denom_pub ("denom_pub",
     89                                       &denom_pub),
     90       GNUNET_PQ_result_spec_auto_from_type ("coin_pub",
     91                                             &deposit.coin.coin_pub),
     92       GNUNET_PQ_result_spec_allow_null (
     93         GNUNET_PQ_result_spec_auto_from_type ("age_commitment_hash",
     94                                               &deposit.coin.h_age_commitment),
     95         &deposit.coin.no_age_commitment),
     96       GNUNET_PQ_result_spec_allow_null (
     97         GNUNET_PQ_result_spec_auto_from_type ("wallet_data_hash",
     98                                               &deposit.wallet_data_hash),
     99         &deposit.no_wallet_data_hash),
    100       GNUNET_PQ_result_spec_auto_from_type ("coin_sig",
    101                                             &deposit.csig),
    102       GNUNET_PQ_result_spec_timestamp ("refund_deadline",
    103                                        &deposit.refund_deadline),
    104       GNUNET_PQ_result_spec_timestamp ("wire_deadline",
    105                                        &deposit.wire_deadline),
    106       GNUNET_PQ_result_spec_auto_from_type ("h_contract_terms",
    107                                             &deposit.h_contract_terms),
    108       GNUNET_PQ_result_spec_auto_from_type ("wire_salt",
    109                                             &deposit.wire_salt),
    110       GNUNET_PQ_result_spec_string ("receiver_wire_account",
    111                                     &deposit.receiver_wire_account.full_payto),
    112       GNUNET_PQ_result_spec_bool ("done",
    113                                   &done),
    114       GNUNET_PQ_result_spec_uint64 ("coin_deposit_serial_id",
    115                                     &rowid),
    116       GNUNET_PQ_result_spec_end
    117     };
    118     enum GNUNET_GenericReturnValue ret;
    119 
    120     memset (&deposit,
    121             0,
    122             sizeof (deposit));
    123     if (GNUNET_OK !=
    124         GNUNET_PQ_extract_result (result,
    125                                   rs,
    126                                   i))
    127     {
    128       GNUNET_break (0);
    129       dsc->status = GNUNET_SYSERR;
    130       return;
    131     }
    132     ret = dsc->cb (dsc->cb_cls,
    133                    rowid,
    134                    exchange_timestamp,
    135                    &deposit,
    136                    &denom_pub,
    137                    done);
    138     GNUNET_PQ_cleanup_result (rs);
    139     if (GNUNET_OK != ret)
    140       break;
    141   }
    142 }
    143 
    144 
    145 enum GNUNET_DB_QueryStatus
    146 TEH_PG_select_coin_deposits_above_serial_id (
    147   void *cls,
    148   uint64_t serial_id,
    149   TALER_EXCHANGEDB_DepositCallback cb,
    150   void *cb_cls)
    151 {
    152   struct PostgresClosure *pg = cls;
    153   struct GNUNET_PQ_QueryParam params[] = {
    154     GNUNET_PQ_query_param_uint64 (&serial_id),
    155     GNUNET_PQ_query_param_end
    156   };
    157   struct CoinDepositSerialContext dsc = {
    158     .cb = cb,
    159     .cb_cls = cb_cls,
    160     .pg = pg,
    161     .status = GNUNET_OK
    162   };
    163   enum GNUNET_DB_QueryStatus qs;
    164 
    165   /* Fetch deposits with rowid '\geq' the given parameter */
    166   PREPARE (pg,
    167            "audit_get_coin_deposits_incr",
    168            "SELECT"
    169            " cdep.amount_with_fee"
    170            ",bdep.wallet_timestamp"
    171            ",bdep.exchange_timestamp"
    172            ",bdep.merchant_pub"
    173            ",bdep.wallet_data_hash"
    174            ",denom.denom_pub"
    175            ",kc.coin_pub"
    176            ",kc.age_commitment_hash"
    177            ",cdep.coin_sig"
    178            ",bdep.refund_deadline"
    179            ",bdep.wire_deadline"
    180            ",bdep.h_contract_terms"
    181            ",bdep.wire_salt"
    182            ",wt.payto_uri AS receiver_wire_account"
    183            ",bdep.done"
    184            ",cdep.coin_deposit_serial_id"
    185            " FROM coin_deposits cdep"
    186            " JOIN batch_deposits bdep"
    187            "   USING (batch_deposit_serial_id)"
    188            " JOIN wire_targets wt"
    189            "   USING (wire_target_h_payto)"
    190            " JOIN known_coins kc"
    191            "   USING (coin_pub)"
    192            " JOIN denominations denom"
    193            "   USING (denominations_serial)"
    194            " WHERE (coin_deposit_serial_id>=$1)"
    195            " ORDER BY coin_deposit_serial_id ASC;");
    196   qs = GNUNET_PQ_eval_prepared_multi_select (pg->conn,
    197                                              "audit_get_coin_deposits_incr",
    198                                              params,
    199                                              &coin_deposit_serial_helper_cb,
    200                                              &dsc);
    201   if (GNUNET_OK != dsc.status)
    202     return GNUNET_DB_STATUS_HARD_ERROR;
    203   return qs;
    204 }