2022-11-16 17:44:48 +01:00
|
|
|
/*
|
|
|
|
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/>
|
|
|
|
*/
|
|
|
|
/**
|
2022-11-18 17:18:45 +01:00
|
|
|
* @file exchangedb/pg_batch_reserves_in_insert.c
|
2022-11-16 17:44:48 +01:00
|
|
|
* @brief Implementation of the reserves_in_insert function for Postgres
|
2022-11-25 12:09:20 +01:00
|
|
|
* @author Joseph Xu
|
2022-11-16 17:44:48 +01:00
|
|
|
*/
|
|
|
|
#include "platform.h"
|
|
|
|
#include "taler_error_codes.h"
|
|
|
|
#include "taler_dbevents.h"
|
|
|
|
#include "taler_pq_lib.h"
|
|
|
|
#include "pg_batch_reserves_in_insert.h"
|
|
|
|
#include "pg_helper.h"
|
|
|
|
#include "pg_start.h"
|
2022-11-21 15:45:16 +01:00
|
|
|
#include "pg_rollback.h"
|
2022-11-16 17:44:48 +01:00
|
|
|
#include "pg_start_read_committed.h"
|
|
|
|
#include "pg_commit.h"
|
|
|
|
#include "pg_reserves_get.h"
|
|
|
|
#include "pg_reserves_update.h"
|
|
|
|
#include "pg_setup_wire_target.h"
|
|
|
|
#include "pg_event_notify.h"
|
2022-11-25 12:09:20 +01:00
|
|
|
#include "pg_preflight.h"
|
2022-11-16 17:44:48 +01:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
2022-11-23 16:41:57 +01:00
|
|
|
* Generate event notification for the reserve change.
|
2022-11-16 17:44:48 +01:00
|
|
|
*
|
|
|
|
* @param reserve_pub reserve to notfiy on
|
|
|
|
*/
|
2022-11-23 16:41:57 +01:00
|
|
|
static char *
|
|
|
|
compute_notify_on_reserve (const struct TALER_ReservePublicKeyP *reserve_pub)
|
2022-11-16 17:44:48 +01:00
|
|
|
{
|
|
|
|
struct TALER_ReserveEventP rep = {
|
|
|
|
.header.size = htons (sizeof (rep)),
|
|
|
|
.header.type = htons (TALER_DBEVENT_EXCHANGE_RESERVE_INCOMING),
|
|
|
|
.reserve_pub = *reserve_pub
|
|
|
|
};
|
|
|
|
|
2022-11-23 16:41:57 +01:00
|
|
|
return GNUNET_PG_get_event_notify_channel (&rep.header);
|
2022-11-16 17:44:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
enum GNUNET_DB_QueryStatus
|
|
|
|
TEH_PG_batch_reserves_in_insert (void *cls,
|
2022-12-06 13:29:23 +01:00
|
|
|
const struct
|
|
|
|
TALER_EXCHANGEDB_ReserveInInfo *reserves,
|
2022-11-21 16:37:50 +01:00
|
|
|
unsigned int reserves_length,
|
|
|
|
enum GNUNET_DB_QueryStatus *results)
|
2022-11-16 17:44:48 +01:00
|
|
|
{
|
|
|
|
struct PostgresClosure *pg = cls;
|
|
|
|
enum GNUNET_DB_QueryStatus qs1;
|
|
|
|
struct GNUNET_TIME_Timestamp expiry;
|
|
|
|
struct GNUNET_TIME_Timestamp gc;
|
2022-11-18 17:18:45 +01:00
|
|
|
struct TALER_PaytoHashP h_payto;
|
2022-11-16 17:44:48 +01:00
|
|
|
uint64_t reserve_uuid;
|
2022-11-18 17:18:45 +01:00
|
|
|
bool conflicted;
|
|
|
|
bool transaction_duplicate;
|
2022-11-23 16:41:57 +01:00
|
|
|
bool need_update = false;
|
2022-11-18 17:18:45 +01:00
|
|
|
struct GNUNET_TIME_Timestamp reserve_expiration
|
|
|
|
= GNUNET_TIME_relative_to_timestamp (pg->idle_reserve_expiration_time);
|
2022-11-23 16:41:57 +01:00
|
|
|
bool conflicts[reserves_length];
|
|
|
|
char *notify_s[reserves_length];
|
2022-11-16 17:44:48 +01:00
|
|
|
|
2022-11-25 12:09:20 +01:00
|
|
|
if (GNUNET_OK !=
|
|
|
|
TEH_PG_preflight (pg))
|
|
|
|
{
|
|
|
|
GNUNET_break (0);
|
|
|
|
return GNUNET_DB_STATUS_HARD_ERROR;
|
|
|
|
}
|
2022-11-21 16:37:50 +01:00
|
|
|
PREPARE (pg,
|
|
|
|
"reserve_create",
|
|
|
|
"SELECT "
|
|
|
|
"out_reserve_found AS conflicted"
|
|
|
|
",transaction_duplicate"
|
|
|
|
",ruuid AS reserve_uuid"
|
2022-11-23 16:41:57 +01:00
|
|
|
" FROM batch_reserves_insert"
|
|
|
|
" ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12);");
|
2022-11-16 17:44:48 +01:00
|
|
|
expiry = GNUNET_TIME_absolute_to_timestamp (
|
|
|
|
GNUNET_TIME_absolute_add (reserves->execution_time.abs_time,
|
|
|
|
pg->idle_reserve_expiration_time));
|
|
|
|
gc = GNUNET_TIME_absolute_to_timestamp (
|
|
|
|
GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
|
|
|
|
pg->legal_reserve_expiration_time));
|
|
|
|
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
|
|
|
|
"Creating reserve %s with expiration in %s\n",
|
|
|
|
TALER_B2S (&(reserves->reserve_pub)),
|
|
|
|
GNUNET_STRINGS_relative_time_to_string (
|
|
|
|
pg->idle_reserve_expiration_time,
|
|
|
|
GNUNET_NO));
|
2022-11-23 16:41:57 +01:00
|
|
|
|
|
|
|
{
|
|
|
|
if (GNUNET_OK !=
|
|
|
|
TEH_PG_start_read_committed(pg,
|
|
|
|
"READ_COMMITED"))
|
|
|
|
{
|
|
|
|
GNUNET_break (0);
|
|
|
|
return GNUNET_DB_STATUS_HARD_ERROR;
|
|
|
|
}
|
|
|
|
}
|
2022-11-16 17:44:48 +01:00
|
|
|
/* Optimistically assume this is a new reserve, create balance for the first
|
|
|
|
time; we do this before adding the actual transaction to "reserves_in",
|
|
|
|
as for a new reserve it can't be a duplicate 'add' operation, and as
|
|
|
|
the 'add' operation needs the reserve entry as a foreign key. */
|
2022-12-06 13:29:23 +01:00
|
|
|
for (unsigned int i = 0; i<reserves_length; i++)
|
2022-11-23 16:41:57 +01:00
|
|
|
{
|
|
|
|
const struct TALER_EXCHANGEDB_ReserveInInfo *reserve = &reserves[i];
|
|
|
|
notify_s[i] = compute_notify_on_reserve (&reserve->reserve_pub);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (unsigned int i=0;i<reserves_length;i++)
|
2022-11-16 17:44:48 +01:00
|
|
|
{
|
2022-11-21 16:37:50 +01:00
|
|
|
const struct TALER_EXCHANGEDB_ReserveInInfo *reserve = &reserves[i];
|
2022-11-16 17:44:48 +01:00
|
|
|
struct GNUNET_PQ_QueryParam params[] = {
|
2022-11-23 16:41:57 +01:00
|
|
|
GNUNET_PQ_query_param_auto_from_type (&reserve->reserve_pub),
|
|
|
|
GNUNET_PQ_query_param_timestamp (&expiry),
|
|
|
|
GNUNET_PQ_query_param_timestamp (&gc),
|
|
|
|
GNUNET_PQ_query_param_uint64 (&reserve->wire_reference),
|
|
|
|
TALER_PQ_query_param_amount (&reserve->balance),
|
|
|
|
GNUNET_PQ_query_param_string (reserve->exchange_account_name),
|
|
|
|
GNUNET_PQ_query_param_timestamp (&reserve->execution_time),
|
|
|
|
GNUNET_PQ_query_param_auto_from_type (&h_payto),
|
|
|
|
GNUNET_PQ_query_param_string (reserve->sender_account_details),
|
|
|
|
GNUNET_PQ_query_param_timestamp (&reserve_expiration),
|
|
|
|
GNUNET_PQ_query_param_string (notify_s[i]),
|
2022-11-16 17:44:48 +01:00
|
|
|
GNUNET_PQ_query_param_end
|
|
|
|
};
|
2022-11-23 16:41:57 +01:00
|
|
|
|
2022-11-16 17:44:48 +01:00
|
|
|
struct GNUNET_PQ_ResultSpec rs[] = {
|
2022-11-18 17:18:45 +01:00
|
|
|
GNUNET_PQ_result_spec_bool ("conflicted",
|
|
|
|
&conflicted),
|
|
|
|
GNUNET_PQ_result_spec_bool ("transaction_duplicate",
|
|
|
|
&transaction_duplicate),
|
2022-11-23 16:41:57 +01:00
|
|
|
GNUNET_PQ_result_spec_uint64 ("reserve_uuid",
|
|
|
|
&reserve_uuid),
|
2022-11-16 17:44:48 +01:00
|
|
|
GNUNET_PQ_result_spec_end
|
|
|
|
};
|
|
|
|
|
2022-11-21 16:37:50 +01:00
|
|
|
TALER_payto_hash (reserve->sender_account_details,
|
|
|
|
&h_payto);
|
2022-11-25 12:09:20 +01:00
|
|
|
|
2022-11-16 17:44:48 +01:00
|
|
|
/* Note: query uses 'on conflict do nothing' */
|
|
|
|
qs1 = GNUNET_PQ_eval_prepared_singleton_select (pg->conn,
|
|
|
|
"reserve_create",
|
|
|
|
params,
|
|
|
|
rs);
|
2022-11-25 12:09:20 +01:00
|
|
|
|
2022-11-16 17:44:48 +01:00
|
|
|
if (qs1 < 0)
|
2022-11-23 16:41:57 +01:00
|
|
|
{
|
|
|
|
GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
|
|
|
|
"Failed to create reserves (%d)\n",
|
|
|
|
qs1);
|
2022-12-06 13:29:23 +01:00
|
|
|
return qs1;
|
2022-11-23 16:41:57 +01:00
|
|
|
}
|
|
|
|
GNUNET_assert (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS != qs1);
|
|
|
|
results[i] = (transaction_duplicate)
|
2022-11-21 16:37:50 +01:00
|
|
|
? GNUNET_DB_STATUS_SUCCESS_NO_RESULTS
|
|
|
|
: GNUNET_DB_STATUS_SUCCESS_ONE_RESULT;
|
2022-11-23 16:41:57 +01:00
|
|
|
conflicts[i] = conflicted;
|
2022-11-25 12:09:20 +01:00
|
|
|
// fprintf(stdout, "%d", conflicts[i]);
|
2022-11-23 16:41:57 +01:00
|
|
|
if (!conflicts[i] && transaction_duplicate)
|
|
|
|
{
|
|
|
|
GNUNET_break (0);
|
|
|
|
TEH_PG_rollback (pg);
|
|
|
|
return GNUNET_DB_STATUS_HARD_ERROR;
|
|
|
|
}
|
|
|
|
need_update |= conflicted;
|
2022-11-16 17:44:48 +01:00
|
|
|
}
|
2022-11-23 16:41:57 +01:00
|
|
|
// commit
|
|
|
|
{
|
|
|
|
enum GNUNET_DB_QueryStatus cs;
|
|
|
|
|
|
|
|
cs = TEH_PG_commit (pg);
|
|
|
|
if (cs < 0)
|
|
|
|
return cs;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!need_update)
|
|
|
|
goto exit;
|
|
|
|
// begin serializable
|
|
|
|
{
|
|
|
|
if (GNUNET_OK !=
|
|
|
|
TEH_PG_start(pg,
|
|
|
|
"reserve-insert-continued"))
|
|
|
|
{
|
|
|
|
GNUNET_break (0);
|
|
|
|
return GNUNET_DB_STATUS_HARD_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
enum GNUNET_DB_QueryStatus qs2;
|
|
|
|
PREPARE (pg,
|
|
|
|
"reserves_in_add_transaction",
|
|
|
|
"SELECT batch_reserves_update"
|
|
|
|
" ($1,$2,$3,$4,$5,$6,$7,$8,$9);");
|
|
|
|
for (unsigned int i=0;i<reserves_length;i++)
|
|
|
|
{
|
|
|
|
if (! conflicts[i])
|
|
|
|
continue;
|
|
|
|
{
|
|
|
|
const struct TALER_EXCHANGEDB_ReserveInInfo *reserve = &reserves[i];
|
|
|
|
struct GNUNET_PQ_QueryParam params[] = {
|
|
|
|
GNUNET_PQ_query_param_auto_from_type (&reserve->reserve_pub),
|
|
|
|
GNUNET_PQ_query_param_timestamp (&expiry),
|
|
|
|
GNUNET_PQ_query_param_uint64 (&reserve->wire_reference),
|
|
|
|
TALER_PQ_query_param_amount (&reserve->balance),
|
|
|
|
GNUNET_PQ_query_param_string (reserve->exchange_account_name),
|
|
|
|
GNUNET_PQ_query_param_bool (conflicted),
|
|
|
|
GNUNET_PQ_query_param_auto_from_type (&h_payto),
|
|
|
|
GNUNET_PQ_query_param_string (notify_s[i]),
|
|
|
|
GNUNET_PQ_query_param_end
|
|
|
|
};
|
2022-11-25 12:09:20 +01:00
|
|
|
|
2022-11-23 16:41:57 +01:00
|
|
|
qs2 = GNUNET_PQ_eval_prepared_non_select (pg->conn,
|
|
|
|
"reserves_in_add_transaction",
|
|
|
|
params);
|
|
|
|
if (qs2<0)
|
|
|
|
{
|
|
|
|
GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
|
|
|
|
"Failed to update reserves (%d)\n",
|
|
|
|
qs2);
|
|
|
|
return qs2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
{
|
|
|
|
enum GNUNET_DB_QueryStatus cs;
|
|
|
|
|
|
|
|
cs = TEH_PG_commit (pg);
|
|
|
|
if (cs < 0)
|
|
|
|
return cs;
|
|
|
|
}
|
|
|
|
|
|
|
|
exit:
|
|
|
|
for (unsigned int i=0;i<reserves_length;i++)
|
|
|
|
GNUNET_free (notify_s[i]);
|
|
|
|
|
2022-11-21 16:37:50 +01:00
|
|
|
return reserves_length;
|
2022-11-16 17:44:48 +01:00
|
|
|
}
|