From 9084ac48216876c8a2f9d5a1a78c4b6216676de7 Mon Sep 17 00:00:00 2001 From: Christian Grothoff Date: Sun, 5 Jul 2015 13:05:58 +0200 Subject: [PATCH] fixing RC counting and a comma --- src/mint-lib/mint_api_withdraw.c | 33 +++++----- src/mint-lib/test_mint_api.c | 5 +- src/mint/taler-mint-httpd_db.c | 20 ++++-- src/mint/taler-mint-httpd_deposit.c | 4 +- src/mint/taler-mint-httpd_parsing.c | 62 ++++++++++++++----- src/mint/taler-mint-httpd_parsing.h | 15 +++-- src/mint/taler-mint-httpd_refresh.c | 4 +- src/mint/taler-mint-httpd_responses.c | 87 ++++++++++++--------------- src/mint/taler-mint-httpd_withdraw.c | 2 +- 9 files changed, 138 insertions(+), 94 deletions(-) diff --git a/src/mint-lib/mint_api_withdraw.c b/src/mint-lib/mint_api_withdraw.c index a7c302c75..1b13a0d9a 100644 --- a/src/mint-lib/mint_api_withdraw.c +++ b/src/mint-lib/mint_api_withdraw.c @@ -36,7 +36,7 @@ */ #define JSON_WARN(error) \ GNUNET_log (GNUNET_ERROR_TYPE_WARNING, \ - "JSON parsing failed at %s:%u: %s (%s)", \ + "JSON parsing failed at %s:%u: %s (%s)\n", \ __FILE__, __LINE__, error.text, error.source) @@ -800,7 +800,20 @@ handle_withdraw_sign_finished (void *cls, wsh->job = NULL; json = NULL; - if (0 == wsh->eno) + if (CURLE_OK != + curl_easy_getinfo (eh, + CURLINFO_RESPONSE_CODE, + &response_code)) + { + /* unexpected error... */ + GNUNET_break (0); + response_code = 0; + } + fprintf (stderr, + "Response code: %u\n", + (unsigned int) response_code); + if ( (0 == wsh->eno) && + (0 != response_code) ) { json = json_loadb (wsh->buf, wsh->buf_size, @@ -812,18 +825,6 @@ handle_withdraw_sign_finished (void *cls, response_code = 0; } } - if (NULL != json) - { - if (CURLE_OK != - curl_easy_getinfo (eh, - CURLINFO_RESPONSE_CODE, - &response_code)) - { - /* unexpected error... */ - GNUNET_break (0); - response_code = 0; - } - } switch (response_code) { case 0: @@ -1032,6 +1033,10 @@ TALER_MINT_withdraw_sign (struct TALER_MINT_Handle *mint, curl_easy_setopt (eh, CURLOPT_URL, wsh->url)); + GNUNET_assert (CURLE_OK == + curl_easy_setopt (eh, + CURLOPT_VERBOSE, + 1LL)); GNUNET_assert (CURLE_OK == curl_easy_setopt (eh, CURLOPT_POSTFIELDS, diff --git a/src/mint-lib/test_mint_api.c b/src/mint-lib/test_mint_api.c index 6d8e0638d..8d206970d 100644 --- a/src/mint-lib/test_mint_api.c +++ b/src/mint-lib/test_mint_api.c @@ -358,6 +358,7 @@ add_incoming_cb (void *cls, cmd->details.admin_add_incoming.aih = NULL; if (MHD_HTTP_OK != http_status) { + GNUNET_break (0); fail (is); return; } @@ -385,12 +386,10 @@ withdraw_sign_cb (void *cls, struct InterpreterState *is = cls; struct Command *cmd = &is->commands[is->ip]; - fprintf (stderr, - "Withdraw completed: %u\n", - http_status); cmd->details.withdraw_sign.wsh = NULL; if (NULL == sig) { + GNUNET_break (0); fail (is); return; } diff --git a/src/mint/taler-mint-httpd_db.c b/src/mint/taler-mint-httpd_db.c index e4e8c59ec..5afc104f5 100644 --- a/src/mint/taler-mint-httpd_db.c +++ b/src/mint/taler-mint-httpd_db.c @@ -411,11 +411,23 @@ TMH_DB_execute_withdraw_sign (struct MHD_Connection *connection, break; } } + if (0 == (res & 1)) + { + /* did not encounter any deposit operations, how can we have a reserve? */ + GNUNET_break (0); + return TMH_RESPONSE_reply_internal_db_error (connection); + } + if (0 == (res & 2)) + { + /* did not encounter any withdraw operations, set to zero */ + TALER_amount_get_zero (deposit_total.currency, + &withdraw_total); + } /* All reserve balances should be non-negative */ - GNUNET_break (GNUNET_SYSERR != - TALER_amount_subtract (&balance, - &deposit_total, - &withdraw_total)); + GNUNET_assert (GNUNET_SYSERR != + TALER_amount_subtract (&balance, + &deposit_total, + &withdraw_total)); if (0 < TALER_amount_cmp (&amount_required, &balance)) { diff --git a/src/mint/taler-mint-httpd_deposit.c b/src/mint/taler-mint-httpd_deposit.c index 1d4ef26df..19ea9cb50 100644 --- a/src/mint/taler-mint-httpd_deposit.c +++ b/src/mint/taler-mint-httpd_deposit.c @@ -139,8 +139,8 @@ parse_and_handle_deposit_request (struct MHD_Connection *connection, struct TALER_MINTDB_DenominationKeyIssueInformation *dki; struct TMH_KS_StateHandle *ks; struct TMH_PARSE_FieldSpecification spec[] = { - TMH_PARSE_MEMBER_DENOMINATION_PUBLIC_KEY ("denom_pub", &deposit.coin.denom_pub), - TMH_PARSE_MEMBER_DENOMINATION_SIGNATURE ("ub_sig", &deposit.coin.denom_sig), + TMH_PARSE_member_denomination_public_key ("denom_pub", &deposit.coin.denom_pub), + TMH_PARSE_member_denomination_signature ("ub_sig", &deposit.coin.denom_sig), TMH_PARSE_MEMBER_FIXED ("coin_pub", &deposit.coin.coin_pub), TMH_PARSE_MEMBER_FIXED ("merchant_pub", &deposit.merchant_pub), TMH_PARSE_MEMBER_FIXED ("H_contract", &deposit.h_contract), diff --git a/src/mint/taler-mint-httpd_parsing.c b/src/mint/taler-mint-httpd_parsing.c index d48674d2b..413d39bc6 100644 --- a/src/mint/taler-mint-httpd_parsing.c +++ b/src/mint/taler-mint-httpd_parsing.c @@ -768,24 +768,20 @@ TMH_PARSE_json_data (struct MHD_Connection *connection, *((void**)spec[i].destination) = ptr; break; case TMH_PARSE_JNC_RET_RSA_PUBLIC_KEY: - ptr = NULL; ret = TMH_PARSE_navigate_json (connection, root, TMH_PARSE_JNC_FIELD, spec[i].field_name, TMH_PARSE_JNC_RET_RSA_PUBLIC_KEY, - &ptr); - spec[i].destination = ptr; + spec[i].destination); break; case TMH_PARSE_JNC_RET_RSA_SIGNATURE: - ptr = NULL; ret = TMH_PARSE_navigate_json (connection, root, TMH_PARSE_JNC_FIELD, spec[i].field_name, TMH_PARSE_JNC_RET_RSA_SIGNATURE, - &ptr); - spec[i].destination = ptr; + spec[i].destination); break; case TMH_PARSE_JNC_RET_AMOUNT: GNUNET_assert (sizeof (struct TALER_Amount) == @@ -857,25 +853,25 @@ TMH_PARSE_release_data (struct TMH_PARSE_FieldSpecification *spec) break; case TMH_PARSE_JNC_RET_RSA_PUBLIC_KEY: { - struct TALER_DenominationPublicKey pk; + struct TALER_DenominationPublicKey *pk; - pk = *(struct TALER_DenominationPublicKey *) spec[i].destination; - if (NULL != pk.rsa_public_key) + pk = spec[i].destination; + if (NULL != pk->rsa_public_key) { - GNUNET_CRYPTO_rsa_public_key_free (pk.rsa_public_key); - pk.rsa_public_key = NULL; + GNUNET_CRYPTO_rsa_public_key_free (pk->rsa_public_key); + pk->rsa_public_key = NULL; } } break; case TMH_PARSE_JNC_RET_RSA_SIGNATURE: { - struct TALER_DenominationSignature sig; + struct TALER_DenominationSignature *sig; - sig = *(struct TALER_DenominationSignature *) spec[i].destination; - if (NULL != sig.rsa_signature) + sig = spec[i].destination; + if (NULL != sig->rsa_signature) { - GNUNET_CRYPTO_rsa_signature_free (sig.rsa_signature); - sig.rsa_signature = NULL; + GNUNET_CRYPTO_rsa_signature_free (sig->rsa_signature); + sig->rsa_signature = NULL; } } break; @@ -1084,6 +1080,40 @@ TMH_PARSE_member_time_abs (const char *field, } +/** + * Generate line in parser specification for RSA public key. + * + * @param field name of the field + * @param[out] pk key to initialize + * @return corresponding field spec + */ +struct TMH_PARSE_FieldSpecification +TMH_PARSE_member_denomination_public_key (const char *field, + struct TALER_DenominationPublicKey *pk) +{ + struct TMH_PARSE_FieldSpecification ret = + { field, pk, 0, 0, TMH_PARSE_JNC_RET_RSA_PUBLIC_KEY, 0 }; + return ret; +} + + +/** + * Generate line in parser specification for RSA public key. + * + * @param field name of the field + * @param sig the signature to initialize + * @return corresponding field spec + */ +struct TMH_PARSE_FieldSpecification +TMH_PARSE_member_denomination_signature (const char *field, + struct TALER_DenominationSignature *sig) +{ + struct TMH_PARSE_FieldSpecification ret = + { field, sig, 0, 0, TMH_PARSE_JNC_RET_RSA_SIGNATURE, 0 }; + return ret; +} + + /** * Generate line in parser specification for an amount. * diff --git a/src/mint/taler-mint-httpd_parsing.h b/src/mint/taler-mint-httpd_parsing.h index c6981e60e..7dcbca68d 100644 --- a/src/mint/taler-mint-httpd_parsing.h +++ b/src/mint/taler-mint-httpd_parsing.h @@ -277,17 +277,24 @@ TMH_PARSE_member_object (const char *field, * Generate line in parser specification for RSA public key. * * @param field name of the field - * @param ptrpk address of `struct TALER_DenominationPublicKey` initialize + * @param[out] pk key to initialize + * @return corresponding field spec */ -#define TMH_PARSE_MEMBER_DENOMINATION_PUBLIC_KEY(field,ptrpk) { field, ptrpk, 0, 0, TMH_PARSE_JNC_RET_RSA_PUBLIC_KEY, 0 } +struct TMH_PARSE_FieldSpecification +TMH_PARSE_member_denomination_public_key (const char *field, + struct TALER_DenominationPublicKey *pk); + /** * Generate line in parser specification for RSA public key. * * @param field name of the field - * @param ptrsig address of `struct TALER_DenominationSignature *` initialize + * @param sig the signature to initialize + * @return corresponding field spec */ -#define TMH_PARSE_MEMBER_DENOMINATION_SIGNATURE(field,ptrsig) { field, ptrsig, 0, 0, TMH_PARSE_JNC_RET_RSA_SIGNATURE, 0 } +struct TMH_PARSE_FieldSpecification +TMH_PARSE_member_denomination_signature (const char *field, + struct TALER_DenominationSignature *sig); /** diff --git a/src/mint/taler-mint-httpd_refresh.c b/src/mint/taler-mint-httpd_refresh.c index 661376f5e..28670b98d 100644 --- a/src/mint/taler-mint-httpd_refresh.c +++ b/src/mint/taler-mint-httpd_refresh.c @@ -174,8 +174,8 @@ get_coin_public_info (struct MHD_Connection *connection, struct TALER_Amount amount; struct TMH_PARSE_FieldSpecification spec[] = { TMH_PARSE_MEMBER_FIXED ("coin_pub", &r_melt_detail->coin_info.coin_pub), - TMH_PARSE_MEMBER_DENOMINATION_SIGNATURE ("denom_sig", &sig.rsa_signature), - TMH_PARSE_MEMBER_DENOMINATION_PUBLIC_KEY ("denom_pub", &pk.rsa_public_key), + TMH_PARSE_member_denomination_signature ("denom_sig", &sig), + TMH_PARSE_member_denomination_public_key ("denom_pub", &pk), TMH_PARSE_MEMBER_FIXED ("confirm_sig", &melt_sig), TMH_PARSE_member_amount ("value_with_fee", &amount), TMH_PARSE_MEMBER_END diff --git a/src/mint/taler-mint-httpd_responses.c b/src/mint/taler-mint-httpd_responses.c index 74ecd15d1..9cacffe1b 100644 --- a/src/mint/taler-mint-httpd_responses.c +++ b/src/mint/taler-mint-httpd_responses.c @@ -47,10 +47,12 @@ TMH_RESPONSE_reply_json (struct MHD_Connection *connection, int ret; json_str = json_dumps (json, JSON_INDENT(2)); + GNUNET_assert (NULL != json_str); resp = MHD_create_response_from_buffer (strlen (json_str), json_str, MHD_RESPMEM_MUST_FREE); if (NULL == resp) { + free (json_str); GNUNET_break (0); return MHD_NO; } @@ -84,12 +86,17 @@ TMH_RESPONSE_reply_json_pack (struct MHD_Connection *connection, json_t *json; va_list argp; int ret; + json_error_t jerror; va_start (argp, fmt); - json = json_vpack_ex (NULL, 0, fmt, argp); + json = json_vpack_ex (&jerror, 0, fmt, argp); va_end (argp); if (NULL == json) { + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + "Failed to pack JSON with format `%s': %s\n", + fmt, + jerror.text); GNUNET_break (0); return MHD_NO; } @@ -229,10 +236,10 @@ TMH_RESPONSE_reply_external_error (struct MHD_Connection *connection, const char *hint) { return TMH_RESPONSE_reply_json_pack (connection, - MHD_HTTP_BAD_REQUEST, - "{s:s, s:s}", - "error", "client error", - "hint", hint); + MHD_HTTP_BAD_REQUEST, + "{s:s, s:s}", + "error", "client error", + "hint", hint); } @@ -247,9 +254,9 @@ int TMH_RESPONSE_reply_commit_error (struct MHD_Connection *connection) { return TMH_RESPONSE_reply_json_pack (connection, - MHD_HTTP_BAD_REQUEST, - "{s:s}", - "error", "commit failure"); + MHD_HTTP_BAD_REQUEST, + "{s:s}", + "error", "commit failure"); } @@ -571,16 +578,14 @@ compile_reserve_history (const struct TALER_MINTDB_ReserveHistory *rh, "type", "WITHDRAW", "signature", transaction, "amount", TALER_json_from_amount (&value))); - break; } } if (0 == ret) { - /* history is empty!? */ - GNUNET_break (0); - json_decref (json_history); - return NULL; + /* did not encounter any withdraw operations, set to zero */ + TALER_amount_get_zero (deposit_total.currency, + &withdraw_total); } if (GNUNET_SYSERR == TALER_amount_subtract (balance, @@ -591,6 +596,7 @@ compile_reserve_history (const struct TALER_MINTDB_ReserveHistory *rh, json_decref (json_history); return NULL; } + return json_history; } @@ -609,7 +615,6 @@ TMH_RESPONSE_reply_withdraw_status_success (struct MHD_Connection *connection, json_t *json_balance; json_t *json_history; struct TALER_Amount balance; - int ret; json_history = compile_reserve_history (rh, &balance); @@ -617,14 +622,11 @@ TMH_RESPONSE_reply_withdraw_status_success (struct MHD_Connection *connection, return TMH_RESPONSE_reply_internal_error (connection, "balance calculation failure"); json_balance = TALER_json_from_amount (&balance); - ret = TMH_RESPONSE_reply_json_pack (connection, - MHD_HTTP_OK, - "{s:o, s:o}", - "balance", json_balance, - "history", json_history); - json_decref (json_history); - json_decref (json_balance); - return ret; + return TMH_RESPONSE_reply_json_pack (connection, + MHD_HTTP_OK, + "{s:o, s:o}", + "balance", json_balance, + "history", json_history); } @@ -644,7 +646,6 @@ TMH_RESPONSE_reply_withdraw_sign_insufficient_funds (struct MHD_Connection *conn json_t *json_balance; json_t *json_history; struct TALER_Amount balance; - int ret; json_history = compile_reserve_history (rh, &balance); @@ -652,15 +653,12 @@ TMH_RESPONSE_reply_withdraw_sign_insufficient_funds (struct MHD_Connection *conn return TMH_RESPONSE_reply_internal_error (connection, "balance calculation failure"); json_balance = TALER_json_from_amount (&balance); - ret = TMH_RESPONSE_reply_json_pack (connection, - MHD_HTTP_FORBIDDEN, - "{s:s, s:o, s:o}", - "error", "Insufficient funds" - "balance", json_balance, - "history", json_history); - json_decref (json_history); - json_decref (json_balance); - return ret; + return TMH_RESPONSE_reply_json_pack (connection, + MHD_HTTP_FORBIDDEN, + "{s:s, s:o, s:o}", + "error", "Insufficient funds", + "balance", json_balance, + "history", json_history); } @@ -676,15 +674,12 @@ TMH_RESPONSE_reply_withdraw_sign_success (struct MHD_Connection *connection, const struct TALER_MINTDB_CollectableBlindcoin *collectable) { json_t *sig_json; - int ret; sig_json = TALER_json_from_rsa_signature (collectable->sig.rsa_signature); - ret = TMH_RESPONSE_reply_json_pack (connection, - MHD_HTTP_OK, - "{s:o}", - "ev_sig", sig_json); - json_decref (sig_json); - return ret; + return TMH_RESPONSE_reply_json_pack (connection, + MHD_HTTP_OK, + "{s:o}", + "ev_sig", sig_json); } @@ -743,7 +738,6 @@ TMH_RESPONSE_reply_refresh_melt_success (struct MHD_Connection *connection, struct TALER_RefreshMeltConfirmationPS body; struct TALER_MintSignatureP sig; json_t *sig_json; - int ret; body.purpose.size = htonl (sizeof (struct TALER_RefreshMeltConfirmationPS)); body.purpose.purpose = htonl (TALER_SIGNATURE_MINT_CONFIRM_MELT); @@ -754,13 +748,11 @@ TMH_RESPONSE_reply_refresh_melt_success (struct MHD_Connection *connection, sig_json = TALER_json_from_eddsa_sig (&body.purpose, &sig.eddsa_signature); GNUNET_assert (NULL != sig_json); - ret = TMH_RESPONSE_reply_json_pack (connection, - MHD_HTTP_OK, - "{s:i, s:o}", - "noreveal_index", (int) noreveal_index, - "signature", sig_json); - json_decref (sig_json); - return ret; + return TMH_RESPONSE_reply_json_pack (connection, + MHD_HTTP_OK, + "{s:i, s:o}", + "noreveal_index", (int) noreveal_index, + "signature", sig_json); } @@ -916,7 +908,6 @@ TMH_RESPONSE_reply_refresh_reveal_missmatch (struct MHD_Connection *connection, json_array_append_new (info_links, info_link_k); } - return TMH_RESPONSE_reply_json_pack (connection, MHD_HTTP_CONFLICT, "{s:s, s:i, s:i, s:o, s:o, s:o, s:o, s:s}", diff --git a/src/mint/taler-mint-httpd_withdraw.c b/src/mint/taler-mint-httpd_withdraw.c index ca7ec9a01..877ae8f3c 100644 --- a/src/mint/taler-mint-httpd_withdraw.c +++ b/src/mint/taler-mint-httpd_withdraw.c @@ -105,7 +105,7 @@ TMH_WITHDRAW_handler_withdraw_sign (struct TMH_RequestHandler *rh, TMH_PARSE_MEMBER_VARIABLE ("coin_ev"), TMH_PARSE_MEMBER_FIXED ("reserve_pub", &wsrd.reserve_pub), TMH_PARSE_MEMBER_FIXED ("reserve_sig", &signature), - TMH_PARSE_MEMBER_DENOMINATION_PUBLIC_KEY ("denom_pub", &denomination_pub), + TMH_PARSE_member_denomination_public_key ("denom_pub", &denomination_pub), TMH_PARSE_MEMBER_END };