remove currency field from databases

This commit is contained in:
Christian Grothoff 2019-08-17 21:35:21 +02:00
parent cbf6281ca2
commit 4bf425c902
No known key found for this signature in database
GPG Key ID: 939E6BE1E29FC3CC
17 changed files with 1608 additions and 2043 deletions

File diff suppressed because it is too large Load Diff

View File

@ -5,3 +5,6 @@ DB = postgres
[auditordb-postgres] [auditordb-postgres]
# Argument for Postgres for how to connect to the database. # Argument for Postgres for how to connect to the database.
CONFIG = "postgres:///talercheck" CONFIG = "postgres:///talercheck"
[taler]
CURRENCY = "EUR"

View File

@ -54,7 +54,7 @@ static int result = -1;
/** /**
* Currency we use. * Currency we use, must match CURRENCY in "test-auditor-db-postgres.conf".
*/ */
#define CURRENCY "EUR" #define CURRENCY "EUR"

View File

@ -60,7 +60,7 @@ sign_account_data (void *cls,
char *json_out; char *json_out;
FILE *out; FILE *out;
int ret; int ret;
if (GNUNET_NO == ai->credit_enabled) if (GNUNET_NO == ai->credit_enabled)
return; return;
if (NULL == ai->wire_response_filename) if (NULL == ai->wire_response_filename)
@ -71,6 +71,7 @@ sign_account_data (void *cls,
global_ret = 1; global_ret = 1;
return; return;
} }
wire = TALER_JSON_exchange_wire_signature_make (ai->payto_url, wire = TALER_JSON_exchange_wire_signature_make (ai->payto_url,
&master_priv); &master_priv);
GNUNET_assert (NULL != wire); GNUNET_assert (NULL != wire);
@ -199,6 +200,9 @@ run (void *cls,
global_ret = 1; global_ret = 1;
return; return;
} }
GNUNET_log (GNUNET_ERROR_TYPE_INFO,
"Signing /wire response with private key matching public key `%s'\n",
masters);
GNUNET_free (masters); GNUNET_free (masters);
TALER_EXCHANGEDB_find_accounts (cfg, TALER_EXCHANGEDB_find_accounts (cfg,
&sign_account_data, &sign_account_data,

View File

@ -684,6 +684,9 @@ exchange_serve_process_config ()
return GNUNET_SYSERR; return GNUNET_SYSERR;
} }
GNUNET_free (TEH_master_public_key_str); GNUNET_free (TEH_master_public_key_str);
GNUNET_log (GNUNET_ERROR_TYPE_INFO,
"Launching exchange with public key `%s'...\n",
GNUNET_p2s (&TEH_master_public_key.eddsa_pub));
if ( (GNUNET_OK != if ( (GNUNET_OK !=
TEH_VALIDATION_init (cfg)) || TEH_VALIDATION_init (cfg)) ||

View File

@ -173,8 +173,9 @@ load_account (void *cls,
&TEH_master_public_key)) &TEH_master_public_key))
{ {
GNUNET_log (GNUNET_ERROR_TYPE_ERROR, GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
"Invalid signature in `%s'\n", "Invalid signature in `%s' for public key `%s'\n",
ai->wire_response_filename); ai->wire_response_filename,
GNUNET_p2s (&TEH_master_public_key.eddsa_pub));
json_decref (wire_s); json_decref (wire_s);
*ret = GNUNET_SYSERR; *ret = GNUNET_SYSERR;
return; return;

File diff suppressed because it is too large Load Diff

View File

@ -7,6 +7,8 @@ DB = postgres
#The connection string the plugin has to use for connecting to the database #The connection string the plugin has to use for connecting to the database
CONFIG = postgres:///talercheck CONFIG = postgres:///talercheck
[taler]
CURRENCY = EUR
[exchangedb] [exchangedb]

View File

@ -55,7 +55,7 @@ static int result;
/** /**
* Currency we use. * Currency we use. Must match test-exchange-db-*.conf.
*/ */
#define CURRENCY "EUR" #define CURRENCY "EUR"
@ -343,61 +343,6 @@ never_called_cb (void *cls,
} }
/**
* Callback used to process data of a merchant under KYC monitoring.
*
* @param cls closure
* @param payto_url payto URL of this particular merchant (bank account)
* @param general_id general identificator valid at the KYC-caring institution
* @param kyc_checked status of KYC check: if GNUNET_OK, the merchant was
* checked at least once, never otherwise.
* @param merchant_serial_id serial ID identifying this merchant (bank
* account) into the database system; it helps making more efficient
* queries instead of the payto URL.
*/
static void
kcs (void *cls,
const char *payto_url,
const char *general_id,
uint8_t kyc_checked,
uint64_t merchant_serial_id)
{
struct TALER_EXCHANGEDB_Session *session = cls;
struct TALER_Amount amount;
struct TALER_Amount sum;
GNUNET_assert (GNUNET_OK ==
TALER_amount_get_zero (CURRENCY,
&amount));
amount.value = 30;
FAILIF
(GNUNET_OK != plugin->insert_kyc_event (NULL,
session,
merchant_serial_id,
&amount));
amount.value = 20;
amount.fraction = 70;
FAILIF
(GNUNET_OK != plugin->insert_kyc_event (NULL,
session,
merchant_serial_id,
&amount));
FAILIF
(0 >= plugin->get_kyc_events (NULL,
session,
merchant_serial_id,
&sum));
FAILIF ((50 != sum.value) || (70 != sum.fraction));
FAILIF (2 != plugin->clean_kyc_events (NULL,
session,
merchant_serial_id));
drop:
return;
}
/** /**
* Function called with information about a refresh order. * Function called with information about a refresh order.
* Checks that the response matches what we expect to see. * Checks that the response matches what we expect to see.
@ -2225,26 +2170,6 @@ run (void *cls)
FAILIF (GNUNET_OK != FAILIF (GNUNET_OK !=
test_wire_fees (session)); test_wire_fees (session));
FAILIF (GNUNET_OK !=
plugin->insert_kyc_merchant (NULL,
session,
"dummy", // NULL segfaults.
"payto://mock"));
FAILIF (GNUNET_OK !=
plugin->mark_kyc_merchant (NULL,
session,
"payto://mock"));
FAILIF (GNUNET_OK !=
plugin->get_kyc_status (NULL,
session,
"payto://mock",
&kcs,
session));
FAILIF (GNUNET_OK !=
plugin->unmark_kyc_merchant (NULL,
session,
"payto://mock"));
plugin->preflight (plugin->cls, plugin->preflight (plugin->cls,
session); session);

View File

@ -793,29 +793,6 @@ typedef int
int done); int done);
/**
* Callback used to process data of a merchant under KYC monitoring.
*
* @param cls closure
* @param payto_url payto URL of this particular
* merchant (bank account)
* @param general_id general identificator valid
* at the KYC-caring institution
* @param kyc_checked status of KYC check:
* if GNUNET_OK, the merchant was checked at least once,
* never otherwise.
* @param merchant_serial_id serial ID identifying
* this merchant (bank account) into the database system;
* it helps making more efficient queries than the payto
* URL.
*/
typedef void
(*TALER_EXCHANGEDB_KycStatusCallback)(void *cls,
const char *payto_url,
const char *general_id,
uint8_t kyc_checked,
uint64_t merchant_serial_id);
/** /**
* Function called with details about coins that were melted, * Function called with details about coins that were melted,
* with the goal of auditing the refresh's execution. * with the goal of auditing the refresh's execution.
@ -918,19 +895,6 @@ typedef void
const struct TALER_TransferPublicKeyP *tp); const struct TALER_TransferPublicKeyP *tp);
/**
* Callback for handling a KYC timestamped event associated with
* a certain customer (= merchant).
*
* @param cls closure
* @param url "payto" URL associated with the customer
* @param timeout last time when the KYC was issued to the customer.
*/
typedef void
(*TALER_EXCHANGEDB_KycCallback)(void *cls,
const char *url,
struct GNUNET_TIME_Absolute timeout);
/** /**
* Function called with details about coins that were refunding, * Function called with details about coins that were refunding,
* with the goal of auditing the refund's execution. * with the goal of auditing the refund's execution.
@ -2509,126 +2473,6 @@ struct TALER_EXCHANGEDB_Plugin
TALER_EXCHANGEDB_WireMissingCallback cb, TALER_EXCHANGEDB_WireMissingCallback cb,
void *cb_cls); void *cb_cls);
/**
* Insert a merchant into the KYC monitor table, namely it
* associates a flag to the merchant that indicates whether
* a KYC check has been done or not on this merchant.
*
* @param cls closure
* @param session db session
* @param general_id identificator at the KYC-aware institution,
* can be NULL if this is in-line wiht the rules.
* @param payto_url payto:// URL indentifying the merchant
* bank account.
* @return database transaction status.
*/
enum GNUNET_DB_QueryStatus
(*insert_kyc_merchant)(void *cls,
struct TALER_EXCHANGEDB_Session *session,
const char *general_id,
const char *payto_url);
/**
* Mark a merchant as KYC-checked.
*
* @param cls closure
* @param session db session
* @param payto_url payto:// URL indentifying the merchant
* to check. Note, different banks may have different
* policies to check their customers.
* @return database transaction status.
*/
enum GNUNET_DB_QueryStatus
(*mark_kyc_merchant)(void *cls,
struct TALER_EXCHANGEDB_Session *session,
const char *payto_url);
/**
* Mark a merchant as NOT KYC-checked.
*
* @param cls closure
* @param session db session
* @param payto_url payto:// URL indentifying the merchant
* to unmark. Note, different banks may have different
* policies to check their customers.
* @return database transaction status.
*/
enum GNUNET_DB_QueryStatus
(*unmark_kyc_merchant)(void *cls,
struct TALER_EXCHANGEDB_Session *session,
const char *payto_url);
/**
* Retrieve KYC-check status related to a particular merchant.
*
* @param cls closure
* @param session db session
* @param payto_url URL identifying a merchant bank account,
* whose KYC is going to be retrieved.
* @param ksc callback to process all the row's columns. As
* expectable, it will only be called _if_ a row is found.
* @param ksc_cls closure for above callback.
* @return transaction status.
*/
enum GNUNET_DB_QueryStatus
(*get_kyc_status)(void *cls,
struct TALER_EXCHANGEDB_Session *session,
const char *payto_url,
TALER_EXCHANGEDB_KycStatusCallback ksc,
void *ksc_cls);
/**
* Record timestamp where a particular merchant performed
* a wire transfer.
*
* @param cls closure.
* @param session db session.
* @param merchant_serial_id serial id of the merchant who
* performed the wire transfer.
* @param amount amount of the wire transfer being monitored.
* @return database transaction status.
*/
enum GNUNET_DB_QueryStatus
(*insert_kyc_event)(void *cls,
struct TALER_EXCHANGEDB_Session *session,
uint64_t merchant_serial_id,
struct TALER_Amount *amount);
/**
* Calculate sum of money flow related to a particular merchant,
* used for KYC monitoring.
*
* @param cls closure
* @param session DB session
* @param merchant_serial_id serial id identifying the merchant
* into the KYC monitoring system.
* @param amount[out] will store the amount of money received
* by this merchant.
*/
enum GNUNET_DB_QueryStatus
(*get_kyc_events)(void *cls,
struct TALER_EXCHANGEDB_Session *session,
uint64_t merchant_serial_id,
struct TALER_Amount *amount);
/**
* Delete wire transfer records related to a particular merchant.
* This method would be called by the logic once that merchant
* gets successfully KYC checked.
*
* @param cls closure
* @param session DB session
* @param merchant_serial_id serial id of the merchant whose
* KYC records have to be deleted.
* @return DB transaction status.
*/
enum GNUNET_DB_QueryStatus
(*clean_kyc_events)(void *cls,
struct TALER_EXCHANGEDB_Session *session,
uint64_t merchant_serial_id);
}; };
#endif /* _TALER_EXCHANGE_DB_H */ #endif /* _TALER_EXCHANGE_DB_H */

