aboutsummaryrefslogtreecommitdiff
path: root/src/backend/taler-merchant-reconciliation.c
blob: 0611204dcf980d2970b9fd9c0538c01d512dbd24 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
/*
  This file is part of TALER
  Copyright (C) 2023-2024 Taler Systems SA

  TALER is free software; you can redistribute it and/or modify it under the
  terms of the GNU Affero General Public License as published by the Free Software
  Foundation; either version 3, or (at your option) any later version.

  TALER is distributed in the hope that it will be useful, but WITHOUT ANY
  WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more details.

  You should have received a copy of the GNU Affero General Public License along with
  TALER; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
*/
/**
 * @file taler-merchant-reconciliation.c
 * @brief Process that reconciles information about incoming bank transfers with orders by asking the exchange
 * @author Christian Grothoff
 */
#include "platform.h"
#include <gnunet/gnunet_util_lib.h>
#include <jansson.h>
#include <pthread.h>
#include <taler/taler_dbevents.h>
#include "taler_merchant_bank_lib.h"
#include "taler_merchantdb_lib.h"
#include "taler_merchantdb_plugin.h"

/**
 * Timeout for the exchange interaction.  Rather long as we should do
 * long-polling and do not want to wake up too often.
 */
#define EXCHANGE_TIMEOUT GNUNET_TIME_relative_multiply ( \
          GNUNET_TIME_UNIT_MINUTES, \
          30)

/**
 * How many inquiries do we process concurrently at most.
 */
#define OPEN_INQUIRY_LIMIT 1024

/**
 * How many inquiries do we process concurrently per exchange at most.
 */
#define EXCHANGE_INQUIRY_LIMIT 16


/**
 * Information about an inquiry job.
 */
struct Inquiry;


/**
 * Information about an exchange.
 */
struct Exchange
{
  /**
   * Kept in a DLL.
   */
  struct Exchange *next;

  /**
   * Kept in a DLL.
   */
  struct Exchange *prev;

  /**
   * Head of active inquiries.
   */
  struct Inquiry *w_head;

  /**
   * Tail of active inquiries.
   */
  struct Inquiry *w_tail;

  /**
   * Which exchange are we tracking here.
   */
  char *exchange_url;

  /**
   * The keys of this exchange
   */
  struct TALER_EXCHANGE_Keys *keys;

  /**
   * How many active inquiries do we have right now with this exchange.
   */
  unsigned int exchange_inquiries;

  /**
   * How long should we wait between requests
   * for transfer details?
   */
  struct GNUNET_TIME_Relative transfer_delay;

};


/**
 * Information about an inquiry job.
 */
struct Inquiry
{
  /**
   * Kept in a DLL.
   */
  struct Inquiry *next;

  /**
   * Kept in a DLL.
   */
  struct Inquiry *prev;

  /**
   * Handle to the exchange that made the transfer.
   */
  struct Exchange *exchange;

  /**
   * Task where we retry fetching transfer details from the exchange.
   */
  struct GNUNET_SCHEDULER_Task *task;

  /**
   * For which merchant instance is this tracking request?
   */
  char *instance_id;

  /**
   * payto:// URI used for the transfer.
   */
  char *payto_uri;

  /**
   * Handle for the /wire/transfers request.
   */
  struct TALER_EXCHANGE_TransfersGetHandle *wdh;

  /**
   * When did the transfer happen?
   */
  struct GNUNET_TIME_Timestamp execution_time;

  /**
   * Argument for the /wire/transfers request.
   */
  struct TALER_WireTransferIdentifierRawP wtid;

  /**
   * Amount of the wire transfer.
   */
  struct TALER_Amount total;

  /**
   * Row of the wire transfer in our database.
   */
  uint64_t rowid;

};


/**
 * Head of known exchanges.
 */
static struct Exchange *e_head;

/**
 * Tail of known exchanges.
 */
static struct Exchange *e_tail;

/**
 * The merchant's configuration.
 */
static const struct GNUNET_CONFIGURATION_Handle *cfg;

/**
 * Our database plugin.
 */
static struct TALER_MERCHANTDB_Plugin *db_plugin;

/**
 * Handle to the context for interacting with the bank.
 */
static struct GNUNET_CURL_Context *ctx;

/**
 * Scheduler context for running the @e ctx.
 */
static struct GNUNET_CURL_RescheduleContext *rc;

/**
 * Main task for #find_work().
 */
static struct GNUNET_SCHEDULER_Task *task;

/**
 * Event handler to learn that there are new transfers
 * to check.
 */
static struct GNUNET_DB_EventHandler *eh;

/**
 * Event handler to learn that there may be new exchange
 * keys to check.
 */
