testing_api_cmd_refresh.c (39059B)
1 /* 2 This file is part of TALER 3 Copyright (C) 2018-2022 Taler Systems SA 4 5 TALER is free software; you can redistribute it and/or modify it 6 under the terms of the GNU General Public License as published by 7 the Free Software Foundation; either version 3, or (at your 8 option) any later version. 9 10 TALER is distributed in the hope that it will be useful, but 11 WITHOUT ANY WARRANTY; without even the implied warranty of 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 General Public License for more details. 14 15 You should have received a copy of the GNU General Public 16 License along with TALER; see the file COPYING. If not, see 17 <http://www.gnu.org/licenses/> 18 */ 19 /** 20 * @file testing/testing_api_cmd_refresh.c 21 * @brief commands for testing all "refresh" features. 22 * @author Marcello Stanisci 23 * @author Özgür Kesim 24 */ 25 #include "taler/taler_json_lib.h" 26 #include <gnunet/gnunet_curl_lib.h> 27 struct MeltState; 28 struct RevealMeltState; 29 #define TALER_EXCHANGE_POST_MELT_RESULT_CLOSURE struct MeltState 30 #include "taler/exchange/post-melt.h" 31 #define TALER_EXCHANGE_POST_REVEAL_MELT_RESULT_CLOSURE struct RevealMeltState 32 #include "taler/exchange/post-reveal-melt.h" 33 #include "taler/taler_testing_lib.h" 34 #include "taler/taler_signatures.h" 35 #include "backoff.h" 36 37 /** 38 * How long do we wait AT MOST when retrying? 39 */ 40 #define MAX_BACKOFF GNUNET_TIME_relative_multiply ( \ 41 GNUNET_TIME_UNIT_MILLISECONDS, 100) 42 43 /** 44 * How often do we retry before giving up? 45 */ 46 #define NUM_RETRIES 5 47 48 /** 49 * How long do we wait AT MOST when retrying? 50 */ 51 #define MAX_BACKOFF GNUNET_TIME_relative_multiply ( \ 52 GNUNET_TIME_UNIT_MILLISECONDS, 100) 53 54 /** 55 * Information about a fresh coin generated by the refresh 56 * operation. 57 */ 58 struct TALER_TESTING_FreshCoinData 59 { 60 61 /** 62 * If @e amount is NULL, this specifies the denomination key to 63 * use. Otherwise, this will be set (by the interpreter) to the 64 * denomination PK matching @e amount. 65 */ 66 const struct TALER_EXCHANGE_DenomPublicKey *pk; 67 68 /** 69 * Set (by the interpreter) to the exchange's signature over the 70 * coin's public key. 71 */ 72 struct TALER_DenominationSignature sig; 73 74 /** 75 * Set (by the interpreter) to the coin's private key. 76 */ 77 struct TALER_CoinSpendPrivateKeyP coin_priv; 78 79 /** 80 * Set (by the interpreter) to the coin's public key. 81 */ 82 struct TALER_CoinSpendPublicKeyP coin_pub; 83 84 /** 85 * Fresh age commitment for the coin with proof and its hash, NULL if not 86 * applicable. 87 */ 88 struct TALER_AgeCommitmentProof *age_commitment_proof; 89 struct TALER_AgeCommitmentHashP h_age_commitment; 90 91 /** 92 * The blinding key (needed for recoup operations). 93 */ 94 union GNUNET_CRYPTO_BlindingSecretP blinding_key; 95 96 }; 97 98 99 /** 100 * State for a "refresh melt" command. 101 */ 102 struct MeltState 103 { 104 105 /** 106 * Reference to reserve_withdraw operations for coin to 107 * be used for the /refresh/melt operation. 108 */ 109 const char *coin_reference; 110 111 /** 112 * Our command. 113 */ 114 const struct TALER_TESTING_Command *cmd; 115 116 /** 117 * Reference to a previous melt command. 118 */ 119 const char *melt_reference; 120 121 /** 122 * Melt handle while operation is running. 123 */ 124 struct TALER_EXCHANGE_PostMeltHandle *mh; 125 126 /** 127 * Expected entry in the coin history created by this 128 * operation. 129 */ 130 struct TALER_EXCHANGE_CoinHistoryEntry che; 131 132 /** 133 * Interpreter state. 134 */ 135 struct TALER_TESTING_Interpreter *is; 136 137 /** 138 * The input for the call to /melt 139 */ 140 struct TALER_EXCHANGE_MeltInput melt_input; 141 142 /** 143 * Length of the @a blinding_values array with the exchange values 144 * and blinding keys we are using. 145 */ 146 unsigned int num_blinding_values; 147 148 /** 149 * Blinding values returned per coin. 150 */ 151 struct TALER_ExchangeBlindingValues *blinding_values; 152 153 /** 154 * The input for the call to /reveal-melt 155 */ 156 struct TALER_EXCHANGE_RevealMeltInput reveal_melt_input; 157 158 /** 159 * Array of the denomination public keys 160 * corresponding to the @e num_fresh_coins; 161 */ 162 struct TALER_EXCHANGE_DenomPublicKey *fresh_pks; 163 164 /** 165 * Private key of the dirty coin being melted. 166 */ 167 const struct TALER_CoinSpendPrivateKeyP *melt_priv; 168 169 /** 170 * Public key of the dirty coin being melted. 171 */ 172 struct TALER_CoinSpendPublicKeyP melt_pub; 173 174 /** 175 * Entropy seed for the refresh-melt operation. 176 */ 177 struct TALER_PublicRefreshMasterSeedP rms; 178 179 /** 180 * If false, @e blinding_seed contains the seed for the 181 * blinding values for CS signatures 182 */ 183 bool no_blinding_seed; 184 185 /** 186 * If @e no_blinding_seed is false, contains the blinding 187 * seed from which the nonces were derived for CS signatures 188 */ 189 struct TALER_BlindingMasterSeedP blinding_seed; 190 191 /** 192 * The refresh commitment we calculated 193 */ 194 struct TALER_RefreshCommitmentP rc; 195 196 /** 197 * Version of the refresh commitment @e rc. 198 */ 199 enum TALER_RefreshCommitmentVersion rc_version; 200 201 /** 202 * The kappa refresh nonces for signing with the old coin. 203 */ 204 struct TALER_KappaPublicRefreshNoncesP kappa_nonces; 205 206 /** 207 * Task scheduled to try later. 208 */ 209 struct GNUNET_SCHEDULER_Task *retry_task; 210 211 /** 212 * How long do we wait until we retry? 213 */ 214 struct GNUNET_TIME_Relative backoff; 215 216 /** 217 * How long did we wait in total for retries? 218 */ 219 struct GNUNET_TIME_Relative total_backoff; 220 221 /** 222 * Amounts to be generated during melt. 223 */ 224 const char **melt_fresh_amounts; 225 226 /** 227 * Number of fresh coins generated by the melt. 228 */ 229 unsigned int num_fresh_coins; 230 231 /** 232 * Expected HTTP response code. 233 */ 234 unsigned int expected_response_code; 235 236 /** 237 * if set to #GNUNET_YES, then two /refresh/melt operations 238 * will be performed. This is needed to trigger the logic 239 * that manages those already-made requests. Note: it 240 * is not possible to just copy-and-paste a test refresh melt 241 * CMD to have the same effect, because every data preparation 242 * generates new planchets that (in turn) make the whole "hash" 243 * different from any previous one, therefore NOT allowing the 244 * exchange to pick any previous /rerfesh/melt operation from 245 * the database. 246 */ 247 bool double_melt; 248 249 /** 250 * If true, melt like a client facing an exchange from before 251 * protocol v45, that is, with the refresh commitment that does not 252 * include the transfer public keys (#11862). 253 */ 254 bool legacy_commitment; 255 256 /** 257 * How often should we retry on (transient) failures? 258 */ 259 unsigned int do_retry; 260 261 /** 262 * Set by the melt callback as it comes from the exchange. 263 */ 264 uint16_t noreveal_index; 265 266 /** 267 * The signatures over the nonces we need to reveal 268 */ 269 struct TALER_RevealPrivateRefreshNonceSignaturesP revealed_signatures; 270 271 }; 272 273 274 /** 275 * State for a "refresh reveal" CMD. 276 */ 277 struct RevealMeltState 278 { 279 /** 280 * Link to a "refresh melt" command. 281 */ 282 const char *melt_reference; 283 284 /** 285 * Reveal handle while operation is running. 286 */ 287 struct TALER_EXCHANGE_PostRevealMeltHandle *rmh; 288 289 /** 290 * Our command. 291 */ 292 const struct TALER_TESTING_Command *cmd; 293 294 /** 295 * Convenience struct to keep in one place all the 296 * data related to one fresh coin, set by the reveal callback 297 * as it comes from the exchange. 298 */ 299 struct TALER_TESTING_FreshCoinData *fresh_coins; 300 301 /** 302 * Array of @e num_fresh_coins planchet secrets derived 303 * from the transfer secret per fresh coin. 304 */ 305 struct TALER_PlanchetMasterSecretP *psa; 306 307 /** 308 * Interpreter state. 309 */ 310 struct TALER_TESTING_Interpreter *is; 311 312 /** 313 * Task scheduled to try later. 314 */ 315 struct GNUNET_SCHEDULER_Task *retry_task; 316 317 /** 318 * How long do we wait until we retry? 319 */ 320 struct GNUNET_TIME_Relative backoff; 321 322 /** 323 * How long did we wait in total for retries? 324 */ 325 struct GNUNET_TIME_Relative total_backoff; 326 327 /** 328 * Number of fresh coins withdrawn, set by the 329 * reveal callback as it comes from the exchange, 330 * it is the length of the @e fresh_coins array. 331 */ 332 unsigned int num_fresh_coins; 333 334 /** 335 * Expected HTTP response code. 336 */ 337 unsigned int expected_response_code; 338 339 /** 340 * How often should we retry on (transient) failures? 341 */ 342 unsigned int do_retry; 343 344 }; 345 346 347 /** 348 * State for a "refresh link" CMD. 349 */ 350 struct RefreshLinkState 351 { 352 /** 353 * Link to a "refresh reveal" command. 354 */ 355 const char *reveal_reference; 356 357 /** 358 * Our command. 359 */ 360 const struct TALER_TESTING_Command *cmd; 361 362 /** 363 * Handle to the ongoing operation. 364 */ 365 struct TALER_EXCHANGE_LinkHandle *rlh; 366 367 /** 368 * Interpreter state. 369 */ 370 struct TALER_TESTING_Interpreter *is; 371 372 /** 373 * Task scheduled to try later. 374 */ 375 struct GNUNET_SCHEDULER_Task *retry_task; 376 377 /** 378 * How long do we wait until we retry? 379 */ 380 struct GNUNET_TIME_Relative backoff; 381 382 /** 383 * How long did we wait in total for retries? 384 */ 385 struct GNUNET_TIME_Relative total_backoff; 386 387 /** 388 * Expected HTTP response code. 389 */ 390 unsigned int expected_response_code; 391 392 /** 393 * How often should we retry on (transient) failures? 394 */ 395 unsigned int do_retry; 396 397 }; 398 399 400 /** 401 * Run the command. 402 * 403 * @param cls closure. 404 * @param cmd the command to execute. 405 * @param is the interpreter state. 406 */ 407 static void 408 melt_reveal_run (void *cls, 409 const struct TALER_TESTING_Command *cmd, 410 struct TALER_TESTING_Interpreter *is); 411 412 413 /** 414 * Task scheduled to re-try #melt_reveal_run. 415 * 416 * @param cls a `struct RefreshRevealState` 417 */ 418 static void 419 do_reveal_retry (void *cls) 420 { 421 struct RevealMeltState *rrs = cls; 422 423 rrs->retry_task = NULL; 424 TALER_TESTING_touch_cmd (rrs->is); 425 melt_reveal_run (rrs, 426 NULL, 427 rrs->is); 428 } 429 430 431 /** 432 * "refresh reveal" request callback; it checks that the response 433 * code is expected and copies into its command's state the data 434 * coming from the exchange, namely the fresh coins. 435 * 436 * @param rrs closure, a `struct RevealMeltState` 437 * @param rmr HTTP response details 438 */ 439 static void 440 reveal_cb (struct RevealMeltState *rrs, 441 const struct TALER_EXCHANGE_PostRevealMeltResponse *rmr) 442 { 443 const struct TALER_EXCHANGE_HttpResponse *hr = &rmr->hr; 444 const struct TALER_TESTING_Command *melt_cmd; 445 446 rrs->rmh = NULL; 447 if (rrs->expected_response_code != hr->http_status) 448 { 449 if (0 != rrs->do_retry) 450 { 451 rrs->do_retry--; 452 if ( (0 == hr->http_status) || 453 (TALER_EC_GENERIC_DB_SOFT_FAILURE == hr->ec) || 454 (MHD_HTTP_INTERNAL_SERVER_ERROR == hr->http_status) ) 455 { 456 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 457 "Retrying refresh reveal failed with %u/%d\n", 458 hr->http_status, 459 (int) hr->ec); 460 /* on DB conflicts, do not use backoff */ 461 if (TALER_EC_GENERIC_DB_SOFT_FAILURE == hr->ec) 462 rrs->backoff = GNUNET_TIME_UNIT_ZERO; 463 else 464 rrs->backoff = GNUNET_TIME_randomized_backoff (rrs->backoff, 465 MAX_BACKOFF); 466 rrs->total_backoff = GNUNET_TIME_relative_add (rrs->total_backoff, 467 rrs->backoff); 468 TALER_TESTING_inc_tries (rrs->is); 469 rrs->retry_task = GNUNET_SCHEDULER_add_delayed (rrs->backoff, 470 &do_reveal_retry, 471 rrs); 472 return; 473 } 474 } 475 TALER_TESTING_unexpected_status_with_body (rrs->is, 476 hr->http_status, 477 rrs->expected_response_code, 478 hr->reply); 479 return; 480 } 481 melt_cmd = TALER_TESTING_interpreter_lookup_command (rrs->is, 482 rrs->melt_reference); 483 if (NULL == melt_cmd) 484 { 485 GNUNET_break (0); 486 TALER_TESTING_interpreter_fail (rrs->is); 487 return; 488 } 489 switch (hr->http_status) 490 { 491 case MHD_HTTP_OK: 492 rrs->num_fresh_coins = rmr->details.ok.num_coins; 493 rrs->psa = GNUNET_new_array (rrs->num_fresh_coins, 494 struct TALER_PlanchetMasterSecretP); 495 rrs->fresh_coins = GNUNET_new_array (rrs->num_fresh_coins, 496 struct TALER_TESTING_FreshCoinData); 497 for (unsigned int i = 0; i<rrs->num_fresh_coins; i++) 498 { 499 const struct TALER_EXCHANGE_RevealedCoinInfo *coin 500 = &rmr->details.ok.coins[i]; 501 struct TALER_TESTING_FreshCoinData *fc = &rrs->fresh_coins[i]; 502 503 rrs->psa[i] = coin->ps; 504 fc->blinding_key = coin->bks; 505 if (GNUNET_OK != 506 TALER_TESTING_get_trait_denom_pub (melt_cmd, 507 i, 508 &fc->pk)) 509 { 510 GNUNET_break (0); 511 TALER_TESTING_interpreter_fail (rrs->is); 512 return; 513 } 514 fc->coin_priv = coin->coin_priv; 515 GNUNET_CRYPTO_eddsa_key_get_public (&fc->coin_priv.eddsa_priv, 516 &fc->coin_pub.eddsa_pub); 517 518 if (NULL != coin->age_commitment_proof) 519 { 520 fc->age_commitment_proof = 521 TALER_age_commitment_proof_duplicate (coin->age_commitment_proof); 522 fc->h_age_commitment = coin->h_age_commitment; 523 } 524 525 TALER_denom_sig_copy (&fc->sig, 526 &coin->sig); 527 } 528 if (0 != rrs->total_backoff.rel_value_us) 529 { 530 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 531 "Total reveal backoff for %s was %s\n", 532 rrs->cmd->label, 533 GNUNET_STRINGS_relative_time_to_string (rrs->total_backoff, 534 true)); 535 } 536 break; 537 default: 538 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 539 "Unknown HTTP status %u/%d\n", 540 hr->http_status, 541 (int) hr->ec); 542 } 543 TALER_TESTING_interpreter_next (rrs->is); 544 } 545 546 547 /** 548 * Run the command. 549 * 550 * @param cls closure. 551 * @param cmd the command to execute. 552 * @param is the interpreter state. 553 */ 554 static void 555 melt_run (void *cls, 556 const struct TALER_TESTING_Command *cmd, 557 struct TALER_TESTING_Interpreter *is); 558 559 560 /** 561 * Run the command. 562 * 563 * @param cls closure. 564 * @param cmd the command to execute. 565 * @param is the interpreter state. 566 */ 567 static void 568 melt_reveal_run (void *cls, 569 const struct TALER_TESTING_Command *cmd, 570 struct TALER_TESTING_Interpreter *is) 571 { 572 struct RevealMeltState *rrs = cls; 573 struct MeltState *ms; 574 const struct TALER_TESTING_Command *melt_cmd; 575 576 rrs->cmd = cmd; 577 rrs->is = is; 578 melt_cmd = TALER_TESTING_interpreter_lookup_command (is, 579 rrs->melt_reference); 580 if (NULL == melt_cmd) 581 { 582 GNUNET_break (0); 583 TALER_TESTING_interpreter_fail (rrs->is); 584 return; 585 } 586 GNUNET_assert (melt_cmd->run == &melt_run); 587 ms = melt_cmd->cls; 588 ms->reveal_melt_input.rms = &ms->rms; 589 ms->reveal_melt_input.melt_input = &ms->melt_input; 590 ms->reveal_melt_input.blinding_seed = ms->no_blinding_seed 591 ? NULL 592 : &ms->blinding_seed; 593 ms->reveal_melt_input.num_blinding_values = ms->num_blinding_values; 594 ms->reveal_melt_input.blinding_values = ms->blinding_values; 595 ms->reveal_melt_input.noreveal_index = ms->noreveal_index; 596 ms->reveal_melt_input.rc_version = ms->rc_version; 597 rrs->rmh = TALER_EXCHANGE_post_reveal_melt_create ( 598 TALER_TESTING_interpreter_get_context (is), 599 TALER_TESTING_get_exchange_url (is), 600 &ms->reveal_melt_input); 601 if (NULL == rrs->rmh) 602 { 603 GNUNET_break (0); 604 TALER_TESTING_interpreter_fail (is); 605 return; 606 } 607 GNUNET_assert (TALER_EC_NONE == 608 TALER_EXCHANGE_post_reveal_melt_start (rrs->rmh, 609 &reveal_cb, 610 rrs)); 611 } 612 613 614 /** 615 * Free the state from a "refresh reveal" CMD, and possibly 616 * cancel a pending operation thereof. 617 * 618 * @param cls closure. 619 * @param cmd the command which is being cleaned up. 620 */ 621 static void 622 melt_reveal_cleanup (void *cls, 623 const struct TALER_TESTING_Command *cmd) 624 { 625 struct RevealMeltState *rrs = cls; 626 627 (void) cmd; 628 if (NULL != rrs->rmh) 629 { 630 TALER_TESTING_command_incomplete (rrs->is, 631 cmd->label); 632 TALER_EXCHANGE_post_reveal_melt_cancel (rrs->rmh); 633 rrs->rmh = NULL; 634 } 635 if (NULL != rrs->retry_task) 636 { 637 GNUNET_SCHEDULER_cancel (rrs->retry_task); 638 rrs->retry_task = NULL; 639 } 640 641 for (unsigned int j = 0; j < rrs->num_fresh_coins; j++) 642 { 643 TALER_denom_sig_free (&rrs->fresh_coins[j].sig); 644 TALER_age_commitment_proof_free (rrs->fresh_coins[j].age_commitment_proof); 645 GNUNET_free (rrs->fresh_coins[j].age_commitment_proof); 646 } 647 GNUNET_free (rrs->fresh_coins); 648 GNUNET_free (rrs->psa); 649 rrs->num_fresh_coins = 0; 650 GNUNET_free (rrs); 651 } 652 653 654 static void 655 melt_cb (struct MeltState *ms, 656 const struct TALER_EXCHANGE_PostMeltResponse *mr); 657 658 659 /** 660 * Create and start the POST /melt operation of @a ms. 661 * 662 * @param[in,out] ms melt command state, with @e melt_input set up 663 * @return #GNUNET_OK on success 664 */ 665 static enum GNUNET_GenericReturnValue 666 start_melt (struct MeltState *ms) 667 { 668 ms->mh = TALER_EXCHANGE_post_melt_create ( 669 TALER_TESTING_interpreter_get_context (ms->is), 670 TALER_TESTING_get_exchange_url (ms->is), 671 TALER_TESTING_get_keys (ms->is), 672 &ms->rms, 673 &ms->melt_input); 674 if (NULL == ms->mh) 675 return GNUNET_SYSERR; 676 GNUNET_assert (TALER_EC_NONE == 677 TALER_EXCHANGE_post_melt_start (ms->mh, 678 &melt_cb, 679 ms)); 680 return GNUNET_OK; 681 } 682 683 684 /** 685 * Resubmit the prepared melt. The first request may have committed 686 * even if its response never reached us. 687 */ 688 static void 689 do_melt_retry (void *cls) 690 { 691 struct MeltState *ms = cls; 692 693 ms->retry_task = NULL; 694 TALER_TESTING_touch_cmd (ms->is); 695 if (GNUNET_OK != start_melt (ms)) 696 TALER_TESTING_interpreter_fail (ms->is); 697 } 698 699 700 /** 701 * Callback for a " /melt" operation; checks if the HTTP 702 * response code is okay and re-run the melt operation if the 703 * CMD was set to do so. 704 * 705 * @param ms closure. 706 * @param mr melt response details 707 */ 708 static void 709 melt_cb (struct MeltState *ms, 710 const struct TALER_EXCHANGE_PostMeltResponse *mr) 711 { 712 const struct TALER_EXCHANGE_HttpResponse *hr = &mr->hr; 713 714 ms->mh = NULL; 715 if (ms->expected_response_code != hr->http_status) 716 { 717 if (0 != ms->do_retry) 718 { 719 ms->do_retry--; 720 if ( (0 == hr->http_status) || 721 (TALER_EC_GENERIC_DB_SOFT_FAILURE == hr->ec) || 722 (MHD_HTTP_INTERNAL_SERVER_ERROR == hr->http_status) ) 723 { 724 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 725 "Retrying refresh melt failed with %u/%d\n", 726 hr->http_status, 727 (int) hr->ec); 728 /* on DB conflicts, do not use backoff */ 729 if (TALER_EC_GENERIC_DB_SOFT_FAILURE == hr->ec) 730 ms->backoff = GNUNET_TIME_UNIT_ZERO; 731 else 732 ms->backoff = GNUNET_TIME_randomized_backoff (ms->backoff, 733 MAX_BACKOFF); 734 ms->total_backoff = GNUNET_TIME_relative_add (ms->total_backoff, 735 ms->backoff); 736 TALER_TESTING_inc_tries (ms->is); 737 ms->retry_task = GNUNET_SCHEDULER_add_delayed (ms->backoff, 738 &do_melt_retry, 739 ms); 740 return; 741 } 742 } 743 TALER_TESTING_unexpected_status_with_body (ms->is, 744 hr->http_status, 745 ms->expected_response_code, 746 hr->reply); 747 return; 748 } 749 if (MHD_HTTP_OK != hr->http_status) 750 { 751 /* the exchange rejected the operation, the coin is unaffected */ 752 ms->che.type = TALER_EXCHANGE_CTT_NONE; 753 } 754 if ( (MHD_HTTP_CONFLICT == hr->http_status) && 755 (TALER_EC_EXCHANGE_GENERIC_COIN_CONFLICTING_DENOMINATION_KEY == 756 hr->ec) ) 757 { 758 const struct TALER_EXCHANGE_CoinDenominationConflict *cdc 759 = &mr->details.conflict.details.denomination_conflict; 760 761 /* the library verified the exchange's proof; the denomination it 762 names must be one we know, and differ from the one we used */ 763 if ( (! cdc->verified) || 764 (0 == GNUNET_memcmp (&cdc->prev_h_denom_pub, 765 &ms->melt_input.melt_pk.h_key)) ) 766 { 767 GNUNET_break (0); 768 TALER_TESTING_interpreter_fail (ms->is); 769 return; 770 } 771 } 772 if (MHD_HTTP_OK == hr->http_status) 773 { 774 if ( (ms->noreveal_index < TALER_CNC_KAPPA) && 775 ( (ms->noreveal_index != mr->details.ok.noreveal_index) || 776 (0 != GNUNET_memcmp (&ms->rc, &mr->details.ok.rc)) ) ) 777 { 778 GNUNET_break (0); 779 TALER_TESTING_interpreter_fail (ms->is); 780 return; 781 } 782 ms->noreveal_index = mr->details.ok.noreveal_index; 783 ms->rc = mr->details.ok.rc; 784 /* for the coin history check */ 785 ms->che.details.melt.rc = ms->rc; 786 ms->che.details.melt.noreveal_index = ms->noreveal_index; 787 ms->rc_version = mr->details.ok.rc_version; 788 if (ms->rc_version != ms->melt_input.rc_version) 789 { 790 GNUNET_break (0); 791 TALER_TESTING_interpreter_fail (ms->is); 792 return; 793 } 794 if (mr->details.ok.num_melt_blinding_values != ms->num_fresh_coins) 795 { 796 GNUNET_break (0); 797 TALER_TESTING_interpreter_fail (ms->is); 798 return; 799 } 800 ms->no_blinding_seed = (NULL == mr->details.ok.blinding_seed); 801 if (NULL != mr->details.ok.blinding_seed) 802 ms->blinding_seed = *mr->details.ok.blinding_seed; 803 ms->num_blinding_values = mr->details.ok.num_melt_blinding_values; 804 if (NULL != ms->blinding_values) 805 { 806 for (unsigned int i = 0; i < ms->num_blinding_values; i++) 807 TALER_denom_ewv_free (&ms->blinding_values[i]); 808 GNUNET_free (ms->blinding_values); 809 } 810 ms->blinding_values = GNUNET_new_array ( 811 ms->num_blinding_values, 812 struct TALER_ExchangeBlindingValues); 813 for (unsigned int i = 0; i<ms->num_blinding_values; i++) 814 { 815 TALER_denom_ewv_copy (&ms->blinding_values[i], 816 &mr->details.ok.melt_blinding_values[i]); 817 } 818 } 819 if (0 != ms->total_backoff.rel_value_us) 820 { 821 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 822 "Total melt backoff for %s was %s\n", 823 ms->cmd->label, 824 GNUNET_STRINGS_relative_time_to_string (ms->total_backoff, 825 true)); 826 } 827 if (ms->double_melt) 828 { 829 TALER_LOG_DEBUG ("Doubling the melt (%s)\n", 830 ms->cmd->label); 831 ms->double_melt = false; 832 ms->retry_task = GNUNET_SCHEDULER_add_now (&do_melt_retry, ms); 833 return; 834 } 835 TALER_TESTING_interpreter_next (ms->is); 836 } 837 838 839 /** 840 * Run the command. 841 * 842 * @param cls closure. 843 * @param cmd the command to execute. 844 * @param is the interpreter state. 845 */ 846 static void 847 melt_run (void *cls, 848 const struct TALER_TESTING_Command *cmd, 849 struct TALER_TESTING_Interpreter *is) 850 { 851 static const char *default_melt_fresh_amounts[] = { 852 "EUR:1", "EUR:1", "EUR:1", "EUR:0.1", 853 NULL 854 }; 855 struct MeltState *rms = cls; 856 unsigned int num_fresh_coins; 857 const char **melt_fresh_amounts; 858 859 rms->cmd = cmd; 860 if (NULL == (melt_fresh_amounts = rms->melt_fresh_amounts)) 861 melt_fresh_amounts = default_melt_fresh_amounts; 862 rms->is = is; 863 rms->noreveal_index = UINT16_MAX; 864 TALER_refresh_master_setup_random (&rms->rms); 865 for (num_fresh_coins = 0; 866 NULL != melt_fresh_amounts[num_fresh_coins]; 867 num_fresh_coins++) 868 ; 869 rms->num_fresh_coins = num_fresh_coins; 870 /* The interpreter can run the command again after a rewind. */ 871 if (NULL != rms->fresh_pks) 872 { 873 for (unsigned int i = 0; i < rms->num_fresh_coins; i++) 874 TALER_denom_pub_free (&rms->fresh_pks[i].key); 875 GNUNET_free (rms->fresh_pks); 876 } 877 rms->fresh_pks = GNUNET_new_array ( 878 num_fresh_coins, 879 struct TALER_EXCHANGE_DenomPublicKey); 880 { 881 struct TALER_Amount melt_amount; 882 struct TALER_Amount fresh_amount; 883 const struct TALER_AgeCommitmentProof *age_commitment_proof = NULL; 884 const struct TALER_AgeCommitmentHashP *h_age_commitment = NULL; 885 const struct TALER_DenominationSignature *melt_sig; 886 const struct TALER_EXCHANGE_DenomPublicKey *melt_denom_pub; 887 const struct TALER_TESTING_Command *coin_command; 888 bool age_restricted_denom; 889 890 if (NULL == (coin_command 891 = TALER_TESTING_interpreter_lookup_command ( 892 is, 893 rms->coin_reference))) 894 { 895 GNUNET_break (0); 896 TALER_TESTING_interpreter_fail (rms->is); 897 return; 898 } 899 900 if (GNUNET_OK != 901 TALER_TESTING_get_trait_coin_priv (coin_command, 902 0, 903 &rms->melt_priv)) 904 { 905 GNUNET_break (0); 906 TALER_TESTING_interpreter_fail (rms->is); 907 return; 908 } 909 if (GNUNET_OK != 910 TALER_TESTING_get_trait_age_commitment_proof (coin_command, 911 0, 912 &age_commitment_proof)) 913 { 914 GNUNET_break (0); 915 TALER_TESTING_interpreter_fail (rms->is); 916 return; 917 } 918 919 if (GNUNET_OK != 920 TALER_TESTING_get_trait_h_age_commitment (coin_command, 921 0, 922 &h_age_commitment)) 923 { 924 GNUNET_break (0); 925 TALER_TESTING_interpreter_fail (rms->is); 926 return; 927 } 928 if (GNUNET_OK != 929 TALER_TESTING_get_trait_denom_sig (coin_command, 930 0, 931 &melt_sig)) 932 { 933 GNUNET_break (0); 934 TALER_TESTING_interpreter_fail (rms->is); 935 return; 936 } 937 if (GNUNET_OK != 938 TALER_TESTING_get_trait_denom_pub (coin_command, 939 0, 940 &melt_denom_pub)) 941 { 942 GNUNET_break (0); 943 TALER_TESTING_interpreter_fail (rms->is); 944 return; 945 } 946 947 /* Melt amount starts with the melt fee of the old coin; we'll add the 948 values and withdraw fees of the fresh coins next */ 949 melt_amount = melt_denom_pub->fees.refresh; 950 age_restricted_denom = melt_denom_pub->key.age_mask.bits != 0; 951 GNUNET_assert (age_restricted_denom == (NULL != age_commitment_proof)); 952 GNUNET_assert ((NULL == age_commitment_proof) || 953 (0 < age_commitment_proof->commitment.num)); 954 for (unsigned int i = 0; i<num_fresh_coins; i++) 955 { 956 const struct TALER_EXCHANGE_DenomPublicKey *fresh_pk; 957 958 if (GNUNET_OK != 959 TALER_string_to_amount (melt_fresh_amounts[i], 960 &fresh_amount)) 961 { 962 GNUNET_break (0); 963 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 964 "Failed to parse amount `%s' at index %u\n", 965 melt_fresh_amounts[i], 966 i); 967 TALER_TESTING_interpreter_fail (rms->is); 968 return; 969 } 970 fresh_pk = TALER_TESTING_find_pk (TALER_TESTING_get_keys (rms->is), 971 &fresh_amount, 972 age_restricted_denom); 973 if (NULL == fresh_pk) 974 { 975 GNUNET_break (0); 976 /* Subroutine logs specific error */ 977 TALER_TESTING_interpreter_fail (rms->is); 978 return; 979 } 980 GNUNET_assert (0 <= 981 TALER_amount_add (&melt_amount, 982 &melt_amount, 983 &fresh_amount)); 984 GNUNET_assert (0 <= 985 TALER_amount_add (&melt_amount, 986 &melt_amount, 987 &fresh_pk->fees.withdraw)); 988 rms->fresh_pks[i] = *fresh_pk; 989 /* Make a deep copy of the RSA key */ 990 TALER_denom_pub_copy (&rms->fresh_pks[i].key, 991 &fresh_pk->key); 992 } /* end for */ 993 994 rms->melt_input.melt_priv = *rms->melt_priv; 995 GNUNET_CRYPTO_eddsa_key_get_public (&rms->melt_priv->eddsa_priv, 996 &rms->melt_pub.eddsa_pub); 997 rms->melt_input.melt_amount = melt_amount; 998 rms->melt_input.melt_sig = *melt_sig; 999 rms->melt_input.melt_pk = *melt_denom_pub; 1000 1001 if (NULL != age_commitment_proof) 1002 { 1003 GNUNET_assert (NULL != h_age_commitment); 1004 rms->melt_input.melt_age_commitment_proof = age_commitment_proof; 1005 rms->melt_input.melt_h_age_commitment = h_age_commitment; 1006 } 1007 rms->melt_input.fresh_denom_pubs = rms->fresh_pks; 1008 rms->melt_input.num_fresh_denom_pubs = num_fresh_coins; 1009 /* chosen once, so that retries keep the same commitment */ 1010 rms->melt_input.rc_version 1011 = rms->legacy_commitment 1012 ? TALER_REFRESH_COMMITMENT_VERSION_LEGACY_VDOLDPLUS_BROKEN 1013 : TALER_EXCHANGE_refresh_commitment_version ( 1014 TALER_TESTING_get_keys (is)); 1015 1016 GNUNET_assert (age_restricted_denom == 1017 (NULL != age_commitment_proof)); 1018 GNUNET_assert ((NULL == age_commitment_proof) || 1019 (0 < age_commitment_proof->commitment.num)); 1020 1021 rms->che.type = TALER_EXCHANGE_CTT_MELT; 1022 rms->che.amount = melt_amount; 1023 if (NULL != age_commitment_proof) 1024 rms->che.details.melt.h_age_commitment = *h_age_commitment; 1025 else 1026 rms->che.details.melt.no_hac = true; 1027 1028 if (GNUNET_OK != start_melt (rms)) 1029 { 1030 GNUNET_break (0); 1031 TALER_TESTING_interpreter_fail (rms->is); 1032 return; 1033 } 1034 } 1035 } 1036 1037 1038 /** 1039 * Free the "refresh melt" CMD state, and possibly cancel a 1040 * pending operation thereof. 1041 * 1042 * @param cls closure, must be a `struct RefreshMeltState`. 1043 * @param cmd the command which is being cleaned up. 1044 */ 1045 static void 1046 melt_cleanup (void *cls, 1047 const struct TALER_TESTING_Command *cmd) 1048 { 1049 struct MeltState *rms = cls; 1050 1051 (void) cmd; 1052 if (NULL != rms->mh) 1053 { 1054 TALER_TESTING_command_incomplete (rms->is, 1055 cmd->label); 1056 TALER_EXCHANGE_post_melt_cancel (rms->mh); 1057 rms->mh = NULL; 1058 } 1059 if (NULL != rms->retry_task) 1060 { 1061 GNUNET_SCHEDULER_cancel (rms->retry_task); 1062 rms->retry_task = NULL; 1063 } 1064 if (NULL != rms->fresh_pks) 1065 { 1066 for (unsigned int i = 0; i < rms->num_fresh_coins; i++) 1067 TALER_denom_pub_free (&rms->fresh_pks[i].key); 1068 GNUNET_free (rms->fresh_pks); 1069 } 1070 if (NULL != rms->blinding_values) 1071 { 1072 for (unsigned int i = 0; i < rms->num_blinding_values; i++) 1073 TALER_denom_ewv_free (&rms->blinding_values[i]); 1074 GNUNET_free (rms->blinding_values); 1075 } 1076 GNUNET_free (rms->melt_fresh_amounts); 1077 GNUNET_free (rms); 1078 } 1079 1080 1081 /** 1082 * Offer internal data to the "refresh melt" CMD. 1083 * 1084 * @param cls closure. 1085 * @param[out] ret result (could be anything). 1086 * @param trait name of the trait. 1087 * @param index index number of the object to offer. 1088 * @return #GNUNET_OK on success. 1089 */ 1090 static enum GNUNET_GenericReturnValue 1091 melt_traits (void *cls, 1092 const void **ret, 1093 const char *trait, 1094 unsigned int index) 1095 { 1096 struct MeltState *rms = cls; 1097 1098 if (index >= rms->num_fresh_coins) 1099 { 1100 GNUNET_break (0); 1101 return GNUNET_SYSERR; 1102 } 1103 { 1104 struct TALER_TESTING_Trait traits[] = { 1105 TALER_TESTING_make_trait_denom_pub (index, 1106 &rms->fresh_pks[index]), 1107 TALER_TESTING_make_trait_coin_priv (0, 1108 rms->melt_priv), 1109 TALER_TESTING_make_trait_coin_pub (0, 1110 &rms->melt_pub), 1111 TALER_TESTING_make_trait_coin_history (0, 1112 &rms->che), 1113 TALER_TESTING_make_trait_age_commitment_proof ( 1114 index, 1115 rms->melt_input.melt_age_commitment_proof), 1116 TALER_TESTING_make_trait_h_age_commitment ( 1117 index, 1118 rms->melt_input.melt_h_age_commitment), 1119 TALER_TESTING_make_trait_refresh_seed (&rms->rms), 1120 TALER_TESTING_make_trait_refresh_commitment (&rms->rc), 1121 TALER_TESTING_make_trait_blinding_seed (rms->no_blinding_seed 1122 ? NULL 1123 : &rms->blinding_seed), 1124 (NULL != rms->reveal_melt_input.blinding_values) 1125 ? TALER_TESTING_make_trait_exchange_blinding_values ( 1126 index, 1127 &rms->reveal_melt_input.blinding_values[index]) 1128 : TALER_TESTING_trait_end (), 1129 TALER_TESTING_trait_end () 1130 }; 1131 1132 return TALER_TESTING_get_trait (traits, 1133 ret, 1134 trait, 1135 index); 1136 } 1137 } 1138 1139 1140 /** 1141 * Parse list of amounts for melt operation. 1142 * 1143 * @param[in,out] rms where to store the list 1144 * @param ap NULL-termianted list of amounts to be melted (one per fresh coin) 1145 * @return #GNUNET_OK on success 1146 */ 1147 static enum GNUNET_GenericReturnValue 1148 parse_amounts (struct MeltState *rms, 1149 va_list ap) 1150 { 1151 unsigned int len; 1152 unsigned int off; 1153 const char *amount; 1154 1155 len = 0; 1156 off = 0; 1157 while (NULL != (amount = va_arg (ap, const char *))) 1158 { 1159 if (len == off) 1160 { 1161 struct TALER_Amount a; 1162 1163 GNUNET_array_grow (rms->melt_fresh_amounts, 1164 len, 1165 off + 16); 1166 if (GNUNET_OK != 1167 TALER_string_to_amount (amount, &a)) 1168 { 1169 GNUNET_break (0); 1170 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 1171 "Failed to parse amount `%s' at index %u\n", 1172 amount, off); 1173 GNUNET_free (rms->melt_fresh_amounts); 1174 rms->melt_fresh_amounts = NULL; 1175 return GNUNET_SYSERR; 1176 } 1177 rms->melt_fresh_amounts[off++] = amount; 1178 } 1179 } 1180 if (0 == off) 1181 return GNUNET_OK; /* no amounts given == use defaults! */ 1182 /* ensure NULL-termination */ 1183 GNUNET_array_grow (rms->melt_fresh_amounts, 1184 len, 1185 off + 1); 1186 return GNUNET_OK; 1187 } 1188 1189 1190 struct TALER_TESTING_Command 1191 TALER_TESTING_cmd_melt (const char *label, 1192 const char *coin_reference, 1193 unsigned int expected_response_code, 1194 ...) 1195 { 1196 struct MeltState *rms; 1197 va_list ap; 1198 1199 rms = GNUNET_new (struct MeltState); 1200 rms->coin_reference = coin_reference; 1201 rms->expected_response_code = expected_response_code; 1202 va_start (ap, 1203 expected_response_code); 1204 GNUNET_assert (GNUNET_OK == 1205 parse_amounts (rms, ap)); 1206 va_end (ap); 1207 { 1208 struct TALER_TESTING_Command cmd = { 1209 .label = label, 1210 .cls = rms, 1211 .run = &melt_run, 1212 .cleanup = &melt_cleanup, 1213 .traits = &melt_traits 1214 }; 1215 1216 return cmd; 1217 } 1218 } 1219 1220 1221 struct TALER_TESTING_Command 1222 TALER_TESTING_cmd_melt_double (const char *label, 1223 const char *coin_reference, 1224 unsigned int expected_response_code, 1225 ...) 1226 { 1227 struct MeltState *rms; 1228 va_list ap; 1229 1230 rms = GNUNET_new (struct MeltState); 1231 rms->coin_reference = coin_reference; 1232 rms->expected_response_code = expected_response_code; 1233 rms->double_melt = true; 1234 va_start (ap, 1235 expected_response_code); 1236 GNUNET_assert (GNUNET_OK == 1237 parse_amounts (rms, ap)); 1238 va_end (ap); 1239 { 1240 struct TALER_TESTING_Command cmd = { 1241 .label = label, 1242 .cls = rms, 1243 .run = &melt_run, 1244 .cleanup = &melt_cleanup, 1245 .traits = &melt_traits 1246 }; 1247 1248 return cmd; 1249 } 1250 } 1251 1252 1253 struct TALER_TESTING_Command 1254 TALER_TESTING_cmd_melt_with_retry (struct TALER_TESTING_Command cmd) 1255 { 1256 struct MeltState *rms; 1257 1258 GNUNET_assert (&melt_run == cmd.run); 1259 rms = cmd.cls; 1260 rms->do_retry = NUM_RETRIES; 1261 return cmd; 1262 } 1263 1264 1265 struct TALER_TESTING_Command 1266 TALER_TESTING_cmd_melt_with_legacy_commitment ( 1267 struct TALER_TESTING_Command cmd) 1268 { 1269 struct MeltState *rms; 1270 1271 GNUNET_assert (&melt_run == cmd.run); 1272 rms = cmd.cls; 1273 rms->legacy_commitment = true; 1274 return cmd; 1275 } 1276 1277 1278 /** 1279 * Offer internal data from a "refresh reveal" CMD. 1280 * 1281 * @param cls closure. 1282 * @param[out] ret result (could be anything). 1283 * @param trait name of the trait. 1284 * @param index index number of the object to offer. 1285 * @return #GNUNET_OK on success. 1286 */ 1287 static enum GNUNET_GenericReturnValue 1288 melt_reveal_traits (void *cls, 1289 const void **ret, 1290 const char *trait, 1291 unsigned int index) 1292 { 1293 struct RevealMeltState *rrs = cls; 1294 1295 if (index >= rrs->num_fresh_coins) 1296 return GNUNET_SYSERR; 1297 1298 { 1299 struct TALER_TESTING_Trait traits[] = { 1300 TALER_TESTING_make_trait_coin_priv ( 1301 index, 1302 &rrs->fresh_coins[index].coin_priv), 1303 TALER_TESTING_make_trait_coin_pub ( 1304 index, 1305 &rrs->fresh_coins[index].coin_pub), 1306 TALER_TESTING_make_trait_age_commitment_proof ( 1307 index, 1308 rrs->fresh_coins[index].age_commitment_proof), 1309 TALER_TESTING_make_trait_h_age_commitment ( 1310 index, 1311 &rrs->fresh_coins[index].h_age_commitment), 1312 TALER_TESTING_make_trait_denom_pub ( 1313 index, 1314 rrs->fresh_coins[index].pk), 1315 TALER_TESTING_make_trait_denom_sig ( 1316 index, 1317 &rrs->fresh_coins[index].sig), 1318 TALER_TESTING_make_trait_blinding_key ( 1319 index, 1320 &rrs->fresh_coins[index].blinding_key), 1321 TALER_TESTING_make_trait_array_length ( 1322 &rrs->num_fresh_coins), 1323 TALER_TESTING_make_trait_fresh_coins ( 1324 (const struct TALER_TESTING_FreshCoinData **) &rrs->fresh_coins), 1325 TALER_TESTING_make_trait_planchet_secrets (index, 1326 &rrs->psa[index]), 1327 TALER_TESTING_trait_end () 1328 }; 1329 1330 return TALER_TESTING_get_trait (traits, 1331 ret, 1332 trait, 1333 index); 1334 } 1335 } 1336 1337 1338 struct TALER_TESTING_Command 1339 TALER_TESTING_cmd_melt_reveal (const char *label, 1340 const char *melt_reference, 1341 unsigned int expected_response_code) 1342 { 1343 struct RevealMeltState *rrs; 1344 1345 rrs = GNUNET_new (struct RevealMeltState); 1346 rrs->melt_reference = melt_reference; 1347 rrs->expected_response_code = expected_response_code; 1348 { 1349 struct TALER_TESTING_Command cmd = { 1350 .cls = rrs, 1351 .label = label, 1352 .run = &melt_reveal_run, 1353 .cleanup = &melt_reveal_cleanup, 1354 .traits = &melt_reveal_traits 1355 }; 1356 1357 return cmd; 1358 } 1359 } 1360 1361 1362 struct TALER_TESTING_Command 1363 TALER_TESTING_cmd_melt_reveal_with_retry (struct TALER_TESTING_Command cmd) 1364 { 1365 struct RevealMeltState *rrs; 1366 1367 GNUNET_assert (&melt_reveal_run == cmd.run); 1368 rrs = cmd.cls; 1369 rrs->do_retry = NUM_RETRIES; 1370 return cmd; 1371 }