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