-work on new DB queries

This commit is contained in:
Christian Grothoff 2022-12-30 21:28:19 +01:00
parent 5754adc414
commit b1d9745545
No known key found for this signature in database
GPG Key ID: 939E6BE1E29FC3CC
5 changed files with 289 additions and 14 deletions

View File

@ -26,7 +26,6 @@
#include "pg_helper.h"
enum GNUNET_DB_QueryStatus
TEH_PG_lookup_signkey_revocation (
void *cls,
@ -44,7 +43,6 @@ TEH_PG_lookup_signkey_revocation (
GNUNET_PQ_result_spec_end
};
PREPARE (pg,
"lookup_signkey_revocation",
"SELECT "

View File

@ -48,7 +48,7 @@ struct WireTransferResultContext
/**
* Set to #GNUNET_SYSERR on serious errors.
*/
int status;
enum GNUNET_GenericReturnValue status;
};
@ -141,13 +141,14 @@ TEH_PG_lookup_wire_transfer (
GNUNET_PQ_query_param_auto_from_type (wtid),
GNUNET_PQ_query_param_end
};
struct WireTransferResultContext ctx;
struct WireTransferResultContext ctx = {
.cb = cb,
.cb_cls = cb_cls,
.pg = pg,
.status = GNUNET_OK
};
enum GNUNET_DB_QueryStatus qs;
ctx.cb = cb;
ctx.cb_cls = cb_cls;
ctx.pg = pg;
ctx.status = GNUNET_OK;
PREPARE (pg,
"lookup_transactions",
"SELECT"

View File

@ -25,6 +25,96 @@
#include "pg_select_aml_history.h"
#include "pg_helper.h"
/**
* Closure for #handle_aml_result.
*/
struct AmlHistoryResultContext
{
/**
* Function to call on each result.
*/
TALER_EXCHANGEDB_AmlHistoryCallback cb;
/**
* Closure for @e cb.
*/
void *cb_cls;
/**
* Plugin context.
*/
struct PostgresClosure *pg;
/**
* Set to #GNUNET_SYSERR on serious errors.
*/
enum GNUNET_GenericReturnValue status;
};
/**
* Function to be called with the results of a SELECT statement
* that has returned @a num_results results. Helper function
* for #TEH_PG_select_aml_history().
*
* @param cls closure of type `struct AmlHistoryResultContext *`
* @param result the postgres result
* @param num_results the number of results in @a result
*/
static void
handle_aml_result (void *cls,
PGresult *result,
unsigned int num_results)
{
struct AmlHistoryResultContext *ctx = cls;
struct PostgresClosure *pg = ctx->pg;
for (unsigned int i = 0; i<num_results; i++)
{
struct TALER_Amount new_threshold;
uint32_t ns;
struct GNUNET_TIME_Absolute decision_time;
char *justification;
struct TALER_AmlOfficerPublicKeyP decider_pub;
struct TALER_AmlOfficerSignatureP decider_sig;
struct GNUNET_PQ_ResultSpec rs[] = {
TALER_PQ_RESULT_SPEC_AMOUNT ("new_threshold",
&new_threshold),
GNUNET_PQ_result_spec_uint32 ("new_status",
&ns),
GNUNET_PQ_result_spec_absolute_time ("decision_time",
&decision_time),
GNUNET_PQ_result_spec_string ("justification",
&justification),
GNUNET_PQ_result_spec_auto_from_type ("decider_pub",
&decider_pub),
GNUNET_PQ_result_spec_auto_from_type ("decider_sig",
&decider_sig),
GNUNET_PQ_result_spec_end
};
if (GNUNET_OK !=
GNUNET_PQ_extract_result (result,
rs,
i))
{
GNUNET_break (0);
ctx->status = GNUNET_SYSERR;
return;
}
ctx->cb (ctx->cb_cls,
&new_threshold,
(enum TALER_AmlDecisionState) ns,
decision_time,
justification,
&decider_pub,
&decider_sig);
GNUNET_PQ_cleanup_result (rs);
}
}
enum GNUNET_DB_QueryStatus
TEH_PG_select_aml_history (
void *cls,
@ -32,6 +122,42 @@ TEH_PG_select_aml_history (
TALER_EXCHANGEDB_AmlHistoryCallback cb,
void *cb_cls)
{
GNUNET_break (0); // FIXME: not implemeted!
return GNUNET_DB_STATUS_HARD_ERROR;
struct PostgresClosure *pg = cls;
struct GNUNET_PQ_QueryParam params[] = {
GNUNET_PQ_query_param_auto_from_type (h_payto),
GNUNET_PQ_query_param_end
};
struct AmlHistoryResultContext ctx = {
.cb = cb,
.cb_cls = cb_cls,
.pg = pg,
.status = GNUNET_OK
};
enum GNUNET_DB_QueryStatus qs;
PREPARE (pg,
"lookup_aml_history",
"SELECT"
" aggregation_serial_id"
",deposits.h_contract_terms"
",payto_uri"
",wire_targets.wire_target_h_payto"
",kc.coin_pub"
",deposits.merchant_pub"
",wire_out.execution_date"
",deposits.amount_with_fee_val"
",deposits.amount_with_fee_frac"
",denom.fee_deposit_val"
",denom.fee_deposit_frac"
",denom.denom_pub"
" FROM aml_history"
" WHERE h_payto=$1;");
qs = GNUNET_PQ_eval_prepared_multi_select (pg->conn,
"lookup_aml_history",
params,
&handle_aml_result,
&ctx);
if (GNUNET_OK != ctx.status)
return GNUNET_DB_STATUS_HARD_ERROR;
return qs;
}

View File

@ -26,6 +26,87 @@
#include "pg_helper.h"
/**
* Closure for #handle_aml_result.
*/
struct AmlProcessResultContext
{
/**
* Function to call on each result.
*/
TALER_EXCHANGEDB_AmlStatusCallback cb;
/**
* Closure for @e cb.
*/
void *cb_cls;
/**
* Plugin context.
*/
struct PostgresClosure *pg;
/**
* Set to #GNUNET_SYSERR on serious errors.
*/
enum GNUNET_GenericReturnValue status;
};
/**
* Function to be called with the results of a SELECT statement
* that has returned @a num_results results. Helper function
* for #TEH_PG_select_aml_process().
*
* @param cls closure of type `struct AmlProcessResultContext *`
* @param result the postgres result
* @param num_results the number of results in @a result
*/
static void
handle_aml_result (void *cls,
PGresult *result,
unsigned int num_results)
{
struct AmlProcessResultContext *ctx = cls;
struct PostgresClosure *pg = ctx->pg;
for (unsigned int i = 0; i<num_results; i++)
{
struct TALER_PaytoHashP h_payto;
struct TALER_Amount threshold;
uint64_t rowid;
uint32_t sv;
struct GNUNET_PQ_ResultSpec rs[] = {
GNUNET_PQ_result_spec_uint64 ("aml_status_serial_id",
&rowid),
GNUNET_PQ_result_spec_auto_from_type ("h_payto",
&h_payto),
TALER_PQ_RESULT_SPEC_AMOUNT ("threshold",
&threshold),
GNUNET_PQ_result_spec_uint32 ("status",
&sv),
GNUNET_PQ_result_spec_end
};
if (GNUNET_OK !=
GNUNET_PQ_extract_result (result,
rs,
i))
{
GNUNET_break (0);
ctx->status = GNUNET_SYSERR;
return;
}
ctx->cb (ctx->cb_cls,
rowid,
&h_payto,
&threshold,
(enum TALER_AmlDecisionState) sv);
GNUNET_PQ_cleanup_result (rs);
}
}
enum GNUNET_DB_QueryStatus
TEH_PG_select_aml_process (
void *cls,
@ -35,6 +116,53 @@ TEH_PG_select_aml_process (
TALER_EXCHANGEDB_AmlStatusCallback cb,
void *cb_cls)
{
GNUNET_break (0); // FIXME: not implemeted!
return GNUNET_DB_STATUS_HARD_ERROR;
struct PostgresClosure *pg = cls;
struct GNUNET_PQ_QueryParam params[] = {
GNUNET_PQ_query_param_uint32 (&decision),
GNUNET_PQ_query_param_uint64 (&row_off),
GNUNET_PQ_query_param_end
};
struct AmlProcessResultContext ctx = {
.cb = cb,
.cb_cls = cb_cls,
.pg = pg,
.status = GNUNET_OK
};
enum GNUNET_DB_QueryStatus qs;
const char *stmt = forward
? "select_aml_process_inc"
: "select_aml_process_dec";
PREPARE (pg,
"select_aml_process_inc",
"SELECT"
" aml_status_serial_id"
",h_payto"
",threshold_var"
",threshold_frac"
",status"
" FROM aml_status"
" WHERE aml_status_serial_id > $2"
" AND $1 = status & $1"
" ORDER BY aml_status_serial_id INC");
PREPARE (pg,
"select_aml_process_dec",
"SELECT"
" aml_status_serial_id"
",h_payto"
",threshold_var"
",threshold_frac"
",status"
" FROM aml_status"
" WHERE aml_status_serial_id < $2"
" AND $1 = status & $1"
" ORDER BY aml_status_serial_id DESC");
qs = GNUNET_PQ_eval_prepared_multi_select (pg->conn,
stmt,
params,
&handle_aml_result,
&ctx);
if (GNUNET_OK != ctx.status)
return GNUNET_DB_STATUS_HARD_ERROR;
return qs;
}

View File

@ -32,6 +32,28 @@ TEH_PG_trigger_aml_process (
const struct TALER_PaytoHashP *h_payto,
const struct TALER_Amount *threshold_crossed)
{
GNUNET_break (0); // FIXME: not implemeted!
return GNUNET_DB_STATUS_HARD_ERROR;
struct PostgresClosure *pg = cls;
struct GNUNET_PQ_QueryParam params[] = {
GNUNET_PQ_query_param_auto_from_type (h_payto),
TALER_PQ_query_param_amount (threshold_crossed),
GNUNET_PQ_query_param_end
};
PREPARE (pg,
"trigger_aml_process",
"INSERT INTO aml_status"
"(h_payto"
",threshold_val"
",threshold_frac"
",status)"
"VALUES"
"($1, $2, $3, 1)" // 1: decision needed
"ON CONFLICT DO"
" UPDATE SET"
" threshold_val=$2"
" ,threshold_frac=$3"
" ,status=status | 1;"); // do not clear 'frozen' status
return GNUNET_PQ_eval_prepared_non_select (pg->conn,
"trigger_aml_process",
params);
}