diff options
author | Florian Dold <florian@dold.me> | 2024-06-16 17:23:59 +0200 |
---|---|---|
committer | Florian Dold <florian@dold.me> | 2024-06-16 17:25:06 +0200 |
commit | 65e2a270ce7173d76c8e94008ec803ab144f1923 (patch) | |
tree | fefdee207aa83059bfef520bae6c519b1c17d79c | |
parent | 8170cd200c1bec7c6ea9f71fa20e3aeacf3446ec (diff) |
util: always provide currency spec for main currency
The logic is now part of the configuration parser, as the merchant also
needs the same logic.
m--------- | contrib/wallet-core | 0 | ||||
-rw-r--r-- | src/exchange/taler-exchange-httpd.c | 31 | ||||
-rw-r--r-- | src/include/taler_util.h | 31 | ||||
-rw-r--r-- | src/util/config.c | 47 |
4 files changed, 70 insertions, 39 deletions
diff --git a/contrib/wallet-core b/contrib/wallet-core -Subproject 5934e007f637bd9834a811e67c0a030d7a59f2c +Subproject c412a2fff5ac72f3a15ffcf0c59b226a9c7ad93 diff --git a/src/exchange/taler-exchange-httpd.c b/src/exchange/taler-exchange-httpd.c index 36459fbd7..b54d450d4 100644 --- a/src/exchange/taler-exchange-httpd.c +++ b/src/exchange/taler-exchange-httpd.c @@ -90,7 +90,7 @@ * Above what request latency do we start to log? */ #define WARN_LATENCY GNUNET_TIME_relative_multiply ( \ - GNUNET_TIME_UNIT_MILLISECONDS, 500) + GNUNET_TIME_UNIT_MILLISECONDS, 500) /** * Are clients allowed to request /keys for times other than the @@ -2059,11 +2059,6 @@ handle_mhd_request (void *cls, static enum GNUNET_GenericReturnValue exchange_serve_process_config (void) { - static struct TALER_CurrencySpecification defspec = { - .num_fractional_input_digits = 2, - .num_fractional_normal_digits = 2, - .num_fractional_trailing_zero_digits = 2 - }; if (GNUNET_OK != TALER_KYCLOGIC_kyc_init (TEH_cfg)) { @@ -2135,6 +2130,7 @@ exchange_serve_process_config (void) if (GNUNET_OK != TALER_CONFIG_parse_currencies (TEH_cfg, + TEH_currency, &num_cspecs, &cspecs)) return GNUNET_SYSERR; @@ -2150,24 +2146,8 @@ exchange_serve_process_config (void) break; } } - if (NULL == TEH_cspec) - { - GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_WARNING, - "taler", - "CURRENCY", - "Lacking enabled currency specification for the given currency, using default"); - defspec.map_alt_unit_names - = GNUNET_JSON_PACK ( - GNUNET_JSON_pack_string ("0", - TEH_currency) - ); - defspec.name = TEH_currency; - GNUNET_assert (strlen (TEH_currency) < - sizeof (defspec.currency)); - strcpy (defspec.currency, - TEH_currency); - TEH_cspec = &defspec; - } + /* currency parser must provide default spec for main currency */ + GNUNET_assert (NULL != TEH_cspec); if (GNUNET_OK != TALER_config_get_amount (TEH_cfg, "exchange", @@ -2222,7 +2202,8 @@ exchange_serve_process_config (void) if (GNUNET_SYSERR == TEH_enable_rewards) { GNUNET_log (GNUNET_ERROR_TYPE_ERROR, - "Need YES or NO in section `exchange' under `ENABLE_REWARDS'\n"); + "Need YES or NO in section `exchange' under `ENABLE_REWARDS'\n") + ; return GNUNET_SYSERR; } if (GNUNET_OK != diff --git a/src/include/taler_util.h b/src/include/taler_util.h index 8feb8451c..000d0dee5 100644 --- a/src/include/taler_util.h +++ b/src/include/taler_util.h @@ -57,16 +57,16 @@ /* Define logging functions */ #define TALER_LOG_DEBUG(...) \ - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__) + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__) #define TALER_LOG_INFO(...) \ - GNUNET_log (GNUNET_ERROR_TYPE_INFO, __VA_ARGS__) + GNUNET_log (GNUNET_ERROR_TYPE_INFO, __VA_ARGS__) #define TALER_LOG_WARNING(...) \ - GNUNET_log (GNUNET_ERROR_TYPE_WARNING, __VA_ARGS__) + GNUNET_log (GNUNET_ERROR_TYPE_WARNING, __VA_ARGS__) #define TALER_LOG_ERROR(...) \ - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, __VA_ARGS__) + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, __VA_ARGS__) /** @@ -77,11 +77,11 @@ * @param reason string to print as warning */ #define TALER_assert_as(EXP, reason) \ - do { \ - if (EXP) break; \ - TALER_LOG_ERROR ("%s at %s:%d\n", reason, __FILE__, __LINE__); \ - abort (); \ - } while (0) + do { \ + if (EXP) break; \ + TALER_LOG_ERROR ("%s at %s:%d\n", reason, __FILE__, __LINE__); \ + abort (); \ + } while (0) /** @@ -112,10 +112,11 @@ #define TALER_gcry_ok(cmd) \ - do {int rc; rc = cmd; if (! rc) break; \ - TALER_LOG_ERROR ("A Gcrypt call failed at %s:%d with error: %s\n", \ - __FILE__, \ - __LINE__, gcry_strerror (rc)); abort (); } while (0) + do {int rc; rc = cmd; if (! rc) break; \ + TALER_LOG_ERROR ("A Gcrypt call failed at %s:%d with error: %s\n", \ + __FILE__, \ + __LINE__, gcry_strerror (rc)); abort (); } while (0 \ + ) /** @@ -259,6 +260,7 @@ struct TALER_CurrencySpecification * our configuration. * * @param cfg configuration to parse + * @param main_currency main currency of the component * @param[out] num_currencies set to number of enabled currencies, length of @e cspecs * @param[out] cspecs set to currency specification array * @return #GNUNET_OK on success, #GNUNET_NO if zero @@ -267,6 +269,7 @@ struct TALER_CurrencySpecification */ enum GNUNET_GenericReturnValue TALER_CONFIG_parse_currencies (const struct GNUNET_CONFIGURATION_Handle *cfg, + const char *main_currency, unsigned int *num_currencies, struct TALER_CurrencySpecification **cspecs); @@ -771,7 +774,7 @@ TALER_get_lowest_age ( * @return lowest age for the largest age group */ #define TALER_adult_age(mask) \ - sizeof((mask)->bits) * 8 - __builtin_clz ((mask)->bits) - 1 + sizeof((mask)->bits) * 8 - __builtin_clz ((mask)->bits) - 1 /** * Handle to an external process that will assist diff --git a/src/util/config.c b/src/util/config.c index f5accaad8..9a32ec3e8 100644 --- a/src/util/config.c +++ b/src/util/config.c @@ -438,12 +438,18 @@ TALER_check_currency_scale_map (const json_t *map) enum GNUNET_GenericReturnValue TALER_CONFIG_parse_currencies (const struct GNUNET_CONFIGURATION_Handle *cfg, + const char *main_currency, unsigned int *num_currencies, struct TALER_CurrencySpecification **cspecs) { struct CurrencyParserContext cpc = { .cfg = cfg }; + static struct TALER_CurrencySpecification defspec = { + .num_fractional_input_digits = 2, + .num_fractional_normal_digits = 2, + .num_fractional_trailing_zero_digits = 2 + }; GNUNET_CONFIGURATION_iterate_sections (cfg, &parse_currencies_cb, @@ -455,6 +461,47 @@ TALER_CONFIG_parse_currencies (const struct GNUNET_CONFIGURATION_Handle *cfg, 0); return GNUNET_SYSERR; } + /* Make sure that there is some sane fallback for the main currency */ + if (NULL != main_currency) + { + struct TALER_CurrencySpecification *mspec = NULL; + for (unsigned int i = 0; i<cpc.num_currencies; i++) + { + struct TALER_CurrencySpecification *cspec; + + cspec = &cpc.cspecs[i]; + if (0 == strcmp (main_currency, + cspec->currency)) + { + mspec = cspec; + break; + } + } + if (NULL == mspec) + { + GNUNET_log (GNUNET_ERROR_TYPE_WARNING, + "Lacking enabled currency specification for main currency %s, using fallback currency specification.\n", + main_currency); + if (cpc.len_cspecs == cpc.num_currencies) + { + GNUNET_array_grow (cpc.cspecs, + cpc.len_cspecs, + cpc.len_cspecs + 1); + } + mspec = &cpc.cspecs[cpc.num_currencies++]; + *mspec = defspec; + GNUNET_assert (strlen (main_currency) < TALER_CURRENCY_LEN); + strcpy (mspec->currency, + main_currency); + mspec->map_alt_unit_names + = GNUNET_JSON_PACK ( + GNUNET_JSON_pack_string ("0", + main_currency) + ); + mspec->name = GNUNET_strdup (main_currency); + } + } + /* cspecs might've been overgrown, grow back to minimum size */ GNUNET_array_grow (cpc.cspecs, cpc.len_cspecs, cpc.num_currencies); |