LCOV - code coverage report
Current view: top level - exchangedb - do_import_credits.c (source / functions) Coverage Total Hit
Test: coverage.info Lines: 89.6 % 134 120
Test Date: 2026-11-18 17:34:04 Functions: 100.0 % 4 4

            Line data    Source code
       1              : /*
       2              :    This file is part of TALER
       3              :    Copyright (C) 2022--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 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/do_import_credits.c
      18              :  * @brief Implementation of the do_import_credits function for Postgres
      19              :  * @author Christian Grothoff
      20              :  * @author Joseph Xu
      21              :  */
      22              : #include "taler/taler_pq_lib.h"
      23              : #include "exchange-database/do_import_credits.h"
      24              : #include "helper.h"
      25              : 
      26              : 
      27              : /**
      28              :  * Compute the notification channel for a reserve that was credited.
      29              :  *
      30              :  * @param reserve_pub reserve to notify on
      31              :  * @return string to pass to postgres for the notification
      32              :  */
      33              : static char *
      34           84 : compute_notify_on_reserve (const struct TALER_ReservePublicKeyP *reserve_pub)
      35              : {
      36           84 :   struct TALER_EXCHANGEDB_ReserveEventP rep = {
      37           84 :     .header.size = htons (sizeof (rep)),
      38           84 :     .header.type = htons (TALER_DBEVENT_EXCHANGE_RESERVE_INCOMING),
      39              :     .reserve_pub = *reserve_pub
      40              :   };
      41              : 
      42           84 :   return GNUNET_PQ_get_event_notify_channel (&rep.header);
      43              : }
      44              : 
      45              : 
      46              : /**
      47              :  * Compute the notification channel for an account that authenticated itself.
      48              :  *
      49              :  * @param h_payto normalized hash of the account that was authenticated
      50              :  * @return string to pass to postgres for the notification
      51              :  */
      52              : static char *
      53           22 : compute_notify_on_kycauth (const struct TALER_NormalizedPaytoHashP *h_payto)
      54              : {
      55           22 :   struct TALER_EXCHANGEDB_KycCompletedEventP rep = {
      56           22 :     .header.size = htons (sizeof (rep)),
      57           22 :     .header.type = htons (TALER_DBEVENT_EXCHANGE_KYC_COMPLETED),
      58              :     .h_payto = *h_payto
      59              :   };
      60              : 
      61           22 :   return GNUNET_PQ_get_event_notify_channel (&rep.header);
      62              : }
      63              : 
      64              : 
      65              : /**
      66              :  * Closure for #helper_cb().
      67              :  */
      68              : struct Context
      69              : {
      70              :   /**
      71              :    * Array with entries set to 'true' for transfers we had already imported.
      72              :    */
      73              :   bool *transaction_duplicates;
      74              : 
      75              :   /**
      76              :    * Length of the @e transaction_duplicates array.
      77              :    */
      78              :   unsigned int length;
      79              : 
      80              :   /**
      81              :    * Set to #GNUNET_SYSERR on failures.
      82              :    */
      83              :   enum GNUNET_GenericReturnValue status;
      84              : };
      85              : 
      86              : 
      87              : /**
      88              :  * Function called with the rows the stored procedure returned, one per
      89              :  * reserve transfer, in input order.
      90              :  *
      91              :  * @param cls closure of type `struct Context *`
      92              :  * @param result the postgres result
      93              :  * @param num_results the number of results in @a result
      94              :  */
      95              : static void
      96          147 : helper_cb (void *cls,
      97              :            PGresult *result,
      98              :            unsigned int num_results)
      99              : {
     100          147 :   struct Context *ctx = cls;
     101              : 
     102          147 :   if (num_results != ctx->length)
     103              :   {
     104              :     /* The procedure returns one row per reserve transfer we passed in; a
     105              :        different count means the two have drifted apart.  More rows would
     106              :        write past the caller's array, fewer would leave entries of it
     107              :        unset. */
     108            0 :     GNUNET_break (0);
     109            0 :     ctx->status = GNUNET_SYSERR;
     110            0 :     return;
     111              :   }
     112          231 :   for (unsigned int i = 0; i<num_results; i++)
     113              :   {
     114           84 :     struct GNUNET_PQ_ResultSpec rs[] = {
     115           84 :       GNUNET_PQ_result_spec_bool ("out_duplicate",
     116           84 :                                   &ctx->transaction_duplicates[i]),
     117              :       GNUNET_PQ_result_spec_end
     118              :     };
     119              : 
     120           84 :     if (GNUNET_OK !=
     121           84 :         GNUNET_PQ_extract_result (result,
     122              :                                   rs,
     123              :                                   i))
     124              :     {
     125            0 :       GNUNET_break (0);
     126            0 :       ctx->status = GNUNET_SYSERR;
     127            0 :       return;
     128              :     }
     129              :   }
     130              : }
     131              : 
     132              : 
     133              : enum GNUNET_DB_QueryStatus
     134          147 : TALER_EXCHANGEDB_do_import_credits (
     135              :   struct TALER_EXCHANGEDB_PostgresContext *pg,
     136              :   const struct TALER_EXCHANGEDB_CreditBatch *batch,
     137              :   enum GNUNET_DB_QueryStatus *reserve_results)
     138          147 : {
     139          147 :   unsigned int rlen = batch->reserves_length;
     140          147 :   unsigned int klen = batch->kycauths_length;
     141          147 :   unsigned int wlen = batch->wads_length;
     142          147 :   unsigned int dups = 0;
     143              : 
     144              :   /* reserve transfers */
     145          147 :   struct TALER_ReservePublicKeyP reserve_pubs[GNUNET_NZL (rlen)];
     146          147 :   uint64_t wire_references[GNUNET_NZL (rlen)];
     147          147 :   struct TALER_Amount balances[GNUNET_NZL (rlen)];
     148          147 :   struct GNUNET_TIME_Timestamp execution_times[GNUNET_NZL (rlen)];
     149          147 :   struct TALER_FullPaytoHashP h_full_paytos[GNUNET_NZL (rlen)];
     150          147 :   struct TALER_NormalizedPaytoHashP h_normalized_paytos[GNUNET_NZL (rlen)];
     151          147 :   const char *payto_uris[GNUNET_NZL (rlen)];
     152          147 :   char *notify_s[GNUNET_NZL (rlen)];
     153          147 :   bool transaction_duplicates[GNUNET_NZL (rlen)];
     154              : 
     155              :   /* KYC authentication transfers */
     156          147 :   union TALER_AccountPublicKeyP ka_account_pubs[GNUNET_NZL (klen)];
     157          147 :   uint64_t ka_wire_references[GNUNET_NZL (klen)];
     158          147 :   struct TALER_Amount ka_balances[GNUNET_NZL (klen)];
     159          147 :   struct GNUNET_TIME_Timestamp ka_execution_times[GNUNET_NZL (klen)];
     160          147 :   struct TALER_FullPaytoHashP ka_h_full_paytos[GNUNET_NZL (klen)];
     161          147 :   struct TALER_NormalizedPaytoHashP ka_h_normalized_paytos[GNUNET_NZL (klen)];
     162          147 :   const char *ka_payto_uris[GNUNET_NZL (klen)];
     163          147 :   char *ka_notify_s[GNUNET_NZL (klen)];
     164              : 
     165              :   /* WAD transfers */
     166          147 :   struct TALER_WadIdentifierP wad_ids[GNUNET_NZL (wlen)];
     167          147 :   const char *wad_origin_exchange_urls[GNUNET_NZL (wlen)];
     168          147 :   struct TALER_Amount wad_balances[GNUNET_NZL (wlen)];
     169          147 :   struct GNUNET_TIME_Timestamp wad_execution_times[GNUNET_NZL (wlen)];
     170              : 
     171              :   struct GNUNET_TIME_Timestamp reserve_expiration
     172          147 :     = GNUNET_TIME_relative_to_timestamp (pg->idle_reserve_expiration_time);
     173              :   struct GNUNET_TIME_Timestamp gc
     174          147 :     = GNUNET_TIME_relative_to_timestamp (pg->legal_reserve_expiration_time);
     175              :   struct GNUNET_TIME_Absolute lease_until
     176          147 :     = GNUNET_TIME_relative_to_absolute (batch->lease);
     177              :   enum GNUNET_DB_QueryStatus qs;
     178              : 
     179          231 :   for (unsigned int i = 0; i<rlen; i++)
     180              :   {
     181           84 :     const struct TALER_EXCHANGEDB_ReserveInInfo *reserve = &batch->reserves[i];
     182              : 
     183           84 :     TALER_full_payto_hash (reserve->sender_account_details,
     184              :                            &h_full_paytos[i]);
     185           84 :     TALER_full_payto_normalize_and_hash (reserve->sender_account_details,
     186              :                                          &h_normalized_paytos[i]);
     187           84 :     notify_s[i] = compute_notify_on_reserve (reserve->reserve_pub);
     188           84 :     reserve_pubs[i] = *reserve->reserve_pub;
     189           84 :     balances[i] = *reserve->balance;
     190           84 :     execution_times[i] = reserve->execution_time;
     191           84 :     payto_uris[i] = reserve->sender_account_details.full_payto;
     192           84 :     wire_references[i] = reserve->wire_reference;
     193              :   }
     194          169 :   for (unsigned int i = 0; i<klen; i++)
     195              :   {
     196           22 :     const struct TALER_EXCHANGEDB_KycauthInInfo *ka = &batch->kycauths[i];
     197              : 
     198           22 :     TALER_full_payto_hash (ka->sender_account_details,
     199              :                            &ka_h_full_paytos[i]);
     200           22 :     TALER_full_payto_normalize_and_hash (ka->sender_account_details,
     201              :                                          &ka_h_normalized_paytos[i]);
     202           22 :     ka_notify_s[i] = compute_notify_on_kycauth (&ka_h_normalized_paytos[i]);
     203           22 :     ka_account_pubs[i] = *ka->account_pub;
     204           22 :     ka_balances[i] = *ka->balance;
     205           22 :     ka_execution_times[i] = ka->execution_time;
     206           22 :     ka_payto_uris[i] = ka->sender_account_details.full_payto;
     207           22 :     ka_wire_references[i] = ka->wire_reference;
     208              :   }
     209          147 :   for (unsigned int i = 0; i<wlen; i++)
     210              :   {
     211            0 :     const struct TALER_EXCHANGEDB_WadInInfo *wad = &batch->wads[i];
     212              : 
     213            0 :     wad_ids[i] = *wad->wad_id;
     214            0 :     wad_origin_exchange_urls[i] = wad->origin_exchange_url;
     215            0 :     wad_balances[i] = *wad->balance;
     216            0 :     wad_execution_times[i] = wad->execution_time;
     217              :   }
     218              : 
     219          147 :   PREPARE (pg,
     220              :            "do_import_credits",
     221              :            "SELECT"
     222              :            " out_duplicate"
     223              :            " FROM exchange_do_import_credits"
     224              :            " ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13,$14,$15,$16,$17"
     225              :            " ,$18,$19,$20,$21,$22,$23,$24,$25,$26,$27,$28);");
     226              :   {
     227          147 :     struct GNUNET_PQ_QueryParam params[] = {
     228          147 :       GNUNET_PQ_query_param_string (batch->exchange_account_name),
     229          147 :       GNUNET_PQ_query_param_timestamp (&gc),
     230          147 :       GNUNET_PQ_query_param_timestamp (&reserve_expiration),
     231              :       /* reserve transfers */
     232          147 :       GNUNET_PQ_query_param_array_auto_from_type (rlen,
     233              :                                                   reserve_pubs,
     234              :                                                   pg->conn),
     235          147 :       GNUNET_PQ_query_param_array_uint64 (rlen,
     236              :                                           wire_references,
     237              :                                           pg->conn),
     238          147 :       TALER_PQ_query_param_array_amount (rlen,
     239              :                                          balances,
     240              :                                          pg->conn),
     241          147 :       GNUNET_PQ_query_param_array_timestamp (rlen,
     242              :                                              execution_times,
     243              :                                              pg->conn),
     244          147 :       GNUNET_PQ_query_param_array_auto_from_type (rlen,
     245              :                                                   h_full_paytos,
     246              :                                                   pg->conn),
     247          147 :       GNUNET_PQ_query_param_array_auto_from_type (rlen,
     248              :                                                   h_normalized_paytos,
     249              :                                                   pg->conn),
     250          147 :       GNUNET_PQ_query_param_array_ptrs_string (rlen,
     251              :                                                payto_uris,
     252              :                                                pg->conn),
     253          147 :       GNUNET_PQ_query_param_array_ptrs_string (rlen,
     254              :                                                (const char **) notify_s,
     255              :                                                pg->conn),
     256              :       /* KYC authentication transfers */
     257          147 :       GNUNET_PQ_query_param_array_auto_from_type (klen,
     258              :                                                   ka_account_pubs,
     259              :                                                   pg->conn),
     260          147 :       GNUNET_PQ_query_param_array_uint64 (klen,
     261              :                                           ka_wire_references,
     262              :                                           pg->conn),
     263          147 :       TALER_PQ_query_param_array_amount (klen,
     264              :                                          ka_balances,
     265              :                                          pg->conn),
     266          147 :       GNUNET_PQ_query_param_array_timestamp (klen,
     267              :                                              ka_execution_times,
     268              :                                              pg->conn),
     269          147 :       GNUNET_PQ_query_param_array_auto_from_type (klen,
     270              :                                                   ka_h_full_paytos,
     271              :                                                   pg->conn),
     272          147 :       GNUNET_PQ_query_param_array_auto_from_type (klen,
     273              :                                                   ka_h_normalized_paytos,
     274              :                                                   pg->conn),
     275          147 :       GNUNET_PQ_query_param_array_ptrs_string (klen,
     276              :                                                ka_payto_uris,
     277              :                                                pg->conn),
     278          147 :       GNUNET_PQ_query_param_array_ptrs_string (klen,
     279              :                                                (const char **) ka_notify_s,
     280              :                                                pg->conn),
     281              :       /* WAD transfers */
     282          147 :       GNUNET_PQ_query_param_array_auto_from_type (wlen,
     283              :                                                   wad_ids,
     284              :                                                   pg->conn),
     285          147 :       GNUNET_PQ_query_param_array_ptrs_string (wlen,
     286              :                                                wad_origin_exchange_urls,
     287              :                                                pg->conn),
     288          147 :       TALER_PQ_query_param_array_amount (wlen,
     289              :                                          wad_balances,
     290              :                                          pg->conn),
     291          147 :       GNUNET_PQ_query_param_array_timestamp (wlen,
     292              :                                              wad_execution_times,
     293              :                                              pg->conn),
     294              :       /* shard bookkeeping */
     295          147 :       GNUNET_PQ_query_param_string (batch->job_name),
     296          147 :       GNUNET_PQ_query_param_uint64 (&batch->shard_start),
     297          147 :       GNUNET_PQ_query_param_uint64 (&batch->shard_end),
     298          147 :       GNUNET_PQ_query_param_uint64 (&batch->progress_row),
     299          147 :       GNUNET_PQ_query_param_absolute_time (&lease_until),
     300              :       GNUNET_PQ_query_param_end
     301              :     };
     302          147 :     struct Context ctx = {
     303              :       .transaction_duplicates = transaction_duplicates,
     304              :       .length = rlen,
     305              :       .status = GNUNET_OK
     306              :     };
     307              : 
     308          147 :     qs = GNUNET_PQ_eval_prepared_multi_select (pg->conn,
     309              :                                                "do_import_credits",
     310              :                                                params,
     311              :                                                &helper_cb,
     312              :                                                &ctx);
     313          147 :     GNUNET_PQ_cleanup_query_params_closures (params);
     314          147 :     if ( (qs >= 0) &&
     315          147 :          (GNUNET_OK != ctx.status) )
     316            0 :       qs = GNUNET_DB_STATUS_HARD_ERROR;
     317          147 :     if (qs < 0)
     318            0 :       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
     319              :                   "Failed to import credit batch (%d)\n",
     320              :                   qs);
     321              :   }
     322              : 
     323          231 :   for (unsigned int i = 0; i<rlen; i++)
     324           84 :     GNUNET_free (notify_s[i]);
     325          169 :   for (unsigned int i = 0; i<klen; i++)
     326           22 :     GNUNET_free (ka_notify_s[i]);
     327          147 :   if (qs < 0)
     328            0 :     return qs;
     329          231 :   for (unsigned int i = 0; i<rlen; i++)
     330              :   {
     331           84 :     if (transaction_duplicates[i])
     332            2 :       dups++;
     333           84 :     reserve_results[i] = transaction_duplicates[i]
     334              :       ? GNUNET_DB_STATUS_SUCCESS_NO_RESULTS
     335           84 :       : GNUNET_DB_STATUS_SUCCESS_ONE_RESULT;
     336              :   }
     337          147 :   GNUNET_PQ_event_do_poll (pg->conn);
     338          147 :   if (0 != dups)
     339            2 :     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
     340              :                 "%u/%u duplicates among incoming transactions. Try increasing WIREWATCH_IDLE_SLEEP_INTERVAL in the [exchange] configuration section (if this happens a lot).\n",
     341              :                 dups,
     342              :                 rlen);
     343          147 :   return qs;
     344              : }
        

Generated by: LCOV version 2.0-1