diff options
author | Christian Grothoff <grothoff@gnunet.org> | 2023-10-14 00:14:03 +0200 |
---|---|---|
committer | Christian Grothoff <grothoff@gnunet.org> | 2023-10-14 00:14:03 +0200 |
commit | 8afe2a17f05ce158f854d1be7af8ec058c0da08b (patch) | |
tree | 61f7f56ebdf8cbc22bfe9eaac51e1c5e498f034f /src/lib | |
parent | 3050f902cfc7f36b9c4b45a759cb6dd18e8d4cd1 (diff) |
make stefan_lin a float, as it should have been in the first place
Diffstat (limited to 'src/lib')
-rw-r--r-- | src/lib/exchange_api_handle.c | 18 | ||||
-rw-r--r-- | src/lib/exchange_api_stefan.c | 35 | ||||
-rw-r--r-- | src/lib/test_stefan.c | 19 |
3 files changed, 28 insertions, 44 deletions
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) { |