sync

Backup service to store encrypted wallet databases (experimental)
Log | Files | Refs | Submodules | README | LICENSE

syncdb_lookup_block_hashes_TR.c (4840B)


      1 /*
      2   This file is part of TALER
      3   (C) 2026 Taler Systems SA
      4 
      5   TALER is free software; you can redistribute it and/or modify it under the
      6   terms of the GNU Lesser 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 syncdb/syncdb_lookup_block_hashes_TR.c
     18  * @brief lookup the hashes of all the blocks of an account
     19  * @author Iván Ávalos
     20  */
     21 #include "syncdb_pg.h"
     22 
     23 
     24 /**
     25  * Closure for the hash iterator callback.
     26  */
     27 struct HashIteratorContext
     28 {
     29   /**
     30    * Iterator function to call for every block.
     31    */
     32   SYNC_DB_BlockHashIterator it;
     33 
     34   /**
     35    * Closure for @e it.
     36    */
     37   void *it_cls;
     38 
     39   /**
     40    * Query status to return.
     41    */
     42   enum SYNC_DB_QueryStatus qs;
     43 
     44   /**
     45    * Number of block hashes returned.
     46    */
     47   uint32_t num_hashes;
     48 
     49   /**
     50    * True if the account does not exist.
     51    */
     52   bool out_account_missing;
     53 
     54   /**
     55    * True if the account has no blocks at all.
     56    */
     57   bool out_backup_empty;
     58 };
     59 
     60 
     61 /**
     62  * Helper function for #SYNCDB_lookup_block_hashes_TR().
     63  * To be called with the results of a SELECT statement
     64  * that has returned @a num_results results.
     65  *
     66  * @param cls closure of type `struct HashIteratorContext *`
     67  * @param result the postgres result
     68  * @param num_results the number of results in @a result
     69  */
     70 static void
     71 block_hashes_cb (void *cls,
     72                  PGresult *result,
     73                  unsigned int num_results)
     74 {
     75   struct HashIteratorContext *hic = cls;
     76 
     77   for (unsigned int i = 0; i < num_results; i++)
     78   {
     79     bool out_account_missing;
     80     bool out_backup_empty;
     81     struct SYNC_BlockNonce nonce;
     82     struct GNUNET_HashCode block_hash;
     83     struct GNUNET_PQ_ResultSpec rs[] = {
     84       GNUNET_PQ_result_spec_bool ("out_account_missing",
     85                                   &out_account_missing),
     86       GNUNET_PQ_result_spec_bool ("out_backup_empty",
     87                                   &out_backup_empty),
     88       GNUNET_PQ_result_spec_auto_from_type ("nonce",
     89                                             &nonce),
     90       GNUNET_PQ_result_spec_auto_from_type ("block_hash",
     91                                             &block_hash),
     92       GNUNET_PQ_result_spec_end,
     93     };
     94 
     95     if (GNUNET_OK !=
     96         GNUNET_PQ_extract_result (result,
     97                                   rs,
     98                                   i))
     99     {
    100       GNUNET_break (0);
    101       hic->qs = SYNC_DB_SOFT_ERROR;
    102       return;
    103     }
    104 
    105     hic->qs = i + 1;
    106     hic->out_account_missing = out_account_missing;
    107     hic->out_backup_empty = out_backup_empty;
    108     if ( (! out_account_missing) &&
    109          (! out_backup_empty) )
    110     {
    111       /* Count the block even in the counting pass (NULL iterator) */
    112       hic->num_hashes++;
    113       if (NULL != hic->it)
    114         hic->it (hic->it_cls,
    115                  &nonce,
    116                  &block_hash);
    117     }
    118     GNUNET_PQ_cleanup_result (rs);
    119   }
    120 }
    121 
    122 
    123 enum SYNC_DB_QueryStatus
    124 SYNCDB_lookup_block_hashes_TR (
    125   const struct SYNC_AccountPublicKeyP *account_pub,
    126   SYNC_DB_BlockHashIterator it,
    127   void *it_cls)
    128 {
    129   struct GNUNET_PQ_QueryParam params[] = {
    130     GNUNET_PQ_query_param_auto_from_type (account_pub),
    131     GNUNET_PQ_query_param_end,
    132   };
    133   struct HashIteratorContext hic = {
    134     .it = it,
    135     .it_cls = it_cls,
    136   };
    137   enum GNUNET_DB_QueryStatus qs;
    138 
    139   SYNCDB_preflight ();
    140   PREPARE ("blocks_select_hashes",
    141            "SELECT"
    142            " out_account_missing"
    143            ",out_backup_empty"
    144            ",nonce"
    145            ",block_hash"
    146            " FROM sync_do_lookup_block_hashes"
    147            " ($1)");
    148   qs = GNUNET_PQ_eval_prepared_multi_select (pg->conn,
    149                                              "blocks_select_hashes",
    150                                              params,
    151                                              &block_hashes_cb,
    152                                              &hic);
    153   switch (qs)
    154   {
    155   case GNUNET_DB_STATUS_HARD_ERROR:
    156     GNUNET_break (0);
    157     return SYNC_DB_HARD_ERROR;
    158   case GNUNET_DB_STATUS_SOFT_ERROR:
    159     return SYNC_DB_SOFT_ERROR;
    160   case GNUNET_DB_STATUS_SUCCESS_NO_RESULTS:
    161     /* the stored procedure always returns a status row */
    162     GNUNET_break (0);
    163     return SYNC_DB_NO_RESULTS;
    164   default:
    165     break;
    166   }
    167   /* map the flags of the (single) status row to SYNC_DB codes */
    168   if (hic.out_account_missing)
    169     return SYNC_DB_PAYMENT_REQUIRED;
    170   if (hic.out_backup_empty)
    171     return SYNC_DB_NO_RESULTS;
    172   return hic.num_hashes;
    173 }
    174 
    175 
    176 /* end of syncdb_lookup_block_hashes_TR.c */