taler-helper-auditor-purses.c (57712B)
1 /* 2 This file is part of TALER 3 Copyright (C) 2016-2024 Taler Systems SA 4 5 TALER is free software; you can redistribute it and/or modify it under the 6 terms of the GNU Affero Public License as published by the Free Software 7 Foundation; either version 3, or (at your option) any later version. 8 9 TALER is distributed in the hope that it will be useful, but WITHOUT ANY 10 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR 11 A PARTICULAR PURPOSE. See the GNU Affero Public License for more details. 12 13 You should have received a copy of the GNU Affero Public License along with 14 TALER; see the file COPYING. If not, see <http://www.gnu.org/licenses/> 15 */ 16 /** 17 * @file auditor/taler-helper-auditor-purses.c 18 * @brief audits the purses of an exchange database 19 * @author Christian Grothoff 20 */ 21 #include "platform.h" 22 #include <gnunet/gnunet_util_lib.h> 23 #include "auditordb_lib.h" 24 #include "exchangedb_lib.h" 25 #include "taler/taler_bank_service.h" 26 #include "taler/taler_signatures.h" 27 #include "report-lib.h" 28 #include "taler/taler_dbevents.h" 29 #include "auditor-database/delete_purse_info.h" 30 #include "auditor-database/event_listen.h" 31 #include "auditor-database/get_auditor_progress.h" 32 #include "auditor-database/get_balance.h" 33 #include "auditor-database/get_purse_info.h" 34 #include "auditor-database/insert_amount_arithmetic_inconsistency.h" 35 #include "auditor-database/insert_auditor_progress.h" 36 #include "auditor-database/insert_bad_sig_losses.h" 37 #include "auditor-database/insert_balance.h" 38 #include "auditor-database/insert_purse_info.h" 39 #include "auditor-database/insert_purse_not_closed_inconsistencies.h" 40 #include "auditor-database/insert_row_inconsistency.h" 41 struct PurseContext; 42 #define TALER_AUDITORDB_EXPIRED_PURSE_RESULT_CLOSURE struct PurseContext 43 #include "auditor-database/iterate_purse_expired.h" 44 #include "auditor-database/update_purse_info.h" 45 #include "exchange-database/get_global_fee.h" 46 struct PurseContext; 47 #define TALER_EXCHANGEDB_ACCOUNT_MERGE_RESULT_CLOSURE struct PurseContext 48 #define TALER_EXCHANGEDB_ALL_PURSE_DECISION_RESULT_CLOSURE struct PurseContext 49 #define TALER_EXCHANGEDB_ALL_PURSE_DELETIONS_RESULT_CLOSURE struct PurseContext 50 #define TALER_EXCHANGEDB_PURSE_DEPOSIT_RESULT_CLOSURE struct PurseContext 51 #define TALER_EXCHANGEDB_PURSE_MERGE_RESULT_CLOSURE struct PurseContext 52 #define TALER_EXCHANGEDB_PURSE_REQUEST_RESULT_CLOSURE struct PurseContext 53 #include "exchange-database/iterate_account_merges_above_serial_id.h" 54 #include "exchange-database/iterate_all_purse_decisions_above_serial_id.h" 55 #include "exchange-database/iterate_all_purse_deletions_above_serial_id.h" 56 #include "exchange-database/get_purse.h" 57 #include "exchange-database/iterate_purse_deposits_above_serial_id.h" 58 #include "exchange-database/iterate_purse_merges_above_serial_id.h" 59 #include "exchange-database/iterate_purse_requests_above_serial_id.h" 60 61 62 /** 63 * Use a 1 day grace period to deal with clocks not being perfectly synchronized. 64 */ 65 #define EXPIRATION_GRACE_PERIOD GNUNET_TIME_UNIT_DAYS 66 67 /** 68 * Return value from main(). 69 */ 70 static int global_ret; 71 72 /** 73 * Run in test mode. Exit when idle instead of 74 * going to sleep and waiting for more work. 75 */ 76 static int test_mode; 77 78 /** 79 * Checkpointing our progress for purses. 80 */ 81 static TALER_ARL_DEF_PP (purse_account_merge_serial_id); 82 static TALER_ARL_DEF_PP (purse_decision_serial_id); 83 static TALER_ARL_DEF_PP (purse_deletion_serial_id); 84 static TALER_ARL_DEF_PP (purse_deposits_serial_id); 85 static TALER_ARL_DEF_PP (purse_merges_serial_id); 86 static TALER_ARL_DEF_PP (purse_request_serial_id); 87 static TALER_ARL_DEF_PP (purse_open_counter); 88 static TALER_ARL_DEF_AB (purse_global_balance); 89 90 /** 91 * Total amount purses were merged with insufficient balance. 92 */ 93 static TALER_ARL_DEF_AB (purse_total_balance_insufficient_loss); 94 95 /** 96 * Total amount purse decisions are delayed past deadline. 97 */ 98 static TALER_ARL_DEF_AB (purse_total_delayed_decisions); 99 100 /** 101 * Total amount affected by purses not having been closed on time. 102 */ 103 static TALER_ARL_DEF_AB (purse_total_balance_purse_not_closed); 104 105 /** 106 * Profits the exchange made by bad amount calculations. 107 */ 108 static TALER_ARL_DEF_AB (purse_total_arithmetic_delta_plus); 109 110 /** 111 * Losses the exchange made by bad amount calculations. 112 */ 113 static TALER_ARL_DEF_AB (purse_total_arithmetic_delta_minus); 114 115 /** 116 * Total amount lost by operations for which signatures were invalid. 117 */ 118 static TALER_ARL_DEF_AB (purse_total_bad_sig_loss); 119 120 /** 121 * Should we run checks that only work for exchange-internal audits? 122 */ 123 static int internal_checks; 124 125 static struct GNUNET_DB_EventHandler *eh; 126 127 /** 128 * The auditors's configuration. 129 */ 130 static const struct GNUNET_CONFIGURATION_Handle *cfg; 131 132 /* ***************************** Report logic **************************** */ 133 134 135 /** 136 * Report a (serious) inconsistency in the exchange's database with 137 * respect to calculations involving amounts. 138 * 139 * @param operation what operation had the inconsistency 140 * @param rowid affected row, 0 if row is missing 141 * @param exchange amount calculated by exchange 142 * @param auditor amount calculated by auditor 143 * @param profitable 1 if @a exchange being larger than @a auditor is 144 * profitable for the exchange for this operation, 145 * -1 if @a exchange being smaller than @a auditor is 146 * profitable for the exchange, and 0 if it is unclear 147 * @return transaction status 148 */ 149 static enum GNUNET_DB_QueryStatus 150 report_amount_arithmetic_inconsistency ( 151 const char *operation, 152 uint64_t rowid, 153 const struct TALER_Amount *exchange, 154 const struct TALER_Amount *auditor, 155 int profitable) 156 { 157 struct TALER_Amount delta; 158 struct TALER_Amount *target; 159 enum GNUNET_DB_QueryStatus qs; 160 161 if (0 < TALER_amount_cmp (exchange, 162 auditor)) 163 { 164 /* exchange > auditor */ 165 TALER_ARL_amount_subtract (&delta, 166 exchange, 167 auditor); 168 } 169 else 170 { 171 /* exchange <= auditor */ 172 profitable = -profitable; 173 TALER_ARL_amount_subtract (&delta, 174 auditor, 175 exchange); 176 } 177 178 { 179 struct TALER_AUDITORDB_AmountArithmeticInconsistency aai = { 180 .profitable = profitable, 181 .problem_row_id = rowid, 182 .operation = (char *) operation, 183 .exchange_amount = *exchange, 184 .auditor_amount = *auditor 185 }; 186 187 qs = TALER_AUDITORDB_insert_amount_arithmetic_inconsistency ( 188 TALER_ARL_adb, 189 &aai); 190 191 if (qs < 0) 192 { 193 GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs); 194 return qs; 195 } 196 } 197 198 if (0 != profitable) 199 { 200 target = (1 == profitable) 201 ? &TALER_ARL_USE_AB (purse_total_arithmetic_delta_plus) 202 : &TALER_ARL_USE_AB (purse_total_arithmetic_delta_minus); 203 TALER_ARL_amount_add (target, 204 target, 205 &delta); 206 } 207 return qs; 208 } 209 210 211 /** 212 * Report a (serious) inconsistency in the exchange's database. 213 * 214 * @param table affected table 215 * @param rowid affected row, 0 if row is missing 216 * @param diagnostic message explaining the problem 217 * @return transaction status 218 */ 219 static enum GNUNET_DB_QueryStatus 220 report_row_inconsistency (const char *table, 221 uint64_t rowid, 222 const char *diagnostic) 223 { 224 enum GNUNET_DB_QueryStatus qs; 225 struct TALER_AUDITORDB_RowInconsistency ri = { 226 .diagnostic = (char *) diagnostic, 227 .row_table = (char *) table, 228 .problem_row_id = rowid 229 }; 230 231 qs = TALER_AUDITORDB_insert_row_inconsistency ( 232 TALER_ARL_adb, 233 &ri); 234 235 if (qs < 0) 236 { 237 GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs); 238 return qs; 239 } 240 return qs; 241 } 242 243 244 /** 245 * Obtain the purse fee for a purse created at @a time. 246 * 247 * @param atime when was the purse created 248 * @param[out] fee set to the purse fee 249 * @return #GNUNET_DB_STATUS_SUCCESS_ONE_RESULT on success 250 */ 251 static enum GNUNET_DB_QueryStatus 252 get_purse_fee (struct GNUNET_TIME_Timestamp atime, 253 struct TALER_Amount *fee) 254 { 255 enum GNUNET_DB_QueryStatus qs; 256 struct TALER_MasterSignatureP master_sig; 257 struct GNUNET_TIME_Timestamp start_date; 258 struct GNUNET_TIME_Timestamp end_date; 259 struct TALER_GlobalFeeSet fees; 260 struct GNUNET_TIME_Relative ptimeout; 261 struct GNUNET_TIME_Relative hexp; 262 uint32_t pacl; 263 264 qs = TALER_EXCHANGEDB_get_global_fee (TALER_ARL_edb, 265 atime, 266 &start_date, 267 &end_date, 268 &fees, 269 &ptimeout, 270 &hexp, 271 &pacl, 272 &master_sig); 273 if (0 > qs) 274 { 275 GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs); 276 return qs; 277 } 278 if (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS == qs) 279 { 280 char *diag; 281 282 GNUNET_asprintf (&diag, 283 "purse fee unavailable at %s\n", 284 GNUNET_TIME_timestamp2s (atime)); 285 qs = report_row_inconsistency ("purse-fee", 286 atime.abs_time.abs_value_us, 287 diag); 288 GNUNET_free (diag); 289 if (0 > qs) 290 { 291 GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs); 292 return qs; 293 } 294 return GNUNET_DB_STATUS_SUCCESS_NO_RESULTS; 295 } 296 *fee = fees.purse; 297 return GNUNET_DB_STATUS_SUCCESS_ONE_RESULT; 298 } 299 300 301 /* ***************************** Analyze purses ************************ */ 302 /* This logic checks the purses_requests, purse_deposits, 303 purse_refunds, purse_merges and account_merges */ 304 305 /** 306 * Summary data we keep per purse. 307 */ 308 struct PurseSummary 309 { 310 /** 311 * Public key of the purse. 312 * Always set when the struct is first initialized. 313 */ 314 struct TALER_PurseContractPublicKeyP purse_pub; 315 316 /** 317 * Balance of the purse from deposits (includes purse fee, excludes deposit 318 * fees), as calculated by auditor. 319 */ 320 struct TALER_Amount balance; 321 322 /** 323 * Expected value of the purse, excludes purse fee. 324 */ 325 struct TALER_Amount total_value; 326 327 /** 328 * Purse balance according to exchange DB. 329 */ 330 struct TALER_Amount exchange_balance; 331 332 /** 333 * Contract terms of the purse. 334 */ 335 struct TALER_PrivateContractHashP h_contract_terms; 336 337 /** 338 * Merge timestamp (as per exchange DB). 339 */ 340 struct GNUNET_TIME_Timestamp merge_timestamp; 341 342 /** 343 * Purse creation date. This is when the merge 344 * fee is applied. 345 */ 346 struct GNUNET_TIME_Timestamp creation_date; 347 348 /** 349 * Purse expiration date. 350 */ 351 struct GNUNET_TIME_Timestamp expiration_date; 352 353 /** 354 * Did we have a previous purse info? Used to decide between UPDATE and 355 * INSERT later. Initialized in #load_auditor_purse_summary(). 356 */ 357 bool had_pi; 358 359 /** 360 * Was the purse deleted? Note: as this is set via an UPDATE, it 361 * may be false at the auditor even if the purse was deleted. Thus, 362 * this value is only meaningful for *internal* checks. 363 */ 364 bool purse_deleted; 365 366 /** 367 * Was the purse refunded? Note: as this is set via an UPDATE, it 368 * may be false at the auditor even if the purse was deleted. Thus, 369 * this value is only meaningful for *internal* checks. 370 */ 371 bool purse_refunded; 372 373 }; 374 375 376 /** 377 * Load the auditor's remembered state about the purse into @a ps. 378 * 379 * @param[in,out] ps purse summary to (fully) initialize 380 * @return transaction status code 381 */ 382 static enum GNUNET_DB_QueryStatus 383 load_auditor_purse_summary (struct PurseSummary *ps) 384 { 385 enum GNUNET_DB_QueryStatus qs; 386 uint64_t rowid; 387 388 qs = TALER_AUDITORDB_get_purse_info (TALER_ARL_adb, 389 &ps->purse_pub, 390 &rowid, 391 &ps->balance, 392 &ps->expiration_date); 393 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 394 "Loaded purse `%s' info (%d)\n", 395 TALER_B2S (&ps->purse_pub), 396 (int) qs); 397 if (0 > qs) 398 { 399 GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs); 400 return qs; 401 } 402 if (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS == qs) 403 { 404 ps->had_pi = false; 405 GNUNET_assert (GNUNET_OK == 406 TALER_amount_set_zero (TALER_ARL_currency, 407 &ps->balance)); 408 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 409 "Creating fresh purse `%s'\n", 410 TALER_B2S (&ps->purse_pub)); 411 return GNUNET_DB_STATUS_SUCCESS_NO_RESULTS; 412 } 413 ps->had_pi = true; 414 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 415 "Auditor remembers purse `%s' has balance %s\n", 416 TALER_B2S (&ps->purse_pub), 417 TALER_amount2s (&ps->balance)); 418 return GNUNET_DB_STATUS_SUCCESS_ONE_RESULT; 419 } 420 421 422 /** 423 * Closure to the various callbacks we make while checking a purse. 424 */ 425 struct PurseContext 426 { 427 /** 428 * Map from hash of purse's public key to a `struct PurseSummary`. 429 */ 430 struct GNUNET_CONTAINER_MultiHashMap *purses; 431 432 /** 433 * Transaction status code, set to error codes if applicable. 434 */ 435 enum GNUNET_DB_QueryStatus qs; 436 437 }; 438 439 440 /** 441 * Create a new purse for @a purse_pub in @a pc. 442 * 443 * @param[in,out] pc context to update 444 * @param purse_pub key for which to create a purse 445 * @return NULL on error 446 */ 447 static struct PurseSummary * 448 setup_purse (struct PurseContext *pc, 449 const struct TALER_PurseContractPublicKeyP *purse_pub) 450 { 451 struct PurseSummary *ps; 452 struct GNUNET_HashCode key; 453 enum GNUNET_DB_QueryStatus qs; 454 455 GNUNET_CRYPTO_hash (purse_pub, 456 sizeof (*purse_pub), 457 &key); 458 ps = GNUNET_CONTAINER_multihashmap_get (pc->purses, 459 &key); 460 if (NULL != ps) 461 { 462 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 463 "Found purse `%s' summary in cache\n", 464 TALER_B2S (&ps->purse_pub)); 465 return ps; 466 } 467 ps = GNUNET_new (struct PurseSummary); 468 ps->purse_pub = *purse_pub; 469 GNUNET_assert (GNUNET_OK == 470 TALER_amount_set_zero (TALER_ARL_currency, 471 &ps->balance)); 472 /* get purse meta-data from exchange DB */ 473 qs = TALER_EXCHANGEDB_get_purse (TALER_ARL_edb, 474 purse_pub, 475 &ps->creation_date, 476 &ps->expiration_date, 477 &ps->total_value, 478 &ps->exchange_balance, 479 &ps->h_contract_terms, 480 &ps->merge_timestamp, 481 &ps->purse_deleted, 482 &ps->purse_refunded); 483 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 484 "Loaded purse `%s' meta-data (%d)\n", 485 TALER_B2S (purse_pub), 486 (int) qs); 487 if (0 >= qs) 488 { 489 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 490 "Failed to load meta-data of purse `%s'\n", 491 TALER_B2S (&ps->purse_pub)); 492 GNUNET_free (ps); 493 pc->qs = qs; 494 return NULL; 495 } 496 GNUNET_assert (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT == qs); 497 qs = load_auditor_purse_summary (ps); 498 if (0 > qs) 499 { 500 GNUNET_free (ps); 501 pc->qs = qs; 502 return NULL; 503 } 504 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 505 "Starting purse `%s' analysis\n", 506 TALER_B2S (purse_pub)); 507 GNUNET_assert (GNUNET_OK == 508 GNUNET_CONTAINER_multihashmap_put (pc->purses, 509 &key, 510 ps, 511 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY) 512 ); 513 return ps; 514 } 515 516 517 /** 518 * Function called on purse requests. 519 * 520 * @param pc closure 521 * @param rowid which row in the database was the request stored in 522 * @param purse_pub public key of the purse 523 * @param merge_pub public key representing the merge capability 524 * @param purse_creation when was the purse created 525 * @param purse_expiration when would an unmerged purse expire 526 * @param h_contract_terms contract associated with the purse 527 * @param age_limit the age limit for deposits into the purse 528 * @param target_amount amount to be put into the purse 529 * @param purse_sig signature of the purse over the initialization data 530 * @return #GNUNET_OK to continue to iterate 531 */ 532 static enum GNUNET_GenericReturnValue 533 handle_purse_requested ( 534 struct PurseContext *pc, 535 uint64_t rowid, 536 const struct TALER_PurseContractPublicKeyP *purse_pub, 537 const struct TALER_PurseMergePublicKeyP *merge_pub, 538 struct GNUNET_TIME_Timestamp purse_creation, 539 struct GNUNET_TIME_Timestamp purse_expiration, 540 const struct TALER_PrivateContractHashP *h_contract_terms, 541 uint32_t age_limit, 542 const struct TALER_Amount *target_amount, 543 const struct TALER_PurseContractSignatureP *purse_sig) 544 { 545 struct PurseSummary *ps; 546 struct GNUNET_HashCode key; 547 548 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 549 "Handling purse request `%s'\n", 550 TALER_B2S (purse_pub)); 551 GNUNET_assert (rowid >= TALER_ARL_USE_PP (purse_request_serial_id)); 552 TALER_ARL_USE_PP (purse_request_serial_id) = rowid + 1; 553 if (GNUNET_OK != 554 TALER_wallet_purse_create_verify (purse_expiration, 555 h_contract_terms, 556 merge_pub, 557 age_limit, 558 target_amount, 559 purse_pub, 560 purse_sig)) 561 { 562 struct TALER_AUDITORDB_BadSigLosses bsl = { 563 .problem_row_id = rowid, 564 .operation = (char *) "purse-request", 565 .loss = *target_amount, 566 .operation_specific_pub = purse_pub->eddsa_pub 567 }; 568 enum GNUNET_DB_QueryStatus qs; 569 570 qs = TALER_AUDITORDB_insert_bad_sig_losses ( 571 TALER_ARL_adb, 572 &bsl); 573 if (qs < 0) 574 { 575 GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs); 576 pc->qs = qs; 577 return GNUNET_SYSERR; 578 } 579 TALER_ARL_amount_add (&TALER_ARL_USE_AB (purse_total_bad_sig_loss), 580 &TALER_ARL_USE_AB (purse_total_bad_sig_loss), 581 target_amount); 582 } 583 GNUNET_CRYPTO_hash (purse_pub, 584 sizeof (*purse_pub), 585 &key); 586 ps = GNUNET_new (struct PurseSummary); 587 ps->purse_pub = *purse_pub; 588 GNUNET_assert (GNUNET_OK == 589 TALER_amount_set_zero (TALER_ARL_currency, 590 &ps->balance)); 591 ps->creation_date = purse_creation; 592 ps->expiration_date = purse_expiration; 593 ps->total_value = *target_amount; 594 ps->h_contract_terms = *h_contract_terms; 595 { 596 enum GNUNET_DB_QueryStatus qs; 597 598 qs = load_auditor_purse_summary (ps); 599 if (0 > qs) 600 { 601 GNUNET_free (ps); 602 pc->qs = qs; 603 return GNUNET_SYSERR; 604 } 605 } 606 GNUNET_assert (GNUNET_OK == 607 GNUNET_CONTAINER_multihashmap_put (pc->purses, 608 &key, 609 ps, 610 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY) 611 ); 612 return GNUNET_OK; 613 } 614 615 616 /** 617 * Function called with details about purse deposits that have been made, with 618 * the goal of auditing the deposit's execution. 619 * 620 * @param pc closure 621 * @param rowid unique serial ID for the deposit in our DB 622 * @param deposit deposit details 623 * @param reserve_pub which reserve is the purse merged into, NULL if unknown 624 * @param flags purse flags 625 * @param auditor_balance purse balance (according to the 626 * auditor during auditing) 627 * @param purse_total target amount the purse should reach 628 * @param denom_pub denomination public key of @a coin_pub 629 * @return #GNUNET_OK to continue to iterate, #GNUNET_SYSERR to stop 630 */ 631 static enum GNUNET_GenericReturnValue 632 handle_purse_deposits ( 633 struct PurseContext *pc, 634 uint64_t rowid, 635 const struct TALER_EXCHANGEDB_PurseDeposit *deposit, 636 const struct TALER_ReservePublicKeyP *reserve_pub, 637 enum TALER_WalletAccountMergeFlags flags, 638 const struct TALER_Amount *auditor_balance, 639 const struct TALER_Amount *purse_total, 640 const struct TALER_DenominationPublicKey *denom_pub) 641 { 642 struct TALER_Amount amount_minus_fee; 643 const char *base_url 644 = (NULL == deposit->exchange_base_url) 645 ? TALER_ARL_exchange_url 646 : deposit->exchange_base_url; 647 struct TALER_DenominationHashP h_denom_pub; 648 649 /* should be monotonically increasing */ 650 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 651 "Handling purse deposit `%s'\n", 652 TALER_B2S (&deposit->purse_pub)); 653 GNUNET_assert (rowid >= TALER_ARL_USE_PP (purse_deposits_serial_id)); 654 TALER_ARL_USE_PP (purse_deposits_serial_id) = rowid + 1; 655 656 { 657 const struct TALER_EXCHANGEDB_DenominationKeyInformation *issue; 658 enum GNUNET_DB_QueryStatus qs; 659 660 qs = TALER_ARL_get_denomination_info (denom_pub, 661 &issue, 662 &h_denom_pub); 663 if (0 > qs) 664 { 665 GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs); 666 if (GNUNET_DB_STATUS_HARD_ERROR == qs) 667 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 668 "Hard database error trying to get denomination %s from database!\n", 669 TALER_B2S (denom_pub)); 670 pc->qs = qs; 671 return GNUNET_SYSERR; 672 } 673 if (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS == qs) 674 { 675 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 676 "Failed to find denomination key for purse deposit `%s' in record %llu\n", 677 TALER_B2S (&deposit->purse_pub), 678 (unsigned long long) rowid); 679 qs = report_row_inconsistency ("purse-deposit", 680 rowid, 681 "denomination key not found"); 682 if (0 > qs) 683 { 684 GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs); 685 pc->qs = qs; 686 return GNUNET_SYSERR; 687 } 688 return GNUNET_OK; 689 } 690 if (TALER_ARL_SR_INVALID_NEGATIVE == 691 TALER_ARL_amount_subtract_neg (&amount_minus_fee, 692 &deposit->amount, 693 &issue->fees.deposit)) 694 { 695 /* The deposited amount does not even cover the deposit fee of the 696 denomination; report the bogus row instead of dying here. */ 697 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 698 "Purse deposit `%s' in record %llu is below the deposit fee of its denomination\n", 699 TALER_B2S (&deposit->purse_pub), 700 (unsigned long long) rowid); 701 qs = report_row_inconsistency ("purse-deposit", 702 rowid, 703 "deposited amount below deposit fee"); 704 if (0 > qs) 705 { 706 GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs); 707 pc->qs = qs; 708 return GNUNET_SYSERR; 709 } 710 return GNUNET_OK; 711 } 712 } 713 714 if (GNUNET_OK != 715 TALER_wallet_purse_deposit_verify (base_url, 716 &deposit->purse_pub, 717 &deposit->amount, 718 &h_denom_pub, 719 &deposit->h_age_commitment, 720 &deposit->coin_pub, 721 &deposit->coin_sig)) 722 { 723 struct TALER_AUDITORDB_BadSigLosses bsl = { 724 .problem_row_id = rowid, 725 .operation = (char *) "purse-deposit", 726 .loss = deposit->amount, 727 .operation_specific_pub = deposit->coin_pub.eddsa_pub 728 }; 729 enum GNUNET_DB_QueryStatus qs; 730 731 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 732 "Failed to verify purse deposit signature on `%s' in record %llu\n", 733 TALER_B2S (&deposit->purse_pub), 734 (unsigned long long) rowid); 735 qs = TALER_AUDITORDB_insert_bad_sig_losses ( 736 TALER_ARL_adb, 737 &bsl); 738 if (qs < 0) 739 { 740 GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs); 741 pc->qs = qs; 742 return GNUNET_SYSERR; 743 } 744 TALER_ARL_amount_add (&TALER_ARL_USE_AB (purse_total_bad_sig_loss), 745 &TALER_ARL_USE_AB (purse_total_bad_sig_loss), 746 &deposit->amount); 747 return GNUNET_OK; 748 } 749 750 { 751 struct PurseSummary *ps; 752 753 ps = setup_purse (pc, 754 &deposit->purse_pub); 755 if (NULL == ps) 756 { 757 enum GNUNET_DB_QueryStatus qs; 758 759 if (0 > pc->qs) 760 { 761 GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == pc->qs); 762 return GNUNET_SYSERR; 763 } 764 GNUNET_assert (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS == pc->qs); 765 qs = report_row_inconsistency ("purse_deposit", 766 rowid, 767 "purse not found"); 768 if (0 > qs) 769 { 770 GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs); 771 pc->qs = qs; 772 return GNUNET_SYSERR; 773 } 774 return GNUNET_OK; 775 } 776 TALER_ARL_amount_add (&ps->balance, 777 &ps->balance, 778 &amount_minus_fee); 779 TALER_ARL_amount_add (&TALER_ARL_USE_AB (purse_global_balance), 780 &TALER_ARL_USE_AB (purse_global_balance), 781 &amount_minus_fee); 782 } 783 return GNUNET_OK; 784 } 785 786 787 /** 788 * Function called with details about purse merges that have been made, with 789 * the goal of auditing the purse merge execution. 790 * 791 * @param pc closure 792 * @param rowid unique serial ID for the deposit in our DB 793 * @param partner_base_url where is the reserve, NULL for this exchange 794 * @param amount total amount expected in the purse 795 * @param balance current balance in the purse 796 * @param flags purse flags 797 * @param merge_pub merge capability key 798 * @param reserve_pub reserve the merge affects 799 * @param merge_sig signature affirming the merge 800 * @param purse_pub purse key 801 * @param merge_timestamp when did the merge happen 802 * @return #GNUNET_OK to continue to iterate, #GNUNET_SYSERR to stop 803 */ 804 static enum GNUNET_GenericReturnValue 805 handle_purse_merged ( 806 struct PurseContext *pc, 807 uint64_t rowid, 808 const char *partner_base_url, 809 const struct TALER_Amount *amount, 810 const struct TALER_Amount *balance, 811 enum TALER_WalletAccountMergeFlags flags, 812 const struct TALER_PurseMergePublicKeyP *merge_pub, 813 const struct TALER_ReservePublicKeyP *reserve_pub, 814 const struct TALER_PurseMergeSignatureP *merge_sig, 815 const struct TALER_PurseContractPublicKeyP *purse_pub, 816 struct GNUNET_TIME_Timestamp merge_timestamp) 817 { 818 struct PurseSummary *ps; 819 enum GNUNET_DB_QueryStatus qs; 820 821 /* should be monotonically increasing */ 822 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 823 "Handling purse merged `%s'\n", 824 TALER_B2S (purse_pub)); 825 GNUNET_assert (rowid >= TALER_ARL_USE_PP (purse_merges_serial_id)); 826 TALER_ARL_USE_PP (purse_merges_serial_id) = rowid + 1; 827 828 { 829 struct TALER_NormalizedPayto reserve_url; 830 831 reserve_url 832 = TALER_reserve_make_payto (NULL == partner_base_url 833 ? TALER_ARL_exchange_url 834 : partner_base_url, 835 reserve_pub); 836 if (GNUNET_OK != 837 TALER_wallet_purse_merge_verify (reserve_url, 838 merge_timestamp, 839 purse_pub, 840 merge_pub, 841 merge_sig)) 842 { 843 struct TALER_AUDITORDB_BadSigLosses bsl = { 844 .problem_row_id = rowid, 845 .operation = (char *) "merge-purse", 846 .loss = *amount, 847 .operation_specific_pub = merge_pub->eddsa_pub 848 }; 849 850 GNUNET_free (reserve_url.normalized_payto); 851 qs = TALER_AUDITORDB_insert_bad_sig_losses ( 852 TALER_ARL_adb, 853 &bsl); 854 if (qs < 0) 855 { 856 GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs); 857 pc->qs = qs; 858 return GNUNET_SYSERR; 859 } 860 TALER_ARL_amount_add (&TALER_ARL_USE_AB (purse_total_bad_sig_loss), 861 &TALER_ARL_USE_AB (purse_total_bad_sig_loss), 862 amount); 863 return GNUNET_OK; 864 } 865 GNUNET_free (reserve_url.normalized_payto); 866 } 867 868 ps = setup_purse (pc, 869 purse_pub); 870 if (NULL == ps) 871 { 872 if (pc->qs < 0) 873 { 874 GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == pc->qs); 875 return GNUNET_SYSERR; 876 } 877 GNUNET_assert (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS == pc->qs); 878 qs = report_row_inconsistency ("purse-merge", 879 rowid, 880 "purse not found"); 881 if (qs < 0) 882 { 883 GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs); 884 pc->qs = qs; 885 return GNUNET_SYSERR; 886 } 887 return GNUNET_OK; 888 } 889 GNUNET_break (GNUNET_TIME_timestamp_cmp (merge_timestamp, 890 ==, 891 ps->merge_timestamp)); 892 TALER_ARL_amount_add (&ps->balance, 893 &ps->balance, 894 amount); 895 return GNUNET_OK; 896 } 897 898 899 /** 900 * Function called with details about account merge requests that have been 901 * made, with the goal of auditing the account merge execution. 902 * 903 * @param pc closure 904 * @param rowid unique serial ID for the deposit in our DB 905 * @param reserve_pub reserve affected by the merge 906 * @param purse_pub purse being merged 907 * @param h_contract_terms hash over contract of the purse 908 * @param purse_expiration when would the purse expire 909 * @param amount total amount in the purse 910 * @param min_age minimum age of all coins deposited into the purse 911 * @param flags how was the purse created 912 * @param purse_fee if a purse fee was paid, how high is it 913 * @param merge_timestamp when was the merge approved 914 * @param reserve_sig signature by reserve approving the merge 915 * @return #GNUNET_OK to continue to iterate, #GNUNET_SYSERR to stop 916 */ 917 static enum GNUNET_GenericReturnValue 918 handle_account_merged ( 919 struct PurseContext *pc, 920 uint64_t rowid, 921 const struct TALER_ReservePublicKeyP *reserve_pub, 922 const struct TALER_PurseContractPublicKeyP *purse_pub, 923 const struct TALER_PrivateContractHashP *h_contract_terms, 924 struct GNUNET_TIME_Timestamp purse_expiration, 925 const struct TALER_Amount *amount, 926 uint32_t min_age, 927 enum TALER_WalletAccountMergeFlags flags, 928 const struct TALER_Amount *purse_fee, 929 struct GNUNET_TIME_Timestamp merge_timestamp, 930 const struct TALER_ReserveSignatureP *reserve_sig) 931 { 932 struct PurseSummary *ps; 933 enum GNUNET_DB_QueryStatus qs; 934 935 /* should be monotonically increasing */ 936 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 937 "Handling account merge on purse `%s'\n", 938 TALER_B2S (purse_pub)); 939 GNUNET_assert (rowid >= TALER_ARL_USE_PP (purse_account_merge_serial_id)); 940 TALER_ARL_USE_PP (purse_account_merge_serial_id) = rowid + 1; 941 if (GNUNET_OK != 942 TALER_wallet_account_merge_verify (merge_timestamp, 943 purse_pub, 944 purse_expiration, 945 h_contract_terms, 946 amount, 947 purse_fee, 948 min_age, 949 flags, 950 reserve_pub, 951 reserve_sig)) 952 { 953 struct TALER_AUDITORDB_BadSigLosses bsl = { 954 .problem_row_id = rowid, 955 .operation = (char *) "account-merge", 956 .loss = *amount, 957 .operation_specific_pub = reserve_pub->eddsa_pub 958 }; 959 960 qs = TALER_AUDITORDB_insert_bad_sig_losses ( 961 TALER_ARL_adb, 962 &bsl); 963 if (qs < 0) 964 { 965 GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs); 966 pc->qs = qs; 967 return GNUNET_SYSERR; 968 } 969 TALER_ARL_amount_add (&TALER_ARL_USE_AB (purse_total_bad_sig_loss), 970 &TALER_ARL_USE_AB (purse_total_bad_sig_loss), 971 amount); 972 return GNUNET_OK; 973 } 974 ps = setup_purse (pc, 975 purse_pub); 976 if (NULL == ps) 977 { 978 if (0 > pc->qs) 979 { 980 GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == pc->qs); 981 return GNUNET_SYSERR; 982 } 983 GNUNET_assert (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS == pc->qs); 984 qs = report_row_inconsistency ("account-merge", 985 rowid, 986 "purse not found"); 987 if (0 > qs) 988 { 989 GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs); 990 pc->qs = qs; 991 return GNUNET_SYSERR; 992 } 993 return GNUNET_OK; 994 } 995 TALER_ARL_amount_add (&TALER_ARL_USE_AB (purse_global_balance), 996 &TALER_ARL_USE_AB (purse_global_balance), 997 purse_fee); 998 TALER_ARL_amount_add (&ps->balance, 999 &ps->balance, 1000 purse_fee); 1001 return GNUNET_OK; 1002 } 1003 1004 1005 /** 1006 * Function called with details about purse decisions that have been made. 1007 * 1008 * @param pc closure 1009 * @param rowid unique serial ID for the deposit in our DB 1010 * @param purse_pub which purse was the decision made on 1011 * @param refunded true if decision was to refund 1012 * @return #GNUNET_OK to continue to iterate, #GNUNET_SYSERR to stop 1013 */ 1014 static enum GNUNET_GenericReturnValue 1015 handle_purse_decision ( 1016 struct PurseContext *pc, 1017 uint64_t rowid, 1018 const struct TALER_PurseContractPublicKeyP *purse_pub, 1019 bool refunded) 1020 { 1021 struct PurseSummary *ps; 1022 struct GNUNET_HashCode key; 1023 enum GNUNET_DB_QueryStatus qs; 1024 struct TALER_Amount purse_fee; 1025 struct TALER_Amount balance_without_purse_fee; 1026 1027 GNUNET_assert (rowid >= TALER_ARL_USE_PP (purse_decision_serial_id)); 1028 TALER_ARL_USE_PP (purse_decision_serial_id) = rowid + 1; 1029 ps = setup_purse (pc, 1030 purse_pub); 1031 if (NULL == ps) 1032 { 1033 if (0 > pc->qs) 1034 { 1035 GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == pc->qs); 1036 return GNUNET_SYSERR; 1037 } 1038 qs = report_row_inconsistency ("purse-decision", 1039 rowid, 1040 "purse not found"); 1041 if (0 > qs) 1042 { 1043 GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs); 1044 pc->qs = qs; 1045 return GNUNET_SYSERR; 1046 } 1047 return GNUNET_OK; 1048 } 1049 qs = get_purse_fee (ps->creation_date, 1050 &purse_fee); 1051 if (0 > qs) 1052 { 1053 GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs); 1054 pc->qs = qs; 1055 return GNUNET_SYSERR; 1056 } 1057 if (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS == qs) 1058 return GNUNET_OK; /* already reported */ 1059 if (0 > 1060 TALER_amount_subtract (&balance_without_purse_fee, 1061 &ps->balance, 1062 &purse_fee)) 1063 { 1064 qs = report_row_inconsistency ("purse-request", 1065 rowid, 1066 "purse fee higher than balance"); 1067 if (0 > qs) 1068 { 1069 GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs); 1070 pc->qs = qs; 1071 return GNUNET_SYSERR; 1072 } 1073 GNUNET_assert (GNUNET_OK == 1074 TALER_amount_set_zero (TALER_ARL_currency, 1075 &balance_without_purse_fee)); 1076 } 1077 1078 if (refunded) 1079 { 1080 if (-1 != TALER_amount_cmp (&balance_without_purse_fee, 1081 &ps->total_value)) 1082 { 1083 qs = report_amount_arithmetic_inconsistency ("purse-decision: refund", 1084 rowid, 1085 &ps->total_value, 1086 &balance_without_purse_fee, 1087 0); 1088 if (0 > qs) 1089 { 1090 GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs); 1091 pc->qs = qs; 1092 return GNUNET_SYSERR; 1093 } 1094 } 1095 if ( (internal_checks) && 1096 (! ps->purse_refunded) ) 1097 { 1098 qs = report_row_inconsistency ( 1099 "purse-decision", 1100 rowid, 1101 "purse not marked as refunded (internal check)"); 1102 if (qs < 0) 1103 { 1104 GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs); 1105 pc->qs = qs; 1106 return GNUNET_SYSERR; 1107 } 1108 } 1109 } 1110 else 1111 { 1112 if (-1 == TALER_amount_cmp (&balance_without_purse_fee, 1113 &ps->total_value)) 1114 { 1115 qs = report_amount_arithmetic_inconsistency ("purse-decision: merge", 1116 rowid, 1117 &ps->total_value, 1118 &balance_without_purse_fee, 1119 0); 1120 if (0 > qs) 1121 { 1122 GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs); 1123 pc->qs = qs; 1124 return GNUNET_SYSERR; 1125 } 1126 TALER_ARL_amount_add (&TALER_ARL_USE_AB ( 1127 purse_total_balance_insufficient_loss), 1128 &TALER_ARL_USE_AB ( 1129 purse_total_balance_insufficient_loss), 1130 &ps->total_value); 1131 } 1132 } 1133 1134 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 1135 "Deleting purse with decision `%s'\n", 1136 TALER_B2S (&ps->purse_pub)); 1137 /* The purse ceases to exist, so the money it held is no longer 1138 part of the balance the exchange holds in open purses. */ 1139 if (0 > 1140 TALER_amount_subtract (&TALER_ARL_USE_AB (purse_global_balance), 1141 &TALER_ARL_USE_AB (purse_global_balance), 1142 &ps->balance)) 1143 { 1144 /* Should be impossible: we added this balance to the global 1145 balance when we learned about it. */ 1146 GNUNET_break (0); 1147 GNUNET_assert (GNUNET_OK == 1148 TALER_amount_set_zero (TALER_ARL_currency, 1149 &TALER_ARL_USE_AB ( 1150 purse_global_balance))); 1151 } 1152 qs = TALER_AUDITORDB_delete_purse_info (TALER_ARL_adb, 1153 purse_pub); 1154 if (qs < 0) 1155 { 1156 GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs); 1157 pc->qs = qs; 1158 return GNUNET_SYSERR; 1159 } 1160 GNUNET_CRYPTO_hash (purse_pub, 1161 sizeof (*purse_pub), 1162 &key); 1163 GNUNET_assert (GNUNET_YES == 1164 GNUNET_CONTAINER_multihashmap_remove (pc->purses, 1165 &key, 1166 ps)); 1167 GNUNET_free (ps); 1168 return GNUNET_OK; 1169 } 1170 1171 1172 /** 1173 * Function called on explicitly deleted purses. 1174 * 1175 * @param pc closure 1176 * @param deletion_serial_id row ID with the deletion data 1177 * @param purse_pub public key of the purse 1178 * @param purse_sig signature affirming deletion of the purse 1179 * @return #GNUNET_OK to continue to iterate 1180 */ 1181 static enum GNUNET_GenericReturnValue 1182 handle_purse_deletion ( 1183 struct PurseContext *pc, 1184 uint64_t deletion_serial_id, 1185 const struct TALER_PurseContractPublicKeyP *purse_pub, 1186 const struct TALER_PurseContractSignatureP *purse_sig) 1187 { 1188 struct PurseSummary *ps; 1189 1190 GNUNET_assert (deletion_serial_id >= 1191 TALER_ARL_USE_PP (purse_deletion_serial_id)); 1192 TALER_ARL_USE_PP (purse_deletion_serial_id) = deletion_serial_id + 1; 1193 ps = setup_purse (pc, 1194 purse_pub); 1195 if (NULL == ps) 1196 { 1197 enum GNUNET_DB_QueryStatus qs; 1198 1199 if (0 > pc->qs) 1200 { 1201 GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == pc->qs); 1202 return GNUNET_SYSERR; 1203 } 1204 GNUNET_assert (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS == pc->qs); 1205 qs = report_row_inconsistency ("purse-delete", 1206 deletion_serial_id, 1207 "purse not found"); 1208 if (0 > qs) 1209 { 1210 GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs); 1211 pc->qs = qs; 1212 return GNUNET_SYSERR; 1213 } 1214 return GNUNET_OK; 1215 } 1216 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 1217 "Handling purse `%s' deletion\n", 1218 TALER_B2S (purse_pub)); 1219 if (GNUNET_OK != 1220 TALER_wallet_purse_delete_verify (purse_pub, 1221 purse_sig)) 1222 { 1223 enum GNUNET_DB_QueryStatus qs; 1224 1225 qs = report_row_inconsistency ( 1226 "purse-delete", 1227 deletion_serial_id, 1228 "purse deletion signature invalid"); 1229 if (qs < 0) 1230 { 1231 GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs); 1232 pc->qs = qs; 1233 return GNUNET_SYSERR; 1234 } 1235 } 1236 else 1237 { 1238 if ( (internal_checks) && 1239 (! ps->purse_deleted) ) 1240 { 1241 enum GNUNET_DB_QueryStatus qs; 1242 1243 qs = report_row_inconsistency ( 1244 "purse-delete", 1245 deletion_serial_id, 1246 "purse not marked as deleted (internal check)"); 1247 if (qs < 0) 1248 { 1249 GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs); 1250 pc->qs = qs; 1251 return GNUNET_SYSERR; 1252 } 1253 } 1254 } 1255 return GNUNET_OK; 1256 } 1257 1258 1259 /** 1260 * Function called on expired purses. 1261 * 1262 * @param pc closure 1263 * @param purse_pub public key of the purse 1264 * @param balance amount of money in the purse 1265 * @param expiration_date when did the purse expire? 1266 * @return #GNUNET_OK to continue to iterate 1267 */ 1268 static enum GNUNET_GenericReturnValue 1269 handle_purse_expired ( 1270 struct PurseContext *pc, 1271 const struct TALER_PurseContractPublicKeyP *purse_pub, 1272 const struct TALER_Amount *balance, 1273 struct GNUNET_TIME_Timestamp expiration_date) 1274 { 1275 enum GNUNET_DB_QueryStatus qs; 1276 struct TALER_AUDITORDB_PurseNotClosedInconsistencies pnci = { 1277 .amount = *balance, 1278 .expiration_date = expiration_date.abs_time, 1279 .purse_pub = purse_pub->eddsa_pub 1280 }; 1281 1282 qs = TALER_AUDITORDB_insert_purse_not_closed_inconsistencies ( 1283 TALER_ARL_adb, 1284 &pnci); 1285 if (qs < 0) 1286 { 1287 GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs); 1288 pc->qs = qs; 1289 return GNUNET_SYSERR; 1290 } 1291 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 1292 "Handling purse expiration `%s'\n", 1293 TALER_B2S (purse_pub)); 1294 TALER_ARL_amount_add (&TALER_ARL_USE_AB (purse_total_delayed_decisions), 1295 &TALER_ARL_USE_AB (purse_total_delayed_decisions), 1296 balance); 1297 return GNUNET_OK; 1298 } 1299 1300 1301 /** 1302 * Check that the purse summary matches what the exchange database 1303 * thinks about the purse, and update our own state of the purse. 1304 * 1305 * Remove all purses that we are happy with from the DB. 1306 * 1307 * @param cls our `struct PurseContext` 1308 * @param key hash of the purse public key 1309 * @param value a `struct PurseSummary` 1310 * @return #GNUNET_OK to process more entries 1311 */ 1312 static enum GNUNET_GenericReturnValue 1313 verify_purse_balance (void *cls, 1314 const struct GNUNET_HashCode *key, 1315 void *value) 1316 { 1317 struct PurseContext *pc = cls; 1318 struct PurseSummary *ps = value; 1319 enum GNUNET_DB_QueryStatus qs; 1320 1321 if (internal_checks) 1322 { 1323 struct TALER_Amount pf; 1324 struct TALER_Amount balance_without_purse_fee; 1325 1326 /* subtract purse fee from ps->balance to get actual balance we expect, as 1327 we track the balance including purse fee, while the exchange subtracts 1328 the purse fee early on. */ 1329 qs = get_purse_fee (ps->creation_date, 1330 &pf); 1331 if (qs < 0) 1332 { 1333 GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs); 1334 pc->qs = qs; 1335 return GNUNET_SYSERR; 1336 } 1337 if (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS == qs) 1338 return GNUNET_OK; /* error already reported */ 1339 if (0 > 1340 TALER_amount_subtract (&balance_without_purse_fee, 1341 &ps->balance, 1342 &pf)) 1343 { 1344 qs = report_row_inconsistency ("purse", 1345 0, 1346 "purse fee higher than balance"); 1347 if (qs < 0) 1348 { 1349 GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs); 1350 pc->qs = qs; 1351 return GNUNET_SYSERR; 1352 } 1353 GNUNET_assert (GNUNET_OK == 1354 TALER_amount_set_zero (TALER_ARL_currency, 1355 &balance_without_purse_fee)); 1356 } 1357 1358 if (0 != TALER_amount_cmp (&ps->exchange_balance, 1359 &balance_without_purse_fee)) 1360 { 1361 qs = report_amount_arithmetic_inconsistency ("purse-balance", 1362 0, 1363 &ps->exchange_balance, 1364 &balance_without_purse_fee, 1365 0); 1366 if (qs < 0) 1367 { 1368 GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs); 1369 pc->qs = qs; 1370 return GNUNET_SYSERR; 1371 } 1372 } 1373 } 1374 1375 if (ps->had_pi) 1376 { 1377 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 1378 "Updating purse `%s'\n", 1379 TALER_B2S (&ps->purse_pub)); 1380 qs = TALER_AUDITORDB_update_purse_info (TALER_ARL_adb, 1381 &ps->purse_pub, 1382 &ps->balance); 1383 } 1384 else 1385 { 1386 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 1387 "Inserting purse `%s'\n", 1388 TALER_B2S (&ps->purse_pub)); 1389 qs = TALER_AUDITORDB_insert_purse_info (TALER_ARL_adb, 1390 &ps->purse_pub, 1391 &ps->balance, 1392 &ps->total_value, 1393 ps->expiration_date); 1394 ps->had_pi = true; 1395 } 1396 if (qs < 0) 1397 { 1398 GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs); 1399 pc->qs = qs; 1400 return GNUNET_SYSERR; 1401 } 1402 GNUNET_assert (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT == qs); 1403 return GNUNET_OK; 1404 } 1405 1406 1407 /** 1408 * Clear memory from the purses hash map. 1409 * 1410 * @param cls our `struct PurseContext` 1411 * @param key hash of the purse public key 1412 * @param value a `struct PurseSummary` 1413 * @return #GNUNET_OK to process more entries 1414 */ 1415 static enum GNUNET_GenericReturnValue 1416 release_purse_balance (void *cls, 1417 const struct GNUNET_HashCode *key, 1418 void *value) 1419 { 1420 struct PurseContext *pc = cls; 1421 struct PurseSummary *ps = value; 1422 1423 GNUNET_assert (GNUNET_YES == 1424 GNUNET_CONTAINER_multihashmap_remove (pc->purses, 1425 key, 1426 ps)); 1427 GNUNET_free (ps); 1428 return GNUNET_OK; 1429 } 1430 1431 1432 /** 1433 * Analyze purses for being well-formed. 1434 * 1435 * @param cls NULL 1436 * @return transaction status code 1437 */ 1438 static enum GNUNET_DB_QueryStatus 1439 analyze_purses (void *cls) 1440 { 1441 struct PurseContext pc; 1442 enum GNUNET_DB_QueryStatus qs; 1443 1444 (void) cls; 1445 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 1446 "Analyzing purses\n"); 1447 qs = TALER_AUDITORDB_get_auditor_progress ( 1448 TALER_ARL_adb, 1449 TALER_ARL_GET_PP (purse_account_merge_serial_id), 1450 TALER_ARL_GET_PP (purse_decision_serial_id), 1451 TALER_ARL_GET_PP (purse_deletion_serial_id), 1452 TALER_ARL_GET_PP (purse_deposits_serial_id), 1453 TALER_ARL_GET_PP (purse_merges_serial_id), 1454 TALER_ARL_GET_PP (purse_request_serial_id), 1455 TALER_ARL_GET_PP (purse_open_counter), 1456 NULL); 1457 if (0 > qs) 1458 { 1459 GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs); 1460 return qs; 1461 } 1462 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 1463 "Resuming purse audit at %llu/%llu/%llu/%llu/%llu/%llu/%llu\n", 1464 (unsigned long long) TALER_ARL_USE_PP ( 1465 purse_open_counter), 1466 (unsigned long long) TALER_ARL_USE_PP ( 1467 purse_request_serial_id), 1468 (unsigned long long) TALER_ARL_USE_PP ( 1469 purse_decision_serial_id), 1470 (unsigned long long) TALER_ARL_USE_PP ( 1471 purse_deletion_serial_id), 1472 (unsigned long long) TALER_ARL_USE_PP ( 1473 purse_merges_serial_id), 1474 (unsigned long long) TALER_ARL_USE_PP ( 1475 purse_deposits_serial_id), 1476 (unsigned long long) TALER_ARL_USE_PP ( 1477 purse_account_merge_serial_id)); 1478 pc.qs = GNUNET_DB_STATUS_SUCCESS_ONE_RESULT; 1479 qs = TALER_AUDITORDB_get_balance ( 1480 TALER_ARL_adb, 1481 TALER_ARL_GET_AB (purse_global_balance), 1482 TALER_ARL_GET_AB (purse_total_balance_insufficient_loss), 1483 TALER_ARL_GET_AB (purse_total_delayed_decisions), 1484 TALER_ARL_GET_AB (purse_total_balance_purse_not_closed), 1485 TALER_ARL_GET_AB (purse_total_arithmetic_delta_plus), 1486 TALER_ARL_GET_AB (purse_total_arithmetic_delta_minus), 1487 TALER_ARL_GET_AB (purse_total_bad_sig_loss), 1488 NULL); 1489 if (qs < 0) 1490 { 1491 GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs); 1492 return qs; 1493 } 1494 pc.purses = GNUNET_CONTAINER_multihashmap_create (512, 1495 GNUNET_NO); 1496 1497 qs = TALER_EXCHANGEDB_iterate_purse_requests_above_serial_id ( 1498 TALER_ARL_edb, 1499 TALER_ARL_USE_PP (purse_request_serial_id), 1500 &handle_purse_requested, 1501 &pc); 1502 if (qs < 0) 1503 { 1504 GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs); 1505 return qs; 1506 } 1507 if (pc.qs < 0) 1508 { 1509 GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == pc.qs); 1510 return pc.qs; 1511 } 1512 qs = TALER_EXCHANGEDB_iterate_purse_merges_above_serial_id ( 1513 TALER_ARL_edb, 1514 TALER_ARL_USE_PP (purse_merges_serial_id), 1515 &handle_purse_merged, 1516 &pc); 1517 if (qs < 0) 1518 { 1519 GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs); 1520 return qs; 1521 } 1522 if (pc.qs < 0) 1523 { 1524 GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == pc.qs); 1525 return pc.qs; 1526 } 1527 1528 qs = TALER_EXCHANGEDB_iterate_purse_deposits_above_serial_id ( 1529 TALER_ARL_edb, 1530 TALER_ARL_USE_PP (purse_deposits_serial_id), 1531 &handle_purse_deposits, 1532 &pc); 1533 if (qs < 0) 1534 { 1535 GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs); 1536 return qs; 1537 } 1538 if (pc.qs < 0) 1539 { 1540 GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == pc.qs); 1541 return pc.qs; 1542 } 1543 1544 /* Charge purse fee! */ 1545 qs = TALER_EXCHANGEDB_iterate_account_merges_above_serial_id ( 1546 TALER_ARL_edb, 1547 TALER_ARL_USE_PP (purse_account_merge_serial_id), 1548 &handle_account_merged, 1549 &pc); 1550 if (qs < 0) 1551 { 1552 GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs); 1553 return qs; 1554 } 1555 if (pc.qs < 0) 1556 { 1557 GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == pc.qs); 1558 return pc.qs; 1559 } 1560 1561 qs = TALER_EXCHANGEDB_iterate_all_purse_decisions_above_serial_id ( 1562 TALER_ARL_edb, 1563 TALER_ARL_USE_PP (purse_decision_serial_id), 1564 &handle_purse_decision, 1565 &pc); 1566 if (qs < 0) 1567 { 1568 GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs); 1569 return qs; 1570 } 1571 if (pc.qs < 0) 1572 { 1573 GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == pc.qs); 1574 return pc.qs; 1575 } 1576 1577 qs = TALER_EXCHANGEDB_iterate_all_purse_deletions_above_serial_id ( 1578 TALER_ARL_edb, 1579 TALER_ARL_USE_PP (purse_deletion_serial_id), 1580 &handle_purse_deletion, 1581 &pc); 1582 if (qs < 0) 1583 { 1584 GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs); 1585 return qs; 1586 } 1587 if (pc.qs < 0) 1588 { 1589 GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == pc.qs); 1590 return pc.qs; 1591 } 1592 1593 qs = TALER_AUDITORDB_iterate_purse_expired ( 1594 TALER_ARL_adb, 1595 &handle_purse_expired, 1596 &pc); 1597 if (qs < 0) 1598 { 1599 GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs); 1600 return qs; 1601 } 1602 if (pc.qs < 0) 1603 { 1604 GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == pc.qs); 1605 return pc.qs; 1606 } 1607 1608 GNUNET_CONTAINER_multihashmap_iterate (pc.purses, 1609 &verify_purse_balance, 1610 &pc); 1611 GNUNET_CONTAINER_multihashmap_iterate (pc.purses, 1612 &release_purse_balance, 1613 &pc); 1614 GNUNET_break (0 == 1615 GNUNET_CONTAINER_multihashmap_size (pc.purses)); 1616 GNUNET_CONTAINER_multihashmap_destroy (pc.purses); 1617 if (pc.qs < 0) 1618 { 1619 GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == pc.qs); 1620 return pc.qs; 1621 } 1622 qs = TALER_AUDITORDB_insert_balance ( 1623 TALER_ARL_adb, 1624 TALER_ARL_SET_AB (purse_global_balance), 1625 TALER_ARL_SET_AB (purse_total_balance_insufficient_loss), 1626 TALER_ARL_SET_AB (purse_total_delayed_decisions), 1627 TALER_ARL_SET_AB (purse_total_balance_purse_not_closed), 1628 TALER_ARL_SET_AB (purse_total_arithmetic_delta_plus), 1629 TALER_ARL_SET_AB (purse_total_arithmetic_delta_minus), 1630 TALER_ARL_SET_AB (purse_total_bad_sig_loss), 1631 NULL); 1632 if (0 > qs) 1633 { 1634 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 1635 "Failed to update auditor DB, not recording progress\n"); 1636 GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs); 1637 return qs; 1638 } 1639 qs = TALER_AUDITORDB_insert_auditor_progress ( 1640 TALER_ARL_adb, 1641 TALER_ARL_SET_PP (purse_account_merge_serial_id), 1642 TALER_ARL_SET_PP (purse_decision_serial_id), 1643 TALER_ARL_SET_PP (purse_deletion_serial_id), 1644 TALER_ARL_SET_PP (purse_deposits_serial_id), 1645 TALER_ARL_SET_PP (purse_merges_serial_id), 1646 TALER_ARL_SET_PP (purse_request_serial_id), 1647 TALER_ARL_SET_PP (purse_open_counter), 1648 NULL); 1649 if (0 > qs) 1650 { 1651 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 1652 "Failed to update auditor DB, not recording progress\n"); 1653 GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs); 1654 return qs; 1655 } 1656 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 1657 "Concluded purse audit step at %llu/%llu/%llu/%llu/%llu/%llu\n", 1658 (unsigned long long) TALER_ARL_USE_PP ( 1659 purse_request_serial_id), 1660 (unsigned long long) TALER_ARL_USE_PP ( 1661 purse_decision_serial_id), 1662 (unsigned long long) TALER_ARL_USE_PP ( 1663 purse_deletion_serial_id), 1664 (unsigned long long) TALER_ARL_USE_PP ( 1665 purse_merges_serial_id), 1666 (unsigned long long) TALER_ARL_USE_PP ( 1667 purse_deposits_serial_id), 1668 (unsigned long long) TALER_ARL_USE_PP ( 1669 purse_account_merge_serial_id)); 1670 return GNUNET_DB_STATUS_SUCCESS_ONE_RESULT; 1671 } 1672 1673 1674 /** 1675 * Function called on events received from Postgres. 1676 * 1677 * @param cls closure, NULL 1678 * @param extra additional event data provided 1679 * @param extra_size number of bytes in @a extra 1680 */ 1681 static void 1682 db_notify (void *cls, 1683 const void *extra, 1684 size_t extra_size) 1685 { 1686 (void) cls; 1687 (void) extra; 1688 (void) extra_size; 1689 1690 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 1691 "Received notification to wake purses\n"); 1692 if (GNUNET_OK != 1693 TALER_ARL_setup_sessions_and_run (&analyze_purses, 1694 NULL)) 1695 { 1696 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 1697 "Audit failed\n"); 1698 GNUNET_SCHEDULER_shutdown (); 1699 global_ret = EXIT_FAILURE; 1700 return; 1701 } 1702 } 1703 1704 1705 /** 1706 * Function called on shutdown. 1707 */ 1708 static void 1709 do_shutdown (void *cls) 1710 { 1711 (void) cls; 1712 1713 if (NULL != eh) 1714 { 1715 TALER_AUDITORDB_event_listen_cancel (eh); 1716 eh = NULL; 1717 } 1718 TALER_ARL_done (); 1719 } 1720 1721 1722 /** 1723 * Main function that will be run. 1724 * 1725 * @param cls closure 1726 * @param args remaining command-line arguments 1727 * @param cfgfile name of the configuration file used (for saving, can be NULL!) 1728 * @param c configuration 1729 */ 1730 static void 1731 run (void *cls, 1732 char *const *args, 1733 const char *cfgfile, 1734 const struct GNUNET_CONFIGURATION_Handle *c) 1735 { 1736 (void) cls; 1737 (void) args; 1738 (void) cfgfile; 1739 1740 cfg = c; 1741 GNUNET_SCHEDULER_add_shutdown (&do_shutdown, 1742 NULL); 1743 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 1744 "Launching purses auditor\n"); 1745 if (EXIT_SUCCESS != 1746 (global_ret = TALER_ARL_init (c))) 1747 { 1748 return; 1749 } 1750 if (test_mode != 1) 1751 { 1752 struct GNUNET_DB_EventHeaderP es = { 1753 .size = htons (sizeof (es)), 1754 .type = htons (TALER_DBEVENT_EXCHANGE_AUDITOR_WAKE_HELPER_PURSES) 1755 }; 1756 1757 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 1758 "Running helper indefinitely\n"); 1759 eh = TALER_AUDITORDB_event_listen (TALER_ARL_adb, 1760 &es, 1761 GNUNET_TIME_UNIT_FOREVER_REL, 1762 &db_notify, 1763 NULL); 1764 } 1765 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 1766 "Starting audit\n"); 1767 if (GNUNET_OK != 1768 TALER_ARL_setup_sessions_and_run (&analyze_purses, 1769 NULL)) 1770 { 1771 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 1772 "Audit failed\n"); 1773 GNUNET_SCHEDULER_shutdown (); 1774 global_ret = EXIT_FAILURE; 1775 return; 1776 } 1777 } 1778 1779 1780 /** 1781 * The main function to check the database's handling of purses. 1782 * 1783 * @param argc number of arguments from the command line 1784 * @param argv command line arguments 1785 * @return 0 ok, 1 on error 1786 */ 1787 int 1788 main (int argc, 1789 char *const *argv) 1790 { 1791 const struct GNUNET_GETOPT_CommandLineOption options[] = { 1792 GNUNET_GETOPT_option_flag ('i', 1793 "internal", 1794 "perform checks only applicable for exchange-internal audits", 1795 &internal_checks), 1796 GNUNET_GETOPT_option_flag ('t', 1797 "test", 1798 "run in test mode and exit when idle", 1799 &test_mode), 1800 GNUNET_GETOPT_option_timetravel ('T', 1801 "timetravel"), 1802 GNUNET_GETOPT_OPTION_END 1803 }; 1804 enum GNUNET_GenericReturnValue ret; 1805 1806 ret = GNUNET_PROGRAM_run ( 1807 TALER_AUDITOR_project_data (), 1808 argc, 1809 argv, 1810 "taler-helper-auditor-purses", 1811 gettext_noop ("Audit Taler exchange purse handling"), 1812 options, 1813 &run, 1814 NULL); 1815 if (GNUNET_SYSERR == ret) 1816 return EXIT_NOTCONFIGURED; 1817 if (GNUNET_NO == ret) 1818 return EXIT_SUCCESS; 1819 return global_ret; 1820 } 1821 1822 1823 /* end of taler-helper-auditor-purses.c */