static struct GNUNET_DB_EventHandler *eh_keys;

/**
 * How many active inquiries do we have right now.
 */
static unsigned int active_inquiries;

/**
 * Set to true if we ever encountered any problem.
 */
static bool found_problem;

/**
 * Value to return from main(). 0 on success, non-zero on errors.
 */
static int global_ret;

/**
 * #GNUNET_YES if we are in test mode and should exit when idle.
 */
static int test_mode;

/**
 * True if the last DB query was limited by the
 * #OPEN_INQUIRY_LIMIT and we thus should check again
 * as soon as we are substantially below that limit,
 * and not only when we get a DB notification.
 */
static bool at_limit;


/**
 * Initiate download from exchange.
 *
 * @param cls a `struct Inquiry *`
 */
static void
exchange_request (void *cls);


/**
 * The exchange @a e is ready to handle more inquiries,
 * prepare to launch them.
 *
 * @param[in,out] e exchange to potentially launch inquiries on
 */
static void
launch_inquiries_at_exchange (struct Exchange *e)
{
  for (struct Inquiry *w = e->w_head;
       NULL != w;
       w = w->next)
  {
    if (e->exchange_inquiries > EXCHANGE_INQUIRY_LIMIT)
      break;
    if ( (NULL == w->task) &&
         (NULL == w->wdh) )
    {
      e->exchange_inquiries++;
      w->task = GNUNET_SCHEDULER_add_now (&exchange_request,
                                          w);
    }
  }
}


/**
 * Updates the transaction status for inquiry @a w to the given values.
 *
 * @param w inquiry to update status for
 * @param next_attempt when should we retry @a w (if ever)
 * @param ec error code to use (if any)
 * @param failed failure status (if ultimately failed)
 * @param verified success status (if ultimately successful)
 */
static void
update_transaction_status (const struct Inquiry *w,
                           struct GNUNET_TIME_Absolute next_attempt,
                           enum TALER_ErrorCode ec,
                           bool failed,
                           bool verified)
{
  enum GNUNET_DB_QueryStatus qs;

  if (failed)
    found_problem = true;
  qs = db_plugin->update_transfer_status (db_plugin->cls,
                                          w->exchange->exchange_url,
                                          &w->wtid,
                                          next_attempt,
                                          ec,
                                          failed,
                                          verified);
  if (qs < 0)
  {
    GNUNET_break (0);
    global_ret = EXIT_FAILURE;
    GNUNET_SCHEDULER_shutdown ();
    return;
  }
}


/**
 * Interact with the database to get the current set
 * of exchange keys known to us.
 *
 * @param e the exchange to check
 */
static void
sync_keys (struct Exchange *e)
{
  enum GNUNET_DB_QueryStatus qs;
  struct TALER_EXCHANGE_Keys *keys;

  qs = db_plugin->select_exchange_keys (db_plugin->cls,
                                        e->exchange_url,
                                        &keys);
  if (qs < 0)
  {
    GNUNET_break (0);
    return;
  }
  if (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS == qs)
  {
    GNUNET_break (0);
    return;
  }
  TALER_EXCHANGE_keys_decref (e->keys);
  e->keys = keys;
  launch_inquiries_at_exchange (e);
}


/**
 * Lookup our internal data structure for the given
 * @a exchange_url or create one if we do not yet have
 * one.
 *
 * @param exchange_url base URL of the exchange
 * @return our state for this exchange
 */
static struct Exchange *
find_exchange (const char *exchange_url)
{
  struct Exchange *e;

  for (e = e_head; NULL != e; e = e->next)
    if (0 == strcmp (exchange_url,
                     e->exchange_url))
      return e;
  e = GNUNET_new (struct Exchange);
  e->exchange_url = GNUNET_strdup (exchange_url);
  GNUNET_CONTAINER_DLL_insert (e_head,
                               e_tail,
                               e);
  sync_keys (e);
  return e;
}


/**
 * Finds new transfers that require work in the merchant database.
 *
 * @param cls NULL
 */
static void
find_work (void *cls);


/**
 * Free resources of @a w.
 *
 * @param[in] w inquiry job to terminate
 */
