Line data Source code
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 20 : 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 20 : struct GNUNET_PQ_QueryParam params[] = {
35 20 : GNUNET_PQ_query_param_auto_from_type (purse_pub),
36 20 : GNUNET_PQ_query_param_auto_from_type (&econtract->contract_pub),
37 20 : GNUNET_PQ_query_param_fixed_size (econtract->econtract,
38 20 : econtract->econtract_size),
39 20 : GNUNET_PQ_query_param_auto_from_type (&econtract->econtract_sig),
40 : GNUNET_PQ_query_param_end
41 : };
42 :
43 20 : *in_conflict = false;
44 : /* Used in #postgres_insert_contract() */
45 20 : 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 20 : qs = GNUNET_PQ_eval_prepared_non_select (pg->conn,
59 : "insert_contract",
60 : params);
61 20 : if (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS != qs)
62 20 : return qs;
63 : {
64 : struct TALER_EncryptedContract econtract2;
65 :
66 0 : qs = TALER_TALER_EXCHANGEDB_select_contract_by_purse (pg,
67 : purse_pub,
68 : &econtract2);
69 0 : if (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT != qs)
70 : {
71 0 : GNUNET_break (0);
72 0 : return GNUNET_DB_STATUS_HARD_ERROR;
73 : }
74 0 : if ( (0 == GNUNET_memcmp (&econtract->contract_pub,
75 0 : &econtract2.contract_pub)) &&
76 0 : (econtract2.econtract_size ==
77 0 : econtract->econtract_size) &&
78 0 : (0 == memcmp (econtract2.econtract,
79 0 : econtract->econtract,
80 0 : econtract->econtract_size)) )
81 : {
82 0 : GNUNET_free (econtract2.econtract);
83 0 : return GNUNET_DB_STATUS_SUCCESS_NO_RESULTS;
84 : }
85 0 : GNUNET_free (econtract2.econtract);
86 0 : *in_conflict = true;
87 0 : return GNUNET_DB_STATUS_SUCCESS_ONE_RESULT;
88 : }
89 : }
|