LCOV - code coverage report
Current view: top level - exchange - taler-exchange-aggregator.c (source / functions) Hit Total Coverage
Test: rcoverage.info Lines: 335 620 54.0 %
Date: 2017-09-17 17:24:28 Functions: 20 20 100.0 %

          Line data    Source code
       1             : /*
       2             :   This file is part of TALER
       3             :   Copyright (C) 2016, 2017 GNUnet e.V.
       4             : 
       5             :   TALER is free software; you can redistribute it and/or modify it under the
       6             :   terms of the GNU Affero 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 Affero General Public License for more details.
      12             : 
      13             :   You should have received a copy of the GNU Affero General Public License along with
      14             :   TALER; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
      15             : */
      16             : 
      17             : /**
      18             :  * @file taler-exchange-aggregator.c
      19             :  * @brief Process that aggregates outgoing transactions and executes them
      20             :  * @author Christian Grothoff
      21             :  */
      22             : #include "platform.h"
      23             : #include <gnunet/gnunet_util_lib.h>
      24             : #include <jansson.h>
      25             : #include <pthread.h>
      26             : #include "taler_exchangedb_lib.h"
      27             : #include "taler_exchangedb_plugin.h"
      28             : #include "taler_json_lib.h"
      29             : #include "taler_wire_lib.h"
      30             : 
      31             : 
      32             : /**
      33             :  * Information we keep for each loaded wire plugin.
      34             :  */
      35             : struct WirePlugin
      36             : {
      37             :   /**
      38             :    * Plugins are kept in a DLL.
      39             :    */
      40             :   struct WirePlugin *next;
      41             : 
      42             :   /**
      43             :    * Plugins are kept in a DLL.
      44             :    */
      45             :   struct WirePlugin *prev;
      46             : 
      47             :   /**
      48             :    * Handle to the plugin.
      49             :    */
      50             :   struct TALER_WIRE_Plugin *wire_plugin;
      51             : 
      52             :   /**
      53             :    * Name of the plugin.
      54             :    */
      55             :   char *type;
      56             : 
      57             :   /**
      58             :    * Wire transfer fee structure.
      59             :    */
      60             :   struct TALER_EXCHANGEDB_AggregateFees *af;
      61             : 
      62             : };
      63             : 
      64             : 
      65             : /**
      66             :  * Data we keep to #run_transfers().  There is at most
      67             :  * one of these around at any given point in time.
      68             :  */
      69             : struct WirePrepareData
      70             : {
      71             : 
      72             :   /**
      73             :    * Database session for all of our transactions.
      74             :    */
      75             :   struct TALER_EXCHANGEDB_Session *session;
      76             : 
      77             :   /**
      78             :    * Wire execution handle.
      79             :    */
      80             :   struct TALER_WIRE_ExecuteHandle *eh;
      81             : 
      82             :   /**
      83             :    * Wire plugin used for this preparation.
      84             :    */
      85             :   struct WirePlugin *wp;
      86             : 
      87             :   /**
      88             :    * Row ID of the transfer.
      89             :    */
      90             :   unsigned long long row_id;
      91             : 
      92             : };
      93             : 
      94             : 
      95             : /**
      96             :  * Information about one aggregation process to be executed.  There is
      97             :  * at most one of these around at any given point in time.
      98             :  */
      99             : struct AggregationUnit
     100             : {
     101             :   /**
     102             :    * Public key of the merchant.
     103             :    */
     104             :   struct TALER_MerchantPublicKeyP merchant_pub;
     105             : 
     106             :   /**
     107             :    * Total amount to be transferred, before subtraction of @e wire_fee and rounding down.
     108             :    */
     109             :   struct TALER_Amount total_amount;
     110             : 
     111             :   /**
     112             :    * Final amount to be transferred (after fee and rounding down).
     113             :    */
     114             :   struct TALER_Amount final_amount;
     115             : 
     116             :   /**
     117             :    * Wire fee we charge for @e wp at @e execution_time.
     118             :    */
     119             :   struct TALER_Amount wire_fee;
     120             : 
     121             :   /**
     122             :    * Hash of @e wire.
     123             :    */
     124             :   struct GNUNET_HashCode h_wire;
     125             : 
     126             :   /**
     127             :    * Wire transfer identifier we use.
     128             :    */
     129             :   struct TALER_WireTransferIdentifierRawP wtid;
     130             : 
     131             :   /**
     132             :    * Row ID of the transaction that started it all.
     133             :    */
     134             :   unsigned long long row_id;
     135             : 
     136             :   /**
     137             :    * The current time.
     138             :    */
     139             :   struct GNUNET_TIME_Absolute execution_time;
     140             : 
     141             :   /**
     142             :    * Wire details of the merchant.
     143             :    */
     144             :   json_t *wire;
     145             : 
     146             :   /**
     147             :    * Wire plugin to be used for the preparation.
     148             :    */
     149             :   struct WirePlugin *wp;
     150             : 
     151             :   /**
     152             :    * Database session for all of our transactions.
     153             :    */
     154             :   struct TALER_EXCHANGEDB_Session *session;
     155             : 
     156             :   /**
     157             :    * Wire preparation handle.
     158             :    */
     159             :   struct TALER_WIRE_PrepareHandle *ph;
     160             : 
     161             :   /**
     162             :    * Array of #aggregation_limit row_ids from the
     163             :    * aggregation.
     164             :    */
     165             :   unsigned long long *additional_rows;
     166             : 
     167             :   /**
     168             :    * Offset specifying how many #additional_rows are in use.
     169             :    */
     170             :   unsigned int rows_offset;
     171             : 
     172             :   /**
     173             :    * Set to #GNUNET_YES if we have to abort due to failure.
     174             :    */
     175             :   int failed;
     176             : 
     177             : };
     178             : 
     179             : 
     180             : /**
     181             :  * Context we use while closing a reserve.
     182             :  */
     183             : struct CloseTransferContext
     184             : {
     185             :   /**
     186             :    * Handle for preparing the wire transfer.
     187             :    */
     188             :   struct TALER_WIRE_PrepareHandle *ph;
     189             : 
     190             :   /**
     191             :    * Our database session.
     192             :    */
     193             :   struct TALER_EXCHANGEDB_Session *session;
     194             : 
     195             :   /**
     196             :    * Wire transfer method.
     197             :    */
     198             :   char *type;
     199             : 
     200             :   /**
     201             :    * Wire plugin used for closing the reserve.
     202             :    */
     203             :   struct WirePlugin *wp;
     204             : };
     205             : 
     206             : 
     207             : /**
     208             :  * Active context while processing reserve closing,
     209             :  * or NULL.
     210             :  */
     211             : static struct CloseTransferContext *ctc;
     212             : 
     213             : /**
     214             :  * Which currency is used by this exchange?
     215             :  */
     216             : static char *exchange_currency_string;
     217             : 
     218             : /**
     219             :  * What is the base URL of this exchange?
     220             :  */
     221             : static char *exchange_base_url;
     222             : 
     223             : /**
     224             :  * The exchange's configuration (global)
     225             :  */
     226             : static struct GNUNET_CONFIGURATION_Handle *cfg;
     227             : 
     228             : /**
     229             :  * Our DB plugin.
     230             :  */
     231             : static struct TALER_EXCHANGEDB_Plugin *db_plugin;
     232             : 
     233             : /**
     234             :  * Head of list of loaded wire plugins.
     235             :  */
     236             : static struct WirePlugin *wp_head;
     237             : 
     238             : /**
     239             :  * Tail of list of loaded wire plugins.
     240             :  */
     241             : static struct WirePlugin *wp_tail;
     242             : 
     243             : /**
     244             :  * Next task to run, if any.
     245             :  */
     246             : static struct GNUNET_SCHEDULER_Task *task;
     247             : 
     248             : /**
     249             :  * If we are currently executing a transfer, information about
     250             :  * the active transfer is here. Otherwise, this variable is NULL.
     251             :  */
     252             : static struct WirePrepareData *wpd;
     253             : 
     254             : /**
     255             :  * If we are currently aggregating transactions, information about the
     256             :  * active aggregation is here. Otherwise, this variable is NULL.
     257             :  */
     258             : static struct AggregationUnit *au;
     259             : 
     260             : /**
     261             :  * Value to return from main(). #GNUNET_OK on success, #GNUNET_SYSERR
     262             :  * on serious errors.
     263             :  */
     264             : static int global_ret;
     265             : 
     266             : /**
     267             :  * #GNUNET_YES if we are in test mode and should exit when idle.
     268             :  */
     269             : static int test_mode;
     270             : 
     271             : /**
     272             :  * Did #run_reserve_closures() have any work during its last run?
     273             :  */
     274             : static int reserves_idle;
     275             : 
     276             : /**
     277             :  * Limit on the number of transactions we aggregate at once.  Note
     278             :  * that the limit must be big enough to ensure that when transactions
     279             :  * of the smallest possible unit are aggregated, they do surpass the
     280             :  * "tiny" threshold beyond which we never trigger a wire transaction!
     281             :  *
     282             :  * Note: do not change here, Postgres requires us to hard-code the
     283             :  * LIMIT in the prepared statement.
     284             :  */
     285             : static unsigned int aggregation_limit = TALER_EXCHANGEDB_MATCHING_DEPOSITS_LIMIT;
     286             : 
     287             : 
     288             : /**
     289             :  * Extract wire plugin type from @a wire address
     290             :  *
     291             :  * @param wire a wire address
     292             :  * @return NULL if @a wire is ill-formed
     293             :  */
     294             : const char *
     295          22 : extract_type (const json_t *wire)
     296             : {
     297             :   const char *type;
     298             :   json_t *t;
     299             : 
     300          22 :   t = json_object_get (wire, "type");
     301          22 :   if (NULL == t)
     302             :   {
     303           0 :     GNUNET_break (0);
     304           0 :     return NULL;
     305             :   }
     306          22 :   type = json_string_value (t);
     307          22 :   if (NULL == type)
     308             :   {
     309           0 :     GNUNET_break (0);
     310           0 :     return NULL;
     311             :   }
     312          22 :   return type;
     313             : }
     314             : 
     315             : 
     316             : /**
     317             :  * Advance the "af" pointer in @a wp to point to the
     318             :  * currently valid record.
     319             :  *
     320             :  * @param wp wire transfer fee data structure to update
     321             :  * @param now timestamp to update fees to
     322             :  */
     323             : static void
     324          39 : advance_fees (struct WirePlugin *wp,
     325             :               struct GNUNET_TIME_Absolute now)
     326             : {
     327             :   struct TALER_EXCHANGEDB_AggregateFees *af;
     328             : 
     329             :   /* First, try to see if we have current fee information in memory */
     330          39 :   af = wp->af;
     331         100 :   while ( (NULL != af) &&
     332          22 :           (af->end_date.abs_value_us < now.abs_value_us) )
     333             :   {
     334           0 :     struct TALER_EXCHANGEDB_AggregateFees *n = af->next;
     335             : 
     336           0 :     GNUNET_free (af);
     337           0 :     af = n;
     338             :   }
     339          39 :   wp->af = af;
     340          39 : }
     341             : 
     342             : 
     343             : /**
     344             :  * Update wire transfer fee data structure in @a wp.
     345             :  *
     346             :  * @param wp wire transfer fee data structure to update
     347             :  * @param now timestamp to update fees to
     348             :  * @param session DB session to use
     349             :  * @return transaction status
     350             :  */
     351             : static enum GNUNET_DB_QueryStatus
     352          22 : update_fees (struct WirePlugin *wp,
     353             :              struct GNUNET_TIME_Absolute now,
     354             :              struct TALER_EXCHANGEDB_Session *session)
     355             : {
     356             :   enum GNUNET_DB_QueryStatus qs;
     357             :   
     358          22 :   advance_fees (wp,
     359             :                 now);
     360          22 :   if (NULL != wp->af)
     361           5 :     return GNUNET_DB_STATUS_SUCCESS_ONE_RESULT;
     362             :   /* Let's try to load it from disk... */
     363          17 :   wp->af = TALER_EXCHANGEDB_fees_read (cfg,
     364          17 :                                        wp->type);
     365          17 :   advance_fees (wp,
     366             :                 now);
     367          70 :   for (struct TALER_EXCHANGEDB_AggregateFees *p = wp->af;
     368             :        NULL != p;
     369          36 :        p = p->next)
     370             :   {
     371          72 :     qs = db_plugin->insert_wire_fee (db_plugin->cls,
     372             :                                      session,
     373          36 :                                      wp->type,
     374             :                                      p->start_date,
     375             :                                      p->end_date,
     376          36 :                                      &p->wire_fee,
     377          36 :                                      &p->master_sig);
     378          36 :     if (qs < 0)
     379             :     {
     380           0 :       TALER_EXCHANGEDB_fees_free (wp->af);
     381           0 :       wp->af = NULL;
     382           0 :       return qs;
     383             :     }
     384             :   }
     385          17 :   if (NULL != wp->af)
     386          17 :     return GNUNET_DB_STATUS_SUCCESS_ONE_RESULT;
     387           0 :   GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     388             :               "Failed to find current wire transfer fees for `%s'\n",
     389             :               wp->type);
     390           0 :   return GNUNET_DB_STATUS_SUCCESS_NO_RESULTS;
     391             : }
     392             : 
     393             : 
     394             : /**
     395             :  * Find the wire plugin for the given wire address.
     396             :  *
     397             :  * @param type wire plugin type we need a plugin for
     398             :  * @return NULL on error
     399             :  */
     400             : static struct WirePlugin *
     401          40 : find_plugin (const char *type)
     402             : {
     403             :   struct WirePlugin *wp;
     404             : 
     405          40 :   if (NULL == type)
     406           0 :     return NULL;
     407          40 :   for (wp = wp_head; NULL != wp; wp = wp->next)
     408          23 :     if (0 == strcmp (type,
     409          23 :                      wp->type))
     410          23 :       return wp;
     411          17 :   wp = GNUNET_new (struct WirePlugin);
     412          17 :   wp->wire_plugin = TALER_WIRE_plugin_load (cfg,
     413             :                                             type);
     414          17 :   if (NULL == wp->wire_plugin)
     415             :   {
     416           0 :     fprintf (stderr,
     417             :              "Failed to load wire plugin for `%s'\n",
     418             :              type);
     419           0 :     GNUNET_free (wp);
     420           0 :     return NULL;
     421             :   }
     422          17 :   wp->type = GNUNET_strdup (type);
     423          17 :   GNUNET_CONTAINER_DLL_insert (wp_head,
     424             :                                wp_tail,
     425             :                                wp);
     426          17 :   return wp;
     427             : }
     428             : 
     429             : 
     430             : /**
     431             :  * Free data stored in #au.
     432             :  */
     433             : static void
     434          61 : cleanup_au (void)
     435             : {
     436          61 :   if (NULL == au)
     437           0 :     return;
     438          61 :   GNUNET_free_non_null (au->additional_rows);
     439          61 :   if (NULL != au->wire)
     440             :   {
     441          21 :     json_decref (au->wire);
     442          21 :     au->wire = NULL;
     443             :   }
     444          61 :   GNUNET_free (au);
     445          61 :   au = NULL;
     446             : }
     447             : 
     448             : 
     449             : /**
     450             :  * We're being aborted with CTRL-C (or SIGTERM). Shut down.
     451             :  *
     452             :  * @param cls closure
     453             :  */
     454             : static void
     455          28 : shutdown_task (void *cls)
     456             : {
     457          28 :   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
     458             :               "Running shutdown\n");
     459          28 :   if (NULL != task)
     460             :   {
     461           0 :     GNUNET_SCHEDULER_cancel (task);
     462           0 :     task = NULL;
     463             :   }
     464          28 :   if (NULL != wpd)
     465             :   {
     466           0 :     if (NULL != wpd->eh)
     467             :     {
     468           0 :       wpd->wp->wire_plugin->execute_wire_transfer_cancel (wpd->wp->wire_plugin->cls,
     469           0 :                                                           wpd->eh);
     470           0 :       wpd->eh = NULL;
     471             :     }
     472           0 :     db_plugin->rollback (db_plugin->cls,
     473           0 :                          wpd->session);
     474           0 :     GNUNET_free (wpd);
     475           0 :     wpd = NULL;
     476             :   }
     477          28 :   if (NULL != au)
     478             :   {
     479           0 :     if (NULL != au->ph)
     480             :     {
     481           0 :       au->wp->wire_plugin->prepare_wire_transfer_cancel (au->wp->wire_plugin->cls,
     482           0 :                                                          au->ph);
     483           0 :       au->ph = NULL;
     484             :     }
     485           0 :     db_plugin->rollback (db_plugin->cls,
     486           0 :                          au->session);
     487           0 :     cleanup_au ();
     488             :   }
     489          28 :   if (NULL != ctc)
     490             :   {
     491           0 :     ctc->wp->wire_plugin->prepare_wire_transfer_cancel (ctc->wp->wire_plugin->cls,
     492           0 :                                                         ctc->ph);
     493           0 :     ctc->ph = NULL;
     494           0 :     db_plugin->rollback (db_plugin->cls,
     495           0 :                          ctc->session);
     496           0 :     GNUNET_free (ctc->type);
     497           0 :     GNUNET_free (ctc);
     498           0 :     ctc = NULL;
     499             :   }
     500          28 :   TALER_EXCHANGEDB_plugin_unload (db_plugin);
     501             : 
     502             :   {
     503             :     struct WirePlugin *wp;
     504             : 
     505          73 :     while (NULL != (wp = wp_head))
     506             :     {
     507          17 :       GNUNET_CONTAINER_DLL_remove (wp_head,
     508             :                                    wp_tail,
     509             :                                    wp);
     510          17 :       TALER_WIRE_plugin_unload (wp->wire_plugin);
     511          17 :       TALER_EXCHANGEDB_fees_free (wp->af);
     512          17 :       GNUNET_free (wp->type);
     513          17 :       GNUNET_free (wp);
     514             :     }
     515             :   }
     516          28 :   GNUNET_CONFIGURATION_destroy (cfg);
     517          28 :   cfg = NULL;
     518          28 : }
     519             : 
     520             : 
     521             : /**
     522             :  * Parse configuration parameters for the exchange server into the
     523             :  * corresponding global variables.
     524             :  *
     525             :  * @return #GNUNET_OK on success
     526             :  */
     527             : static int
     528          28 : exchange_serve_process_config ()
     529             : {
     530          28 :   if (GNUNET_OK !=
     531          28 :       GNUNET_CONFIGURATION_get_value_string (cfg,
     532             :                                              "taler",
     533             :                                              "currency",
     534             :                                              &exchange_currency_string))
     535             :   {
     536           0 :     GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
     537             :                                "taler",
     538             :                                "currency");
     539           0 :     return GNUNET_SYSERR;
     540             :   }
     541          28 :   if (strlen (exchange_currency_string) >= TALER_CURRENCY_LEN)
     542             :   {
     543           0 :     fprintf (stderr,
     544             :              "Currency `%s' longer than the allowed limit of %u characters.",
     545             :              exchange_currency_string,
     546             :              (unsigned int) TALER_CURRENCY_LEN);
     547           0 :     return GNUNET_SYSERR;
     548             :   }
     549             : 
     550          28 :   if (NULL ==
     551          28 :       (db_plugin = TALER_EXCHANGEDB_plugin_load (cfg)))
     552             :   {
     553           0 :     fprintf (stderr,
     554             :              "Failed to initialize DB subsystem\n");
     555           0 :     return GNUNET_SYSERR;
     556             :   }
     557          28 :   if (GNUNET_OK !=
     558          28 :       db_plugin->create_tables (db_plugin->cls))
     559             :   {
     560           0 :     fprintf (stderr,
     561             :              "Failed to initialize DB tables\n");
     562           0 :     TALER_EXCHANGEDB_plugin_unload (db_plugin);
     563           0 :     return GNUNET_SYSERR;
     564             :   }
     565             : 
     566          28 :   return GNUNET_OK;
     567             : }
     568             : 
     569             : 
     570             : /**
     571             :  * Function called with details about deposits that have been made,
     572             :  * with the goal of executing the corresponding wire transaction.
     573             :  *
     574             :  * @param cls NULL
     575             :  * @param row_id identifies database entry
     576             :  * @param merchant_pub public key of the merchant
     577             :  * @param coin_pub public key of the coin
     578             :  * @param amount_with_fee amount that was deposited including fee
     579             :  * @param deposit_fee amount the exchange gets to keep as transaction fees
     580             :  * @param h_contract_terms hash of the proposal data known to merchant and customer
     581             :  * @param wire_deadline by which the merchant adviced that he would like the
     582             :  *        wire transfer to be executed
     583             :  * @param wire wire details for the merchant
     584             :  * @return transaction status code,  #GNUNET_DB_STATUS_SUCCESS_ONE_RESULT to continue to iterate
     585             :  */
     586             : static enum GNUNET_DB_QueryStatus
     587          21 : deposit_cb (void *cls,
     588             :             uint64_t row_id,
     589             :             const struct TALER_MerchantPublicKeyP *merchant_pub,
     590             :             const struct TALER_CoinSpendPublicKeyP *coin_pub,
     591             :             const struct TALER_Amount *amount_with_fee,
     592             :             const struct TALER_Amount *deposit_fee,
     593             :             const struct GNUNET_HashCode *h_contract_terms,
     594             :             struct GNUNET_TIME_Absolute wire_deadline,
     595             :             const json_t *wire)
     596             : {
     597             :   enum GNUNET_DB_QueryStatus qs;
     598             :   
     599          21 :   au->merchant_pub = *merchant_pub;
     600          21 :   if (GNUNET_SYSERR ==
     601          21 :       TALER_amount_subtract (&au->total_amount,
     602             :                              amount_with_fee,
     603             :                              deposit_fee))
     604             :   {
     605           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     606             :                 "Fatally malformed record at row %llu over %s\n",
     607             :                 (unsigned long long) row_id,
     608             :                 TALER_amount2s (amount_with_fee));
     609           0 :     return GNUNET_DB_STATUS_HARD_ERROR;
     610             :   }
     611          21 :   au->row_id = row_id;
     612          21 :   GNUNET_assert (NULL == au->wire);
     613          21 :   au->wire = json_incref ((json_t *) wire);
     614          21 :   TALER_JSON_hash (au->wire,
     615          21 :                    &au->h_wire);
     616          21 :   GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_NONCE,
     617          21 :                               &au->wtid,
     618             :                               sizeof (au->wtid));
     619          21 :   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
     620             :               "Starting aggregation under H(WTID)=%s\n",
     621             :               TALER_B2S (&au->wtid));
     622             : 
     623          21 :   au->wp = find_plugin (extract_type (au->wire));
     624          21 :   if (NULL == au->wp)
     625           0 :     return GNUNET_DB_STATUS_HARD_ERROR;
     626             : 
     627             :   /* make sure we have current fees */
     628          21 :   au->execution_time = GNUNET_TIME_absolute_get ();
     629          21 :   (void) GNUNET_TIME_round_abs (&au->execution_time);
     630          42 :   qs = update_fees (au->wp,
     631          21 :                     au->execution_time,
     632          21 :                     au->session);
     633          21 :   if (qs <= 0)
     634             :   {
     635           0 :     if (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS == qs)
     636           0 :       qs = GNUNET_DB_STATUS_HARD_ERROR;
     637           0 :     GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs);
     638           0 :     return qs;
     639             :   }
     640          21 :   au->wire_fee = au->wp->af->wire_fee;
     641             : 
     642          42 :   qs = db_plugin->insert_aggregation_tracking (db_plugin->cls,
     643          21 :                                                au->session,
     644          21 :                                                &au->wtid,
     645             :                                                row_id);
     646          21 :   if (qs <= 0)
     647             :   {
     648           0 :     GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs);
     649           0 :     return qs;
     650             :   }
     651          42 :   qs = db_plugin->mark_deposit_done (db_plugin->cls,
     652          21 :                                      au->session,
     653             :                                      row_id);
     654          21 :   if (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT != qs)
     655             :   {
     656           0 :     GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs);
     657           0 :     return qs;
     658             :   }
     659          21 :   return qs;
     660             : }
     661             : 
     662             : 
     663             : /**
     664             :  * Function called with details about another deposit we
     665             :  * can aggregate into an existing aggregation unit.
     666             :  *
     667             :  * @param cls NULL
     668             :  * @param row_id identifies database entry
     669             :  * @param merchant_pub public key of the merchant
     670             :  * @param coin_pub public key of the coin
     671             :  * @param amount_with_fee amount that was deposited including fee
     672             :  * @param deposit_fee amount the exchange gets to keep as transaction fees
     673             :  * @param h_contract_terms hash of the proposal data known to merchant and customer
     674             :  * @param wire_deadline by which the merchant adviced that he would like the
     675             :  *        wire transfer to be executed
     676             :  * @param wire wire details for the merchant
     677             :  * @return transaction status code
     678             :  */
     679             : static enum GNUNET_DB_QueryStatus
     680          17 : aggregate_cb (void *cls,
     681             :               uint64_t row_id,
     682             :               const struct TALER_MerchantPublicKeyP *merchant_pub,
     683             :               const struct TALER_CoinSpendPublicKeyP *coin_pub,
     684             :               const struct TALER_Amount *amount_with_fee,
     685             :               const struct TALER_Amount *deposit_fee,
     686             :               const struct GNUNET_HashCode *h_contract_terms,
     687             :               struct GNUNET_TIME_Absolute wire_deadline,
     688             :               const json_t *wire)
     689             : {
     690             :   struct TALER_Amount delta;
     691             :   enum GNUNET_DB_QueryStatus qs;
     692             : 
     693          17 :   GNUNET_break (0 ==
     694             :                 memcmp (&au->merchant_pub,
     695             :                         merchant_pub,
     696             :                         sizeof (struct TALER_MerchantPublicKeyP)));
     697             :   /* compute contribution of this coin after fees */
     698          17 :   if (GNUNET_SYSERR ==
     699          17 :       TALER_amount_subtract (&delta,
     700             :                              amount_with_fee,
     701             :                              deposit_fee))
     702             :   {
     703           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     704             :                 "Fatally malformed record at %llu over amount %s\n",
     705             :                 (unsigned long long) row_id,
     706             :                 TALER_amount2s (amount_with_fee));
     707           0 :     return GNUNET_DB_STATUS_HARD_ERROR;
     708             :   }
     709             :   /* add to total */
     710          17 :   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
     711             :               "Adding transaction amount %s to aggregation\n",
     712             :               TALER_amount2s (&delta));
     713          17 :   if (GNUNET_OK !=
     714          17 :       TALER_amount_add (&au->total_amount,
     715          17 :                         &au->total_amount,
     716             :                         &delta))
     717             :   {
     718           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     719             :                 "Overflow or currency incompatibility during aggregation at %llu\n",
     720             :                 (unsigned long long) row_id);
     721             :     /* Skip this one, but keep going! */
     722           0 :     return GNUNET_DB_STATUS_SUCCESS_ONE_RESULT;
     723             :   }
     724          17 :   if (au->rows_offset >= aggregation_limit)
     725             :   {
     726             :     /* Bug: we asked for at most #aggregation_limit results! */
     727           0 :     GNUNET_break (0);
     728             :     /* Skip this one, but keep going. */
     729           0 :     return GNUNET_DB_STATUS_SUCCESS_ONE_RESULT;
     730             :   }
     731          17 :   if (NULL == au->additional_rows)
     732           9 :     au->additional_rows = GNUNET_new_array (aggregation_limit,
     733             :                                             unsigned long long);
     734             :   /* "append" to our list of rows */
     735          17 :   au->additional_rows[au->rows_offset++] = row_id;
     736             :   /* insert into aggregation tracking table */
     737          34 :   qs = db_plugin->insert_aggregation_tracking (db_plugin->cls,
     738          17 :                                                au->session,
     739          17 :                                                &au->wtid,
     740             :                                                row_id);
     741          17 :   if (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT != qs)
     742             :   {
     743           0 :     GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs);
     744           0 :     return qs;
     745             :   }
     746          34 :   qs = db_plugin->mark_deposit_done (db_plugin->cls,
     747          17 :                                      au->session,
     748             :                                      row_id);
     749          17 :   if (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT != qs)
     750             :   {
     751           0 :     GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs);
     752           0 :     return qs;
     753             :   }
     754          17 :   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
     755             :               "Added row %llu to aggregation\n",
     756             :               (unsigned long long) row_id);
     757          17 :   return GNUNET_DB_STATUS_SUCCESS_ONE_RESULT;
     758             : }
     759             : 
     760             : 
     761             : /**
     762             :  * Function to be called with the prepared transfer data
     763             :  * when running an aggregation on a merchant.
     764             :  *
     765             :  * @param cls closure with the `struct AggregationUnit`
     766             :  * @param buf transaction data to persist, NULL on error
     767             :  * @param buf_size number of bytes in @a buf, 0 on error
     768             :  */
     769             : static void
     770             : prepare_cb (void *cls,
     771             :             const char *buf,
     772             :             size_t buf_size);
     773             : 
     774             : 
     775             : /**
     776             :  * Main work function that finds and triggers transfers for reserves
     777             :  * closures.
     778             :  *
     779             :  * @param cls closure
     780             :  */
     781             : static void
     782             : run_reserve_closures (void *cls);
     783             : 
     784             : 
     785             : /**
     786             :  * Main work function that queries the DB and aggregates transactions
     787             :  * into larger wire transfers.
     788             :  *
     789             :  * @param cls NULL
     790             :  */
     791             : static void
     792             : run_aggregation (void *cls);
     793             : 
     794             : 
     795             : /**
     796             :  * Execute the wire transfers that we have committed to
     797             :  * do.
     798             :  *
     799             :  * @param cls pointer to an `int` which we will return from main()
     800             :  */
     801             : static void
     802             : run_transfers (void *cls);
     803             : 
     804             : 
     805             : /**
     806             :  * Perform a database commit. If it fails, print a warning.
     807             :  *
     808             :  * @param session session to perform the commit for.
     809             :  * @return status of commit
     810             :  */
     811             : static enum GNUNET_DB_QueryStatus
     812          40 : commit_or_warn (struct TALER_EXCHANGEDB_Session *session)
     813             : {
     814             :   enum GNUNET_DB_QueryStatus qs;
     815             : 
     816          40 :   qs = db_plugin->commit (db_plugin->cls,
     817             :                           session);
     818          40 :   if (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS == qs)
     819          40 :     return qs;
     820           0 :   GNUNET_log ((GNUNET_DB_STATUS_SOFT_ERROR == qs)
     821             :               ? GNUNET_ERROR_TYPE_INFO
     822             :               : GNUNET_ERROR_TYPE_ERROR,
     823             :               "Failed to commit database transaction!\n");
     824           0 :   return qs;
     825             : }
     826             : 
     827             : 
     828             : /**
     829             :  * Function to be called with the prepared transfer data
     830             :  * when closing a reserve.
     831             :  *
     832             :  * @param cls closure with a `struct CloseTransferContext`
     833             :  * @param buf transaction data to persist, NULL on error
     834             :  * @param buf_size number of bytes in @a buf, 0 on error
     835             :  */
     836             : static void
     837           1 : prepare_close_cb (void *cls,
     838             :                   const char *buf,
     839             :                   size_t buf_size)
     840             : {
     841             :   enum GNUNET_DB_QueryStatus qs;
     842             :     
     843           1 :   GNUNET_assert (cls == ctc);
     844             : 
     845           1 :   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
     846             :               "Prepared for reserve closing\n");
     847           1 :   ctc->ph = NULL;
     848           1 :   if (NULL == buf)
     849             :   {
     850           0 :     GNUNET_break (0); /* why? how to best recover? */
     851           0 :     db_plugin->rollback (db_plugin->cls,
     852           0 :                          ctc->session);
     853             :     /* start again */
     854           0 :     GNUNET_free (ctc->type);
     855           0 :     GNUNET_free (ctc);
     856           0 :     ctc = NULL;
     857           0 :     task = GNUNET_SCHEDULER_add_now (&run_aggregation,
     858             :                                      NULL);
     859           0 :     return;
     860             :   }
     861             : 
     862             :   /* Commit our intention to execute the wire transfer! */
     863           2 :   qs = db_plugin->wire_prepare_data_insert (db_plugin->cls,
     864           1 :                                             ctc->session,
     865           1 :                                             ctc->type,
     866             :                                             buf,
     867             :                                             buf_size);
     868           1 :   if (GNUNET_DB_STATUS_HARD_ERROR == qs)
     869             :   {
     870           0 :     GNUNET_break (0); 
     871           0 :     db_plugin->rollback (db_plugin->cls,
     872           0 :                          ctc->session);
     873           0 :     global_ret = GNUNET_SYSERR;
     874           0 :     GNUNET_SCHEDULER_shutdown ();
     875           0 :     GNUNET_free (ctc->type);
     876           0 :     GNUNET_free (ctc);
     877           0 :     ctc = NULL;
     878           0 :     return;
     879             :   }
     880           1 :   if (GNUNET_DB_STATUS_SOFT_ERROR == qs)
     881             :   {
     882           0 :     db_plugin->rollback (db_plugin->cls,
     883           0 :                          ctc->session);
     884             :     /* start again */
     885           0 :     task = GNUNET_SCHEDULER_add_now (&run_aggregation,
     886             :                                      NULL);
     887           0 :     GNUNET_free (ctc->type);
     888           0 :     GNUNET_free (ctc);
     889           0 :     ctc = NULL;
     890           0 :     return;
     891             :   }
     892             : 
     893             :   /* finally commit */
     894           1 :   (void) commit_or_warn (ctc->session);
     895           1 :   GNUNET_free (ctc->type);
     896           1 :   GNUNET_free (ctc);
     897           1 :   ctc = NULL;
     898           1 :   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
     899             :               "Reserve closure committed, running transfer\n");
     900           1 :   task = GNUNET_SCHEDULER_add_now (&run_transfers,
     901             :                                    NULL);
     902             : }
     903             : 
     904             : 
     905             : /**
     906             :  * Closure for #expired_reserve_cb().
     907             :  */
     908             : struct ExpiredReserveContext
     909             : {
     910             : 
     911             :   /**
     912             :    * Database session we are using.
     913             :    */
     914             :   struct TALER_EXCHANGEDB_Session *session;
     915             : 
     916             :   /**
     917             :    * Set to #GNUNET_YES if the transaction continues
     918             :    * asynchronously.
     919             :    */
     920             :   int async_cont;
     921             : };
     922             : 
     923             : 
     924             : /**
     925             :  * Function called with details about expired reserves.
     926             :  * We trigger the reserve closure by inserting the respective
     927             :  * closing record and prewire instructions into the respective
     928             :  * tables.
     929             :  *
     930             :  * @param cls a `struct ExpiredReserveContext *`
     931             :  * @param reserve_pub public key of the reserve
     932             :  * @param left amount left in the reserve
     933             :  * @param account_details information about the reserve's bank account
     934             :  * @param expiration_date when did the reserve expire
     935             :  * @return transaction status code
     936             :  */
     937             : static enum GNUNET_DB_QueryStatus
     938           1 : expired_reserve_cb (void *cls,
     939             :                     const struct TALER_ReservePublicKeyP *reserve_pub,
     940             :                     const struct TALER_Amount *left,
     941             :                     const json_t *account_details,
     942             :                     struct GNUNET_TIME_Absolute expiration_date)
     943             : {
     944           1 :   struct ExpiredReserveContext *erc = cls;
     945           1 :   struct TALER_EXCHANGEDB_Session *session = erc->session;
     946             :   struct GNUNET_TIME_Absolute now;
     947             :   struct TALER_WireTransferIdentifierRawP wtid;
     948             :   struct TALER_Amount amount_without_fee;
     949             :   const struct TALER_Amount *closing_fee;
     950             :   int ret;
     951             :   enum GNUNET_DB_QueryStatus qs;
     952             :   const char *type;
     953             :   struct WirePlugin *wp;
     954             : 
     955           1 :   GNUNET_assert (NULL == ctc);
     956           1 :   now = GNUNET_TIME_absolute_get ();
     957             : 
     958             :   /* lookup wire plugin */
     959           1 :   type = extract_type (account_details);
     960           1 :   if (NULL == type)
     961             :   {
     962           0 :     GNUNET_break (0);
     963           0 :     global_ret = GNUNET_SYSERR;
     964           0 :     GNUNET_SCHEDULER_shutdown ();
     965           0 :     return GNUNET_DB_STATUS_HARD_ERROR;
     966             :   }
     967           1 :   wp = find_plugin (type);
     968           1 :   if (NULL == wp)
     969             :   {
     970           0 :     GNUNET_break (0);
     971           0 :     global_ret = GNUNET_SYSERR;
     972           0 :     GNUNET_SCHEDULER_shutdown ();
     973           0 :     return GNUNET_DB_STATUS_HARD_ERROR;
     974             :   }
     975             : 
     976             :   /* lookup `closing_fee` */
     977           1 :   qs = update_fees (wp,
     978             :                     now,
     979             :                     session);
     980           1 :   if (qs <= 0)
     981             :   {
     982           0 :     if (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS == qs)
     983           0 :       qs = GNUNET_DB_STATUS_HARD_ERROR;
     984           0 :     GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs);
     985           0 :     global_ret = GNUNET_SYSERR;
     986           0 :     if (GNUNET_DB_STATUS_HARD_ERROR == qs)
     987           0 :       GNUNET_SCHEDULER_shutdown ();
     988           0 :     return qs;
     989             :   }
     990           1 :   closing_fee = &wp->af->closing_fee;
     991             : 
     992             :   /* calculate transfer amount */
     993           1 :   ret = TALER_amount_subtract (&amount_without_fee,
     994             :                                left,
     995             :                                closing_fee);
     996           1 :   if ( (GNUNET_SYSERR == ret) ||
     997             :        (GNUNET_NO == ret) )
     998             :   {
     999             :     /* Closing fee higher than remaining balance, close
    1000             :        without wire transfer. */
    1001           0 :     closing_fee = left;
    1002           0 :     TALER_amount_get_zero (left->currency,
    1003             :                            &amount_without_fee);
    1004             :   }
    1005             : 
    1006             :   /* NOTE: sizeof (*reserve_pub) == sizeof (wtid) right now, but to
    1007             :      be future-compatible, we use the memset + min construction */
    1008           1 :   memset (&wtid,
    1009             :           0,
    1010             :           sizeof (wtid));
    1011           1 :   memcpy (&wtid,
    1012             :           reserve_pub,
    1013             :           GNUNET_MIN (sizeof (wtid),
    1014             :                       sizeof (*reserve_pub)));
    1015           1 :   qs = db_plugin->insert_reserve_closed (db_plugin->cls,
    1016             :                                          session,
    1017             :                                          reserve_pub,
    1018             :                                          now,
    1019             :                                          account_details,
    1020             :                                          &wtid,
    1021             :                                          left,
    1022             :                                          closing_fee);
    1023           1 :   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
    1024             :               "Closing reserve %s over %s (%d, %d)\n",
    1025             :               TALER_B2S (reserve_pub),
    1026             :               TALER_amount2s (left),
    1027             :               ret,
    1028             :               qs);
    1029           1 :   if ( (GNUNET_OK == ret) &&
    1030             :        (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT == qs) )
    1031             :   {
    1032             :     /* success, perform wire transfer */
    1033           1 :     if (GNUNET_SYSERR ==
    1034           1 :         wp->wire_plugin->amount_round (wp->wire_plugin->cls,
    1035             :                                        &amount_without_fee))
    1036             :     {
    1037           0 :       GNUNET_break (0);
    1038           0 :       global_ret = GNUNET_SYSERR;
    1039           0 :       GNUNET_SCHEDULER_shutdown ();
    1040           0 :       return GNUNET_DB_STATUS_HARD_ERROR;
    1041             :     }
    1042           1 :     ctc = GNUNET_new (struct CloseTransferContext);
    1043           1 :     ctc->wp = wp;
    1044           1 :     ctc->session = session;
    1045           1 :     ctc->type = GNUNET_strdup (type);
    1046           1 :     ctc->ph
    1047           1 :       = wp->wire_plugin->prepare_wire_transfer (wp->wire_plugin->cls,
    1048             :                                                 account_details,
    1049             :                                                 &amount_without_fee,
    1050             :                                                 exchange_base_url,
    1051             :                                                 &wtid,
    1052             :                                                 &prepare_close_cb,
    1053             :                                                 ctc);
    1054           1 :     if (NULL == ctc->ph)
    1055             :     {
    1056           0 :       GNUNET_break (0);
    1057           0 :       global_ret = GNUNET_SYSERR;
    1058           0 :       GNUNET_SCHEDULER_shutdown ();
    1059           0 :       GNUNET_free (ctc->type);
    1060           0 :       GNUNET_free (ctc);
    1061           0 :       ctc = NULL;
    1062           0 :       return GNUNET_DB_STATUS_HARD_ERROR;
    1063             :     }
    1064           1 :     erc->async_cont = GNUNET_YES;
    1065           1 :     return GNUNET_DB_STATUS_SUCCESS_ONE_RESULT;
    1066             :   }
    1067             :   /* Check for hard failure */
    1068           0 :   if ( (GNUNET_SYSERR == ret) ||
    1069             :        (GNUNET_DB_STATUS_HARD_ERROR == qs) )
    1070             :   {
    1071           0 :     GNUNET_break (0);
    1072           0 :     global_ret = GNUNET_SYSERR;
    1073           0 :     GNUNET_SCHEDULER_shutdown ();
    1074           0 :     return GNUNET_DB_STATUS_HARD_ERROR;
    1075             :   }
    1076             :   /* Reserve balance was almost zero OR soft error */
    1077           0 :   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
    1078             :               "Reserve was virtually empty, moving on\n");
    1079           0 :   return qs;
    1080             : }
    1081             : 
    1082             : 
    1083             : /**
    1084             :  * Main work function that finds and triggers transfers for reserves
    1085             :  * closures.
    1086             :  *
    1087             :  * @param cls closure
    1088             :  */
    1089             : static void
    1090          45 : run_reserve_closures (void *cls)
    1091             : {
    1092             :   struct TALER_EXCHANGEDB_Session *session;
    1093             :   enum GNUNET_DB_QueryStatus qs;
    1094             :   const struct GNUNET_SCHEDULER_TaskContext *tc;
    1095             :   struct ExpiredReserveContext erc;
    1096             :   
    1097          45 :   task = NULL;
    1098          45 :   reserves_idle = GNUNET_NO;
    1099          45 :   tc = GNUNET_SCHEDULER_get_task_context ();
    1100          45 :   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
    1101          44 :     return;
    1102          45 :   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
    1103             :               "Checking for reserves to close\n");
    1104          45 :   if (NULL == (session = db_plugin->get_session (db_plugin->cls)))
    1105             :   {
    1106           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
    1107             :                 "Failed to obtain database session!\n");
    1108           0 :     global_ret = GNUNET_SYSERR;
    1109           0 :     GNUNET_SCHEDULER_shutdown ();
    1110           0 :     return;
    1111             :   }
    1112          45 :   if (GNUNET_OK !=
    1113          45 :       db_plugin->start (db_plugin->cls,
    1114             :                         session))
    1115             :   {
    1116           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
    1117             :                 "Failed to start database transaction!\n");
    1118           0 :     global_ret = GNUNET_SYSERR;
    1119           0 :     GNUNET_SCHEDULER_shutdown ();
    1120           0 :     return;
    1121             :   }
    1122          45 :   erc.session = session;
    1123          45 :   erc.async_cont = GNUNET_NO;
    1124          45 :   qs = db_plugin->get_expired_reserves (db_plugin->cls,
    1125             :                                         session,
    1126             :                                         GNUNET_TIME_absolute_get (),
    1127             :                                         &expired_reserve_cb,
    1128             :                                         &erc);
    1129          45 :   switch (qs) 
    1130             :   {
    1131             :   case GNUNET_DB_STATUS_HARD_ERROR:    
    1132           0 :     GNUNET_break (0);
    1133           0 :     db_plugin->rollback (db_plugin->cls,
    1134             :                          session);
    1135           0 :     global_ret = GNUNET_SYSERR;
    1136           0 :     GNUNET_SCHEDULER_shutdown ();
    1137           0 :     return;
    1138             :   case GNUNET_DB_STATUS_SOFT_ERROR:
    1139           0 :     db_plugin->rollback (db_plugin->cls,
    1140             :                          session);
    1141           0 :     task = GNUNET_SCHEDULER_add_now (&run_reserve_closures,
    1142             :                                      NULL);
    1143           0 :     return;
    1144             :   case GNUNET_DB_STATUS_SUCCESS_NO_RESULTS:
    1145          44 :     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
    1146             :                 "No more idle reserves, going back to aggregation\n");
    1147          44 :     reserves_idle = GNUNET_YES;
    1148          44 :     db_plugin->rollback (db_plugin->cls,
    1149             :                          session);
    1150          44 :     task = GNUNET_SCHEDULER_add_now (&run_aggregation,
    1151             :                                      NULL);
    1152          44 :     return;
    1153             :   case GNUNET_DB_STATUS_SUCCESS_ONE_RESULT:
    1154           1 :     if (GNUNET_YES == erc.async_cont)
    1155           1 :       break;
    1156           0 :     (void) commit_or_warn (session);
    1157           0 :     task = GNUNET_SCHEDULER_add_now (&run_reserve_closures,
    1158             :                                      NULL);
    1159           0 :     return;
    1160             :   }
    1161             : }
    1162             : 
    1163             : 
    1164             : /**
    1165             :  * Main work function that queries the DB and aggregates transactions
    1166             :  * into larger wire transfers.
    1167             :  *
    1168             :  * @param cls NULL
    1169             :  */
    1170             : static void
    1171          94 : run_aggregation (void *cls)
    1172             : {
    1173             :   static int swap;
    1174             :   struct TALER_EXCHANGEDB_Session *session;
    1175             :   enum GNUNET_DB_QueryStatus qs;
    1176             :   const struct GNUNET_SCHEDULER_TaskContext *tc;
    1177             : 
    1178          94 :   task = NULL;
    1179          94 :   tc = GNUNET_SCHEDULER_get_task_context ();
    1180          94 :   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
    1181           0 :     return;
    1182          94 :   if (0 == (++swap % 2))
    1183             :   {
    1184          33 :     task = GNUNET_SCHEDULER_add_now (&run_reserve_closures,
    1185             :                                      NULL);
    1186          33 :     return;
    1187             :   }
    1188          61 :   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
    1189             :               "Checking for ready deposits to aggregate\n");
    1190          61 :   if (NULL == (session = db_plugin->get_session (db_plugin->cls)))
    1191             :   {
    1192           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
    1193             :                 "Failed to obtain database session!\n");
    1194           0 :     global_ret = GNUNET_SYSERR;
    1195           0 :     GNUNET_SCHEDULER_shutdown ();
    1196           0 :     return;
    1197             :   }
    1198          61 :   if (GNUNET_OK !=
    1199          61 :       db_plugin->start_deferred_wire_out (db_plugin->cls,
    1200             :                                           session))
    1201             :   {
    1202           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
    1203             :                 "Failed to start database transaction!\n");
    1204           0 :     global_ret = GNUNET_SYSERR;
    1205           0 :     GNUNET_SCHEDULER_shutdown ();
    1206           0 :     return;
    1207             :   }
    1208          61 :   au = GNUNET_new (struct AggregationUnit);
    1209          61 :   au->session = session;
    1210          61 :   qs = db_plugin->get_ready_deposit (db_plugin->cls,
    1211             :                                      session,
    1212             :                                      &deposit_cb,
    1213             :                                      au);
    1214          61 :   if (0 >= qs)
    1215             :   {
    1216          40 :     cleanup_au ();
    1217          40 :     db_plugin->rollback (db_plugin->cls,
    1218             :                          session);
    1219          40 :     if (GNUNET_DB_STATUS_HARD_ERROR == qs)
    1220             :     {
    1221           0 :       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
    1222             :                   "Failed to execute deposit iteration!\n");
    1223           0 :       global_ret = GNUNET_SYSERR;
    1224           0 :       GNUNET_SCHEDULER_shutdown ();
    1225           0 :       return;
    1226             :     }
    1227          40 :     if (GNUNET_DB_STATUS_SOFT_ERROR == qs)
    1228             :     {
    1229             :       /* should re-try immediately */
    1230           0 :       swap--; /* do not count failed attempts */
    1231           0 :       task = GNUNET_SCHEDULER_add_now (&run_aggregation,
    1232             :                                        NULL);
    1233           0 :       return;
    1234             :     }
    1235          40 :     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
    1236             :                 "No more ready deposits, going to sleep\n");
    1237          80 :     if ( (GNUNET_YES == test_mode) &&
    1238          40 :          (swap >= 2) )
    1239             :     {
    1240             :       /* in test mode, shutdown if we end up being idle */
    1241          28 :       GNUNET_SCHEDULER_shutdown ();
    1242             :     }
    1243             :     else
    1244             :     {
    1245          12 :       if ( (GNUNET_NO == reserves_idle) ||
    1246           0 :            (GNUNET_YES == test_mode) )
    1247             :         /* Possibly more to on reserves, go for it immediately */
    1248          12 :         task = GNUNET_SCHEDULER_add_now (&run_reserve_closures,
    1249             :                                          NULL);
    1250             :       else
    1251             :         /* nothing to do, sleep for a minute and try again */
    1252           0 :         task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
    1253             :                                              &run_aggregation,
    1254             :                                              NULL);
    1255             :     }
    1256          40 :     return;
    1257             :   }
    1258             : 
    1259             :   /* Now try to find other deposits to aggregate */
    1260          21 :   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
    1261             :               "Found ready deposit for %s, aggregating\n",
    1262             :               TALER_B2S (&au->merchant_pub));
    1263          42 :   qs = db_plugin->iterate_matching_deposits (db_plugin->cls,
    1264             :                                              session,
    1265          21 :                                              &au->h_wire,
    1266          21 :                                              &au->merchant_pub,
    1267             :                                              &aggregate_cb,
    1268             :                                              au,
    1269             :                                              aggregation_limit);
    1270          42 :   if ( (GNUNET_DB_STATUS_HARD_ERROR == qs) ||
    1271          21 :        (GNUNET_YES == au->failed) )
    1272             :   {
    1273           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
    1274             :                 "Failed to execute deposit iteration!\n");
    1275           0 :     cleanup_au ();
    1276           0 :     db_plugin->rollback (db_plugin->cls,
    1277             :                          session);
    1278           0 :     global_ret = GNUNET_SYSERR;
    1279           0 :     GNUNET_SCHEDULER_shutdown ();
    1280           0 :     return;
    1281             :   }
    1282          21 :   if (GNUNET_DB_STATUS_SOFT_ERROR == qs)
    1283             :   {
    1284             :     /* serializiability issue, try again */
    1285           0 :     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
    1286             :                 "Serialization issue, trying again later!\n");
    1287           0 :     db_plugin->rollback (db_plugin->cls,
    1288             :                          session);
    1289           0 :     task = GNUNET_SCHEDULER_add_now (&run_aggregation,
    1290             :                                      NULL);
    1291           0 :     return;
    1292             :   }
    1293             :   
    1294             :   /* Subtract wire transfer fee and round to the unit supported by the
    1295             :      wire transfer method; Check if after rounding down, we still have
    1296             :      an amount to transfer, and if not mark as 'tiny'. */
    1297          21 :   if ( (GNUNET_OK !=
    1298          21 :         TALER_amount_subtract (&au->final_amount,
    1299          21 :                                &au->total_amount,
    1300          38 :                                &au->wire_fee)) ||
    1301             :        (GNUNET_SYSERR ==
    1302          34 :         au->wp->wire_plugin->amount_round (au->wp->wire_plugin->cls,
    1303          34 :                                            &au->final_amount)) ||
    1304          31 :        ( (0 == au->final_amount.value) &&
    1305          14 :          (0 == au->final_amount.fraction) ) )
    1306             :   {
    1307           4 :     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
    1308             :                 "Aggregate value too low for transfer (%d/%s)\n",
    1309             :                 qs,
    1310             :                 TALER_amount2s (&au->final_amount));
    1311             :     /* Rollback ongoing transaction, as we will not use the respective
    1312             :        WTID and thus need to remove the tracking data */
    1313           4 :     db_plugin->rollback (db_plugin->cls,
    1314             :                          session);
    1315             : 
    1316             :     /* There were results, just the value was too low.  Start another
    1317             :        transaction to mark all* of the selected deposits as minor! */
    1318           4 :     if (GNUNET_OK !=
    1319           4 :         db_plugin->start (db_plugin->cls,
    1320             :                           session))
    1321             :     {
    1322           0 :       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
    1323             :                   "Failed to start database transaction!\n");
    1324           0 :       global_ret = GNUNET_SYSERR;
    1325           0 :       cleanup_au ();      
    1326           0 :       GNUNET_SCHEDULER_shutdown ();
    1327           0 :       return;
    1328             :     }
    1329             :     /* Mark transactions by row_id as minor */
    1330           8 :     qs = db_plugin->mark_deposit_tiny (db_plugin->cls,
    1331             :                                        session,
    1332           4 :                                        au->row_id);
    1333           4 :     if (0 <= qs)
    1334             :     {
    1335           9 :       for (unsigned int i=0;i<au->rows_offset;i++)
    1336             :       {
    1337          10 :         qs = db_plugin->mark_deposit_tiny (db_plugin->cls,
    1338             :                                            session,
    1339           5 :                                            au->additional_rows[i]);
    1340           5 :         if (0 > qs)
    1341           0 :           break;
    1342             :       }
    1343             :     }
    1344           4 :     if (GNUNET_DB_STATUS_SOFT_ERROR == qs)
    1345             :     {
    1346           0 :       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
    1347             :                   "Serialization issue, trying again later!\n");
    1348           0 :       db_plugin->rollback (db_plugin->cls,
    1349             :                            session);
    1350           0 :       cleanup_au ();
    1351             :       /* start again */
    1352           0 :       task = GNUNET_SCHEDULER_add_now (&run_aggregation,
    1353             :                                        NULL);
    1354           0 :       return;
    1355             :     }
    1356           4 :     if (GNUNET_DB_STATUS_HARD_ERROR == qs)
    1357             :     {
    1358           0 :       db_plugin->rollback (db_plugin->cls,
    1359             :                            session);
    1360           0 :       cleanup_au ();
    1361           0 :       GNUNET_SCHEDULER_shutdown ();
    1362           0 :       return;
    1363             :     }
    1364             :     /* commit */
    1365           4 :     (void) commit_or_warn (session);
    1366           4 :     cleanup_au ();
    1367             :     /* start again */
    1368           4 :     task = GNUNET_SCHEDULER_add_now (&run_aggregation,
    1369             :                                      NULL);
    1370           4 :     return;
    1371             :   }
    1372             :   {
    1373             :     char *amount_s;
    1374             : 
    1375          17 :     amount_s = TALER_amount_to_string (&au->final_amount);
    1376          17 :     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
    1377             :                 "Preparing wire transfer of %s to %s\n",
    1378             :                 amount_s,
    1379             :                 TALER_B2S (&au->merchant_pub));
    1380          17 :     GNUNET_free (amount_s);
    1381             :   }
    1382          51 :   au->ph = au->wp->wire_plugin->prepare_wire_transfer (au->wp->wire_plugin->cls,
    1383          17 :                                                        au->wire,
    1384          17 :                                                        &au->final_amount,
    1385             :                                                        exchange_base_url,
    1386          17 :                                                        &au->wtid,
    1387             :                                                        &prepare_cb,
    1388             :                                                        au);
    1389          17 :   if (NULL == au->ph)
    1390             :   {
    1391           0 :     GNUNET_break (0); /* why? how to best recover? */
    1392           0 :     db_plugin->rollback (db_plugin->cls,
    1393             :                          session);
    1394           0 :     cleanup_au ();
    1395             :     /* start again */
    1396           0 :     task = GNUNET_SCHEDULER_add_now (&run_aggregation,
    1397             :                                      NULL);
    1398           0 :     return;
    1399             :   }
    1400             :   /* otherwise we continue with #prepare_cb(), see below */
    1401             : }
    1402             : 
    1403             : 
    1404             : /**
    1405             :  * Function to be called with the prepared transfer data.
    1406             :  *
    1407             :  * @param cls NULL
    1408             :  * @param buf transaction data to persist, NULL on error
    1409             :  * @param buf_size number of bytes in @a buf, 0 on error
    1410             :  */
    1411             : static void
    1412          17 : prepare_cb (void *cls,
    1413             :             const char *buf,
    1414             :             size_t buf_size)
    1415             : {
    1416          17 :   struct TALER_EXCHANGEDB_Session *session = au->session;
    1417             :   enum GNUNET_DB_QueryStatus qs;
    1418             : 
    1419          17 :   GNUNET_free_non_null (au->additional_rows);
    1420          17 :   au->additional_rows = NULL;
    1421          17 :   if (NULL == buf)
    1422             :   {
    1423           0 :     GNUNET_break (0); /* why? how to best recover? */
    1424           0 :     db_plugin->rollback (db_plugin->cls,
    1425             :                          session);
    1426             :     /* start again */
    1427           0 :     task = GNUNET_SCHEDULER_add_now (&run_aggregation,
    1428             :                                      NULL);
    1429           0 :     cleanup_au ();
    1430           0 :     return;
    1431             :   }
    1432             : 
    1433          17 :   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
    1434             :               "Storing %u bytes of wire prepare data\n",
    1435             :               (unsigned int) buf_size);
    1436             :   /* Commit our intention to execute the wire transfer! */
    1437          34 :   qs = db_plugin->wire_prepare_data_insert (db_plugin->cls,
    1438             :                                             session,
    1439          17 :                                             au->wp->type,
    1440             :                                             buf,
    1441             :                                             buf_size);
    1442             :   /* Commit the WTID data to 'wire_out' to finally satisfy aggregation
    1443             :      table constraints */
    1444          17 :   if (qs >= 0)
    1445          34 :     qs = db_plugin->store_wire_transfer_out (db_plugin->cls,
    1446             :                                              session,
    1447          17 :                                              au->execution_time,
    1448          17 :                                              &au->wtid,
    1449          17 :                                              au->wire,
    1450          17 :                                              &au->final_amount);
    1451          17 :   cleanup_au ();
    1452          17 :   if (GNUNET_DB_STATUS_SOFT_ERROR == qs)
    1453             :   {
    1454           0 :     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
    1455             :                 "Serialization issue for prepared wire data; trying again later!\n");
    1456           0 :     db_plugin->rollback (db_plugin->cls,
    1457             :                          session);
    1458             :     /* start again */
    1459           0 :     task = GNUNET_SCHEDULER_add_now (&run_aggregation,
    1460             :                                      NULL);
    1461           0 :     return;
    1462             :   }
    1463          17 :   if (GNUNET_DB_STATUS_HARD_ERROR == qs)
    1464             :   {
    1465           0 :     GNUNET_break (0);
    1466           0 :     db_plugin->rollback (db_plugin->cls,
    1467             :                          session);
    1468             :     /* die hard */
    1469           0 :     global_ret = GNUNET_SYSERR;
    1470           0 :     GNUNET_SCHEDULER_shutdown ();
    1471           0 :     return;
    1472             :   }
    1473             : 
    1474          17 :   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
    1475             :               "Stored wire transfer out instructions\n");
    1476             : 
    1477             :   /* Now we can finally commit the overall transaction, as we are
    1478             :      again consistent if all of this passes. */
    1479          17 :   switch (commit_or_warn (session))
    1480             :   {
    1481             :   case GNUNET_DB_STATUS_SOFT_ERROR:
    1482             :     /* try again */
    1483           0 :     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
    1484             :                 "Commit issue for prepared wire data; trying again later!\n");
    1485           0 :     task = GNUNET_SCHEDULER_add_now (&run_aggregation,
    1486             :                                      NULL);
    1487           0 :     return;
    1488             :   case GNUNET_DB_STATUS_HARD_ERROR:
    1489           0 :     GNUNET_break (0);
    1490           0 :     global_ret = GNUNET_SYSERR;
    1491           0 :     GNUNET_SCHEDULER_shutdown ();
    1492           0 :     return;
    1493             :   case GNUNET_DB_STATUS_SUCCESS_NO_RESULTS:
    1494          17 :     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
    1495             :                 "Preparation complete, switching to transfer mode\n");
    1496             :     /* run alternative task: actually do wire transfer! */
    1497          17 :     task = GNUNET_SCHEDULER_add_now (&run_transfers,
    1498             :                                      NULL);
    1499          17 :     return;
    1500             :   default:
    1501           0 :     GNUNET_break (0);
    1502           0 :     global_ret = GNUNET_SYSERR;
    1503           0 :     GNUNET_SCHEDULER_shutdown ();
    1504           0 :     return;
    1505             :   }
    1506             : }
    1507             : 
    1508             : 
    1509             : /**
    1510             :  * Function called with the result from the execute step.
    1511             :  *
    1512             :  * @param cls NULL
    1513             :  * @param success #GNUNET_OK on success, #GNUNET_SYSERR on failure
    1514             :  * @param serial_id unique ID of the wire transfer in the bank's records; UINT64_MAX on error
    1515             :  * @param emsg NULL on success, otherwise an error message
    1516             :  */
    1517             : static void
    1518          18 : wire_confirm_cb (void *cls,
    1519             :                  int success,
    1520             :                  uint64_t serial_id,
    1521             :                  const char *emsg)
    1522             : {
    1523          18 :   struct TALER_EXCHANGEDB_Session *session = wpd->session;
    1524             :   enum GNUNET_DB_QueryStatus qs;
    1525             : 
    1526          18 :   wpd->eh = NULL;
    1527          18 :   if (GNUNET_SYSERR == success)
    1528             :   {
    1529           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
    1530             :                 "Wire transaction failed: %s\n",
    1531             :                 emsg);
    1532           0 :     db_plugin->rollback (db_plugin->cls,
    1533             :                          session);
    1534           0 :     global_ret = GNUNET_SYSERR;
    1535           0 :     GNUNET_SCHEDULER_shutdown ();
    1536           0 :     GNUNET_free (wpd);
    1537           0 :     wpd = NULL;
    1538           0 :     return;
    1539             :   }
    1540          36 :   qs = db_plugin->wire_prepare_data_mark_finished (db_plugin->cls,
    1541             :                                                    session,
    1542          18 :                                                    wpd->row_id);
    1543          18 :   if (0 >= qs)
    1544             :   {
    1545           0 :     GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs);
    1546           0 :     db_plugin->rollback (db_plugin->cls,
    1547             :                          session);
    1548           0 :     if (GNUNET_DB_STATUS_SOFT_ERROR == qs)
    1549             :     {
    1550             :       /* try again */
    1551           0 :       task = GNUNET_SCHEDULER_add_now (&run_aggregation,
    1552             :                                        NULL);
    1553             :     }
    1554             :     else
    1555             :     {
    1556           0 :       global_ret = GNUNET_SYSERR;
    1557           0 :       GNUNET_SCHEDULER_shutdown ();
    1558             :     }
    1559           0 :     GNUNET_free (wpd);
    1560           0 :     wpd = NULL;
    1561           0 :     return;
    1562             :   }
    1563          18 :   GNUNET_free (wpd);
    1564          18 :   wpd = NULL;
    1565          18 :   switch (commit_or_warn (session))
    1566             :   {
    1567             :   case GNUNET_DB_STATUS_SOFT_ERROR:
    1568             :     /* try again */
    1569           0 :     task = GNUNET_SCHEDULER_add_now (&run_aggregation,
    1570             :                                      NULL);
    1571           0 :     return;
    1572             :   case GNUNET_DB_STATUS_HARD_ERROR:
    1573           0 :     GNUNET_break (0);
    1574           0 :     global_ret = GNUNET_SYSERR;
    1575           0 :     GNUNET_SCHEDULER_shutdown ();
    1576           0 :     return;
    1577             :   case GNUNET_DB_STATUS_SUCCESS_NO_RESULTS:
    1578          18 :     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
    1579             :                 "Wire transfer complete\n");
    1580             :     /* continue with #run_transfers(), just to guard
    1581             :        against the unlikely case that there are more. */
    1582          18 :     task = GNUNET_SCHEDULER_add_now (&run_transfers,
    1583             :                                      NULL);
    1584          18 :     return;
    1585             :   default:
    1586           0 :     GNUNET_break (0);
    1587           0 :     global_ret = GNUNET_SYSERR;
    1588           0 :     GNUNET_SCHEDULER_shutdown ();
    1589           0 :     return;
    1590             :   }
    1591             : }
    1592             : 
    1593             : 
    1594             : /**
    1595             :  * Callback with data about a prepared transaction.
    1596             :  *
    1597             :  * @param cls NULL
    1598             :  * @param rowid row identifier used to mark prepared transaction as done
    1599             :  * @param wire_method wire method the preparation was done for
    1600             :  * @param buf transaction data that was persisted, NULL on error
    1601             :  * @param buf_size number of bytes in @a buf, 0 on error
    1602             :  */
    1603             : static void
    1604          18 : wire_prepare_cb (void *cls,
    1605             :                  uint64_t rowid,
    1606             :                  const char *wire_method,
    1607             :                  const char *buf,
    1608             :                  size_t buf_size)
    1609             : {
    1610          18 :   wpd->row_id = rowid;
    1611          18 :   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
    1612             :               "Starting wire transfer %llu\n",
    1613             :               (unsigned long long) rowid);
    1614          18 :   wpd->wp = find_plugin (wire_method);
    1615          18 :   wpd->eh = wpd->wp->wire_plugin->execute_wire_transfer (wpd->wp->wire_plugin->cls,
    1616             :                                                          buf,
    1617             :                                                          buf_size,
    1618             :                                                          &wire_confirm_cb,
    1619             :                                                          NULL);
    1620          18 :   if (NULL == wpd->eh)
    1621             :   {
    1622           0 :     GNUNET_break (0); /* why? how to best recover? */
    1623           0 :     db_plugin->rollback (db_plugin->cls,
    1624           0 :                          wpd->session);
    1625           0 :     global_ret = GNUNET_SYSERR;
    1626           0 :     GNUNET_SCHEDULER_shutdown ();
    1627           0 :     GNUNET_free (wpd);
    1628           0 :     wpd = NULL;
    1629           0 :     return;
    1630             :   }
    1631             : }
    1632             : 
    1633             : 
    1634             : /**
    1635             :  * Execute the wire transfers that we have committed to
    1636             :  * do.
    1637             :  *
    1638             :  * @param cls NULL
    1639             :  */
    1640             : static void
    1641          64 : run_transfers (void *cls)
    1642             : {
    1643             :   enum GNUNET_DB_QueryStatus qs;
    1644             :   struct TALER_EXCHANGEDB_Session *session;
    1645             :   const struct GNUNET_SCHEDULER_TaskContext *tc;
    1646             : 
    1647          64 :   task = NULL;
    1648          64 :   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
    1649             :               "Checking for pending wire transfers\n");
    1650          64 :   tc = GNUNET_SCHEDULER_get_task_context ();
    1651          64 :   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
    1652           0 :     return;
    1653          64 :   if (NULL == (session = db_plugin->get_session (db_plugin->cls)))
    1654             :   {
    1655           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
    1656             :                 "Failed to obtain database session!\n");
    1657           0 :     global_ret = GNUNET_SYSERR;
    1658           0 :     GNUNET_SCHEDULER_shutdown ();
    1659           0 :     return;
    1660             :   }
    1661          64 :   if (GNUNET_OK !=
    1662          64 :       db_plugin->start (db_plugin->cls,
    1663             :                         session))
    1664             :   {
    1665           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
    1666             :                 "Failed to start database transaction!\n");
    1667           0 :     global_ret = GNUNET_SYSERR;
    1668           0 :     GNUNET_SCHEDULER_shutdown ();
    1669           0 :     return;
    1670             :   }
    1671          64 :   wpd = GNUNET_new (struct WirePrepareData);
    1672          64 :   wpd->session = session;
    1673          64 :   qs = db_plugin->wire_prepare_data_get (db_plugin->cls,
    1674             :                                          session,
    1675             :                                          &wire_prepare_cb,
    1676             :                                          NULL);
    1677          64 :   if (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT == qs)
    1678          18 :     return;  /* continues in #wire_prepare_cb() */
    1679          46 :   db_plugin->rollback (db_plugin->cls,
    1680             :                        session);
    1681          46 :   GNUNET_free (wpd);
    1682          46 :   wpd = NULL;
    1683          46 :   switch (qs)
    1684             :   {
    1685             :   case GNUNET_DB_STATUS_HARD_ERROR:
    1686           0 :     GNUNET_break (0);
    1687           0 :     global_ret = GNUNET_SYSERR;
    1688           0 :     GNUNET_SCHEDULER_shutdown ();
    1689           0 :     return;
    1690             :   case GNUNET_DB_STATUS_SOFT_ERROR:
    1691             :     /* try again */
    1692           0 :     task = GNUNET_SCHEDULER_add_now (&run_transfers,
    1693             :                                      NULL);      
    1694           0 :     return;
    1695             :   case GNUNET_DB_STATUS_SUCCESS_NO_RESULTS:
    1696             :     /* no more prepared wire transfers, go back to aggregation! */
    1697          46 :     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
    1698             :                 "No more pending wire transfers, starting aggregation\n");
    1699          46 :     task = GNUNET_SCHEDULER_add_now (&run_aggregation,
    1700             :                                      NULL);
    1701          46 :     return;
    1702             :   case GNUNET_DB_STATUS_SUCCESS_ONE_RESULT:
    1703             :     /* should be impossible */
    1704           0 :     GNUNET_assert (0);
    1705             :   }
    1706             : }
    1707             : 
    1708             : 
    1709             : /**
    1710             :  * First task.
    1711             :  *
    1712             :  * @param cls closure, NULL
    1713             :  * @param args remaining command-line arguments
    1714             :  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
    1715             :  * @param c configuration
    1716             :  */
    1717             : static void
    1718          28 : run (void *cls,
    1719             :      char *const *args,
    1720             :      const char *cfgfile,
    1721             :      const struct GNUNET_CONFIGURATION_Handle *c)
    1722             : {
    1723          28 :   if (GNUNET_OK !=
    1724          28 :       GNUNET_CONFIGURATION_get_value_string (c,
    1725             :                                              "exchange",
    1726             :                                              "BASE_URL",
    1727             :                                              &exchange_base_url))
    1728             :   {
    1729           0 :     GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
    1730             :                                "exchange",
    1731             :                                "BASE_URL");
    1732           0 :     global_ret = 1;
    1733           0 :     return;
    1734             :   }
    1735          28 :   cfg = GNUNET_CONFIGURATION_dup (c);
    1736          28 :   if (GNUNET_OK != exchange_serve_process_config ())
    1737             :   {
    1738           0 :     GNUNET_CONFIGURATION_destroy (cfg);
    1739           0 :     cfg = NULL;
    1740           0 :     global_ret = 1;
    1741           0 :     return;
    1742             :   }
    1743          28 :   task = GNUNET_SCHEDULER_add_now (&run_transfers,
    1744             :                                    NULL);
    1745          28 :   GNUNET_SCHEDULER_add_shutdown (&shutdown_task,
    1746             :                                  cls);
    1747             : }
    1748             : 
    1749             : 
    1750             : /**
    1751             :  * The main function of the taler-exchange-aggregator.
    1752             :  *
    1753             :  * @param argc number of arguments from the command line
    1754             :  * @param argv command line arguments
    1755             :  * @return 0 ok, 1 on error
    1756             :  */
    1757             : int
    1758          28 : main (int argc,
    1759             :       char *const *argv)
    1760             : {
    1761          28 :   struct GNUNET_GETOPT_CommandLineOption options[] = {
    1762             :     GNUNET_GETOPT_option_flag ('t',
    1763             :                                "test",
    1764             :                                "run in test mode and exit when idle",
    1765             :                                &test_mode),
    1766             :     GNUNET_GETOPT_option_version (VERSION "-" VCS_VERSION),
    1767             :     GNUNET_GETOPT_OPTION_END
    1768             :   };
    1769             : 
    1770          28 :   if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv,
    1771             :                                                  &argc, &argv))
    1772           0 :     return 2;
    1773          28 :   if (GNUNET_OK !=
    1774          28 :       GNUNET_PROGRAM_run (argc, argv,
    1775             :                           "taler-exchange-aggregator",
    1776             :                           gettext_noop ("background process that aggregates and executes wire transfers to merchants"),
    1777             :                           options,
    1778             :                           &run, NULL))
    1779             :   {
    1780           0 :     GNUNET_free ((void*) argv);
    1781           0 :     return 1;
    1782             :   }
    1783          28 :   GNUNET_free ((void*) argv);
    1784          28 :   return global_ret;
    1785             : }
    1786             : 
    1787             : /* end of taler-exchange-aggregator.c */

Generated by: LCOV version 1.13