static void
end_inquiry (struct Inquiry *w)
{
  struct Exchange *e = w->exchange;

  GNUNET_assert (active_inquiries > 0);
  active_inquiries--;
  if (NULL != w->wdh)
  {
    TALER_EXCHANGE_transfers_get_cancel (w->wdh);
    w->wdh = NULL;
  }
  GNUNET_free (w->instance_id);
  GNUNET_free (w->payto_uri);
  GNUNET_CONTAINER_DLL_remove (e->w_head,
                               e->w_tail,
                               w);
  GNUNET_free (w);
  if ( (active_inquiries < OPEN_INQUIRY_LIMIT / 2) &&
       (NULL == task) &&
       (at_limit) )
  {
    at_limit = false;
    GNUNET_assert (NULL == task);
    task = GNUNET_SCHEDULER_add_now (&find_work,
                                     NULL);
  }
  if ( (NULL == task) &&
       (! at_limit) &&
       (0 == active_inquiries) &&
       (test_mode) )
  {
    GNUNET_log (GNUNET_ERROR_TYPE_INFO,
                "No more open inquiries and in test mode. Existing.\n");
    GNUNET_SCHEDULER_shutdown ();
    return;
  }
}


/**
 * We're being aborted with CTRL-C (or SIGTERM). Shut down.
 *
 * @param cls closure (NULL)
 */
static void
shutdown_task (void *cls)
{
  (void) cls;
  GNUNET_log (GNUNET_ERROR_TYPE_INFO,
              "Running shutdown\n");
  while (NULL != e_head)
  {
    struct Exchange *e = e_head;

    while (NULL != e->w_head)
    {
      struct Inquiry *w = e->w_head;

      end_inquiry (w);
    }
    GNUNET_free (e->exchange_url);
    if (NULL != e->keys)
    {
      TALER_EXCHANGE_keys_decref (e->keys);
      e->keys = NULL;
    }
    GNUNET_CONTAINER_DLL_remove (e_head,
                                 e_tail,
                                 e);
    GNUNET_free (e);
  }
  if (NULL != eh)
  {
    db_plugin->event_listen_cancel (eh);
    eh = NULL;
  }
  if (NULL != eh_keys)
  {
    db_plugin->event_listen_cancel (eh_keys);
    eh_keys = NULL;
  }
  if (NULL != task)
  {
    GNUNET_SCHEDULER_cancel (task);
    task = NULL;
  }
  TALER_MERCHANTDB_plugin_unload (db_plugin);
  db_plugin = NULL;
  cfg = NULL;
  if (NULL != ctx)
  {
    GNUNET_CURL_fini (ctx);
    ctx = NULL;
  }
  if (NULL != rc)
  {
    GNUNET_CURL_gnunet_rc_destroy (rc);
    rc = NULL;
  }
}


/**
 * Check that the given @a wire_fee is what the @a e should charge
 * at the @a execution_time.  If the fee is correct (according to our
 * database), return #GNUNET_OK.  If we do not have the fee structure in our
 * DB, we just accept it and return #GNUNET_NO; if we have proof that the fee
 * is bogus, we respond with the proof to the client and return
 * #GNUNET_SYSERR.
 *
 * @param w inquiry to check fees of
 * @param execution_time time of the wire transfer
 * @param wire_fee fee claimed by the exchange
 * @return #GNUNET_SYSERR if we returned hard proof of
 *   missbehavior from the exchange to the client
 */
static enum GNUNET_GenericReturnValue
check_wire_fee (struct Inquiry *w,
                struct GNUNET_TIME_Timestamp execution_time,
                const struct TALER_Amount *wire_fee)
{
  struct Exchange *e = w->exchange;
  const struct TALER_EXCHANGE_Keys *keys = e->keys;
  struct TALER_WireFeeSet fees;
  struct TALER_MasterSignatureP master_sig;
  struct GNUNET_TIME_Timestamp start_date;
  struct GNUNET_TIME_Timestamp end_date;
  enum GNUNET_DB_QueryStatus qs;
  char *wire_method;

  if (NULL == keys)
  {
    GNUNET_break (0);
    return GNUNET_NO;
  }
  wire_method = TALER_payto_get_method (w->payto_uri);
  qs = db_plugin->lookup_wire_fee (db_plugin->cls,
                                   &keys->master_pub,
                                   wire_method,
                                   execution_time,
                                   &fees,
                                   &start_date,
                                   &end_date,
                                   &master_sig);
  switch (qs)
  {
  case GNUNET_DB_STATUS_HARD_ERROR:
    GNUNET_break (0);
    GNUNET_free (wire_method);
    return GNUNET_SYSERR;
  case GNUNET_DB_STATUS_SOFT_ERROR:
    GNUNET_free (wire_method);
    return GNUNET_NO;
  case GNUNET_DB_STATUS_SUCCESS_NO_RESULTS:
    GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
                "Failed to find wire fee for `%s' and method `%s' at %s in DB, accepting blindly that the fee is %s\n",
                TALER_B2S (&keys->master_pub),
                wire_method,
                GNUNET_TIME_timestamp2s (execution_time),
                TALER_amount2s (wire_fee));
    GNUNET_free (wire_method);
    return GNUNET_OK;
  case GNUNET_DB_STATUS_SUCCESS_ONE_RESULT:
    break;
  }
  if ( (GNUNET_OK !=
        TALER_amount_cmp_currency (&fees.wire,
                                   wire_fee)) ||
       (0 > TALER_amount_cmp (&fees.wire,
                              wire_fee)) )
  {
    GNUNET_break_op (0);
    GNUNET_free (wire_method);
    return GNUNET_SYSERR;   /* expected_fee >= wire_fee */
  }
  GNUNET_free (wire_method);
  return GNUNET_OK;
}


