aboutsummaryrefslogtreecommitdiff
path: root/src/auditor/taler-helper-auditor-aggregation.c
blob: cdcbd37d10d028ac418a4b077ec64d05541ed138 (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
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
/*
  This file is part of TALER
  Copyright (C) 2016-2020 Taler Systems SA

  TALER is free software; you can redistribute it and/or modify it under the
  terms of the GNU Affero 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 Public License for more details.

  You should have received a copy of the GNU Affero Public License along with
  TALER; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
*/
/**
 * @file auditor/taler-helper-auditor-aggregation.c
 * @brief audits an exchange's aggregations.
 * @author Christian Grothoff
 */
#include "platform.h"
#include <gnunet/gnunet_util_lib.h>
#include "taler_auditordb_plugin.h"
#include "taler_exchangedb_lib.h"
#include "taler_json_lib.h"
#include "taler_bank_service.h"
#include "taler_signatures.h"
#include "report-lib.h"


/**
 * Return value from main().
 */
static int global_ret;

/**
 * Checkpointing our progress for aggregations.
 */
static struct TALER_AUDITORDB_ProgressPointAggregation ppa;

/**
 * Checkpointing our progress for aggregations.
 */
static struct TALER_AUDITORDB_ProgressPointAggregation ppa_start;

/**
 * Array of reports about row inconsitencies.
 */
static json_t *report_row_inconsistencies;

/**
 * Array of reports about irregular wire out entries.
 */
static json_t *report_wire_out_inconsistencies;

/**
 * Total delta between calculated and stored wire out transfers,
 * for positive deltas.
 */
static struct TALER_Amount total_wire_out_delta_plus;

/**
 * Total delta between calculated and stored wire out transfers
 * for negative deltas.
 */
static struct TALER_Amount total_wire_out_delta_minus;

/**
 * Array of reports about inconsistencies about coins.
 */
static json_t *report_coin_inconsistencies;

/**
 * Profits the exchange made by bad amount calculations on coins.
 */
static struct TALER_Amount total_coin_delta_plus;

/**
 * Losses the exchange made by bad amount calculations on coins.
 */
static struct TALER_Amount total_coin_delta_minus;

/**
 * Report about amount calculation differences (causing profit
 * or loss at the exchange).
 */
static json_t *report_amount_arithmetic_inconsistencies;

/**
 * Array of reports about wire fees being ambiguous in terms of validity periods.
 */
static json_t *report_fee_time_inconsistencies;

/**
 * Profits the exchange made by bad amount calculations.
 */
static struct TALER_Amount total_arithmetic_delta_plus;

/**
 * Losses the exchange made by bad amount calculations.
 */
static struct TALER_Amount total_arithmetic_delta_minus;

/**
 * Total aggregation fees earned.
 */
static struct TALER_Amount total_aggregation_fee_income;

/**
 * Array of reports about coin operations with bad signatures.
 */
static json_t *report_bad_sig_losses;

/**
 * Total amount lost by operations for which signatures were invalid.
 */
static struct TALER_Amount total_bad_sig_loss;

/**
 * Should we run checks that only work for exchange-internal audits?
 */
static int internal_checks;


/**
 * Report a (serious) inconsistency in the exchange's database with
 * respect to calculations involving amounts.
 *
 * @param operation what operation had the inconsistency
 * @param rowid affected row, UINT64_MAX if row is missing
 * @param exchange amount calculated by exchange
 * @param auditor amount calculated by auditor
 * @param profitable 1 if @a exchange being larger than @a auditor is
 *           profitable for the exchange for this operation,
 *           -1 if @a exchange being smaller than @a auditor is
 *           profitable for the exchange, and 0 if it is unclear
 */
static void
report_amount_arithmetic_inconsistency (
  const char *operation,
  uint64_t rowid,
  const struct TALER_Amount *exchange,
  const struct TALER_Amount *auditor,
  int profitable)
{
  struct TALER_Amount delta;
  struct TALER_Amount *target;

  if (0 < TALER_amount_cmp (exchange,
                            auditor))
  {
    /* exchange > auditor */
    TALER_ARL_amount_subtract (&delta,
                               exchange,
                               auditor);
  }
  else
  {
    /* auditor < exchange */
    profitable = -profitable;
    TALER_ARL_amount_subtract (&delta,
                               auditor,
                               exchange);
  }
  TALER_ARL_report (report_amount_arithmetic_inconsistencies,
                    json_pack ("{s:s, s:I, s:o, s:o, s:I}",
                               "operation", operation,
                               "rowid", (json_int_t) rowid,
                               "exchange", TALER_JSON_from_amount (exchange),
                               "auditor", TALER_JSON_from_amount (auditor),
                               "profitable", (json_int_t) profitable));
  if (0 != profitable)
  {
    target = (1 == profitable)
             ? &total_arithmetic_delta_plus
             : &total_arithmetic_delta_minus;
    TALER_ARL_amount_add (target,
                          target,
                          &delta);
  }
}


/**
 * Report a (serious) inconsistency in the exchange's database with
 * respect to calculations involving amounts of a coin.
 *
 * @param operation what operation had the inconsistency
 * @param coin_pub affected coin
 * @param exchange amount calculated by exchange
 * @param auditor amount calculated by auditor
 * @param profitable 1 if @a exchange being larger than @a auditor is
 *           profitable for the exchange for this operation,
 *           -1 if @a exchange being smaller than @a auditor is
 *           profitable for the exchange, and 0 if it is unclear
 */
static void
report_coin_arithmetic_inconsistency (
  const char *operation,
  const struct TALER_CoinSpendPublicKeyP *coin_pub,
  const struct TALER_Amount *exchange,
  const struct TALER_Amount *auditor,
  int profitable)
{
  struct TALER_Amount delta;
  struct TALER_Amount *target;

  if (0 < TALER_amount_cmp (exchange,
                            auditor))
  {
    /* exchange > auditor */
    TALER_ARL_amount_subtract (&delta,
                               exchange,
                               auditor);
  }
  else
  {
    /* auditor < exchange */
    profitable = -profitable;
    TALER_ARL_amount_subtract (&delta,
                               auditor,
                               exchange);
  }
  TALER_ARL_report (report_coin_inconsistencies,
                    json_pack ("{s:s, s:o, s:o, s:o, s:I}",
                               "operation", operation,
                               "coin_pub", GNUNET_JSON_from_data_auto (
                                 coin_pub),
                               "exchange", TALER_JSON_from_amount (exchange),
                               "auditor", TALER_JSON_from_amount (auditor),
                               "profitable", (json_int_t) profitable));
  if (0 != profitable)
  {
    target = (1 == profitable)
             ? &total_coin_delta_plus
             : &total_coin_delta_minus;
    TALER_ARL_amount_add (target,
                          target,
                          &delta);
  }
}


/**
 * Report a (serious) inconsistency in the exchange's database.
 *
 * @param table affected table
 * @param rowid affected row, UINT64_MAX if row is missing
 * @param diagnostic message explaining the problem
 */
static void
report_row_inconsistency (const char *table,
                          uint64_t rowid,
                          const char *diagnostic)
{
  TALER_ARL_report (report_row_inconsistencies,
                    json_pack ("{s:s, s:I, s:s}",
                               "table", table,
                               "row", (json_int_t) rowid,
                               "diagnostic", diagnostic));
}


/* *********************** Analyze aggregations ******************** */
/* This logic checks that the aggregator did the right thing
   paying each merchant what they were due (and on time). */


/**
 * Information about wire fees charged by the exchange.
 */
struct WireFeeInfo
{

  /**
   * Kept in a DLL.
   */
  struct WireFeeInfo *next;

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

  /**
   * When does the fee go into effect (inclusive).
   */
  struct GNUNET_TIME_Absolute start_date;

  /**
   * When does the fee stop being in effect (exclusive).
   */
  struct GNUNET_TIME_Absolute end_date;

  /**
   * How high is the wire fee.
   */
  struct TALER_Amount wire_fee;

  /**
   * How high is the closing fee.
   */
  struct TALER_Amount closing_fee;

};


/**
 * Closure for callbacks during #analyze_merchants().
 */
struct AggregationContext
{

  /**
   * DLL of wire fees charged by the exchange.
   */
  struct WireFeeInfo *fee_head;

  /**
   * DLL of wire fees charged by the exchange.
   */
  struct WireFeeInfo *fee_tail;

  /**
   * Final result status.
   */
  enum GNUNET_DB_QueryStatus qs;
};


/**
 * Closure for #wire_transfer_information_cb.
 */
struct WireCheckContext
{

  /**
   * Corresponding merchant context.
   */
  struct AggregationContext *ac;

  /**
   * Total deposits claimed by all transactions that were aggregated
   * under the given @e wtid.
   */
  struct TALER_Amount total_deposits;

  /**
   * Hash of the wire transfer details of the receiver.
   */
  struct GNUNET_HashCode h_wire;

  /**
   * Execution time of the wire transfer.
   */
  struct GNUNET_TIME_Absolute date;

  /**
   * Database transaction status.
   */
  enum GNUNET_DB_QueryStatus qs;

};


/**
 * Check coin's transaction history for plausibility.  Does NOT check
 * the signatures (those are checked independently), but does calculate
 * the amounts for the aggregation table and checks that the total
 * claimed coin value is within the value of the coin's denomination.
 *
 * @param coin_pub public key of the coin (for reporting)
 * @param h_contract_terms hash of the proposal for which we calculate the amount
 * @param merchant_pub public key of the merchant (who is allowed to issue refunds)
 * @param issue denomination information about the coin
 * @param tl_head head of transaction history to verify
 * @param[out] merchant_gain amount the coin contributes to the wire transfer to the merchant
 * @param[out] deposit_gain amount the coin contributes excluding refunds
 * @return #GNUNET_OK on success, #GNUNET_SYSERR if the transaction must fail (hard error)
 */
static int
check_transaction_history_for_deposit (
  const struct TALER_CoinSpendPublicKeyP *coin_pub,
  const struct GNUNET_HashCode *h_contract_terms,
  const struct TALER_MerchantPublicKeyP *merchant_pub,
  const struct TALER_DenominationKeyValidityPS *issue,
  const struct TALER_EXCHANGEDB_TransactionList *tl_head,
  struct TALER_Amount *merchant_gain,
  struct TALER_Amount *deposit_gain)
{
  struct TALER_Amount expenditures;
  struct TALER_Amount refunds;
  struct TALER_Amount spent;
  struct TALER_Amount merchant_loss;
  const struct TALER_Amount *deposit_fee;
  int refund_deposit_fee;

  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              "Checking transaction history of coin %s\n",
              TALER_B2S (coin_pub));
  GNUNET_assert (GNUNET_OK ==
                 TALER_amount_set_zero (TALER_ARL_currency,
                                        &expenditures));
  GNUNET_assert (GNUNET_OK ==
                 TALER_amount_set_zero (TALER_ARL_currency,
                                        &refunds));
  GNUNET_assert (GNUNET_OK ==
                 TALER_amount_set_zero (TALER_ARL_currency,
                                        merchant_gain));
  GNUNET_assert (GNUNET_OK ==
                 TALER_amount_set_zero (TALER_ARL_currency,
                                        &merchant_loss));
  /* Go over transaction history to compute totals; note that we do not bother
     to reconstruct the order of the events, so instead of subtracting we
     compute positive (deposit, melt) and negative (refund) values separately
     here, and then subtract the negative from the positive at the end (after
     the loops). *///
  refund_deposit_fee = GNUNET_NO;
  deposit_fee = NULL;
  for (const struct TALER_EXCHANGEDB_TransactionList *tl = tl_head;
       NULL != tl;
       tl = tl->next)
  {
    const struct TALER_Amount *amount_with_fee;
    const struct TALER_Amount *fee_claimed;

    switch (tl->type)
    {
    case TALER_EXCHANGEDB_TT_DEPOSIT:
      /* check wire and h_wire are consistent */
      {
        struct GNUNET_HashCode hw;

        if (GNUNET_OK !=
            TALER_JSON_merchant_wire_signature_hash (
              tl->details.deposit->receiver_wire_account,
              &hw))
        {
          report_row_inconsistency ("deposits",
                                    tl->serial_id,
                                    "wire account given is malformed");
        }
        else if (0 !=
                 GNUNET_memcmp (&hw,
                                &tl->details.deposit->h_wire))
        {
          report_row_inconsistency ("deposits",
                                    tl->serial_id,
                                    "h(wire) does not match wire");
        }
      }
      amount_with_fee = &tl->details.deposit->amount_with_fee; /* according to exchange*/
      fee_claimed = &tl->details.deposit->deposit_fee; /* Fee according to exchange DB */
      TALER_ARL_amount_add (&expenditures,
                            &expenditures,
                            amount_with_fee);
      /* Check if this deposit is within the remit of the aggregation
         we are investigating, if so, include it in the totals. */
      if ( (0 == GNUNET_memcmp (merchant_pub,
                                &tl->details.deposit->merchant_pub)) &&
           (0 == GNUNET_memcmp (h_contract_terms,
                                &tl->details.deposit->h_contract_terms)) )
      {
        struct TALER_Amount amount_without_fee;

        TALER_ARL_amount_subtract (&amount_without_fee,
                                   amount_with_fee,
                                   fee_claimed);
        TALER_ARL_amount_add (merchant_gain,
                              merchant_gain,
                              &amount_without_fee);
        GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                    "Detected applicable deposit of %s\n",
                    TALER_amount2s (&amount_without_fee));
        deposit_fee = fee_claimed; /* We had a deposit, remember the fee, we may need it */
      }
      /* Check that the fees given in the transaction list and in dki match */
      {
        struct TALER_Amount fee_expected;

        /* Fee according to denomination data of auditor */
        TALER_amount_ntoh (&fee_expected,
                           &issue->fee_deposit);
        if (0 !=
            TALER_amount_cmp (&fee_expected,
                              fee_claimed))
        {
          /* Disagreement in fee structure between auditor and exchange DB! */
          report_amount_arithmetic_inconsistency ("deposit fee",
                                                  UINT64_MAX,
                                                  fee_claimed,
                                                  &fee_expected,
                                                  1);
        }
      }
      break;
    case TALER_EXCHANGEDB_TT_MELT:
      amount_with_fee = &tl->details.melt->amount_with_fee;
      fee_claimed = &tl->details.melt->melt_fee;
      TALER_ARL_amount_add (&expenditures,
                            &expenditures,
                            amount_with_fee);
      /* Check that the fees given in the transaction list and in dki match */
      {
        struct TALER_Amount fee_expected;

        TALER_amount_ntoh (&fee_expected,
                           &issue->fee_refresh);
        if (0 !=
            TALER_amount_cmp (&fee_expected,
                              fee_claimed))
        {
          /* Disagreement in fee structure between exchange and auditor */
          report_amount_arithmetic_inconsistency ("melt fee",
                                                  UINT64_MAX,
                                                  fee_claimed,
                                                  &fee_expected,
                                                  1);
        }
      }
      break;
    case TALER_EXCHANGEDB_TT_REFUND:
      amount_with_fee = &tl->details.refund->refund_amount;
      fee_claimed = &tl->details.refund->refund_fee;
      TALER_ARL_amount_add (&refunds,
                            &refunds,
                            amount_with_fee);
      TALER_ARL_amount_add (&expenditures,
                            &expenditures,
                            fee_claimed);
      /* Check if this refund is within the remit of the aggregation
         we are investigating, if so, include it in the totals. */
      if ( (0 == GNUNET_memcmp (merchant_pub,
                                &tl->details.refund->merchant_pub)) &&
           (0 == GNUNET_memcmp (h_contract_terms,
                                &tl->details.refund->h_contract_terms)) )
      {
        GNUNET_log (GNUNET_ERROR_TYPE_INFO,
                    "Detected applicable refund of %s\n",
                    TALER_amount2s (amount_with_fee));
        TALER_ARL_amount_add (&merchant_loss,
                              &merchant_loss,
                              amount_with_fee);
        /* If there is a refund, we give back the deposit fee */
        refund_deposit_fee = GNUNET_YES;
      }
      /* Check that the fees given in the transaction list and in dki match */
      {
        struct TALER_Amount fee_expected;

        TALER_amount_ntoh (&fee_expected,
                           &issue->fee_refund);
        if (0 !=
            TALER_amount_cmp (&fee_expected,
                              fee_claimed))
        {
          /* Disagreement in fee structure between exchange and auditor! */
          report_amount_arithmetic_inconsistency ("refund fee",
                                                  UINT64_MAX,
                                                  fee_claimed,
                                                  &fee_expected,
                                                  1);
        }
      }
      break;
    case TALER_EXCHANGEDB_TT_OLD_COIN_RECOUP:
      amount_with_fee = &tl->details.old_coin_recoup->value;
      /* We count recoups of refreshed coins like refunds for the dirty old
         coin, as they equivalently _increase_ the remaining value on the
         _old_ coin */
      TALER_ARL_amount_add (&refunds,
                            &refunds,
                            amount_with_fee);
      break;
    case TALER_EXCHANGEDB_TT_RECOUP:
      /* We count recoups of the coin as expenditures, as it
         equivalently decreases the remaining value of the recouped coin. */
      amount_with_fee = &tl->details.recoup->value;
      TALER_ARL_amount_add (&expenditures,
                            &expenditures,
                            amount_with_fee);
      break;
    case TALER_EXCHANGEDB_TT_RECOUP_REFRESH:
      /* We count recoups of the coin as expenditures, as it
         equivalently decreases the remaining value of the recouped coin. */
      amount_with_fee = &tl->details.recoup_refresh->value;
      TALER_ARL_amount_add (&expenditures,
                            &expenditures,
                            amount_with_fee);
      break;
    }
  } /* for 'tl' */

  GNUNET_log (GNUNET_ERROR_TYPE_INFO,
              "Deposits for this aggregation (after fees) are %s\n",
              TALER_amount2s (merchant_gain));
  GNUNET_log (GNUNET_ERROR_TYPE_INFO,
              "Aggregation loss due to refunds is %s\n",
              TALER_amount2s (&merchant_loss));
  *deposit_gain = *merchant_gain;
  if ( (GNUNET_YES == refund_deposit_fee) &&
       (NULL != deposit_fee) )
  {
    /* We had a /deposit operation AND a /refund operation,
       and should thus not charge the merchant the /deposit fee */
    TALER_ARL_amount_add (merchant_gain,
                          merchant_gain,
                          deposit_fee);
  }
  {
    struct TALER_Amount final_gain;

    if (TALER_ARL_SR_INVALID_NEGATIVE ==
        TALER_ARL_amount_subtract_neg (&final_gain,
                                       merchant_gain,
                                       &merchant_loss))
    {
      /* refunds above deposits? Bad! */
      report_coin_arithmetic_inconsistency ("refund (merchant)",
                                            coin_pub,
                                            merchant_gain,
                                            &merchant_loss,
                                            1);
      /* For the overall aggregation, we should not count this
         as a NEGATIVE contribution as that is not allowed; so
         let's count it as zero as that's the best we can do. */
      GNUNET_assert (GNUNET_OK ==
                     TALER_amount_set_zero (TALER_ARL_currency,
                                            merchant_gain));
    }
    else
    {
      *merchant_gain = final_gain;
    }
  }


  /* Calculate total balance change, i.e. expenditures (recoup, deposit, refresh)
     minus refunds (refunds, recoup-to-old) */
  GNUNET_log (GNUNET_ERROR_TYPE_INFO,
              "Subtracting refunds of %s from coin value loss\n",
              TALER_amount2s (&refunds));
  if (TALER_ARL_SR_INVALID_NEGATIVE ==
      TALER_ARL_amount_subtract_neg (&spent,
                                     &expenditures,
                                     &refunds))
  {
    /* refunds above expenditures? Bad! */
    report_coin_arithmetic_inconsistency ("refund (balance)",
                                          coin_pub,
                                          &expenditures,
                                          &refunds,
                                          1);
  }
  else
  {
    /* Now check that 'spent' is less or equal than the total coin value */
    struct TALER_Amount value;

    TALER_amount_ntoh (&value,
                       &issue->value);
    if (1 == TALER_amount_cmp (&spent,
                               &value))
    {
      /* spent > value */
      report_coin_arithmetic_inconsistency ("spend",
                                            coin_pub,
                                            &spent,
                                            &value,
                                            -1);
    }
  }


  GNUNET_log (GNUNET_ERROR_TYPE_INFO,
              "Final merchant gain after refunds is %s\n",
              TALER_amount2s (deposit_gain));
  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              "Coin %s contributes %s to contract %s\n",
              TALER_B2S (coin_pub),
              TALER_amount2s (merchant_gain),
              GNUNET_h2s (h_contract_terms));
  return GNUNET_OK;
}


