aboutsummaryrefslogtreecommitdiff
path: root/src/exchangedb/pg_batch_ensure_coin_known.c
blob: e49813064463f849bb054ec1b6c98a827c18db21 (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
/*
   This file is part of TALER
   Copyright (C) 2022 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 exchangedb/pg_batch_ensure_coin_known.c
 * @brief Implementation of the batch_ensure_coin_known function for Postgres
 * @author Christian Grothoff
 */
#include "platform.h"
#include "taler_error_codes.h"
#include "taler_dbevents.h"
#include "taler_pq_lib.h"
#include "pg_batch_ensure_coin_known.h"
#include "pg_helper.h"


static enum GNUNET_DB_QueryStatus
insert1 (struct PostgresClosure *pg,
         const struct TALER_CoinPublicInfo coin[1],
         struct TALER_EXCHANGEDB_CoinInfo result[1])
{
  enum GNUNET_DB_QueryStatus qs;
  bool is_denom_pub_hash_null = false;
  bool is_age_hash_null = false;
  PREPARE (pg,
           "batch1_known_coin",
           "SELECT"
           " existed1 AS existed"
           ",known_coin_id1 AS known_coin_id"
           ",denom_pub_hash1 AS denom_hash"
           ",age_commitment_hash1 AS h_age_commitment"
           " FROM exchange_do_batch1_known_coin"
           "  ($1, $2, $3, $4);"
           );
  struct GNUNET_PQ_QueryParam params[] = {
    GNUNET_PQ_query_param_auto_from_type (&coin[0].coin_pub),
    GNUNET_PQ_query_param_auto_from_type (&coin[0].denom_pub_hash),
    GNUNET_PQ_query_param_auto_from_type (&coin[0].h_age_commitment),
    TALER_PQ_query_param_denom_sig (&coin[0].denom_sig),
    GNUNET_PQ_query_param_end
  };
  struct GNUNET_PQ_ResultSpec rs[] = {
    GNUNET_PQ_result_spec_bool ("existed",
                                &result[0].existed),
    GNUNET_PQ_result_spec_uint64 ("known_coin_id",
                                  &result[0].known_coin_id),
    GNUNET_PQ_result_spec_allow_null (
      GNUNET_PQ_result_spec_auto_from_type ("denom_pub_hash",
                                            &result[0].denom_hash),
      &is_denom_pub_hash_null),
    GNUNET_PQ_result_spec_allow_null (
      GNUNET_PQ_result_spec_auto_from_type ("age_commitment_hash",
                                            &result[0].h_age_commitment),
      &is_age_hash_null),
    GNUNET_PQ_result_spec_end
  };

  qs = GNUNET_PQ_eval_prepared_singleton_select (pg->conn,
                                                 "batch1_known_coin",
                                                 params,
                                                 rs);
  switch (qs)
  {
  case GNUNET_DB_STATUS_HARD_ERROR:
    GNUNET_break (0);
    return qs;
  case GNUNET_DB_STATUS_SOFT_ERROR:
    return qs;
  case GNUNET_DB_STATUS_SUCCESS_NO_RESULTS:
    GNUNET_break (0); /* should be impossible */
    return GNUNET_DB_STATUS_HARD_ERROR;
  case GNUNET_DB_STATUS_SUCCESS_ONE_RESULT:
    break; /* continued below */
  }

  if ( (! is_denom_pub_hash_null) &&
       (0 != GNUNET_memcmp (&result[0].denom_hash,
                            &coin->denom_pub_hash)) )
  {
    GNUNET_break_op (0);
    result[0].denom_conflict = true;
  }

  if ( (! is_age_hash_null) &&
       (0 != GNUNET_memcmp (&result[0].h_age_commitment,
                            &coin->h_age_commitment)) )
  {
    GNUNET_break (GNUNET_is_zero (&result[0].h_age_commitment));
    GNUNET_break_op (0);
    result[0].age_conflict = true;
  }
  return qs;
}


static enum GNUNET_DB_QueryStatus
insert2 (struct PostgresClosure *pg,
         const struct TALER_CoinPublicInfo coin[2],
         struct TALER_EXCHANGEDB_CoinInfo result[2])
{
  enum GNUNET_DB_QueryStatus qs;
  bool is_denom_pub_hash_null = false;
  bool is_age_hash_null = false;
  bool is_denom_pub_hash_null2 = false;
  bool is_age_hash_null2 = false;

  PREPARE (pg,
           "batch2_known_coin",
           "SELECT"
           " existed1 AS existed"
           ",known_coin_id1 AS known_coin_id"
           ",denom_pub_hash1 AS denom_hash"
           ",age_commitment_hash1 AS h_age_commitment"
           ",existed2 AS existed2"
           ",known_coin_id2 AS known_coin_id2"
           ",denom_pub_hash2 AS denom_hash2"
           ",age_commitment_hash2 AS h_age_commitment2"
           " FROM exchange_do_batch2_known_coin"
           "  ($1, $2, $3, $4, $5, $6, $7, $8);"
           );
  struct GNUNET_PQ_QueryParam params[] = {
    GNUNET_PQ_query_param_auto_from_type (&coin[0].coin_pub),
    GNUNET_PQ_query_param_auto_from_type (&coin[0].denom_pub_hash),
    GNUNET_PQ_query_param_auto_from_type (&coin[0].h_age_commitment),
    TALER_PQ_query_param_denom_sig (&coin[0].denom_sig),

    GNUNET_PQ_query_param_auto_from_type (&coin[1].coin_pub),
    GNUNET_PQ_query_param_auto_from_type (&coin[1].denom_pub_hash),
    GNUNET_PQ_query_param_auto_from_type (&coin[1].h_age_commitment),
    TALER_PQ_query_param_denom_sig (&coin[0].denom_sig),
    GNUNET_PQ_query_param_end
  };
  struct GNUNET_PQ_ResultSpec rs[] = {
    GNUNET_PQ_result_spec_bool ("existed",
                                &result[0].existed),
    GNUNET_PQ_result_spec_uint64 ("known_coin_id",
                                  &result[0].known_coin_id),
    GNUNET_PQ_result_spec_allow_null (
      GNUNET_PQ_result_spec_auto_from_type ("denom_pub_hash",
                                            &result[0].denom_hash),
      &is_denom_pub_hash_null),
    GNUNET_PQ_result_spec_allow_null (
      GNUNET_PQ_result_spec_auto_from_type ("age_commitment_hash",
                                            &result[0].h_age_commitment),
      &is_age_hash_null),
    GNUNET_PQ_result_spec_bool ("existed2",
                                &result[1].existed),
    GNUNET_PQ_result_spec_uint64 ("known_coin_id2",
                                  &result[1].known_coin_id),
    GNUNET_PQ_result_spec_allow_null (
      GNUNET_PQ_result_spec_auto_from_type ("denom_pub_hash2",
                                            &result[1].denom_hash),
      &is_denom_pub_hash_null2),
    GNUNET_PQ_result_spec_allow_null (
      GNUNET_PQ_result_spec_auto_from_type ("age_commitment_hash2",
                                            &result[1].h_age_commitment),
      &is_age_hash_null2),
    GNUNET_PQ_result_spec_end
  };

  qs = GNUNET_PQ_eval_prepared_singleton_select (pg->conn,
                                                 "batch2_known_coin",
                                                 params,
                                                 rs);
  switch (qs)
  {
  case GNUNET_DB_STATUS_HARD_ERROR:
    GNUNET_break (0);
    return qs;
  case GNUNET_DB_STATUS_SOFT_ERROR:
    return qs;
  case GNUNET_DB_STATUS_SUCCESS_NO_RESULTS:
    GNUNET_break (0); /* should be impossible */
    return GNUNET_DB_STATUS_HARD_ERROR;
  case GNUNET_DB_STATUS_SUCCESS_ONE_RESULT:
    break; /* continued below */
  }

  if ( (! is_denom_pub_hash_null) &&
       (0 != GNUNET_memcmp (&result[0].denom_hash,
                            &coin[0].denom_pub_hash)) )
  {
    GNUNET_break_op (0);
    result[0].denom_conflict = true;
  }

  if ( (! is_age_hash_null) &&
       (0 != GNUNET_memcmp (&result[0].h_age_commitment,
                            &coin[0].h_age_commitment)) )
  {
    GNUNET_break (GNUNET_is_zero (&result[0].h_age_commitment));
    GNUNET_break_op (0);
    result[0].age_conflict = true;
  }
  if ( (! is_denom_pub_hash_null2) &&
       (0 != GNUNET_memcmp (&result[1].denom_hash,
                            &coin[1].denom_pub_hash)) )
  {
    GNUNET_break_op (0);
    result[1].denom_conflict = true;
  }

  if ( (! is_age_hash_null) &&
       (0 != GNUNET_memcmp (&result[1].h_age_commitment,
                            &coin[1].h_age_commitment)) )
  {
    GNUNET_break (GNUNET_is_zero (&result[1].h_age_commitment));
    GNUNET_break_op (0);
    result[1].age_conflict = true;
  }
  return qs;
}


static enum GNUNET_DB_QueryStatus
insert4 (struct PostgresClosure *pg,
         const struct TALER_CoinPublicInfo coin[4],
         struct TALER_EXCHANGEDB_CoinInfo result[4])
{
  enum GNUNET_DB_QueryStatus qs;
  bool is_denom_pub_hash_null = false;
  bool is_age_hash_null = false;
  bool is_denom_pub_hash_null2 = false;
  bool is_age_hash_null2 = false;
  bool is_denom_pub_hash_null3 = false;
  bool is_age_hash_null3 = false;
  bool is_denom_pub_hash_null4 = false;
  bool is_age_hash_null4 = false;
  PREPARE (pg,
           "batch4_known_coin",
           "SELECT"
           " existed1 AS existed"
           ",known_coin_id1 AS known_coin_id"
           ",denom_pub_hash1 AS denom_hash"
           ",age_commitment_hash1 AS h_age_commitment"
           ",existed2 AS existed2"
           ",known_coin_id2 AS known_coin_id2"
           ",denom_pub_hash2 AS denom_hash2"
           ",age_commitment_hash2 AS h_age_commitment2"
           ",existed3 AS existed3"
           ",known_coin_id3 AS known_coin_id3"
           ",denom_pub_hash3 AS denom_hash3"
           ",age_commitment_hash3 AS h_age_commitment3"
           ",existed4 AS existed4"
           ",known_coin_id4 AS known_coin_id4"
           ",denom_pub_hash4 AS denom_hash4"
           ",age_commitment_hash4 AS h_age_commitment4"
           " FROM exchange_do_batch2_known_coin"
           "  ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16);"
           );
  struct GNUNET_PQ_QueryParam params[] = {
    GNUNET_PQ_query_param_auto_from_type (&coin[0].coin_pub),
    GNUNET_PQ_query_param_auto_from_type (&coin[0].denom_pub_hash),
    GNUNET_PQ_query_param_auto_from_type (&coin[0].h_age_commitment),
    TALER_PQ_query_param_denom_sig (&coin[0].denom_sig),

    GNUNET_PQ_query_param_auto_from_type (&coin[1].coin_pub),
    GNUNET_PQ_query_param_auto_from_type (&coin[1].denom_pub_hash),
    GNUNET_PQ_query_param_auto_from_type (&coin[1].h_age_commitment),
    TALER_PQ_query_param_denom_sig (&coin[0].denom_sig),

    GNUNET_PQ_query_param_auto_from_type (&coin[2].coin_pub),
    GNUNET_PQ_query_param_auto_from_type (&coin[2].denom_pub_hash),
    GNUNET_PQ_query_param_auto_from_type (&coin[2].h_age_commitment),
    TALER_PQ_query_param_denom_sig (&coin[2].denom_sig),

    GNUNET_PQ_query_param_auto_from_type (&coin[3].coin_pub),
    GNUNET_PQ_query_param_auto_from_type (&coin[3].denom_pub_hash),
    GNUNET_PQ_query_param_auto_from_type (&coin[3].h_age_commitment),
    TALER_PQ_query_param_denom_sig (&coin[3].denom_sig),
    GNUNET_PQ_query_param_end
  };
  struct GNUNET_PQ_ResultSpec rs[] = {
    GNUNET_PQ_result_spec_bool ("existed",
                                &result[0].existed),
    GNUNET_PQ_result_spec_uint64 ("known_coin_id",
                                  &result[0].known_coin_id),
    GNUNET_PQ_result_spec_allow_null (
      GNUNET_PQ_result_spec_auto_from_type ("denom_pub_hash",
                                            &result[0].denom_hash),
      &is_denom_pub_hash_null),
    GNUNET_PQ_result_spec_allow_null (
      GNUNET_PQ_result_spec_auto_from_type ("age_commitment_hash",
                                            &result[0].h_age_commitment),
      &is_age_hash_null),
    GNUNET_PQ_result_spec_bool ("existed2",
                                &result[1].existed),
    GNUNET_PQ_result_spec_uint64 ("known_coin_id2",
                                  &result[1].known_coin_id),
    GNUNET_PQ_result_spec_allow_null (
      GNUNET_PQ_result_spec_auto_from_type ("denom_pub_hash2",
                                            &result[1].denom_hash),
      &is_denom_pub_hash_null2),
    GNUNET_PQ_result_spec_allow_null (
      GNUNET_PQ_result_spec_auto_from_type ("age_commitment_hash2",
                                            &result[1].h_age_commitment),
      &is_age_hash_null2),
    GNUNET_PQ_result_spec_bool ("existed3",
                                &result[2].existed),
    GNUNET_PQ_result_spec_uint64 ("known_coin_id3",
                                  &result[2].known_coin_id),
    GNUNET_PQ_result_spec_allow_null (
      GNUNET_PQ_result_spec_auto_from_type ("denom_pub_hash3",
                                            &result[2].denom_hash),
      &is_denom_pub_hash_null3),
    GNUNET_PQ_result_spec_allow_null (
      GNUNET_PQ_result_spec_auto_from_type ("age_commitment_hash3",
                                            &result[2].h_age_commitment),
      &is_age_hash_null3),
    GNUNET_PQ_result_spec_bool ("existed4",
                                &result[3].existed),
    GNUNET_PQ_result_spec_uint64 ("known_coin_id4",
                                  &result[3].known_coin_id),
    GNUNET_PQ_result_spec_allow_null (
      GNUNET_PQ_result_spec_auto_from_type ("denom_pub_hash4",
                                            &result[3].denom_hash),
      &is_denom_pub_hash_null4),
    GNUNET_PQ_result_spec_allow_null (
      GNUNET_PQ_result_spec_auto_from_type ("age_commitment_hash4",
                                            &result[3].h_age_commitment),
      &is_age_hash_null4),
    GNUNET_PQ_result_spec_end
  };

  qs = GNUNET_PQ_eval_prepared_singleton_select (pg->conn,
                                                 "batch4_known_coin",
                                                 params,
                                                 rs);
  switch (qs)
  {
  case GNUNET_DB_STATUS_HARD_ERROR:
    GNUNET_break (0);
    return qs;
  case GNUNET_DB_STATUS_SOFT_ERROR:
    return qs;
  case GNUNET_DB_STATUS_SUCCESS_NO_RESULTS:
    GNUNET_break (0); /* should be impossible */
    return GNUNET_DB_STATUS_HARD_ERROR;
  case GNUNET_DB_STATUS_SUCCESS_ONE_RESULT:
    break; /* continued below */
  }

  if ( (! is_denom_pub_hash_null) &&
       (0 != GNUNET_memcmp (&result[0].denom_hash,
                            &coin[0].denom_pub_hash)) )
  {
    GNUNET_break_op (0);
    result[0].denom_conflict = true;
  }
  if ( (! is_age_hash_null) &&
       (0 != GNUNET_memcmp (&result[0].h_age_commitment,
                            &coin[0].h_age_commitment)) )
  {
    GNUNET_break (GNUNET_is_zero (&result[0].h_age_commitment));
    GNUNET_break_op (0);
    result[0].age_conflict = true;
  }

  if ( (! is_denom_pub_hash_null2) &&
       (0 != GNUNET_memcmp (&result[1].denom_hash,
                            &coin[1].denom_pub_hash)) )
  {
    GNUNET_break_op (0);
    result[1].denom_conflict = true;
  }
  if ( (! is_age_hash_null2) &&
       (0 != GNUNET_memcmp (&result[1].h_age_commitment,
                            &coin[1].h_age_commitment)) )
  {
    GNUNET_break (GNUNET_is_zero (&result[1].h_age_commitment));
    GNUNET_break_op (0);
    result[1].age_conflict = true;
  }

  if ( (! is_denom_pub_hash_null3) &&
       (0 != GNUNET_memcmp (&result[2].denom_hash,
                            &coin[2].denom_pub_hash)) )
  {
    GNUNET_break_op (0);
    result[2].denom_conflict = true;
  }
  if ( (! is_age_hash_null3) &&
       (0 != GNUNET_memcmp (&result[2].h_age_commitment,
                            &coin[2].h_age_commitment)) )
  {
    GNUNET_break (GNUNET_is_zero (&result[2].h_age_commitment));
    GNUNET_break_op (0);
    result[2].age_conflict = true;
  }

  if ( (! is_denom_pub_hash_null4) &&
       (0 != GNUNET_memcmp (&result[3].denom_hash,
                            &coin[3].denom_pub_hash)) )
  {
    GNUNET_break_op (0);
    result[3].denom_conflict = true;
  }
  if ( (! is_age_hash_null4) &&
       (0 != GNUNET_memcmp (&result[3].h_age_commitment,
                            &coin[3].h_age_commitment)) )
  {
    GNUNET_break (GNUNET_is_zero (&result[3].h_age_commitment));
    GNUNET_break_op (0);
    result[3].age_conflict = true;
  }
  return qs;
}


enum GNUNET_DB_QueryStatus
TEH_PG_batch_ensure_coin_known (
  void *cls,
  const struct TALER_CoinPublicInfo *coin,
  struct TALER_EXCHANGEDB_CoinInfo *result,
  unsigned int coin_length,
  unsigned int batch_size)
{
  struct PostgresClosure *pg = cls;
  enum GNUNET_DB_QueryStatus qs = 0;
  unsigned int i = 0;

  while ( (qs >= 0) &&
          (i < coin_length) )
  {
    unsigned int bs = GNUNET_MIN (batch_size,
                                  coin_length - i);
    if (bs >= 4)
    {
      qs = insert4 (pg,
                    &coin[i],
                    &result[i]);
      i += 4;
      continue;
    }
    switch (bs)
    {
    case 3:
    case 2:
      qs = insert2 (pg,
                    &coin[i],
                    &result[i]);
      i += 2;
      break;
    case 1:
      qs = insert1 (pg,
                    &coin[i],
                    &result[i]);
      i += 1;
      break;
    case 0:
      GNUNET_assert (0);
      break;
    }
  } /* end while */
  if (qs < 0)
    return qs;
  return i;
}