LCOV - code coverage report
Current view: top level - exchangedb - iterate_active_signkeys.c (source / functions) Coverage Total Hit
Test: coverage.info Lines: 90.9 % 22 20
Test Date: 2026-04-14 15:39:31 Functions: 100.0 % 2 2

            Line data    Source code
       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 exchangedb/iterate_active_signkeys.c
      18              :  * @brief Implementation of the iterate_active_signkeys function for Postgres
      19              :  * @author Christian Grothoff
      20              :  */
      21              : #include "taler/taler_pq_lib.h"
      22              : #include "exchange-database/iterate_active_signkeys.h"
      23              : #include "helper.h"
      24              : 
      25              : 
      26              : /**
      27              :  * Closure for #signkeys_cb_helper()
      28              :  */
      29              : struct SignkeysIteratorContext
      30              : {
      31              :   /**
      32              :    * Function to call with the results.
      33              :    */
      34              :   TALER_EXCHANGEDB_ActiveSignkeysCallback cb;
      35              : 
      36              :   /**
      37              :    * Closure to pass to @e cb
      38              :    */
      39              :   void *cb_cls;
      40              : 
      41              : };
      42              : 
      43              : 
      44              : /**
      45              :  * Helper function for #TALER_EXCHANGEDB_iterate_active_signkeys().
      46              :  * Calls the callback with each signkey.
      47              :  *
      48              :  * @param cls a `struct SignkeysIteratorContext`
      49              :  * @param result db results
      50              :  * @param num_results number of results in @a result
      51              :  */
      52              : static void
      53           41 : signkeys_cb_helper (void *cls,
      54              :                     PGresult *result,
      55              :                     unsigned int num_results)
      56              : {
      57           41 :   struct SignkeysIteratorContext *dic = cls;
      58              : 
      59           68 :   for (unsigned int i = 0; i<num_results; i++)
      60              :   {
      61              :     struct TALER_EXCHANGEDB_SignkeyMetaData meta;
      62              :     struct TALER_ExchangePublicKeyP exchange_pub;
      63              :     struct TALER_MasterSignatureP master_sig;
      64           27 :     struct GNUNET_PQ_ResultSpec rs[] = {
      65           27 :       GNUNET_PQ_result_spec_auto_from_type ("master_sig",
      66              :                                             &master_sig),
      67           27 :       GNUNET_PQ_result_spec_auto_from_type ("exchange_pub",
      68              :                                             &exchange_pub),
      69           27 :       GNUNET_PQ_result_spec_timestamp ("valid_from",
      70              :                                        &meta.start),
      71           27 :       GNUNET_PQ_result_spec_timestamp ("expire_sign",
      72              :                                        &meta.expire_sign),
      73           27 :       GNUNET_PQ_result_spec_timestamp ("expire_legal",
      74              :                                        &meta.expire_legal),
      75              :       GNUNET_PQ_result_spec_end
      76              :     };
      77              : 
      78           27 :     if (GNUNET_OK !=
      79           27 :         GNUNET_PQ_extract_result (result,
      80              :                                   rs,
      81              :                                   i))
      82              :     {
      83            0 :       GNUNET_break (0);
      84            0 :       return;
      85              :     }
      86           27 :     dic->cb (dic->cb_cls,
      87              :              &exchange_pub,
      88              :              &meta,
      89              :              &master_sig);
      90              :   }
      91              : }
      92              : 
      93              : 
      94              : /**
      95              :  * Function called to invoke @a cb on every non-revoked exchange signing key
      96              :  * that has been signed by the master key.  Revoked and (for signing!)
      97              :  * expired keys are skipped. Runs in its own read-only transaction.
      98              :  *
      99              :  * @param pg the database context
     100              :  * @param cb function to call on each signing key
     101              :  * @param cb_cls closure for @a cb
     102              :  * @return transaction status code
     103              :  */
     104              : enum GNUNET_DB_QueryStatus
     105           41 : TALER_EXCHANGEDB_iterate_active_signkeys (struct
     106              :                                           TALER_EXCHANGEDB_PostgresContext *pg,
     107              :                                           TALER_EXCHANGEDB_ActiveSignkeysCallback
     108              :                                           cb,
     109              :                                           void *cb_cls)
     110              : {
     111           41 :   struct GNUNET_TIME_Absolute now = {0};
     112           41 :   struct GNUNET_PQ_QueryParam params[] = {
     113           41 :     GNUNET_PQ_query_param_absolute_time (&now),
     114              :     GNUNET_PQ_query_param_end
     115              :   };
     116           41 :   struct SignkeysIteratorContext dic = {
     117              :     .cb = cb,
     118              :     .cb_cls = cb_cls,
     119              :   };
     120              : 
     121           41 :   PREPARE (pg,
     122              :            "select_signkeys",
     123              :            "SELECT"
     124              :            " master_sig"
     125              :            ",exchange_pub"
     126              :            ",valid_from"
     127              :            ",expire_sign"
     128              :            ",expire_legal"
     129              :            " FROM exchange_sign_keys esk"
     130              :            " WHERE"
     131              :            "   expire_sign > $1"
     132              :            " AND NOT EXISTS "
     133              :            "  (SELECT esk_serial "
     134              :            "     FROM signkey_revocations skr"
     135              :            "    WHERE esk.esk_serial = skr.esk_serial);");
     136           41 :   now = GNUNET_TIME_absolute_get ();
     137           41 :   return GNUNET_PQ_eval_prepared_multi_select (pg->conn,
     138              :                                                "select_signkeys",
     139              :                                                params,
     140              :                                                &signkeys_cb_helper,
     141              :                                                &dic);
     142              : }
        

Generated by: LCOV version 2.0-1