pg_ensure_coin_known.c (5776B)
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_ensure_coin_known.c 18 * @brief Implementation of the ensure_coin_known 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_exchangedb_plugin.h" 25 #include "taler/taler_pq_lib.h" 26 #include "pg_ensure_coin_known.h" 27 #include "pg_helper.h" 28 29 30 enum TALER_EXCHANGEDB_CoinKnownStatus 31 TEH_PG_ensure_coin_known (void *cls, 32 const struct TALER_CoinPublicInfo *coin, 33 uint64_t *known_coin_id, 34 struct TALER_DenominationHashP *denom_hash, 35 struct TALER_AgeCommitmentHashP *h_age_commitment) 36 { 37 struct PostgresClosure *pg = cls; 38 enum GNUNET_DB_QueryStatus qs; 39 bool existed; 40 bool no_denom_pub_hash; 41 bool no_age_commitment_hash; 42 struct GNUNET_PQ_QueryParam params[] = { 43 GNUNET_PQ_query_param_auto_from_type (&coin->coin_pub), 44 GNUNET_PQ_query_param_auto_from_type (&coin->denom_pub_hash), 45 coin->no_age_commitment 46 ? GNUNET_PQ_query_param_null () 47 : GNUNET_PQ_query_param_auto_from_type (&coin->h_age_commitment), 48 TALER_PQ_query_param_denom_sig (&coin->denom_sig), 49 GNUNET_PQ_query_param_end 50 }; 51 struct GNUNET_PQ_ResultSpec rs[] = { 52 GNUNET_PQ_result_spec_bool ("existed", 53 &existed), 54 GNUNET_PQ_result_spec_uint64 ("known_coin_id", 55 known_coin_id), 56 GNUNET_PQ_result_spec_allow_null ( 57 GNUNET_PQ_result_spec_auto_from_type ("denom_pub_hash", 58 denom_hash), 59 &no_denom_pub_hash), 60 GNUNET_PQ_result_spec_allow_null ( 61 GNUNET_PQ_result_spec_auto_from_type ("age_commitment_hash", 62 h_age_commitment), 63 &no_age_commitment_hash), 64 GNUNET_PQ_result_spec_end 65 }; 66 67 /* 68 See also: 69 https://stackoverflow.com/questions/34708509/how-to-use-returning-with-on-conflict-in-postgresql/37543015#37543015 70 */ 71 PREPARE (pg, 72 "insert_known_coin", 73 "WITH dd" 74 " (denominations_serial" 75 " ,coin" 76 " ) AS (" 77 " SELECT " 78 " denominations_serial" 79 " ,coin" 80 " FROM denominations" 81 " WHERE denom_pub_hash=$2" 82 " ), input_rows" 83 " (coin_pub) AS (" 84 " VALUES ($1::BYTEA)" 85 " ), ins AS (" 86 " INSERT INTO known_coins " 87 " (coin_pub" 88 " ,denominations_serial" 89 " ,age_commitment_hash" 90 " ,denom_sig" 91 " ,remaining" 92 " ) SELECT " 93 " $1" 94 " ,denominations_serial" 95 " ,$3" 96 " ,$4" 97 " ,coin" 98 " FROM dd" 99 " ON CONFLICT DO NOTHING" /* CONFLICT on (coin_pub) */ 100 " RETURNING " 101 " known_coin_id" 102 " ) " 103 "SELECT " 104 " FALSE AS existed" 105 " ,known_coin_id" 106 " ,NULL AS denom_pub_hash" 107 " ,NULL AS age_commitment_hash" 108 " FROM ins " 109 "UNION ALL " 110 "SELECT " 111 " TRUE AS existed" 112 " ,known_coin_id" 113 " ,denom_pub_hash" 114 " ,kc.age_commitment_hash" 115 " FROM input_rows" 116 " JOIN known_coins kc USING (coin_pub)" 117 " JOIN denominations USING (denominations_serial)" 118 " LIMIT 1"); 119 qs = GNUNET_PQ_eval_prepared_singleton_select (pg->conn, 120 "insert_known_coin", 121 params, 122 rs); 123 switch (qs) 124 { 125 case GNUNET_DB_STATUS_HARD_ERROR: 126 GNUNET_break (0); 127 return TALER_EXCHANGEDB_CKS_HARD_FAIL; 128 case GNUNET_DB_STATUS_SOFT_ERROR: 129 return TALER_EXCHANGEDB_CKS_SOFT_FAIL; 130 case GNUNET_DB_STATUS_SUCCESS_NO_RESULTS: 131 GNUNET_break (0); /* should be impossible */ 132 return TALER_EXCHANGEDB_CKS_HARD_FAIL; 133 case GNUNET_DB_STATUS_SUCCESS_ONE_RESULT: 134 if (! existed) 135 return TALER_EXCHANGEDB_CKS_ADDED; 136 break; /* continued below */ 137 } 138 139 if ( (! no_denom_pub_hash) && 140 (0 != GNUNET_memcmp (denom_hash, 141 &coin->denom_pub_hash)) ) 142 { 143 GNUNET_break_op (0); 144 return TALER_EXCHANGEDB_CKS_DENOM_CONFLICT; 145 } 146 147 if (no_age_commitment_hash != coin->no_age_commitment) 148 { 149 if (no_age_commitment_hash) 150 { 151 GNUNET_break_op (0); 152 return TALER_EXCHANGEDB_CKS_AGE_CONFLICT_EXPECTED_NULL; 153 } 154 else 155 { 156 GNUNET_break_op (0); 157 return TALER_EXCHANGEDB_CKS_AGE_CONFLICT_EXPECTED_NON_NULL; 158 } 159 } 160 else if ( (! no_age_commitment_hash) && 161 (0 != GNUNET_memcmp (h_age_commitment, 162 &coin->h_age_commitment)) ) 163 { 164 GNUNET_break_op (0); 165 return TALER_EXCHANGEDB_CKS_AGE_CONFLICT_VALUE_DIFFERS; 166 } 167 168 return TALER_EXCHANGEDB_CKS_PRESENT; 169 }