LCOV - code coverage report
Current view: top level - exchangedb - exchangedb_transactions.c (source / functions) Hit Total Coverage
Test: GNU Taler exchange coverage report Lines: 50 68 73.5 %
Date: 2021-08-30 06:43:37 Functions: 1 1 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :   This file is part of TALER
       3             :   Copyright (C) 2017-2020 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 exchangedb/exchangedb_transactions.c
      18             :  * @brief Logic to compute transaction totals of a transaction list for a coin
      19             :  * @author Christian Grothoff
      20             :  */
      21             : #include "platform.h"
      22             : #include "taler_exchangedb_lib.h"
      23             : 
      24             : 
      25             : /**
      26             :  * Calculate the total value of all transactions performed.
      27             :  * Stores @a off plus the cost of all transactions in @a tl
      28             :  * in @a ret.
      29             :  *
      30             :  * @param tl transaction list to process
      31             :  * @param off offset to use as the starting value
      32             :  * @param[out] ret where the resulting total is to be stored (may alias @a off)
      33             :  * @return #GNUNET_OK on success, #GNUNET_SYSERR on errors
      34             :  */
      35             : int
      36          71 : TALER_EXCHANGEDB_calculate_transaction_list_totals (
      37             :   struct TALER_EXCHANGEDB_TransactionList *tl,
      38             :   const struct TALER_Amount *off,
      39             :   struct TALER_Amount *ret)
      40             : {
      41          71 :   struct TALER_Amount spent = *off;
      42             :   struct TALER_Amount refunded;
      43             :   struct TALER_Amount deposit_fee;
      44             :   bool have_refund;
      45             : 
      46          71 :   GNUNET_assert (GNUNET_OK ==
      47             :                  TALER_amount_set_zero (spent.currency,
      48             :                                         &refunded));
      49          71 :   have_refund = false;
      50         176 :   for (struct TALER_EXCHANGEDB_TransactionList *pos = tl;
      51             :        NULL != pos;
      52         105 :        pos = pos->next)
      53             :   {
      54         105 :     switch (pos->type)
      55             :     {
      56          20 :     case TALER_EXCHANGEDB_TT_DEPOSIT:
      57             :       /* spent += pos->amount_with_fee */
      58          20 :       if (0 >
      59          20 :           TALER_amount_add (&spent,
      60             :                             &spent,
      61          20 :                             &pos->details.deposit->amount_with_fee))
      62             :       {
      63           0 :         GNUNET_break (0);
      64           0 :         return GNUNET_SYSERR;
      65             :       }
      66          20 :       deposit_fee = pos->details.deposit->deposit_fee;
      67          20 :       break;
      68          59 :     case TALER_EXCHANGEDB_TT_MELT:
      69             :       /* spent += pos->amount_with_fee */
      70          59 :       if (0 >
      71          59 :           TALER_amount_add (&spent,
      72             :                             &spent,
      73          59 :                             &pos->details.melt->amount_with_fee))
      74             :       {
      75           0 :         GNUNET_break (0);
      76           0 :         return GNUNET_SYSERR;
      77             :       }
      78          59 :       break;
      79           4 :     case TALER_EXCHANGEDB_TT_REFUND:
      80             :       /* refunded += pos->refund_amount - pos->refund_fee */
      81           4 :       if (0 >
      82           4 :           TALER_amount_add (&refunded,
      83             :                             &refunded,
      84           4 :                             &pos->details.refund->refund_amount))
      85             :       {
      86           0 :         GNUNET_break (0);
      87           0 :         return GNUNET_SYSERR;
      88             :       }
      89           4 :       if (0 >
      90           4 :           TALER_amount_add (&spent,
      91             :                             &spent,
      92           4 :                             &pos->details.refund->refund_fee))
      93             :       {
      94           0 :         GNUNET_break (0);
      95           0 :         return GNUNET_SYSERR;
      96             :       }
      97           4 :       have_refund = true;
      98           4 :       break;
      99          17 :     case TALER_EXCHANGEDB_TT_OLD_COIN_RECOUP:
     100             :       /* refunded += pos->value */
     101          17 :       if (0 >
     102          17 :           TALER_amount_add (&refunded,
     103             :                             &refunded,
     104          17 :                             &pos->details.old_coin_recoup->value))
     105             :       {
     106           0 :         GNUNET_break (0);
     107           0 :         return GNUNET_SYSERR;
     108             :       }
     109          17 :       break;
     110           1 :     case TALER_EXCHANGEDB_TT_RECOUP:
     111             :       /* spent += pos->value */
     112           1 :       if (0 >
     113           1 :           TALER_amount_add (&spent,
     114             :                             &spent,
     115           1 :                             &pos->details.recoup->value))
     116             :       {
     117           0 :         GNUNET_break (0);
     118           0 :         return GNUNET_SYSERR;
     119             :       }
     120           1 :       break;
     121           4 :     case TALER_EXCHANGEDB_TT_RECOUP_REFRESH:
     122             :       /* spent += pos->value */
     123           4 :       if (0 >
     124           4 :           TALER_amount_add (&spent,
     125             :                             &spent,
     126           4 :                             &pos->details.recoup_refresh->value))
     127             :       {
     128           0 :         GNUNET_break (0);
     129           0 :         return GNUNET_SYSERR;
     130             :       }
     131           4 :       break;
     132             :     }
     133         105 :   }
     134          71 :   if (have_refund)
     135             :   {
     136             :     /* If we gave any refund, also discount ONE deposit fee */
     137           3 :     if (0 >
     138           3 :         TALER_amount_add (&refunded,
     139             :                           &refunded,
     140             :                           &deposit_fee))
     141             :     {
     142           0 :       GNUNET_break (0);
     143           0 :       return GNUNET_SYSERR;
     144             :     }
     145             :   }
     146             :   /* spent = spent - refunded */
     147          71 :   if (0 >
     148          71 :       TALER_amount_subtract (&spent,
     149             :                              &spent,
     150             :                              &refunded))
     151             :   {
     152           0 :     GNUNET_break (0);
     153           0 :     return GNUNET_SYSERR;
     154             :   }
     155          71 :   *ret = spent;
     156          71 :   return GNUNET_OK;
     157             : }

Generated by: LCOV version 1.14