-update auditor schema in preparation to fix #4960

This commit is contained in:
Christian Grothoff 2022-07-31 21:54:29 +02:00
parent af6a9a9546
commit 31bfe5234e
No known key found for this signature in database
GPG Key ID: 939E6BE1E29FC3CC
6 changed files with 143 additions and 14 deletions

View File

@ -267,9 +267,11 @@ CREATE TABLE IF NOT EXISTS auditor_predicted_result
(master_pub BYTEA NOT NULL CONSTRAINT master_pub_ref REFERENCES auditor_exchanges(master_pub) ON DELETE CASCADE
,balance_val INT8 NOT NULL
,balance_frac INT4 NOT NULL
,drained_val INT8 NOT NULL
,drained_frac INT4 NOT NULL
);
COMMENT ON TABLE auditor_predicted_result
IS 'Table with the sum of the ledger, auditor_historic_revenue and the auditor_reserve_balance. This is the final amount that the exchange should have in its bank account right now.';
IS 'Table with the sum of the ledger, auditor_historic_revenue and the auditor_reserve_balance and the drained profits. This is the final amount that the exchange should have in its bank account right now (and the total amount drained as profits to non-escrow accounts).';
-- Finally, commit everything

View File

@ -670,20 +670,26 @@ setup_connection (struct PostgresClosure *pg)
"(master_pub"
",balance_val"
",balance_frac"
") VALUES ($1,$2,$3);",
3),
",drained_val"
",drained_frac"
") VALUES ($1,$2,$3,$4,$5);",
5),
/* Used in #postgres_update_predicted_result() */
GNUNET_PQ_make_prepare ("auditor_predicted_result_update",
"UPDATE auditor_predicted_result SET"
" balance_val=$1"
",balance_frac=$2"
" WHERE master_pub=$3;",
3),
",drained_val=$3"
",drained_frac=$4"
" WHERE master_pub=$5;",
5),
/* Used in #postgres_get_predicted_balance() */
GNUNET_PQ_make_prepare ("auditor_predicted_result_select",
"SELECT"
" balance_val"
",balance_frac"
",drained_val"
",drained_frac"
" FROM auditor_predicted_result"
" WHERE master_pub=$1;",
1),
@ -2833,18 +2839,21 @@ postgres_select_historic_reserve_revenue (
* @param cls the @e cls of this struct with the plugin-specific state
* @param master_pub master key of the exchange
* @param balance what the bank account balance of the exchange should show
* @param drained amount that was drained in profits
* @return transaction status code
*/
static enum GNUNET_DB_QueryStatus
postgres_insert_predicted_result (
void *cls,
const struct TALER_MasterPublicKeyP *master_pub,
const struct TALER_Amount *balance)
const struct TALER_Amount *balance,
const struct TALER_Amount *drained)
{
struct PostgresClosure *pg = cls;
struct GNUNET_PQ_QueryParam params[] = {
GNUNET_PQ_query_param_auto_from_type (master_pub),
TALER_PQ_query_param_amount (balance),
TALER_PQ_query_param_amount (drained),
GNUNET_PQ_query_param_end
};
@ -2861,17 +2870,20 @@ postgres_insert_predicted_result (
* @param cls the @e cls of this struct with the plugin-specific state
* @param master_pub master key of the exchange
* @param balance what the bank account balance of the exchange should show
* @param drained amount that was drained in profits
* @return transaction status code
*/
static enum GNUNET_DB_QueryStatus
postgres_update_predicted_result (
void *cls,
const struct TALER_MasterPublicKeyP *master_pub,
const struct TALER_Amount *balance)
const struct TALER_Amount *balance,
const struct TALER_Amount *drained)
{
struct PostgresClosure *pg = cls;
struct GNUNET_PQ_QueryParam params[] = {
TALER_PQ_query_param_amount (balance),
TALER_PQ_query_param_amount (drained),
GNUNET_PQ_query_param_auto_from_type (master_pub),
GNUNET_PQ_query_param_end
};
@ -2888,12 +2900,14 @@ postgres_update_predicted_result (
* @param cls the @e cls of this struct with the plugin-specific state
* @param master_pub master key of the exchange
* @param[out] balance expected bank account balance of the exchange
* @param[out] drained amount drained so far
* @return transaction status code
*/
static enum GNUNET_DB_QueryStatus
postgres_get_predicted_balance (void *cls,
const struct TALER_MasterPublicKeyP *master_pub,
struct TALER_Amount *balance)
struct TALER_Amount *balance,
struct TALER_Amount *drained)
{
struct PostgresClosure *pg = cls;
struct GNUNET_PQ_QueryParam params[] = {
@ -2903,6 +2917,8 @@ postgres_get_predicted_balance (void *cls,
struct GNUNET_PQ_ResultSpec rs[] = {
TALER_PQ_RESULT_SPEC_AMOUNT ("balance",
balance),
TALER_PQ_RESULT_SPEC_AMOUNT ("drained",
drained),
GNUNET_PQ_result_spec_end
};

View File

@ -347,7 +347,9 @@ run (void *cls)
struct TALER_Amount melt_fee_balance2;
struct TALER_Amount refund_fee_balance2;
struct TALER_Amount rbalance;
struct TALER_Amount dbalance;
struct TALER_Amount rbalance2;
struct TALER_Amount dbalance2;
struct TALER_Amount loss;
struct TALER_Amount loss2;
struct TALER_Amount iirp;
@ -372,6 +374,9 @@ run (void *cls)
GNUNET_assert (GNUNET_OK ==
TALER_string_to_amount (CURRENCY ":13.57986",
&rbalance));
GNUNET_assert (GNUNET_OK ==
TALER_string_to_amount (CURRENCY ":12.57986",
&dbalance));
GNUNET_assert (GNUNET_OK ==
TALER_string_to_amount (CURRENCY ":1.6",
&loss));
@ -613,7 +618,8 @@ run (void *cls)
FAILIF (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT !=
plugin->insert_predicted_result (plugin->cls,
&master_pub,
&rbalance));
&rbalance,
&dbalance));
GNUNET_log (GNUNET_ERROR_TYPE_INFO,
"Test: update_predicted_result\n");
@ -621,11 +627,15 @@ run (void *cls)
GNUNET_assert (GNUNET_OK ==
TALER_string_to_amount (CURRENCY ":78.901234",
&rbalance));
GNUNET_assert (GNUNET_OK ==
TALER_string_to_amount (CURRENCY ":73.901234",
&dbalance));
FAILIF (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT !=
plugin->update_predicted_result (plugin->cls,
&master_pub,
&rbalance));
&rbalance,
&dbalance));
FAILIF (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT !=
plugin->insert_wire_fee_summary (plugin->cls,
@ -659,7 +669,8 @@ run (void *cls)
FAILIF (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT !=
plugin->get_predicted_balance (plugin->cls,
&master_pub,
&rbalance2));
&rbalance2,
&dbalance2));
FAILIF (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT !=
plugin->del_reserve_info (plugin->cls,
@ -668,6 +679,8 @@ run (void *cls)
FAILIF (0 != TALER_amount_cmp (&rbalance2,
&rbalance));
FAILIF (0 != TALER_amount_cmp (&dbalance2,
&dbalance));
plugin->rollback (plugin->cls);

View File

@ -706,6 +706,20 @@ prepare_statements (struct PostgresClosure *pg)
" WHERE NOT executed"
" ORDER BY trigger_date ASC;",
0),
/* Used in #postgres_profit_drains_get() */
GNUNET_PQ_make_prepare (
"get_profit_drain",
"SELECT"
" profit_drain_serial_id"
",account_section"
",payto_uri"
",trigger_date"
",amount_val"
",amount_frac"
",master_sig"
" FROM profit_drains"
" WHERE wtid=$1;",
1),
/* Used in #postgres_profit_drains_set_finished() */
GNUNET_PQ_make_prepare (
"drain_profit_set_finished",
@ -16322,6 +16336,58 @@ postgres_profit_drains_get_pending (
}
/**
* Function called to get information about a profit drain event.
*
* @param cls the @e cls of this struct with the plugin-specific state
* @param wtid wire transfer ID to look up drain event for
* @param[out] serial set to serial ID of the entry
* @param[out] account_section set to account to drain
* @param[out] payto_uri set to account to wire funds to
* @param[out] request_timestamp set to time of the signature
* @param[out] amount set to amount to wire
* @param[out] master_sig set to signature affirming the operation
* @return transaction status code
*/
static enum GNUNET_DB_QueryStatus
postgres_get_drain_profit (
void *cls,
const struct TALER_WireTransferIdentifierRawP *wtid,
uint64_t *serial,
char **account_section,
char **payto_uri,
struct GNUNET_TIME_Timestamp *request_timestamp,
struct TALER_Amount *amount,
struct TALER_MasterSignatureP *master_sig)
{
struct PostgresClosure *pg = cls;
struct GNUNET_PQ_QueryParam params[] = {
GNUNET_PQ_query_param_auto_from_type (wtid),
GNUNET_PQ_query_param_end
};
struct GNUNET_PQ_ResultSpec rs[] = {
GNUNET_PQ_result_spec_uint64 ("profit_drain_serial_id",
serial),
GNUNET_PQ_result_spec_string ("account_section",
account_section),
GNUNET_PQ_result_spec_string ("payto_uri",
payto_uri),
GNUNET_PQ_result_spec_timestamp ("trigger_date",
request_timestamp),
TALER_PQ_RESULT_SPEC_AMOUNT ("amount",
amount),
GNUNET_PQ_result_spec_auto_from_type ("master_sig",
master_sig),
GNUNET_PQ_result_spec_end
};
return GNUNET_PQ_eval_prepared_singleton_select (pg->conn,
"get_profit_drain",
params,
rs);
}
/**
* Set profit drain operation to finished.
*
@ -16666,6 +16732,8 @@ libtaler_plugin_exchangedb_postgres_init (void *cls)
= &postgres_insert_drain_profit;
plugin->profit_drains_get_pending
= &postgres_profit_drains_get_pending;
plugin->get_drain_profit
= &postgres_get_drain_profit;
plugin->profit_drains_set_finished
= &postgres_profit_drains_set_finished;
return plugin;

View File

@ -1335,12 +1335,14 @@ struct TALER_AUDITORDB_Plugin
* @param cls the @e cls of this struct with the plugin-specific state
* @param master_pub master key of the exchange
* @param balance what the bank account balance of the exchange should show
* @param drained_profits total profits drained by the exchange so far
* @return transaction status code
*/
enum GNUNET_DB_QueryStatus
(*insert_predicted_result)(void *cls,
const struct TALER_MasterPublicKeyP *master_pub,
const struct TALER_Amount *balance);
const struct TALER_Amount *balance,
const struct TALER_Amount *drained_profits);
/**
@ -1350,12 +1352,14 @@ struct TALER_AUDITORDB_Plugin
* @param cls the @e cls of this struct with the plugin-specific state
* @param master_pub master key of the exchange
* @param balance what the bank account balance of the exchange should show
* @param drained_profits total profits drained by the exchange so far
* @return transaction status code
*/
enum GNUNET_DB_QueryStatus
(*update_predicted_result)(void *cls,
const struct TALER_MasterPublicKeyP *master_pub,
const struct TALER_Amount *balance);
const struct TALER_Amount *balance,
const struct TALER_Amount *drained_profits);
/**
@ -1364,12 +1368,14 @@ struct TALER_AUDITORDB_Plugin
* @param cls the @e cls of this struct with the plugin-specific state
* @param master_pub master key of the exchange
* @param[out] balance expected bank account balance of the exchange
* @param[out] drained_profits total profits drained by the exchange so far
* @return transaction status code
*/
enum GNUNET_DB_QueryStatus
(*get_predicted_balance)(void *cls,
const struct TALER_MasterPublicKeyP *master_pub,
struct TALER_Amount *balance);
struct TALER_Amount *balance,
struct TALER_Amount *drained_profits);
};

View File

@ -5545,6 +5545,30 @@ struct TALER_EXCHANGEDB_Plugin
const struct TALER_MasterSignatureP *master_sig);
/**
* Function called to get information about a profit drain event.
*
* @param cls the @e cls of this struct with the plugin-specific state
* @param wtid wire transfer ID to look up drain event for
* @param[out] serial set to serial ID of the entry
* @param[out] account_section set to account to drain
* @param[out] payto_uri set to account to wire funds to
* @param[out] request_timestamp set to time of the signature
* @param[out] amount set to amount to wire
* @param[out] master_sig set to signature affirming the operation
* @return transaction status code
*/
enum GNUNET_DB_QueryStatus
(*get_drain_profit)(void *cls,
const struct TALER_WireTransferIdentifierRawP *wtid,
uint64_t *serial,
char **account_section,
char **payto_uri,
struct GNUNET_TIME_Timestamp *request_timestamp,
struct TALER_Amount *amount,
struct TALER_MasterSignatureP *master_sig);
/**
* Get profit drain operation ready to execute.
*