diff options
Diffstat (limited to 'src/backenddb/plugin_merchantdb_postgres.c')
-rw-r--r-- | src/backenddb/plugin_merchantdb_postgres.c | 325 |
1 files changed, 278 insertions, 47 deletions
diff --git a/src/backenddb/plugin_merchantdb_postgres.c b/src/backenddb/plugin_merchantdb_postgres.c index c196790c..8e402d31 100644 --- a/src/backenddb/plugin_merchantdb_postgres.c +++ b/src/backenddb/plugin_merchantdb_postgres.c @@ -2848,6 +2848,7 @@ RETRY: { struct GNUNET_PQ_QueryParam params[] = { GNUNET_PQ_query_param_uint64 (&credit_serial), + TALER_PQ_query_param_amount (&td->wire_fee), GNUNET_PQ_query_param_absolute_time (&td->execution_time), GNUNET_PQ_query_param_auto_from_type (&td->exchange_sig), GNUNET_PQ_query_param_auto_from_type (&td->exchange_pub), @@ -3118,14 +3119,109 @@ postgres_lookup_deposits_by_contract_and_coin ( /** - * Closure for #lookup_transfer_details_cb(). + * Lookup transfer status. + * + * @param cls closure + * @param exchange_url the exchange that made the transfer + * @param wtid wire transfer subject + * @param[out] total_amount amount that was transferred (in total, minus @a wire_fee) + * @param[out] wire_fee the wire fee the exchange charged + * @param[out] execution_time when the transfer was executed by the exchange + * @param[out] verified did we confirm the transfer was OK + * @return transaction status */ -struct LookupTransferDetailsContext +static enum GNUNET_DB_QueryStatus +postgres_lookup_transfer ( + void *cls, + const char *exchange_url, + const struct TALER_WireTransferIdentifierRawP *wtid, + struct TALER_Amount *total_amount, + struct TALER_Amount *wire_fee, + struct GNUNET_TIME_Absolute *execution_time, + bool *verified) +{ + struct PostgresClosure *pg = cls; + struct GNUNET_PQ_QueryParam params[] = { + GNUNET_PQ_query_param_string (exchange_url), + GNUNET_PQ_query_param_auto_from_type (wtid), + GNUNET_PQ_query_param_end + }; + uint8_t verified8; + struct TALER_Amount credit_amount; + struct GNUNET_PQ_ResultSpec rs[] = { + TALER_PQ_RESULT_SPEC_AMOUNT ("credit_amount", + &credit_amount), + TALER_PQ_RESULT_SPEC_AMOUNT ("wire_fee", + wire_fee), + GNUNET_PQ_result_spec_absolute_time ("execution_time", + execution_time), + GNUNET_PQ_result_spec_auto_from_type ("verified", + &verified8), + GNUNET_PQ_result_spec_end + }; + enum GNUNET_DB_QueryStatus qs; + + check_connection (pg); + qs = GNUNET_PQ_eval_prepared_singleton_select (pg->conn, + "lookup_transfer", + params, + rs); + if (qs > 0) + { + *verified = (0 != verified8); + // FIXME: unclear if table stores 'total' including or excluding fee :-(. + // Check and update DOCS and taler_exchange_service.h header! + if (0 > + TALER_amount_add (total_amount, + &credit_amount, + wire_fee)) + { + GNUNET_break (0); + return GNUNET_DB_STATUS_HARD_ERROR; + } + } + return qs; +} + + +/** + * Set transfer status to verified. + * + * @param cls closure + * @param exchange_url the exchange that made the transfer + * @param wtid wire transfer subject + * @return transaction status + */ +static enum GNUNET_DB_QueryStatus +postgres_set_transfer_status_to_verified ( + void *cls, + const char *exchange_url, + const struct TALER_WireTransferIdentifierRawP *wtid) +{ + struct PostgresClosure *pg = cls; + struct GNUNET_PQ_QueryParam params[] = { + GNUNET_PQ_query_param_auto_from_type (wtid), + GNUNET_PQ_query_param_string (exchange_url), + GNUNET_PQ_query_param_end + }; + + check_connection (pg); + return GNUNET_PQ_eval_prepared_non_select ( + pg->conn, + "set_transfer_status_to_verified", + params); +} + + +/** + * Closure for #lookup_transfer_summary_cb(). + */ +struct LookupTransferSummaryContext { /** * Function to call for each order that was aggregated. */ - TALER_MERCHANTDB_TransferDetailsCallback cb; + TALER_MERCHANTDB_TransferSummaryCallback cb; /** * Closure for @e cb. @@ -3148,16 +3244,16 @@ struct LookupTransferDetailsContext * Function to be called with the results of a SELECT statement * that has returned @a num_results results. * - * @param cls of type `struct LookupTransferDetailsContext *` + * @param cls of type `struct LookupTransferSummaryContext *` * @param result the postgres result * @param num_result the number of results in @a result */ static void -lookup_transfer_details_cb (void *cls, +lookup_transfer_summary_cb (void *cls, PGresult *result, unsigned int num_results) { - struct LookupTransferDetailsContext *ltdc = cls; + struct LookupTransferSummaryContext *ltdc = cls; struct PostgresClosure *pg = ltdc->pg; for (unsigned int i = 0; i<num_results; i++) @@ -3195,16 +3291,134 @@ lookup_transfer_details_cb (void *cls, /** + * Lookup transfer summary. + * + * @param cls closure + * @param exchange_url the exchange that made the transfer + * @param wtid wire transfer subject + * @param cb function to call with detailed transfer data + * @param cb_cls closure for @a cb + * @return transaction status + */ +static enum GNUNET_DB_QueryStatus +postgres_lookup_transfer_summary ( + void *cls, + const char *exchange_url, + const struct TALER_WireTransferIdentifierRawP *wtid, + TALER_MERCHANTDB_TransferSummaryCallback cb, + void *cb_cls) +{ + struct PostgresClosure *pg = cls; + struct GNUNET_PQ_QueryParam params[] = { + GNUNET_PQ_query_param_string (exchange_url), + GNUNET_PQ_query_param_auto_from_type (wtid), + GNUNET_PQ_query_param_end + }; + struct LookupTransferSummaryContext ltdc = { + .cb = cb, + .cb_cls = cb_cls, + .pg = pg + }; + enum GNUNET_DB_QueryStatus qs; + + check_connection (pg); + qs = GNUNET_PQ_eval_prepared_multi_select ( + pg->conn, + "lookup_transfer_summary", + params, + &lookup_transfer_summary_cb, + <dc); + if (0 >= qs) + return qs; + return ltdc.qs; +} + + +/** + * Closure for #lookup_transfer_details_cb(). + */ +struct LookupTransferDetailsContext +{ + /** + * Function to call for each order that was aggregated. + */ + TALER_MERCHANTDB_TransferDetailsCallback cb; + + /** + * Closure for @e cb. + */ + void *cb_cls; + + /** + * Plugin context. + */ + struct PostgresClosure *pg; + + /** + * Transaction result. + */ + enum GNUNET_DB_QueryStatus qs; +}; + + +/** + * Function to be called with the results of a SELECT statement + * that has returned @a num_results results. + * + * @param cls of type `struct LookupTransferDetailsContext *` + * @param result the postgres result + * @param num_result the number of results in @a result + */ +static void +lookup_transfer_details_cb (void *cls, + PGresult *result, + unsigned int num_results) +{ + struct LookupTransferDetailsContext *ltdc = cls; + struct PostgresClosure *pg = ltdc->pg; + + for (unsigned int i = 0; i<num_results; i++) + { + uint32_t current_offset; + struct TALER_TrackTransferDetails ttd; + struct GNUNET_PQ_ResultSpec rs[] = { + GNUNET_PQ_result_spec_uint32 ("offset_in_exchange_list", + ¤t_offset), + GNUNET_PQ_result_spec_auto_from_type ("h_contract_terms", + &ttd.h_contract_terms), + GNUNET_PQ_result_spec_auto_from_type ("coin_pub", + &ttd.coin_pub), + TALER_PQ_RESULT_SPEC_AMOUNT ("exchange_deposit_value", + &ttd.coin_value), + TALER_PQ_RESULT_SPEC_AMOUNT ("exchange_deposit_fee", + &ttd.coin_fee), + GNUNET_PQ_result_spec_end + }; + + if (GNUNET_OK != + GNUNET_PQ_extract_result (result, + rs, + i)) + { + GNUNET_break (0); + ltdc->qs = GNUNET_DB_STATUS_HARD_ERROR; + return; + } + ltdc->qs = i + 1; + ltdc->cb (ltdc->cb_cls, + (unsigned int) current_offset, + &ttd); + GNUNET_PQ_cleanup_result (rs); + } +} + + +/** * Lookup transfer details. * * @param cls closure - * @param instance_id instance to lookup payments for * @param exchange_url the exchange that made the transfer - * @param payto_uri account that received the transfer * @param wtid wire transfer subject - * @param total_amount amount that was transferred (in total, minus @a wire_fee) - * @param wire_fee the wire fee the exchange charged - * @param execution_time when the transfer was executed by the exchange * @param cb function to call with detailed transfer data * @param cb_cls closure for @a cb * @return transaction status @@ -3212,25 +3426,15 @@ lookup_transfer_details_cb (void *cls, static enum GNUNET_DB_QueryStatus postgres_lookup_transfer_details ( void *cls, - const char *instance_id, const char *exchange_url, - const char *payto_uri, const struct TALER_WireTransferIdentifierRawP *wtid, - const struct TALER_Amount *total_amount, - const struct TALER_Amount *wire_fee, - struct GNUNET_TIME_Absolute execution_time, TALER_MERCHANTDB_TransferDetailsCallback cb, void *cb_cls) { struct PostgresClosure *pg = cls; - struct TALER_Amount transfer_amount; struct GNUNET_PQ_QueryParam params[] = { - GNUNET_PQ_query_param_string (instance_id), GNUNET_PQ_query_param_string (exchange_url), - GNUNET_PQ_query_param_string (payto_uri), GNUNET_PQ_query_param_auto_from_type (wtid), - TALER_PQ_query_param_amount (&transfer_amount), - GNUNET_PQ_query_param_absolute_time (&execution_time), GNUNET_PQ_query_param_end }; struct LookupTransferDetailsContext ltdc = { @@ -3240,14 +3444,6 @@ postgres_lookup_transfer_details ( }; enum GNUNET_DB_QueryStatus qs; - if (0 > - TALER_amount_subtract (&transfer_amount, - total_amount, - wire_fee)) - { - GNUNET_break (0); - return GNUNET_DB_STATUS_HARD_ERROR; - } check_connection (pg); qs = GNUNET_PQ_eval_prepared_multi_select ( pg->conn, @@ -5687,14 +5883,16 @@ libtaler_plugin_merchantdb_postgres_init (void *cls) "INSERT INTO merchant_transfer_signatures" "(credit_serial" ",signkey_serial" + ",wire_fee_val" + ",wire_fee_frac" ",execution_time" ",exchange_sig) " - "SELECT $1, signkey_serial, $2, $3" + "SELECT $1, signkey_serial, $2, $3, $4, $5" " FROM merchant_exchange_signing_keys" - " WHERE exchange_pub=$4" + " WHERE exchange_pub=$6" " ORDER BY start_date DESC" " LIMIT 1", - 4), + 6), /* for postgres_insert_transfer_details() */ GNUNET_PQ_make_prepare ("insert_transfer_to_coin_mapping", "INSERT INTO merchant_transfer_to_coin" @@ -5755,8 +5953,30 @@ libtaler_plugin_merchantdb_postgres_init (void *cls) " FROM merchant_instances" " WHERE merchant_id=$1)", 3), - /* for postgres_lookup_transfer_details() */ - GNUNET_PQ_make_prepare ("lookup_transfer_details", + /* for postgres_lookup_transfer() */ + GNUNET_PQ_make_prepare ("lookup_transfer", + "SELECT" + " credit_amount_val" + ",credit_amount_frac" + ",wire_fee_val" + ",wire_fee_frac" + ",execution_time" + ",verified" + " FROM merchant_transfers" + " JOIN merchant_transfer_signatures USING (credit_serial)" + " JOIN merchant_accounts USING (account_serial)" + " WHERE wtid=$2" + " AND exchange_url=$1", + 2), + /* for postgres_set_transfer_status_to_verified() */ + GNUNET_PQ_make_prepare ("set_transfer_status_to_verified", + "UPDATE merchant_transfers SET" + " verified=TRUE" + " WHERE wtid=$1" + " AND exchange_url=$2", + 2), + /* for postgres_lookup_transfer_summary() */ + GNUNET_PQ_make_prepare ("lookup_transfer_summary", "SELECT" " order_id" ",exchange_deposit_value_val" @@ -5764,22 +5984,29 @@ libtaler_plugin_merchantdb_postgres_init (void *cls) ",exchange_deposit_fee_val" ",exchange_deposit_fee_frac" " FROM merchant_transfers" - " JOIN merchant_accounts USING (account_serial)" " JOIN merchant_transfer_to_coin USING (credit_serial)" - " JOIN merchant_transfer_signatures USING (credit_serial)" " JOIN merchant_deposits USING (deposit_serial)" " JOIN merchant_contract_terms USING (order_serial)" - " WHERE wtid=$4" - " AND merchant_transfers.exchange_url=$2" - " AND credit_amount_val=$5" - " AND credit_amount_frac=$6" - " AND payto_uri=$3" - " AND execution_time=$7" - " AND merchant_contract_terms.merchant_serial=" - " (SELECT merchant_serial" - " FROM merchant_instances" - " WHERE merchant_id=$1)", - 7), + " WHERE wtid=$2" + " AND merchant_transfers.exchange_url=$1", + 2), + /* for postgres_lookup_transfer_details() */ + GNUNET_PQ_make_prepare ("lookup_transfer_details", + "SELECT" + " merchant_contract_terms.h_contract_terms" + ",merchant_transfer_to_coin.offset_in_exchange_list" + ",merchant_deposits.coin_pub" + ",exchange_deposit_value_val" + ",exchange_deposit_value_frac" + ",exchange_deposit_fee_val" + ",exchange_deposit_fee_frac" + " FROM merchant_transfer_to_coin" + " JOIN merchant_deposits USING (deposit_serial)" + " JOIN merchant_contract_terms USING (order_serial)" + " JOIN merchant_transfers USING (credit_serial)" + " WHERE merchant_transfers.wtid=$2" + " AND merchant_transfers.exchange_url=$1", + 2), /* OLD API: */ #if 0 @@ -6101,6 +6328,10 @@ libtaler_plugin_merchantdb_postgres_init (void *cls) plugin->lookup_wire_fee = &postgres_lookup_wire_fee; plugin->lookup_deposits_by_contract_and_coin = &postgres_lookup_deposits_by_contract_and_coin; + plugin->lookup_transfer = &postgres_lookup_transfer; + plugin->set_transfer_status_to_verified = + &postgres_set_transfer_status_to_verified; + plugin->lookup_transfer_summary = &postgres_lookup_transfer_summary; plugin->lookup_transfer_details = &postgres_lookup_transfer_details; /* OLD API: */ |