fix documentation and minor issues in DB tools

This commit is contained in:
Christian Grothoff 2015-03-16 18:19:05 +01:00
parent b3e076b10a
commit 293b4018d1
2 changed files with 180 additions and 87 deletions

View File

@ -1,6 +1,6 @@
/* /*
This file is part of TALER This file is part of TALER
Copyright (C) 2014 Christian Grothoff (and other contributing authors) Copyright (C) 2014, 2015 Christian Grothoff (and other contributing authors)
TALER is free software; you can redistribute it and/or modify it under the TALER is free software; you can redistribute it and/or modify it under the
terms of the GNU General Public License as published by the Free Software terms of the GNU General Public License as published by the Free Software
@ -14,7 +14,7 @@
TALER; see the file COPYING. If not, If not, see <http://www.gnu.org/licenses/> TALER; see the file COPYING. If not, If not, see <http://www.gnu.org/licenses/>
*/ */
/** /**
* @file taler-mint-dbinit.c * @file mint/taler-mint-dbinit.c
* @brief Create tables for the mint database. * @brief Create tables for the mint database.
* @author Florian Dold * @author Florian Dold
*/ */
@ -24,74 +24,96 @@
#include "taler_util.h" #include "taler_util.h"
#include "mint_db.h" #include "mint_db.h"
#define break_db_err(result) do { \
GNUNET_break(0); \
GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Database failure: %s\n", PQresultErrorMessage (result)); \
PQclear (result); \
} while (0)
/**
* Mint directory with the keys.
*/
static char *mint_base_dir; static char *mint_base_dir;
/**
* Our configuration.
*/
static struct GNUNET_CONFIGURATION_Handle *cfg; static struct GNUNET_CONFIGURATION_Handle *cfg;
/**
* Database connection handle.
*/
static PGconn *db_conn; static PGconn *db_conn;
static char *TALER_MINT_db_connection_cfg_str;
/** /**
* The main function of the serve tool * The main function of the database initialization tool.
* Used to initialize the Taler Mint's database.
* *
* @param argc number of arguments from the command line * @param argc number of arguments from the command line
* @param argv command line arguments * @param argv command line arguments
* @return 0 ok, 1 on error * @return 0 ok, 1 on error
*/ */
int int
main (int argc, char *const *argv) main (int argc,
char *const *argv)
{ {
static const struct GNUNET_GETOPT_CommandLineOption options[] = { static const struct GNUNET_GETOPT_CommandLineOption options[] = {
GNUNET_GETOPT_OPTION_HELP ("gnunet-mint-keyup OPTIONS"), GNUNET_GETOPT_OPTION_HELP ("gnunet-mint-dbinit OPTIONS"),
{'d', "mint-dir", "DIR", {'d', "mint-dir", "DIR",
"mint directory", 1, "mint directory", 1,
&GNUNET_GETOPT_set_filename, &mint_base_dir}, &GNUNET_GETOPT_set_filename, &mint_base_dir},
GNUNET_GETOPT_OPTION_END GNUNET_GETOPT_OPTION_END
}; };
char *db_connection_cfg_str;
if (GNUNET_GETOPT_run ("taler-mint-serve", options, argc, argv) < 0) if (GNUNET_GETOPT_run ("taler-mint-dbinit",
options,
argc, argv) < 0)
return 1; return 1;
GNUNET_assert (GNUNET_OK == GNUNET_log_setup ("taler-mint-dbinit", "INFO", NULL)); GNUNET_assert (GNUNET_OK ==
GNUNET_log_setup ("taler-mint-dbinit",
"INFO",
NULL));
if (NULL == mint_base_dir) if (NULL == mint_base_dir)
{ {
fprintf (stderr, "Mint base directory not given.\n"); fprintf (stderr,
"Mint base directory not given.\n");
return 1; return 1;
} }
cfg = TALER_config_load (mint_base_dir); cfg = TALER_config_load (mint_base_dir);
if (NULL == cfg) if (NULL == cfg)
{ {
fprintf (stderr, "Can't load mint configuration.\n"); fprintf (stderr,
"Failed to load mint configuration.\n");
return 1; return 1;
} }
if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (cfg, "mint", "db", &TALER_MINT_db_connection_cfg_str)) if (GNUNET_OK !=
GNUNET_CONFIGURATION_get_value_string (cfg,
"mint",
"db",
&db_connection_cfg_str))
{ {
fprintf (stderr, "Configuration 'mint.db' not found.\n"); fprintf (stderr,
return 42; "Configuration 'mint.db' not found.\n");
return 1;
} }
db_conn = PQconnectdb (TALER_MINT_db_connection_cfg_str); db_conn = PQconnectdb (db_connection_cfg_str);
if (CONNECTION_OK != PQstatus (db_conn)) if (CONNECTION_OK != PQstatus (db_conn))
{ {
fprintf (stderr, "Database connection failed: %s\n", PQerrorMessage (db_conn)); fprintf (stderr,
"Database connection failed: %s\n",
PQerrorMessage (db_conn));
free (db_connection_cfg_str);
return 1; return 1;
} }
free (db_connection_cfg_str);
if (GNUNET_OK != TALER_MINT_DB_create_tables (GNUNET_NO)) if (GNUNET_OK != TALER_MINT_DB_create_tables (GNUNET_NO))
{ {
fprintf (stderr, "Failed to initialize database.\n"); fprintf (stderr,
"Failed to initialize database.\n");
return 1; return 1;
} }
return 0; return 0;
} }
/* end of taler-mint-dbinit.c */

