summaryrefslogtreecommitdiff
path: root/src/benchmark/taler-exchange-benchmark-new.c
blob: 0289d520541e51974521dfdc8d175853a7c6dbe2 (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
/*
  This file is part of TALER
  (C) 2014-2018 Taler Systems SA

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

  TALER is distributed in the hope that it will be useful, but
  WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  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 merchant/backend/taler-merchant-httpd.c
 * @brief HTTP serving layer intended to perform crypto-work and
 * communication with the exchange
 * @author Marcello Stanisci
 */

#include "platform.h"
#include <taler/taler_util.h>
#include <taler/taler_signatures.h>
#include <taler/taler_exchange_service.h>
#include <taler/taler_json_lib.h>
#include <gnunet/gnunet_util_lib.h>
#include <microhttpd.h>
#include <taler/taler_bank_service.h>
#include <taler/taler_fakebank_lib.h>
#include <taler/taler_testing_lib.h>
#include <taler/taler_testing_bank_lib.h>
#include <taler/taler_error_codes.h>

/* Error codes.  */
enum BenchmarkError {

  MISSING_BANK_URL,
  FAILED_TO_LAUNCH_BANK,
  BAD_CLI_ARG,
  BAD_CONFIG_FILE,
  NO_CONFIG_FILE_GIVEN
};

/**
 * Probability a coin can be refreshed.
 * This probability multiplied by the number of coins
 * generated during the average refresh must be smaller
 * than one.  The variance must be covered by the
 * #INVALID_COIN_SLACK.
 */
#define REFRESH_PROBABILITY 0.1


/**
 * The whole benchmark is a repetition of a "unit".  Each
 * unit is a array containing a withdraw+deposit operation,
 * and _possibly_ a refresh of the deposited coin.
 */
#define UNITY_SIZE 6

/* Hard-coded params.  Note, the bank is expected to
 * have the Tor user with account number 3 and password 'x'.
 *
 * This is not a problem _so far_, as the fakebank mocks logins,
 * and the Python bank makes that account by default.  */
#define USER_ACCOUNT_NO 3
#define EXCHANGE_ACCOUNT_NO 2
#define USER_LOGIN_NAME "Tor"
#define USER_LOGIN_PASS "x"
#define EXCHANGE_URL "http://example.com/"

#define FIRST_INSTRUCTION -1

#define CMD_TRANSFER_TO_EXCHANGE(label,amount) \
   TALER_TESTING_cmd_fakebank_transfer (label, amount, \
     bank_url, USER_ACCOUNT_NO, EXCHANGE_ACCOUNT_NO, \
     USER_LOGIN_NAME, USER_LOGIN_PASS, EXCHANGE_URL)

/**
 * Exit code.
 */
static unsigned int result;

/**
 * Bank process.
 */
static struct GNUNET_OS_Process *bankd;

/**
 * How many coins we want to create.
 */
static unsigned int howmany_coins;

/**
 * Log level used during the run.
 */
static char *loglev;

/**
 * Log file.
 */
static char *logfile;

/**
 * Config filename.
 */
static char *cfg_filename;

/**
 * Bank base URL.
 */
static char *bank_url;

/**
 * Currency used.
 */
static char *currency;

/**
 * Convenience macros to allocate all the currency-dependant
 * strings;  note that the argument list of the macro is ignored.
 * It is kept as a way to make the macro more auto-descriptive
 * where it is called.
 */

#define ALLOCATE_AMOUNTS(...) \
  char *AMOUNT_5; \
  char *AMOUNT_4; \
  char *AMOUNT_1; \
  \
  GNUNET_asprintf (&AMOUNT_5, \
                   "%s:5", \
                   currency); \
  GNUNET_asprintf (&AMOUNT_4, \
                   "%s:4", \
                   currency); \
  GNUNET_asprintf (&AMOUNT_1, \
                   "%s:1", \
                   currency);

/**
 * Throw a weighted coin with @a probability.
 *
 * @return #GNUNET_OK with @a probability,
 *         #GNUNET_NO with 1 - @a probability
 */
static unsigned int
eval_probability (float probability)
{
  uint64_t random;
  float random_01;

  random = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
				     UINT64_MAX);
  random_01 = (double) random / UINT64_MAX;
  return (random_01 <= probability) ? GNUNET_OK : GNUNET_NO;
}


