auditordb_lib.h (16569B)
1 /* 2 This file is part of TALER 3 Copyright (C) 2016 Taler Systems SA 4 5 TALER is free software; you can redistribute it and/or modify it under the 6 terms of the GNU General 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 General Public License for more details. 12 13 You should have received a copy of the GNU General Public License along with 14 TALER; see the file COPYING. If not, see <http://www.gnu.org/licenses/> 15 */ 16 /** 17 * @file include/auditordb_lib.h 18 * @brief high-level interface for the auditor's database 19 * @author Florian Dold 20 * @author Benedikt Mueller 21 * @author Christian Grothoff 22 */ 23 #ifndef TALER_AUDITORDB_LIB_H 24 #define TALER_AUDITORDB_LIB_H 25 26 #include <jansson.h> 27 #include <gnunet/gnunet_util_lib.h> 28 #include <gnunet/gnunet_db_lib.h> 29 #include <taler/taler_util.h> 30 #include <auditordb_lib.h> 31 32 /** 33 * Connection to the postgres database of the auditor. 34 */ 35 struct TALER_AUDITORDB_PostgresContext; 36 37 38 /** 39 * Information about a deposit confirmation we received from 40 * a merchant. 41 */ 42 struct TALER_AUDITORDB_DepositConfirmation 43 { 44 45 /** 46 * Hash over the contract for which this deposit is made. 47 */ 48 struct TALER_PrivateContractHashP h_contract_terms; 49 50 /** 51 * Hash over the policy extension for the deposit. 52 */ 53 struct TALER_ExtensionPolicyHashP h_policy; 54 55 /** 56 * Hash over the wiring information of the merchant. 57 */ 58 struct TALER_MerchantWireHashP h_wire; 59 60 /** 61 * Time when this deposit confirmation was generated by the exchange. 62 */ 63 struct GNUNET_TIME_Timestamp exchange_timestamp; 64 65 /** 66 * How much time does the @e merchant have to issue a refund 67 * request? Zero if refunds are not allowed. After this time, the 68 * coin cannot be refunded. Note that the wire transfer will not be 69 * performed by the exchange until the refund deadline. This value 70 * is taken from the original deposit request. 71 */ 72 struct GNUNET_TIME_Timestamp refund_deadline; 73 74 /** 75 * How much time does the @e exchange have to wire the funds? 76 */ 77 struct GNUNET_TIME_Timestamp wire_deadline; 78 79 /** 80 * Amount to be deposited, excluding fee. Calculated from the 81 * amount with fee and the fee from the deposit request. 82 */ 83 struct TALER_Amount total_without_fee; 84 85 /** 86 * Array of the coin public keys involved in the 87 * batch deposit operation. 88 */ 89 const struct TALER_CoinSpendPublicKeyP *coin_pubs; 90 91 /** 92 * Array of coin deposit signatures from the deposit operation. 93 */ 94 const struct TALER_CoinSpendSignatureP *coin_sigs; 95 96 /** 97 * The Merchant's public key. Allows the merchant to later refund 98 * the transaction or to inquire about the wire transfer identifier. 99 */ 100 struct TALER_MerchantPublicKeyP merchant; 101 102 /** 103 * Signature from the exchange of type 104 * #TALER_SIGNATURE_EXCHANGE_CONFIRM_DEPOSIT. 105 */ 106 struct TALER_ExchangeSignatureP exchange_sig; 107 108 /** 109 * Public signing key from the exchange matching @e exchange_sig. 110 */ 111 struct TALER_ExchangePublicKeyP exchange_pub; 112 113 /** 114 * Exchange master signature over @e exchange_sig. 115 */ 116 struct TALER_MasterSignatureP master_sig; 117 118 /** 119 * Row of this entry in the auditor database. 120 */ 121 uint64_t row_id; 122 123 /** 124 * Length of the @e coin_pubs and @e coin_sigs arrays. 125 */ 126 unsigned int num_coins; 127 128 bool suppressed; 129 130 }; 131 132 133 /** 134 * Information about a row inconsistency 135 */ 136 struct TALER_AUDITORDB_Generic_Update 137 { 138 uint64_t row_id; 139 bool suppressed; 140 bool ancient; 141 }; 142 143 144 /** 145 * Information about a coin inconsistency 146 */ 147 struct TALER_AUDITORDB_CoinInconsistency 148 { 149 uint64_t row_id; 150 char *operation; 151 struct TALER_Amount exchange_amount; 152 struct TALER_Amount auditor_amount; 153 struct GNUNET_CRYPTO_EddsaPublicKey coin_pub; 154 bool profitable; 155 bool suppressed; 156 }; 157 158 /** 159 * Information about a row inconsistency 160 */ 161 struct TALER_AUDITORDB_RowInconsistency 162 { 163 uint64_t row_id; 164 char *row_table; 165 char *diagnostic; 166 bool suppressed; 167 }; 168 169 /** 170 * Information about a bad sig loss 171 */ 172 struct TALER_AUDITORDB_BadSigLosses 173 { 174 uint64_t row_id; 175 uint64_t problem_row_id; 176 char *operation; 177 struct TALER_Amount loss; 178 struct GNUNET_CRYPTO_EddsaPublicKey operation_specific_pub; 179 bool suppressed; 180 }; 181 182 /** 183 * Information about a closure lags 184 */ 185 struct TALER_AUDITORDB_ClosureLags 186 { 187 uint64_t row_id; 188 uint64_t problem_row_id; 189 struct TALER_Amount amount; 190 struct GNUNET_TIME_Absolute deadline; 191 struct TALER_WireTransferIdentifierRawP wtid; 192 struct TALER_FullPayto account; 193 bool suppressed; 194 }; 195 196 /** 197 * Information about a emergency 198 */ 199 struct TALER_AUDITORDB_Emergency 200 { 201 uint64_t row_id; 202 struct TALER_DenominationHashP denompub_h; 203 struct TALER_Amount denom_risk; 204 struct TALER_Amount denom_loss; 205 struct GNUNET_TIME_Absolute deposit_start; 206 struct GNUNET_TIME_Absolute deposit_end; 207 struct TALER_Amount value; 208 bool suppressed; 209 }; 210 211 /** 212 * Information about an emergency by count 213 */ 214 struct TALER_AUDITORDB_EmergenciesByCount 215 { 216 uint64_t row_id; 217 struct TALER_DenominationHashP denompub_h; 218 uint64_t num_issued; 219 uint64_t num_known; 220 struct TALER_Amount risk; 221 struct GNUNET_TIME_Absolute start; 222 struct GNUNET_TIME_Absolute deposit_end; 223 struct TALER_Amount value; 224 bool suppressed; 225 }; 226 227 /** 228 * Information about a fee time inconsistency 229 */ 230 struct TALER_AUDITORDB_FeeTimeInconsistency 231 { 232 uint64_t row_id; 233 uint64_t problem_row_id; 234 char *type; 235 struct GNUNET_TIME_Absolute time; 236 char *diagnostic; 237 bool suppressed; 238 }; 239 240 /** 241 * Information about a denom key validity withdraw inconsistency 242 */ 243 struct TALER_AUDITORDB_DenominationKeyValidityWithdrawInconsistency 244 { 245 uint64_t row_id; 246 uint64_t problem_row_id; 247 struct GNUNET_TIME_Absolute execution_date; 248 struct TALER_ReservePublicKeyP reserve_pub; 249 struct TALER_DenominationHashP denompub_h; 250 bool suppressed; 251 }; 252 253 /** 254 * Information about a purse not closed inconsistencies 255 */ 256 struct TALER_AUDITORDB_PurseNotClosedInconsistencies 257 { 258 uint64_t row_id; 259 struct GNUNET_CRYPTO_EddsaPublicKey purse_pub; 260 struct TALER_Amount amount; 261 struct GNUNET_TIME_Absolute expiration_date; 262 bool suppressed; 263 }; 264 265 /** 266 * Information about a reserve balance insufficient inconsistency 267 */ 268 struct TALER_AUDITORDB_ReserveBalanceInsufficientInconsistency 269 { 270 uint64_t row_id; 271 struct GNUNET_CRYPTO_EddsaPublicKey reserve_pub; 272 bool inconsistency_gain; 273 struct TALER_Amount inconsistency_amount; 274 bool suppressed; 275 }; 276 277 /** 278 * Information about a reserve in inconsistency 279 */ 280 struct TALER_AUDITORDB_ReserveInInconsistency 281 { 282 uint64_t serial_id; 283 uint64_t bank_row_id; 284 struct TALER_Amount amount_exchange_expected; 285 struct TALER_Amount amount_wired; 286 struct TALER_ReservePublicKeyP reserve_pub; 287 struct GNUNET_TIME_Absolute timestamp; 288 struct TALER_FullPayto account; 289 char *diagnostic; 290 bool suppressed; 291 292 }; 293 294 /** 295 * Information about a disagreement between the bank and the exchange 296 * about an incoming wire transfer that established an account key for 297 * KYC authentication. 298 */ 299 struct TALER_AUDITORDB_KycauthInInconsistency 300 { 301 /** 302 * Row of the finding in our own table. 303 */ 304 uint64_t serial_id; 305 306 /** 307 * Row of the wire transfer in the bank's credit history. This is what 308 * the two sides are matched on: the exchange stores it in 309 * `kycauths_in.wire_reference`. 310 */ 311 uint64_t bank_row_id; 312 313 /** 314 * Amount the exchange claims to have received, zero if the exchange 315 * does not know the transfer at all. 316 */ 317 struct TALER_Amount amount_exchange_expected; 318 319 /** 320 * Amount the bank says it transferred, zero if the bank does not 321 * report the transfer at all. 322 */ 323 struct TALER_Amount amount_wired; 324 325 /** 326 * Account public key the transfer established, as known to whichever 327 * side did report the transfer. 328 */ 329 union TALER_AccountPublicKeyP account_pub; 330 331 /** 332 * When the transfer was executed. 333 */ 334 struct GNUNET_TIME_Absolute timestamp; 335 336 /** 337 * Bank account that was debited, as known to whichever side did report 338 * the transfer. 339 */ 340 struct TALER_FullPayto account; 341 342 /** 343 * Human-readable description of what does not match. 344 */ 345 char *diagnostic; 346 347 bool suppressed; 348 349 }; 350 351 /** 352 * Balance values for a reserve (or all reserves). 353 */ 354 struct TALER_AUDITORDB_ReserveFeeBalance 355 { 356 /** 357 * Remaining funds. 358 */ 359 struct TALER_Amount reserve_balance; 360 361 /** 362 * Losses from operations that should not have 363 * happened (e.g. negative balance). 364 */ 365 struct TALER_Amount reserve_loss; 366 367 /** 368 * Fees charged for withdraw. 369 */ 370 struct TALER_Amount withdraw_fee_balance; 371 372 /** 373 * Fees charged for closing. 374 */ 375 struct TALER_Amount close_fee_balance; 376 377 /** 378 * Fees charged for purse creation. 379 */ 380 struct TALER_Amount purse_fee_balance; 381 382 /** 383 * Opening fees charged. 384 */ 385 struct TALER_Amount open_fee_balance; 386 387 /** 388 * History fees charged. 389 */ 390 struct TALER_Amount history_fee_balance; 391 }; 392 393 394 /** 395 * Balance data for denominations in circulation. 396 */ 397 struct TALER_AUDITORDB_DenominationCirculationData 398 { 399 /** 400 * Amount of outstanding coins in circulation. 401 */ 402 struct TALER_Amount denom_balance; 403 404 /** 405 * Amount lost due coins illicitly accepted (effectively, a 406 * negative @a denom_balance). 407 */ 408 struct TALER_Amount denom_loss; 409 410 /** 411 * Total amount that could still be theoretically lost in the future due to 412 * recoup operations. (Total put into circulation minus @e recoup_loss). 413 */ 414 struct TALER_Amount denom_risk; 415 416 /** 417 * Amount lost due to recoups. 418 */ 419 struct TALER_Amount recoup_loss; 420 421 /** 422 * Number of coins of this denomination that the exchange signed into 423 * existence. 424 */ 425 uint64_t num_issued; 426 }; 427 428 struct TALER_AUDITORDB_DenominationsWithoutSigs 429 { 430 uint64_t row_id; 431 struct TALER_DenominationHashP denompub_h; 432 struct TALER_Amount value; 433 struct GNUNET_TIME_Absolute start_time; 434 struct GNUNET_TIME_Absolute end_time; 435 bool suppressed; 436 }; 437 438 struct TALER_AUDITORDB_MisattributionInInconsistency 439 { 440 uint64_t row_id; 441 struct TALER_Amount amount; 442 uint64_t bank_row; 443 struct TALER_ReservePublicKeyP reserve_pub; 444 bool suppressed; 445 446 }; 447 448 struct TALER_AUDITORDB_DenominationPending 449 { 450 uint64_t row_id; 451 struct TALER_DenominationHashP denom_pub_hash; 452 struct TALER_Amount denom_balance; 453 struct TALER_Amount denom_loss; 454 uint64_t num_issued; 455 struct TALER_Amount denom_risk; 456 struct TALER_Amount recoup_loss; 457 bool suppressed; 458 459 }; 460 461 struct TALER_AUDITORDB_HistoricReserveSummary 462 { 463 uint64_t row_id; 464 struct GNUNET_TIME_Absolute start_date; 465 struct GNUNET_TIME_Absolute end_date; 466 struct TALER_Amount reserve_profits; 467 bool suppressed; 468 469 }; 470 471 struct TALER_AUDITORDB_WireFormatInconsistency 472 { 473 uint64_t row_id; 474 struct TALER_Amount amount; 475 uint64_t wire_offset; 476 char *diagnostic; 477 bool suppressed; 478 479 }; 480 481 struct TALER_AUDITORDB_WireOutInconsistency 482 { 483 uint64_t row_id; 484 struct TALER_FullPayto destination_account; 485 char *diagnostic; 486 uint64_t wire_out_row_id; 487 struct TALER_Amount expected; 488 struct TALER_Amount claimed; 489 bool suppressed; 490 491 }; 492 493 struct TALER_AUDITORDB_RowMinorInconsistencies 494 { 495 uint64_t row_id; 496 char *row_table; 497 uint64_t problem_row; 498 char *diagnostic; 499 bool suppressed; 500 }; 501 502 503 struct TALER_AUDITORDB_ReserveBalanceSummaryWrongInconsistency 504 { 505 uint64_t row_id; 506 struct TALER_ReservePublicKeyP reserve_pub; 507 struct TALER_Amount exchange_amount; 508 struct TALER_Amount auditor_amount; 509 bool suppressed; 510 511 }; 512 513 struct TALER_AUDITORDB_ReserveNotClosedInconsistency 514 { 515 uint64_t row_id; 516 struct TALER_ReservePublicKeyP reserve_pub; 517 struct TALER_Amount balance; 518 struct GNUNET_TIME_Absolute expiration_time; 519 char *diagnostic; 520 bool suppressed; 521 522 }; 523 524 525 enum TALER_AUDITORDB_DeletableSuppressableTables 526 { 527 /** 528 * For auditor_amount_arithmetic_inconsistency table. 529 */ 530 TALER_AUDITORDB_AMOUNT_ARITHMETIC_INCONSISTENCY, 531 532 /** 533 * For auditor_closure_lags table. 534 */ 535 TALER_AUDITORDB_CLOSURE_LAGS, 536 537 /** 538 * For auditor_bad_sig_losses table. 539 */ 540 TALER_AUDITORDB_BAD_SIG_LOSSES, 541 542 /** 543 * For auditor_coin_inconsistency table. 544 */ 545 TALER_AUDITORDB_COIN_INCONSISTENCY, 546 547 /** 548 * For auditor_denomination_key_validity_withdraw_inconsistency table. 549 */ 550 TALER_AUDITORDB_DENOMINATION_KEY_VALIDITY_WITHDRAW_INCONSISTENCY, 551 552 /** 553 * For auditor_denomination_pending table. 554 */ 555 TALER_AUDITORDB_DENOMINATION_PENDING, 556 557 /** 558 * For auditor_denominations_without_sig table. 559 */ 560 TALER_AUDITORDB_DENOMINATIONS_WITHOUT_SIG, 561 562 /** 563 * For auditor_deposit_confirmation table. 564 */ 565 TALER_AUDITORDB_DEPOSIT_CONFIRMATION, 566 567 /** 568 * For auditor_emergency table. 569 */ 570 TALER_AUDITORDB_EMERGENCY, 571 572 /** 573 * For auditor_emergency_by_count table. 574 */ 575 TALER_AUDITORDB_EMERGENCY_BY_COUNT, 576 577 /** 578 * For auditor_fee_time_inconsistency table. 579 */ 580 TALER_AUDITORDB_FEE_TIME_INCONSISTENCY, 581 582 /** 583 * For auditor_misattribution_in_inconsistency table. 584 */ 585 TALER_AUDITORDB_MISATTRIBUTION_IN_INCONSISTENCY, 586 587 /** 588 * For auditor_purse_not_closed_inconsistency table. 589 */ 590 TALER_AUDITORDB_PURSE_NOT_CLOSED_INCONSISTENCY, 591 592 /** 593 * For auditor_refreshes_hanging table. 594 */ 595 TALER_AUDITORDB_REFRESHES_HANGING, 596 597 /** 598 * For auditor_reserve_balance_insufficient_inconsistency table. 599 */ 600 TALER_AUDITORDB_RESERVE_BALANCE_INSUFFICIENT_INCONSISTENCY, 601 602 /** 603 * For auditor_reserve_balance_summary_wrong_inconsistency table. 604 */ 605 TALER_AUDITORDB_RESERVE_BALANCE_SUMMARY_WRONG_INCONSISTENCY, 606 607 /** 608 * For auditor_reserve_in_inconsistency table. 609 */ 610 TALER_AUDITORDB_RESERVE_IN_INCONSISTENCY, 611 612 /** 613 * For auditor_reserve_not_closed_inconsistency table. 614 */ 615 TALER_AUDITORDB_RESERVE_NOT_CLOSED_INCONSISTENCY, 616 617 /** 618 * For auditor_row_inconsistency table. 619 */ 620 TALER_AUDITORDB_ROW_INCONSISTENCY, 621 622 /** 623 * For auditor_row_minor_inconsistency table. 624 */ 625 TALER_AUDITORDB_ROW_MINOR_INCONSISTENCY, 626 627 /** 628 * For auditor_wire_format_inconsistency table. 629 */ 630 TALER_AUDITORDB_WIRE_FORMAT_INCONSISTENCY, 631 632 /** 633 * For auditor_wire_out_inconsistency table. 634 */ 635 TALER_AUDITORDB_WIRE_OUT_INCONSISTENCY, 636 637 /** 638 * For auditor_kycauth_in_inconsistency table. Note that new values 639 * must be appended here rather than inserted alphabetically: the 640 * numbering is what `TALER_AUDITORDB_get_deletable_suppressable_table_name()` 641 * indexes its table with. 642 */ 643 TALER_AUDITORDB_KYCAUTH_IN_INCONSISTENCY, 644 645 /** 646 * For auditor_aml_holds table. 647 */ 648 TALER_AUDITORDB_AML_HOLDS, 649 650 /** 651 * Terminal. 652 */ 653 TALER_AUDITORDB_DELETABLESUPPRESSABLE_TABLES_MAX 654 }; 655 656 657 /** 658 * Information about a wire transfer the exchange aggregated but did not 659 * execute. 660 */ 661 struct TALER_AUDITORDB_AmlHold 662 { 663 uint64_t row_id; 664 665 /** 666 * Wire transfer the deposits were aggregated into. 667 */ 668 struct TALER_WireTransferIdentifierRawP wtid; 669 670 /** 671 * Account the transfer should be made to. 672 */ 673 struct TALER_FullPaytoHashP wire_target_h_payto; 674 675 /** 676 * Payto URI of @e wire_target_h_payto. 677 */ 678 struct TALER_FullPayto account; 679 680 /** 681 * What the exchange still owes on this transfer. 682 */ 683 struct TALER_Amount amount; 684 685 /** 686 * Why the exchange says it has not made the transfer, an 687 * `enum TALER_EXCHANGEDB_DeferralReason` value taken from its 688 * `aggregation_deferrals` row. Zero if the exchange gave no reason at all, 689 * which is the case that says it is simply not paying. 690 */ 691 uint32_t deferral_reason; 692 693 /** 694 * Measure the exchange named as blocking the payout, or 0 if it named none. 695 */ 696 uint64_t legitimization_measure_serial_id; 697 698 /** 699 * When did the auditor first see this transfer being held? 700 */ 701 struct GNUNET_TIME_Absolute creation_date; 702 703 bool suppressed; 704 }; 705 706 707 /** 708 * Information about an arithmetic inconsistency 709 */ 710 struct TALER_AUDITORDB_AmountArithmeticInconsistency 711 { 712 uint64_t row_id; 713 uint64_t problem_row_id; 714 char *operation; 715 struct TALER_Amount exchange_amount; 716 struct TALER_Amount auditor_amount; 717 bool profitable; 718 bool suppressed; 719 }; 720 721 722 /** 723 * Initialize database connection. 724 * 725 * @param cfg configuration to use 726 * @return NULL on failure 727 */ 728 struct TALER_AUDITORDB_PostgresContext * 729 TALER_AUDITORDB_connect (const struct GNUNET_CONFIGURATION_Handle *cfg); 730 731 732 /** 733 * Shutdown the database connection. 734 * 735 * @param[in] pg connection to disconnect 736 */ 737 void 738 TALER_AUDITORDB_disconnect (struct TALER_AUDITORDB_PostgresContext *pg); 739 740 741 #endif