exchange

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

do_refresh.c (5717B)


      1 /*
      2    This file is part of TALER
      3    Copyright (C) 2025 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_refresh.c
     18  * @brief Implementation of the do_refresh function for Postgres
     19  * @author Özgür Kesim
     20  */
     21 #include "exchangedb_lib.h"
     22 #include "taler/taler_pq_lib.h"
     23 #include "exchange-database/do_refresh.h"
     24 #include "helper.h"
     25 
     26 
     27 enum GNUNET_DB_QueryStatus
     28 TALER_EXCHANGEDB_do_refresh (
     29   struct TALER_EXCHANGEDB_PostgresContext *pg,
     30   struct TALER_EXCHANGEDB_Refresh_vDOLDPLUS *refresh,
     31   const struct GNUNET_TIME_Timestamp *timestamp,
     32   bool *found,
     33   uint32_t *noreveal_index,
     34   bool *zombie_required,
     35   bool *nonce_reuse,
     36   bool *balance_ok,
     37   struct TALER_Amount *coin_balance)
     38 {
     39   uint32_t rc_version = refresh->rc_version;
     40   struct GNUNET_PQ_QueryParam params[] = {
     41     GNUNET_PQ_query_param_auto_from_type (&refresh->rc),
     42     GNUNET_PQ_query_param_timestamp (timestamp),
     43     GNUNET_PQ_query_param_auto_from_type (&refresh->refresh_seed), /* 3 */
     44     (TALER_REFRESH_COMMITMENT_VERSION_LEGACY_V27 == refresh->rc_version)
     45         ? GNUNET_PQ_query_param_null ()
     46         : GNUNET_PQ_query_param_array_auto_from_type (refresh->num_coins,
     47                                                       refresh->transfer_pubs,
     48                                                       pg->conn),
     49     GNUNET_PQ_query_param_auto_from_type (&refresh->planchets_h),
     50     TALER_PQ_query_param_amount (pg->conn, /* 6 */
     51                                  &refresh->amount_with_fee),
     52     (refresh->no_blinding_seed)
     53         ? GNUNET_PQ_query_param_null ()
     54         : GNUNET_PQ_query_param_auto_from_type (&refresh->blinding_seed),
     55     (0 < refresh->num_cs_r_values)
     56         ? TALER_PQ_query_param_array_cs_r_pub (refresh->num_cs_r_values,
     57                                                refresh->cs_r_values,
     58                                                pg->conn)
     59         : GNUNET_PQ_query_param_null (),
     60     (0 < refresh->num_cs_r_values)
     61         ? GNUNET_PQ_query_param_uint64 (&refresh->cs_r_choices) /* 9 */
     62         : GNUNET_PQ_query_param_null (),
     63     GNUNET_PQ_query_param_auto_from_type (&refresh->selected_h),
     64     TALER_PQ_query_param_array_blinded_denom_sig (refresh->num_coins,
     65                                                   refresh->denom_sigs,
     66                                                   pg->conn),
     67     GNUNET_PQ_query_param_array_uint64 (refresh->num_coins, /* 12 */
     68                                         refresh->denom_serials,
     69                                         pg->conn),
     70     GNUNET_PQ_query_param_auto_from_type (&refresh->coin.coin_pub),
     71     GNUNET_PQ_query_param_auto_from_type (&refresh->coin_sig),
     72     GNUNET_PQ_query_param_uint32 (&refresh->noreveal_index), /* 15 */
     73     GNUNET_PQ_query_param_bool (*zombie_required),
     74     GNUNET_PQ_query_param_uint32 (&rc_version),
     75     GNUNET_PQ_query_param_end
     76   };
     77   bool coin_found;
     78   bool no_noreveal_index;
     79   bool no_coin_balance;
     80   struct GNUNET_PQ_ResultSpec rs[] = {
     81     GNUNET_PQ_result_spec_bool ("coin_found",
     82                                 &coin_found),
     83     GNUNET_PQ_result_spec_bool ("balance_ok",
     84                                 balance_ok),
     85     GNUNET_PQ_result_spec_bool ("zombie_required",
     86                                 zombie_required),
     87     GNUNET_PQ_result_spec_bool ("nonce_reuse",
     88                                 nonce_reuse),
     89     GNUNET_PQ_result_spec_bool ("found",
     90                                 found),
     91     GNUNET_PQ_result_spec_allow_null (
     92       GNUNET_PQ_result_spec_uint32 ("noreveal_index",
     93                                     noreveal_index),
     94       &no_noreveal_index),
     95     GNUNET_PQ_result_spec_allow_null (
     96       TALER_PQ_RESULT_SPEC_AMOUNT ("coin_balance",
     97                                    coin_balance),
     98       &no_coin_balance),
     99     GNUNET_PQ_result_spec_end
    100   };
    101   enum GNUNET_DB_QueryStatus qs;
    102 
    103   GNUNET_assert ((TALER_REFRESH_COMMITMENT_VERSION_LEGACY_V27 ==
    104                   refresh->rc_version) ==
    105                  (NULL == refresh->transfer_pubs));
    106   PREPARE (pg,
    107            "do_refresh",
    108            "SELECT "
    109            " out_coin_found AS coin_found"
    110            ",out_balance_ok AS balance_ok"
    111            ",out_zombie_bad AS zombie_required"
    112            ",out_nonce_reuse AS nonce_reuse"
    113            ",out_idempotent AS found"
    114            ",out_noreveal_index AS noreveal_index"
    115            ",out_coin_balance AS coin_balance"
    116            " FROM exchange_do_refresh"
    117            " ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13,$14,$15,$16,$17);");
    118   qs = GNUNET_PQ_eval_prepared_singleton_select (pg->conn,
    119                                                  "do_refresh",
    120                                                  params,
    121                                                  rs);
    122   GNUNET_PQ_cleanup_query_params_closures (params);
    123   if (0 > qs)
    124   {
    125     GNUNET_break (0);
    126     return qs;
    127   }
    128   if (! coin_found)
    129   {
    130     return GNUNET_DB_STATUS_SUCCESS_NO_RESULTS;
    131   }
    132   if (*found && no_noreveal_index)
    133   {
    134     GNUNET_break (0);
    135     return GNUNET_DB_STATUS_HARD_ERROR;
    136   }
    137   if (! (*balance_ok) && no_coin_balance)
    138   {
    139     GNUNET_break (0);
    140     return GNUNET_DB_STATUS_HARD_ERROR;
    141   }
    142   return qs;
    143 }