/**
 * Function called with the results of the lookup of the
 * transaction data associated with a wire transfer identifier.
 *
 * @param[in,out] cls a `struct WireCheckContext`
 * @param rowid which row in the table is the information from (for diagnostics)
 * @param merchant_pub public key of the merchant (should be same for all callbacks with the same @e cls)
 * @param h_wire hash of wire transfer details of the merchant (should be same for all callbacks with the same @e cls)
 * @param account_details where did we transfer the funds?
 * @param exec_time execution time of the wire transfer (should be same for all callbacks with the same @e cls)
 * @param h_contract_terms which proposal was this payment about
 * @param denom_pub denomination of @a coin_pub
 * @param coin_pub which public key was this payment about
 * @param coin_value amount contributed by this coin in total (with fee),
 *                   but excluding refunds by this coin
 * @param deposit_fee applicable deposit fee for this coin, actual
 *        fees charged may differ if coin was refunded
 */
static void
wire_transfer_information_cb (
  void *cls,
  uint64_t rowid,
  const struct TALER_MerchantPublicKeyP *merchant_pub,
  const struct GNUNET_HashCode *h_wire,
  const json_t *account_details,
  struct GNUNET_TIME_Absolute exec_time,
  const struct GNUNET_HashCode *h_contract_terms,
  const struct TALER_DenominationPublicKey *denom_pub,
  const struct TALER_CoinSpendPublicKeyP *coin_pub,
  const struct TALER_Amount *coin_value,
  const struct TALER_Amount *deposit_fee)
{
  struct WireCheckContext *wcc = cls;
  const struct TALER_DenominationKeyValidityPS *issue;
  struct TALER_Amount computed_value;
  struct TALER_Amount total_deposit_without_refunds;
  struct TALER_EXCHANGEDB_TransactionList *tl;
  struct TALER_CoinPublicInfo coin;
  enum GNUNET_DB_QueryStatus qs;
  struct GNUNET_HashCode hw;

  if (GNUNET_OK !=
      TALER_JSON_merchant_wire_signature_hash (account_details,
                                               &hw))
  {
    report_row_inconsistency ("aggregation",
                              rowid,
                              "failed to compute hash of given wire data");
  }
  else if (0 !=
           GNUNET_memcmp (&hw,
                          h_wire))
  {
    report_row_inconsistency ("aggregation",
                              rowid,
                              "database contains wrong hash code for wire details");
  }

  /* Obtain coin's transaction history */
  qs = TALER_ARL_edb->get_coin_transactions (TALER_ARL_edb->cls,
                                             TALER_ARL_esession,
                                             coin_pub,
                                             GNUNET_YES,
                                             &tl);
  if ( (qs < 0) ||
       (NULL == tl) )
  {
    wcc->qs = qs;
    report_row_inconsistency ("aggregation",
                              rowid,
                              "no transaction history for coin claimed in aggregation");
    return;
  }
  qs = TALER_ARL_edb->get_known_coin (TALER_ARL_edb->cls,
                                      TALER_ARL_esession,
                                      coin_pub,
                                      &coin);
  if (qs <= 0)
  {
    /* this should be a foreign key violation at this point! */
    GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs);
    wcc->qs = qs;
    report_row_inconsistency ("aggregation",
                              rowid,
                              "could not get coin details for coin claimed in aggregation");
    TALER_ARL_edb->free_coin_transaction_list (TALER_ARL_edb->cls,
                                               tl);
    return;
  }

  qs = TALER_ARL_get_denomination_info_by_hash (&coin.denom_pub_hash,
                                                &issue);
  if (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT != qs)
  {
    GNUNET_CRYPTO_rsa_signature_free (coin.denom_sig.rsa_signature);
    TALER_ARL_edb->free_coin_transaction_list (TALER_ARL_edb->cls,
                                               tl);
    if (0 == qs)
      report_row_inconsistency ("aggregation",
                                rowid,
                                "could not find denomination key for coin claimed in aggregation");
    else
      wcc->qs = qs;
    return;
  }
  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              "Testing coin `%s' for validity\n",
              TALER_B2S (&coin.coin_pub));
  if (GNUNET_OK !=
      TALER_test_coin_valid (&coin,
                             denom_pub))
  {
    TALER_ARL_report (report_bad_sig_losses,
                      json_pack ("{s:s, s:I, s:o, s:o}",
                                 "operation", "wire",
                                 "row", (json_int_t) rowid,
                                 "loss", TALER_JSON_from_amount (coin_value),
                                 "coin_pub", GNUNET_JSON_from_data_auto (
                                   &coin.coin_pub)));
    TALER_ARL_amount_add (&total_bad_sig_loss,
                          &total_bad_sig_loss,
                          coin_value);
    GNUNET_CRYPTO_rsa_signature_free (coin.denom_sig.rsa_signature);
    TALER_ARL_edb->free_coin_transaction_list (TALER_ARL_edb->cls,
                                               tl);
    report_row_inconsistency ("deposit",
                              rowid,
                              "coin denomination signature invalid");
    return;
  }
  GNUNET_CRYPTO_rsa_signature_free (coin.denom_sig.rsa_signature);
  coin.denom_sig.rsa_signature = NULL; /* just to be sure */
  GNUNET_assert (NULL != issue); /* mostly to help static analysis */
  /* Check transaction history to see if it supports aggregate
     valuation */
  if (GNUNET_OK !=
      check_transaction_history_for_deposit (coin_pub,
                                             h_contract_terms,
                                             merchant_pub,
                                             issue,
                                             tl,
                                             &computed_value,
                                             &total_deposit_without_refunds))
  {
    TALER_ARL_edb->free_coin_transaction_list (TALER_ARL_edb->cls,
                                               tl);
    wcc->qs = GNUNET_DB_STATUS_HARD_ERROR;
    return;
  }
  TALER_ARL_edb->free_coin_transaction_list (TALER_ARL_edb->cls,
                                             tl);
  GNUNET_log (GNUNET_ERROR_TYPE_INFO,
              "Coin contributes %s to aggregate (deposits after fees and refunds)\n",
              TALER_amount2s (&computed_value));
  {
    struct TALER_Amount coin_value_without_fee;

    if (TALER_ARL_SR_INVALID_NEGATIVE ==
        TALER_ARL_amount_subtract_neg (&coin_value_without_fee,
                                       coin_value,
                                       deposit_fee))
    {
      wcc->qs = GNUNET_DB_STATUS_HARD_ERROR;
      report_amount_arithmetic_inconsistency (
        "aggregation (fee structure)",
        rowid,
        coin_value,
        deposit_fee,
        -1);
      return;
    }
    if (0 !=
        TALER_amount_cmp (&total_deposit_without_refunds,
                          &coin_value_without_fee))
    {
      GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
                  "Expected coin contribution of %s to aggregate\n",
                  TALER_amount2s (&coin_value_without_fee));
      report_amount_arithmetic_inconsistency (
        "aggregation (contribution)",
        rowid,
        &coin_value_without_fee,
        &
        total_deposit_without_refunds,
        -1);
    }
  }
  /* Check other details of wire transfer match */
  if (0 != GNUNET_memcmp (h_wire,
                          &wcc->h_wire))
  {
    report_row_inconsistency ("aggregation",
                              rowid,
                              "target of outgoing wire transfer do not match hash of wire from deposit");
  }
  if (exec_time.abs_value_us != wcc->date.abs_value_us)
  {
    /* This should be impossible from database constraints */
    GNUNET_break (0);
    report_row_inconsistency ("aggregation",
                              rowid,
                              "date given in aggregate does not match wire transfer date");
  }

  /* Add coin's contribution to total aggregate value */
  {
    struct TALER_Amount res;

    TALER_ARL_amount_add (&res,
                          &wcc->total_deposits,
                          &computed_value);
    wcc->total_deposits = res;
  }
}


