exchange

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

get_reserves.c (5851B)


      1 /*
      2    This file is part of TALER
      3    Copyright (C) 2024 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 #include "taler/taler_pq_lib.h"
     17 #include "pg_helper.h"
     18 #include "auditor-database/get_reserves.h"
     19 
     20 
     21 struct ReservesContext
     22 {
     23 
     24   /**
     25    * Function to call for each bad sig loss.
     26    */
     27   TALER_AUDITORDB_ReservesCallback cb;
     28 
     29   /**
     30    * Closure for @e cb
     31    */
     32   void *cb_cls;
     33 
     34   /**
     35    * Plugin context.
     36    */
     37   struct TALER_AUDITORDB_PostgresContext *pg;
     38 
     39   /**
     40    * Query status to return.
     41    */
     42   enum GNUNET_DB_QueryStatus qs;
     43 };
     44 
     45 
     46 /**
     47  * Helper function for #TALER_AUDITORDB_get_reserves().
     48  * To be called with the results of a SELECT statement
     49  * that has returned @a num_results results.
     50  *
     51  * @param cls closure of type `struct ReservesContext *`
     52  * @param result the postgres result
     53  * @param num_results the number of results in @a result
     54  */
     55 static void
     56 reserves_cb (void *cls,
     57              PGresult *result,
     58              unsigned int num_results)
     59 {
     60   struct ReservesContext *dcc = cls;
     61   struct TALER_AUDITORDB_PostgresContext *pg = dcc->pg;
     62 
     63   for (unsigned int i = 0; i < num_results; i++)
     64   {
     65     struct TALER_AUDITORDB_Reserves dc;
     66     struct GNUNET_PQ_ResultSpec rs[] = {
     67       GNUNET_PQ_result_spec_uint64 ("auditor_reserves_rowid",
     68                                     &dc.auditor_reserves_rowid),
     69       GNUNET_PQ_result_spec_auto_from_type ("reserve_pub",
     70                                             &dc.reserve_pub),
     71       TALER_PQ_RESULT_SPEC_AMOUNT ("reserve_balance",
     72                                    &dc.reserve_balance),
     73       TALER_PQ_RESULT_SPEC_AMOUNT ("reserve_loss",
     74                                    &dc.reserve_loss),
     75       TALER_PQ_RESULT_SPEC_AMOUNT ("withdraw_fee_balance",
     76                                    &dc.withdraw_fee_balance),
     77       TALER_PQ_RESULT_SPEC_AMOUNT ("close_fee_balance",
     78                                    &dc.close_fee_balance),
     79       TALER_PQ_RESULT_SPEC_AMOUNT ("purse_fee_balance",
     80                                    &dc.purse_fee_balance),
     81       TALER_PQ_RESULT_SPEC_AMOUNT ("open_fee_balance",
     82                                    &dc.open_fee_balance),
     83       TALER_PQ_RESULT_SPEC_AMOUNT ("history_fee_balance",
     84                                    &dc.history_fee_balance),
     85       GNUNET_PQ_result_spec_absolute_time ("expiration_date",
     86                                            &dc.expiration_date),
     87       GNUNET_PQ_result_spec_allow_null (
     88         GNUNET_PQ_result_spec_string ("origin_account",
     89                                       &dc.origin_account.full_payto),
     90         NULL),
     91       GNUNET_PQ_result_spec_end
     92     };
     93     enum GNUNET_GenericReturnValue rval;
     94 
     95     dc.origin_account.full_payto = NULL;
     96     if (GNUNET_OK !=
     97         GNUNET_PQ_extract_result (result,
     98                                   rs,
     99                                   i))
    100     {
    101       GNUNET_break (0);
    102       dcc->qs = GNUNET_DB_STATUS_HARD_ERROR;
    103       return;
    104     }
    105     dcc->qs = i + 1;
    106     rval = dcc->cb (dcc->cb_cls,
    107                     dc.auditor_reserves_rowid,
    108                     &dc);
    109     GNUNET_PQ_cleanup_result (rs);
    110     if (GNUNET_OK != rval)
    111       break;
    112   }
    113 }
    114 
    115 
    116 enum GNUNET_DB_QueryStatus
    117 TALER_AUDITORDB_get_reserves (struct TALER_AUDITORDB_PostgresContext *pg,
    118                               int64_t limit,
    119                               uint64_t offset,
    120                               TALER_AUDITORDB_ReservesCallback cb,
    121                               void *cb_cls)
    122 {
    123   uint64_t plimit = (uint64_t) ((limit < 0) ? -limit : limit);
    124   struct GNUNET_PQ_QueryParam params[] = {
    125     GNUNET_PQ_query_param_uint64 (&offset),
    126     GNUNET_PQ_query_param_uint64 (&plimit),
    127     GNUNET_PQ_query_param_end
    128   };
    129   struct ReservesContext dcc = {
    130     .cb = cb,
    131     .cb_cls = cb_cls,
    132     .pg = pg
    133   };
    134   enum GNUNET_DB_QueryStatus qs;
    135 
    136   PREPARE (pg,
    137            "auditor_reserves_get_desc",
    138            "SELECT"
    139            " auditor_reserves_rowid,"
    140            " reserve_pub,"
    141            " reserve_balance,"
    142            " reserve_loss,"
    143            " withdraw_fee_balance,"
    144            " close_fee_balance,"
    145            " purse_fee_balance,"
    146            " open_fee_balance,"
    147            " history_fee_balance,"
    148            " expiration_date,"
    149            " origin_account"
    150            " FROM auditor_reserves"
    151            " WHERE (auditor_reserves_rowid < $1)"
    152            " ORDER BY auditor_reserves_rowid DESC"
    153            " LIMIT $2"
    154            );
    155   PREPARE (pg,
    156            "auditor_reserves_get_asc",
    157            "SELECT"
    158            " auditor_reserves_rowid,"
    159            " reserve_pub,"
    160            " reserve_balance,"
    161            " reserve_loss,"
    162            " withdraw_fee_balance,"
    163            " close_fee_balance,"
    164            " purse_fee_balance,"
    165            " open_fee_balance,"
    166            " history_fee_balance,"
    167            " expiration_date,"
    168            " origin_account"
    169            " FROM auditor_reserves"
    170            " WHERE (auditor_reserves_rowid > $1)"
    171            " ORDER BY auditor_reserves_rowid ASC"
    172            " LIMIT $2"
    173            );
    174   qs = GNUNET_PQ_eval_prepared_multi_select (
    175     pg->conn,
    176     (limit > 0)
    177     ? "auditor_reserves_get_asc"
    178     : "auditor_reserves_get_desc",
    179     params,
    180     &reserves_cb,
    181     &dcc);
    182   if (qs > 0)
    183     return dcc.qs;
    184   GNUNET_break (GNUNET_DB_STATUS_HARD_ERROR != qs);
    185   return qs;
    186 }