summaryrefslogtreecommitdiff
path: root/src/exchangedb/plugin_exchangedb_postgres.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/exchangedb/plugin_exchangedb_postgres.c')
-rw-r--r--src/exchangedb/plugin_exchangedb_postgres.c254
1 files changed, 247 insertions, 7 deletions
diff --git a/src/exchangedb/plugin_exchangedb_postgres.c b/src/exchangedb/plugin_exchangedb_postgres.c
index 74dc8779..dfd7432e 100644
--- a/src/exchangedb/plugin_exchangedb_postgres.c
+++ b/src/exchangedb/plugin_exchangedb_postgres.c
@@ -347,6 +347,30 @@ prepare_statements (struct PostgresClosure *pg)
" WHERE denom_pub_hash=$1);",
1),
/* Used in #postgres_reserves_get() */
+ GNUNET_PQ_make_prepare ("reserves_get_with_kyc",
+ "SELECT"
+ " current_balance_val"
+ ",current_balance_frac"
+ ",expiration_date"
+ ",gc_date"
+ ",FALSE AS kyc_ok" // FIXME
+ ",CAST (0 AS INT8) AS payment_target_uuid" // FIXME
+ " FROM reserves"
+ " WHERE reserve_pub=$1"
+ " LIMIT 1;",
+ 1),
+#if FIXME_DD23
+ /* Used in #postgres_get_kyc_status() */
+ GNUNET_PQ_make_prepare ("get_kyc_status",
+ "SELECT"
+ ",kyc_ok"
+ ",wire_target_serial_id AS payment_target_uuid"
+ " FROM wire_targets"
+ " WHERE payto_uri=$1"
+ " LIMIT 1;",
+ 1),
+#endif
+ /* Used in #reserves_get() */
GNUNET_PQ_make_prepare ("reserves_get",
"SELECT"
" current_balance_val"
@@ -3464,10 +3488,100 @@ postgres_iterate_auditor_denominations (
* @param[in,out] reserve the reserve data. The public key of the reserve should be
* set in this structure; it is used to query the database. The balance
* and expiration are then filled accordingly.
+ * @param[out] kyc set to the KYC status of the reserve
* @return transaction status
*/
static enum GNUNET_DB_QueryStatus
postgres_reserves_get (void *cls,
+ struct TALER_EXCHANGEDB_Reserve *reserve,
+ struct TALER_EXCHANGEDB_KycStatus *kyc)
+{
+ struct PostgresClosure *pg = cls;
+ struct GNUNET_PQ_QueryParam params[] = {
+ GNUNET_PQ_query_param_auto_from_type (&reserve->pub),
+ GNUNET_PQ_query_param_end
+ };
+ uint8_t ok8;
+ struct GNUNET_PQ_ResultSpec rs[] = {
+ TALER_PQ_RESULT_SPEC_AMOUNT ("current_balance",
+ &reserve->balance),
+ TALER_PQ_result_spec_absolute_time ("expiration_date",
+ &reserve->expiry),
+ TALER_PQ_result_spec_absolute_time ("gc_date",
+ &reserve->gc),
+ GNUNET_PQ_result_spec_uint64 ("payment_target_uuid",
+ &kyc->payment_target_uuid),
+ GNUNET_PQ_result_spec_auto_from_type ("kyc_ok",
+ &ok8),
+ GNUNET_PQ_result_spec_end
+ };
+ enum GNUNET_DB_QueryStatus qs;
+
+ qs = GNUNET_PQ_eval_prepared_singleton_select (pg->conn,
+ "reserves_get_with_kyc",
+ params,
+ rs);
+ kyc->type = TALER_EXCHANGEDB_KYC_WITHDRAW;
+ kyc->ok = (0 != ok8);
+ return qs;
+}
+
+
+/**
+ * Get the KYC status for a bank account.
+ *
+ * @param cls the @e cls of this struct with the plugin-specific state
+ * @param payto_uri payto:// URI that identifies the bank account
+ * @param[out] kyc set to the KYC status of the reserve
+ * @return transaction status
+ */
+static enum GNUNET_DB_QueryStatus
+postgres_get_kyc_status (void *cls,
+ const char *payto_uri,
+ struct TALER_EXCHANGEDB_KycStatus *kyc)
+{
+ struct PostgresClosure *pg = cls;
+ struct GNUNET_PQ_QueryParam params[] = {
+ GNUNET_PQ_query_param_string (payto_uri),
+ GNUNET_PQ_query_param_end
+ };
+ uint8_t ok8;
+ struct GNUNET_PQ_ResultSpec rs[] = {
+ GNUNET_PQ_result_spec_uint64 ("payment_target_uuid",
+ &kyc->payment_target_uuid),
+ GNUNET_PQ_result_spec_auto_from_type ("kyc_ok",
+ &ok8),
+ GNUNET_PQ_result_spec_end
+ };
+ enum GNUNET_DB_QueryStatus qs;
+
+#if FIXME_DD23
+ qs = GNUNET_PQ_eval_prepared_singleton_select (pg->conn,
+ "get_kyc_status",
+ params,
+ rs);
+#else
+ qs = 1;
+ ok8 = 0;
+ kyc->payment_target_uuid = 0;
+#endif
+ kyc->type = TALER_EXCHANGEDB_KYC_DEPOSIT;
+ kyc->ok = (0 != ok8);
+ return qs;
+}
+
+
+/**
+ * Get the summary of a reserve.
+ *
+ * @param cls the `struct PostgresClosure` with the plugin-specific state
+ * @param[in,out] reserve the reserve data. The public key of the reserve should be
+ * set in this structure; it is used to query the database. The balance
+ * and expiration are then filled accordingly.
+ * @return transaction status
+ */
+static enum GNUNET_DB_QueryStatus
+reserves_get_internal (void *cls,
struct TALER_EXCHANGEDB_Reserve *reserve)
{
struct PostgresClosure *pg = cls;
@@ -3476,9 +3590,12 @@ postgres_reserves_get (void *cls,
GNUNET_PQ_query_param_end
};
struct GNUNET_PQ_ResultSpec rs[] = {
- TALER_PQ_RESULT_SPEC_AMOUNT ("current_balance", &reserve->balance),
- TALER_PQ_result_spec_absolute_time ("expiration_date", &reserve->expiry),
- TALER_PQ_result_spec_absolute_time ("gc_date", &reserve->gc),
+ TALER_PQ_RESULT_SPEC_AMOUNT ("current_balance",
+ &reserve->balance),
+ TALER_PQ_result_spec_absolute_time ("expiration_date",
+ &reserve->expiry),
+ TALER_PQ_result_spec_absolute_time ("gc_date",
+ &reserve->gc),
GNUNET_PQ_result_spec_end
};
@@ -3699,7 +3816,7 @@ postgres_reserves_in_insert (void *cls,
{
enum GNUNET_DB_QueryStatus reserve_exists;
- reserve_exists = postgres_reserves_get (pg,
+ reserve_exists = reserves_get_internal (pg,
&reserve);
switch (reserve_exists)
{
@@ -3916,7 +4033,7 @@ postgres_insert_withdraw_info (
/* update reserve balance */
reserve.pub = collectable->reserve_pub;
if (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT !=
- (qs = postgres_reserves_get (pg,
+ (qs = reserves_get_internal (pg,
&reserve)))
{
/* Should have been checked before we got here... */
@@ -4327,6 +4444,126 @@ postgres_get_reserve_history (void *cls,
/**
+ * Closure for withdraw_amount_by_account_cb()
+ */
+struct WithdrawAmountByAccountContext
+{
+ /**
+ * Function to call on each amount.
+ */
+ TALER_EXCHANGEDB_WithdrawHistoryCallback cb;
+
+ /**
+ * Closure for @e cb
+ */
+ void *cb_cls;
+
+ /**
+ * Our plugin's context.
+ */
+ struct PostgresClosure *pg;
+
+ /**
+ * Set to true on failures.
+ */
+ bool failed;
+};
+
+
+/**
+ * Helper function for #postgres_select_withdraw_amounts_by_account().
+ * To be called with the results of a SELECT statement
+ * that has returned @a num_results results.
+ *
+ * @param cls closure of type `struct WithdrawAmountByAccountContext *`
+ * @param result the postgres result
+ * @param num_results the number of results in @a result
+ */
+static void
+withdraw_amount_by_account_cb (void *cls,
+ PGresult *result,
+ unsigned int num_results)
+{
+ struct WithdrawAmountByAccountContext *wac = cls;
+ struct PostgresClosure *pg = wac->pg;
+
+ for (unsigned int i = 0; num_results; i++)
+ {
+ struct TALER_Amount val;
+ struct GNUNET_PQ_ResultSpec rs[] = {
+ TALER_PQ_RESULT_SPEC_AMOUNT ("val",
+ &val),
+ GNUNET_PQ_result_spec_end
+ };
+
+ if (GNUNET_OK !=
+ GNUNET_PQ_extract_result (result,
+ rs,
+ i))
+ {
+ GNUNET_break (0);
+ wac->failed = true;
+ return;
+ }
+ wac->cb (wac->cb_cls,
+ &val);
+ }
+}
+
+
+/**
+ * Find out all of the amounts that have been withdrawn
+ * so far from the same bank account that created the
+ * given reserve.
+ *
+ * @param cls the `struct PostgresClosure` with the plugin-specific state
+ * @param reserve_pub reserve to select withdrawals by
+ * @param duration how far back should we select withdrawals
+ * @param cb function to call on each amount withdrawn
+ * @param cb_cls closure for @a cb
+ * @return transaction status
+ */
+static enum GNUNET_DB_QueryStatus
+postgres_select_withdraw_amounts_by_account (
+ void *cls,
+ const struct TALER_ReservePublicKeyP *reserve_pub,
+ struct GNUNET_TIME_Relative duration,
+ TALER_EXCHANGEDB_WithdrawHistoryCallback cb,
+ void *cb_cls)
+{
+ struct PostgresClosure *pg = cls;
+ struct WithdrawAmountByAccountContext wac = {
+ .pg = pg,
+ .cb = cb,
+ .cb_cls = cb_cls
+ };
+ struct GNUNET_TIME_Absolute start
+ = GNUNET_TIME_absolute_subtract (GNUNET_TIME_absolute_get (),
+ duration);
+ struct GNUNET_PQ_QueryParam params[] = {
+ GNUNET_PQ_query_param_auto_from_type (reserve_pub),
+ GNUNET_PQ_query_param_absolute_time (&start),
+ GNUNET_PQ_query_param_end
+ };
+ enum GNUNET_DB_QueryStatus qs;
+
+ qs = GNUNET_PQ_eval_prepared_multi_select (
+ pg->conn,
+ "select_XXX",
+ params,
+ &withdraw_amount_by_account_cb,
+ &wac);
+
+ if (wac.failed)
+ {
+ GNUNET_break (0);
+ qs = GNUNET_DB_STATUS_HARD_ERROR;
+ }
+ return qs;
+}
+
+
+/**
* Check if we have the specified deposit already in the database.
*
* @param cls the `struct PostgresClosure` with the plugin-specific state
@@ -6875,7 +7112,7 @@ postgres_insert_reserve_closed (
/* update reserve balance */
reserve.pub = *reserve_pub;
if (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT !=
- (qs = postgres_reserves_get (cls,
+ (qs = reserves_get_internal (cls,
&reserve)))
{
/* Existence should have been checked before we got here... */
@@ -8667,7 +8904,7 @@ postgres_insert_recoup_request (
/* Update reserve balance */
reserve.pub = *reserve_pub;
- qs = postgres_reserves_get (pg,
+ qs = reserves_get_internal (pg,
&reserve);
if (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT != qs)
{
@@ -10889,12 +11126,15 @@ libtaler_plugin_exchangedb_postgres_init (void *cls)
plugin->iterate_auditor_denominations =
&postgres_iterate_auditor_denominations;
plugin->reserves_get = &postgres_reserves_get;
+ plugin->get_kyc_status = &postgres_get_kyc_status;
plugin->reserves_in_insert = &postgres_reserves_in_insert;
plugin->get_latest_reserve_in_reference =
&postgres_get_latest_reserve_in_reference;
plugin->get_withdraw_info = &postgres_get_withdraw_info;
plugin->insert_withdraw_info = &postgres_insert_withdraw_info;
plugin->get_reserve_history = &postgres_get_reserve_history;
+ plugin->select_withdraw_amounts_by_account
+ = &postgres_select_withdraw_amounts_by_account;
plugin->free_reserve_history = &common_free_reserve_history;
plugin->count_known_coins = &postgres_count_known_coins;
plugin->ensure_coin_known = &postgres_ensure_coin_known;