/**
 * Lookup the wire fee that the exchange charges at @a timestamp.
 *
 * @param ac context for caching the result
 * @param method method of the wire plugin
 * @param timestamp time for which we need the fee
 * @return NULL on error (fee unknown)
 */
static const struct TALER_Amount *
get_wire_fee (struct AggregationContext *ac,
              const char *method,
              struct GNUNET_TIME_Absolute timestamp)
{
  struct WireFeeInfo *wfi;
  struct WireFeeInfo *pos;
  struct TALER_MasterSignatureP master_sig;

  /* Check if fee is already loaded in cache */
  for (pos = ac->fee_head; NULL != pos; pos = pos->next)
  {
    if ( (pos->start_date.abs_value_us <= timestamp.abs_value_us) &&
         (pos->end_date.abs_value_us > timestamp.abs_value_us) )
      return &pos->wire_fee;
    if (pos->start_date.abs_value_us > timestamp.abs_value_us)
      break;
  }

  /* Lookup fee in exchange database */
  wfi = GNUNET_new (struct WireFeeInfo);
  if (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT !=
      TALER_ARL_edb->get_wire_fee (TALER_ARL_edb->cls,
                                   TALER_ARL_esession,
                                   method,
                                   timestamp,
                                   &wfi->start_date,
                                   &wfi->end_date,
                                   &wfi->wire_fee,
                                   &wfi->closing_fee,
                                   &master_sig))
  {
    GNUNET_break (0);
    GNUNET_free (wfi);
    return NULL;
  }

