exchange

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

pg_get_exchange_signkeys.c (5340B)


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