/**
 * Actual commands collection.
 */
static void
run (void *cls,
     struct TALER_TESTING_Interpreter *is)
{
  #define APIKEY_SANDBOX "Authorization: ApiKey sandbox"
  struct TALER_Amount total_reserve_amount;
  struct TALER_Amount withdraw_fee;
  char *withdraw_fee_str;

  struct TALER_TESTING_Command all_commands
    [1 + /* Withdraw block */
     howmany_coins + /* All units */
     1 /* End CMD */];

  /* Will be freed by testing-lib.  */
  GNUNET_assert
    (GNUNET_OK == GNUNET_CURL_append_header
      (is->ctx, APIKEY_SANDBOX));

  ALLOCATE_AMOUNTS
    (AMOUNT_5,
     AMOUNT_4,
     AMOUNT_1);

  strcpy (total_reserve_amount.currency,
          currency);
  total_reserve_amount.value = 5 * howmany_coins;
  GNUNET_asprintf (&withdraw_fee_str,
                   "%s:0.1",
                   currency);
  TALER_string_to_amount (withdraw_fee_str,
                          &withdraw_fee);
  for (unsigned int i = 0; i < howmany_coins; i++)
    TALER_amount_add (&total_reserve_amount,
                      &total_reserve_amount,
                      &withdraw_fee);

  struct TALER_TESTING_Command make_reserve[] = {

    CMD_TRANSFER_TO_EXCHANGE
      ("create-reserve",
       TALER_amount_to_string (&total_reserve_amount)),

    TALER_TESTING_cmd_exec_wirewatch
      ("wirewatch",
       cfg_filename),

    TALER_TESTING_cmd_end ()

  };

  all_commands[0] = TALER_TESTING_cmd_batch ("make-reserve",
                                             make_reserve);
  for (unsigned int i = 0; i < howmany_coins; i++)
  {
    char *withdraw_label;
    char *order_enc;
    struct TALER_TESTING_Command unit[UNITY_SIZE];

    GNUNET_asprintf (&withdraw_label,
                     "withdraw-%u",
                     i);

    GNUNET_asprintf (&order_enc,
                     "{\"nonce\": %u}",
                     i);

    unit[0] = TALER_TESTING_cmd_withdraw_amount
      (withdraw_label,
       is->exchange,
       "create-reserve",
       AMOUNT_5,
       MHD_HTTP_OK);

    unit[1] = TALER_TESTING_cmd_deposit
      ("deposit",
       is->exchange,
       withdraw_label,
       0, /* Index of the one withdrawn coin in the traits.  */
       TALER_TESTING_make_wire_details
         (24,
          "https://no-aggregation"),
       order_enc,
       GNUNET_TIME_UNIT_ZERO,
       AMOUNT_1,
       MHD_HTTP_OK);

    if (eval_probability (REFRESH_PROBABILITY))
    {
      char *melt_label;
      char *reveal_label;

      GNUNET_asprintf (&melt_label,
                       "refresh-melt-%u",
                       i);

      GNUNET_asprintf (&reveal_label,
                       "refresh-reveal-%u",
                       i);

      unit[2] = TALER_TESTING_cmd_refresh_melt
        (melt_label,
         is->exchange,
         AMOUNT_4,
         withdraw_label,
         MHD_HTTP_OK);

      unit[3] = TALER_TESTING_cmd_refresh_reveal
        (reveal_label,
         is->exchange,
         melt_label,
         MHD_HTTP_OK);

      unit[4] = TALER_TESTING_cmd_refresh_link
        ("refresh-link",
         is->exchange,
         reveal_label,
         MHD_HTTP_OK);

      unit[5] = TALER_TESTING_cmd_end ();
    }
    else unit[2] = TALER_TESTING_cmd_end ();
    
    all_commands[1 + i] = TALER_TESTING_cmd_batch ("unit",
                                                   unit);
  }
  all_commands[1 + howmany_coins] = TALER_TESTING_cmd_end ();
  TALER_TESTING_run (is,
                     all_commands);
  result = 1;
}