/**
 * Closure for #check_transfer()
 */
struct CheckTransferContext
{

  /**
   * Pointer to the detail that we are currently
   * checking in #check_transfer().
   */
  const struct TALER_TrackTransferDetails *current_detail;

  /**
   * Which transaction detail are we currently looking at?
   */
  unsigned int current_offset;

  /**
   * #GNUNET_NO if we did not find a matching coin.
   * #GNUNET_SYSERR if we found a matching coin, but the amounts do not match.
   * #GNUNET_OK if we did find a matching coin.
   */
  enum GNUNET_GenericReturnValue check_transfer_result;

  /**
   * Set to error code, if any.
   */
  enum TALER_ErrorCode ec;

  /**
   * Set to true if @e ec indicates a permanent failure.
   */
  bool failure;
};


/**
 * This function checks that the information about the coin which
 * was paid back by _this_ wire transfer matches what _we_ (the merchant)
 * knew about this coin.
 *
 * @param cls closure with our `struct CheckTransferContext  *`
 * @param exchange_url URL of the exchange that issued @a coin_pub
 * @param amount_with_fee amount the exchange will transfer for this coin
 * @param deposit_fee fee the exchange will charge for this coin
 * @param refund_fee fee the exchange will charge for refunding this coin
 * @param wire_fee paid wire fee
 * @param h_wire hash of merchant's wire details
 * @param deposit_timestamp when did the exchange receive the deposit
 * @param refund_deadline until when are refunds allowed
 * @param exchange_sig signature by the exchange
 * @param exchange_pub exchange signing key used for @a exchange_sig
 */
static void
check_transfer (void *cls,
                const char *exchange_url,
                const struct TALER_Amount *amount_with_fee,
                const struct TALER_Amount *deposit_fee,
                const struct TALER_Amount *refund_fee,
                const struct TALER_Amount *wire_fee,
                const struct TALER_MerchantWireHashP *h_wire,
                struct GNUNET_TIME_Timestamp deposit_timestamp,
                struct GNUNET_TIME_Timestamp refund_deadline,
                const struct TALER_ExchangeSignatureP *exchange_sig,
                const struct TALER_ExchangePublicKeyP *exchange_pub)
{
  struct CheckTransferContext *ctc = cls;
  const struct TALER_TrackTransferDetails *ttd = ctc->current_detail;

  if (GNUNET_SYSERR == ctc->check_transfer_result)
  {
    GNUNET_break (0);
    return;   /* already had a serious issue; odd that we're called more than once as well... */
  }
  GNUNET_log (GNUNET_ERROR_TYPE_INFO,
              "Checking coin with value %s\n",
              TALER_amount2s (amount_with_fee));
  if ( (GNUNET_OK !=
        TALER_amount_cmp_currency (amount_with_fee,
                                   &ttd->coin_value)) ||
       (0 != TALER_amount_cmp (amount_with_fee,
                               &ttd->coin_value)) )
  {
    /* Disagreement between the exchange and us about how much this
       coin is worth! */
    GNUNET_break_op (0);
    GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
                "Disagreement about coin value %s\n",
                TALER_amount2s (amount_with_fee));
    GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
                "Exchange gave it a value of %s\n",
                TALER_amount2s (&ttd->coin_value));
    ctc->check_transfer_result = GNUNET_SYSERR;
    /* Build the `TrackTransferConflictDetails` */
    ctc->ec = TALER_EC_MERCHANT_PRIVATE_POST_TRANSFERS_CONFLICTING_REPORTS;
    ctc->failure = true;
    /* FIXME: this should be reported to the auditor (once the auditor has an API for this) */
    return;
  }
  if ( (GNUNET_OK !=
        TALER_amount_cmp_currency (deposit_fee,
                                   &ttd->coin_fee)) ||
       (0 != TALER_amount_cmp (deposit_fee,
                               &ttd->coin_fee)) )
  {
    /* Disagreement between the exchange and us about how much this
       coin is worth! */
    GNUNET_break_op (0);
    GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
                "Expected fee is %s\n",
                TALER_amount2s (&ttd->coin_fee));
    GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
                "Fee claimed by exchange is %s\n",
                TALER_amount2s (deposit_fee));
    ctc->check_transfer_result = GNUNET_SYSERR;
    /* Build the `TrackTransferConflictDetails` */
    ctc->ec = TALER_EC_MERCHANT_PRIVATE_POST_TRANSFERS_CONFLICTING_REPORTS;
    ctc->failure = true;
    /* FIXME: this should be reported to the auditor (once the auditor has an API for this) */
    return;
  }
  ctc->check_transfer_result = GNUNET_OK;
}


