summaryrefslogtreecommitdiff
path: root/src/bank-lib/fakebank.h
blob: 4e81b353969784929687b930aabc57c3ef5817c3 (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
/*
  This file is part of TALER
  (C) 2016, 2017, 2018 Inria and GNUnet e.V.

  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.h
 * @brief definitions for the "/history[-range]" layer.
 * @author Marcello Stanisci <stanisci.m@gmail.com>
 */

#ifndef FAKEBANK_H
#define FAKEBANK_H
#include "platform.h"
#include <gnunet/gnunet_util_lib.h>
#include "taler_bank_service.h"

/**
 * Details about a transcation we (as the simulated bank) received.
 */
struct Transaction
{
  /**
   * We store transactions in a DLL.
   */
  struct Transaction *next;

  /**
   * We store transactions in a DLL.
   */
  struct Transaction *prev;

  /**
   * Amount to be transferred.
   */
  struct TALER_Amount amount;

  /**
   * Account to debit.
   */
  uint64_t debit_account;

  /**
   * Account to credit.
   */
  uint64_t credit_account;

  /**
   * Subject of the transfer.
   */
  char *subject;

  /**
   * Base URL of the exchange.
   */
  char *exchange_base_url;

  /**
   * When did the transaction happen?
   */
  struct GNUNET_TIME_Absolute date;

  /**
   * Number of this transaction.
   */
  uint64_t row_id;

  /**
   * Flag set if the transfer was rejected.
   */
  int rejected;

  /**
   * Has this transaction been subjected to #TALER_FAKEBANK_check()
   * and should thus no longer be counted in
   * #TALER_FAKEBANK_check_empty()?
   */
  int checked;
};


/******************************************
 * Definitions for "/history" start here. *
 ******************************************/

/**
 * Needed to implement ascending/descending ordering
 * of /history results.
 */
struct HistoryElement
{

  /**
   * History JSON element.
   */
  json_t *element;

  /**
   * Previous element.
   */
  struct HistoryElement *prev;

  /**
   * Next element.
   */
  struct HistoryElement *next;
};


/**
 * Values to implement the "/history-range" range.
 */
struct HistoryRangeDates
{
  /**
   * Oldest row in the results.
   */
  struct GNUNET_TIME_Absolute start;

  /**
   * Youngest row in the results.
   */
  struct GNUNET_TIME_Absolute end;
};

/**
 * Values to implement the "/history" range.
 */
struct HistoryRangeIds
{

  /**
   * (Exclusive) row ID for the result set.
   */
  unsigned long long start;

  /**
   * How many transactions we want in the result set.  If
   * negative/positive, @a start will be strictly younger/older
   * of any element in the result set.
   */
  long long count;
};


/**
 * This is the "base" structure for both the /history and the
 * /history-range API calls.
 */
struct HistoryArgs
{

  /**
   * Direction asked by the client: CREDIT / DEBIT / BOTH / CANCEL.
   */
  enum TALER_BANK_Direction direction;

  /**
   * Bank account number of the requesting client.
   */
  unsigned long long account_number;

  /**
   * Ordering of the results.
   */
  unsigned int ascending;

  /**
   * Overloaded type that indicates the "range" to be returned
   * in the results; this can be either a date range, or a
   * starting row id + the count.
   */
  void *range;
};



/**
 * Type for a function that decides whether or not
 * the history-building loop should iterate once again.
 * Typically called from inside the 'while' condition.
 *
 * @param ha history argument.
 * @param pos current position.
 * @return GNUNET_YES if the iteration shuold go on.
 */
typedef int (*CheckAdvance)
  (const struct HistoryArgs *ha,
   const struct Transaction *pos);

/**
 * Type for a function that steps over the next element
 * in the list of all transactions, after the current @a pos
 * _got_ included in the result.
 */
typedef struct Transaction * (*Step)
  (const struct HistoryArgs *ha,
   const struct Transaction *pos);

/*
 * Type for a function that steps over the next element
 * in the list of all transactions, after the current @a pos
 * did _not_ get included in the result.
 */
typedef struct Transaction * (*Skip)
  (const struct HistoryArgs *ha,
   const struct Transaction *pos);

/**
 * Actual history response builder.
 *
 * @param pos first (included) element in the result set.
 * @param ha history arguments.
 * @param caller_name which function is building the history.
 * @return MHD_YES / MHD_NO, after having enqueued the response
 *         object into MHD.
 */
int
TFH_build_history_response (struct MHD_Connection *connection,
                            struct Transaction *pos,
                            struct HistoryArgs *ha,
                            Skip skip,
                            Step step,
                            CheckAdvance advance);


/**
 * Parse URL history arguments, of _both_ APIs:
 * /history and /history-range.
 *
 * @param connection MHD connection.
 * @param function_name name of the caller.
 * @param ha[out] will contain the parsed values.
 * @return GNUNET_OK only if the parsing succeedes.
 */
int
TFH_parse_history_common_args (struct MHD_Connection *connection,
                               struct HistoryArgs *ha);


/**
 * Decides whether the history builder will advance or not
 * to the next element.
 *
 * @param ha history args
 * @return GNUNET_YES/NO to advance/not-advance.
 */
int
TFH_handle_history_advance (const struct HistoryArgs *ha,
                            const struct Transaction *pos);

/**
 * Iterates on the "next" element to be processed.  To
 * be used when the current element does not get inserted in
 * the result.
 *
 * @param ha history arguments.
 * @param pos current element being processed.
 * @return the next element to be processed.
 */
struct Transaction *
TFH_handle_history_skip (const struct HistoryArgs *ha,
                         const struct Transaction *pos);

/**
 * Iterates on the "next" element to be processed.  To
 * be used when the current element _gets_ inserted in the result.
 *
 * @param ha history arguments.
 * @param pos current element being processed.
 * @return the next element to be processed.
 */
struct Transaction *
TFH_handle_history_step (const struct HistoryArgs *ha,
                         const struct Transaction *pos);

/**
 * Decides whether the history builder will advance or not
 * to the next element.
 *
 * @param ha history args
 * @return GNUNET_YES/NO to advance/not-advance.
 */
int
TFH_handle_history_range_advance (const struct HistoryArgs *ha,
                                  const struct Transaction *pos);

/**
 * Iterates towards the "next" element to be processed.  To
 * be used when the current element does not get inserted in
 * the result.
 *
 * @param ha history arguments.
 * @param pos current element being processed.
 * @return the next element to be processed.
 */
struct Transaction *
TFH_handle_history_range_skip (const struct HistoryArgs *ha,
                               const struct Transaction *pos);

/**
 * Iterates on the "next" element to be processed.  To
 * be used when the current element _gets_ inserted in the result.
 * Same implementation of the "skip" counterpart, as /history-range
 * does not have the notion of count/delta.
 */
Step TFH_handle_history_range_step;
#endif