anastasis

Credential backup and recovery protocol and service
Log | Files | Refs | Submodules | README | LICENSE

anastasis-db_pg.h (3590B)


      1 /*
      2   This file is part of Anastasis
      3   Copyright (C) 2020-2022 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/anastasis-db_pg.h
     18  * @brief internal shared state for the Anastasis postgres database
     19  * @author Christian Grothoff
     20  */
     21 #ifndef ANASTASIS_DB_PG_H
     22 #define ANASTASIS_DB_PG_H
     23 
     24 #include "platform.h"
     25 #include "anastasis_util_lib.h"
     26 #include "anastasis_database_lib.h"
     27 #include <gnunet/gnunet_util_lib.h>
     28 #include <gnunet/gnunet_db_lib.h>
     29 #include <gnunet/gnunet_pq_lib.h>
     30 #include <taler/taler_pq_lib.h>
     31 
     32 /**
     33  * How long do we keep transient accounts open (those that have
     34  * not been paid at all, but are awaiting payment). This puts
     35  * a cap on how long users have to make a payment after a payment
     36  * request was generated.
     37  */
     38 #define TRANSIENT_LIFETIME GNUNET_TIME_UNIT_WEEKS
     39 
     40 /**
     41  * How often do we re-try if we run into a DB serialization error?
     42  */
     43 #define MAX_RETRIES 3
     44 
     45 
     46 /**
     47  * Shared database state for the Anastasis postgres backend.
     48  */
     49 struct PostgresClosure
     50 {
     51 
     52   /**
     53    * Postgres connection handle.
     54    */
     55   struct GNUNET_PQ_Context *conn;
     56 
     57   /**
     58    * Underlying configuration.
     59    */
     60   const struct GNUNET_CONFIGURATION_Handle *cfg;
     61 
     62   /**
     63    * Name of the currently active transaction, NULL if none is active.
     64    */
     65   const char *transaction_name;
     66 
     67   /**
     68    * Currency we accept payments in.
     69    */
     70   char *currency;
     71 
     72   /**
     73    * Prepared statements have been initialized in this edition.
     74    */
     75   uint64_t prep_gen;
     76 };
     77 
     78 
     79 /**
     80  * Global database state, initialized by ANASTASIS_DB_init().
     81  */
     82 extern struct PostgresClosure *pg;
     83 
     84 /**
     85  * Prepares SQL statement @a sql under @a name for
     86  * connection @a pg once.
     87  * Returns with #GNUNET_DB_STATUS_HARD_ERROR on failure.
     88  *
     89  * @param name name to prepare the statement under
     90  * @param sql actual SQL text
     91  */
     92 #define PREPARE(name,sql)                               \
     93         do {                                            \
     94           static unsigned long long gen;                \
     95                                                   \
     96           if (gen < pg->prep_gen)                       \
     97           {                                             \
     98             struct GNUNET_PQ_PreparedStatement ps[] = { \
     99               GNUNET_PQ_make_prepare (name, sql),       \
    100               GNUNET_PQ_PREPARED_STATEMENT_END          \
    101             };                                          \
    102                                                   \
    103             if (GNUNET_OK !=                            \
    104                 GNUNET_PQ_prepare_statements (pg->conn, \
    105                                               ps))      \
    106             {                                           \
    107               GNUNET_break (0);                         \
    108               return GNUNET_DB_STATUS_HARD_ERROR;       \
    109             }                                           \
    110             gen = pg->prep_gen;                         \
    111           }                                             \
    112         } while (0)
    113 
    114 
    115 #endif