diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/backenddb/plugin_merchantdb_postgres.c | 79 | ||||
-rw-r--r-- | src/backenddb/test_merchantdb.c | 152 | ||||
-rw-r--r-- | src/include/taler_merchantdb_plugin.h | 30 |
3 files changed, 256 insertions, 5 deletions
diff --git a/src/backenddb/plugin_merchantdb_postgres.c b/src/backenddb/plugin_merchantdb_postgres.c index e0600593..17e4d0a4 100644 --- a/src/backenddb/plugin_merchantdb_postgres.c +++ b/src/backenddb/plugin_merchantdb_postgres.c @@ -267,13 +267,29 @@ postgres_initialize (void *cls) " VALUES " "($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11)", 11), + GNUNET_PQ_make_prepare ("lookup_wire_fee", + "SELECT" + " wire_fee_val" + ",wire_fee_frac" + ",wire_fee_curr" + ",closing_fee_val" + ",closing_fee_frac" + ",closing_fee_curr" + ",start_date" + ",end_date" + ",exchange_sig" + " FROM exchange_wire_fees" + " WHERE exchange_pub=$1" + " AND h_wire_method=$2" + " AND start_date <= $3" + " AND end_date > $3", + 1), GNUNET_PQ_make_prepare ("find_contract_terms_from_hash", "SELECT" " contract_terms" " FROM merchant_contract_terms" - " WHERE" - " h_contract_terms=$1" - " AND merchant_pub=$2", + " WHERE h_contract_terms=$1" + " AND merchant_pub=$2", 2), GNUNET_PQ_make_prepare ("end_transaction", "COMMIT", @@ -1908,6 +1924,62 @@ postgres_store_wire_fee_by_exchange (void *cls, /** + * Obtain information about wire fees charged by an exchange, + * including signature (so we have proof). + * + * @param cls closure + * @param exchange_pub public key of the exchange + * @param h_wire_method hash of wire method + * @param contract_date date of the contract to use for the lookup + * @param[out] wire_fee wire fee charged + * @param[out] closing_fee closing fee charged (irrelevant for us, + * but needed to check signature) + * @param[out] start_date start of fee being used + * @param[out] end_date end of fee being used + * @param[out] exchange_sig signature of exchange over fee structure + * @return transaction status code + */ +static enum GNUNET_DB_QueryStatus +postgres_lookup_wire_fee (void *cls, + const struct TALER_MasterPublicKeyP *exchange_pub, + const struct GNUNET_HashCode *h_wire_method, + struct GNUNET_TIME_Absolute contract_date, + struct TALER_Amount *wire_fee, + struct TALER_Amount *closing_fee, + struct GNUNET_TIME_Absolute *start_date, + struct GNUNET_TIME_Absolute *end_date, + struct TALER_MasterSignatureP *exchange_sig) +{ + struct PostgresClosure *pg = cls; + struct GNUNET_PQ_QueryParam params[] = { + GNUNET_PQ_query_param_auto_from_type (exchange_pub), + GNUNET_PQ_query_param_auto_from_type (h_wire_method), + GNUNET_PQ_query_param_absolute_time (&contract_date), + GNUNET_PQ_query_param_end + }; + struct GNUNET_PQ_ResultSpec rs[] = { + TALER_PQ_result_spec_amount ("wire_fee", + wire_fee), + TALER_PQ_result_spec_amount ("closing_fee", + closing_fee), + GNUNET_PQ_result_spec_absolute_time ("start_date", + start_date), + GNUNET_PQ_result_spec_absolute_time ("end_date", + end_date), + GNUNET_PQ_result_spec_auto_from_type ("exchange_sig", + exchange_sig), + GNUNET_PQ_result_spec_end + }; + + check_connection (pg); + return GNUNET_PQ_eval_prepared_singleton_select (pg->conn, + "lookup_wire_fee", + params, + rs); +} + + +/** * Closure for #process_refund_cb. */ struct FindRefundContext @@ -2417,6 +2489,7 @@ libtaler_plugin_merchantdb_postgres_init (void *cls) plugin->find_contract_terms_by_date_and_range = &postgres_find_contract_terms_by_date_and_range; plugin->find_contract_terms_from_hash = &postgres_find_contract_terms_from_hash; plugin->get_refunds_from_contract_terms_hash = &postgres_get_refunds_from_contract_terms_hash; + plugin->lookup_wire_fee = &postgres_lookup_wire_fee; plugin->increase_refund_for_contract = postgres_increase_refund_for_contract; plugin->mark_proposal_paid = postgres_mark_proposal_paid; plugin->start = postgres_start; diff --git a/src/backenddb/test_merchantdb.c b/src/backenddb/test_merchantdb.c index 0f43b8f3..212359d7 100644 --- a/src/backenddb/test_merchantdb.c +++ b/src/backenddb/test_merchantdb.c @@ -363,6 +363,153 @@ proof_cb (void *cls, /** + * Test the wire fee storage. + * + * @return #GNUNET_OK on success + */ +static int +test_wire_fee () +{ + struct TALER_MasterPublicKeyP exchange_pub; + struct GNUNET_HashCode h_wire_method; + struct GNUNET_TIME_Absolute contract_date; + struct TALER_Amount wire_fee1; + struct TALER_Amount closing_fee1; + struct TALER_Amount wire_fee2; + struct TALER_Amount closing_fee2; + struct TALER_Amount wire_fee3; + struct TALER_Amount closing_fee3; + struct GNUNET_TIME_Absolute date1; + struct GNUNET_TIME_Absolute date2; + struct GNUNET_TIME_Absolute date3; + struct GNUNET_TIME_Absolute start_date; + struct GNUNET_TIME_Absolute end_date; + struct TALER_MasterSignatureP exchange_sig; + struct TALER_MasterSignatureP exchange_sig2; + + RND_BLK (&exchange_pub); + RND_BLK (&h_wire_method); + RND_BLK (&exchange_sig); + date1 = GNUNET_TIME_absolute_get (); + date2 = GNUNET_TIME_absolute_add (date1, + GNUNET_TIME_UNIT_DAYS); + date3 = GNUNET_TIME_absolute_add (date2, + GNUNET_TIME_UNIT_DAYS); + GNUNET_assert (GNUNET_OK == + TALER_string_to_amount (CURRENCY ":5", + &closing_fee1)); + GNUNET_assert (GNUNET_OK == + TALER_string_to_amount (CURRENCY ":4", + &wire_fee1)); + GNUNET_assert (GNUNET_OK == + TALER_string_to_amount (CURRENCY ":3", + &closing_fee2)); + GNUNET_assert (GNUNET_OK == + TALER_string_to_amount (CURRENCY ":2", + &wire_fee2)); + if (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT != + plugin->store_wire_fee_by_exchange (plugin->cls, + &exchange_pub, + &h_wire_method, + &wire_fee1, + &closing_fee1, + date1, + date2, + &exchange_sig)) + { + GNUNET_break (0); + return GNUNET_SYSERR; + } + if (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT != + plugin->store_wire_fee_by_exchange (plugin->cls, + &exchange_pub, + &h_wire_method, + &wire_fee2, + &closing_fee2, + date2, + date3, + &exchange_sig)) + { + GNUNET_break (0); + return GNUNET_SYSERR; + } + contract_date = date2; /* test inclusive/exclusive range */ + if (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT != + plugin->lookup_wire_fee (plugin->cls, + &exchange_pub, + &h_wire_method, + contract_date, + &wire_fee3, + &closing_fee3, + &start_date, + &end_date, + &exchange_sig2)) + { + GNUNET_break (0); + return GNUNET_SYSERR; + } + if ( (start_date.abs_value_us != date2.abs_value_us) || + (end_date.abs_value_us != date3.abs_value_us) || + (0 != memcmp (&exchange_sig, + &exchange_sig2, + sizeof (exchange_sig))) || + (0 != TALER_amount_cmp (&wire_fee2, + &wire_fee3)) || + (0 != TALER_amount_cmp (&closing_fee2, + &closing_fee3)) ) + { + GNUNET_break (0); + return GNUNET_SYSERR; + } + contract_date = GNUNET_TIME_absolute_add (date1, + GNUNET_TIME_UNIT_SECONDS); + if (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT != + plugin->lookup_wire_fee (plugin->cls, + &exchange_pub, + &h_wire_method, + contract_date, + &wire_fee3, + &closing_fee3, + &start_date, + &end_date, + &exchange_sig2)) + { + GNUNET_break (0); + return GNUNET_SYSERR; + } + if ( (start_date.abs_value_us != date1.abs_value_us) || + (end_date.abs_value_us != date2.abs_value_us) || + (0 != memcmp (&exchange_sig, + &exchange_sig2, + sizeof (exchange_sig))) || + (0 != TALER_amount_cmp (&wire_fee1, + &wire_fee3)) || + (0 != TALER_amount_cmp (&closing_fee1, + &closing_fee3)) ) + { + GNUNET_break (0); + return GNUNET_SYSERR; + } + contract_date = date3; /* outside of valid range! */ + if (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS != + plugin->lookup_wire_fee (plugin->cls, + &exchange_pub, + &h_wire_method, + contract_date, + &wire_fee3, + &closing_fee3, + &start_date, + &end_date, + &exchange_sig2)) + { + GNUNET_break (0); + return GNUNET_SYSERR; + } + return GNUNET_OK; +} + + +/** * Main function that will be run by the scheduler. * * @param cls closure with config @@ -635,7 +782,10 @@ run (void *cls) &too_big_refund_amount, "make refund testing fail due" " to too big refund amount")); - + + FAILIF (GNUNET_OK != + test_wire_fee ()); + if (-1 == result) result = 0; diff --git a/src/include/taler_merchantdb_plugin.h b/src/include/taler_merchantdb_plugin.h index 2fd801f1..a5e91f5f 100644 --- a/src/include/taler_merchantdb_plugin.h +++ b/src/include/taler_merchantdb_plugin.h @@ -408,7 +408,7 @@ struct TALER_MERCHANTDB_Plugin * including signature (so we have proof). * * @param cls closure - * @paramm exchange_pub public key of the exchange + * @param exchange_pub public key of the exchange * @param h_wire_method hash of wire method * @param wire_fee wire fee charged * @param closing_fee closing fee charged (irrelevant for us, @@ -555,8 +555,36 @@ struct TALER_MERCHANTDB_Plugin const struct TALER_WireTransferIdentifierRawP *wtid, TALER_MERCHANTDB_ProofCallback cb, void *cb_cls); + + /** + * Obtain information about wire fees charged by an exchange, + * including signature (so we have proof). + * + * @param cls closure + * @param exchange_pub public key of the exchange + * @param h_wire_method hash of wire method + * @param contract_date date of the contract to use for the lookup + * @param[out] wire_fee wire fee charged + * @param[out] closing_fee closing fee charged (irrelevant for us, + * but needed to check signature) + * @param[out] start_date start of fee being used + * @param[out] end_date end of fee being used + * @param[out] exchange_sig signature of exchange over fee structure + * @return transaction status code + */ + enum GNUNET_DB_QueryStatus + (*lookup_wire_fee) (void *cls, + const struct TALER_MasterPublicKeyP *exchange_pub, + const struct GNUNET_HashCode *h_wire_method, + struct GNUNET_TIME_Absolute contract_date, + struct TALER_Amount *wire_fee, + struct TALER_Amount *closing_fee, + struct GNUNET_TIME_Absolute *start_date, + struct GNUNET_TIME_Absolute *end_date, + struct TALER_MasterSignatureP *exchange_sig); + /** * Function called when some backoffice staff decides to award or * increase the refund on an existing contract. |