From 362bf0c5bd2270ed6a54dfbdfd83702dca892797 Mon Sep 17 00:00:00 2001 From: Christian Grothoff Date: Tue, 20 Jul 2021 19:33:59 +0200 Subject: -workshop refactoring --- ...ler-merchant-httpd_private-patch-instances-ID.c | 146 +++++++-------------- 1 file changed, 47 insertions(+), 99 deletions(-) (limited to 'src/backend/taler-merchant-httpd_private-patch-instances-ID.c') diff --git a/src/backend/taler-merchant-httpd_private-patch-instances-ID.c b/src/backend/taler-merchant-httpd_private-patch-instances-ID.c index 0a839a3a..8b0917e7 100644 --- a/src/backend/taler-merchant-httpd_private-patch-instances-ID.c +++ b/src/backend/taler-merchant-httpd_private-patch-instances-ID.c @@ -24,6 +24,7 @@ */ #include "platform.h" #include "taler-merchant-httpd_private-patch-instances-ID.h" +#include "taler-merchant-httpd_helper.h" #include @@ -74,12 +75,12 @@ patch_instances_ID (struct TMH_MerchantInstance *mi, &is.address), GNUNET_JSON_spec_json ("jurisdiction", &is.jurisdiction), - TALER_JSON_spec_amount ("default_max_deposit_fee", - &is.default_max_deposit_fee), TALER_JSON_spec_amount ("default_max_wire_fee", &is.default_max_wire_fee), GNUNET_JSON_spec_uint32 ("default_wire_fee_amortization", &is.default_wire_fee_amortization), + TALER_JSON_spec_amount ("default_max_deposit_fee", + &is.default_max_deposit_fee), TALER_JSON_spec_relative_time ("default_wire_transfer_delay", &is.default_wire_transfer_delay), TALER_JSON_spec_relative_time ("default_pay_delay", @@ -87,6 +88,7 @@ patch_instances_ID (struct TMH_MerchantInstance *mi, GNUNET_JSON_spec_end () }; enum GNUNET_DB_QueryStatus qs; + bool committed = false; GNUNET_assert (NULL != mi); { @@ -100,7 +102,19 @@ patch_instances_ID (struct TMH_MerchantInstance *mi, ? MHD_YES : MHD_NO; } - if (! json_is_array (payto_uris)) + if ((0 != strcasecmp (is.default_max_deposit_fee.currency, + TMH_currency)) || + (0 != strcasecmp (is.default_max_wire_fee.currency, + TMH_currency))) + { + GNUNET_break_op (0); + GNUNET_JSON_parse_free (spec); + return TALER_MHD_reply_with_error (connection, + MHD_HTTP_CONFLICT, + TALER_EC_GENERIC_CURRENCY_MISMATCH, + NULL); + } + if (! TMH_payto_uri_array_valid (payto_uris)) return TALER_MHD_reply_with_error (connection, MHD_HTTP_BAD_REQUEST, TALER_EC_GENERIC_PAYTO_URI_MALFORMED, @@ -119,18 +133,6 @@ patch_instances_ID (struct TMH_MerchantInstance *mi, free_wm (wm); } } - if ((0 != strcasecmp (is.default_max_deposit_fee.currency, - TMH_currency)) || - (0 != strcasecmp (is.default_max_wire_fee.currency, - TMH_currency))) - { - GNUNET_break_op (0); - GNUNET_JSON_parse_free (spec); - return TALER_MHD_reply_with_error (connection, - MHD_HTTP_CONFLICT, - TALER_EC_GENERIC_CURRENCY_MISMATCH, - NULL); - } if (GNUNET_OK != TMH_db->start (TMH_db->cls, "PATCH /instances")) @@ -199,31 +201,12 @@ patch_instances_ID (struct TMH_MerchantInstance *mi, { const char *str = json_string_value (json_array_get (payto_uris, i)); - if (NULL == str) - { - GNUNET_break_op (0); - TMH_db->rollback (TMH_db->cls); - GNUNET_JSON_parse_free (spec); - GNUNET_assert (NULL == wm_head); - return TALER_MHD_reply_with_error (connection, - MHD_HTTP_BAD_REQUEST, - TALER_EC_GENERIC_PAYTO_URI_MALFORMED, - NULL); - } if (0 == strcasecmp (uri, str)) { - if (NULL != matches[i]) - { - GNUNET_break (0); - TMH_db->rollback (TMH_db->cls); - GNUNET_JSON_parse_free (spec); - GNUNET_assert (NULL == wm_head); - return TALER_MHD_reply_with_error (connection, - MHD_HTTP_BAD_REQUEST, - TALER_EC_GENERIC_PAYTO_URI_MALFORMED, - str); - } + /* our own existing payto URIs should be unique, that is no + duplicates in the list, so we cannot match twice */ + GNUNET_assert (NULL == matches[i]); matches[i] = wm; matched = true; break; @@ -239,6 +222,7 @@ patch_instances_ID (struct TMH_MerchantInstance *mi, uri); wm->deleting = true; qs = TMH_db->inactivate_account (TMH_db->cls, + mi->settings.id, &wm->h_wire); if (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT != qs) { @@ -262,6 +246,7 @@ patch_instances_ID (struct TMH_MerchantInstance *mi, if (! wm->active) { qs = TMH_db->activate_account (TMH_db->cls, + mi->settings.id, &wm->h_wire); if (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT != qs) { @@ -281,51 +266,7 @@ patch_instances_ID (struct TMH_MerchantInstance *mi, GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Adding NEW account `%s'\n", ad.payto_uri); - GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_NONCE, - &ad.salt, - sizeof (ad.salt)); - wm = GNUNET_new (struct TMH_WireMethod); - wm->j_wire = json_pack ("{s:s, s:o}", - "payto_uri", ad.payto_uri, - "salt", GNUNET_JSON_from_data_auto (&ad.salt)); - GNUNET_assert (NULL != wm->j_wire); - wm->wire_method - = TALER_payto_get_method (ad.payto_uri); - if (NULL == wm->wire_method) - { - GNUNET_break_op (0); - TMH_db->rollback (TMH_db->cls); - GNUNET_JSON_parse_free (spec); - json_decref (wm->j_wire); - GNUNET_free (wm); - return TALER_MHD_reply_with_error (connection, - MHD_HTTP_BAD_REQUEST, - TALER_EC_GENERIC_PAYTO_URI_MALFORMED, - ad.payto_uri); - - } - /* This also tests for things like the IBAN being malformed */ - if (GNUNET_OK != - TALER_JSON_merchant_wire_signature_hash (wm->j_wire, - &wm->h_wire)) - { - GNUNET_break_op (0); - free_wm (wm); - while (NULL != (wm = wm_head)) - { - GNUNET_CONTAINER_DLL_remove (wm_head, - wm_tail, - wm); - free_wm (wm); - } - TMH_db->rollback (TMH_db->cls); - GNUNET_JSON_parse_free (spec); - return TALER_MHD_reply_with_error (connection, - MHD_HTTP_BAD_REQUEST, - TALER_EC_GENERIC_PAYTO_URI_MALFORMED, - ad.payto_uri); - } - wm->active = true; + wm = TMH_setup_wire_account (ad.payto_uri); GNUNET_CONTAINER_DLL_insert (wm_head, wm_tail, wm); @@ -349,10 +290,31 @@ patch_instances_ID (struct TMH_MerchantInstance *mi, retry: if (GNUNET_DB_STATUS_SOFT_ERROR == qs) continue; + if (qs >= 0) + committed = true; break; } /* for(... MAX_RETRIES) */ giveup: - if (0 > qs) + /* Deactivate existing wire methods that were removed above */ + for (struct TMH_WireMethod *wm = mi->wm_head; + NULL != wm; + wm = wm->next) + { + /* We did not flip the 'active' bits earlier because the + DB transaction could still fail. Now it is time to update our + runtime state. */ + GNUNET_assert (! (wm->deleting & wm->enabling)); + if (committed) + { + if (wm->deleting) + wm->active = false; + if (wm->enabling) + wm->active = true; + } + wm->deleting = false; + wm->enabling = false; + } + if (! committed) { struct TMH_WireMethod *wm; @@ -369,22 +331,6 @@ giveup: TALER_EC_GENERIC_DB_COMMIT_FAILED, NULL); } - /* Deactivate existing wire methods that were removed above */ - for (struct TMH_WireMethod *wm = mi->wm_head; - NULL != wm; - wm = wm->next) - { - /* We did not flip the 'active' bits earlier because the - DB transaction could still fail. Now it is time to update our - runtime state. */ - GNUNET_assert (! (wm->deleting & wm->enabling)); - if (wm->deleting) - wm->active = false; - if (wm->enabling) - wm->active = true; - wm->deleting = false; - wm->enabling = false; - } /* Update our 'settings' */ GNUNET_free (mi->settings.name); @@ -400,6 +346,8 @@ giveup: { struct TMH_WireMethod *wm; + /* Note: this _could_ be done more efficiently if + someone wrote a GNUNET_CONTAINER_DLL_merge()... */ while (NULL != (wm = wm_head)) { GNUNET_CONTAINER_DLL_remove (wm_head, -- cgit v1.2.3