exchange

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

pg_insert_purse_request.c (4743B)


      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/pg_insert_purse_request.c
     18  * @brief Implementation of the insert_purse_request 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_purse_request.h"
     26 #include "pg_get_purse_request.h"
     27 #include "pg_helper.h"
     28 
     29 
     30 enum GNUNET_DB_QueryStatus
     31 TEH_PG_insert_purse_request (
     32   void *cls,
     33   const struct TALER_PurseContractPublicKeyP *purse_pub,
     34   const struct TALER_PurseMergePublicKeyP *merge_pub,
     35   struct GNUNET_TIME_Timestamp purse_expiration,
     36   const struct TALER_PrivateContractHashP *h_contract_terms,
     37   uint32_t age_limit,
     38   enum TALER_WalletAccountMergeFlags flags,
     39   const struct TALER_Amount *purse_fee,
     40   const struct TALER_Amount *amount,
     41   const struct TALER_PurseContractSignatureP *purse_sig,
     42   bool *in_conflict)
     43 {
     44   struct PostgresClosure *pg = cls;
     45   enum GNUNET_DB_QueryStatus qs;
     46   struct GNUNET_TIME_Timestamp now = GNUNET_TIME_timestamp_get ();
     47   uint32_t flags32 = (uint32_t) flags;
     48   bool in_reserve_quota = (TALER_WAMF_MODE_CREATE_FROM_PURSE_QUOTA
     49                            == (flags & TALER_WAMF_MERGE_MODE_MASK));
     50   struct GNUNET_PQ_QueryParam params[] = {
     51     GNUNET_PQ_query_param_auto_from_type (purse_pub),
     52     GNUNET_PQ_query_param_auto_from_type (merge_pub),
     53     GNUNET_PQ_query_param_timestamp (&now),
     54     GNUNET_PQ_query_param_timestamp (&purse_expiration),
     55     GNUNET_PQ_query_param_auto_from_type (h_contract_terms),
     56     GNUNET_PQ_query_param_uint32 (&age_limit),
     57     GNUNET_PQ_query_param_uint32 (&flags32),
     58     GNUNET_PQ_query_param_bool (in_reserve_quota),
     59     TALER_PQ_query_param_amount (pg->conn,
     60                                  amount),
     61     TALER_PQ_query_param_amount (pg->conn,
     62                                  purse_fee),
     63     GNUNET_PQ_query_param_auto_from_type (purse_sig),
     64     GNUNET_PQ_query_param_end
     65   };
     66 
     67   *in_conflict = false;
     68   PREPARE (pg,
     69            "insert_purse_request",
     70            "INSERT INTO purse_requests"
     71            "  (purse_pub"
     72            "  ,merge_pub"
     73            "  ,purse_creation"
     74            "  ,purse_expiration"
     75            "  ,h_contract_terms"
     76            "  ,age_limit"
     77            "  ,flags"
     78            "  ,in_reserve_quota"
     79            "  ,amount_with_fee"
     80            "  ,purse_fee"
     81            "  ,purse_sig"
     82            "  ) VALUES "
     83            "  ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11)"
     84            "  ON CONFLICT DO NOTHING;");
     85   qs = GNUNET_PQ_eval_prepared_non_select (pg->conn,
     86                                            "insert_purse_request",
     87                                            params);
     88   if (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS != qs)
     89     return qs;
     90   {
     91     struct TALER_PurseMergePublicKeyP merge_pub2;
     92     struct GNUNET_TIME_Timestamp purse_expiration2;
     93     struct TALER_PrivateContractHashP h_contract_terms2;
     94     uint32_t age_limit2;
     95     struct TALER_Amount amount2;
     96     struct TALER_Amount balance;
     97     struct TALER_PurseContractSignatureP purse_sig2;
     98 
     99     qs = TEH_PG_get_purse_request (pg,
    100                                    purse_pub,
    101                                    &merge_pub2,
    102                                    &purse_expiration2,
    103                                    &h_contract_terms2,
    104                                    &age_limit2,
    105                                    &amount2,
    106                                    &balance,
    107                                    &purse_sig2);
    108     if (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT != qs)
    109     {
    110       GNUNET_break (0);
    111       return GNUNET_DB_STATUS_HARD_ERROR;
    112     }
    113     if ( (age_limit2 == age_limit) &&
    114          (0 == TALER_amount_cmp (amount,
    115                                  &amount2)) &&
    116          (0 == GNUNET_memcmp (&h_contract_terms2,
    117                               h_contract_terms)) &&
    118          (0 == GNUNET_memcmp (&merge_pub2,
    119                               merge_pub)) )
    120     {
    121       return GNUNET_DB_STATUS_SUCCESS_NO_RESULTS;
    122     }
    123     *in_conflict = true;
    124     return GNUNET_DB_STATUS_SUCCESS_ONE_RESULT;
    125   }
    126 }