donau

Donation authority for GNU Taler (experimental)
Log | Files | Refs | Submodules | README | LICENSE

insert_donation_unit.c (3274B)


      1 /*
      2    This file is part of TALER
      3    Copyright (C) 2024 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 src/donaudb/insert_donation_unit.c
     18  * @brief Implementation of the insert_donation_unit function for Postgres
     19  * @author Johannes Casaburi
     20  */
     21 #include <donau_config.h>
     22 #include <taler/taler_error_codes.h>
     23 #include <taler/taler_dbevents.h>
     24 #include <taler/taler_pq_lib.h>
     25 #include "insert_donation_unit.h"
     26 #include "helper.h"
     27 #include "donau_pq_lib.h"
     28 
     29 
     30 enum GNUNET_DB_QueryStatus
     31 DONAUDB_insert_donation_unit (struct DONAUDB_PostgresContext *ctx,
     32                               const struct DONAU_DonationUnitHashP *
     33                               h_donation_unit_pub,
     34                               const struct DONAU_DonationUnitPublicKey *
     35                               donation_unit_pub,
     36                               const uint64_t validity_year,
     37                               const struct TALER_Amount *value)
     38 {
     39   struct GNUNET_PQ_QueryParam iparams[] = {
     40     GNUNET_PQ_query_param_auto_from_type (h_donation_unit_pub),
     41     DONAU_PQ_query_param_donation_unit_pub (donation_unit_pub),
     42     GNUNET_PQ_query_param_uint64 (&validity_year),
     43     TALER_PQ_query_param_amount (ctx->conn,
     44                                  value),
     45     GNUNET_PQ_query_param_end
     46   };
     47 
     48   /* A donation_units row is a historical fact, not a mirror of the current
     49      configuration: receipts_submitted references h_donation_unit_pub and
     50      DONAUDB_get_receipts_submitted_total() sums donation_units.value over
     51      those references.  Rewriting validity_year or value here would therefore
     52      retroactively change donation statements we have already issued under
     53      that key -- so an existing row is never touched.  The security module
     54      re-announces every key on each restart, and h_donation_unit_pub covers
     55      the key material only, so this conflict is the normal case, not an
     56      error.  An operator who needs a different year or value must have the
     57      security module produce a *new* key; the schema deliberately has no
     58      UNIQUE constraint on (validity_year, value), so the new unit coexists
     59      with the old one, which stays valid for the signatures already made
     60      with it. */
     61   PREPARE (ctx,
     62            "insert_donation_unit",
     63            "INSERT INTO donation_units "
     64            "(h_donation_unit_pub"
     65            ",donation_unit_pub"
     66            ",validity_year"
     67            ",value"
     68            ") VALUES ($1, $2, $3, $4)"
     69            " ON CONFLICT (h_donation_unit_pub) DO NOTHING;");
     70   return GNUNET_PQ_eval_prepared_non_select (ctx->conn,
     71                                              "insert_donation_unit",
     72                                              iparams);
     73 }