/**
 * Function called with detailed wire transfer data, including all
 * of the coin transactions that were combined into the wire transfer.
 *
 * @param cls closure a `struct Inquiry *`
 * @param tgr response details
 */
static void
wire_transfer_cb (void *cls,
                  const struct TALER_EXCHANGE_TransfersGetResponse *tgr)
{
  struct Inquiry *w = cls;
  struct Exchange *e = w->exchange;
  const struct TALER_EXCHANGE_TransferData *td = NULL;

  e->exchange_inquiries--;
  w->wdh = NULL;
  if (EXCHANGE_INQUIRY_LIMIT - 1 == e->exchange_inquiries)
    launch_inquiries_at_exchange (e);
  GNUNET_log (GNUNET_ERROR_TYPE_INFO,
              "Got response code %u from exchange for GET /transfers/$WTID\n",
              tgr->hr.http_status);
  switch (tgr->hr.http_status)
  {
  case MHD_HTTP_OK:
    td = &tgr->details.ok.td;
    w->execution_time = td->execution_time;
    e->transfer_delay = GNUNET_TIME_UNIT_ZERO;
    break;
  case MHD_HTTP_BAD_REQUEST:
  case MHD_HTTP_FORBIDDEN:
    update_transaction_status (w,
                               GNUNET_TIME_UNIT_FOREVER_ABS,
                               TALER_EC_MERCHANT_EXCHANGE_TRANSFERS_HARD_FAILURE,
                               true,
                               false);
    end_inquiry (w);
    return;
  case MHD_HTTP_NOT_FOUND:
    update_transaction_status (w,
                               GNUNET_TIME_UNIT_FOREVER_ABS,
                               TALER_EC_MERCHANT_EXCHANGE_TRANSFERS_FATAL_NOT_FOUND,
                               true,
                               false);
    end_inquiry (w);
    return;
  case MHD_HTTP_INTERNAL_SERVER_ERROR:
  case MHD_HTTP_BAD_GATEWAY:
  case MHD_HTTP_GATEWAY_TIMEOUT:
    e->transfer_delay = GNUNET_TIME_STD_BACKOFF (e->transfer_delay);
    update_transaction_status (w,
                               GNUNET_TIME_relative_to_absolute (
                                 e->transfer_delay),
                               TALER_EC_MERCHANT_EXCHANGE_TRANSFERS_TRANSIENT_FAILURE,
                               false,
                               false);
    end_inquiry (w);
    return;
  default:
    e->transfer_delay = GNUNET_TIME_STD_BACKOFF (e->transfer_delay);
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                "Unexpected HTTP status %u\n",
                tgr->hr.http_status);
    update_transaction_status (w,
                               GNUNET_TIME_relative_to_absolute (
                                 e->transfer_delay),
                               TALER_EC_MERCHANT_EXCHANGE_TRANSFERS_TRANSIENT_FAILURE,
                               false,
                               false);
    end_inquiry (w);
    return;
  }
  db_plugin->preflight (db_plugin->cls);

  {
    enum GNUNET_DB_QueryStatus qs;

    qs = db_plugin->insert_transfer_details (db_plugin->cls,
                                             w->instance_id,
                                             w->exchange->exchange_url,
                                             w->payto_uri,
                                             &w->wtid,
                                             td);
    if (0 > qs)
    {
      /* Always report on DB error as well to enable diagnostics */
      GNUNET_break (GNUNET_DB_STATUS_HARD_ERROR == qs);
      global_ret = EXIT_FAILURE;
      GNUNET_SCHEDULER_shutdown ();
      return;
    }
    if (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS == qs)
    {
      GNUNET_log (GNUNET_ERROR_TYPE_INFO,
                  "Transfer already known. Ignoring duplicate.\n");
      return;
    }
  }

  {
    struct CheckTransferContext ctc = {
      .ec = TALER_EC_NONE,
      .failure = false
    };

    for (unsigned int i = 0; i<td->details_length; i++)
    {
      const struct TALER_TrackTransferDetails *ttd = &td->details[i];
      enum GNUNET_DB_QueryStatus qs;

      if (TALER_EC_NONE != ctc.ec)
        break; /* already encountered an error */
      ctc.current_offset = i;
      ctc.current_detail = ttd;
      /* Set the coin as "never seen" before. */
      ctc.check_transfer_result = GNUNET_NO;
      qs = db_plugin->lookup_deposits_by_contract_and_coin (
        db_plugin->cls,
        w->instance_id,
        &ttd->h_contract_terms,
        &ttd->coin_pub,
        &check_transfer,
        &ctc);
      switch (qs)
      {
      case GNUNET_DB_STATUS_SOFT_ERROR:
        GNUNET_break (0);
        ctc.ec = TALER_EC_GENERIC_DB_FETCH_FAILED;
        break;
      case GNUNET_DB_STATUS_HARD_ERROR:
        GNUNET_break (0);
        ctc.ec = TALER_EC_GENERIC_DB_FETCH_FAILED;
        break;
      case GNUNET_DB_STATUS_SUCCESS_NO_RESULTS:
        /* The exchange says we made this deposit, but WE do not
           recall making it (corrupted / unreliable database?)!
           Well, let's say thanks and accept the money! */
        GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
                    "Failed to find payment data in DB\n");
        ctc.check_transfer_result = GNUNET_OK;
        break;
      case GNUNET_DB_STATUS_SUCCESS_ONE_RESULT:
        break;
      }
      switch (ctc.check_transfer_result)
      {
      case GNUNET_NO:
        /* Internal error: how can we have called #check_transfer()
           but still have no result? */
        GNUNET_break (0);
        ctc.ec = TALER_EC_GENERIC_INTERNAL_INVARIANT_FAILURE;
        return;
      case GNUNET_SYSERR:
        /* #check_transfer() failed, report conflict! */
        GNUNET_break_op (0);
        GNUNET_assert (TALER_EC_NONE != ctc.ec);
        break;
      case GNUNET_OK:
        break;
      }
    }
    if (TALER_EC_NONE != ctc.ec)
    {
      update_transaction_status (
        w,
        ctc.failure
        ? GNUNET_TIME_UNIT_FOREVER_ABS
        : GNUNET_TIME_relative_to_absolute (
          GNUNET_TIME_UNIT_MINUTES),
        ctc.ec,
        ctc.failure,
        false);
      end_inquiry (w);
      return;
    }
  }

  if (GNUNET_SYSERR ==
      check_wire_fee (w,
                      td->execution_time,
                      &td->wire_fee))
  {
    GNUNET_break_op (0);
    update_transaction_status (w,
                               GNUNET_TIME_UNIT_FOREVER_ABS,
                               TALER_EC_MERCHANT_PRIVATE_POST_TRANSFERS_BAD_WIRE_FEE,
                               true,
                               false);
    end_inquiry (w);
    return;
  }

  if ( (GNUNET_OK !=
        TALER_amount_cmp_currency (&td->total_amount,
                                   &w->total)) ||
       (0 !=
        TALER_amount_cmp (&td->total_amount,
                          &w->total)) )
  {
    GNUNET_break_op (0);
    GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
                "Wire transfer total value was %s\n",
                TALER_amount2s (&w->total));
    GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
                "Exchange claimed total value to be %s\n",
                TALER_amount2s (&td->total_amount));
    update_transaction_status (w,
                               GNUNET_TIME_UNIT_FOREVER_ABS,
                               TALER_EC_MERCHANT_EXCHANGE_TRANSFERS_CONFLICTING_TRANSFERS,
                               true,
                               false);
    end_inquiry (w);
    return;
  }
  /* set transaction to successful */
  update_transaction_status (w,
                             GNUNET_TIME_UNIT_FOREVER_ABS,
                             TALER_EC_NONE,
                             false,
                             true);
  end_inquiry (w);
}


