exchange

Base system with REST service to issue digital coins, run by the payment service provider
Log | Files | Refs | Submodules | README | LICENSE

auditordb_lib.h (16605B)


      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 };
    156 
    157 /**
    158  * Information about a row inconsistency
    159  */
    160 struct TALER_AUDITORDB_RowInconsistency
    161 {
    162   uint64_t row_id;
    163   char *row_table;
    164   char *diagnostic;
    165   bool suppressed;
    166 };
    167 
    168 /**
    169  * Information about a bad sig loss
    170  */
    171 struct TALER_AUDITORDB_BadSigLosses
    172 {
    173   uint64_t row_id;
    174   uint64_t problem_row_id;
    175   char *operation;
    176   struct TALER_Amount loss;
    177   struct GNUNET_CRYPTO_EddsaPublicKey operation_specific_pub;
    178   bool suppressed;
    179 };
    180 
    181 /**
    182  * Information about a closure lags
    183  */
    184 struct TALER_AUDITORDB_ClosureLags
    185 {
    186   uint64_t row_id;
    187   uint64_t problem_row_id;
    188   struct TALER_Amount amount;
    189   struct GNUNET_TIME_Absolute deadline;
    190   struct TALER_WireTransferIdentifierRawP wtid;
    191   struct TALER_FullPayto account;
    192   bool suppressed;
    193 };
    194 
    195 /**
    196  * Information about a emergency
    197  */
    198 struct TALER_AUDITORDB_Emergency
    199 {
    200   uint64_t row_id;
    201   struct TALER_DenominationHashP denompub_h;
    202   struct TALER_Amount denom_risk;
    203   struct TALER_Amount denom_loss;
    204   struct GNUNET_TIME_Absolute deposit_start;
    205   struct GNUNET_TIME_Absolute deposit_end;
    206   struct TALER_Amount value;
    207   bool suppressed;
    208 };
    209 
    210 /**
    211  * Information about an emergency by count
    212  */
    213 struct TALER_AUDITORDB_EmergenciesByCount
    214 {
    215   uint64_t row_id;
    216   struct TALER_DenominationHashP denompub_h;
    217   uint64_t num_issued;
    218   uint64_t num_known;
    219   struct TALER_Amount risk;
    220   struct GNUNET_TIME_Absolute start;
    221   struct GNUNET_TIME_Absolute deposit_end;
    222   struct TALER_Amount value;
    223   bool suppressed;
    224 };
    225 
    226 /**
    227  * Information about a fee time inconsistency
    228  */
    229 struct TALER_AUDITORDB_FeeTimeInconsistency
    230 {
    231   uint64_t row_id;
    232   uint64_t problem_row_id;
    233   char *type;
    234   struct GNUNET_TIME_Absolute time;
    235   char *diagnostic;
    236 };
    237 
    238 /**
    239  * Information about a denom key validity withdraw inconsistency
    240  */
    241 struct TALER_AUDITORDB_DenominationKeyValidityWithdrawInconsistency
    242 {
    243   uint64_t row_id;
    244   uint64_t problem_row_id;
    245   struct GNUNET_TIME_Absolute execution_date;
    246   struct TALER_ReservePublicKeyP reserve_pub;
    247   struct TALER_DenominationHashP denompub_h;
    248   bool suppressed;
    249 };
    250 
    251 /**
    252  * Information about a purse not closed inconsistencies
    253  */
    254 struct TALER_AUDITORDB_PurseNotClosedInconsistencies
    255 {
    256   uint64_t row_id;
    257   struct GNUNET_CRYPTO_EddsaPublicKey purse_pub;
    258   struct TALER_Amount amount;
    259   struct GNUNET_TIME_Absolute expiration_date;
    260   bool suppressed;
    261 };
    262 
    263 /**
    264  * Information about a reserve balance insufficient inconsistency
    265  */
    266 struct TALER_AUDITORDB_ReserveBalanceInsufficientInconsistency
    267 {
    268   uint64_t row_id;
    269   struct GNUNET_CRYPTO_EddsaPublicKey reserve_pub;
    270   bool inconsistency_gain;
    271   struct TALER_Amount inconsistency_amount;
    272   bool suppressed;
    273 };
    274 
    275 /**
    276  * Information about a reserve in inconsistency
    277  */
    278 struct TALER_AUDITORDB_ReserveInInconsistency
    279 {
    280   uint64_t serial_id;
    281   uint64_t bank_row_id;
    282   struct TALER_Amount amount_exchange_expected;
    283   struct TALER_Amount amount_wired;
    284   struct TALER_ReservePublicKeyP reserve_pub;
    285   struct GNUNET_TIME_Absolute timestamp;
    286   struct TALER_FullPayto account;
    287   char *diagnostic;
    288   bool suppressed;
    289 
    290 };
    291 
    292 /**
    293  * Information about a disagreement between the bank and the exchange
    294  * about an incoming wire transfer that established an account key for
    295  * KYC authentication.
    296  */
    297 struct TALER_AUDITORDB_KycauthInInconsistency
    298 {
    299   /**
    300    * Row of the finding in our own table.
    301    */
    302   uint64_t serial_id;
    303 
    304   /**
    305    * Row of the wire transfer in the bank's credit history.  This is what
    306    * the two sides are matched on: the exchange stores it in
    307    * `kycauths_in.wire_reference`.
    308    */
    309   uint64_t bank_row_id;
    310 
    311   /**
    312    * Amount the exchange claims to have received, zero if the exchange
    313    * does not know the transfer at all.
    314    */
    315   struct TALER_Amount amount_exchange_expected;
    316 
    317   /**
    318    * Amount the bank says it transferred, zero if the bank does not
    319    * report the transfer at all.
    320    */
    321   struct TALER_Amount amount_wired;
    322 
    323   /**
    324    * Account public key the transfer established, as known to whichever
    325    * side did report the transfer.
    326    */
    327   union TALER_AccountPublicKeyP account_pub;
    328 
    329   /**
    330    * When the transfer was executed.
    331    */
    332   struct GNUNET_TIME_Absolute timestamp;
    333 
    334   /**
    335    * Bank account that was debited, as known to whichever side did report
    336    * the transfer.
    337    */
    338   struct TALER_FullPayto account;
    339 
    340   /**
    341    * Human-readable description of what does not match.
    342    */
    343   char *diagnostic;
    344 
    345   bool suppressed;
    346 
    347 };
    348 
    349 /**
    350  * Balance values for a reserve (or all reserves).
    351  */
    352 struct TALER_AUDITORDB_ReserveFeeBalance
    353 {
    354   /**
    355    * Remaining funds.
    356    */
    357   struct TALER_Amount reserve_balance;
    358 
    359   /**
    360    * Losses from operations that should not have
    361    * happened (e.g. negative balance).
    362    */
    363   struct TALER_Amount reserve_loss;
    364 
    365   /**
    366    * Fees charged for withdraw.
    367    */
    368   struct TALER_Amount withdraw_fee_balance;
    369 
    370   /**
    371    * Fees charged for closing.
    372    */
    373   struct TALER_Amount close_fee_balance;
    374 
    375   /**
    376    * Fees charged for purse creation.
    377    */
    378   struct TALER_Amount purse_fee_balance;
    379 
    380   /**
    381    * Opening fees charged.
    382    */
    383   struct TALER_Amount open_fee_balance;
    384 
    385   /**
    386    * History fees charged.
    387    */
    388   struct TALER_Amount history_fee_balance;
    389 };
    390 
    391 
    392 /**
    393  * Balance data for denominations in circulation.
    394  */
    395 struct TALER_AUDITORDB_DenominationCirculationData
    396 {
    397   /**
    398    * Amount of outstanding coins in circulation.
    399    */
    400   struct TALER_Amount denom_balance;
    401 
    402   /**
    403    * Amount lost due coins illicitly accepted (effectively, a
    404    * negative @a denom_balance).
    405    */
    406   struct TALER_Amount denom_loss;
    407 
    408   /**
    409    * Total amount that could still be theoretically lost in the future due to
    410    * recoup operations.  (Total put into circulation minus @e recoup_loss).
    411    */
    412   struct TALER_Amount denom_risk;
    413 
    414   /**
    415    * Amount lost due to recoups.
    416    */
    417   struct TALER_Amount recoup_loss;
    418 
    419   /**
    420    * Number of coins of this denomination that the exchange signed into
    421    * existence.
    422    */
    423   uint64_t num_issued;
    424 };
    425 
    426 struct TALER_AUDITORDB_DenominationsWithoutSigs
    427 {
    428   uint64_t row_id;
    429   struct TALER_DenominationHashP denompub_h;
    430   struct TALER_Amount value;
    431   struct GNUNET_TIME_Absolute start_time;
    432   struct GNUNET_TIME_Absolute end_time;
    433   bool suppressed;
    434 };
    435 
    436 struct TALER_AUDITORDB_MisattributionInInconsistency
    437 {
    438   uint64_t row_id;
    439   struct TALER_Amount amount;
    440   uint64_t bank_row;
    441   struct TALER_ReservePublicKeyP reserve_pub;
    442   bool suppressed;
    443 
    444 };
    445 
    446 struct TALER_AUDITORDB_DenominationPending
    447 {
    448   uint64_t row_id;
    449   struct TALER_DenominationHashP denom_pub_hash;
    450   struct TALER_Amount denom_balance;
    451   struct TALER_Amount denom_loss;
    452   uint64_t num_issued;
    453   struct TALER_Amount denom_risk;
    454   struct TALER_Amount recoup_loss;
    455   bool suppressed;
    456 
    457 };
    458 
    459 struct TALER_AUDITORDB_HistoricReserveSummary
    460 {
    461   uint64_t row_id;
    462   struct GNUNET_TIME_Absolute start_date;
    463   struct GNUNET_TIME_Absolute end_date;
    464   struct TALER_Amount reserve_profits;
    465   bool suppressed;
    466 
    467 };
    468 
    469 struct TALER_AUDITORDB_WireFormatInconsistency
    470 {
    471   uint64_t row_id;
    472   struct TALER_Amount amount;
    473   uint64_t wire_offset;
    474   char *diagnostic;
    475   bool suppressed;
    476 
    477 };
    478 
    479 struct TALER_AUDITORDB_WireOutInconsistency
    480 {
    481   uint64_t row_id;
    482   struct TALER_FullPayto destination_account;
    483   char *diagnostic;
    484   uint64_t wire_out_row_id;
    485   struct TALER_Amount expected;
    486   struct TALER_Amount claimed;
    487   bool suppressed;
    488 
    489 };
    490 
    491 struct TALER_AUDITORDB_RowMinorInconsistencies
    492 {
    493   uint64_t row_id;
    494   char *row_table;
    495   uint64_t problem_row;
    496   char *diagnostic;
    497   bool suppressed;
    498 };
    499 
    500 
    501 struct TALER_AUDITORDB_ReserveBalanceSummaryWrongInconsistency
    502 {
    503   uint64_t row_id;
    504   struct TALER_ReservePublicKeyP reserve_pub;
    505   struct TALER_Amount exchange_amount;
    506   struct TALER_Amount auditor_amount;
    507   bool suppressed;
    508 
    509 };
    510 
    511 struct TALER_AUDITORDB_ReserveNotClosedInconsistency
    512 {
    513   uint64_t row_id;
    514   struct TALER_ReservePublicKeyP reserve_pub;
    515   struct TALER_Amount balance;
    516   struct GNUNET_TIME_Absolute expiration_time;
    517   char *diagnostic;
    518   bool suppressed;
    519 
    520 };
    521 
    522 
    523 enum TALER_AUDITORDB_DeletableSuppressableTables
    524 {
    525   /**
    526    * For auditor_amount_arithmetic_inconsistency table.
    527    */
    528   TALER_AUDITORDB_AMOUNT_ARITHMETIC_INCONSISTENCY,
    529 
    530   /**
    531    * For auditor_closure_lags table.
    532    */
    533   TALER_AUDITORDB_CLOSURE_LAGS,
    534 
    535   /**
    536    * For auditor_progress table.
    537    */
    538   TALER_AUDITORDB_PROGRESS,
    539 
    540   /**
    541    * For auditor_bad_sig_losses table.
    542    */
    543   TALER_AUDITORDB_BAD_SIG_LOSSES,
    544 
    545   /**
    546    * For auditor_coin_inconsistency table.
    547    */
    548   TALER_AUDITORDB_COIN_INCONSISTENCY,
    549 
    550   /**
    551    * For auditor_denomination_key_validity_withdraw_inconsistency table.
    552    */
    553   TALER_AUDITORDB_DENOMINATION_KEY_VALIDITY_WITHDRAW_INCONSISTENCY,
    554 
    555   /**
    556    * For auditor_denomination_pending table.
    557    */
    558   TALER_AUDITORDB_DENOMINATION_PENDING,
    559 
    560   /**
    561    * For auditor_denominations_without_sig table.
    562    */
    563   TALER_AUDITORDB_DENOMINATIONS_WITHOUT_SIG,
    564 
    565   /**
    566    * For auditor_deposit_confirmation table.
    567    */
    568   TALER_AUDITORDB_DEPOSIT_CONFIRMATION,
    569 
    570   /**
    571    * For auditor_emergency table.
    572    */
    573   TALER_AUDITORDB_EMERGENCY,
    574 
    575   /**
    576    * For auditor_emergency_by_count table.
    577    */
    578   TALER_AUDITORDB_EMERGENCY_BY_COUNT,
    579 
    580   /**
    581    * For auditor_fee_time_inconsistency table.
    582    */
    583   TALER_AUDITORDB_FEE_TIME_INCONSISTENCY,
    584 
    585   /**
    586    * For auditor_misattribution_in_inconsistency table.
    587    */
    588   TALER_AUDITORDB_MISATTRIBUTION_IN_INCONSISTENCY,
    589 
    590   /**
    591    * For auditor_purse_not_closed_inconsistency table.
    592    */
    593   TALER_AUDITORDB_PURSE_NOT_CLOSED_INCONSISTENCY,
    594 
    595   /**
    596    * For auditor_refreshes_hanging table.
    597    */
    598   TALER_AUDITORDB_REFRESHES_HANGING,
    599 
    600   /**
    601    * For auditor_reserve_balance_insufficient_inconsistency table.
    602    */
    603   TALER_AUDITORDB_RESERVE_BALANCE_INSUFFICIENT_INCONSISTENCY,
    604 
    605   /**
    606    * For auditor_reserve_balance_summary_wrong_inconsistency table.
    607    */
    608   TALER_AUDITORDB_RESERVE_BALANCE_SUMMARY_WRONG_INCONSISTENCY,
    609 
    610   /**
    611    * For auditor_reserve_in_inconsistency table.
    612    */
    613   TALER_AUDITORDB_RESERVE_IN_INCONSISTENCY,
    614 
    615   /**
    616    * For auditor_reserve_not_closed_inconsistency table.
    617    */
    618   TALER_AUDITORDB_RESERVE_NOT_CLOSED_INCONSISTENCY,
    619 
    620   /**
    621    * For auditor_row_inconsistency table.
    622    */
    623   TALER_AUDITORDB_ROW_INCONSISTENCY,
    624 
    625   /**
    626    * For auditor_row_minor_inconsistency table.
    627    */
    628   TALER_AUDITORDB_ROW_MINOR_INCONSISTENCY,
    629 
    630   /**
    631    * For auditor_wire_format_inconsistency table.
    632    */
    633   TALER_AUDITORDB_WIRE_FORMAT_INCONSISTENCY,
    634 
    635   /**
    636    * For auditor_wire_out_inconsistency table.
    637    */
    638   TALER_AUDITORDB_WIRE_OUT_INCONSISTENCY,
    639 
    640   /**
    641    * For auditor_kycauth_in_inconsistency table.  Note that new values
    642    * must be appended here rather than inserted alphabetically: the
    643    * numbering is what `TALER_AUDITORDB_get_deletable_suppressable_table_name()`
    644    * indexes its table with.
    645    */
    646   TALER_AUDITORDB_KYCAUTH_IN_INCONSISTENCY,
    647 
    648   /**
    649    * For auditor_aml_holds table.
    650    */
    651   TALER_AUDITORDB_AML_HOLDS,
    652 
    653   /**
    654    * Terminal.
    655    */
    656   TALER_AUDITORDB_DELETABLESUPPRESSABLE_TABLES_MAX
    657 };
    658 
    659 
    660 /**
    661  * Information about a wire transfer the exchange aggregated but did not
    662  * execute.
    663  */
    664 struct TALER_AUDITORDB_AmlHold
    665 {
    666   uint64_t row_id;
    667 
    668   /**
    669    * Wire transfer the deposits were aggregated into.
    670    */
    671   struct TALER_WireTransferIdentifierRawP wtid;
    672 
    673   /**
    674    * Account the transfer should be made to.
    675    */
    676   struct TALER_FullPaytoHashP wire_target_h_payto;
    677 
    678   /**
    679    * Payto URI of @e wire_target_h_payto.
    680    */
    681   struct TALER_FullPayto account;
    682 
    683   /**
    684    * What the exchange still owes on this transfer.
    685    */
    686   struct TALER_Amount amount;
    687 
    688   /**
    689    * Why the exchange says it has not made the transfer, an
    690    * `enum TALER_EXCHANGEDB_DeferralReason` value taken from its
    691    * `aggregation_deferrals` row.  Zero if the exchange gave no reason at all,
    692    * which is the case that says it is simply not paying.
    693    */
    694   uint32_t deferral_reason;
    695 
    696   /**
    697    * Measure the exchange named as blocking the payout, or 0 if it named none.
    698    */
    699   uint64_t legitimization_measure_serial_id;
    700 
    701   /**
    702    * When did the auditor first see this transfer being held?
    703    */
    704   struct GNUNET_TIME_Absolute creation_date;
    705 
    706   bool suppressed;
    707 };
    708 
    709 
    710 /**
    711  * Information about an arithmetic inconsistency
    712  */
    713 struct TALER_AUDITORDB_AmountArithmeticInconsistency
    714 {
    715   uint64_t row_id;
    716   uint64_t problem_row_id;
    717   char *operation;
    718   struct TALER_Amount exchange_amount;
    719   struct TALER_Amount auditor_amount;
    720   bool profitable;
    721   bool suppressed;
    722 };
    723 
    724 
    725 /**
    726  * Initialize database connection.
    727  *
    728  * @param cfg configuration to use
    729  * @return NULL on failure
    730  */
    731 struct TALER_AUDITORDB_PostgresContext *
    732 TALER_AUDITORDB_connect (const struct GNUNET_CONFIGURATION_Handle *cfg);
    733 
    734 
    735 /**
    736  * Shutdown the database connection.
    737  *
    738  * @param[in] pg connection to disconnect
    739  */
    740 void
    741 TALER_AUDITORDB_disconnect (struct TALER_AUDITORDB_PostgresContext *pg);
    742 
    743 
    744 #endif