/**
 * Send SIGTERM and wait for process termination.
 *
 * @param process process to terminate.
 */
void
terminate_process (struct GNUNET_OS_Process *process)
{
  GNUNET_OS_process_kill (process, SIGTERM);
  GNUNET_OS_process_wait (process);
  GNUNET_OS_process_destroy (process);
}

/**
 * The main function of the serve tool
 *
 * @param argc number of arguments from the command line
 * @param argv command line arguments
 * @return 0 ok, or `enum PaymentGeneratorError` on error
 */
int
main (int argc,
      char *const *argv)
{
  struct GNUNET_CONFIGURATION_Handle *cfg;

  loglev = NULL;
  GNUNET_log_setup ("taler-exchange-benchmark",
                    loglev,
                    logfile);

  struct GNUNET_GETOPT_CommandLineOption options[] = {

    GNUNET_GETOPT_option_cfgfile
      (&cfg_filename),

    GNUNET_GETOPT_option_version
      (PACKAGE_VERSION " " VCS_VERSION),

    GNUNET_GETOPT_option_help
      ("Exchange benchmark"),

    GNUNET_GETOPT_option_loglevel
      (&loglev),

    GNUNET_GETOPT_option_uint
      ('n',
       "coins-number",
       "CN",
       "How many coins we should instantiate",
       &howmany_coins),

    GNUNET_GETOPT_option_string
      ('b',
       "bank-url",
       "BU",
       "bank base url, mandatory",
       &bank_url),

    GNUNET_GETOPT_option_string
      ('l',
       "logfile",
       "LF",
       "will log to file LF",
       &logfile),

    GNUNET_GETOPT_OPTION_END
  };
  
  if (GNUNET_SYSERR == (result = GNUNET_GETOPT_run
      ("taler-exchange-benchmark",
       options,
       argc,
       argv))) 
  {
    TALER_LOG_ERROR ("Unparsable CLI options\n");
    return BAD_CLI_ARG;
  }

  if (NULL == cfg_filename)
  {
    TALER_LOG_ERROR ("-c option is mandatory\n");
    return NO_CONFIG_FILE_GIVEN;
  }

  cfg = GNUNET_CONFIGURATION_create ();
  if (GNUNET_OK != GNUNET_CONFIGURATION_load
      (cfg,
       cfg_filename))
  {
    TALER_LOG_ERROR ("Could not parse configuration\n");
    return BAD_CONFIG_FILE;
  }
  if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string
      (cfg,
       "taler",
       "currency",
       &currency))
  {
    GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
                               "taler",
                               "currency");
    GNUNET_CONFIGURATION_destroy (cfg);
    return BAD_CONFIG_FILE;
  }
  GNUNET_CONFIGURATION_destroy (cfg);

  if (NULL == bank_url)
  {
    TALER_LOG_ERROR ("Option -b is mandatory!\n");
    return MISSING_BANK_URL;
  }

  if (NULL == (bankd = TALER_TESTING_run_bank
    (cfg_filename,
     bank_url)))
  {
    TALER_LOG_ERROR ("Failed to run the bank\n");
    return FAILED_TO_LAUNCH_BANK;
  }

  result = TALER_TESTING_setup_with_exchange
    (run,
     NULL,
     cfg_filename);

  terminate_process (bankd);

  return (GNUNET_OK == result) ? 0 : result;
}