View File

@ -1,6 +1,6 @@
/* /*
This file is part of TALER This file is part of TALER
Copyright (C) 2014 Christian Grothoff (and other contributing authors) Copyright (C) 2014, 2015 Christian Grothoff (and other contributing authors)
TALER is free software; you can redistribute it and/or modify it under the TALER is free software; you can redistribute it and/or modify it under the
terms of the GNU General Public License as published by the Free Software terms of the GNU General Public License as published by the Free Software
@ -13,14 +13,12 @@
You should have received a copy of the GNU General Public License along with You should have received a copy of the GNU General Public License along with
TALER; see the file COPYING. If not, If not, see <http://www.gnu.org/licenses/> TALER; see the file COPYING. If not, If not, see <http://www.gnu.org/licenses/>
*/ */
/** /**
* @file taler-mint-reservemod.c * @file taler-mint-reservemod.c
* @brief Modify reserves. * @brief Modify reserves. Allows manipulation of reserve balances for testing.
* @author Florian Dold * @author Florian Dold
* @author Benedikt Mueller * @author Benedikt Mueller
*/ */
#include "platform.h" #include "platform.h"
#include <gnunet/gnunet_util_lib.h> #include <gnunet/gnunet_util_lib.h>
#include <libpq-fe.h> #include <libpq-fe.h>
@ -29,47 +27,74 @@
#include "mint_db.h" #include "mint_db.h"
#include "db_pq.h" #include "db_pq.h"
char *mintdir;
/**
* Director of the mint, containing the keys.
*/
static char *mintdir;
/**
* Public key of the reserve to manipulate.
*/
static struct GNUNET_CRYPTO_EddsaPublicKey *reserve_pub; static struct GNUNET_CRYPTO_EddsaPublicKey *reserve_pub;
struct GNUNET_CONFIGURATION_Handle *cfg; /**
* Handle to the mint's configuration
*/
static struct GNUNET_CONFIGURATION_Handle *cfg;
/**
* Database connection handle.
*/
static PGconn *db_conn; static PGconn *db_conn;
/** /**
* Create a new or add to existing reserve. * Create a new or add to existing reserve. Fails if currencies do
* Fails if currencies do not match. * not match.
* *
* @param denom denomination to add * @param denom denomination to add
* * @return #GNUNET_OK on success,
* @return ... * #GNUNET_SYSERR on error
*/ */
static int static int
reservemod_add (struct TALER_Amount denom) reservemod_add (struct TALER_Amount denom)
{ {
PGresult *result; PGresult *result;
{ const void *param_values[] = {
const void *param_values[] = { reserve_pub }; reserve_pub
int param_lengths[] = {sizeof(struct GNUNET_CRYPTO_EddsaPublicKey)}; };
int param_formats[] = {1}; int param_lengths[] = {
result = PQexecParams (db_conn, sizeof(struct GNUNET_CRYPTO_EddsaPublicKey)
"select balance_value, balance_fraction, balance_currency from reserves where reserve_pub=$1 limit 1;", };
1, NULL, (const char * const *) param_values, param_lengths, param_formats, 1); int param_formats[] = {
} 1
};
struct TALER_Amount old_denom;
struct TALER_Amount new_denom;
struct TALER_AmountNBO new_denom_nbo;
result = PQexecParams (db_conn,
"SELECT balance_value, balance_fraction, balance_currency"
" FROM reserves"
" WHERE reserve_pub=$1"
" LIMIT 1;",
1,
NULL,
(const char * const *) param_values,
param_lengths,
param_formats,
1);
if (PGRES_TUPLES_OK != PQresultStatus (result)) if (PGRES_TUPLES_OK != PQresultStatus (result))
{ {
fprintf (stderr, "Select failed: %s\n", PQresultErrorMessage (result)); fprintf (stderr,
"Select failed: %s\n",
PQresultErrorMessage (result));
return GNUNET_SYSERR; return GNUNET_SYSERR;
} }
if (0 == PQntuples (result)) if (0 == PQntuples (result))
{ {
struct GNUNET_TIME_AbsoluteNBO exnbo; struct GNUNET_TIME_AbsoluteNBO exnbo;
exnbo = GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_add ( GNUNET_TIME_absolute_get (), GNUNET_TIME_UNIT_YEARS));
uint32_t value = htonl (denom.value); uint32_t value = htonl (denom.value);
uint32_t fraction = htonl (denom.fraction); uint32_t fraction = htonl (denom.fraction);
const void *param_values[] = { const void *param_values[] = {
@ -77,33 +102,53 @@ reservemod_add (struct TALER_Amount denom)
&value, &value,
&fraction, &fraction,
denom.currency, denom.currency,
&exnbo}; &exnbo
int param_lengths[] = {32, 4, 4, strlen(denom.currency), 8}; };
int param_formats[] = {1, 1, 1, 1, 1}; int param_lengths[] = {
sizeof (struct GNUNET_CRYPTO_EddsaPublicKey),
sizeof (uint32_t),
sizeof (uint32_t),
strlen (denom.currency),
sizeof (struct GNUNET_TIME_AbsoluteNBO)
};
int param_formats[] = {
1, 1, 1, 1, 1
};
exnbo = GNUNET_TIME_absolute_hton (GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_YEARS));
result = PQexecParams (db_conn, result = PQexecParams (db_conn,
"insert into reserves (reserve_pub, balance_value, balance_fraction, balance_currency, " "INSERT INTO reserves (reserve_pub, balance_value, balance_fraction, balance_currency, expiration_date)"
" expiration_date )" " VALUES ($1,$2,$3,$4,$5);",
"values ($1,$2,$3,$4,$5);", 5,
5, NULL, (const char **) param_values, param_lengths, param_formats, 1); NULL,
(const char **) param_values,
param_lengths,
param_formats,
1);
if (PGRES_COMMAND_OK != PQresultStatus (result)) if (PGRES_COMMAND_OK != PQresultStatus (result))
{ {
fprintf (stderr, "Insert failed: %s\n", PQresultErrorMessage (result)); fprintf (stderr,
"Insert failed: %s\n",
PQresultErrorMessage (result));
return GNUNET_SYSERR; return GNUNET_SYSERR;
} }
} }
else else
{ {
struct TALER_Amount old_denom;
struct TALER_Amount new_denom;
struct TALER_AmountNBO new_denom_nbo;
int param_lengths[] = {4, 4, 32};
int param_formats[] = {1, 1, 1};
const void *param_values[] = { const void *param_values[] = {
&new_denom_nbo.value, &new_denom_nbo.value,
&new_denom_nbo.fraction, &new_denom_nbo.fraction,
reserve_pub reserve_pub
}; };
int param_lengths[] = {
sizeof (new_denom_nbo.value),
sizeof (new_denom_nbo.fraction),
sizeof (struct GNUNET_CRYPTO_EddsaPublicKey)
};
int param_formats[] = {
1, 1, 1
};
GNUNET_assert (GNUNET_OK == GNUNET_assert (GNUNET_OK ==
TALER_DB_extract_amount (result, 0, TALER_DB_extract_amount (result, 0,
@ -111,28 +156,36 @@ reservemod_add (struct TALER_Amount denom)
"balance_fraction", "balance_fraction",
"balance_currency", "balance_currency",
&old_denom)); &old_denom));
new_denom = TALER_amount_add (old_denom, denom); new_denom = TALER_amount_add (old_denom,
denom);
new_denom_nbo = TALER_amount_hton (new_denom); new_denom_nbo = TALER_amount_hton (new_denom);
result = PQexecParams (db_conn, result = PQexecParams (db_conn,
"UPDATE reserves" "UPDATE reserves"
"SET balance_value = $1, balance_fraction = $2, " " SET balance_value = $1, balance_fraction = $2, status_sig = NULL, status_sign_pub = NULL"
" status_sig = NULL, status_sign_pub = NULL " " WHERE reserve_pub = $3;",
"WHERE reserve_pub = $3 ", 3,
3, NULL, (const char **) param_values, param_lengths, param_formats, 1); NULL,
(const char **) param_values,
param_lengths,
param_formats,
1);
if (PGRES_COMMAND_OK != PQresultStatus (result)) if (PGRES_COMMAND_OK != PQresultStatus (result))
{ {
fprintf (stderr, "Update failed: %s\n", PQresultErrorMessage (result)); fprintf (stderr,
"Update failed: %s\n",
PQresultErrorMessage (result));
return GNUNET_SYSERR; return GNUNET_SYSERR;
} }
/* FIXME: strcmp!? There must be an API that returns an int! */
if (0 != strcmp ("1", PQcmdTuples (result))) if (0 != strcmp ("1",
PQcmdTuples (result)))
{ {
fprintf (stderr, "Update failed (updated '%s' tupes instead of '1')\n", fprintf (stderr,
"Update failed (updated `%s' tupes instead of '1')\n",
PQcmdTuples (result)); PQcmdTuples (result));
return GNUNET_SYSERR; return GNUNET_SYSERR;
} }
} }
return GNUNET_OK; return GNUNET_OK;
} }
@ -151,7 +204,7 @@ main (int argc, char *const *argv)
static char *reserve_pub_str; static char *reserve_pub_str;
static char *add_str; static char *add_str;
static const struct GNUNET_GETOPT_CommandLineOption options[] = { static const struct GNUNET_GETOPT_CommandLineOption options[] = {
GNUNET_GETOPT_OPTION_HELP ("gnunet-mint-keyup OPTIONS"), GNUNET_GETOPT_OPTION_HELP ("gnunet-mint-reservemod OPTIONS"),
{'d', "mint-dir", "DIR", {'d', "mint-dir", "DIR",
"mint directory with keys to update", 1, "mint directory with keys to update", 1,
&GNUNET_GETOPT_set_filename, &mintdir}, &GNUNET_GETOPT_set_filename, &mintdir},
@ -165,13 +218,19 @@ main (int argc, char *const *argv)
}; };
char *TALER_MINT_db_connection_cfg_str; char *TALER_MINT_db_connection_cfg_str;
GNUNET_assert (GNUNET_OK == GNUNET_log_setup ("taler-mint-keycheck", "WARNING", NULL)); GNUNET_assert (GNUNET_OK ==
GNUNET_log_setup ("taler-mint-reservemod",
"WARNING",
NULL));
if (GNUNET_GETOPT_run ("taler-mint-keyup", options, argc, argv) < 0) if (GNUNET_GETOPT_run ("taler-mint-keyup",
options,
argc, argv) < 0)
return 1; return 1;
if (NULL == mintdir) if (NULL == mintdir)
{ {
fprintf (stderr, "mint directory not given\n"); fprintf (stderr,
"Mint directory not given\n");
return 1; return 1;
} }
@ -183,14 +242,15 @@ main (int argc, char *const *argv)
reserve_pub, reserve_pub,
sizeof (struct GNUNET_CRYPTO_EddsaPublicKey)))) sizeof (struct GNUNET_CRYPTO_EddsaPublicKey))))
{ {
fprintf (stderr, "reserve key invalid\n"); fprintf (stderr,
"Parsing reserve key invalid\n");
return 1; return 1;
} }
cfg = TALER_config_load (mintdir); cfg = TALER_config_load (mintdir);
if (NULL == cfg) if (NULL == cfg)
{ {
fprintf (stderr, "can't load mint configuration\n"); fprintf (stderr,
"Failed to load mint configuration\n");
return 1; return 1;
} }
if (GNUNET_OK != if (GNUNET_OK !=
@ -199,29 +259,40 @@ main (int argc, char *const *argv)
"db", "db",
&TALER_MINT_db_connection_cfg_str)) &TALER_MINT_db_connection_cfg_str))
{ {
fprintf (stderr, "db configuration string not found\n"); fprintf (stderr,
return 42; "Database configuration string not found\n");
return 1;
} }
db_conn = PQconnectdb (TALER_MINT_db_connection_cfg_str); db_conn = PQconnectdb (TALER_MINT_db_connection_cfg_str);
if (CONNECTION_OK != PQstatus (db_conn)) if (CONNECTION_OK != PQstatus (db_conn))
{ {
fprintf (stderr, "db connection failed: %s\n", PQerrorMessage (db_conn)); fprintf (stderr,
"Database connection failed: %s\n",
PQerrorMessage (db_conn));
return 1; return 1;
} }
if (NULL != add_str) if (NULL != add_str)
{ {
struct TALER_Amount add_value; struct TALER_Amount add_value;
if (GNUNET_OK != TALER_string_to_amount (add_str, &add_value))
if (GNUNET_OK !=
TALER_string_to_amount (add_str,
&add_value))
{ {
fprintf (stderr, "could not read value\n"); fprintf (stderr,
"Failed to parse currency amount `%s'\n",
add_str);
return 1; return 1;
} }
if (GNUNET_OK != reservemod_add (add_value)) if (GNUNET_OK !=
reservemod_add (add_value))
{ {
fprintf (stderr, "adding value failed\n"); fprintf (stderr,
"Failed to update reserve.\n");
return 1; return 1;
} }
} }
return 0; return 0;
} }
/* end taler-mint-reservemod.c */