  /* Check signature. (This is not terribly meaningful as the exchange can
     easily make this one up, but it means that we have proof that the master
     key was used for inconsistent wire fees if a merchant complains.) */
  {
    if (GNUNET_OK !=
        TALER_exchange_offline_wire_fee_verify (
          method,
          wfi->start_date,
          wfi->end_date,
          &wfi->wire_fee,
          &wfi->closing_fee,
          &TALER_ARL_master_pub,
          &master_sig))
    {
      report_row_inconsistency ("wire-fee",
                                timestamp.abs_value_us,
                                "wire fee signature invalid at given time");
    }
  }

  /* Established fee, keep in sorted list */
  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              "Wire fee is %s starting at %s\n",
              TALER_amount2s (&wfi->wire_fee),
              GNUNET_STRINGS_absolute_time_to_string (wfi->start_date));
  if ( (NULL == pos) ||
       (NULL == pos->prev) )
    GNUNET_CONTAINER_DLL_insert (ac->fee_head,
                                 ac->fee_tail,
                                 wfi);
  else
    GNUNET_CONTAINER_DLL_insert_after (ac->fee_head,
                                       ac->fee_tail,
                                       pos->prev,
                                       wfi);
  /* Check non-overlaping fee invariant */
  if ( (NULL != wfi->prev) &&
       (wfi->prev->end_date.abs_value_us > wfi->start_date.abs_value_us) )
  {
    TALER_ARL_report (report_fee_time_inconsistencies,
                      json_pack ("{s:s, s:s, s:o}",
                                 "type", method,
                                 "diagnostic",
                                 "start date before previous end date",
                                 "time", TALER_ARL_json_from_time_abs (
                                   wfi->start_date)));
  }
  if ( (NULL != wfi->next) &&
       (wfi->next->start_date.abs_value_us >= wfi->end_date.abs_value_us) )
  {
    TALER_ARL_report (report_fee_time_inconsistencies,
                      json_pack ("{s:s, s:s, s:o}",
                                 "type", method,
                                 "diagnostic",
                                 "end date date after next start date",
                                 "time", TALER_ARL_json_from_time_abs (
                                   wfi->end_date)));
  }
  return &wfi->wire_fee;
}


