simplify debit history computation

This commit is contained in:
Christian Grothoff 2020-01-15 22:39:16 +01:00
parent 85c285be79
commit a5395c2065
No known key found for this signature in database
GPG Key ID: 939E6BE1E29FC3CC

View File

@ -185,14 +185,14 @@ build_history (struct TALER_TESTING_Interpreter *is,
{ {
struct HistoryState *hs = is->commands[is->ip].cls; struct HistoryState *hs = is->commands[is->ip].cls;
unsigned int total; unsigned int total;
unsigned int pos;
struct History *h; struct History *h;
const struct TALER_TESTING_Command *add_incoming_cmd; const struct TALER_TESTING_Command *add_incoming_cmd;
int inc; int inc;
unsigned int start; unsigned int start;
unsigned int end; unsigned int end;
/* GNUNET_YES whenever either no 'start' value was given for the history /* #GNUNET_YES whenever either no 'start' value was given for the history
* query, or the given value is found in the list of all the CMDs. * query, or the given value is found in the list of all the CMDs. */
*/
int ok; int ok;
const uint64_t *row_id_start = NULL; const uint64_t *row_id_start = NULL;
@ -219,7 +219,7 @@ build_history (struct TALER_TESTING_Interpreter *is,
return 0; return 0;
} }
/* AKA 'delta'. */ /* AKA 'delta' */
if (hs->num_results > 0) if (hs->num_results > 0)
{ {
inc = 1; /* _inc_rement */ inc = 1; /* _inc_rement */
@ -233,189 +233,97 @@ build_history (struct TALER_TESTING_Interpreter *is,
end = 0; end = 0;
} }
total = 0;
ok = GNUNET_NO; ok = GNUNET_NO;
if (NULL == row_id_start) if (NULL == row_id_start)
ok = GNUNET_YES; ok = GNUNET_YES;
h = NULL;
// FIXME: simplify logic by folding the TWO loops into ONE, total = 0;
// (first doubling h if needed, and finally shrinking h to required size) GNUNET_array_grow (h,
total,
/* This loop counts how many commands _later than "start"_ belong 4);
* to the history of the caller. This is stored in the @var total pos = 0;
* variable. */
for (unsigned int off = start; off != end + inc; off += inc) for (unsigned int off = start; off != end + inc; off += inc)
{ {
const struct TALER_TESTING_Command *pos = &is->commands[off]; const struct TALER_TESTING_Command *cmd = &is->commands[off];
const uint64_t *row_id; const uint64_t *row_id;
const char *debit_account; const char *debit_account;
const char *credit_account; const char *credit_account;
const struct TALER_Amount *amount;
const struct TALER_WireTransferIdentifierRawP *wtid;
const char *account_url;
/* The following command allows us to skip over those CMDs if ( (GNUNET_OK !=
* that do not offer a "row_id" trait. Such skipped CMDs are TALER_TESTING_get_trait_bank_row (cmd,
* not interesting for building a history. */// &row_id)) ||
if (GNUNET_OK != (GNUNET_OK !=
TALER_TESTING_get_trait_bank_row (pos, TALER_TESTING_get_trait_payto (cmd,
&row_id)) TALER_TESTING_PT_DEBIT,
continue; &debit_account)) ||
(GNUNET_OK !=
TALER_TESTING_get_trait_payto (cmd,
TALER_TESTING_PT_CREDIT,
&credit_account)) ||
(GNUNET_OK !=
TALER_TESTING_get_trait_amount_obj (cmd,
0,
&amount)) ||
(GNUNET_OK !=
TALER_TESTING_get_trait_wtid (cmd,
0,
&wtid)) ||
(GNUNET_OK !=
TALER_TESTING_get_trait_url (cmd,
1,
&account_url)) )
continue; /* not an event we care about */
/* Seek "/history" starting row. */ /* Seek "/history" starting row. */
if (NULL != row_id_start) if ( (NULL != row_id_start) &&
(*row_id_start == *row_id) &&
(GNUNET_NO == ok) )
{ {
if (*row_id_start == *row_id) /* Until here nothing counted */
{ ok = GNUNET_YES;
/* Doesn't count, start is excluded from output. */ continue;
total = 0;
ok = GNUNET_YES;
continue;
}
} }
/* when 'start' was _not_ given, then ok == GNUNET_YES */ /* when 'start' was _not_ given, then ok == GNUNET_YES */
if (GNUNET_NO == ok) if (GNUNET_NO == ok)
continue; /* skip until we find the marker */ continue; /* skip until we find the marker */
if (0 != strcasecmp (hs->account_url,
TALER_LOG_DEBUG ("Found first row\n"); debit_account))
continue; /* account missmatch */
if (total >= GNUNET_MAX (hs->num_results, if (total >= GNUNET_MAX (hs->num_results,
-hs->num_results) ) -hs->num_results) )
{ {
TALER_LOG_DEBUG ("Hit history limit\n"); TALER_LOG_DEBUG ("Hit history limit\n");
break; break;
} }
TALER_LOG_INFO ("Found history: %s->%s for account %s\n",
GNUNET_assert (GNUNET_OK ==
TALER_TESTING_get_trait_payto (pos,
TALER_TESTING_PT_DEBIT,
&debit_account));
GNUNET_assert (GNUNET_OK ==
TALER_TESTING_get_trait_payto (pos,
TALER_TESTING_PT_CREDIT,
&credit_account));
TALER_LOG_INFO ("Potential history element:"
" %s->%s; my account: %s\n",
debit_account, debit_account,
credit_account, credit_account,
hs->account_url); hs->account_url);
if (0 == strcasecmp (hs->account_url, /* found matching record, make sure we have room */
debit_account)) if (pos == total)
{ GNUNET_array_grow (h,
TALER_LOG_INFO ("+1 my history\n"); total,
total++; /* found matching record */ pos * 2);
} h[total].url = GNUNET_strdup (credit_account);
h[total].details.credit_account_url = h[total].url;
h[total].details.amount = *amount;
h[total].row_id = *row_id;
h[total].details.wtid = *wtid;
h[total].details.debit_account_url = account_url;
pos++;
} }
GNUNET_assert (GNUNET_YES == ok); GNUNET_assert (GNUNET_YES == ok);
GNUNET_array_grow (h,
if (0 == total) total,
pos);
if (0 == pos)
{ {
TALER_LOG_DEBUG ("Checking history at first CMD.. (2)\n"); TALER_LOG_DEBUG ("Empty history computed\n");
*rh = NULL; *rh = NULL;
return 0; return 0;
} }
h = GNUNET_new_array ((unsigned int) total,
struct History);
total = 0;
ok = GNUNET_NO;
if (NULL == row_id_start)
ok = GNUNET_YES;
/**
* This loop _only_ populates the array of history elements.
*/
for (unsigned int off = start; off != end + inc; off += inc)
{
const struct TALER_TESTING_Command *pos = &is->commands[off];
const uint64_t *row_id;
char *bank_hostname;
const char *credit_account;
const char *debit_account;
if (GNUNET_OK !=
TALER_TESTING_get_trait_bank_row (pos,
&row_id))
continue;
if (NULL != row_id_start)
{
if (*row_id_start == *row_id)
{
/**
* Warning: this zeroing is superfluous, as
* total doesn't get incremented if 'start'
* was given and couldn't be found.
*/total = 0;
ok = GNUNET_YES;
continue;
}
}
TALER_LOG_INFO ("Found first row (2)\n");
if (GNUNET_NO == ok)
{
TALER_LOG_INFO ("Skip on `%s'\n",
pos->label);
continue; /* skip until we find the marker */
}
if (total >= GNUNET_MAX (hs->num_results,
-hs->num_results))
{
TALER_LOG_INFO ("Hit history limit (2)\n");
break;
}
GNUNET_assert (GNUNET_OK ==
TALER_TESTING_get_trait_payto (pos,
TALER_TESTING_PT_DEBIT,
&debit_account));
GNUNET_assert (GNUNET_OK ==
TALER_TESTING_get_trait_payto (pos,
TALER_TESTING_PT_CREDIT,
&credit_account));
TALER_LOG_INFO ("Potential history bit: %s->%s; my account: %s\n",
debit_account,
credit_account,
hs->account_url);
bank_hostname = strchr (hs->account_url, ':');
GNUNET_assert (NULL != bank_hostname);
bank_hostname += 3;
/* Next two blocks only put the 'direction' and 'banking'
* information. */
/* Asked for debit, and account got the debit. */
if (0 == strcasecmp (hs->account_url,
debit_account))
{
const struct TALER_Amount *amount;
const struct TALER_WireTransferIdentifierRawP *wtid;
const char *account_url;
GNUNET_assert (GNUNET_OK ==
TALER_TESTING_get_trait_amount_obj (pos,
0,
&amount));
GNUNET_assert (GNUNET_OK ==
TALER_TESTING_get_trait_wtid (pos,
0,
&wtid));
GNUNET_assert (GNUNET_OK ==
TALER_TESTING_get_trait_url (pos,
1,
&account_url));
h[total].url = GNUNET_strdup (credit_account);
h[total].details.credit_account_url = h[total].url;
h[total].details.amount = *amount;
h[total].row_id = *row_id;
h[total].details.wtid = *wtid;
h[total].details.debit_account_url = account_url;
TALER_LOG_INFO ("+1-bit of my history\n");
total++;
}
}
*rh = h; *rh = h;
return total; return total;
} }