/**
 * Initiate download from an exchange for a given inquiry.
 *
 * @param cls a `struct Inquiry *`
 */
static void
exchange_request (void *cls)
{
  struct Inquiry *w = cls;
  struct Exchange *e = w->exchange;

  w->task = NULL;
  if (NULL == e->keys)
    return;
  w->wdh = TALER_EXCHANGE_transfers_get (
    ctx,
    e->exchange_url,
    e->keys,
    &w->wtid,
    &wire_transfer_cb,
    w);
  if (NULL == w->wdh)
  {
    GNUNET_break (0);
    e->exchange_inquiries--;
    e->transfer_delay = GNUNET_TIME_STD_BACKOFF (e->transfer_delay);
    update_transaction_status (w,
                               GNUNET_TIME_relative_to_absolute (
                                 e->transfer_delay),
                               TALER_EC_MERCHANT_EXCHANGE_TRANSFERS_TRANSIENT_FAILURE,
                               false,
                               false);
    end_inquiry (w);
    return;
  }
  /* Wait at least 1m for the network transfer */
  update_transaction_status (w,
                             GNUNET_TIME_relative_to_absolute (
                               GNUNET_TIME_UNIT_MINUTES),
                             TALER_EC_MERCHANT_EXCHANGE_TRANSFERS_AWAITING_LIST,
                             false,
                             false);
}