View File

@ -94,24 +94,28 @@ TALER_PQ_query_param_absolute_time_nbo (const struct GNUNET_TIME_AbsoluteNBO *x)
* Currency amount expected. * Currency amount expected.
* *
* @param name name of the field in the table * @param name name of the field in the table
* @param currency expected currency for the @a amount
* @param[out] amount where to store the result * @param[out] amount where to store the result
* @return array entry for the result specification to use * @return array entry for the result specification to use
*/ */
struct GNUNET_PQ_ResultSpec struct GNUNET_PQ_ResultSpec
TALER_PQ_result_spec_amount_nbo (const char *name, TALER_PQ_result_spec_amount_nbo (const char *name,
struct TALER_AmountNBO *amount); const char *currency,
struct TALER_AmountNBO *amount);
/** /**
* Currency amount expected. * Currency amount expected.
* *
* @param name name of the field in the table * @param name name of the field in the table
* @param currency expected currency for the @a amount
* @param[out] amount where to store the result * @param[out] amount where to store the result
* @return array entry for the result specification to use * @return array entry for the result specification to use
*/ */
struct GNUNET_PQ_ResultSpec struct GNUNET_PQ_ResultSpec
TALER_PQ_result_spec_amount (const char *name, TALER_PQ_result_spec_amount (const char *name,
struct TALER_Amount *amount); const char *currency,
struct TALER_Amount *amount);
/** /**

View File

@ -87,7 +87,8 @@ TALER_gcrypt_init (void);
*/ */
const char * const char *
TALER_b2s (const void *buf, TALER_b2s (const void *buf,
size_t buf_size); size_t buf_size);
/** /**
* Convert a fixed-sized object to a string using * Convert a fixed-sized object to a string using

View File

@ -150,7 +150,6 @@ TALER_JSON_exchange_wire_signature_make (const char *payto_url,
GNUNET_CRYPTO_eddsa_key_get_public (&master_priv->eddsa_priv, GNUNET_CRYPTO_eddsa_key_get_public (&master_priv->eddsa_priv,
&master_pub.eddsa_pub); &master_pub.eddsa_pub);
TALER_exchange_wire_signature_make (payto_url, TALER_exchange_wire_signature_make (payto_url,
master_priv, master_priv,
&master_sig); &master_sig);

View File

@ -42,19 +42,19 @@
*/ */
static int static int
qconv_amount_nbo (void *cls, qconv_amount_nbo (void *cls,
const void *data, const void *data,
size_t data_len, size_t data_len,
void *param_values[], void *param_values[],
int param_lengths[], int param_lengths[],
int param_formats[], int param_formats[],
unsigned int param_length, unsigned int param_length,
void *scratch[], void *scratch[],
unsigned int scratch_length) unsigned int scratch_length)
{ {
const struct TALER_AmountNBO *amount = data; const struct TALER_AmountNBO *amount = data;
unsigned int off = 0; unsigned int off = 0;
GNUNET_assert (3 == param_length); GNUNET_assert (2 == param_length);
param_values[off] = (void *) &amount->value; param_values[off] = (void *) &amount->value;
param_lengths[off] = sizeof (amount->value); param_lengths[off] = sizeof (amount->value);
param_formats[off] = 1; param_formats[off] = 1;
@ -62,10 +62,6 @@ qconv_amount_nbo (void *cls,
param_values[off] = (void *) &amount->fraction; param_values[off] = (void *) &amount->fraction;
param_lengths[off] = sizeof (amount->fraction); param_lengths[off] = sizeof (amount->fraction);
param_formats[off] = 1; param_formats[off] = 1;
off++;
param_values[off] = (void *) amount->currency;
param_lengths[off] = strlen (amount->currency);
param_formats[off] = 1;
return 0; return 0;
} }
@ -83,7 +79,7 @@ struct GNUNET_PQ_QueryParam
TALER_PQ_query_param_amount_nbo (const struct TALER_AmountNBO *x) TALER_PQ_query_param_amount_nbo (const struct TALER_AmountNBO *x)
{ {
struct GNUNET_PQ_QueryParam res = struct GNUNET_PQ_QueryParam res =
{ &qconv_amount_nbo, NULL, x, sizeof (*x), 3 }; { &qconv_amount_nbo, NULL, x, sizeof (*x), 2 };
return res; return res;
} }
@ -104,14 +100,14 @@ TALER_PQ_query_param_amount_nbo (const struct TALER_AmountNBO *x)
*/ */
static int static int
qconv_amount (void *cls, qconv_amount (void *cls,
const void *data, const void *data,
size_t data_len, size_t data_len,
void *param_values[], void *param_values[],
int param_lengths[], int param_lengths[],
int param_formats[], int param_formats[],
unsigned int param_length, unsigned int param_length,
void *scratch[], void *scratch[],
unsigned int scratch_length) unsigned int scratch_length)
{ {
const struct TALER_Amount *amount_hbo = data; const struct TALER_Amount *amount_hbo = data;
struct TALER_AmountNBO *amount; struct TALER_AmountNBO *amount;
@ -119,16 +115,16 @@ qconv_amount (void *cls,
amount = GNUNET_new (struct TALER_AmountNBO); amount = GNUNET_new (struct TALER_AmountNBO);
scratch[0] = amount; scratch[0] = amount;
TALER_amount_hton (amount, TALER_amount_hton (amount,
amount_hbo); amount_hbo);
qconv_amount_nbo (cls, qconv_amount_nbo (cls,
amount, amount,
sizeof (struct TALER_AmountNBO), sizeof (struct TALER_AmountNBO),
param_values, param_values,
param_lengths, param_lengths,
param_formats, param_formats,
param_length, param_length,
&scratch[1], &scratch[1],
scratch_length - 1); scratch_length - 1);
return 1; return 1;
} }
@ -146,7 +142,7 @@ struct GNUNET_PQ_QueryParam
TALER_PQ_query_param_amount (const struct TALER_Amount *x) TALER_PQ_query_param_amount (const struct TALER_Amount *x)
{ {
struct GNUNET_PQ_QueryParam res = struct GNUNET_PQ_QueryParam res =
{ &qconv_amount, NULL, x, sizeof (*x), 3 }; { &qconv_amount, NULL, x, sizeof (*x), 2 };
return res; return res;
} }
@ -167,14 +163,14 @@ TALER_PQ_query_param_amount (const struct TALER_Amount *x)
*/ */
static int static int
qconv_json (void *cls, qconv_json (void *cls,
const void *data, const void *data,
size_t data_len, size_t data_len,
void *param_values[], void *param_values[],
int param_lengths[], int param_lengths[],
int param_formats[], int param_formats[],
unsigned int param_length, unsigned int param_length,
void *scratch[], void *scratch[],
unsigned int scratch_length) unsigned int scratch_length)
{ {
const json_t *json = data; const json_t *json = data;
char *str; char *str;

View File

@ -40,15 +40,14 @@
*/ */
static int static int
extract_amount_nbo_helper (PGresult *result, extract_amount_nbo_helper (PGresult *result,
int row, int row,
const char *val_name, const char *currency,
const char *frac_name, const char *val_name,
const char *curr_name, const char *frac_name,
struct TALER_AmountNBO *r_amount_nbo) struct TALER_AmountNBO *r_amount_nbo)
{ {
int val_num; int val_num;
int frac_num; int frac_num;
int curr_num;
int len; int len;
/* These checks are simply to check that clients obey by our naming /* These checks are simply to check that clients obey by our naming
@ -59,9 +58,6 @@ extract_amount_nbo_helper (PGresult *result,
GNUNET_assert (NULL != GNUNET_assert (NULL !=
strstr (frac_name, strstr (frac_name,
"_frac")); "_frac"));
GNUNET_assert (NULL !=
strstr (curr_name,
"_curr"));
/* Set return value to invalid in case we don't finish */ /* Set return value to invalid in case we don't finish */
memset (r_amount_nbo, memset (r_amount_nbo,
0, 0,
@ -70,27 +66,18 @@ extract_amount_nbo_helper (PGresult *result,
val_name); val_name);
frac_num = PQfnumber (result, frac_num = PQfnumber (result,
frac_name); frac_name);
curr_num = PQfnumber (result,
curr_name);
if (val_num < 0) if (val_num < 0)
{ {
GNUNET_log (GNUNET_ERROR_TYPE_ERROR, GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
"Field `%s' does not exist in result\n", "Field `%s' does not exist in result\n",
val_name); val_name);
return GNUNET_SYSERR; return GNUNET_SYSERR;
} }
if (frac_num < 0) if (frac_num < 0)
{ {
GNUNET_log (GNUNET_ERROR_TYPE_ERROR, GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
"Field `%s' does not exist in result\n", "Field `%s' does not exist in result\n",
frac_name); frac_name);
return GNUNET_SYSERR;
}
if (curr_num < 0)
{
GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
"Field `%s' does not exist in result\n",
curr_name);
return GNUNET_SYSERR; return GNUNET_SYSERR;
} }
if ( (PQgetisnull (result, if ( (PQgetisnull (result,
@ -98,10 +85,7 @@ extract_amount_nbo_helper (PGresult *result,
val_num)) || val_num)) ||
(PQgetisnull (result, (PQgetisnull (result,
row, row,
frac_num)) || frac_num)) )
(PQgetisnull (result,
row,
curr_num)) )
{ {
GNUNET_break (0); GNUNET_break (0);
return GNUNET_NO; return GNUNET_NO;
@ -115,13 +99,9 @@ extract_amount_nbo_helper (PGresult *result,
row, row,
frac_num); frac_num);
len = GNUNET_MIN (TALER_CURRENCY_LEN - 1, len = GNUNET_MIN (TALER_CURRENCY_LEN - 1,
PQgetlength (result, strlen (currency));
row,
curr_num));
memcpy (r_amount_nbo->currency, memcpy (r_amount_nbo->currency,
PQgetvalue (result, currency,
row,
curr_num),
len); len);
return GNUNET_OK; return GNUNET_OK;
} }
@ -130,7 +110,7 @@ extract_amount_nbo_helper (PGresult *result,
/** /**
* Extract data from a Postgres database @a result at row @a row. * Extract data from a Postgres database @a result at row @a row.
* *
* @param cls closure * @param cls closure, a `const char *` giving the currency
* @param result where to extract data from * @param result where to extract data from
* @param row row to extract data from * @param row row to extract data from
* @param fname name (or prefix) of the fields to extract from * @param fname name (or prefix) of the fields to extract from
@ -143,35 +123,31 @@ extract_amount_nbo_helper (PGresult *result,
*/ */
static int static int
extract_amount_nbo (void *cls, extract_amount_nbo (void *cls,
PGresult *result, PGresult *result,
int row, int row,
const char *fname, const char *fname,
size_t *dst_size, size_t *dst_size,
void *dst) void *dst)
{ {
const char *currency = cls;
char *val_name; char *val_name;
char *frac_name; char *frac_name;
char *curr_name;
int ret; int ret;
GNUNET_asprintf (&val_name, GNUNET_asprintf (&val_name,
"%s_val", "%s_val",
fname); fname);
GNUNET_asprintf (&frac_name, GNUNET_asprintf (&frac_name,
"%s_frac", "%s_frac",
fname); fname);
GNUNET_asprintf (&curr_name,
"%s_curr",
fname);
ret = extract_amount_nbo_helper (result, ret = extract_amount_nbo_helper (result,
row, row,
val_name, currency,
frac_name, val_name,
curr_name, frac_name,
dst); dst);
GNUNET_free (val_name); GNUNET_free (val_name);
GNUNET_free (frac_name); GNUNET_free (frac_name);
GNUNET_free (curr_name);
return ret; return ret;
} }
@ -185,12 +161,17 @@ extract_amount_nbo (void *cls,
*/ */
struct GNUNET_PQ_ResultSpec struct GNUNET_PQ_ResultSpec
TALER_PQ_result_spec_amount_nbo (const char *name, TALER_PQ_result_spec_amount_nbo (const char *name,
struct TALER_AmountNBO *amount) const char *currency,
struct TALER_AmountNBO *amount)
{ {
struct GNUNET_PQ_ResultSpec res = struct GNUNET_PQ_ResultSpec res = {
{ &extract_amount_nbo, NULL, NULL, .conv = &extract_amount_nbo,
(void *) amount, sizeof (*amount), .cls = (void *) currency,
name, NULL }; .dst = (void *) amount,
.dst_size = sizeof (*amount),
.fname = name
};
return res; return res;
} }
@ -198,7 +179,7 @@ TALER_PQ_result_spec_amount_nbo (const char *name,
/** /**
* Extract data from a Postgres database @a result at row @a row. * Extract data from a Postgres database @a result at row @a row.
* *
* @param cls closure * @param cls closure, a `const char *` giving the currency
* @param result where to extract data from * @param result where to extract data from
* @param row row to extract data from * @param row row to extract data from
* @param fname name (or prefix) of the fields to extract from * @param fname name (or prefix) of the fields to extract from
@ -211,39 +192,35 @@ TALER_PQ_result_spec_amount_nbo (const char *name,
*/ */
static int static int
extract_amount (void *cls, extract_amount (void *cls,
PGresult *result, PGresult *result,
int row, int row,
const char *fname, const char *fname,
size_t *dst_size, size_t *dst_size,
void *dst) void *dst)
{ {
const char *currency = cls;
struct TALER_Amount *r_amount = dst; struct TALER_Amount *r_amount = dst;
char *val_name; char *val_name;
char *frac_name; char *frac_name;
char *curr_name;
struct TALER_AmountNBO amount_nbo; struct TALER_AmountNBO amount_nbo;
int ret; int ret;
GNUNET_asprintf (&val_name, GNUNET_asprintf (&val_name,
"%s_val", "%s_val",
fname); fname);
GNUNET_asprintf (&frac_name, GNUNET_asprintf (&frac_name,
"%s_frac", "%s_frac",
fname); fname);
GNUNET_asprintf (&curr_name,
"%s_curr",
fname);
ret = extract_amount_nbo_helper (result, ret = extract_amount_nbo_helper (result,
row, row,
val_name, currency,
frac_name, val_name,
curr_name, frac_name,
&amount_nbo); &amount_nbo);
TALER_amount_ntoh (r_amount, TALER_amount_ntoh (r_amount,
&amount_nbo); &amount_nbo);
GNUNET_free (val_name); GNUNET_free (val_name);
GNUNET_free (frac_name); GNUNET_free (frac_name);
GNUNET_free (curr_name);
return ret; return ret;
} }
@ -252,17 +229,23 @@ extract_amount (void *cls,
* Currency amount expected. * Currency amount expected.
* *
* @param name name of the field in the table * @param name name of the field in the table
* @param currency the currency the amount is in
* @param[out] amount where to store the result * @param[out] amount where to store the result
* @return array entry for the result specification to use * @return array entry for the result specification to use
*/ */
struct GNUNET_PQ_ResultSpec struct GNUNET_PQ_ResultSpec
TALER_PQ_result_spec_amount (const char *name, TALER_PQ_result_spec_amount (const char *name,
struct TALER_Amount *amount) const char *currency,
struct TALER_Amount *amount)
{ {
struct GNUNET_PQ_ResultSpec res = struct GNUNET_PQ_ResultSpec res = {
{ &extract_amount, NULL, NULL, .conv = &extract_amount,
(void *) amount, sizeof (*amount), .cls = (void *) currency,
name, NULL }; .dst = (void *) amount,
.dst_size = sizeof (*amount),
.fname = name
};
return res; return res;
} }
@ -283,11 +266,11 @@ TALER_PQ_result_spec_amount (const char *name,
*/ */
static int static int
extract_json (void *cls, extract_json (void *cls,
PGresult *result, PGresult *result,
int row, int row,
const char *fname, const char *fname,
size_t *dst_size, size_t *dst_size,
void *dst) void *dst)
{ {
json_t **j_dst = dst; json_t **j_dst = dst;
const char *res; const char *res;
@ -296,33 +279,33 @@ extract_json (void *cls,
size_t slen; size_t slen;
fnum = PQfnumber (result, fnum = PQfnumber (result,
fname); fname);
if (fnum < 0) if (fnum < 0)
{ {
GNUNET_log (GNUNET_ERROR_TYPE_ERROR, GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
"Field `%s' does not exist in result\n", "Field `%s' does not exist in result\n",
fname); fname);
return GNUNET_SYSERR; return GNUNET_SYSERR;
} }
if (PQgetisnull (result, if (PQgetisnull (result,
row, row,
fnum)) fnum))
return GNUNET_NO; return GNUNET_NO;
slen = PQgetlength (result, slen = PQgetlength (result,
row, row,
fnum); fnum);
res = (const char *) PQgetvalue (result, res = (const char *) PQgetvalue (result,
row, row,
fnum); fnum);
*j_dst = json_loadb (res, *j_dst = json_loadb (res,
slen, slen,
JSON_REJECT_DUPLICATES, JSON_REJECT_DUPLICATES,
&json_error); &json_error);
if (NULL == *j_dst) if (NULL == *j_dst)
{ {
GNUNET_log (GNUNET_ERROR_TYPE_ERROR, GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
"Failed to parse JSON result for field `%s': %s (%s)\n", "Failed to parse JSON result for field `%s': %s (%s)\n",
fname, fname,
json_error.text, json_error.text,
json_error.source); json_error.source);
return GNUNET_SYSERR; return GNUNET_SYSERR;
@ -340,7 +323,7 @@ extract_json (void *cls,
*/ */
static void static void
clean_json (void *cls, clean_json (void *cls,
void *rd) void *rd)
{ {
json_t **dst = rd; json_t **dst = rd;
@ -363,15 +346,17 @@ struct GNUNET_PQ_ResultSpec
TALER_PQ_result_spec_json (const char *name, TALER_PQ_result_spec_json (const char *name,
json_t **jp) json_t **jp)
{ {
struct GNUNET_PQ_ResultSpec res = struct GNUNET_PQ_ResultSpec res = {
{ &extract_json, &clean_json, NULL, .conv = &extract_json,
(void *) jp, 0, .cleaner = &clean_json,
name, NULL }; .dst = (void *) jp,
.fname = name
};
return res; return res;
} }
/** /**
* Extract data from a Postgres database @a result at row @a row. * Extract data from a Postgres database @a result at row @a row.
* *
@ -399,7 +384,7 @@ extract_round_time (void *cls,
int fnum; int fnum;
fnum = PQfnumber (result, fnum = PQfnumber (result,
fname); fname);
if (fnum < 0) if (fnum < 0)
{ {
GNUNET_break (0); GNUNET_break (0);
@ -443,10 +428,13 @@ struct GNUNET_PQ_ResultSpec
TALER_PQ_result_spec_absolute_time (const char *name, TALER_PQ_result_spec_absolute_time (const char *name,
struct GNUNET_TIME_Absolute *at) struct GNUNET_TIME_Absolute *at)
{ {
struct GNUNET_PQ_ResultSpec res = struct GNUNET_PQ_ResultSpec res = {
{ &extract_round_time, NULL, NULL, .conv = &extract_round_time,
(void *) at, sizeof (struct GNUNET_TIME_Absolute), .dst = (void *) at,
name, NULL }; .dst_size = sizeof (struct GNUNET_TIME_Absolute),
.fname = name
};
return res; return res;
} }
@ -478,15 +466,15 @@ extract_round_time_nbo (void *cls,
int fnum; int fnum;
fnum = PQfnumber (result, fnum = PQfnumber (result,
fname); fname);
if (fnum < 0) if (fnum < 0)
{ {
GNUNET_break (0); GNUNET_break (0);
return GNUNET_SYSERR; return GNUNET_SYSERR;
} }
if (PQgetisnull (result, if (PQgetisnull (result,
row, row,
fnum)) fnum))
{ {
GNUNET_break (0); GNUNET_break (0);
return GNUNET_SYSERR; return GNUNET_SYSERR;
@ -522,10 +510,13 @@ struct GNUNET_PQ_ResultSpec
TALER_PQ_result_spec_absolute_time_nbo (const char *name, TALER_PQ_result_spec_absolute_time_nbo (const char *name,
struct GNUNET_TIME_AbsoluteNBO *at) struct GNUNET_TIME_AbsoluteNBO *at)
{ {
struct GNUNET_PQ_ResultSpec res = struct GNUNET_PQ_ResultSpec res = {
{ &extract_round_time_nbo, NULL, NULL, .conv = &extract_round_time_nbo,
(void *) at, sizeof (struct GNUNET_TIME_AbsoluteNBO), .dst = (void *) at,
name, NULL }; .dst_size = sizeof (struct GNUNET_TIME_AbsoluteNBO),
.fname = name
};
return res; return res;
} }

View File

@ -50,23 +50,18 @@ postgres_prepare (PGconn *db_conn)
"INSERT INTO test_pq (" "INSERT INTO test_pq ("
" hamount_val" " hamount_val"
",hamount_frac" ",hamount_frac"
",hamount_curr"
",namount_val" ",namount_val"
",namount_frac" ",namount_frac"
",namount_curr"
",json" ",json"
") VALUES " ") VALUES "
"($1, $2, $3, $4, $5, $6," "($1, $2, $3, $4, $5);",
"$7);", 5, NULL);
7, NULL);
PREPARE ("test_select", PREPARE ("test_select",
"SELECT" "SELECT"
" hamount_val" " hamount_val"
",hamount_frac" ",hamount_frac"
",hamount_curr"
",namount_val" ",namount_val"
",namount_frac" ",namount_frac"
",namount_curr"
",json" ",json"
" FROM test_pq;", " FROM test_pq;",
0, NULL); 0, NULL);
@ -113,8 +108,8 @@ run_queries (PGconn *conn)
}; };
result = GNUNET_PQ_exec_prepared (conn, result = GNUNET_PQ_exec_prepared (conn,
"test_insert", "test_insert",
params_insert); params_insert);
if (PGRES_COMMAND_OK != PQresultStatus (result)) if (PGRES_COMMAND_OK != PQresultStatus (result))
{ {
GNUNET_log (GNUNET_ERROR_TYPE_ERROR, GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
@ -131,10 +126,10 @@ run_queries (PGconn *conn)
}; };
result = GNUNET_PQ_exec_prepared (conn, result = GNUNET_PQ_exec_prepared (conn,
"test_select", "test_select",
params_select); params_select);
if (1 != if (1 !=
PQntuples (result)) PQntuples (result))
{ {
GNUNET_break (0); GNUNET_break (0);
PQclear (result); PQclear (result);
@ -144,8 +139,8 @@ run_queries (PGconn *conn)
{ {
struct GNUNET_PQ_ResultSpec results_select[] = { struct GNUNET_PQ_ResultSpec results_select[] = {
TALER_PQ_result_spec_amount ("hamount", &hamount2), TALER_PQ_result_spec_amount ("hamount", "EUR", &hamount2),
TALER_PQ_result_spec_amount_nbo ("namount", &namount2), TALER_PQ_result_spec_amount_nbo ("namount", "EUR", &namount2),
TALER_PQ_result_spec_json ("json", &json2), TALER_PQ_result_spec_json ("json", &json2),
GNUNET_PQ_result_spec_end GNUNET_PQ_result_spec_end
}; };
@ -163,8 +158,9 @@ run_queries (PGconn *conn)
&namount2); &namount2);
GNUNET_break (0 == GNUNET_break (0 ==
TALER_amount_cmp (&hamount, TALER_amount_cmp (&hamount,
&hamount2)); &hamount2));
GNUNET_break (42 == json_integer_value (json_object_get (json2, "foo"))); GNUNET_break (42 ==
json_integer_value (json_object_get (json2, "foo")));
GNUNET_PQ_cleanup_result (results_select); GNUNET_PQ_cleanup_result (results_select);
PQclear (result); PQclear (result);
} }
@ -185,8 +181,8 @@ main(int argc,
int ret; int ret;
GNUNET_log_setup ("test-pq", GNUNET_log_setup ("test-pq",
"WARNING", "WARNING",
NULL); NULL);
conn = PQconnectdb ("postgres:///talercheck"); conn = PQconnectdb ("postgres:///talercheck");
if (CONNECTION_OK != PQstatus (conn)) if (CONNECTION_OK != PQstatus (conn))
{ {
@ -202,17 +198,15 @@ main(int argc,
"CREATE TEMPORARY TABLE IF NOT EXISTS test_pq (" "CREATE TEMPORARY TABLE IF NOT EXISTS test_pq ("
" hamount_val INT8 NOT NULL" " hamount_val INT8 NOT NULL"
",hamount_frac INT4 NOT NULL" ",hamount_frac INT4 NOT NULL"
",hamount_curr VARCHAR("TALER_CURRENCY_LEN_STR") NOT NULL"
",namount_val INT8 NOT NULL" ",namount_val INT8 NOT NULL"
",namount_frac INT4 NOT NULL" ",namount_frac INT4 NOT NULL"
",namount_curr VARCHAR("TALER_CURRENCY_LEN_STR") NOT NULL"
",json VARCHAR NOT NULL" ",json VARCHAR NOT NULL"
")"); ")");
if (PGRES_COMMAND_OK != PQresultStatus (result)) if (PGRES_COMMAND_OK != PQresultStatus (result))
{ {
fprintf (stderr, fprintf (stderr,
"Failed to create table: %s\n", "Failed to create table: %s\n",
PQerrorMessage (conn)); PQerrorMessage (conn));
PQclear (result); PQclear (result);
PQfinish (conn); PQfinish (conn);
return 1; return 1;
@ -227,12 +221,12 @@ main(int argc,
} }
ret = run_queries (conn); ret = run_queries (conn);
result = PQexec (conn, result = PQexec (conn,
"DROP TABLE test_pq"); "DROP TABLE test_pq");
if (PGRES_COMMAND_OK != PQresultStatus (result)) if (PGRES_COMMAND_OK != PQresultStatus (result))
{ {
fprintf (stderr, fprintf (stderr,
"Failed to create table: %s\n", "Failed to create table: %s\n",
PQerrorMessage (conn)); PQerrorMessage (conn));
PQclear (result); PQclear (result);
PQfinish (conn); PQfinish (conn);
return 1; return 1;

View File

@ -39,7 +39,7 @@
*/ */
const char * const char *
TALER_b2s (const void *buf, TALER_b2s (const void *buf,
size_t buf_size) size_t buf_size)
{ {
static char ret[9]; static char ret[9];
struct GNUNET_HashCode hc; struct GNUNET_HashCode hc;
@ -49,7 +49,7 @@ TALER_b2s (const void *buf,
buf_size, buf_size,
&hc); &hc);
tmp = GNUNET_STRINGS_data_to_string_alloc (&hc, tmp = GNUNET_STRINGS_data_to_string_alloc (&hc,
sizeof (hc)); sizeof (hc));
memcpy (ret, memcpy (ret,
tmp, tmp,
8); 8);