From 8afe2a17f05ce158f854d1be7af8ec058c0da08b Mon Sep 17 00:00:00 2001 From: Christian Grothoff Date: Sat, 14 Oct 2023 00:14:03 +0200 Subject: make stefan_lin a float, as it should have been in the first place --- src/exchange/taler-exchange-httpd.c | 14 ++++++------- src/exchange/taler-exchange-httpd.h | 2 +- src/exchange/taler-exchange-httpd_keys.c | 4 ++-- src/include/taler_exchange_service.h | 2 +- src/lib/exchange_api_handle.c | 18 ++++++++-------- src/lib/exchange_api_stefan.c | 35 ++++++++++++++------------------ src/lib/test_stefan.c | 19 ++++------------- 7 files changed, 38 insertions(+), 56 deletions(-) (limited to 'src') diff --git a/src/exchange/taler-exchange-httpd.c b/src/exchange/taler-exchange-httpd.c index bcf5b67da..5bc118e3e 100644 --- a/src/exchange/taler-exchange-httpd.c +++ b/src/exchange/taler-exchange-httpd.c @@ -161,7 +161,7 @@ struct TALER_Amount TEH_stefan_log; /** * Linear STEFAN parameter. */ -struct TALER_Amount TEH_stefan_lin; +float TEH_stefan_lin; /** * Default number of fractional digits to render @@ -2112,14 +2112,12 @@ exchange_serve_process_config (void) &TEH_stefan_log)); } if (GNUNET_OK != - TALER_config_get_amount (TEH_cfg, - "exchange", - "STEFAN_LIN", - &TEH_stefan_lin)) + GNUNET_CONFIGURATION_get_value_float (TEH_cfg, + "exchange", + "STEFAN_LIN", + &TEH_stefan_lin)) { - GNUNET_assert (GNUNET_OK == - TALER_amount_set_zero (TEH_currency, - &TEH_stefan_lin)); + TEH_stefan_lin = 0.0f; } if (0 != strcmp (TEH_currency, diff --git a/src/exchange/taler-exchange-httpd.h b/src/exchange/taler-exchange-httpd.h index 1ab77291d..5fa942b15 100644 --- a/src/exchange/taler-exchange-httpd.h +++ b/src/exchange/taler-exchange-httpd.h @@ -110,7 +110,7 @@ extern struct TALER_Amount TEH_stefan_log; /** * Linear STEFAN parameter. */ -extern struct TALER_Amount TEH_stefan_lin; +extern float TEH_stefan_lin; /** * Default ways how to render #TEH_currency amounts. diff --git a/src/exchange/taler-exchange-httpd_keys.c b/src/exchange/taler-exchange-httpd_keys.c index a0cd94416..27b9d6c7f 100644 --- a/src/exchange/taler-exchange-httpd_keys.c +++ b/src/exchange/taler-exchange-httpd_keys.c @@ -2497,8 +2497,8 @@ create_krd (struct TEH_KeyStateHandle *ksh, &TEH_stefan_abs), TALER_JSON_pack_amount ("stefan_log", &TEH_stefan_log), - TALER_JSON_pack_amount ("stefan_lin", - &TEH_stefan_lin), + GNUNET_JSON_pack_double ("stefan_lin", + (double) TEH_stefan_lin), GNUNET_JSON_pack_string ("asset_type", asset_type), GNUNET_JSON_pack_bool ("rewards_allowed", diff --git a/src/include/taler_exchange_service.h b/src/include/taler_exchange_service.h index bf584036a..30c966d75 100644 --- a/src/include/taler_exchange_service.h +++ b/src/include/taler_exchange_service.h @@ -534,7 +534,7 @@ struct TALER_EXCHANGE_Keys /** * Linear STEFAN parameter. */ - struct TALER_Amount stefan_lin; + double stefan_lin; /** * Length of @e accounts array. diff --git a/src/lib/exchange_api_handle.c b/src/lib/exchange_api_handle.c index 811120a85..50a1a9ce9 100644 --- a/src/lib/exchange_api_handle.c +++ b/src/lib/exchange_api_handle.c @@ -933,9 +933,8 @@ decode_keys_json (const json_t *resp_obj, "stefan_log", currency, &key_data->stefan_log), - TALER_JSON_spec_amount ( + GNUNET_JSON_spec_double ( "stefan_lin", - currency, &key_data->stefan_lin), GNUNET_JSON_spec_end () }; @@ -1346,10 +1345,11 @@ keys_completed_cb (void *cls, GNUNET_TIME_timestamp2s (gkh->expire)); if (GNUNET_TIME_absolute_is_past (gkh->expire.abs_time)) { - GNUNET_log (GNUNET_ERROR_TYPE_WARNING, - "Exchange failed to give expiration time, assuming in %s\n", - GNUNET_TIME_relative2s (DEFAULT_EXPIRATION, - true)); + if (MHD_HTTP_OK == response_code) + GNUNET_log (GNUNET_ERROR_TYPE_WARNING, + "Exchange failed to give expiration time, assuming in %s\n", + GNUNET_TIME_relative2s (DEFAULT_EXPIRATION, + true)); gkh->expire = GNUNET_TIME_absolute_to_timestamp ( GNUNET_TIME_relative_to_absolute (DEFAULT_EXPIRATION)); @@ -1689,7 +1689,7 @@ TALER_EXCHANGE_get_keys ( GNUNET_break (CURLE_OK == curl_easy_setopt (eh, CURLOPT_VERBOSE, - 0)); + 1)); GNUNET_break (CURLE_OK == curl_easy_setopt (eh, CURLOPT_TIMEOUT, @@ -2388,8 +2388,8 @@ TALER_EXCHANGE_keys_to_json (const struct TALER_EXCHANGE_Keys *kd) &kd->stefan_abs), TALER_JSON_pack_amount ("stefan_log", &kd->stefan_log), - TALER_JSON_pack_amount ("stefan_lin", - &kd->stefan_lin), + GNUNET_JSON_pack_double ("stefan_lin", + kd->stefan_lin), GNUNET_JSON_pack_string ("asset_type", kd->asset_type), GNUNET_JSON_pack_data_auto ("master_public_key", diff --git a/src/lib/exchange_api_stefan.c b/src/lib/exchange_api_stefan.c index 19e8ce25e..c3576cd73 100644 --- a/src/lib/exchange_api_stefan.c +++ b/src/lib/exchange_api_stefan.c @@ -110,7 +110,7 @@ TALER_EXCHANGE_keys_stefan_b2n ( { const struct TALER_Amount *min; double log_d = amount_to_double (&keys->stefan_log); - double lin_d = amount_to_double (&keys->stefan_lin); + double lin_d = keys->stefan_lin; double abs_d = amount_to_double (&keys->stefan_abs); double bru_d = amount_to_double (brut); double min_d; @@ -125,9 +125,7 @@ TALER_EXCHANGE_keys_stefan_b2n ( min = get_unit (keys); if (NULL == min) return GNUNET_SYSERR; - if (1 != TALER_amount_cmp (min, - /* <= */ - &keys->stefan_lin)) + if (1.0f <= keys->stefan_lin) { /* This cannot work, linear STEFAN fee estimate always exceed any gross amount. */ @@ -137,7 +135,7 @@ TALER_EXCHANGE_keys_stefan_b2n ( min_d = amount_to_double (min); fee_d = abs_d + log_d * log2 (bru_d / min_d) - + lin_d * (bru_d / min_d); + + lin_d * bru_d; if (fee_d > bru_d) { GNUNET_assert (GNUNET_OK == @@ -155,7 +153,7 @@ TALER_EXCHANGE_keys_stefan_b2n ( /** * Our function - * f(x) := ne + ab + lo * log2(x/mi) + li * x / mi - x + * f(x) := ne + ab + lo * log2(x/mi) + li * x - x * for #newton(). */ static double @@ -166,13 +164,13 @@ eval_f (double mi, double ne, double x) { - return ne + ab + lo * log2 (x / mi) + li * (x / mi) - x; + return ne + ab + lo * log2 (x / mi) + li * x - x; } /** * Our function - * f'(x) := lo / log(2) / x + li / mi - 1 + * f'(x) := lo / log(2) / x + li - 1 * for #newton(). */ static double @@ -182,7 +180,7 @@ eval_fp (double mi, double ne, double x) { - return lo / log (2) / x + li / mi - 1; + return lo / log (2) / x + li - 1; } @@ -200,24 +198,23 @@ newton (double mi, { const double eps = 0.00000001; /* max error allowed */ double min_ab = ne + ab; /* result cannot be smaller than this! */ - double lin_factor = (mi - li); /* how many coins do we need per coin for linear factor? */ /* compute lower bounds by various heuristics */ - double min_ab_li = min_ab + min_ab * li / lin_factor; + double min_ab_li = min_ab + min_ab * li; double min_ab_li_lo = min_ab_li + log2 (min_ab_li / mi) * lo; double min_ab_lo = min_ab + log2 (min_ab / mi) * lo; - double min_ab_lo_li = min_ab_lo + min_ab_lo * li / lin_factor; + double min_ab_lo_li = min_ab_lo + min_ab_lo * li; /* take global lower bound */ double x_min = GNUNET_MAX (min_ab_lo_li, min_ab_li_lo); double x = x_min; /* use lower bound as starting point */ /* Objective: invert - ne := br - ab - lo * log2 (br/mi) - li (br/mi) + ne := br - ab - lo * log2 (br/mi) - li * br to find 'br'. Method: use Newton's method to find root of: - f(x) := ne + ab + lo * log2 (x/mi) + li * x / mi - x + f(x) := ne + ab + lo * log2 (x/mi) + li * x - x using also - f'(x) := lo / log(2) / x + li / mi - 1 + f'(x) := lo / log(2) / x + li - 1 */ /* Loop to abort in case of divergence; 100 is already very high, 2-4 is normal! */ @@ -234,7 +231,7 @@ newton (double mi, i, x_min, x_new, - x_new - ab - li * (x_new / mi) - lo * log2 (x / mi)); + x_new - ab - li * x_new - lo * log2 (x / mi)); return x_new; } if (x_new < x_min) @@ -261,7 +258,7 @@ TALER_EXCHANGE_keys_stefan_n2b ( struct TALER_Amount *brut) { const struct TALER_Amount *min; - double lin_d = amount_to_double (&keys->stefan_lin); + double lin_d = keys->stefan_lin; double log_d = amount_to_double (&keys->stefan_log); double abs_d = amount_to_double (&keys->stefan_abs); double net_d = amount_to_double (net); @@ -276,9 +273,7 @@ TALER_EXCHANGE_keys_stefan_n2b ( min = get_unit (keys); if (NULL == min) return GNUNET_SYSERR; - if (1 != TALER_amount_cmp (min, - /* <= */ - &keys->stefan_lin)) + if (1.0f <= keys->stefan_lin) { /* This cannot work, linear STEFAN fee estimate always exceed any gross amount. */ diff --git a/src/lib/test_stefan.c b/src/lib/test_stefan.c index 6941538f2..838cca769 100644 --- a/src/lib/test_stefan.c +++ b/src/lib/test_stefan.c @@ -100,11 +100,7 @@ main (int argc, GNUNET_assert (GNUNET_OK == TALER_string_to_amount ("MAGIC:0.13", &keys.stefan_log)); - GNUNET_assert (GNUNET_OK == - TALER_string_to_amount ("MAGIC:0.15", - &keys.stefan_lin)); - - /* stefan_lin >= unit value, not allowed, test we fail */ + keys.stefan_lin = 1.15; GNUNET_assert (GNUNET_OK == TALER_string_to_amount ("MAGIC:4", &brut)); @@ -123,12 +119,7 @@ main (int argc, TALER_EXCHANGE_keys_stefan_n2b (&keys, &net, &brut)); - - GNUNET_assert (GNUNET_OK == - TALER_string_to_amount ("MAGIC:0.13", - &keys.stefan_lin)); - - /* stefan_lin == unit value, not allowed, test we fail */ + keys.stefan_lin = 1.0; GNUNET_assert (GNUNET_OK == TALER_string_to_amount ("MAGIC:4", &brut)); @@ -148,14 +139,12 @@ main (int argc, &net, &brut)); GNUNET_assert (0 == GNUNET_get_log_skip ()); - GNUNET_assert (GNUNET_OK == - TALER_string_to_amount ("MAGIC:0.1", - &keys.stefan_lin)); + keys.stefan_lin = 0.1; /* try various values for lin and log STEFAN values */ for (unsigned int li = 1; li < 13; li += 1) { - keys.stefan_lin.fraction = li * TALER_AMOUNT_FRAC_BASE / 100; + keys.stefan_lin = 1.0 * li / 100.0; for (unsigned int lx = 1; lx < 100; lx += 1) { -- cgit v1.2.3