fix get_link_data logic

This commit is contained in:
Christian Grothoff 2023-02-07 12:27:45 +01:00
parent 5c983bd05e
commit d0b43b0e6a
No known key found for this signature in database
GPG Key ID: 939E6BE1E29FC3CC

View File

@ -47,34 +47,23 @@ struct LinkDataContext
*/ */
struct TALER_TransferPublicKeyP transfer_pub; struct TALER_TransferPublicKeyP transfer_pub;
/**
* Link data for @e transfer_pub
*/
struct TALER_EXCHANGEDB_LinkList *last;
/** /**
* Status, set to #GNUNET_SYSERR on errors, * Status, set to #GNUNET_SYSERR on errors,
*/ */
enum GNUNET_GenericReturnValue status; enum GNUNET_GenericReturnValue status;
}; };
struct Results {
struct TALER_EXCHANGEDB_LinkList *pos;
struct TALER_TransferPublicKeyP transfer_pub;
};
/** /**
* Free memory of the link data list. * Free memory of the link data list.
* *
* @param cls the @e cls of this struct with the plugin-specific state (unused)
* @param ldl link data list to release * @param ldl link data list to release
*/ */
static void static void
free_link_data_list (void *cls, free_link_data_list (struct TALER_EXCHANGEDB_LinkList *ldl)
struct TALER_EXCHANGEDB_LinkList *ldl)
{ {
struct TALER_EXCHANGEDB_LinkList *next; struct TALER_EXCHANGEDB_LinkList *next;
(void) cls;
while (NULL != ldl) while (NULL != ldl)
{ {
next = ldl->next; next = ldl->next;
@ -85,6 +74,14 @@ free_link_data_list (void *cls,
} }
} }
struct Results
{
struct TALER_EXCHANGEDB_LinkList *pos;
struct TALER_TransferPublicKeyP transfer_pub;
};
static int static int
transfer_pub_cmp (const void *a, transfer_pub_cmp (const void *a,
const void *b) const void *b)
@ -96,6 +93,7 @@ transfer_pub_cmp (const void *a,
&rb->transfer_pub); &rb->transfer_pub);
} }
/** /**
* Function to be called with the results of a SELECT statement * Function to be called with the results of a SELECT statement
* that has returned @a num_results results. * that has returned @a num_results results.
@ -110,22 +108,20 @@ add_ldl (void *cls,
unsigned int num_results) unsigned int num_results)
{ {
struct LinkDataContext *ldctx = cls; struct LinkDataContext *ldctx = cls;
struct Results *temp= GNUNET_new_array (num_results, struct Results *temp = GNUNET_new_array (num_results,
struct Results);; struct Results);
unsigned int temp_off = 0; unsigned int temp_off = 0;
for (int i = num_results - 1; i >= 0; i--) for (int i = num_results - 1; i >= 0; i--)
{ {
struct TALER_EXCHANGEDB_LinkList *pos; struct TALER_EXCHANGEDB_LinkList *pos;
struct TALER_TransferPublicKeyP transfer_pub;
pos = GNUNET_new (struct TALER_EXCHANGEDB_LinkList); pos = GNUNET_new (struct TALER_EXCHANGEDB_LinkList);
{ {
struct TALER_BlindedPlanchet bp; struct TALER_BlindedPlanchet bp;
struct GNUNET_PQ_ResultSpec rs[] = { struct GNUNET_PQ_ResultSpec rs[] = {
GNUNET_PQ_result_spec_auto_from_type ("transfer_pub", GNUNET_PQ_result_spec_auto_from_type ("transfer_pub",
&transfer_pub), &temp[temp_off].transfer_pub),
GNUNET_PQ_result_spec_auto_from_type ("link_sig", GNUNET_PQ_result_spec_auto_from_type ("link_sig",
&pos->orig_coin_link_sig), &pos->orig_coin_link_sig),
TALER_PQ_result_spec_blinded_denom_sig ("ev_sig", TALER_PQ_result_spec_blinded_denom_sig ("ev_sig",
@ -158,66 +154,44 @@ add_ldl (void *cls,
} }
TALER_blinded_planchet_free (&bp); TALER_blinded_planchet_free (&bp);
} }
temp[temp_off].pos = pos; temp[temp_off].pos = pos;
temp[temp_off].transfer_pub = transfer_pub; temp_off++;
temp_off++;
} }
qsort (temp, qsort (temp,
temp_off, temp_off,
sizeof (struct Results), sizeof (struct Results),
transfer_pub_cmp); &transfer_pub_cmp);
for (unsigned int i = 0; i < temp_off; i++) if (temp_off > 0)
{ {
struct TALER_EXCHANGEDB_LinkList *pos; struct TALER_EXCHANGEDB_LinkList *head = NULL;
struct Results *r = &temp[i];
pos = GNUNET_new (struct TALER_EXCHANGEDB_LinkList); head = temp[0].pos;
pos->orig_coin_link_sig = r->pos->orig_coin_link_sig; for (unsigned int i = 1; i < temp_off; i++)
pos->ev_sig = r->pos->ev_sig;
pos->coin_refresh_offset = r->pos->coin_refresh_offset;
pos->alg_values = r->pos->alg_values;
pos->denom_pub = r->pos->denom_pub;
pos->nonce = r->pos->nonce;
pos->have_nonce = r->pos->have_nonce;
pos->next = ldctx->last;
ldctx->last = pos;
}
if (NULL != ldctx->last)
{
ldctx->ldc (ldctx->ldc_cls,
&ldctx->transfer_pub,
ldctx->last);
free_link_data_list (ldctx,
ldctx->last);
}
ldctx->last = NULL;
GNUNET_free(temp);
/*
if ( (NULL != ldctx->last) &&
(0 == GNUNET_memcmp (&transfer_pub,
&ldctx->transfer_pub)) )
{ {
pos->next = ldctx->last; struct TALER_EXCHANGEDB_LinkList *pos = temp[i].pos;
} const struct TALER_TransferPublicKeyP *tp = &temp[i].transfer_pub;
else
{ if (0 == GNUNET_memcmp (tp,
if (NULL != ldctx->last) &temp[i - 1].transfer_pub))
{
pos->next = head;
head = pos;
}
else
{ {
ldctx->ldc (ldctx->ldc_cls, ldctx->ldc (ldctx->ldc_cls,
&ldctx->transfer_pub, &temp[i - 1].transfer_pub,
ldctx->last); head);
free_link_data_list (cls, free_link_data_list (head);
ldctx->last); head = pos;
} }
ldctx->transfer_pub = transfer_pub;
} }
ldctx->last = pos; ldctx->ldc (ldctx->ldc_cls,
&temp[temp_off - 1].transfer_pub,
head);
free_link_data_list (head);
} }
GNUNET_free(temp);*/ GNUNET_free (temp);
} }
@ -239,7 +213,7 @@ TEH_PG_get_link_data (void *cls,
if (-2 == percent_refund) if (-2 == percent_refund)
{ {
const char *mode = getenv ("NEW_LOGIC"); const char *mode = getenv ("TALER_POSTGRES_GET_LINK_DATA_LOGIC");
char dummy; char dummy;
if ( (NULL==mode) || if ( (NULL==mode) ||
@ -252,7 +226,7 @@ TEH_PG_get_link_data (void *cls,
GNUNET_log (GNUNET_ERROR_TYPE_ERROR, GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
"Bad mode `%s' specified\n", "Bad mode `%s' specified\n",
mode); mode);
percent_refund = 0; percent_refund = 4; /* Fastest known */
} }
} }
switch (percent_refund) switch (percent_refund)
@ -325,7 +299,7 @@ TEH_PG_get_link_data (void *cls,
" ,coin_ev BYTEA);"); " ,coin_ev BYTEA);");
break; break;
case 3: case 3:
query="get_link_v3"; query = "get_link_v3";
PREPARE (pg, PREPARE (pg,
query, query,
"SELECT " "SELECT "
@ -346,7 +320,7 @@ TEH_PG_get_link_data (void *cls,
" WHERE old_coin_pub=$1"); " WHERE old_coin_pub=$1");
break; break;
case 4: case 4:
query="get_link_v4"; query = "get_link_v4";
PREPARE (pg, PREPARE (pg,
query, query,
"WITH rc AS MATERIALIZED (" "WITH rc AS MATERIALIZED ("
@ -370,7 +344,7 @@ TEH_PG_get_link_data (void *cls,
" JOIN denominations denoms" " JOIN denominations denoms"
" USING (denominations_serial)" " USING (denominations_serial)"
" WHERE rrc.melt_serial_id = (SELECT melt_serial_id FROM rc)" " WHERE rrc.melt_serial_id = (SELECT melt_serial_id FROM rc)"
); );
break; break;
default: default:
GNUNET_break (0); GNUNET_break (0);
@ -379,26 +353,12 @@ TEH_PG_get_link_data (void *cls,
ldctx.ldc = ldc; ldctx.ldc = ldc;
ldctx.ldc_cls = ldc_cls; ldctx.ldc_cls = ldc_cls;
ldctx.last = NULL;
ldctx.status = GNUNET_OK; ldctx.status = GNUNET_OK;
qs = GNUNET_PQ_eval_prepared_multi_select (pg->conn, qs = GNUNET_PQ_eval_prepared_multi_select (pg->conn,
query, query,
params, params,
&add_ldl, &add_ldl,
&ldctx); &ldctx);
if (NULL != ldctx.last)
{
if (GNUNET_OK == ldctx.status)
{
/* call callback one more time! */
ldc (ldc_cls,
&ldctx.transfer_pub,
ldctx.last);
}
free_link_data_list (cls,
ldctx.last);
ldctx.last = NULL;
}
if (GNUNET_OK != ldctx.status) if (GNUNET_OK != ldctx.status)
return GNUNET_DB_STATUS_HARD_ERROR; return GNUNET_DB_STATUS_HARD_ERROR;
return qs; return qs;