/**
 * Check that a wire transfer made by the exchange is valid
 * (has matching deposits).
 *
 * @param cls a `struct AggregationContext`
 * @param rowid identifier of the respective row in the database
 * @param date timestamp of the wire transfer (roughly)
 * @param wtid wire transfer subject
 * @param wire wire transfer details of the receiver
 * @param amount amount that was wired
 * @return #GNUNET_OK to continue, #GNUNET_SYSERR to stop iteration
 */
static int
check_wire_out_cb (void *cls,
                   uint64_t rowid,
                   struct GNUNET_TIME_Absolute date,
                   const struct TALER_WireTransferIdentifierRawP *wtid,
                   const json_t *wire,
                   const struct TALER_Amount *amount)
{
  struct AggregationContext *ac = cls;
  struct WireCheckContext wcc;
  struct TALER_Amount final_amount;
  struct TALER_Amount exchange_gain;
  enum GNUNET_DB_QueryStatus qs;
  char *method;

  /* should be monotonically increasing */
  GNUNET_assert (rowid >= ppa.last_wire_out_serial_id);
  ppa.last_wire_out_serial_id = rowid + 1;

  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              "Checking wire transfer %s over %s performed on %s\n",
              TALER_B2S (wtid),
              TALER_amount2s (amount),
              GNUNET_STRINGS_absolute_time_to_string (date));
  if (NULL == (method = TALER_JSON_wire_to_method (wire)))
  {
    report_row_inconsistency ("wire_out",
                              rowid,
                              "specified wire address lacks method");
    return GNUNET_OK;
  }

  wcc.ac = ac;
  wcc.qs = GNUNET_DB_STATUS_SUCCESS_ONE_RESULT;
  wcc.date = date;
  GNUNET_assert (GNUNET_OK ==
                 TALER_amount_set_zero (amount->currency,
                                        &wcc.total_deposits));
  if (GNUNET_OK !=
      TALER_JSON_merchant_wire_signature_hash (wire,
                                               &wcc.h_wire))
  {
    GNUNET_break (0);
    GNUNET_free (method);
    return GNUNET_SYSERR;
  }
  qs = TALER_ARL_edb->lookup_wire_transfer (TALER_ARL_edb->cls,
                                            TALER_ARL_esession,
                                            wtid,
                                            &wire_transfer_information_cb,
                                            &wcc);
  if (0 > qs)
  {
    GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs);
    ac->qs = qs;
    GNUNET_free (method);
    return GNUNET_SYSERR;
  }
  if (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT != wcc.qs)
  {
    /* Note: detailed information was already logged
       in #wire_transfer_information_cb, so here we
       only log for debugging */
    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                "Inconsitency for wire_out %llu (WTID %s) detected\n",
                (unsigned long long) rowid,
                TALER_B2S (wtid));
  }


  /* Subtract aggregation fee from total (if possible) */
  {
    const struct TALER_Amount *wire_fee;

    wire_fee = get_wire_fee (ac,
                             method,
                             date);
    if (NULL == wire_fee)
    {
      report_row_inconsistency ("wire-fee",
                                date.abs_value_us,
                                "wire fee unavailable for given time");
      /* If fee is unknown, we just assume the fee is zero */
      final_amount = wcc.total_deposits;
    }
    else if (TALER_ARL_SR_INVALID_NEGATIVE ==
             TALER_ARL_amount_subtract_neg (&final_amount,
                                            &wcc.total_deposits,
                                            wire_fee))
    {
      report_amount_arithmetic_inconsistency (
        "wire out (fee structure)",
        rowid,
        &wcc.total_deposits,
        wire_fee,
        -1);
      /* If fee arithmetic fails, we just assume the fee is zero */
      final_amount = wcc.total_deposits;
    }
  }
  GNUNET_free (method);

  /* Round down to amount supported by wire method */
  GNUNET_break (GNUNET_SYSERR !=
                TALER_amount_round_down (&final_amount,
                                         &TALER_ARL_currency_round_unit));

  /* Calculate the exchange's gain as the fees plus rounding differences! */
  TALER_ARL_amount_subtract (&exchange_gain,
                             &wcc.total_deposits,
                             &final_amount);
  /* Sum up aggregation fees (we simply include the rounding gains) */
  TALER_ARL_amount_add (&total_aggregation_fee_income,
                        &total_aggregation_fee_income,
                        &exchange_gain);

  /* Check that calculated amount matches actual amount */
  if (0 != TALER_amount_cmp (amount,
                             &final_amount))
  {
    struct TALER_Amount delta;

    if (0 < TALER_amount_cmp (amount,
                              &final_amount))
    {
      /* amount > final_amount */
      TALER_ARL_amount_subtract (&delta,
                                 amount,
                                 &final_amount);
      TALER_ARL_amount_add (&total_wire_out_delta_plus,
                            &total_wire_out_delta_plus,
                            &delta);
    }
    else
    {
      /* amount < final_amount */
      TALER_ARL_amount_subtract (&delta,
                                 &final_amount,
                                 amount);
      TALER_ARL_amount_add (&total_wire_out_delta_minus,
                            &total_wire_out_delta_minus,
                            &delta);
    }

    TALER_ARL_report (report_wire_out_inconsistencies,
                      json_pack ("{s:O, s:I, s:o, s:o}",
                                 "destination_account", wire,
                                 "rowid", (json_int_t) rowid,
                                 "expected",
                                 TALER_JSON_from_amount (&final_amount),
                                 "claimed",
                                 TALER_JSON_from_amount (amount)));
    if (TALER_ARL_do_abort ())
      return GNUNET_SYSERR;
    return GNUNET_OK;
  }
  GNUNET_log (GNUNET_ERROR_TYPE_INFO,
              "Aggregation unit %s is OK\n",
              TALER_B2S (wtid));
  if (TALER_ARL_do_abort ())
    return GNUNET_SYSERR;
  return GNUNET_OK;
}


