auditordb postgres plugin: auditor_progress implementation + tests

This commit is contained in:
tg(x) 2016-11-06 14:24:17 +01:00
parent 02d28bc81c
commit 247b8e33b3
3 changed files with 420 additions and 125 deletions

View File

@ -17,9 +17,8 @@
/**
* @file plugin_auditordb_postgres.c
* @brief Low-level (statement-level) Postgres database access for the auditor
* @author Florian Dold
* @author Christian Grothoff
* @author Sree Harsha Totakura
* @author Gabor X Toth
*/
#include "platform.h"
#include "taler_pq_lib.h"
@ -564,6 +563,44 @@ postgres_prepare (PGconn *db_conn)
" WHERE master_pub=$1;",
1, NULL);
/* Used in #postgres_insert_auditor_progress() */
PREPARE ("auditor_progress_insert",
"INSERT INTO auditor_progress "
"(master_pub"
",last_reserve_in_serial_id"
",last_reserve_out_serial_id"
",last_deposit_serial_id"
",last_melt_serial_id"
",last_refund_serial_id"
",last_prewire_serial_id"
") VALUES ($1,$2,$3,$4,$5,$6,$7);",
7, NULL);
/* Used in #postgres_update_auditor_progress() */
PREPARE ("auditor_progress_update",
"UPDATE auditor_progress SET "
" last_reserve_in_serial_id=$1"
",last_reserve_out_serial_id=$2"
",last_deposit_serial_id=$3"
",last_melt_serial_id=$4"
",last_refund_serial_id=$5"
",last_prewire_serial_id=$6"
" WHERE master_pub=$7",
7, NULL);
/* Used in #postgres_get_auditor_progress() */
PREPARE ("auditor_progress_select",
"SELECT"
" last_reserve_in_serial_id"
",last_reserve_out_serial_id"
",last_deposit_serial_id"
",last_melt_serial_id"
",last_refund_serial_id"
",last_prewire_serial_id"
" FROM auditor_progress"
" WHERE master_pub=$1;",
1, NULL);
/* Used in #postgres_insert_reserve_info() */
PREPARE ("auditor_reserves_insert",
"INSERT INTO auditor_reserves "
@ -1287,6 +1324,196 @@ postgres_select_denomination_info (void *cls,
}
/**
* Insert information about the auditor's progress with an exchange's
* data.
*
* @param cls the @e cls of this struct with the plugin-specific state
* @param session connection to use
* @param master_pub master key of the exchange
* @param last_reserve_in_serial_id serial ID of the last reserve_in transfer the auditor processed
* @param last_reserve_out_serial_id serial ID of the last withdraw the auditor processed
* @param last_deposit_serial_id serial ID of the last deposit the auditor processed
* @param last_melt_serial_id serial ID of the last refresh the auditor processed
* @param last_prewire_serial_id serial ID of the last prewire transfer the auditor processed
* @return #GNUNET_OK on success; #GNUNET_SYSERR on failure
*/
int
postgres_insert_auditor_progress (void *cls,
struct TALER_AUDITORDB_Session *session,
const struct TALER_MasterPublicKeyP *master_pub,
uint64_t last_reserve_in_serial_id,
uint64_t last_reserve_out_serial_id,
uint64_t last_deposit_serial_id,
uint64_t last_melt_serial_id,
uint64_t last_refund_serial_id,
uint64_t last_prewire_serial_id)
{
PGresult *result;
int ret;
struct GNUNET_PQ_QueryParam params[] = {
GNUNET_PQ_query_param_auto_from_type (master_pub),
GNUNET_PQ_query_param_uint64 (&last_reserve_in_serial_id),
GNUNET_PQ_query_param_uint64 (&last_reserve_out_serial_id),
GNUNET_PQ_query_param_uint64 (&last_deposit_serial_id),
GNUNET_PQ_query_param_uint64 (&last_melt_serial_id),
GNUNET_PQ_query_param_uint64 (&last_refund_serial_id),
GNUNET_PQ_query_param_uint64 (&last_prewire_serial_id),
GNUNET_PQ_query_param_end
};
result = GNUNET_PQ_exec_prepared (session->conn,
"auditor_progress_insert",
params);
if (PGRES_COMMAND_OK != PQresultStatus (result))
{
ret = GNUNET_SYSERR;
BREAK_DB_ERR (result);
}
else
{
ret = GNUNET_OK;
}
PQclear (result);
return ret;
}
/**
* Update information about the progress of the auditor. There
* must be an existing record for the exchange.
*
* @param cls the @e cls of this struct with the plugin-specific state
* @param session connection to use
* @param master_pub master key of the exchange
* @param last_reserve_in_serial_id serial ID of the last reserve_in transfer the auditor processed
* @param last_reserve_out_serial_id serial ID of the last withdraw the auditor processed
* @param last_deposit_serial_id serial ID of the last deposit the auditor processed
* @param last_melt_serial_id serial ID of the last refresh the auditor processed
* @param last_prewire_serial_id serial ID of the last prewire transfer the auditor processed
* @return #GNUNET_OK on success; #GNUNET_SYSERR on failure
*/
int
postgres_update_auditor_progress (void *cls,
struct TALER_AUDITORDB_Session *session,
const struct TALER_MasterPublicKeyP *master_pub,
uint64_t last_reserve_in_serial_id,
uint64_t last_reserve_out_serial_id,
uint64_t last_deposit_serial_id,
uint64_t last_melt_serial_id,
uint64_t last_refund_serial_id,
uint64_t last_prewire_serial_id)
{
PGresult *result;
int ret;
struct GNUNET_PQ_QueryParam params[] = {
GNUNET_PQ_query_param_uint64 (&last_reserve_in_serial_id),
GNUNET_PQ_query_param_uint64 (&last_reserve_out_serial_id),
GNUNET_PQ_query_param_uint64 (&last_deposit_serial_id),
GNUNET_PQ_query_param_uint64 (&last_melt_serial_id),
GNUNET_PQ_query_param_uint64 (&last_refund_serial_id),
GNUNET_PQ_query_param_uint64 (&last_prewire_serial_id),
GNUNET_PQ_query_param_auto_from_type (master_pub),
GNUNET_PQ_query_param_end
};
result = GNUNET_PQ_exec_prepared (session->conn,
"auditor_progress_update",
params);
if (PGRES_COMMAND_OK != PQresultStatus (result))
{
ret = GNUNET_SYSERR;
BREAK_DB_ERR (result);
}
else
{
ret = GNUNET_OK;
}
PQclear (result);
return ret;
}
/**
* Get information about the progress of the auditor.
*
* @param cls the @e cls of this struct with the plugin-specific state
* @param session connection to use
* @param master_pub master key of the exchange
* @param[out] last_reserve_in_serial_id serial ID of the last reserve_in transfer the auditor processed
* @param[out] last_reserve_out_serial_id serial ID of the last withdraw the auditor processed
* @param[out] last_deposit_serial_id serial ID of the last deposit the auditor processed
* @param[out] last_melt_serial_id serial ID of the last refresh the auditor processed
* @param[out] last_prewire_serial_id serial ID of the last prewire transfer the auditor processed
* @return #GNUNET_OK on success; #GNUNET_SYSERR on failure;
* #GNUNET_NO if we have no records for the @a master_pub
*/
int
postgres_get_auditor_progress (void *cls,
struct TALER_AUDITORDB_Session *session,
const struct TALER_MasterPublicKeyP *master_pub,
uint64_t *last_reserve_in_serial_id,
uint64_t *last_reserve_out_serial_id,
uint64_t *last_deposit_serial_id,
uint64_t *last_melt_serial_id,
uint64_t *last_refund_serial_id,
uint64_t *last_prewire_serial_id)
{
struct GNUNET_PQ_QueryParam params[] = {
GNUNET_PQ_query_param_auto_from_type (master_pub),
GNUNET_PQ_query_param_end
};
PGresult *result;
result = GNUNET_PQ_exec_prepared (session->conn,
"auditor_progress_select",
params);
if (PGRES_TUPLES_OK !=
PQresultStatus (result))
{
BREAK_DB_ERR (result);
PQclear (result);
return GNUNET_SYSERR;
}
int nrows = PQntuples (result);
if (0 == nrows)
{
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
"postgres_get_auditor_progress() returned 0 matching rows\n");
PQclear (result);
return GNUNET_NO;
}
GNUNET_assert (1 == nrows);
struct GNUNET_PQ_ResultSpec rs[] = {
GNUNET_PQ_result_spec_uint64 ("last_reserve_in_serial_id", last_reserve_in_serial_id),
GNUNET_PQ_result_spec_uint64 ("last_reserve_out_serial_id", last_reserve_out_serial_id),
GNUNET_PQ_result_spec_uint64 ("last_deposit_serial_id", last_deposit_serial_id),
GNUNET_PQ_result_spec_uint64 ("last_melt_serial_id", last_melt_serial_id),
GNUNET_PQ_result_spec_uint64 ("last_refund_serial_id", last_refund_serial_id),
GNUNET_PQ_result_spec_uint64 ("last_prewire_serial_id", last_prewire_serial_id),
GNUNET_PQ_result_spec_end
};
if (GNUNET_OK !=
GNUNET_PQ_extract_result (result, rs, 0))
{
GNUNET_break (0);
PQclear (result);
return GNUNET_SYSERR;
}
PQclear (result);
return GNUNET_OK;
}
/**
* Insert information about a reserve. There must not be an
* existing record for the reserve.
@ -2875,32 +3102,46 @@ libtaler_plugin_auditordb_postgres_init (void *cls)
plugin->commit = &postgres_commit;
plugin->rollback = &postgres_rollback;
plugin->gc = &postgres_gc;
plugin->get_predicted_balance = &postgres_get_predicted_balance;
plugin->update_predicted_result = &postgres_update_predicted_result;
plugin->insert_predicted_result = &postgres_insert_predicted_result;
plugin->select_historic_reserve_revenue = &postgres_select_historic_reserve_revenue;
plugin->insert_historic_reserve_revenue = &postgres_insert_historic_reserve_revenue;
plugin->select_historic_losses = &postgres_select_historic_losses;
plugin->insert_historic_losses = &postgres_insert_historic_losses;
plugin->select_historic_denom_revenue = &postgres_select_historic_denom_revenue;
plugin->insert_historic_denom_revenue = &postgres_insert_historic_denom_revenue;
plugin->get_risk_summary = &postgres_get_risk_summary;
plugin->update_risk_summary = &postgres_update_risk_summary;
plugin->insert_risk_summary = &postgres_insert_risk_summary;
plugin->get_denomination_summary = &postgres_get_denomination_summary;
plugin->update_denomination_summary = &postgres_update_denomination_summary;
plugin->insert_denomination_summary = &postgres_insert_denomination_summary;
plugin->get_denomination_balance = &postgres_get_denomination_balance;
plugin->update_denomination_balance = &postgres_update_denomination_balance;
plugin->insert_denomination_balance = &postgres_insert_denomination_balance;
plugin->get_reserve_summary = &postgres_get_reserve_summary;
plugin->update_reserve_summary = &postgres_update_reserve_summary;
plugin->insert_reserve_summary = &postgres_insert_reserve_summary;
plugin->select_denomination_info = &postgres_select_denomination_info;
plugin->insert_denomination_info = &postgres_insert_denomination_info;
plugin->get_auditor_progress = &postgres_get_auditor_progress;
plugin->update_auditor_progress = &postgres_update_auditor_progress;
plugin->insert_auditor_progress = &postgres_insert_auditor_progress;
plugin->get_reserve_info = &postgres_get_reserve_info;
plugin->update_reserve_info = &postgres_update_reserve_info;
plugin->insert_reserve_info = &postgres_insert_reserve_info;
plugin->select_denomination_info = &postgres_select_denomination_info;
plugin->insert_denomination_info = &postgres_insert_denomination_info;
plugin->get_reserve_summary = &postgres_get_reserve_summary;
plugin->update_reserve_summary = &postgres_update_reserve_summary;
plugin->insert_reserve_summary = &postgres_insert_reserve_summary;
plugin->get_denomination_balance = &postgres_get_denomination_balance;
plugin->update_denomination_balance = &postgres_update_denomination_balance;
plugin->insert_denomination_balance = &postgres_insert_denomination_balance;
plugin->get_denomination_summary = &postgres_get_denomination_summary;
plugin->update_denomination_summary = &postgres_update_denomination_summary;
plugin->insert_denomination_summary = &postgres_insert_denomination_summary;
plugin->get_risk_summary = &postgres_get_risk_summary;
plugin->update_risk_summary = &postgres_update_risk_summary;
plugin->insert_risk_summary = &postgres_insert_risk_summary;
plugin->select_historic_denom_revenue = &postgres_select_historic_denom_revenue;
plugin->insert_historic_denom_revenue = &postgres_insert_historic_denom_revenue;
plugin->select_historic_losses = &postgres_select_historic_losses;
plugin->insert_historic_losses = &postgres_insert_historic_losses;
plugin->select_historic_reserve_revenue = &postgres_select_historic_reserve_revenue;
plugin->insert_historic_reserve_revenue = &postgres_insert_historic_reserve_revenue;
plugin->get_predicted_balance = &postgres_get_predicted_balance;
plugin->update_predicted_result = &postgres_update_predicted_result;
plugin->insert_predicted_result = &postgres_insert_predicted_result;
return plugin;
}

View File

@ -16,7 +16,7 @@
/**
* @file auditordb/test_auditordb.c
* @brief test cases for DB interaction functions
* @author Gabor Toth
* @author Gabor X Toth
*/
#include "platform.h"
#include "taler_auditordb_lib.h"
@ -32,10 +32,10 @@ static int result = -1;
* Report line of error if @a cond is true, and jump to label "drop".
*/
#define FAILIF(cond) \
do { \
if (!(cond)){ break;} \
GNUNET_break (0); \
/*goto drop;*/ \
do { \
if (!(cond)){ break;} \
GNUNET_break (0); \
goto drop; \
} while (0)
@ -198,6 +198,77 @@ run (void *cls)
select_denomination_info_result,
&issue));
GNUNET_log (GNUNET_ERROR_TYPE_INFO,
"Test: insert_auditor_progress\n");
uint64_t
last_reserve_in_serial_id = 1234,
last_reserve_out_serial_id = 5678,
last_deposit_serial_id = 123,
last_melt_serial_id = 456,
last_refund_serial_id = 789,
last_prewire_serial_id = 555,
last_reserve_in_serial_id2 = 0,
last_reserve_out_serial_id2 = 0,
last_deposit_serial_id2 = 0,
last_melt_serial_id2 = 0,
last_refund_serial_id2 = 0,
last_prewire_serial_id2 = 0;
FAILIF (GNUNET_OK !=
plugin->insert_auditor_progress (plugin->cls,
session,
&master_pub,
last_reserve_in_serial_id,
last_reserve_out_serial_id,
last_deposit_serial_id,
last_melt_serial_id,
last_refund_serial_id,
last_prewire_serial_id));
GNUNET_log (GNUNET_ERROR_TYPE_INFO,
"Test: update_auditor_progress\n");
last_reserve_in_serial_id++;
last_reserve_out_serial_id++;
last_deposit_serial_id2++;
last_melt_serial_id2++;
last_refund_serial_id2++;
last_prewire_serial_id2++;
FAILIF (GNUNET_OK !=
plugin->update_auditor_progress (plugin->cls,
session,
&master_pub,
last_reserve_in_serial_id,
last_reserve_out_serial_id,
last_deposit_serial_id,
last_melt_serial_id,
last_refund_serial_id,
last_prewire_serial_id));
GNUNET_log (GNUNET_ERROR_TYPE_INFO,
"Test: get_auditor_progress\n");
FAILIF (GNUNET_OK !=
plugin->get_auditor_progress (plugin->cls,
session,
&master_pub,
&last_reserve_in_serial_id2,
&last_reserve_out_serial_id2,
&last_deposit_serial_id2,
&last_melt_serial_id2,
&last_refund_serial_id2,
&last_prewire_serial_id2));
FAILIF (last_reserve_in_serial_id2 != last_reserve_in_serial_id
|| last_reserve_out_serial_id2 != last_reserve_out_serial_id
|| last_deposit_serial_id2 != last_deposit_serial_id
|| last_melt_serial_id2 != last_melt_serial_id
|| last_refund_serial_id2 != last_refund_serial_id
|| last_prewire_serial_id2 != last_prewire_serial_id);
GNUNET_log (GNUNET_ERROR_TYPE_INFO,
"Test: insert_reserve_info\n");
@ -211,12 +282,6 @@ run (void *cls)
TALER_string_to_amount (CURRENCY ":23.456789",
&withdraw_fee_balance));
uint64_t
last_reserve_in_serial_id = 1234,
last_reserve_out_serial_id = 5678,
last_reserve_in_serial_id2 = 0,
last_reserve_out_serial_id2 =0;
FAILIF (GNUNET_OK !=
plugin->insert_reserve_info (plugin->cls,
session,
@ -320,15 +385,6 @@ run (void *cls)
TALER_string_to_amount (CURRENCY ":45.678901",
&refund_fee_balance));
uint64_t
last_deposit_serial_id = 123,
last_melt_serial_id = 456,
last_refund_serial_id = 789,
last_deposit_serial_id2 = 0,
last_melt_serial_id2 = 0,
last_refund_serial_id2 =0;
FAILIF (GNUNET_OK !=
plugin->insert_denomination_balance (plugin->cls,
session,

View File

@ -252,6 +252,84 @@ struct TALER_AUDITORDB_Plugin
void *cb_cls);
/**
* Insert information about the auditor's progress with an exchange's
* data.
*
* @param cls the @e cls of this struct with the plugin-specific state
* @param session connection to use
* @param master_pub master key of the exchange
* @param last_reserve_in_serial_id serial ID of the last reserve_in transfer the auditor processed
* @param last_reserve_out_serial_id serial ID of the last withdraw the auditor processed
* @param last_deposit_serial_id serial ID of the last deposit the auditor processed
* @param last_melt_serial_id serial ID of the last refresh the auditor processed
* @param last_prewire_serial_id serial ID of the last prewire transfer the auditor processed
* @return #GNUNET_OK on success; #GNUNET_SYSERR on failure
*/
int
(*insert_auditor_progress)(void *cls,
struct TALER_AUDITORDB_Session *session,
const struct TALER_MasterPublicKeyP *master_pub,
uint64_t last_reserve_in_serial_id,
uint64_t last_reserve_out_serial_id,
uint64_t last_deposit_serial_id,
uint64_t last_melt_serial_id,
uint64_t last_refund_serial_id,
uint64_t last_prewire_serial_id);
/**
* Update information about the progress of the auditor. There
* must be an existing record for the exchange.
*
* @param cls the @e cls of this struct with the plugin-specific state
* @param session connection to use
* @param master_pub master key of the exchange
* @param last_reserve_in_serial_id serial ID of the last reserve_in transfer the auditor processed
* @param last_reserve_out_serial_id serial ID of the last withdraw the auditor processed
* @param last_deposit_serial_id serial ID of the last deposit the auditor processed
* @param last_melt_serial_id serial ID of the last refresh the auditor processed
* @param last_prewire_serial_id serial ID of the last prewire transfer the auditor processed
* @return #GNUNET_OK on success; #GNUNET_SYSERR on failure
*/
int
(*update_auditor_progress)(void *cls,
struct TALER_AUDITORDB_Session *session,
const struct TALER_MasterPublicKeyP *master_pub,
uint64_t last_reserve_in_serial_id,
uint64_t last_reserve_out_serial_id,
uint64_t last_deposit_serial_id,
uint64_t last_melt_serial_id,
uint64_t last_refund_serial_id,
uint64_t last_prewire_serial_id);
/**
* Get information about the progress of the auditor.
*
* @param cls the @e cls of this struct with the plugin-specific state
* @param session connection to use
* @param master_pub master key of the exchange
* @param[out] last_reserve_in_serial_id serial ID of the last reserve_in transfer the auditor processed
* @param[out] last_reserve_out_serial_id serial ID of the last withdraw the auditor processed
* @param[out] last_deposit_serial_id serial ID of the last deposit the auditor processed
* @param[out] last_melt_serial_id serial ID of the last refresh the auditor processed
* @param[out] last_prewire_serial_id serial ID of the last prewire transfer the auditor processed
* @return #GNUNET_OK on success; #GNUNET_SYSERR on failure;
* #GNUNET_NO if we have no records for the @a master_pub
*/
int
(*get_auditor_progress)(void *cls,
struct TALER_AUDITORDB_Session *session,
const struct TALER_MasterPublicKeyP *master_pub,
uint64_t *last_reserve_in_serial_id,
uint64_t *last_reserve_out_serial_id,
uint64_t *last_deposit_serial_id,
uint64_t *last_melt_serial_id,
uint64_t *last_refund_serial_id,
uint64_t *last_prewire_serial_id);
/**
* Insert information about a reserve. There must not be an
* existing record for the reserve.
@ -806,86 +884,6 @@ struct TALER_AUDITORDB_Plugin
struct TALER_Amount *balance);
/**
* Insert information about the auditor's progress with an exchange's
* data.
*
* @param cls the @e cls of this struct with the plugin-specific state
* @param session connection to use
* @param master_pub master key of the exchange
* @param last_reserve_in_serial_id serial ID of the last reserve_in transfer the auditor processed
* @param last_reserve_out_serial_id serial ID of the last withdraw the auditor processed
* @param last_deposit_serial_id serial ID of the last deposit the auditor processed
* @param last_melt_serial_id serial ID of the last refresh the auditor processed
* @param last_prewire_serial_id serial ID of the last prewire transfer the auditor processed
* @return #GNUNET_OK on success; #GNUNET_SYSERR on failure
*/
int
(*insert_auditor_progress)(void *cls,
struct TALER_AUDITORDB_Session *session,
const struct TALER_MasterPublicKeyP *master_pub,
uint64_t last_reserve_in_serial_id,
uint64_t last_reserve_out_serial_id,
uint64_t last_deposit_serial_id,
uint64_t last_melt_serial_id,
uint64_t last_refund_serial_id,
uint64_t last_prewire_serial_id);
/**
* Update information about the progress of the auditor. There
* must be an existing record for the exchange.
*
* @param cls the @e cls of this struct with the plugin-specific state
* @param session connection to use
* @param master_pub master key of the exchange
* @param last_reserve_in_serial_id serial ID of the last reserve_in transfer the auditor processed
* @param last_reserve_out_serial_id serial ID of the last withdraw the auditor processed
* @param last_deposit_serial_id serial ID of the last deposit the auditor processed
* @param last_melt_serial_id serial ID of the last refresh the auditor processed
* @param last_prewire_serial_id serial ID of the last prewire transfer the auditor processed
* @return #GNUNET_OK on success; #GNUNET_SYSERR on failure
*/
int
(*update_auditor_progress)(void *cls,
struct TALER_AUDITORDB_Session *session,
const struct TALER_MasterPublicKeyP *master_pub,
uint64_t last_reserve_in_serial_id,
uint64_t last_reserve_out_serial_id,
uint64_t last_deposit_serial_id,
uint64_t last_melt_serial_id,
uint64_t last_refund_serial_id,
uint64_t last_prewire_serial_id);
/**
* Get an exchange's predicted balance.
*
* @param cls the @e cls of this struct with the plugin-specific state
* @param session connection to use
* @param master_pub master key of the exchange
* @param[out] last_reserve_in_serial_id serial ID of the last reserve_in transfer the auditor processed
* @param[out] last_reserve_out_serial_id serial ID of the last withdraw the auditor processed
* @param[out] last_deposit_serial_id serial ID of the last deposit the auditor processed
* @param[out] last_melt_serial_id serial ID of the last refresh the auditor processed
* @param[out] last_prewire_serial_id serial ID of the last prewire transfer the auditor processed
* @return #GNUNET_OK on success; #GNUNET_SYSERR on failure;
* #GNUNET_NO if we have no records for the @a master_pub
*/
int
(*get_auditor_progress)(void *cls,
struct TALER_AUDITORDB_Session *session,
const struct TALER_MasterPublicKeyP *master_pub,
uint64_t *last_reserve_in_serial_id,
uint64_t *last_reserve_out_serial_id,
uint64_t *last_deposit_serial_id,
uint64_t *last_melt_serial_id,
uint64_t *last_refund_serial_id,
uint64_t *last_prewire_serial_id);
};