exchange

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

insert_contract.c (3318B)


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