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
|
/*
This file is part of TALER
(C) 2016-2023 Taler Systems SA
TALER is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 3,
or (at your option) any later version.
TALER is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public
License along with TALER; see the file COPYING. If not,
see <http://www.gnu.org/licenses/>
*/
/**
* @file bank-lib/fakebank_twg_history.c
* @brief routines to return account histories for the Taler Wire Gateway API
* @author Christian Grothoff <christian@grothoff.org>
*/
#include "platform.h"
#include <pthread.h>
#include "taler_fakebank_lib.h"
#include "taler_bank_service.h"
#include "taler_mhd_lib.h"
#include <gnunet/gnunet_mhd_compat.h>
#include "fakebank.h"
#include "fakebank_common_lookup.h"
#include "fakebank_common_lp.h"
#include "fakebank_common_parser.h"
/**
* Function called to clean up a history context.
*
* @param cls a `struct HistoryContext *`
*/
static void
history_cleanup (void *cls)
{
struct HistoryContext *hc = cls;
json_decref (hc->history);
GNUNET_free (hc);
}
MHD_RESULT
TALER_FAKEBANK_twg_get_debit_history_ (
struct TALER_FAKEBANK_Handle *h,
struct MHD_Connection *connection,
const char *account,
void **con_cls)
{
struct ConnectionContext *cc = *con_cls;
struct HistoryContext *hc;
struct Transaction *pos;
enum GNUNET_GenericReturnValue ret;
bool in_shutdown;
const char *acc_payto_uri;
if (NULL == cc)
{
cc = GNUNET_new (struct ConnectionContext);
cc->ctx_cleaner = &history_cleanup;
*con_cls = cc;
hc = GNUNET_new (struct HistoryContext);
cc->ctx = hc;
GNUNET_log (GNUNET_ERROR_TYPE_INFO,
"Handling /history/outgoing connection %p\n",
connection);
if (GNUNET_OK !=
(ret = TALER_FAKEBANK_common_parse_history_args (h,
connection,
&hc->ha)))
{
GNUNET_break_op (0);
return (GNUNET_SYSERR == ret) ? MHD_NO : MHD_YES;
}
GNUNET_assert (0 ==
pthread_mutex_lock (&h->big_lock));
if (UINT64_MAX == hc->ha.start_idx)
hc->ha.start_idx = h->serial_counter;
hc->acc = TALER_FAKEBANK_lookup_account_ (h,
account,
NULL);
if (NULL == hc->acc)
{
GNUNET_assert (0 ==
pthread_mutex_unlock (&h->big_lock));
return TALER_MHD_reply_with_error (connection,
MHD_HTTP_NOT_FOUND,
TALER_EC_BANK_UNKNOWN_ACCOUNT,
account);
}
hc->history = json_array ();
if (NULL == hc->history)
{
GNUNET_break (0);
GNUNET_assert (0 ==
pthread_mutex_unlock (&h->big_lock));
return MHD_NO;
}
hc->timeout = GNUNET_TIME_relative_to_absolute (hc->ha.lp_timeout);
}
else
{
hc = cc->ctx;
GNUNET_assert (0 ==
pthread_mutex_lock (&h->big_lock));
}
if (! hc->ha.have_start)
{
pos = (0 > hc->ha.delta)
? hc->acc->out_tail
: hc->acc->out_head;
}
else
{
struct Transaction *t = h->transactions[hc->ha.start_idx % h->ram_limit];
bool overflow;
uint64_t dir;
bool skip = true;
dir = (0 > hc->ha.delta) ? (h->ram_limit - 1) : 1;
overflow = (t->row_id != hc->ha.start_idx);
/* If account does not match, linear scan for
first matching account. */
while ( (! overflow) &&
(NULL != t) &&
(t->debit_account != hc->acc) )
{
skip = false;
t = h->transactions[(t->row_id + dir) % h->ram_limit];
if ( (NULL != t) &&
(t->row_id == hc->ha.start_idx) )
overflow = true; /* full circle, give up! */
}
if ( (NULL == t) ||
overflow)
{
/* FIXME: these conditions are unclear to me. */
if ( (GNUNET_TIME_relative_is_zero (hc->ha.lp_timeout)) &&
(0 < hc->ha.delta))
{
acc_payto_uri = hc->acc->payto_uri;
in_shutdown = h->in_shutdown;
GNUNET_assert (0 ==
pthread_mutex_unlock (&h->big_lock));
if (overflow)
{
return TALER_MHD_reply_with_ec (
connection,
TALER_EC_BANK_ANCIENT_TRANSACTION_GONE,
NULL);
}
goto finish;
}
if (h->in_shutdown)
{
acc_payto_uri = hc->acc->payto_uri;
in_shutdown = h->in_shutdown;
GNUNET_assert (0 ==
pthread_mutex_unlock (&h->big_lock));
goto finish;
}
TALER_FAKEBANK_start_lp_ (h,
connection,
hc->acc,
GNUNET_TIME_absolute_get_remaining (
hc->timeout),
LP_DEBIT,
NULL);
GNUNET_assert (0 ==
pthread_mutex_unlock (&h->big_lock));
return MHD_YES;
}
if (t->debit_account != hc->acc)
{
GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
"Invalid start specified, transaction %llu not with account %s!\n",
(unsigned long long) hc->ha.start_idx,
account);
GNUNET_assert (0 ==
pthread_mutex_unlock (&h->big_lock));
return MHD_NO;
}
if (skip)
{
/* range is exclusive, skip the matching entry */
if (0 > hc->ha.delta)
pos = t->prev_out;
else
pos = t->next_out;
}
else
{
pos = t;
}
}
if (NULL != pos)
GNUNET_log (GNUNET_ERROR_TYPE_INFO,
"Returning %lld debit transactions starting (inclusive) from %llu\n",
(long long) hc->ha.delta,
(unsigned long long) pos->row_id);
while ( (0 != hc->ha.delta) &&
(NULL != pos) )
{
json_t *trans;
char *credit_payto;
if (T_DEBIT != pos->type)
{
GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
"Unexpected CREDIT transaction #%llu for account `%s'\n",
(unsigned long long) pos->row_id,
account);
if (0 > hc->ha.delta)
pos = pos->prev_in;
if (0 < hc->ha.delta)
pos = pos->next_in;
continue;
}
GNUNET_asprintf (&credit_payto,
"payto://x-taler-bank/localhost/%s?receiver-name=%s",
pos->credit_account->account_name,
pos->credit_account->receiver_name);
trans = GNUNET_JSON_PACK (
GNUNET_JSON_pack_uint64 ("row_id",
pos->row_id),
GNUNET_JSON_pack_timestamp ("date",
pos->date),
TALER_JSON_pack_amount ("amount",
&pos->amount),
GNUNET_JSON_pack_string ("credit_account",
credit_payto),
GNUNET_JSON_pack_string ("exchange_base_url",
pos->subject.debit.exchange_base_url),
GNUNET_JSON_pack_data_auto ("wtid",
&pos->subject.debit.wtid));
GNUNET_assert (NULL != trans);
GNUNET_free (credit_payto);
GNUNET_assert (0 ==
json_array_append_new (hc->history,
trans));
if (hc->ha.delta > 0)
hc->ha.delta--;
else
hc->ha.delta++;
if (0 > hc->ha.delta)
pos = pos->prev_out;
if (0 < hc->ha.delta)
pos = pos->next_out;
}
if ( (0 == json_array_size (hc->history)) &&
(! h->in_shutdown) &&
(GNUNET_TIME_absolute_is_future (hc->timeout)) &&
(0 < hc->ha.delta))
{
TALER_FAKEBANK_start_lp_ (h,
connection,
hc->acc,
GNUNET_TIME_absolute_get_remaining (hc->timeout),
LP_DEBIT,
NULL);
GNUNET_assert (0 ==
pthread_mutex_unlock (&h->big_lock));
return MHD_YES;
}
in_shutdown = h->in_shutdown;
acc_payto_uri = hc->acc->payto_uri;
GNUNET_assert (0 ==
pthread_mutex_unlock (&h->big_lock));
finish:
if (0 == json_array_size (hc->history))
{
GNUNET_break (in_shutdown ||
(! GNUNET_TIME_absolute_is_future (hc->timeout)));
return TALER_MHD_reply_static (connection,
MHD_HTTP_NO_CONTENT,
NULL,
NULL,
0);
}
{
json_t *h = hc->history;
hc->history = NULL;
return TALER_MHD_REPLY_JSON_PACK (
connection,
MHD_HTTP_OK,
GNUNET_JSON_pack_string (
"debit_account",
acc_payto_uri),
GNUNET_JSON_pack_array_steal (
"outgoing_transactions",
h));
}
}
MHD_RESULT
TALER_FAKEBANK_twg_get_credit_history_ (
struct TALER_FAKEBANK_Handle *h,
struct MHD_Connection *connection,
const char *account,
void **con_cls)
{
struct ConnectionContext *cc = *con_cls;
struct HistoryContext *hc;
const struct Transaction *pos;
enum GNUNET_GenericReturnValue ret;
bool in_shutdown;
const char *acc_payto_uri;
if (NULL == cc)
{
cc = GNUNET_new (struct ConnectionContext);
cc->ctx_cleaner = &history_cleanup;
*con_cls = cc;
hc = GNUNET_new (struct HistoryContext);
cc->ctx = hc;
hc->history = json_array ();
if (NULL == hc->history)
{
GNUNET_break (0);
return MHD_NO;
}
GNUNET_log (GNUNET_ERROR_TYPE_INFO,
"Handling /history/incoming connection %p\n",
connection);
if (GNUNET_OK !=
(ret = TALER_FAKEBANK_common_parse_history_args (h,
connection,
&hc->ha)))
{
GNUNET_break_op (0);
return (GNUNET_SYSERR == ret) ? MHD_NO : MHD_YES;
}
hc->timeout = GNUNET_TIME_relative_to_absolute (hc->ha.lp_timeout);
GNUNET_assert (0 ==
pthread_mutex_lock (&h->big_lock));
if (UINT64_MAX == hc->ha.start_idx)
hc->ha.start_idx = h->serial_counter;
hc->acc = TALER_FAKEBANK_lookup_account_ (h,
account,
NULL);
if (NULL == hc->acc)
{
GNUNET_assert (0 ==
pthread_mutex_unlock (&h->big_lock));
return TALER_MHD_reply_with_error (connection,
MHD_HTTP_NOT_FOUND,
TALER_EC_BANK_UNKNOWN_ACCOUNT,
account);
}
}
else
{
hc = cc->ctx;
GNUNET_assert (0 ==
pthread_mutex_lock (&h->big_lock));
}
if (! hc->ha.have_start)
{
pos = (0 > hc->ha.delta)
? hc->acc->in_tail
: hc->acc->in_head;
}
else
{
struct Transaction *t = h->transactions[hc->ha.start_idx % h->ram_limit];
bool overflow;
uint64_t dir;
bool skip = true;
overflow = ( (NULL != t) && (t->row_id != hc->ha.start_idx) );
dir = (0 > hc->ha.delta) ? (h->ram_limit - 1) : 1;
/* If account does not match, linear scan for
first matching account. */
while ( (! overflow) &&
(NULL != t) &&
(t->credit_account != hc->acc) )
{
skip = false;
t = h->transactions[(t->row_id + dir) % h->ram_limit];
if ( (NULL != t) &&
(t->row_id == hc->ha.start_idx) )
overflow = true; /* full circle, give up! */
}
if ( (NULL == t) ||
overflow)
{
in_shutdown = h->in_shutdown;
/* FIXME: these conditions are unclear to me. */
if (GNUNET_TIME_relative_is_zero (hc->ha.lp_timeout) &&
(0 < hc->ha.delta))
{
acc_payto_uri = hc->acc->payto_uri;
GNUNET_assert (0 ==
pthread_mutex_unlock (&h->big_lock));
if (overflow)
return TALER_MHD_reply_with_ec (
connection,
TALER_EC_BANK_ANCIENT_TRANSACTION_GONE,
NULL);
goto finish;
}
if (in_shutdown)
{
acc_payto_uri = hc->acc->payto_uri;
GNUNET_assert (0 ==
pthread_mutex_unlock (&h->big_lock));
goto finish;
}
TALER_FAKEBANK_start_lp_ (h,
connection,
hc->acc,
GNUNET_TIME_absolute_get_remaining (
hc->timeout),
LP_CREDIT,
NULL);
GNUNET_assert (0 ==
pthread_mutex_unlock (&h->big_lock));
return MHD_YES;
}
if (skip)
{
/* range from application is exclusive, skip the
matching entry */
if (0 > hc->ha.delta)
pos = t->prev_in;
else
pos = t->next_in;
}
else
{
pos = t;
}
}
if (NULL != pos)
GNUNET_log (GNUNET_ERROR_TYPE_INFO,
"Returning %lld credit transactions starting (inclusive) from %llu\n",
(long long) hc->ha.delta,
(unsigned long long) pos->row_id);
while ( (0 != hc->ha.delta) &&
(NULL != pos) )
{
json_t *trans;
if (T_CREDIT != pos->type)
{
GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
"Unexpected DEBIT transaction #%llu for account `%s'\n",
(unsigned long long) pos->row_id,
account);
if (0 > hc->ha.delta)
pos = pos->prev_in;
if (0 < hc->ha.delta)
pos = pos->next_in;
continue;
}
trans = GNUNET_JSON_PACK (
GNUNET_JSON_pack_uint64 ("row_id",
pos->row_id),
GNUNET_JSON_pack_timestamp ("date",
pos->date),
TALER_JSON_pack_amount ("amount",
&pos->amount),
GNUNET_JSON_pack_string ("debit_account",
pos->debit_account->payto_uri),
GNUNET_JSON_pack_data_auto ("reserve_pub",
&pos->subject.credit.reserve_pub));
GNUNET_assert (NULL != trans);
GNUNET_assert (0 ==
json_array_append_new (hc->history,
trans));
if (hc->ha.delta > 0)
hc->ha.delta--;
else
hc->ha.delta++;
if (0 > hc->ha.delta)
pos = pos->prev_in;
if (0 < hc->ha.delta)
pos = pos->next_in;
}
if ( (0 == json_array_size (hc->history)) &&
(! h->in_shutdown) &&
(GNUNET_TIME_absolute_is_future (hc->timeout)) &&
(0 < hc->ha.delta))
{
TALER_FAKEBANK_start_lp_ (h,
connection,
hc->acc,
GNUNET_TIME_absolute_get_remaining (hc->timeout),
LP_CREDIT,
NULL);
GNUNET_assert (0 ==
pthread_mutex_unlock (&h->big_lock));
return MHD_YES;
}
in_shutdown = h->in_shutdown;
acc_payto_uri = hc->acc->payto_uri;
GNUNET_assert (0 ==
pthread_mutex_unlock (&h->big_lock));
finish:
if (0 == json_array_size (hc->history))
{
GNUNET_break (in_shutdown ||
(! GNUNET_TIME_absolute_is_future (hc->timeout)));
return TALER_MHD_reply_static (connection,
MHD_HTTP_NO_CONTENT,
NULL,
NULL,
0);
}
{
json_t *h = hc->history;
hc->history = NULL;
return TALER_MHD_REPLY_JSON_PACK (
connection,
MHD_HTTP_OK,
GNUNET_JSON_pack_string (
"credit_account",
acc_payto_uri),
GNUNET_JSON_pack_array_steal (
"incoming_transactions",
h));
}
}
|