From 427417b8352c2036dc6f5c0ca6bd20c0b7edd225 Mon Sep 17 00:00:00 2001 From: Christian Grothoff Date: Sun, 20 Mar 2022 13:20:45 +0100 Subject: towards support for new reserve history/status APIs --- src/lib/exchange_api_reserves_history.c | 192 ++++++++++++++------------------ 1 file changed, 86 insertions(+), 106 deletions(-) (limited to 'src/lib/exchange_api_reserves_history.c') diff --git a/src/lib/exchange_api_reserves_history.c b/src/lib/exchange_api_reserves_history.c index f7191b2ad..c0f6c42d2 100644 --- a/src/lib/exchange_api_reserves_history.c +++ b/src/lib/exchange_api_reserves_history.c @@ -81,35 +81,32 @@ struct TALER_EXCHANGE_ReservesHistoryHandle * We received an #MHD_HTTP_OK history code. Handle the JSON * response. * - * @param rgh handle of the request + * @param rsh handle of the request * @param j JSON response * @return #GNUNET_OK on success */ static enum GNUNET_GenericReturnValue -handle_reserves_history_ok (struct TALER_EXCHANGE_ReservesHistoryHandle *rgh, +handle_reserves_history_ok (struct TALER_EXCHANGE_ReservesHistoryHandle *rsh, const json_t *j) { json_t *history; unsigned int len; - bool kyc_ok; - bool kyc_required; - struct TALER_Amount balance; - struct TALER_Amount balance_from_history; + struct TALER_Amount history_balance; + struct TALER_EXCHANGE_ReserveHistory rs = { + .hr.reply = j, + .hr.http_status = MHD_HTTP_OK + }; struct GNUNET_JSON_Specification spec[] = { TALER_JSON_spec_amount_any ("balance", - &balance), + &rs.details.ok.balance), GNUNET_JSON_spec_bool ("kyc_passed", - &kyc_ok), + &rs.details.ok.kyc_ok), GNUNET_JSON_spec_bool ("kyc_required", - &kyc_required), + &rs.details.ok.kyc_required), GNUNET_JSON_spec_json ("history", &history), GNUNET_JSON_spec_end () }; - struct TALER_EXCHANGE_HttpResponse hr = { - .reply = j, - .http_history = MHD_HTTP_OK - }; if (GNUNET_OK != GNUNET_JSON_parse (j, @@ -122,16 +119,19 @@ handle_reserves_history_ok (struct TALER_EXCHANGE_ReservesHistoryHandle *rgh, } len = json_array_size (history); { - struct TALER_EXCHANGE_ReserveHistory *rhistory; + struct TALER_EXCHANGE_ReserveHistoryEntry *rhistory; rhistory = GNUNET_new_array (len, - struct TALER_EXCHANGE_ReserveHistory); + struct TALER_EXCHANGE_ReserveHistoryEntry); + // FIXME: even this history could be partial + // (if the reserve is too old!); update API + // and return incoming & outgoing totals separately? if (GNUNET_OK != - TALER_EXCHANGE_parse_reserve_history (rgh->exchange, + TALER_EXCHANGE_parse_reserve_history (rsh->exchange, history, - &rgh->reserve_pub, - balance.currency, - &balance_from_history, + &rsh->reserve_pub, + rs.details.ok.balance.currency, + &history_balance, len, rhistory)) { @@ -141,25 +141,13 @@ handle_reserves_history_ok (struct TALER_EXCHANGE_ReservesHistoryHandle *rgh, GNUNET_JSON_parse_free (spec); return GNUNET_SYSERR; } - if (0 != - TALER_amount_cmp (&balance_from_history, - &balance)) + if (NULL != rsh->cb) { - /* exchange cannot add up balances!? */ - GNUNET_break_op (0); - TALER_EXCHANGE_free_reserve_history (rhistory, - len); - GNUNET_JSON_parse_free (spec); - return GNUNET_SYSERR; - } - if (NULL != rgh->cb) - { - rgh->cb (rgh->cb_cls, - &hr, - &balance, - len, - rhistory); - rgh->cb = NULL; + rs.details.ok.history = rhistory; + rs.details.ok.history_len = len; + rsh->cb (rsh->cb_cls, + &rs); + rsh->cb = NULL; } TALER_EXCHANGE_free_reserve_history (rhistory, len); @@ -182,75 +170,72 @@ handle_reserves_history_finished (void *cls, long response_code, const void *response) { - struct TALER_EXCHANGE_ReservesHistoryHandle *rgh = cls; + struct TALER_EXCHANGE_ReservesHistoryHandle *rsh = cls; const json_t *j = response; - struct TALER_EXCHANGE_HttpResponse hr = { - .reply = j, - .http_history = (unsigned int) response_code + struct TALER_EXCHANGE_ReserveHistory rs = { + .hr.reply = j, + .hr.http_status = (unsigned int) response_code }; - rgh->job = NULL; + rsh->job = NULL; switch (response_code) { case 0: - hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE; + rs.hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE; break; case MHD_HTTP_OK: if (GNUNET_OK != - handle_reserves_history_ok (rgh, + handle_reserves_history_ok (rsh, j)) { - hr.http_history = 0; - hr.ec = TALER_EC_GENERIC_REPLY_MALFORMED; + rs.hr.http_status = 0; + rs.hr.ec = TALER_EC_GENERIC_REPLY_MALFORMED; } break; case MHD_HTTP_BAD_REQUEST: /* This should never happen, either us or the exchange is buggy (or API version conflict); just pass JSON reply to the application */ GNUNET_break (0); - hr.ec = TALER_JSON_history_error_code (j); - hr.hint = TALER_JSON_history_error_hint (j); + rs.hr.ec = TALER_JSON_get_error_code (j); + rs.hr.hint = TALER_JSON_get_error_hint (j); break; case MHD_HTTP_FORBIDDEN: /* This should never happen, either us or the exchange is buggy (or API version conflict); just pass JSON reply to the application */ GNUNET_break (0); - hr.ec = TALER_JSON_history_error_code (j); - hr.hint = TALER_JSON_history_error_hint (j); + rs.hr.ec = TALER_JSON_get_error_code (j); + rs.hr.hint = TALER_JSON_get_error_hint (j); break; case MHD_HTTP_NOT_FOUND: /* Nothing really to verify, this should never happen, we should pass the JSON reply to the application */ - hr.ec = TALER_JSON_history_error_code (j); - hr.hint = TALER_JSON_history_error_hint (j); + rs.hr.ec = TALER_JSON_get_error_code (j); + rs.hr.hint = TALER_JSON_get_error_hint (j); break; case MHD_HTTP_INTERNAL_SERVER_ERROR: /* Server had an internal issue; we should retry, but this API leaves this to the application */ - hr.ec = TALER_JSON_history_error_code (j); - hr.hint = TALER_JSON_history_error_hint (j); + rs.hr.ec = TALER_JSON_get_error_code (j); + rs.hr.hint = TALER_JSON_get_error_hint (j); break; default: /* unexpected response code */ GNUNET_break_op (0); - hr.ec = TALER_JSON_history_error_code (j); - hr.hint = TALER_JSON_history_error_hint (j); + rs.hr.ec = TALER_JSON_get_error_code (j); + rs.hr.hint = TALER_JSON_get_error_hint (j); GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Unexpected response code %u/%d for reserves history\n", (unsigned int) response_code, - (int) hr.ec); + (int) rs.hr.ec); break; } - if (NULL != rgh->cb) + if (NULL != rsh->cb) { - rgh->cb (rgh->cb_cls, - &hr, - NULL, - 0, - NULL); - rgh->cb = NULL; + rsh->cb (rsh->cb_cls, + &rs); + rsh->cb = NULL; } - TALER_EXCHANGE_reserves_history_cancel (rgh); + TALER_EXCHANGE_reserves_history_cancel (rsh); } @@ -261,12 +246,11 @@ TALER_EXCHANGE_reserves_history ( TALER_EXCHANGE_ReservesHistoryCallback cb, void *cb_cls) { - struct TALER_EXCHANGE_ReservesHistoryHandle *rgh; + struct TALER_EXCHANGE_ReservesHistoryHandle *rsh; struct GNUNET_CURL_Context *ctx; CURL *eh; char arg_str[sizeof (struct TALER_ReservePublicKeyP) * 2 + 32]; - const struct TALER_Amount *history_fee; - const struct TALER_EXCHANGE_Keys *keys; + struct TALER_ReserveSignatureP reserve_sig; struct GNUNET_TIME_Timestamp ts = GNUNET_TIME_timestamp_get (); @@ -276,22 +260,19 @@ TALER_EXCHANGE_reserves_history ( GNUNET_break (0); return NULL; } - keys = TALER_EXCHANGE_get_keys (exchange); - // FIXME: extract history_fee from keys! - history_fee = FIXME; - rgh = GNUNET_new (struct TALER_EXCHANGE_ReservesHistoryHandle); - rgh->exchange = exchange; - rgh->cb = cb; - rgh->cb_cls = cb_cls; + rsh = GNUNET_new (struct TALER_EXCHANGE_ReservesHistoryHandle); + rsh->exchange = exchange; + rsh->cb = cb; + rsh->cb_cls = cb_cls; GNUNET_CRYPTO_eddsa_key_get_public (&reserve_priv->eddsa_priv, - &rgh->reserve_pub.eddsa_pub); + &rsh->reserve_pub.eddsa_pub); { char pub_str[sizeof (struct TALER_ReservePublicKeyP) * 2]; char *end; end = GNUNET_STRINGS_data_to_string ( - &rgh->reserve_pub, - sizeof (rgh->reserve_pub), + &rsh->reserve_pub, + sizeof (rsh->reserve_pub), pub_str, sizeof (pub_str)); *end = '\0'; @@ -300,68 +281,67 @@ TALER_EXCHANGE_reserves_history ( "/reserves/%s/history", pub_str); } - rgh->url = TEAH_path_to_url (exchange, + rsh->url = TEAH_path_to_url (exchange, arg_str); - if (NULL == rgh->url) + if (NULL == rsh->url) { - GNUNET_free (rgh); + GNUNET_free (rsh); return NULL; } - eh = TALER_EXCHANGE_curl_easy_history_ (rgh->url); + eh = TALER_EXCHANGE_curl_easy_get_ (rsh->url); if (NULL == eh) { GNUNET_break (0); - GNUNET_free (rgh->url); - GNUNET_free (rgh); + GNUNET_free (rsh->url); + GNUNET_free (rsh); return NULL; } TALER_wallet_reserve_history_sign (ts, - history_fee, + NULL, /* FIXME: fee! */ reserve_priv, &reserve_sig); { json_t *history_obj = GNUNET_JSON_PACK ( GNUNET_JSON_pack_timestamp ("request_timestamp", - &ts), + ts), GNUNET_JSON_pack_data_auto ("reserve_sig", &reserve_sig)); if (GNUNET_OK != - TALER_curl_easy_post (&rgh->post_ctx, + TALER_curl_easy_post (&rsh->post_ctx, eh, history_obj)) - ) - { - GNUNET_break (0); - curl_easy_cleanup (eh); - json_decref (history_obj); - GNUNET_free (rgh->url); - GNUNET_free (rgh); - return NULL; - } + { + GNUNET_break (0); + curl_easy_cleanup (eh); json_decref (history_obj); + GNUNET_free (rsh->url); + GNUNET_free (rsh); + return NULL; + } + json_decref (history_obj); } ctx = TEAH_handle_to_context (exchange); - rgh->job = GNUNET_CURL_job_add (ctx, + rsh->job = GNUNET_CURL_job_add (ctx, eh, &handle_reserves_history_finished, - rgh); - return rgh; + rsh); + return rsh; } void TALER_EXCHANGE_reserves_history_cancel ( - struct TALER_EXCHANGE_ReservesHistoryHandle *rgh) + struct TALER_EXCHANGE_ReservesHistoryHandle *rsh) { - if (NULL != rgh->job) + if (NULL != rsh->job) { - GNUNET_CURL_job_cancel (rgh->job); - rgh->job = NULL; + GNUNET_CURL_job_cancel (rsh->job); + rsh->job = NULL; } - TALER_curl_easy_post_finished (&rgh->post_ctx); - GNUNET_free (rgh->url); - GNUNET_free (rgh); + TALER_curl_easy_post_finished (&rsh->post_ctx); + GNUNET_free (rsh->url); + GNUNET_free (rsh); } -- cgit v1.2.3