do_update_account_lifetime.c (3798B)
1 /* 2 This file is part of Anastasis 3 Copyright (C) 2020-2026 Anastasis SARL 4 5 Anastasis is free software; you can redistribute it and/or modify it under the 6 terms of the GNU Affero General Public License as published by the Free Software 7 Foundation; either version 3, or (at your option) any later version. 8 9 Anastasis 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 Affero General Public License for more details. 12 13 You should have received a copy of the GNU Affero General Public License along with 14 Anastasis; see the file COPYING.GPL. If not, see <http://www.gnu.org/licenses/> 15 */ 16 /** 17 * @file stasis/do_update_account_lifetime.c 18 * @brief Anastasis database: do update account lifetime 19 * @author Christian Grothoff 20 */ 21 #include "platform.h" 22 #include "anastasis-db_pg.h" 23 #include "anastasis/anastasis-database/do_update_account_lifetime.h" 24 #include "anastasis/anastasis-database/preflight.h" 25 #include <taler/taler_pq_lib.h> 26 27 28 enum GNUNET_DB_QueryStatus 29 ANASTASIS_DB_do_update_account_lifetime ( 30 const struct ANASTASIS_CRYPTO_AccountPublicKeyP *account_pub, 31 const struct ANASTASIS_PaymentSecretP *payment_identifier, 32 struct GNUNET_TIME_Timestamp min_expiration, 33 struct GNUNET_TIME_Relative lifetime, 34 struct GNUNET_TIME_Timestamp *paid_until) 35 { 36 struct GNUNET_PQ_QueryParam params[] = { 37 GNUNET_PQ_query_param_auto_from_type (account_pub), 38 GNUNET_PQ_query_param_auto_from_type (payment_identifier), 39 GNUNET_PQ_query_param_timestamp (&min_expiration), 40 GNUNET_PQ_query_param_relative_time (&lifetime), 41 GNUNET_PQ_query_param_end 42 }; 43 bool payment_credited; 44 struct GNUNET_PQ_ResultSpec rs[] = { 45 GNUNET_PQ_result_spec_bool ("payment_credited", 46 &payment_credited), 47 GNUNET_PQ_result_spec_timestamp ("paid_until", 48 paid_until), 49 GNUNET_PQ_result_spec_end 50 }; 51 enum GNUNET_DB_QueryStatus qs; 52 53 /* Expiration dates are whole seconds; make sure adding the increment 54 cannot break that (GNUNET_PQ_result_spec_timestamp() insists on it). */ 55 if (! GNUNET_TIME_relative_is_forever (lifetime)) 56 lifetime.rel_value_us 57 -= lifetime.rel_value_us % GNUNET_TIME_UNIT_SECONDS.rel_value_us; 58 *paid_until = GNUNET_TIME_UNIT_ZERO_TS; 59 GNUNET_break (GNUNET_OK == 60 ANASTASIS_DB_preflight ()); 61 PREPARE ("do_update_account_lifetime", 62 "SELECT" 63 " out_payment_credited AS payment_credited" 64 ",out_paid_until AS paid_until" 65 " FROM anastasis_do_update_account_lifetime" 66 " ($1, $2, $3, $4);"); 67 qs = GNUNET_PQ_eval_prepared_singleton_select ( 68 pg->conn, 69 "do_update_account_lifetime", 70 params, 71 rs); 72 if (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT != qs) 73 { 74 /* The stored procedure always returns exactly one row, so anything 75 else is a real database failure. */ 76 GNUNET_break (qs < 0); 77 *paid_until = GNUNET_TIME_UNIT_ZERO_TS; 78 return (qs < 0) 79 ? qs 80 : GNUNET_DB_STATUS_HARD_ERROR; 81 } 82 if (! payment_credited) 83 { 84 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 85 "Payment existed, lifetime of account %s unchanged at %s\n", 86 TALER_B2S (account_pub), 87 GNUNET_TIME_timestamp2s (*paid_until)); 88 return GNUNET_DB_STATUS_SUCCESS_NO_RESULTS; 89 } 90 GNUNET_break (! GNUNET_TIME_absolute_is_never (paid_until->abs_time)); 91 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 92 "Updated lifetime of account %s to %s\n", 93 TALER_B2S (account_pub), 94 GNUNET_TIME_timestamp2s (*paid_until)); 95 return GNUNET_DB_STATUS_SUCCESS_ONE_RESULT; 96 } 97 98 99 /* end of do_update_account_lifetime.c */