From 5533bcbf651eafce682212ce635bdf8ce83d4bcb Mon Sep 17 00:00:00 2001 From: Christian Grothoff Date: Wed, 28 Dec 2022 22:55:48 +0100 Subject: add purse delete testing CMD --- src/testing/Makefile.am | 1 + src/testing/testing_api_cmd_purse_delete.c | 190 +++++++++++++++++++++++++++++ src/testing/testing_api_cmd_withdraw.c | 3 - 3 files changed, 191 insertions(+), 3 deletions(-) create mode 100644 src/testing/testing_api_cmd_purse_delete.c (limited to 'src/testing') diff --git a/src/testing/Makefile.am b/src/testing/Makefile.am index 5ae7de40..db2a54e7 100644 --- a/src/testing/Makefile.am +++ b/src/testing/Makefile.am @@ -77,6 +77,7 @@ libtalertesting_la_SOURCES = \ testing_api_cmd_offline_sign_keys.c \ testing_api_cmd_offline_sign_extensions.c \ testing_api_cmd_purse_create_deposit.c \ + testing_api_cmd_purse_delete.c \ testing_api_cmd_purse_deposit.c \ testing_api_cmd_purse_get.c \ testing_api_cmd_purse_merge.c \ diff --git a/src/testing/testing_api_cmd_purse_delete.c b/src/testing/testing_api_cmd_purse_delete.c new file mode 100644 index 00000000..aa085327 --- /dev/null +++ b/src/testing/testing_api_cmd_purse_delete.c @@ -0,0 +1,190 @@ +/* + This file is part of TALER + Copyright (C) 2020 Taler Systems SA + + TALER is free software; you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3, or (at your + option) any later version. + + TALER is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received a copy of the GNU General Public + License along with TALER; see the file COPYING. If not, see + +*/ +/** + * @file testing/testing_api_cmd_purse_delete.c + * @brief command for testing /management/purse/disable. + * @author Christian Grothoff + */ +#include "platform.h" +#include "taler_json_lib.h" +#include +#include "taler_testing_lib.h" +#include "taler_signatures.h" +#include "backoff.h" + + +/** + * State for a "purse_delete" CMD. + */ +struct PurseDeleteState +{ + + /** + * Purse delete handle while operation is running. + */ + struct TALER_EXCHANGE_PurseDeleteHandle *pdh; + + /** + * Our interpreter. + */ + struct TALER_TESTING_Interpreter *is; + + /** + * Expected HTTP response code. + */ + unsigned int expected_response_code; + + /** + * Command that created the purse we now want to + * delete. + */ + const char *purse_cmd; +}; + + +/** + * Callback to analyze the DELETE /purses/$PID response, just used to check if + * the response code is acceptable. + * + * @param cls closure. + * @param pdr HTTP response details + */ +static void +purse_delete_cb (void *cls, + const struct TALER_EXCHANGE_PurseDeleteResponse *pdr) +{ + struct PurseDeleteState *pds = cls; + + pds->pdh = NULL; + if (pds->expected_response_code != pdr->hr.http_status) + { + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + "Unexpected response code %u to command %s in %s:%u\n", + pdr->hr.http_status, + pds->is->commands[pds->is->ip].label, + __FILE__, + __LINE__); + json_dumpf (pdr->hr.reply, + stderr, + 0); + TALER_TESTING_interpreter_fail (pds->is); + return; + } + TALER_TESTING_interpreter_next (pds->is); +} + + +/** + * Run the command. + * + * @param cls closure. + * @param cmd the command to execute. + * @param is the interpreter state. + */ +static void +purse_delete_run (void *cls, + const struct TALER_TESTING_Command *cmd, + struct TALER_TESTING_Interpreter *is) +{ + struct PurseDeleteState *pds = cls; + const struct TALER_PurseContractPrivateKeyP *purse_priv; + const struct TALER_TESTING_Command *ref; + + (void) cmd; + ref = TALER_TESTING_interpreter_lookup_command (is, + pds->purse_cmd); + if (NULL == ref) + { + GNUNET_break (0); + TALER_TESTING_interpreter_fail (is); + return; + } + if (GNUNET_OK != + TALER_TESTING_get_trait_purse_priv (ref, + &purse_priv)) + { + GNUNET_break (0); + TALER_TESTING_interpreter_fail (is); + return; + } + pds->is = is; + pds->pdh = TALER_EXCHANGE_purse_delete ( + is->exchange, + purse_priv, + &purse_delete_cb, + pds); + if (NULL == pds->pdh) + { + GNUNET_break (0); + TALER_TESTING_interpreter_fail (is); + return; + } +} + + +/** + * Free the state of a "purse_delete" CMD, and possibly cancel a + * pending operation thereof. + * + * @param cls closure, must be a `struct PurseDeleteState`. + * @param cmd the command which is being cleaned up. + */ +static void +purse_delete_cleanup (void *cls, + const struct TALER_TESTING_Command *cmd) +{ + struct PurseDeleteState *pds = cls; + + if (NULL != pds->pdh) + { + GNUNET_log (GNUNET_ERROR_TYPE_WARNING, + "Command %u (%s) did not complete\n", + pds->is->ip, + cmd->label); + TALER_EXCHANGE_purse_delete_cancel (pds->pdh); + pds->pdh = NULL; + } + GNUNET_free (pds); +} + + +struct TALER_TESTING_Command +TALER_TESTING_cmd_purse_delete (const char *label, + unsigned int expected_http_status, + const char *purse_cmd) +{ + struct PurseDeleteState *ds; + + ds = GNUNET_new (struct PurseDeleteState); + ds->expected_response_code = expected_http_status; + ds->purse_cmd = purse_cmd; + { + struct TALER_TESTING_Command cmd = { + .cls = ds, + .label = label, + .run = &purse_delete_run, + .cleanup = &purse_delete_cleanup + }; + + return cmd; + } +} + + +/* end of testing_api_cmd_purse_delete.c */ diff --git a/src/testing/testing_api_cmd_withdraw.c b/src/testing/testing_api_cmd_withdraw.c index 7a81d1c3..1bd3c187 100644 --- a/src/testing/testing_api_cmd_withdraw.c +++ b/src/testing/testing_api_cmd_withdraw.c @@ -360,14 +360,12 @@ withdraw_run (void *cls, = TALER_TESTING_interpreter_lookup_command ( is, ws->reserve_reference); - if (NULL == create_reserve) { GNUNET_break (0); TALER_TESTING_interpreter_fail (is); return; } - if (GNUNET_OK != TALER_TESTING_get_trait_reserve_priv (create_reserve, &rp)) @@ -376,7 +374,6 @@ withdraw_run (void *cls, TALER_TESTING_interpreter_fail (is); return; } - if (NULL == ws->exchange_url) ws->exchange_url = GNUNET_strdup (TALER_EXCHANGE_get_base_url (is->exchange)); -- cgit v1.2.3 From 5df74558de6e725841c2f5a2ea6f90d0af264c89 Mon Sep 17 00:00:00 2001 From: Christian Grothoff Date: Thu, 29 Dec 2022 00:34:36 +0100 Subject: misc purse deletion fixes --- src/exchange/taler-exchange-httpd_purses_delete.c | 1 + src/exchangedb/exchange_do_delete_purse.sql | 119 ---------------------- src/exchangedb/exchange_do_purse_delete.sql | 119 ++++++++++++++++++++++ src/exchangedb/procedures.sql.in | 2 +- src/lib/exchange_api_purse_delete.c | 4 + src/testing/test_exchange_p2p.c | 13 +++ 6 files changed, 138 insertions(+), 120 deletions(-) delete mode 100644 src/exchangedb/exchange_do_delete_purse.sql create mode 100644 src/exchangedb/exchange_do_purse_delete.sql (limited to 'src/testing') diff --git a/src/exchange/taler-exchange-httpd_purses_delete.c b/src/exchange/taler-exchange-httpd_purses_delete.c index 58cc7825..d4b74b16 100644 --- a/src/exchange/taler-exchange-httpd_purses_delete.c +++ b/src/exchange/taler-exchange-httpd_purses_delete.c @@ -127,6 +127,7 @@ TEH_handler_purses_delete ( } if (decided) { + GNUNET_break_op (0); return TALER_MHD_reply_with_ec ( connection, TALER_EC_EXCHANGE_PURSE_DELETE_ALREADY_DECIDED, diff --git a/src/exchangedb/exchange_do_delete_purse.sql b/src/exchangedb/exchange_do_delete_purse.sql deleted file mode 100644 index a57f2545..00000000 --- a/src/exchangedb/exchange_do_delete_purse.sql +++ /dev/null @@ -1,119 +0,0 @@ --- --- This file is part of TALER --- Copyright (C) 2014--2022 Taler Systems SA --- --- TALER is free software; you can redistribute it and/or modify it under the --- terms of the GNU General Public License as published by the Free Software --- Foundation; either version 3, or (at your option) any later version. --- --- TALER is distributed in the hope that it will be useful, but WITHOUT ANY --- WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR --- A PARTICULAR PURPOSE. See the GNU General Public License for more details. --- --- You should have received a copy of the GNU General Public License along with --- TALER; see the file COPYING. If not, see --- - -CREATE OR REPLACE FUNCTION exchange_do_delete_purse( - IN in_purse_pub BYTEA, - IN in_purse_sig BYTEA, - IN in_now INT8, - OUT out_decided BOOLEAN, - OUT out_found BOOLEAN) -LANGUAGE plpgsql -AS $$ -DECLARE - my_deposit record; -DECLARE - my_in_reserve_quota BOOLEAN; -BEGIN - -SELECT COUNT(*) FROM purse_decision - WHERE purse_pub=in_purse_pub; -IF FOUND -THEN - out_found=TRUE; - out_decided=TRUE; - RETURN; -END IF; -out_decided=FALSE; - -SELECT in_reserve_quota - INTO my_in_reserve_quota - FROM exchange.purse_requests - WHERE purse_pub=in_purse_pub; -out_found=FOUND; -IF NOT FOUND -THEN - RETURN; -END IF; - --- store reserve deletion -INSERT INTO purse_deletion - (purse_pub - ,purse_sig) -VALUES - (in_purse_pub - ,in_purse_sig) -ON CONFLICT DO NOTHING; - -IF NOT FOUND -THEN - RETURN; -END IF; - --- Delete contract associated with purse, if it exists. -DELETE FROM contracts - WHERE purse_pub=in_purse_pub; - --- store purse decision -INSERT INTO purse_decision - (purse_pub - ,action_timestamp - ,refunded) -VALUES - (in_purse_pub - ,in_now - ,TRUE); - --- update purse quota at reserve -IF (my_in_reserve_quota) -THEN - UPDATE reserves - SET purses_active=purses_active-1 - WHERE reserve_pub IN - (SELECT reserve_pub - FROM exchange.purse_merges - WHERE purse_pub=in_purse_pub - LIMIT 1); -END IF; - --- restore balance to each coin deposited into the purse -FOR my_deposit IN - SELECT coin_pub - ,amount_with_fee_val - ,amount_with_fee_frac - FROM exchange.purse_deposits - WHERE purse_pub = in_purse_pub -LOOP - UPDATE exchange.known_coins SET - remaining_frac=remaining_frac+my_deposit.amount_with_fee_frac - - CASE - WHEN remaining_frac+my_deposit.amount_with_fee_frac >= 100000000 - THEN 100000000 - ELSE 0 - END, - remaining_val=remaining_val+my_deposit.amount_with_fee_val - + CASE - WHEN remaining_frac+my_deposit.amount_with_fee_frac >= 100000000 - THEN 1 - ELSE 0 - END - WHERE coin_pub = my_deposit.coin_pub; -END LOOP; - - -END $$; - -COMMENT ON FUNCTION exchange_do_delete_purse(BYTEA,BYTEA,INT8) - IS 'Delete a previously undecided purse and refund the coins (if any).'; diff --git a/src/exchangedb/exchange_do_purse_delete.sql b/src/exchangedb/exchange_do_purse_delete.sql new file mode 100644 index 00000000..096475b4 --- /dev/null +++ b/src/exchangedb/exchange_do_purse_delete.sql @@ -0,0 +1,119 @@ +-- +-- This file is part of TALER +-- Copyright (C) 2014--2022 Taler Systems SA +-- +-- TALER is free software; you can redistribute it and/or modify it under the +-- terms of the GNU General Public License as published by the Free Software +-- Foundation; either version 3, or (at your option) any later version. +-- +-- TALER is distributed in the hope that it will be useful, but WITHOUT ANY +-- WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR +-- A PARTICULAR PURPOSE. See the GNU General Public License for more details. +-- +-- You should have received a copy of the GNU General Public License along with +-- TALER; see the file COPYING. If not, see +-- + +CREATE OR REPLACE FUNCTION exchange_do_purse_delete( + IN in_purse_pub BYTEA, + IN in_purse_sig BYTEA, + IN in_now INT8, + OUT out_decided BOOLEAN, + OUT out_found BOOLEAN) +LANGUAGE plpgsql +AS $$ +DECLARE + my_deposit record; +DECLARE + my_in_reserve_quota BOOLEAN; +BEGIN + +PERFORM refunded FROM purse_decision + WHERE purse_pub=in_purse_pub; +IF FOUND +THEN + out_found=TRUE; + out_decided=TRUE; + RETURN; +END IF; +out_decided=FALSE; + +SELECT in_reserve_quota + INTO my_in_reserve_quota + FROM exchange.purse_requests + WHERE purse_pub=in_purse_pub; +out_found=FOUND; +IF NOT FOUND +THEN + RETURN; +END IF; + +-- store reserve deletion +INSERT INTO exchange.purse_deletion + (purse_pub + ,purse_sig) +VALUES + (in_purse_pub + ,in_purse_sig) +ON CONFLICT DO NOTHING; + +IF NOT FOUND +THEN + RETURN; +END IF; + +-- Delete contract associated with purse, if it exists. +DELETE FROM contracts + WHERE purse_pub=in_purse_pub; + +-- store purse decision +INSERT INTO purse_decision + (purse_pub + ,action_timestamp + ,refunded) +VALUES + (in_purse_pub + ,in_now + ,TRUE); + +-- update purse quota at reserve +IF (my_in_reserve_quota) +THEN + UPDATE reserves + SET purses_active=purses_active-1 + WHERE reserve_pub IN + (SELECT reserve_pub + FROM exchange.purse_merges + WHERE purse_pub=in_purse_pub + LIMIT 1); +END IF; + +-- restore balance to each coin deposited into the purse +FOR my_deposit IN + SELECT coin_pub + ,amount_with_fee_val + ,amount_with_fee_frac + FROM exchange.purse_deposits + WHERE purse_pub = in_purse_pub +LOOP + UPDATE exchange.known_coins SET + remaining_frac=remaining_frac+my_deposit.amount_with_fee_frac + - CASE + WHEN remaining_frac+my_deposit.amount_with_fee_frac >= 100000000 + THEN 100000000 + ELSE 0 + END, + remaining_val=remaining_val+my_deposit.amount_with_fee_val + + CASE + WHEN remaining_frac+my_deposit.amount_with_fee_frac >= 100000000 + THEN 1 + ELSE 0 + END + WHERE coin_pub = my_deposit.coin_pub; +END LOOP; + + +END $$; + +COMMENT ON FUNCTION exchange_do_purse_delete(BYTEA,BYTEA,INT8) + IS 'Delete a previously undecided purse and refund the coins (if any).'; diff --git a/src/exchangedb/procedures.sql.in b/src/exchangedb/procedures.sql.in index af47bbf6..19483024 100644 --- a/src/exchangedb/procedures.sql.in +++ b/src/exchangedb/procedures.sql.in @@ -28,11 +28,11 @@ SET search_path TO exchange; #include "exchange_do_recoup_to_reserve.sql" #include "exchange_do_recoup_to_coin.sql" #include "exchange_do_gc.sql" +#include "exchange_do_purse_delete.sql" #include "exchange_do_purse_deposit.sql" #include "exchange_do_purse_merge.sql" #include "exchange_do_reserve_purse.sql" #include "exchange_do_expire_purse.sql" -#include "exchange_do_delete_purse.sql" #include "exchange_do_history_request.sql" #include "exchange_do_reserve_open_deposit.sql" #include "exchange_do_reserve_open.sql" diff --git a/src/lib/exchange_api_purse_delete.c b/src/lib/exchange_api_purse_delete.c index 68035b48..27a9082b 100644 --- a/src/lib/exchange_api_purse_delete.c +++ b/src/lib/exchange_api_purse_delete.c @@ -208,6 +208,10 @@ TALER_EXCHANGE_purse_delete ( GNUNET_free (xhdr); } eh = TALER_EXCHANGE_curl_easy_get_ (pdh->url); + GNUNET_assert (CURLE_OK == + curl_easy_setopt (eh, + CURLOPT_CUSTOMREQUEST, + MHD_HTTP_METHOD_DELETE)); GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "URL for purse delete: `%s'\n", pdh->url); diff --git a/src/testing/test_exchange_p2p.c b/src/testing/test_exchange_p2p.c index f159b2f1..ad95bf63 100644 --- a/src/testing/test_exchange_p2p.c +++ b/src/testing/test_exchange_p2p.c @@ -158,6 +158,19 @@ run (void *cls, TALER_TESTING_cmd_end () }; struct TALER_TESTING_Command push[] = { + TALER_TESTING_cmd_purse_create_with_deposit ( + "purse-with-deposit-for-delete", + MHD_HTTP_OK, + "{\"amount\":\"EUR:1\",\"summary\":\"ice cream\"}", + true, /* upload contract */ + GNUNET_TIME_UNIT_MINUTES, /* expiration */ + "withdraw-coin-1", + "EUR:1.01", + NULL), + TALER_TESTING_cmd_purse_delete ( + "purse-with-deposit-delete", + MHD_HTTP_NO_CONTENT, + "purse-with-deposit-for-delete"), TALER_TESTING_cmd_purse_create_with_deposit ( "purse-with-deposit", MHD_HTTP_OK, -- cgit v1.2.3