exchange

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

get_exchange_signkeys.c (5550B)


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