/**
 * Analyze the exchange aggregator's payment processing.
 *
 * @param cls closure
 * @return transaction status code
 */
static enum GNUNET_DB_QueryStatus
analyze_aggregations (void *cls)
{
  struct AggregationContext ac;
  struct WireFeeInfo *wfi;
  enum GNUNET_DB_QueryStatus qsx;
  enum GNUNET_DB_QueryStatus qs;
  enum GNUNET_DB_QueryStatus qsp;

  (void) cls;
  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              "Analyzing aggregations\n");
  qsp = TALER_ARL_adb->get_auditor_progress_aggregation (TALER_ARL_adb->cls,
                                                         TALER_ARL_asession,
                                                         &TALER_ARL_master_pub,
                                                         &ppa);
  if (0 > qsp)
  {
    GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qsp);
    return qsp;
  }
  if (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS == qsp)
  {
    GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
                "First analysis using this auditor, starting audit from scratch\n");
  }
  else
  {
    ppa_start = ppa;
    GNUNET_log (GNUNET_ERROR_TYPE_INFO,
                "Resuming aggregation audit at %llu\n",
                (unsigned long long) ppa.last_wire_out_serial_id);
  }

  memset (&ac,
          0,
          sizeof (ac));
  qsx = TALER_ARL_adb->get_wire_fee_summary (TALER_ARL_adb->cls,
                                             TALER_ARL_asession,
                                             &TALER_ARL_master_pub,
                                             &total_aggregation_fee_income);
  if (0 > qsx)
  {
    GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qsx);
    return qsx;
  }
  ac.qs = GNUNET_DB_STATUS_SUCCESS_ONE_RESULT;
  qs = TALER_ARL_edb->select_wire_out_above_serial_id (
    TALER_ARL_edb->cls,
    TALER_ARL_esession,
    ppa.last_wire_out_serial_id,
    &check_wire_out_cb,
    &ac);
  if (0 > qs)
  {
    GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs);
    ac.qs = qs;
  }
  while (NULL != (wfi = ac.fee_head))
  {
    GNUNET_CONTAINER_DLL_remove (ac.fee_head,
                                 ac.fee_tail,
                                 wfi);
    GNUNET_free (wfi);
  }
  if (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS == qs)
  {
    /* there were no wire out entries to be looked at, we are done */
    return qs;
  }
  if (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT != ac.qs)
  {
    GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == ac.qs);
    return ac.qs;
  }
  if (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS == qsx)
    ac.qs = TALER_ARL_adb->insert_wire_fee_summary (
      TALER_ARL_adb->cls,
      TALER_ARL_asession,
      &TALER_ARL_master_pub,
      &total_aggregation_fee_income);
  else
    ac.qs = TALER_ARL_adb->update_wire_fee_summary (
      TALER_ARL_adb->cls,
      TALER_ARL_asession,
      &TALER_ARL_master_pub,
      &total_aggregation_fee_income);
  if (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT != ac.qs)
  {
    GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == ac.qs);
    return ac.qs;
  }
  if (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT == qsp)
    qs = TALER_ARL_adb->update_auditor_progress_aggregation (
      TALER_ARL_adb->cls,
      TALER_ARL_asession,
      &TALER_ARL_master_pub,
      &ppa);
  else
    qs = TALER_ARL_adb->insert_auditor_progress_aggregation (
      TALER_ARL_adb->cls,
      TALER_ARL_asession,
      &TALER_ARL_master_pub,
      &ppa);
  if (0 >= qs)
  {
    GNUNET_log (GNUNET_ERROR_TYPE_INFO,
                "Failed to update auditor DB, not recording progress\n");
    GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs);
    return qs;
  }
  GNUNET_log (GNUNET_ERROR_TYPE_INFO,
              "Concluded aggregation audit step at %llu\n",
              (unsigned long long) ppa.last_wire_out_serial_id);

  return GNUNET_DB_STATUS_SUCCESS_ONE_RESULT;
}


