taler-helper-auditor-aggregation.c (55527B)
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-aggregation.c 18 * @brief audits an exchange's aggregations. 19 * @author Christian Grothoff 20 */ 21 #include "platform.h" 22 #include <gnunet/gnunet_util_lib.h> 23 #include "auditordb_lib.h" 24 /* WIRE_TRANSFER_OUT's callback typedef also lives in exchangedb_lib.h, so its 25 closure override must be established before that header is included. */ 26 struct AggregationContext; 27 #define TALER_EXCHANGEDB_WIRE_TRANSFER_OUT_RESULT_CLOSURE struct \ 28 AggregationContext 29 #include "exchangedb_lib.h" 30 #include "taler/taler_bank_service.h" 31 #include "taler/taler_signatures.h" 32 #include "taler/taler_dbevents.h" 33 #include "report-lib.h" 34 #include "auditor-database/event_listen.h" 35 #include "auditor-database/get_auditor_progress.h" 36 #include "auditor-database/get_balance.h" 37 #include "auditor-database/insert_amount_arithmetic_inconsistency.h" 38 #include "auditor-database/insert_auditor_progress.h" 39 #include "auditor-database/insert_bad_sig_losses.h" 40 #include "auditor-database/insert_balance.h" 41 #include "auditor-database/insert_coin_inconsistency.h" 42 #include "auditor-database/insert_fee_time_inconsistency.h" 43 #include "auditor-database/insert_row_inconsistency.h" 44 #include "auditor-database/insert_wire_out_inconsistency.h" 45 #include "exchange-database/get_coin_transactions.h" 46 #include "exchange-database/get_known_coin.h" 47 #include "exchange-database/get_wire_fee.h" 48 struct WireCheckContext; 49 #define TALER_EXCHANGEDB_AGGREGATION_DATA_RESULT_CLOSURE struct WireCheckContext 50 #include "exchange-database/iterate_wire_transfers.h" 51 #include "exchange-database/iterate_wire_outs_above_serial_id.h" 52 53 /** 54 * Return value from main(). 55 */ 56 static int global_ret; 57 58 /** 59 * Run in test mode. Exit when idle instead of 60 * going to sleep and waiting for more work. 61 */ 62 static int test_mode; 63 64 /** 65 * Checkpointing our progress for aggregations. 66 */ 67 static TALER_ARL_DEF_PP (aggregation_last_wire_out_serial_id); 68 69 /** 70 * Total aggregation fees (wire fees) earned. 71 */ 72 static TALER_ARL_DEF_AB (aggregation_total_wire_fee_revenue); 73 74 /** 75 * Total delta between calculated and stored wire out transfers, 76 * for positive deltas. 77 */ 78 static TALER_ARL_DEF_AB (aggregation_total_wire_out_delta_plus); 79 80 /** 81 * Total delta between calculated and stored wire out transfers 82 * for negative deltas. 83 */ 84 static TALER_ARL_DEF_AB (aggregation_total_wire_out_delta_minus); 85 86 /** 87 * Profits the exchange made by bad amount calculations on coins. 88 */ 89 static TALER_ARL_DEF_AB (aggregation_total_coin_delta_plus); 90 91 /** 92 * Losses the exchange made by bad amount calculations on coins. 93 */ 94 static TALER_ARL_DEF_AB (aggregation_total_coin_delta_minus); 95 96 /** 97 * Profits the exchange made by bad amount calculations. 98 */ 99 static TALER_ARL_DEF_AB (aggregation_total_arithmetic_delta_plus); 100 101 /** 102 * Losses the exchange made by bad amount calculations. 103 */ 104 static TALER_ARL_DEF_AB (aggregation_total_arithmetic_delta_minus); 105 106 /** 107 * Total amount lost by operations for which signatures were invalid. 108 */ 109 static TALER_ARL_DEF_AB (aggregation_total_bad_sig_loss); 110 111 /** 112 * Should we run checks that only work for exchange-internal audits? 113 */ 114 static int internal_checks; 115 116 static struct GNUNET_DB_EventHandler *eh; 117 118 /** 119 * The auditors's configuration. 120 */ 121 static const struct GNUNET_CONFIGURATION_Handle *cfg; 122 123 /** 124 * Report a (serious) inconsistency in the exchange's database with 125 * respect to calculations involving amounts. 126 * 127 * @param operation what operation had the inconsistency 128 * @param rowid affected row, 0 if row is missing 129 * @param exchange amount calculated by exchange 130 * @param auditor amount calculated by auditor 131 * @param profitable 1 if @a exchange being larger than @a auditor is 132 * profitable for the exchange for this operation, 133 * -1 if @a exchange being smaller than @a auditor is 134 * profitable for the exchange, and 0 if it is unclear 135 * @return transaction status 136 */ 137 static enum GNUNET_DB_QueryStatus 138 report_amount_arithmetic_inconsistency ( 139 const char *operation, 140 uint64_t rowid, 141 const struct TALER_Amount *exchange, 142 const struct TALER_Amount *auditor, 143 int profitable) 144 { 145 struct TALER_Amount delta; 146 struct TALER_Amount *target; 147 148 if (0 < TALER_amount_cmp (exchange, 149 auditor)) 150 { 151 /* exchange > auditor */ 152 TALER_ARL_amount_subtract (&delta, 153 exchange, 154 auditor); 155 } 156 else 157 { 158 /* exchange <= auditor */ 159 profitable = -profitable; 160 TALER_ARL_amount_subtract (&delta, 161 auditor, 162 exchange); 163 } 164 165 { 166 struct TALER_AUDITORDB_AmountArithmeticInconsistency aai = { 167 .problem_row_id = rowid, 168 .profitable = profitable, 169 .operation = (char *) operation, 170 .exchange_amount = *exchange, 171 .auditor_amount = *auditor 172 }; 173 enum GNUNET_DB_QueryStatus qs; 174 175 qs = TALER_AUDITORDB_insert_amount_arithmetic_inconsistency ( 176 TALER_ARL_adb, 177 &aai); 178 179 if (qs < 0) 180 { 181 GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs); 182 return qs; 183 } 184 } 185 if (0 != profitable) 186 { 187 target = (1 == profitable) 188 ? &TALER_ARL_USE_AB (aggregation_total_arithmetic_delta_plus) 189 : &TALER_ARL_USE_AB (aggregation_total_arithmetic_delta_minus); 190 TALER_ARL_amount_add (target, 191 target, 192 &delta); 193 } 194 return GNUNET_DB_STATUS_SUCCESS_ONE_RESULT; 195 } 196 197 198 /** 199 * Report a (serious) inconsistency in the exchange's database with 200 * respect to calculations involving amounts of a coin. 201 * 202 * @param operation what operation had the inconsistency 203 * @param coin_pub affected coin 204 * @param exchange amount calculated by exchange 205 * @param auditor amount calculated by auditor 206 * @param profitable 1 if @a exchange being larger than @a auditor is 207 * profitable for the exchange for this operation, 208 * -1 if @a exchange being smaller than @a auditor is 209 * profitable for the exchange, and 0 if it is unclear 210 * @return transaction status 211 */ 212 static enum GNUNET_DB_QueryStatus 213 report_coin_arithmetic_inconsistency ( 214 const char *operation, 215 const struct TALER_CoinSpendPublicKeyP *coin_pub, 216 const struct TALER_Amount *exchange, 217 const struct TALER_Amount *auditor, 218 int profitable) 219 { 220 struct TALER_Amount delta; 221 struct TALER_Amount *target; 222 223 if (0 < TALER_amount_cmp (exchange, 224 auditor)) 225 { 226 /* exchange > auditor */ 227 TALER_ARL_amount_subtract (&delta, 228 exchange, 229 auditor); 230 } 231 else 232 { 233 /* exchange <= auditor */ 234 profitable = -profitable; 235 TALER_ARL_amount_subtract (&delta, 236 auditor, 237 exchange); 238 } 239 240 { 241 enum GNUNET_DB_QueryStatus qs; 242 struct TALER_AUDITORDB_CoinInconsistency ci = { 243 .operation = (char *) operation, 244 .auditor_amount = *auditor, 245 .exchange_amount = *exchange, 246 .profitable = profitable, 247 .coin_pub = coin_pub->eddsa_pub 248 }; 249 250 qs = TALER_AUDITORDB_insert_coin_inconsistency ( 251 TALER_ARL_adb, 252 &ci); 253 254 if (qs < 0) 255 { 256 GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs); 257 return qs; 258 } 259 } 260 if (0 != profitable) 261 { 262 target = (1 == profitable) 263 ? &TALER_ARL_USE_AB (aggregation_total_coin_delta_plus) 264 : &TALER_ARL_USE_AB (aggregation_total_coin_delta_minus); 265 TALER_ARL_amount_add (target, 266 target, 267 &delta); 268 } 269 return GNUNET_DB_STATUS_SUCCESS_ONE_RESULT; 270 } 271 272 273 /** 274 * Report a (serious) inconsistency in the exchange's database. 275 * 276 * @param table affected table 277 * @param rowid affected row, 0 if row is missing 278 * @param diagnostic message explaining the problem 279 * @return transaction status 280 */ 281 static enum GNUNET_DB_QueryStatus 282 report_row_inconsistency (const char *table, 283 uint64_t rowid, 284 const char *diagnostic) 285 { 286 enum GNUNET_DB_QueryStatus qs; 287 struct TALER_AUDITORDB_RowInconsistency ri = { 288 .diagnostic = (char *) diagnostic, 289 .row_table = (char *) table, 290 .problem_row_id = rowid 291 }; 292 293 qs = TALER_AUDITORDB_insert_row_inconsistency ( 294 TALER_ARL_adb, 295 &ri); 296 297 if (qs < 0) 298 { 299 GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs); 300 return qs; 301 } 302 return GNUNET_DB_STATUS_SUCCESS_ONE_RESULT; 303 } 304 305 306 /* *********************** Analyze aggregations ******************** */ 307 /* This logic checks that the aggregator did the right thing 308 paying each merchant what they were due (and on time). */ 309 310 311 /** 312 * Information about wire fees charged by the exchange. 313 */ 314 struct WireFeeInfo 315 { 316 317 /** 318 * Kept in a DLL. 319 */ 320 struct WireFeeInfo *next; 321 322 /** 323 * Kept in a DLL. 324 */ 325 struct WireFeeInfo *prev; 326 327 /** 328 * When does the fee go into effect (inclusive). 329 */ 330 struct GNUNET_TIME_Timestamp start_date; 331 332 /** 333 * When does the fee stop being in effect (exclusive). 334 */ 335 struct GNUNET_TIME_Timestamp end_date; 336 337 /** 338 * How high are the wire fees. 339 */ 340 struct TALER_WireFeeSet fees; 341 342 }; 343 344 345 /** 346 * Closure for callbacks during #analyze_merchants(). 347 */ 348 struct AggregationContext 349 { 350 351 /** 352 * DLL of wire fees charged by the exchange. 353 */ 354 struct WireFeeInfo *fee_head; 355 356 /** 357 * DLL of wire fees charged by the exchange. 358 */ 359 struct WireFeeInfo *fee_tail; 360 361 /** 362 * Final result status. 363 */ 364 enum GNUNET_DB_QueryStatus qs; 365 }; 366 367 368 /** 369 * Closure for #wire_transfer_information_cb. 370 */ 371 struct WireCheckContext 372 { 373 374 /** 375 * Corresponding merchant context. 376 */ 377 struct AggregationContext *ac; 378 379 /** 380 * Total deposits claimed by all transactions that were aggregated 381 * under the given @e wtid. 382 */ 383 struct TALER_Amount total_deposits; 384 385 /** 386 * Target account details of the receiver. 387 */ 388 struct TALER_FullPayto payto_uri; 389 390 /** 391 * Execution time of the wire transfer. 392 */ 393 struct GNUNET_TIME_Timestamp date; 394 395 /** 396 * Database transaction status. 397 */ 398 enum GNUNET_DB_QueryStatus qs; 399 400 }; 401 402 403 /** 404 * Check coin's transaction history for plausibility. Does NOT check 405 * the signatures (those are checked independently), but does calculate 406 * the amounts for the aggregation table and checks that the total 407 * claimed coin value is within the value of the coin's denomination. 408 * 409 * @param coin_pub public key of the coin (for reporting) 410 * @param h_contract_terms hash of the proposal for which we calculate the amount 411 * @param merchant_pub public key of the merchant (who is allowed to issue refunds) 412 * @param issue denomination information about the coin 413 * @param tl_head head of transaction history to verify 414 * @param[out] merchant_gain amount the coin contributes to the wire transfer to the merchant 415 * @param[out] deposit_gain amount the coin contributes excluding refunds 416 * @return database transaction status 417 */ 418 static enum GNUNET_DB_QueryStatus 419 check_transaction_history_for_deposit ( 420 const struct TALER_CoinSpendPublicKeyP *coin_pub, 421 const struct TALER_PrivateContractHashP *h_contract_terms, 422 const struct TALER_MerchantPublicKeyP *merchant_pub, 423 const struct TALER_EXCHANGEDB_DenominationKeyInformation *issue, 424 const struct TALER_EXCHANGEDB_TransactionList *tl_head, 425 struct TALER_Amount *merchant_gain, 426 struct TALER_Amount *deposit_gain) 427 { 428 struct TALER_Amount expenditures; 429 struct TALER_Amount refunds; 430 struct TALER_Amount spent; 431 const struct TALER_Amount *deposited = NULL; 432 struct TALER_Amount merchant_loss; 433 const struct TALER_Amount *deposit_fee; 434 enum GNUNET_DB_QueryStatus qs; 435 436 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 437 "Checking transaction history of coin %s\n", 438 TALER_B2S (coin_pub)); 439 GNUNET_assert (GNUNET_OK == 440 TALER_amount_set_zero (TALER_ARL_currency, 441 &expenditures)); 442 GNUNET_assert (GNUNET_OK == 443 TALER_amount_set_zero (TALER_ARL_currency, 444 &refunds)); 445 GNUNET_assert (GNUNET_OK == 446 TALER_amount_set_zero (TALER_ARL_currency, 447 merchant_gain)); 448 GNUNET_assert (GNUNET_OK == 449 TALER_amount_set_zero (TALER_ARL_currency, 450 &merchant_loss)); 451 /* Go over transaction history to compute totals; note that we do not bother 452 to reconstruct the order of the events, so instead of subtracting we 453 compute positive (deposit, melt) and negative (refund) values separately 454 here, and then subtract the negative from the positive at the end (after 455 the loops). */ 456 deposit_fee = NULL; 457 for (const struct TALER_EXCHANGEDB_TransactionList *tl = tl_head; 458 NULL != tl; 459 tl = tl->next) 460 { 461 const struct TALER_Amount *fee_claimed; 462 463 switch (tl->type) 464 { 465 case TALER_EXCHANGEDB_TT_DEPOSIT: 466 { 467 const struct TALER_Amount *amount_with_fee; 468 469 amount_with_fee = &tl->details.deposit->amount_with_fee; /* according to exchange*/ 470 fee_claimed = &tl->details.deposit->deposit_fee; /* Fee according to exchange DB */ 471 TALER_ARL_amount_add (&expenditures, 472 &expenditures, 473 amount_with_fee); 474 /* Check if this deposit is within the remit of the aggregation 475 we are investigating, if so, include it in the totals. */ 476 if ((0 == GNUNET_memcmp (merchant_pub, 477 &tl->details.deposit->merchant_pub)) && 478 (0 == GNUNET_memcmp (h_contract_terms, 479 &tl->details.deposit->h_contract_terms))) 480 { 481 struct TALER_Amount amount_without_fee; 482 483 if (NULL != deposited) 484 { 485 struct TALER_AUDITORDB_RowInconsistency ri = { 486 .problem_row_id = tl->serial_id, 487 .diagnostic = (char *) 488 "multiple deposits of the same coin into the same contract detected", 489 .row_table = (char *) "deposits" 490 }; 491 492 qs = TALER_AUDITORDB_insert_row_inconsistency ( 493 TALER_ARL_adb, 494 &ri); 495 496 if (qs < 0) 497 { 498 GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs); 499 return qs; 500 } 501 } 502 deposited = amount_with_fee; 503 if (TALER_ARL_SR_INVALID_NEGATIVE == 504 TALER_ARL_amount_subtract_neg (&amount_without_fee, 505 amount_with_fee, 506 fee_claimed)) 507 { 508 /* The exchange claims a deposit fee that exceeds the deposited 509 amount; report it (the fee check below will also fire) and 510 continue with a zero contribution instead of dying here. */ 511 qs = report_row_inconsistency ("deposits", 512 tl->serial_id, 513 "deposit fee exceeds deposited amount"); 514 if (0 > qs) 515 return qs; 516 GNUNET_assert (GNUNET_OK == 517 TALER_amount_set_zero (TALER_ARL_currency, 518 &amount_without_fee)); 519 } 520 TALER_ARL_amount_add (merchant_gain, 521 merchant_gain, 522 &amount_without_fee); 523 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 524 "Detected applicable deposit of %s\n", 525 TALER_amount2s (&amount_without_fee)); 526 deposit_fee = fee_claimed; /* We had a deposit, remember the fee, we may need it */ 527 } 528 /* Check that the fees given in the transaction list and in dki match */ 529 if (0 != 530 TALER_amount_cmp (&issue->fees.deposit, 531 fee_claimed)) 532 { 533 /* Disagreement in fee structure between auditor and exchange DB! */ 534 qs = report_amount_arithmetic_inconsistency ("deposit fee", 535 0, 536 fee_claimed, 537 &issue->fees.deposit, 538 1); 539 if (0 > qs) 540 return qs; 541 } 542 break; 543 } 544 case TALER_EXCHANGEDB_TT_MELT: 545 { 546 const struct TALER_Amount *amount_with_fee; 547 548 amount_with_fee = &tl->details.melt->amount_with_fee; 549 fee_claimed = &tl->details.melt->melt_fee; 550 TALER_ARL_amount_add (&expenditures, 551 &expenditures, 552 amount_with_fee); 553 /* Check that the fees given in the transaction list and in dki match */ 554 if (0 != 555 TALER_amount_cmp (&issue->fees.refresh, 556 fee_claimed)) 557 { 558 /* Disagreement in fee structure between exchange and auditor */ 559 qs = report_amount_arithmetic_inconsistency ("melt fee", 560 0, 561 fee_claimed, 562 &issue->fees.refresh, 563 1); 564 if (0 > qs) 565 return qs; 566 } 567 break; 568 } 569 case TALER_EXCHANGEDB_TT_REFUND: 570 { 571 const struct TALER_Amount *amount_with_fee; 572 573 amount_with_fee = &tl->details.refund->refund_amount; 574 fee_claimed = &tl->details.refund->refund_fee; 575 TALER_ARL_amount_add (&refunds, 576 &refunds, 577 amount_with_fee); 578 TALER_ARL_amount_add (&expenditures, 579 &expenditures, 580 fee_claimed); 581 /* Check if this refund is within the remit of the aggregation 582 we are investigating, if so, include it in the totals. */ 583 if ((0 == GNUNET_memcmp (merchant_pub, 584 &tl->details.refund->merchant_pub)) && 585 (0 == GNUNET_memcmp (h_contract_terms, 586 &tl->details.refund->h_contract_terms))) 587 { 588 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 589 "Detected applicable refund of %s\n", 590 TALER_amount2s (amount_with_fee)); 591 TALER_ARL_amount_add (&merchant_loss, 592 &merchant_loss, 593 amount_with_fee); 594 } 595 /* Check that the fees given in the transaction list and in dki match */ 596 if (0 != 597 TALER_amount_cmp (&issue->fees.refund, 598 fee_claimed)) 599 { 600 /* Disagreement in fee structure between exchange and auditor! */ 601 qs = report_amount_arithmetic_inconsistency ("refund fee", 602 0, 603 fee_claimed, 604 &issue->fees.refund, 605 1); 606 if (0 > qs) 607 return qs; 608 } 609 break; 610 } 611 case TALER_EXCHANGEDB_TT_RECOUP_REFRESH_RECEIVER: 612 { 613 const struct TALER_Amount *amount_with_fee; 614 615 amount_with_fee = &tl->details.old_coin_recoup->value; 616 /* We count recoups of refreshed coins like refunds for the dirty old 617 coin, as they equivalently _increase_ the remaining value on the 618 _old_ coin */ 619 TALER_ARL_amount_add (&refunds, 620 &refunds, 621 amount_with_fee); 622 break; 623 } 624 case TALER_EXCHANGEDB_TT_RECOUP_WITHDRAW: 625 { 626 const struct TALER_Amount *amount_with_fee; 627 628 /* We count recoups of the coin as expenditures, as it 629 equivalently decreases the remaining value of the recouped coin. */ 630 amount_with_fee = &tl->details.recoup->value; 631 TALER_ARL_amount_add (&expenditures, 632 &expenditures, 633 amount_with_fee); 634 break; 635 } 636 case TALER_EXCHANGEDB_TT_RECOUP_REFRESH: 637 { 638 const struct TALER_Amount *amount_with_fee; 639 640 /* We count recoups of the coin as expenditures, as it 641 equivalently decreases the remaining value of the recouped coin. */ 642 amount_with_fee = &tl->details.recoup_refresh->value; 643 TALER_ARL_amount_add (&expenditures, 644 &expenditures, 645 amount_with_fee); 646 break; 647 } 648 case TALER_EXCHANGEDB_TT_PURSE_DEPOSIT: 649 TALER_ARL_amount_add (&expenditures, 650 &expenditures, 651 &tl->details.purse_deposit->amount); 652 break; 653 case TALER_EXCHANGEDB_TT_PURSE_REFUND: 654 TALER_ARL_amount_add (&refunds, 655 &refunds, 656 &tl->details.purse_refund->refund_amount); 657 break; 658 case TALER_EXCHANGEDB_TT_RESERVE_OPEN: 659 { 660 const struct TALER_Amount *amount_with_fee; 661 662 amount_with_fee = &tl->details.reserve_open->coin_contribution; 663 TALER_ARL_amount_add (&expenditures, 664 &expenditures, 665 amount_with_fee); 666 break; 667 } 668 } /* switch (tl->type) */ 669 } /* for 'tl' */ 670 671 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 672 "Deposits for this aggregation (after fees) are %s\n", 673 TALER_amount2s (merchant_gain)); 674 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 675 "Aggregation loss due to refunds is %s\n", 676 TALER_amount2s (&merchant_loss)); 677 *deposit_gain = *merchant_gain; 678 if ((NULL != deposited) && 679 (NULL != deposit_fee) && 680 (0 == TALER_amount_cmp (&merchant_loss, 681 deposited))) 682 { 683 /* We had a /deposit operation AND /refund operations for the same 684 merchant and contract adding up to the total deposited value 685 including deposit fee. Thus, we should not subtract the /deposit fee 686 from the merchant gain (as it was also refunded). */ 687 TALER_ARL_amount_add (merchant_gain, 688 merchant_gain, 689 deposit_fee); 690 } 691 { 692 struct TALER_Amount final_gain; 693 694 if (TALER_ARL_SR_INVALID_NEGATIVE == 695 TALER_ARL_amount_subtract_neg (&final_gain, 696 merchant_gain, 697 &merchant_loss)) 698 { 699 /* refunds above deposits? Bad! */ 700 qs = report_coin_arithmetic_inconsistency ("refund (merchant)", 701 coin_pub, 702 merchant_gain, 703 &merchant_loss, 704 1); 705 if (0 > qs) 706 return qs; 707 /* For the overall aggregation, we should not count this 708 as a NEGATIVE contribution as that is not allowed; so 709 let's count it as zero as that's the best we can do. */ 710 GNUNET_assert (GNUNET_OK == 711 TALER_amount_set_zero (TALER_ARL_currency, 712 merchant_gain)); 713 } 714 else 715 { 716 *merchant_gain = final_gain; 717 } 718 } 719 720 721 /* Calculate total balance change, i.e. expenditures (recoup, deposit, refresh) 722 minus refunds (refunds, recoup-to-old) */ 723 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 724 "Subtracting refunds of %s from coin value loss\n", 725 TALER_amount2s (&refunds)); 726 if (TALER_ARL_SR_INVALID_NEGATIVE == 727 TALER_ARL_amount_subtract_neg (&spent, 728 &expenditures, 729 &refunds)) 730 { 731 /* refunds above expenditures? Bad! */ 732 qs = report_coin_arithmetic_inconsistency ("refund (balance)", 733 coin_pub, 734 &expenditures, 735 &refunds, 736 1); 737 if (0 > qs) 738 return qs; 739 } 740 else 741 { 742 /* Now check that 'spent' is less or equal than the total coin value */ 743 if (1 == TALER_amount_cmp (&spent, 744 &issue->value)) 745 { 746 /* spent > value */ 747 qs = report_coin_arithmetic_inconsistency ("spend", 748 coin_pub, 749 &spent, 750 &issue->value, 751 -1); 752 if (0 > qs) 753 return qs; 754 } 755 } 756 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 757 "Final merchant gain after refunds is %s\n", 758 TALER_amount2s (deposit_gain)); 759 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 760 "Coin %s contributes %s to contract %s\n", 761 TALER_B2S (coin_pub), 762 TALER_amount2s (merchant_gain), 763 GNUNET_h2s (&h_contract_terms->hash)); 764 return GNUNET_DB_STATUS_SUCCESS_ONE_RESULT; 765 } 766 767 768 /** 769 * Function called with the results of the lookup of the 770 * transaction data associated with a wire transfer identifier. 771 * 772 * @param[in,out] wcc a `struct WireCheckContext` 773 * @param rowid which row in the table is the information from (for diagnostics) 774 * @param merchant_pub public key of the merchant (should be same for all callbacks with the same @e cls) 775 * @param account_pay_uri where did we transfer the funds? 776 * @param h_payto hash over @a account_payto_uri as it is in the DB 777 * @param exchange_payto_uri which of the exchange's accounts was debited, 778 * `full_payto` NULL if not recorded; unused here 779 * @param exec_time execution time of the wire transfer (should be same for all callbacks with the same @e cls) 780 * @param h_contract_terms which proposal was this payment about 781 * @param denom_pub denomination of @a coin_pub 782 * @param coin_pub which public key was this payment about 783 * @param coin_value amount contributed by this coin in total (with fee), 784 * but excluding refunds by this coin 785 * @param deposit_fee applicable deposit fee for this coin, actual 786 * fees charged may differ if coin was refunded 787 */ 788 static void 789 wire_transfer_information_cb ( 790 struct WireCheckContext *wcc, 791 uint64_t rowid, 792 const struct TALER_MerchantPublicKeyP *merchant_pub, 793 const struct TALER_FullPayto account_pay_uri, 794 const struct TALER_FullPaytoHashP *h_payto, 795 const struct TALER_FullPayto exchange_payto_uri, 796 struct GNUNET_TIME_Timestamp exec_time, 797 const struct TALER_PrivateContractHashP *h_contract_terms, 798 const struct TALER_DenominationPublicKey *denom_pub, 799 const struct TALER_CoinSpendPublicKeyP *coin_pub, 800 const struct TALER_Amount *coin_value, 801 const struct TALER_Amount *deposit_fee) 802 { 803 const struct TALER_EXCHANGEDB_DenominationKeyInformation *issue; 804 struct TALER_Amount computed_value; 805 struct TALER_Amount total_deposit_without_refunds; 806 struct TALER_EXCHANGEDB_TransactionList *tl; 807 struct TALER_CoinPublicInfo coin; 808 enum GNUNET_DB_QueryStatus qs; 809 struct TALER_FullPaytoHashP hpt; 810 uint64_t etag_out; 811 812 if (0 > wcc->qs) 813 return; 814 TALER_full_payto_hash (account_pay_uri, 815 &hpt); 816 if (0 != 817 GNUNET_memcmp (&hpt, 818 h_payto)) 819 { 820 qs = report_row_inconsistency ("wire_targets", 821 rowid, 822 "h-payto does not match payto URI"); 823 if (0 > qs) 824 { 825 wcc->qs = qs; 826 return; 827 } 828 } 829 /* Obtain coin's transaction history */ 830 /* FIXME-Optimization: could use 'start' mechanism to only fetch 831 transactions we did not yet process, instead of going over them again and 832 again.*/ 833 834 { 835 struct TALER_Amount balance; 836 struct TALER_DenominationHashP h_denom_pub; 837 838 qs = TALER_EXCHANGEDB_get_coin_transactions (TALER_ARL_edb, 839 false, 840 coin_pub, 841 0, 842 0, 843 &etag_out, 844 &balance, 845 &h_denom_pub, 846 &tl); 847 } 848 if (0 > qs) 849 { 850 wcc->qs = qs; 851 TALER_EXCHANGEDB_free_coin_transaction_list (tl); 852 return; 853 } 854 if (NULL == tl) 855 { 856 qs = report_row_inconsistency ("aggregation", 857 rowid, 858 "no transaction history for coin claimed in aggregation"); 859 if (0 > qs) 860 wcc->qs = qs; 861 return; 862 } 863 qs = TALER_EXCHANGEDB_get_known_coin (TALER_ARL_edb, 864 coin_pub, 865 &coin); 866 if (0 > qs) 867 { 868 GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs); 869 wcc->qs = qs; 870 return; 871 } 872 if (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS == qs) 873 { 874 /* this should be a foreign key violation at this point! */ 875 qs = report_row_inconsistency ("aggregation", 876 rowid, 877 "could not get coin details for coin claimed in aggregation"); 878 if (0 > qs) 879 wcc->qs = qs; 880 TALER_EXCHANGEDB_free_coin_transaction_list (tl); 881 return; 882 } 883 qs = TALER_ARL_get_denomination_info_by_hash (&coin.denom_pub_hash, 884 &issue); 885 if (0 > qs) 886 { 887 wcc->qs = qs; 888 TALER_denom_sig_free (&coin.denom_sig); 889 TALER_EXCHANGEDB_free_coin_transaction_list (tl); 890 return; 891 } 892 if (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS == qs) 893 { 894 TALER_denom_sig_free (&coin.denom_sig); 895 TALER_EXCHANGEDB_free_coin_transaction_list (tl); 896 qs = report_row_inconsistency ("aggregation", 897 rowid, 898 "could not find denomination key for coin claimed in aggregation"); 899 if (0 > qs) 900 wcc->qs = qs; 901 return; 902 } 903 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 904 "Testing coin `%s' for validity\n", 905 TALER_B2S (&coin.coin_pub)); 906 if (GNUNET_OK != 907 TALER_test_coin_valid (&coin, 908 denom_pub)) 909 { 910 struct TALER_AUDITORDB_BadSigLosses bsl = { 911 .problem_row_id = rowid, 912 .operation = (char *) "wire", 913 .loss = *coin_value, 914 .operation_specific_pub = coin.coin_pub.eddsa_pub 915 }; 916 917 qs = TALER_AUDITORDB_insert_bad_sig_losses ( 918 TALER_ARL_adb, 919 &bsl); 920 if (qs < 0) 921 { 922 GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs); 923 wcc->qs = qs; 924 TALER_denom_sig_free (&coin.denom_sig); 925 TALER_EXCHANGEDB_free_coin_transaction_list (tl); 926 return; 927 } 928 TALER_ARL_amount_add (&TALER_ARL_USE_AB (aggregation_total_bad_sig_loss), 929 &TALER_ARL_USE_AB (aggregation_total_bad_sig_loss), 930 coin_value); 931 qs = report_row_inconsistency ("deposit", 932 rowid, 933 "coin denomination signature invalid"); 934 if (0 > qs) 935 { 936 wcc->qs = qs; 937 TALER_denom_sig_free (&coin.denom_sig); 938 TALER_EXCHANGEDB_free_coin_transaction_list (tl); 939 return; 940 } 941 } 942 TALER_denom_sig_free (&coin.denom_sig); 943 GNUNET_assert (NULL != issue); /* mostly to help static analysis */ 944 /* Check transaction history to see if it supports aggregate 945 valuation */ 946 qs = check_transaction_history_for_deposit ( 947 coin_pub, 948 h_contract_terms, 949 merchant_pub, 950 issue, 951 tl, 952 &computed_value, 953 &total_deposit_without_refunds); 954 if (0 > qs) 955 { 956 TALER_EXCHANGEDB_free_coin_transaction_list (tl); 957 wcc->qs = qs; 958 return; 959 } 960 TALER_EXCHANGEDB_free_coin_transaction_list (tl); 961 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 962 "Coin contributes %s to aggregate (deposits after fees and refunds)\n", 963 TALER_amount2s (&computed_value)); 964 { 965 struct TALER_Amount coin_value_without_fee; 966 967 if (TALER_ARL_SR_INVALID_NEGATIVE == 968 TALER_ARL_amount_subtract_neg (&coin_value_without_fee, 969 coin_value, 970 deposit_fee)) 971 { 972 qs = report_amount_arithmetic_inconsistency ( 973 "aggregation (fee structure)", 974 rowid, 975 coin_value, 976 deposit_fee, 977 -1); 978 if (0 > qs) 979 { 980 wcc->qs = qs; 981 return; 982 } 983 } 984 else if (0 != 985 TALER_amount_cmp (&total_deposit_without_refunds, 986 &coin_value_without_fee)) 987 { 988 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 989 "Expected coin contribution of %s to aggregate\n", 990 TALER_amount2s (&coin_value_without_fee)); 991 qs = report_amount_arithmetic_inconsistency ( 992 "aggregation (contribution)", 993 rowid, 994 &coin_value_without_fee, 995 &total_deposit_without_refunds, 996 -1); 997 if (0 > qs) 998 { 999 wcc->qs = qs; 1000 return; 1001 } 1002 } 1003 } 1004 /* Check other details of wire transfer match */ 1005 if (0 != TALER_full_payto_cmp (account_pay_uri, 1006 wcc->payto_uri)) 1007 { 1008 qs = report_row_inconsistency ("aggregation", 1009 rowid, 1010 "target of outgoing wire transfer do not match hash of wire from deposit"); 1011 if (0 > qs) 1012 { 1013 wcc->qs = qs; 1014 return; 1015 } 1016 } 1017 if (GNUNET_TIME_timestamp_cmp (exec_time, 1018 !=, 1019 wcc->date)) 1020 { 1021 /* This should be impossible from database constraints */ 1022 GNUNET_break (0); 1023 qs = report_row_inconsistency ("aggregation", 1024 rowid, 1025 "date given in aggregate does not match wire transfer date"); 1026 if (0 > qs) 1027 { 1028 wcc->qs = qs; 1029 return; 1030 } 1031 } 1032 1033 /* Add coin's contribution to total aggregate value */ 1034 { 1035 struct TALER_Amount res; 1036 1037 TALER_ARL_amount_add (&res, 1038 &wcc->total_deposits, 1039 &computed_value); 1040 wcc->total_deposits = res; 1041 } 1042 } 1043 1044 1045 /** 1046 * Lookup the wire fee that the exchange charges at @a timestamp. 1047 * 1048 * @param ac context for caching the result 1049 * @param method method of the wire plugin 1050 * @param timestamp time for which we need the fee 1051 * @return NULL on error (fee unknown) 1052 */ 1053 static const struct TALER_Amount * 1054 get_wire_fee (struct AggregationContext *ac, 1055 const char *method, 1056 struct GNUNET_TIME_Timestamp timestamp) 1057 { 1058 struct WireFeeInfo *wfi; 1059 struct WireFeeInfo *pos; 1060 struct TALER_MasterSignatureP master_sig; 1061 enum GNUNET_DB_QueryStatus qs; 1062 uint64_t rowid; 1063 1064 /* Check if fee is already loaded in cache */ 1065 for (pos = ac->fee_head; NULL != pos; pos = pos->next) 1066 { 1067 if (GNUNET_TIME_timestamp_cmp (pos->start_date, 1068 <=, 1069 timestamp) && 1070 GNUNET_TIME_timestamp_cmp (pos->end_date, 1071 >, 1072 timestamp)) 1073 return &pos->fees.wire; 1074 if (GNUNET_TIME_timestamp_cmp (pos->start_date, 1075 >, 1076 timestamp)) 1077 break; 1078 } 1079 1080 /* Lookup fee in exchange database */ 1081 wfi = GNUNET_new (struct WireFeeInfo); 1082 if (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT != 1083 TALER_EXCHANGEDB_get_wire_fee (TALER_ARL_edb, 1084 method, 1085 timestamp, 1086 &rowid, 1087 &wfi->start_date, 1088 &wfi->end_date, 1089 &wfi->fees, 1090 &master_sig)) 1091 { 1092 GNUNET_break (0); 1093 GNUNET_free (wfi); 1094 return NULL; 1095 } 1096 1097 /* Check signature. (This is not terribly meaningful as the exchange can 1098 easily make this one up, but it means that we have proof that the master 1099 key was used for inconsistent wire fees if a merchant complains.) */ 1100 if (GNUNET_OK != 1101 TALER_exchange_offline_wire_fee_verify ( 1102 method, 1103 wfi->start_date, 1104 wfi->end_date, 1105 &wfi->fees, 1106 &TALER_ARL_master_pub, 1107 &master_sig)) 1108 { 1109 ac->qs = report_row_inconsistency ("wire-fee", 1110 timestamp.abs_time.abs_value_us, 1111 "wire fee signature invalid at given time"); 1112 /* Note: continue with the fee despite the signature 1113 being invalid here; hopefully it is really only the 1114 signature that is bad ... */ 1115 } 1116 1117 /* Established fee, keep in sorted list */ 1118 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 1119 "Wire fee is %s starting at %s\n", 1120 TALER_amount2s (&wfi->fees.wire), 1121 GNUNET_TIME_timestamp2s (wfi->start_date)); 1122 if (NULL == pos) 1123 /* all cached fees start at or before @a timestamp, 1124 so the new fee belongs at the end of the list */ 1125 GNUNET_CONTAINER_DLL_insert_tail (ac->fee_head, 1126 ac->fee_tail, 1127 wfi); 1128 else if (NULL == pos->prev) 1129 GNUNET_CONTAINER_DLL_insert (ac->fee_head, 1130 ac->fee_tail, 1131 wfi); 1132 else 1133 GNUNET_CONTAINER_DLL_insert_after (ac->fee_head, 1134 ac->fee_tail, 1135 pos->prev, 1136 wfi); 1137 /* Check non-overlaping fee invariant */ 1138 if ((NULL != wfi->prev) && 1139 GNUNET_TIME_timestamp_cmp (wfi->prev->end_date, 1140 >, 1141 wfi->start_date)) 1142 { 1143 struct TALER_AUDITORDB_FeeTimeInconsistency ftib = { 1144 .problem_row_id = rowid, 1145 .diagnostic = (char *) "start date before previous end date", 1146 .time = wfi->start_date.abs_time, 1147 .type = (char *) method 1148 }; 1149 1150 qs = TALER_AUDITORDB_insert_fee_time_inconsistency ( 1151 TALER_ARL_adb, 1152 &ftib); 1153 if (qs < 0) 1154 { 1155 GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs); 1156 ac->qs = qs; 1157 return NULL; 1158 } 1159 } 1160 if ((NULL != wfi->next) && 1161 GNUNET_TIME_timestamp_cmp (wfi->next->start_date, 1162 <, 1163 wfi->end_date)) 1164 { 1165 struct TALER_AUDITORDB_FeeTimeInconsistency ftia = { 1166 .problem_row_id = rowid, 1167 .diagnostic = (char *) "end date after next start date", 1168 .time = wfi->end_date.abs_time, 1169 .type = (char *) method 1170 }; 1171 1172 qs = TALER_AUDITORDB_insert_fee_time_inconsistency ( 1173 TALER_ARL_adb, 1174 &ftia); 1175 1176 if (qs < 0) 1177 { 1178 GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs); 1179 ac->qs = qs; 1180 return NULL; 1181 } 1182 } 1183 return &wfi->fees.wire; 1184 } 1185 1186 1187 /** 1188 * Check that a wire transfer made by the exchange is valid 1189 * (has matching deposits). 1190 * 1191 * @param ac a `struct AggregationContext` 1192 * @param rowid identifier of the respective row in the database 1193 * @param date timestamp of the wire transfer (roughly) 1194 * @param wtid wire transfer subject 1195 * @param payto_uri bank account details of the receiver 1196 * @param amount amount that was wired 1197 * @return #GNUNET_OK to continue, #GNUNET_SYSERR to stop iteration 1198 */ 1199 static enum GNUNET_GenericReturnValue 1200 check_wire_out_cb (struct AggregationContext *ac, 1201 uint64_t rowid, 1202 struct GNUNET_TIME_Timestamp date, 1203 const struct TALER_WireTransferIdentifierRawP *wtid, 1204 const struct TALER_FullPayto payto_uri, 1205 const struct TALER_Amount *amount) 1206 { 1207 struct WireCheckContext wcc; 1208 struct TALER_Amount final_amount; 1209 struct TALER_Amount exchange_gain; 1210 enum GNUNET_DB_QueryStatus qs; 1211 char *method; 1212 1213 /* should be monotonically increasing */ 1214 GNUNET_assert (rowid >= 1215 TALER_ARL_USE_PP (aggregation_last_wire_out_serial_id)); 1216 TALER_ARL_USE_PP (aggregation_last_wire_out_serial_id) = rowid + 1; 1217 1218 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 1219 "Checking wire transfer %s over %s performed on %s\n", 1220 TALER_B2S (wtid), 1221 TALER_amount2s (amount), 1222 GNUNET_TIME_timestamp2s (date)); 1223 if (0 != strcasecmp (amount->currency, 1224 TALER_ARL_currency)) 1225 { 1226 /* Do not feed a foreign currency into our accumulators, 1227 as that would be fatal. */ 1228 qs = report_row_inconsistency ("wire_out", 1229 rowid, 1230 "wire transfer in unexpected currency"); 1231 if (0 > qs) 1232 ac->qs = qs; 1233 return GNUNET_OK; 1234 } 1235 if (NULL == (method = TALER_payto_get_method (payto_uri.full_payto))) 1236 { 1237 qs = report_row_inconsistency ("wire_out", 1238 rowid, 1239 "specified wire address lacks method"); 1240 if (0 > qs) 1241 ac->qs = qs; 1242 return GNUNET_OK; 1243 } 1244 1245 wcc.ac = ac; 1246 wcc.qs = GNUNET_DB_STATUS_SUCCESS_ONE_RESULT; 1247 wcc.date = date; 1248 GNUNET_assert (GNUNET_OK == 1249 TALER_amount_set_zero (TALER_ARL_currency, 1250 &wcc.total_deposits)); 1251 wcc.payto_uri = payto_uri; 1252 qs = TALER_EXCHANGEDB_iterate_wire_transfers (TALER_ARL_edb, 1253 wtid, 1254 &wire_transfer_information_cb, 1255 &wcc); 1256 if (0 > qs) 1257 { 1258 GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs); 1259 ac->qs = qs; 1260 GNUNET_free (method); 1261 return GNUNET_SYSERR; 1262 } 1263 if (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT != wcc.qs) 1264 { 1265 /* Note: detailed information was already logged 1266 in #wire_transfer_information_cb, so here we 1267 only log for debugging */ 1268 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 1269 "Inconsistency for wire_out %llu (WTID %s) detected\n", 1270 (unsigned long long) rowid, 1271 TALER_B2S (wtid)); 1272 if (0 > wcc.qs) 1273 { 1274 /* The coin loop was aborted by a database failure, so 1275 wcc.total_deposits only covers a prefix of the coins. Do not 1276 publish that truncated figure and do not advance the checkpoint 1277 past this row. */ 1278 GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == wcc.qs); 1279 ac->qs = wcc.qs; 1280 GNUNET_free (method); 1281 return GNUNET_SYSERR; 1282 } 1283 } 1284 1285 1286 /* Subtract aggregation fee from total (if possible) */ 1287 { 1288 const struct TALER_Amount *wire_fee; 1289 1290 wire_fee = get_wire_fee (ac, 1291 method, 1292 date); 1293 if (0 > ac->qs) 1294 { 1295 GNUNET_free (method); 1296 return GNUNET_SYSERR; 1297 } 1298 if (NULL == wire_fee) 1299 { 1300 qs = report_row_inconsistency ("wire-fee", 1301 date.abs_time.abs_value_us, 1302 "wire fee unavailable for given time"); 1303 if (qs < 0) 1304 { 1305 ac->qs = qs; 1306 GNUNET_free (method); 1307 return GNUNET_SYSERR; 1308 } 1309 /* If fee is unknown, we just assume the fee is zero */ 1310 final_amount = wcc.total_deposits; 1311 } 1312 else if (TALER_ARL_SR_INVALID_NEGATIVE == 1313 TALER_ARL_amount_subtract_neg (&final_amount, 1314 &wcc.total_deposits, 1315 wire_fee)) 1316 { 1317 qs = report_amount_arithmetic_inconsistency ( 1318 "wire out (fee structure)", 1319 rowid, 1320 &wcc.total_deposits, 1321 wire_fee, 1322 -1); 1323 /* If fee arithmetic fails, we just assume the fee is zero */ 1324 if (0 > qs) 1325 { 1326 ac->qs = qs; 1327 GNUNET_free (method); 1328 return GNUNET_SYSERR; 1329 } 1330 final_amount = wcc.total_deposits; 1331 } 1332 } 1333 GNUNET_free (method); 1334 1335 /* Round down to amount supported by wire method */ 1336 GNUNET_break (GNUNET_SYSERR != 1337 TALER_amount_round_down (&final_amount, 1338 &TALER_ARL_currency_round_unit)); 1339 1340 /* Calculate the exchange's gain as the fees plus rounding differences! */ 1341 TALER_ARL_amount_subtract (&exchange_gain, 1342 &wcc.total_deposits, 1343 &final_amount); 1344 1345 /* Sum up aggregation fees (we simply include the rounding gains) */ 1346 TALER_ARL_amount_add (&TALER_ARL_USE_AB (aggregation_total_wire_fee_revenue), 1347 &TALER_ARL_USE_AB (aggregation_total_wire_fee_revenue), 1348 &exchange_gain); 1349 1350 /* Check that calculated amount matches actual amount */ 1351 if (0 != TALER_amount_cmp (amount, 1352 &final_amount)) 1353 { 1354 struct TALER_Amount delta; 1355 1356 if (0 < TALER_amount_cmp (amount, 1357 &final_amount)) 1358 { 1359 /* amount > final_amount */ 1360 TALER_ARL_amount_subtract (&delta, 1361 amount, 1362 &final_amount); 1363 TALER_ARL_amount_add (&TALER_ARL_USE_AB ( 1364 aggregation_total_wire_out_delta_plus), 1365 &TALER_ARL_USE_AB ( 1366 aggregation_total_wire_out_delta_plus), 1367 &delta); 1368 } 1369 else 1370 { 1371 /* amount < final_amount */ 1372 TALER_ARL_amount_subtract (&delta, 1373 &final_amount, 1374 amount); 1375 TALER_ARL_amount_add (&TALER_ARL_USE_AB ( 1376 aggregation_total_wire_out_delta_minus), 1377 &TALER_ARL_USE_AB ( 1378 aggregation_total_wire_out_delta_minus), 1379 &delta); 1380 } 1381 1382 { 1383 struct TALER_AUDITORDB_WireOutInconsistency woi = { 1384 .destination_account = payto_uri, 1385 .diagnostic = (char *) "aggregated amount does not match expectations", 1386 .wire_out_row_id = rowid, 1387 .expected = final_amount, 1388 .claimed = *amount 1389 }; 1390 1391 qs = TALER_AUDITORDB_insert_wire_out_inconsistency ( 1392 TALER_ARL_adb, 1393 &woi); 1394 1395 if (qs < 0) 1396 { 1397 GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs); 1398 ac->qs = qs; 1399 return GNUNET_SYSERR; 1400 } 1401 } 1402 return GNUNET_OK; 1403 } 1404 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 1405 "Aggregation unit %s is OK\n", 1406 TALER_B2S (wtid)); 1407 return GNUNET_OK; 1408 } 1409 1410 1411 /** 1412 * Analyze the exchange aggregator's payment processing. 1413 * 1414 * @param cls closure 1415 * @return transaction status code 1416 */ 1417 static enum GNUNET_DB_QueryStatus 1418 analyze_aggregations (void *cls) 1419 { 1420 struct AggregationContext ac; 1421 struct WireFeeInfo *wfi; 1422 enum GNUNET_DB_QueryStatus qs; 1423 1424 (void) cls; 1425 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 1426 "Analyzing aggregations\n"); 1427 qs = TALER_AUDITORDB_get_auditor_progress ( 1428 TALER_ARL_adb, 1429 TALER_ARL_GET_PP (aggregation_last_wire_out_serial_id), 1430 NULL); 1431 if (0 > qs) 1432 { 1433 GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs); 1434 return qs; 1435 } 1436 if (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS == qs) 1437 { 1438 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE, 1439 "First analysis using this auditor, starting audit from scratch\n"); 1440 } 1441 else 1442 { 1443 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 1444 "Resuming aggregation audit at %llu\n", 1445 (unsigned long long) TALER_ARL_USE_PP ( 1446 aggregation_last_wire_out_serial_id)); 1447 } 1448 1449 memset (&ac, 1450 0, 1451 sizeof (ac)); 1452 qs = TALER_AUDITORDB_get_balance ( 1453 TALER_ARL_adb, 1454 TALER_ARL_GET_AB (aggregation_total_wire_fee_revenue), 1455 TALER_ARL_GET_AB (aggregation_total_arithmetic_delta_plus), 1456 TALER_ARL_GET_AB (aggregation_total_arithmetic_delta_minus), 1457 TALER_ARL_GET_AB (aggregation_total_bad_sig_loss), 1458 TALER_ARL_GET_AB (aggregation_total_wire_out_delta_plus), 1459 TALER_ARL_GET_AB (aggregation_total_wire_out_delta_minus), 1460 TALER_ARL_GET_AB (aggregation_total_coin_delta_plus), 1461 TALER_ARL_GET_AB (aggregation_total_coin_delta_minus), 1462 NULL); 1463 if (0 > qs) 1464 { 1465 GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs); 1466 return qs; 1467 } 1468 1469 ac.qs = GNUNET_DB_STATUS_SUCCESS_ONE_RESULT; 1470 qs = TALER_EXCHANGEDB_iterate_wire_outs_above_serial_id ( 1471 TALER_ARL_edb, 1472 TALER_ARL_USE_PP (aggregation_last_wire_out_serial_id), 1473 &check_wire_out_cb, 1474 &ac); 1475 if (0 > qs) 1476 { 1477 GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs); 1478 ac.qs = qs; 1479 } 1480 while (NULL != (wfi = ac.fee_head)) 1481 { 1482 GNUNET_CONTAINER_DLL_remove (ac.fee_head, 1483 ac.fee_tail, 1484 wfi); 1485 GNUNET_free (wfi); 1486 } 1487 if (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS == qs) 1488 { 1489 /* there were no wire out entries to be looked at, we are done */ 1490 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 1491 "No wire out entries found\n"); 1492 return qs; 1493 } 1494 if (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT != ac.qs) 1495 { 1496 GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == ac.qs); 1497 return ac.qs; 1498 } 1499 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 1500 "Finished aggregation audit at %llu\n", 1501 (unsigned long long) TALER_ARL_USE_PP ( 1502 aggregation_last_wire_out_serial_id)); 1503 qs = TALER_AUDITORDB_insert_balance ( 1504 TALER_ARL_adb, 1505 TALER_ARL_SET_AB (aggregation_total_wire_fee_revenue), 1506 TALER_ARL_SET_AB (aggregation_total_arithmetic_delta_plus), 1507 TALER_ARL_SET_AB (aggregation_total_arithmetic_delta_minus), 1508 TALER_ARL_SET_AB (aggregation_total_bad_sig_loss), 1509 TALER_ARL_SET_AB (aggregation_total_wire_out_delta_plus), 1510 TALER_ARL_SET_AB (aggregation_total_wire_out_delta_minus), 1511 TALER_ARL_SET_AB (aggregation_total_coin_delta_plus), 1512 TALER_ARL_SET_AB (aggregation_total_coin_delta_minus), 1513 NULL); 1514 if (0 > qs) 1515 { 1516 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 1517 "Failed to update auditor DB, not recording progress\n"); 1518 GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs); 1519 return qs; 1520 } 1521 1522 qs = TALER_AUDITORDB_insert_auditor_progress ( 1523 TALER_ARL_adb, 1524 TALER_ARL_SET_PP (aggregation_last_wire_out_serial_id), 1525 NULL); 1526 if (0 > qs) 1527 { 1528 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 1529 "Failed to update auditor DB, not recording progress\n"); 1530 GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs); 1531 return qs; 1532 } 1533 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 1534 "Concluded aggregation audit step at %llu\n", 1535 (unsigned long long) TALER_ARL_USE_PP ( 1536 aggregation_last_wire_out_serial_id)); 1537 1538 return GNUNET_DB_STATUS_SUCCESS_ONE_RESULT; 1539 } 1540 1541 1542 /** 1543 * Function called on events received from Postgres. 1544 * 1545 * @param cls closure, NULL 1546 * @param extra additional event data provided 1547 * @param extra_size number of bytes in @a extra 1548 */ 1549 static void 1550 db_notify (void *cls, 1551 const void *extra, 1552 size_t extra_size) 1553 { 1554 (void) cls; 1555 (void) extra; 1556 (void) extra_size; 1557 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 1558 "Received notification to wake aggregation helper\n"); 1559 if (GNUNET_OK != 1560 TALER_ARL_setup_sessions_and_run (&analyze_aggregations, 1561 NULL)) 1562 { 1563 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 1564 "Audit failed\n"); 1565 GNUNET_SCHEDULER_shutdown (); 1566 global_ret = EXIT_FAILURE; 1567 return; 1568 } 1569 } 1570 1571 1572 /** 1573 * Function called on shutdown. 1574 */ 1575 static void 1576 do_shutdown (void *cls) 1577 { 1578 (void) cls; 1579 if (NULL != eh) 1580 { 1581 TALER_AUDITORDB_event_listen_cancel (eh); 1582 eh = NULL; 1583 } 1584 TALER_ARL_done (); 1585 } 1586 1587 1588 /** 1589 * Main function that will be run. 1590 * 1591 * @param cls closure 1592 * @param args remaining command-line arguments 1593 * @param cfgfile name of the configuration file used (for saving, can be NULL!) 1594 * @param c configuration 1595 */ 1596 static void 1597 run (void *cls, 1598 char *const *args, 1599 const char *cfgfile, 1600 const struct GNUNET_CONFIGURATION_Handle *c) 1601 { 1602 (void) cls; 1603 (void) args; 1604 (void) cfgfile; 1605 1606 cfg = c; 1607 GNUNET_SCHEDULER_add_shutdown (&do_shutdown, 1608 NULL); 1609 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 1610 "Launching aggregation auditor\n"); 1611 if (EXIT_SUCCESS != 1612 (global_ret = TALER_ARL_init (c))) 1613 { 1614 return; 1615 } 1616 1617 if (test_mode != 1) 1618 { 1619 struct GNUNET_DB_EventHeaderP es = { 1620 .size = htons (sizeof (es)), 1621 .type = htons (TALER_DBEVENT_EXCHANGE_AUDITOR_WAKE_HELPER_AGGREGATION) 1622 }; 1623 1624 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 1625 "Running helper indefinitely\n"); 1626 eh = TALER_AUDITORDB_event_listen (TALER_ARL_adb, 1627 &es, 1628 GNUNET_TIME_UNIT_FOREVER_REL, 1629 &db_notify, 1630 NULL); 1631 } 1632 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 1633 "Starting audit\n"); 1634 if (GNUNET_OK != 1635 TALER_ARL_setup_sessions_and_run (&analyze_aggregations, 1636 NULL)) 1637 { 1638 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 1639 "Audit failed\n"); 1640 GNUNET_SCHEDULER_shutdown (); 1641 global_ret = EXIT_FAILURE; 1642 return; 1643 } 1644 } 1645 1646 1647 /** 1648 * The main function to audit the exchange's aggregation processing. 1649 * 1650 * @param argc number of arguments from the command line 1651 * @param argv command line arguments 1652 * @return 0 ok, 1 on error 1653 */ 1654 int 1655 main (int argc, 1656 char *const *argv) 1657 { 1658 const struct GNUNET_GETOPT_CommandLineOption options[] = { 1659 GNUNET_GETOPT_option_flag ('i', 1660 "internal", 1661 "perform checks only applicable for exchange-internal audits", 1662 &internal_checks), 1663 GNUNET_GETOPT_option_flag ('t', 1664 "test", 1665 "run in test mode and exit when idle", 1666 &test_mode), 1667 GNUNET_GETOPT_option_timetravel ('T', 1668 "timetravel"), 1669 GNUNET_GETOPT_OPTION_END 1670 }; 1671 enum GNUNET_GenericReturnValue ret; 1672 1673 ret = GNUNET_PROGRAM_run ( 1674 TALER_AUDITOR_project_data (), 1675 argc, 1676 argv, 1677 "taler-helper-auditor-aggregation", 1678 gettext_noop ("Audit Taler exchange aggregation activity"), 1679 options, 1680 &run, 1681 NULL); 1682 if (GNUNET_SYSERR == ret) 1683 return EXIT_NOTCONFIGURED; 1684 if (GNUNET_NO == ret) 1685 return EXIT_SUCCESS; 1686 return global_ret; 1687 } 1688 1689 1690 /* end of taler-helper-auditor-aggregation.c */