LCOV - code coverage report
Current view: top level - auditordb - iterate_amount_arithmetic_inconsistencies.c (source / functions) Coverage Total Hit
Test: coverage.info Lines: 89.2 % 37 33
Test Date: 2026-10-29 19:05:17 Functions: 100.0 % 2 2

            Line data    Source code
       1              : /*
       2              :    This file is part of TALER
       3              :    Copyright (C) 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 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              : #include "taler/taler_error_codes.h"
      17              : #include "taler/taler_pq_lib.h"
      18              : #include "pg_helper.h"
      19              : #include "auditor-database/iterate_amount_arithmetic_inconsistencies.h"
      20              : 
      21              : 
      22              : /**
      23              :  * Hard upper bound on the number of records returned by a single
      24              :  * call, regardless of the limit requested by the client.
      25              :  */
      26              : #define MAX_RECORDS 50000
      27              : 
      28              : 
      29              : /**
      30              :  * Closure for #deposit_confirmation_cb().
      31              :  */
      32              : struct AmountArithmeticInconsistencyContext
      33              : {
      34              : 
      35              :   /**
      36              :    * Function to call for each deposit confirmation.
      37              :    */
      38              :   TALER_AUDITORDB_AmountArithmeticInconsistencyCallback cb;
      39              : 
      40              :   /**
      41              :    * Closure for @e cb
      42              :    */
      43              :   void *cb_cls;
      44              : 
      45              :   /**
      46              :    * Plugin context.
      47              :    */
      48              :   struct TALER_AUDITORDB_PostgresContext *pg;
      49              : 
      50              :   /**
      51              :    * Query status to return.
      52              :    */
      53              :   enum GNUNET_DB_QueryStatus qs;
      54              : };
      55              : 
      56              : 
      57              : /**
      58              :  * Helper function for
      59              :  * #TALER_AUDITORDB_iterate_amount_arithmetic_inconsistencies().
      60              :  * To be called with the results of a SELECT statement
      61              :  * that has returned @a num_results results.
      62              :  *
      63              :  * @param cls closure of type `struct DepositConfirmationContext *`
      64              :  * @param result the postgres result
      65              :  * @param num_results the number of results in @a result
      66              :  */
      67              : static void
      68            7 : amount_arithmetic_inconsistency_cb (void *cls,
      69              :                                     PGresult *result,
      70              :                                     unsigned int num_results)
      71              : {
      72            7 :   struct AmountArithmeticInconsistencyContext *dcc = cls;
      73            7 :   struct TALER_AUDITORDB_PostgresContext *pg = dcc->pg;
      74              : 
      75           10 :   for (unsigned int i = 0; i < num_results; i++)
      76              :   {
      77              :     struct TALER_AUDITORDB_AmountArithmeticInconsistency dc;
      78              :     /* libgnunetpq declares GNUNET_PQ_result_spec_int16() but does
      79              :        not implement it, so the SMALLINT is read as INT8 */
      80              :     int64_t profitable;
      81            3 :     struct GNUNET_PQ_ResultSpec rs[] = {
      82            3 :       GNUNET_PQ_result_spec_uint64 ("row_id",
      83              :                                     &dc.row_id),
      84            3 :       GNUNET_PQ_result_spec_uint64 ("problem_row_id",
      85              :                                     &dc.problem_row_id),
      86            3 :       GNUNET_PQ_result_spec_string ("operation",
      87              :                                     &dc.operation),
      88            3 :       TALER_PQ_RESULT_SPEC_AMOUNT ("exchange_amount",
      89              :                                    &dc.exchange_amount),
      90            3 :       TALER_PQ_RESULT_SPEC_AMOUNT ("auditor_amount",
      91              :                                    &dc.auditor_amount),
      92            3 :       GNUNET_PQ_result_spec_int64 ("profitable",
      93              :                                    &profitable),
      94            3 :       GNUNET_PQ_result_spec_bool ("suppressed",
      95              :                                   &dc.suppressed),
      96              :       GNUNET_PQ_result_spec_end
      97              :     };
      98              :     enum GNUNET_GenericReturnValue rval;
      99              : 
     100            3 :     if (GNUNET_OK !=
     101            3 :         GNUNET_PQ_extract_result (result,
     102              :                                   rs,
     103              :                                   i))
     104              :     {
     105            0 :       GNUNET_break (0);
     106            0 :       dcc->qs = GNUNET_DB_STATUS_HARD_ERROR;
     107            0 :       return;
     108              :     }
     109            3 :     dc.profitable = (int16_t) profitable;
     110            3 :     dcc->qs = i + 1;
     111            3 :     rval = dcc->cb (dcc->cb_cls,
     112              :                     &dc);
     113            3 :     GNUNET_PQ_cleanup_result (rs);
     114            3 :     if (GNUNET_OK != rval)
     115            0 :       break;
     116              :   }
     117              : }
     118              : 
     119              : 
     120              : enum GNUNET_DB_QueryStatus
     121            7 : TALER_AUDITORDB_iterate_amount_arithmetic_inconsistencies (
     122              :   struct TALER_AUDITORDB_PostgresContext *pg,
     123              :   int64_t limit,
     124              :   uint64_t offset,
     125              :   bool return_suppressed,
     126              :   TALER_AUDITORDB_AmountArithmeticInconsistencyCallback cb,
     127              :   void *cb_cls)
     128              : {
     129            7 :   uint64_t plimit = GNUNET_MIN ((uint64_t) MAX_RECORDS,
     130              :                                 TALER_AUDITORDB_abs_limit (limit));
     131            7 :   struct GNUNET_PQ_QueryParam params[] = {
     132            7 :     GNUNET_PQ_query_param_uint64 (&offset),
     133            7 :     GNUNET_PQ_query_param_bool (return_suppressed),
     134            7 :     GNUNET_PQ_query_param_uint64 (&plimit),
     135              :     GNUNET_PQ_query_param_end
     136              :   };
     137            7 :   struct AmountArithmeticInconsistencyContext dcc = {
     138              :     .cb = cb,
     139              :     .cb_cls = cb_cls,
     140              :     .pg = pg
     141              :   };
     142              :   enum GNUNET_DB_QueryStatus qs;
     143              : 
     144            7 :   PREPARE (pg,
     145              :            "iterate_amount_arithmetic_inconsistencies_desc",
     146              :            "SELECT"
     147              :            " row_id"
     148              :            ",problem_row_id"
     149              :            ",operation"
     150              :            ",exchange_amount"
     151              :            ",auditor_amount"
     152              :            ",profitable::INT8 AS profitable"
     153              :            ",suppressed"
     154              :            " FROM auditor_amount_arithmetic_inconsistency"
     155              :            " WHERE (row_id<$1)"
     156              :            " AND ($2 OR NOT suppressed)"
     157              :            " ORDER BY row_id DESC"
     158              :            " LIMIT $3"
     159              :            );
     160            7 :   PREPARE (pg,
     161              :            "iterate_amount_arithmetic_inconsistencies_asc",
     162              :            "SELECT"
     163              :            " row_id"
     164              :            ",problem_row_id"
     165              :            ",operation"
     166              :            ",exchange_amount"
     167              :            ",auditor_amount"
     168              :            ",profitable::INT8 AS profitable"
     169              :            ",suppressed"
     170              :            " FROM auditor_amount_arithmetic_inconsistency"
     171              :            " WHERE (row_id>$1)"
     172              :            " AND ($2 OR NOT suppressed)"
     173              :            " ORDER BY row_id ASC"
     174              :            " LIMIT $3"
     175              :            );
     176            7 :   qs = GNUNET_PQ_eval_prepared_multi_select (
     177              :     pg->conn,
     178              :     (limit > 0)
     179              :     ? "iterate_amount_arithmetic_inconsistencies_asc"
     180              :     : "iterate_amount_arithmetic_inconsistencies_desc",
     181              :     params,
     182              :     &amount_arithmetic_inconsistency_cb,
     183              :     &dcc);
     184            7 :   if (qs > 0)
     185            1 :     return dcc.qs;
     186            6 :   GNUNET_break (GNUNET_DB_STATUS_HARD_ERROR != qs);
     187            6 :   return qs;
     188              : }
        

Generated by: LCOV version 2.0-1