/**
 * Main function that will be run.
 *
 * @param cls closure
 * @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)
{
  json_t *report;

  (void) cls;
  (void) args;
  (void) cfgfile;
  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              "Launching auditor\n");
  if (GNUNET_OK !=
      TALER_ARL_init (c))
  {
    global_ret = 1;
    return;
  }
  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              "Starting audit\n");
  GNUNET_assert (GNUNET_OK ==
                 TALER_amount_set_zero (TALER_ARL_currency,
                                        &total_aggregation_fee_income));
  GNUNET_assert (GNUNET_OK ==
                 TALER_amount_set_zero (TALER_ARL_currency,
                                        &total_wire_out_delta_plus));
  GNUNET_assert (GNUNET_OK ==
                 TALER_amount_set_zero (TALER_ARL_currency,
                                        &total_wire_out_delta_minus));
  GNUNET_assert (GNUNET_OK ==
                 TALER_amount_set_zero (TALER_ARL_currency,
                                        &total_arithmetic_delta_plus));
  GNUNET_assert (GNUNET_OK ==
                 TALER_amount_set_zero (TALER_ARL_currency,
                                        &total_arithmetic_delta_minus));
  GNUNET_assert (GNUNET_OK ==
                 TALER_amount_set_zero (TALER_ARL_currency,
                                        &total_coin_delta_plus));
  GNUNET_assert (GNUNET_OK ==
                 TALER_amount_set_zero (TALER_ARL_currency,
                                        &total_coin_delta_minus));
  GNUNET_assert (GNUNET_OK ==
                 TALER_amount_set_zero (TALER_ARL_currency,
                                        &total_bad_sig_loss));
  GNUNET_assert (NULL !=
                 (report_row_inconsistencies
                    = json_array ()));
  GNUNET_assert (NULL !=
                 (report_wire_out_inconsistencies
                    = json_array ()));
  GNUNET_assert (NULL !=
                 (report_coin_inconsistencies
                    = json_array ()));
  GNUNET_assert (NULL !=
                 (report_amount_arithmetic_inconsistencies
                    = json_array ()));
  GNUNET_assert (NULL !=
                 (report_bad_sig_losses
                    = json_array ()));
  GNUNET_assert (NULL !=
                 (report_fee_time_inconsistencies
                    = json_array ()));
  if (GNUNET_OK !=
      TALER_ARL_setup_sessions_and_run (&analyze_aggregations,
                                        NULL))
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                "Audit failed\n");
    TALER_ARL_done (NULL);
    global_ret = 1;
    return;
  }
  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              "Audit complete\n");
  report = json_pack ("{s:o, s:o, s:o, s:o, s:o,"
                      " s:o, s:o, s:o, s:o, s:o,"
                      " s:o, s:o, s:o, s:I, s:I,"
                      " s:o, s:o, s:o }",
                      /* blocks #1 */
                      "wire_out_inconsistencies",
                      report_wire_out_inconsistencies,
                      /* Tested in test-auditor.sh #23 */
                      "total_wire_out_delta_plus",
                      TALER_JSON_from_amount (
                        &total_wire_out_delta_plus),
                      /* Tested in test-auditor.sh #23 */
                      "total_wire_out_delta_minus",
                      TALER_JSON_from_amount (
                        &total_wire_out_delta_minus),
                      /* Tested in test-auditor.sh #28/32 */
                      "bad_sig_losses",
                      report_bad_sig_losses,
                      /* Tested in test-auditor.sh #28/32 */
                      "total_bad_sig_loss",
                      TALER_JSON_from_amount (&total_bad_sig_loss),
                      /* block #2 */
                      /* Tested in test-auditor.sh #15 */
                      "row_inconsistencies",
                      report_row_inconsistencies,
                      "coin_inconsistencies",
                      report_coin_inconsistencies,
                      "total_coin_delta_plus",
                      TALER_JSON_from_amount (&total_coin_delta_plus),
                      "total_coin_delta_minus",
                      TALER_JSON_from_amount (
                        &total_coin_delta_minus),
                      "amount_arithmetic_inconsistencies",
                      report_amount_arithmetic_inconsistencies,
                      /* block #3 */
                      "total_arithmetic_delta_plus",
                      TALER_JSON_from_amount (
                        &total_arithmetic_delta_plus),
                      "total_arithmetic_delta_minus",
                      TALER_JSON_from_amount (
                        &total_arithmetic_delta_minus),
                      "total_aggregation_fee_income",
                      TALER_JSON_from_amount (
                        &total_aggregation_fee_income),
                      "start_ppa_wire_out_serial_id",
                      (json_int_t) ppa_start.last_wire_out_serial_id,
                      "end_ppa_wire_out_serial_id",
                      (json_int_t) ppa.last_wire_out_serial_id,
                      /* block #4 */
                      "auditor_start_time",
                      TALER_ARL_json_from_time_abs (
                        start_time),
                      "auditor_end_time",
                      TALER_ARL_json_from_time_abs (
                        GNUNET_TIME_absolute_get ()),
                      "wire_fee_time_inconsistencies",
                      report_fee_time_inconsistencies
                      );
  GNUNET_break (NULL != report);
  TALER_ARL_done (report);
}