/**
 * Function called with information about a transfer we
 * should ask the exchange about.
 *
 * @param cls closure (NULL)
 * @param rowid row of the transfer in the merchant database
 * @param instance_id instance that received the transfer
 * @param exchange_url base URL of the exchange that initiated the transfer
 * @param payto_uri account of the merchant that received the transfer
 * @param wtid wire transfer subject identifying the aggregation
 * @param total total amount that was wired
 * @param next_attempt when should we next try to interact with the exchange
 */
static void
start_inquiry (
  void *cls,
  uint64_t rowid,
  const char *instance_id,
  const char *exchange_url,
  const char *payto_uri,
  const struct TALER_WireTransferIdentifierRawP *wtid,
  const struct TALER_Amount *total,
  struct GNUNET_TIME_Absolute next_attempt)
{
  struct Exchange *e;
  struct Inquiry *w;

  (void) cls;
  if (GNUNET_TIME_absolute_is_future (next_attempt))
  {
    if (NULL == task)
      task = GNUNET_SCHEDULER_add_at (next_attempt,
                                      &find_work,
                                      NULL);
    return;
  }
  e = find_exchange (exchange_url);
  for (w = e->w_head; NULL != w; w = w->next)
  {
    if (0 == GNUNET_memcmp (&w->wtid,
                            wtid))
    {
      GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
                  "Already processing inquiry. Aborting ongoing inquiry\n");
      end_inquiry (w);
      break;
    }
  }

  active_inquiries++;
  w = GNUNET_new (struct Inquiry);
  w->payto_uri = GNUNET_strdup (payto_uri);
  w->instance_id = GNUNET_strdup (instance_id);
  w->rowid = rowid;
  w->wtid = *wtid;
  w->total = *total;
  GNUNET_CONTAINER_DLL_insert (e->w_head,
                               e->w_tail,
                               w);
  w->exchange = e;
  if (NULL != w->exchange->keys)
    w->task = GNUNET_SCHEDULER_add_now (&exchange_request,
                                        w);
  /* Wait at least 1 minute for /keys */
  update_transaction_status (w,
                             GNUNET_TIME_relative_to_absolute (
                               GNUNET_TIME_UNIT_MINUTES),
                             TALER_EC_MERCHANT_EXCHANGE_TRANSFERS_AWAITING_KEYS,
                             false,
                             false);
}


static void
find_work (void *cls)
{
  enum GNUNET_DB_QueryStatus qs;
  int limit;

  (void) cls;
  task = NULL;
  GNUNET_assert (OPEN_INQUIRY_LIMIT >= active_inquiries);
  limit = OPEN_INQUIRY_LIMIT - active_inquiries;
  if (0 == limit)
  {
    GNUNET_log (GNUNET_ERROR_TYPE_INFO,
                "Not looking for work: at limit\n");
    at_limit = true;
    return;
  }
  at_limit = false;
  qs = db_plugin->select_open_transfers (db_plugin->cls,
                                         limit,
                                         &start_inquiry,
                                         NULL);
  if (qs < 0)
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                "Failed to obtain open transfers from database\n");
    GNUNET_SCHEDULER_shutdown ();
    return;
  }
  if (qs == limit)
  {
    /* DB limited response, re-trigger DB interaction
       the moment we significantly fall below the
       limit */
    at_limit = true;
  }
  if (0 == active_inquiries)
  {
    if (test_mode)
    {
      GNUNET_log (GNUNET_ERROR_TYPE_INFO,
                  "No more open inquiries and in test mode. Existing.\n");
      GNUNET_SCHEDULER_shutdown ();
      return;
    }
    GNUNET_log (
      GNUNET_ERROR_TYPE_INFO,
      "No open inquiries found, waiting for notification to resume\n");
  }
}


/**
 * Function called when transfers are added to the merchant database.  We look
 * for more work.
 *
 * @param cls closure (NULL)
 * @param extra additional event data provided
 * @param extra_size number of bytes in @a extra
 */
static void
transfer_added (void *cls,
                const void *extra,
                size_t extra_size)
{
  (void) cls;
  (void) extra;
  (void) extra_size;
  if (active_inquiries > OPEN_INQUIRY_LIMIT / 2)
  {
    /* Trigger DB only once we are substantially below the limit */
    at_limit = true;
    return;
  }
  if (NULL != task)
    return;
  task = GNUNET_SCHEDULER_add_now (&find_work,
                                   NULL);
}


