exchange

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

pg_insert_contract.c (3337B)


      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_contract.c
     18  * @brief Implementation of the insert_contract 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_contract.h"
     26 #include "pg_select_contract_by_purse.h"
     27 #include "pg_helper.h"
     28 
     29 enum GNUNET_DB_QueryStatus
     30 TEH_PG_insert_contract (
     31   void *cls,
     32   const struct TALER_PurseContractPublicKeyP *purse_pub,
     33   const struct TALER_EncryptedContract *econtract,
     34   bool *in_conflict)
     35 {
     36   struct PostgresClosure *pg = cls;
     37   enum GNUNET_DB_QueryStatus qs;
     38   struct GNUNET_PQ_QueryParam params[] = {
     39     GNUNET_PQ_query_param_auto_from_type (purse_pub),
     40     GNUNET_PQ_query_param_auto_from_type (&econtract->contract_pub),
     41     GNUNET_PQ_query_param_fixed_size (econtract->econtract,
     42                                       econtract->econtract_size),
     43     GNUNET_PQ_query_param_auto_from_type (&econtract->econtract_sig),
     44     GNUNET_PQ_query_param_end
     45   };
     46 
     47   *in_conflict = false;
     48   /* Used in #postgres_insert_contract() */
     49   PREPARE (pg,
     50            "insert_contract",
     51            "INSERT INTO contracts"
     52            "  (purse_pub"
     53            "  ,pub_ckey"
     54            "  ,e_contract"
     55            "  ,contract_sig"
     56            "  ,purse_expiration"
     57            "  ) SELECT "
     58            "  $1, $2, $3, $4, purse_expiration"
     59            "  FROM purse_requests"
     60            "  WHERE purse_pub=$1"
     61            "  ON CONFLICT DO NOTHING;");
     62   qs = GNUNET_PQ_eval_prepared_non_select (pg->conn,
     63                                            "insert_contract",
     64                                            params);
     65   if (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS != qs)
     66     return qs;
     67   {
     68     struct TALER_EncryptedContract econtract2;
     69 
     70     qs = TEH_PG_select_contract_by_purse (pg,
     71                                           purse_pub,
     72                                           &econtract2);
     73     if (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT != qs)
     74     {
     75       GNUNET_break (0);
     76       return GNUNET_DB_STATUS_HARD_ERROR;
     77     }
     78     if ( (0 == GNUNET_memcmp (&econtract->contract_pub,
     79                               &econtract2.contract_pub)) &&
     80          (econtract2.econtract_size ==
     81           econtract->econtract_size) &&
     82          (0 == memcmp (econtract2.econtract,
     83                        econtract->econtract,
     84                        econtract->econtract_size)) )
     85     {
     86       GNUNET_free (econtract2.econtract);
     87       return GNUNET_DB_STATUS_SUCCESS_NO_RESULTS;
     88     }
     89     GNUNET_free (econtract2.econtract);
     90     *in_conflict = true;
     91     return GNUNET_DB_STATUS_SUCCESS_ONE_RESULT;
     92   }
     93 }