exchange

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

pg_insert_reserve_closed.c (4155B)


      1 /*
      2    This file is part of TALER
      3    Copyright (C) 2022, 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  * @file exchangedb/pg_insert_reserve_closed.c
     18  * @brief Implementation of the insert_reserve_closed function for Postgres
     19  * @author Christian Grothoff
     20  */
     21 #include "taler/platform.h"
     22 #include "taler/taler_error_codes.h"
     23 #include "taler/taler_dbevents.h"
     24 #include "taler/taler_pq_lib.h"
     25 #include "pg_insert_reserve_closed.h"
     26 #include "pg_helper.h"
     27 #include "pg_reserves_get.h"
     28 #include "pg_reserves_update.h"
     29 
     30 enum GNUNET_DB_QueryStatus
     31 TEH_PG_insert_reserve_closed (
     32   void *cls,
     33   const struct TALER_ReservePublicKeyP *reserve_pub,
     34   struct GNUNET_TIME_Timestamp execution_date,
     35   const struct TALER_FullPayto receiver_account,
     36   const struct TALER_WireTransferIdentifierRawP *wtid,
     37   const struct TALER_Amount *amount_with_fee,
     38   const struct TALER_Amount *closing_fee,
     39   uint64_t close_request_row)
     40 {
     41   struct PostgresClosure *pg = cls;
     42   struct TALER_EXCHANGEDB_Reserve reserve;
     43   enum GNUNET_DB_QueryStatus qs;
     44   struct TALER_FullPaytoHashP h_payto;
     45 
     46   TALER_full_payto_hash (receiver_account,
     47                          &h_payto);
     48   {
     49     struct GNUNET_PQ_QueryParam params[] = {
     50       GNUNET_PQ_query_param_auto_from_type (reserve_pub),
     51       GNUNET_PQ_query_param_timestamp (&execution_date),
     52       GNUNET_PQ_query_param_auto_from_type (wtid),
     53       GNUNET_PQ_query_param_auto_from_type (&h_payto),
     54       TALER_PQ_query_param_amount (pg->conn,
     55                                    amount_with_fee),
     56       TALER_PQ_query_param_amount (pg->conn,
     57                                    closing_fee),
     58       GNUNET_PQ_query_param_uint64 (&close_request_row),
     59       GNUNET_PQ_query_param_end
     60     };
     61 
     62     PREPARE (pg,
     63              "reserves_close_insert",
     64              "INSERT INTO reserves_close "
     65              "(reserve_pub"
     66              ",execution_date"
     67              ",wtid"
     68              ",wire_target_h_payto"
     69              ",amount"
     70              ",closing_fee"
     71              ",close_request_row"
     72              ") VALUES ($1, $2, $3, $4, $5, $6, $7);");
     73 
     74     qs = GNUNET_PQ_eval_prepared_non_select (pg->conn,
     75                                              "reserves_close_insert",
     76                                              params);
     77   }
     78   if (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT != qs)
     79     return qs;
     80 
     81   /* update reserve balance */
     82   reserve.pub = *reserve_pub;
     83   if (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT !=
     84       (qs = TEH_PG_reserves_get (cls,
     85                                  &reserve)))
     86   {
     87     /* Existence should have been checked before we got here... */
     88     GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs);
     89     if (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS == qs)
     90       qs = GNUNET_DB_STATUS_HARD_ERROR;
     91     return qs;
     92   }
     93   {
     94     enum TALER_AmountArithmeticResult ret;
     95 
     96     ret = TALER_amount_subtract (&reserve.balance,
     97                                  &reserve.balance,
     98                                  amount_with_fee);
     99     if (ret < 0)
    100     {
    101       /* The reserve history was checked to make sure there is enough of a balance
    102          left before we tried this; however, concurrent operations may have changed
    103          the situation by now.  We should re-try the transaction.  */
    104       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
    105                   "Closing of reserve `%s' refused due to balance mismatch. Retrying.\n",
    106                   TALER_B2S (reserve_pub));
    107       return GNUNET_DB_STATUS_HARD_ERROR;
    108     }
    109     GNUNET_break (TALER_AAR_RESULT_ZERO == ret);
    110   }
    111   return TEH_PG_reserves_update (cls,
    112                                  &reserve);
    113 }