/**
 * Function called when keys were changed in the
 * merchant database. Updates ours.
 *
 * @param cls closure (NULL)
 * @param extra additional event data provided
 * @param extra_size number of bytes in @a extra
 */
static void
keys_changed (void *cls,
              const void *extra,
              size_t extra_size)
{
  const char *url = extra;
  struct Exchange *e;

  (void) cls;
  if ( (NULL == extra) ||
       (0 == extra_size) )
  {
    GNUNET_break (0);
    return;
  }
  if ('\0' != url[extra_size - 1])
  {
    GNUNET_break (0);
    return;
  }
  GNUNET_log (GNUNET_ERROR_TYPE_INFO,
              "Received keys change notification: reload `%s'\n",
              url);
  e = find_exchange (url);
  sync_keys (e);
}


/**
 * First task.
 *
 * @param cls closure, NULL
 * @param args remaining command-line arguments
 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
 * @param c configuration
 */
static void
run (void *cls,
     char *const *args,
     const char *cfgfile,
     const struct GNUNET_CONFIGURATION_Handle *c)
{
  (void) args;
  (void) cfgfile;

  cfg = c;
  GNUNET_SCHEDULER_add_shutdown (&shutdown_task,
                                 NULL);
  ctx = GNUNET_CURL_init (&GNUNET_CURL_gnunet_scheduler_reschedule,
                          &rc);
  rc = GNUNET_CURL_gnunet_rc_create (ctx);
  if (NULL == ctx)
  {
    GNUNET_break (0);
    GNUNET_SCHEDULER_shutdown ();
    global_ret = EXIT_FAILURE;
    return;
  }
  if (NULL ==
      (db_plugin = TALER_MERCHANTDB_plugin_load (cfg)))
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                "Failed to initialize DB subsystem\n");
    GNUNET_SCHEDULER_shutdown ();
    global_ret = EXIT_NOTCONFIGURED;
    return;
  }
  if (GNUNET_OK !=
      db_plugin->connect (db_plugin->cls))
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                "Failed to connect to database\n");
    GNUNET_SCHEDULER_shutdown ();
    global_ret = EXIT_FAILURE;
    return;
  }
  {
    struct GNUNET_DB_EventHeaderP es = {
      .size = htons (sizeof (es)),
      .type = htons (TALER_DBEVENT_MERCHANT_WIRE_TRANSFER_CONFIRMED)
    };

    eh = db_plugin->event_listen (db_plugin->cls,
                                  &es,
                                  GNUNET_TIME_UNIT_FOREVER_REL,
                                  &transfer_added,
                                  NULL);
  }
  {
    struct GNUNET_DB_EventHeaderP es = {
      .size = htons (sizeof (es)),
      .type = htons (TALER_DBEVENT_MERCHANT_EXCHANGE_KEYS)
    };

    eh_keys
      = db_plugin->event_listen (db_plugin->cls,
                                 &es,
                                 GNUNET_TIME_UNIT_FOREVER_REL,
                                 &keys_changed,
                                 NULL);
  }

  GNUNET_assert (NULL == task);
  task = GNUNET_SCHEDULER_add_now (&find_work,
                                   NULL);
}


/**
 * The main function of taler-merchant-reconciliation
 *
 * @param argc number of arguments from the command line
 * @param argv command line arguments
 * @return 0 ok, 1 on error
 */
int
main (int argc,
      char *const *argv)
{
  struct GNUNET_GETOPT_CommandLineOption options[] = {
    GNUNET_GETOPT_option_timetravel ('T',
                                     "timetravel"),
    GNUNET_GETOPT_option_flag ('t',
                               "test",
                               "run in test mode and exit when idle",
                               &test_mode),
    GNUNET_GETOPT_option_version (VERSION "-" VCS_VERSION),
    GNUNET_GETOPT_OPTION_END
  };
  enum GNUNET_GenericReturnValue ret;

  TALER_OS_init ();
  ret = GNUNET_PROGRAM_run (
    argc, argv,
    "taler-merchant-reconciliation",
    gettext_noop (
      "background process that reconciles bank transfers with orders by asking the exchange"),
    options,
    &run, NULL);
  if (GNUNET_SYSERR == ret)
    return EXIT_INVALIDARGUMENT;
  if (GNUNET_NO == ret)
    return EXIT_SUCCESS;
  if ( (found_problem) &&
       (0 == global_ret) )
    global_ret = 7;
  return global_ret;
}


/* end of taler-merchant-reconciliation.c */