/**
 * The main function to audit the exchange's aggregation processing.
 *
 * @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)
{
  const struct GNUNET_GETOPT_CommandLineOption options[] = {
    GNUNET_GETOPT_option_flag ('i',
                               "internal",
                               "perform checks only applicable for exchange-internal audits",
                               &internal_checks),
    GNUNET_GETOPT_option_base32_auto ('m',
                                      "exchange-key",
                                      "KEY",
                                      "public key of the exchange (Crockford base32 encoded)",
                                      &TALER_ARL_master_pub),
    GNUNET_GETOPT_option_timetravel ('T',
                                     "timetravel"),
    GNUNET_GETOPT_OPTION_END
  };
  enum GNUNET_GenericReturnValue ret;

  /* force linker to link against libtalerutil; if we do
     not do this, the linker may "optimize" libtalerutil
     away and skip #TALER_OS_init(), which we do need */
  (void) TALER_project_data_default ();
  if (GNUNET_OK !=
      GNUNET_STRINGS_get_utf8_args (argc, argv,
                                    &argc, &argv))
    return 4;
  ret = GNUNET_PROGRAM_run (
    argc,
    argv,
    "taler-helper-auditor-aggregation",
    gettext_noop ("Audit Taler exchange aggregation activity"),
    options,
    &run,
    NULL);
  GNUNET_free_nz ((void *) argv);
  if (GNUNET_SYSERR == ret)
    return 3;
  if (GNUNET_NO == ret)
    return 0;
  return global_ret;
}


/* end of taler-helper-auditor-aggregation.c */