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
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
|
/*
This file is part of TALER
Copyright (C) 2016-2024 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_bank_service.h"
#include "taler_signatures.h"
#include "taler_dbevents.h"
#include "report-lib.h"
/**
* Return value from main().
*/
static int global_ret;
/**
* Run in test mode. Exit when idle instead of
* going to sleep and waiting for more work.
*
* FIXME: not yet implemented!
*/
static int test_mode;
/**
* Checkpointing our progress for aggregations.
*/
static TALER_ARL_DEF_PP (aggregation_last_wire_out_serial_id);
/**
* Total aggregation fees (wire fees) earned.
*/
static TALER_ARL_DEF_AB (aggregation_total_wire_fee_revenue);
/**
* Total delta between calculated and stored wire out transfers,
* for positive deltas.
*/
static TALER_ARL_DEF_AB (aggregation_total_wire_out_delta_plus);
/**
* Total delta between calculated and stored wire out transfers
* for negative deltas.
*/
static TALER_ARL_DEF_AB (aggregation_total_wire_out_delta_minus);
/**
* Profits the exchange made by bad amount calculations on coins.
*/
static TALER_ARL_DEF_AB (aggregation_total_coin_delta_plus);
/**
* Losses the exchange made by bad amount calculations on coins.
*/
static TALER_ARL_DEF_AB (aggregation_total_coin_delta_minus);
/**
* Profits the exchange made by bad amount calculations.
*/
static TALER_ARL_DEF_AB (aggregation_total_arithmetic_delta_plus);
/**
* Losses the exchange made by bad amount calculations.
*/
static TALER_ARL_DEF_AB (aggregation_total_arithmetic_delta_minus);
/**
* Total amount lost by operations for which signatures were invalid.
*/
static TALER_ARL_DEF_AB (aggregation_total_bad_sig_loss);
/**
* Should we run checks that only work for exchange-internal audits?
*/
static int internal_checks;
static struct GNUNET_DB_EventHandler *eh;
/**
* The auditors's configuration.
*/
static const struct GNUNET_CONFIGURATION_Handle *cfg;
/**
* 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, 0 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
* @return transaction status
*/
static enum GNUNET_DB_QueryStatus
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);
}
{
enum GNUNET_DB_QueryStatus qs;
struct TALER_AUDITORDB_AmountArithmeticInconsistency aai = {
.profitable = profitable,
.operation = (char *) operation,
.exchange_amount = *exchange,
.auditor_amount = *auditor
};
qs = TALER_ARL_adb->insert_amount_arithmetic_inconsistency (
TALER_ARL_adb->cls,
&aai);
if (qs < 0)
{
GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs);
return qs;
}
}
if (0 != profitable)
{
target = (1 == profitable)
? &TALER_ARL_USE_AB (aggregation_total_arithmetic_delta_plus)
: &TALER_ARL_USE_AB (aggregation_total_arithmetic_delta_minus);
TALER_ARL_amount_add (target,
target,
&delta);
}
return GNUNET_DB_STATUS_SUCCESS_ONE_RESULT;
}
/**
* 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
* @return transaction status
*/
static enum GNUNET_DB_QueryStatus
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);
}
{
enum GNUNET_DB_QueryStatus qs;
struct TALER_AUDITORDB_CoinInconsistency ci = {
.operation = (char *) operation,
.auditor_amount = *auditor,
.exchange_amount = *exchange,
.profitable = profitable,
.coin_pub = coin_pub->eddsa_pub
};
qs = TALER_ARL_adb->insert_coin_inconsistency (
TALER_ARL_adb->cls,
&ci);
if (qs < 0)
{
GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs);
return qs;
}
}
if (0 != profitable)
{
target = (1 == profitable)
? &TALER_ARL_USE_AB (aggregation_total_coin_delta_plus)
: &TALER_ARL_USE_AB (aggregation_total_coin_delta_minus);
TALER_ARL_amount_add (target,
target,
&delta);
}
return GNUNET_DB_STATUS_SUCCESS_ONE_RESULT;
}
/**
* Report a (serious) inconsistency in the exchange's database.
*
* @param table affected table
* @param rowid affected row, 0 if row is missing
* @param diagnostic message explaining the problem
* @return transaction status
*/
static enum GNUNET_DB_QueryStatus
report_row_inconsistency (const char *table,
uint64_t rowid,
const char *diagnostic)
{
enum GNUNET_DB_QueryStatus qs;
struct TALER_AUDITORDB_RowInconsistency ri = {
.diagnostic = (char *) diagnostic,
.row_table = (char *) table,
.row_id = rowid
};
qs = TALER_ARL_adb->insert_row_inconsistency (
TALER_ARL_adb->cls,
&ri);
if (qs < 0)
{
GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs);
return qs;
}
return GNUNET_DB_STATUS_SUCCESS_ONE_RESULT;
}
/* *********************** 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_Timestamp start_date;
/**
* When does the fee stop being in effect (exclusive).
*/
struct GNUNET_TIME_Timestamp end_date;
/**
* How high are the wire fees.
*/
struct TALER_WireFeeSet fees;
};
/**
* 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;
/**
* Target account details of the receiver.
*/
const char *payto_uri;
/**
* Execution time of the wire transfer.
*/
struct GNUNET_TIME_Timestamp 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 database transaction status
*/
static enum GNUNET_DB_QueryStatus
check_transaction_history_for_deposit (
const struct TALER_CoinSpendPublicKeyP *coin_pub,
const struct TALER_PrivateContractHashP *h_contract_terms,
const struct TALER_MerchantPublicKeyP *merchant_pub,
const struct TALER_EXCHANGEDB_DenominationKeyInformation *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 *deposited = NULL;
struct TALER_Amount merchant_loss;
const struct TALER_Amount *deposit_fee;
enum GNUNET_DB_QueryStatus qs;
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). */
deposit_fee = NULL;
for (const struct TALER_EXCHANGEDB_TransactionList *tl = tl_head;
NULL != tl;
tl = tl->next)
{
const struct TALER_Amount *fee_claimed;
switch (tl->type)
{
case TALER_EXCHANGEDB_TT_DEPOSIT:
/* check wire and h_wire are consistent */
if (NULL != deposited)
{
struct TALER_AUDITORDB_RowInconsistency ri = {
.row_id = tl->serial_id,
.diagnostic =
"multiple deposits of the same coin into the same contract detected",
.row_table = "deposits"
};
qs = TALER_ARL_adb->insert_row_inconsistency (
TALER_ARL_adb->cls,
&ri);
if (qs < 0)
{
GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs);
// FIXME: error handling
}
}
deposited = &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,
deposited);
/* 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,
deposited,
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 */
if (0 !=
TALER_amount_cmp (&issue->fees.deposit,
fee_claimed))
{
/* Disagreement in fee structure between auditor and exchange DB! */
qs = report_amount_arithmetic_inconsistency ("deposit fee",
0,
fee_claimed,
&issue->fees.deposit,
1);
if (0 > qs)
return qs;
}
break;
case TALER_EXCHANGEDB_TT_MELT:
{
const struct TALER_Amount *amount_with_fee;
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 */
if (0 !=
TALER_amount_cmp (&issue->fees.refresh,
fee_claimed))
{
/* Disagreement in fee structure between exchange and auditor */
qs = report_amount_arithmetic_inconsistency ("melt fee",
0,
fee_claimed,
&issue->fees.refresh,
1);
if (0 > qs)
return qs;
}
break;
}
case TALER_EXCHANGEDB_TT_REFUND:
{
const struct TALER_Amount *amount_with_fee;
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);
}
/* Check that the fees given in the transaction list and in dki match */
if (0 !=
TALER_amount_cmp (&issue->fees.refund,
fee_claimed))
{
/* Disagreement in fee structure between exchange and auditor! */
qs = report_amount_arithmetic_inconsistency ("refund fee",
0,
fee_claimed,
&issue->fees.refund,
1);
if (0 > qs)
return qs;
}
break;
}
case TALER_EXCHANGEDB_TT_OLD_COIN_RECOUP:
{
const struct TALER_Amount *amount_with_fee;
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:
{
const struct TALER_Amount *amount_with_fee;
/* 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:
{
const struct TALER_Amount *amount_with_fee;
/* 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;
}
case TALER_EXCHANGEDB_TT_PURSE_DEPOSIT:
{
const struct TALER_Amount *amount_with_fee;
amount_with_fee = &tl->details.purse_deposit->amount;
if (! tl->details.purse_deposit->refunded)
TALER_ARL_amount_add (&expenditures,
&expenditures,
amount_with_fee);
break;
}
case TALER_EXCHANGEDB_TT_PURSE_REFUND:
{
const struct TALER_Amount *amount_with_fee;
amount_with_fee = &tl->details.purse_refund->refund_amount;
fee_claimed = &tl->details.purse_refund->refund_fee;
TALER_ARL_amount_add (&refunds,
&refunds,
amount_with_fee);
TALER_ARL_amount_add (&expenditures,
&expenditures,
fee_claimed);
/* Check that the fees given in the transaction list and in dki match */
if (0 !=
TALER_amount_cmp (&issue->fees.refund,
fee_claimed))
{
/* Disagreement in fee structure between exchange and auditor! */
qs = report_amount_arithmetic_inconsistency ("refund fee",
0,
fee_claimed,
&issue->fees.refund,
1);
if (0 > qs)
return qs;
}
break;
}
case TALER_EXCHANGEDB_TT_RESERVE_OPEN:
{
const struct TALER_Amount *amount_with_fee;
amount_with_fee = &tl->details.reserve_open->coin_contribution;
TALER_ARL_amount_add (&expenditures,
&expenditures,
amount_with_fee);
break;
}
} /* switch (tl->type) */
} /* 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 ((NULL != deposited) &&
(NULL != deposit_fee) &&
(0 == TALER_amount_cmp (&refunds,
deposited)))
{
/* We had a /deposit operation AND /refund operations adding up to the
total deposited value including deposit fee. Thus, we should not
subtract the /deposit fee from the merchant gain (as it was also
refunded). */
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! */
qs = report_coin_arithmetic_inconsistency ("refund (merchant)",
coin_pub,
merchant_gain,
&merchant_loss,
1);
if (0 > qs)
return qs;
/* 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! */
qs = report_coin_arithmetic_inconsistency ("refund (balance)",
coin_pub,
&expenditures,
&refunds,
1);
if (0 > qs)
return qs;
}
else
{
/* Now check that 'spent' is less or equal than the total coin value */
if (1 == TALER_amount_cmp (&spent,
&issue->value))
{
/* spent > value */
qs = report_coin_arithmetic_inconsistency ("spend",
coin_pub,
&spent,
&issue->value,
-1);
if (0 > qs)
return qs;
}
}
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->hash));
return GNUNET_DB_STATUS_SUCCESS_ONE_RESULT;
}
/**
* 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 account_pay_uri where did we transfer the funds?
* @param h_payto hash over @a account_payto_uri as it is in the DB
* @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 char *account_pay_uri,
const struct TALER_PaytoHashP *h_payto,
struct GNUNET_TIME_Timestamp exec_time,
const struct TALER_PrivateContractHashP *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_EXCHANGEDB_DenominationKeyInformation *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 TALER_PaytoHashP hpt;
uint64_t etag_out;
if (0 > wcc->qs)
return;
TALER_payto_hash (account_pay_uri,
&hpt);
if (0 !=
GNUNET_memcmp (&hpt,
h_payto))
{
qs = report_row_inconsistency ("wire_targets",
rowid,
"h-payto does not match payto URI");
if (0 > qs)
{
wcc->qs = qs;
return;
}
}
/* Obtain coin's transaction history */
/* TODO: could use 'start' mechanism to only fetch transactions
we did not yet process, instead of going over them
again and again.*/
{
struct TALER_Amount balance;
struct TALER_DenominationHashP h_denom_pub;
qs = TALER_ARL_edb->get_coin_transactions (TALER_ARL_edb->cls,
false,
coin_pub,
0,
0,
&etag_out,
&balance,
&h_denom_pub,
&tl);
}
if (0 > qs)
{
wcc->qs = qs;
TALER_ARL_edb->free_coin_transaction_list (TALER_ARL_edb->cls,
tl);
return;
}
if (NULL == tl)
{
qs = report_row_inconsistency ("aggregation",
rowid,
"no transaction history for coin claimed in aggregation");
if (0 > qs)
wcc->qs = qs;
return;
}
qs = TALER_ARL_edb->get_known_coin (TALER_ARL_edb->cls,
coin_pub,
&coin);
if (0 > qs)
{
GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs);
wcc->qs = qs;
return;
}
if (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS == qs)
{
/* this should be a foreign key violation at this point! */
qs = report_row_inconsistency ("aggregation",
rowid,
"could not get coin details for coin claimed in aggregation");
if (0 > qs)
wcc->qs = qs;
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 (0 > qs)
{
wcc->qs = qs;
TALER_denom_sig_free (&coin.denom_sig);
TALER_ARL_edb->free_coin_transaction_list (TALER_ARL_edb->cls,
tl);
return;
}
if (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS == qs)
{
TALER_denom_sig_free (&coin.denom_sig);
TALER_ARL_edb->free_coin_transaction_list (TALER_ARL_edb->cls,
tl);
qs = report_row_inconsistency ("aggregation",
rowid,
"could not find denomination key for coin claimed in aggregation");
if (0 > qs)
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))
{
struct TALER_AUDITORDB_BadSigLosses bsl = {
.row_id = rowid,
.operation = "wire",
.loss = *coin_value,
.operation_specific_pub = coin.coin_pub.eddsa_pub
};
qs = TALER_ARL_adb->insert_bad_sig_losses (
TALER_ARL_adb->cls,
&bsl);
if (qs < 0)
{
GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs);
wcc->qs = qs;
TALER_denom_sig_free (&coin.denom_sig);
TALER_ARL_edb->free_coin_transaction_list (TALER_ARL_edb->cls,
tl);
return;
}
TALER_ARL_amount_add (&TALER_ARL_USE_AB (aggregation_total_bad_sig_loss),
&TALER_ARL_USE_AB (aggregation_total_bad_sig_loss),
coin_value);
qs = report_row_inconsistency ("deposit",
rowid,
"coin denomination signature invalid");
if (0 > qs)
{
wcc->qs = qs;
TALER_denom_sig_free (&coin.denom_sig);
TALER_ARL_edb->free_coin_transaction_list (TALER_ARL_edb->cls,
tl);
return;
}
}
TALER_denom_sig_free (&coin.denom_sig);
GNUNET_assert (NULL != issue); /* mostly to help static analysis */
/* Check transaction history to see if it supports aggregate
valuation */
qs = check_transaction_history_for_deposit (
coin_pub,
h_contract_terms,
merchant_pub,
issue,
tl,
&computed_value,
&total_deposit_without_refunds);
if (0 > qs)
{
TALER_ARL_edb->free_coin_transaction_list (TALER_ARL_edb->cls,
tl);
wcc->qs = qs;
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))
{
qs = report_amount_arithmetic_inconsistency (
"aggregation (fee structure)",
rowid,
coin_value,
deposit_fee,
-1);
if (0 > qs)
{
wcc->qs = qs;
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));
qs = report_amount_arithmetic_inconsistency (
"aggregation (contribution)",
rowid,
&coin_value_without_fee,
&total_deposit_without_refunds,
-1);
if (0 > qs)
{
wcc->qs = qs;
return;
}
}
}
/* Check other details of wire transfer match */
if (0 != strcmp (account_pay_uri,
wcc->payto_uri))
{
qs = report_row_inconsistency ("aggregation",
rowid,
"target of outgoing wire transfer do not match hash of wire from deposit");
if (0 > qs)
{
wcc->qs = qs;
return;
}
}
if (GNUNET_TIME_timestamp_cmp (exec_time,
!=,
wcc->date))
{
/* This should be impossible from database constraints */
GNUNET_break (0);
qs = report_row_inconsistency ("aggregation",
rowid,
"date given in aggregate does not match wire transfer date");
if (0 > qs)
{
wcc->qs = qs;
return;
}
}
/* 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_Timestamp timestamp)
{
struct WireFeeInfo *wfi;
struct WireFeeInfo *pos;
struct TALER_MasterSignatureP master_sig;
enum GNUNET_DB_QueryStatus qs;
/* Check if fee is already loaded in cache */
for (pos = ac->fee_head; NULL != pos; pos = pos->next)
{
if (GNUNET_TIME_timestamp_cmp (pos->start_date,
<=,
timestamp) &&
GNUNET_TIME_timestamp_cmp (pos->end_date,
>,
timestamp))
return &pos->fees.wire;
if (GNUNET_TIME_timestamp_cmp (pos->start_date,
>,
timestamp))
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,
method,
timestamp,
&wfi->start_date,
&wfi->end_date,
&wfi->fees,
&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->fees,
&TALER_ARL_master_pub,
&master_sig))
{
report_row_inconsistency ("wire-fee",
timestamp.abs_time.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->fees.wire),
GNUNET_TIME_timestamp2s (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) &&
GNUNET_TIME_timestamp_cmp (wfi->prev->end_date,
>,
wfi->start_date))
{
struct TALER_AUDITORDB_FeeTimeInconsistency ftib = {
.diagnostic = "start date before previous end date",
.time = wfi->start_date.abs_time,
.type = (char *) method
};
qs = TALER_ARL_adb->insert_fee_time_inconsistency (
TALER_ARL_adb->cls,
&ftib);
if (qs < 0)
{
GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs);
ac->qs = qs;
return NULL;
}
}
if ((NULL != wfi->next) &&
GNUNET_TIME_timestamp_cmp (wfi->next->start_date,
>=,
wfi->end_date))
{
struct TALER_AUDITORDB_FeeTimeInconsistency ftia = {
.diagnostic = "end date date after next start date",
.time = wfi->end_date.abs_time,
.type = (char *) method
};
qs = TALER_ARL_adb->insert_fee_time_inconsistency (
TALER_ARL_adb->cls,
&ftia);
if (qs < 0)
{
GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs);
ac->qs = qs;
return NULL;
}
}
return &wfi->fees.wire;
}
/**
* 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 payto_uri bank account details of the receiver
* @param amount amount that was wired
* @return #GNUNET_OK to continue, #GNUNET_SYSERR to stop iteration
*/
static enum GNUNET_GenericReturnValue
check_wire_out_cb (void *cls,
uint64_t rowid,
struct GNUNET_TIME_Timestamp date,
const struct TALER_WireTransferIdentifierRawP *wtid,
const char *payto_uri,
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 >=
TALER_ARL_USE_PP (aggregation_last_wire_out_serial_id));
TALER_ARL_USE_PP (aggregation_last_wire_out_serial_id) = rowid + 1;
GNUNET_log (GNUNET_ERROR_TYPE_INFO,
"Checking wire transfer %s over %s performed on %s\n",
TALER_B2S (wtid),
TALER_amount2s (amount),
GNUNET_TIME_timestamp2s (date));
if (NULL == (method = TALER_payto_get_method (payto_uri)))
{
qs = report_row_inconsistency ("wire_out",
rowid,
"specified wire address lacks method");
if (0 > qs)
ac->qs = qs;
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));
wcc.payto_uri = payto_uri;
qs = TALER_ARL_edb->lookup_wire_transfer (TALER_ARL_edb->cls,
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,
"Inconsistency 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 (0 > ac->qs)
{
GNUNET_free (method);
return GNUNET_SYSERR;
}
if (NULL == wire_fee)
{
report_row_inconsistency ("wire-fee",
date.abs_time.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))
{
qs = 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 */
if (0 > qs)
{
ac->qs = qs;
GNUNET_free (method);
return GNUNET_SYSERR;
}
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 (&TALER_ARL_USE_AB (aggregation_total_wire_fee_revenue),
&TALER_ARL_USE_AB (aggregation_total_wire_fee_revenue),
&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 (&TALER_ARL_USE_AB (
aggregation_total_wire_out_delta_plus),
&TALER_ARL_USE_AB (
aggregation_total_wire_out_delta_plus),
&delta);
}
else
{
/* amount < final_amount */
TALER_ARL_amount_subtract (&delta,
&final_amount,
amount);
TALER_ARL_amount_add (&TALER_ARL_USE_AB (
aggregation_total_wire_out_delta_minus),
&TALER_ARL_USE_AB (
aggregation_total_wire_out_delta_minus),
&delta);
}
{
struct TALER_AUDITORDB_WireOutInconsistency woi = {
.destination_account = (char *) payto_uri,
.diagnostic = "aggregated amount does not match expectations",
.wire_out_row_id = rowid,
.expected = final_amount,
.claimed = *amount
};
qs = TALER_ARL_adb->insert_wire_out_inconsistency (
TALER_ARL_adb->cls,
&woi);
if (qs < 0)
{
GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs);
ac->qs = qs;
return GNUNET_SYSERR;
}
}
return GNUNET_OK;
}
GNUNET_log (GNUNET_ERROR_TYPE_INFO,
"Aggregation unit %s is OK\n",
TALER_B2S (wtid));
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 qs;
(void) cls;
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
"Analyzing aggregations\n");
qs = TALER_ARL_adb->get_auditor_progress (
TALER_ARL_adb->cls,
TALER_ARL_GET_PP (aggregation_last_wire_out_serial_id),
NULL);
if (0 > qs)
{
GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs);
return qs;
}
if (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS == qs)
{
GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
"First analysis using this auditor, starting audit from scratch\n");
}
else
{
GNUNET_log (GNUNET_ERROR_TYPE_INFO,
"Resuming aggregation audit at %llu\n",
(unsigned long long) TALER_ARL_USE_PP (
aggregation_last_wire_out_serial_id));
}
memset (&ac,
0,
sizeof (ac));
qs = TALER_ARL_adb->get_balance (
TALER_ARL_adb->cls,
TALER_ARL_GET_AB (aggregation_total_wire_fee_revenue),
TALER_ARL_GET_AB (aggregation_total_arithmetic_delta_plus),
TALER_ARL_GET_AB (aggregation_total_arithmetic_delta_minus),
TALER_ARL_GET_AB (aggregation_total_bad_sig_loss),
TALER_ARL_GET_AB (aggregation_total_wire_out_delta_plus),
TALER_ARL_GET_AB (aggregation_total_wire_out_delta_minus),
TALER_ARL_GET_AB (aggregation_total_coin_delta_plus),
NULL);
if (0 > qs)
{
GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs);
return qs;
}
ac.qs = GNUNET_DB_STATUS_SUCCESS_ONE_RESULT;
qs = TALER_ARL_edb->select_wire_out_above_serial_id (
TALER_ARL_edb->cls,
TALER_ARL_USE_PP (aggregation_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;
}
qs = TALER_ARL_adb->insert_balance (
TALER_ARL_adb->cls,
TALER_ARL_SET_AB (aggregation_total_wire_fee_revenue),
TALER_ARL_SET_AB (aggregation_total_arithmetic_delta_plus),
TALER_ARL_SET_AB (aggregation_total_arithmetic_delta_minus),
TALER_ARL_SET_AB (aggregation_total_bad_sig_loss),
TALER_ARL_SET_AB (aggregation_total_wire_out_delta_plus),
TALER_ARL_SET_AB (aggregation_total_wire_out_delta_minus),
TALER_ARL_SET_AB (aggregation_total_coin_delta_plus),
NULL);
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;
}
qs = TALER_ARL_adb->update_balance (
TALER_ARL_adb->cls,
TALER_ARL_SET_AB (aggregation_total_wire_fee_revenue),
TALER_ARL_SET_AB (aggregation_total_arithmetic_delta_plus),
TALER_ARL_SET_AB (aggregation_total_arithmetic_delta_minus),
TALER_ARL_SET_AB (aggregation_total_bad_sig_loss),
TALER_ARL_SET_AB (aggregation_total_wire_out_delta_plus),
TALER_ARL_SET_AB (aggregation_total_wire_out_delta_minus),
TALER_ARL_SET_AB (aggregation_total_coin_delta_plus),
NULL);
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;
}
qs = TALER_ARL_adb->insert_auditor_progress (
TALER_ARL_adb->cls,
TALER_ARL_SET_PP (aggregation_last_wire_out_serial_id),
NULL);
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;
}
qs = TALER_ARL_adb->update_auditor_progress (
TALER_ARL_adb->cls,
TALER_ARL_SET_PP (aggregation_last_wire_out_serial_id),
NULL);
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) TALER_ARL_USE_PP (
aggregation_last_wire_out_serial_id));
return GNUNET_DB_STATUS_SUCCESS_ONE_RESULT;
}
/**
* Function called on events received from Postgres.
*
* @param cls closure, NULL
* @param extra additional event data provided
* @param extra_size number of bytes in @a extra
*/
static void
db_notify (void *cls,
const void *extra,
size_t extra_size)
{
(void) cls;
(void) extra;
(void) extra_size;
GNUNET_log (GNUNET_ERROR_TYPE_INFO,
"Received notification to wake aggregation helper\n");
if (GNUNET_OK !=
TALER_ARL_setup_sessions_and_run (&analyze_aggregations,
NULL))
{
GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
"Audit failed\n");
GNUNET_SCHEDULER_shutdown ();
global_ret = EXIT_FAILURE;
return;
}
}
/**
* Function called on shutdown.
*/
static void
do_shutdown (void *cls)
{
(void) cls;
if (NULL != eh)
{
TALER_ARL_adb->event_listen_cancel (eh);
eh = NULL;
}
TALER_ARL_done ();
}
/**
* 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)
{
(void) cls;
(void) args;
(void) cfgfile;
cfg = c;
GNUNET_SCHEDULER_add_shutdown (&do_shutdown,
NULL);
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
"Launching aggregation auditor\n");
if (GNUNET_OK !=
TALER_ARL_init (c))
{
global_ret = EXIT_FAILURE;
return;
}
if (test_mode != 1)
{
struct GNUNET_DB_EventHeaderP es = {
.size = htons (sizeof (es)),
.type = htons (TALER_DBEVENT_EXCHANGE_AUDITOR_WAKE_HELPER_AGGREGATION)
};
GNUNET_log (GNUNET_ERROR_TYPE_INFO,
"Running helper indefinitely\n");
eh = TALER_ARL_adb->event_listen (TALER_ARL_adb->cls,
&es,
GNUNET_TIME_UNIT_FOREVER_REL,
&db_notify,
NULL);
}
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
"Starting audit\n");
if (GNUNET_OK !=
TALER_ARL_setup_sessions_and_run (&analyze_aggregations,
NULL))
{
GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
"Audit failed\n");
GNUNET_SCHEDULER_shutdown ();
global_ret = EXIT_FAILURE;
return;
}
}
/**
* 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_flag ('t',
"test",
"run in test mode and exit when idle",
&test_mode),
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 EXIT_INVALIDARGUMENT;
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 EXIT_INVALIDARGUMENT;
if (GNUNET_NO == ret)
return EXIT_SUCCESS;
return global_ret;
}
/* end of taler-helper-auditor-aggregation.c */
|