From 4c5c63e9b921158bc4bd774d575f5fb562c116c5 Mon Sep 17 00:00:00 2001 From: Christian Grothoff Date: Wed, 15 Jan 2020 22:47:20 +0100 Subject: simplify credit history computation --- src/lib/testing_api_cmd_bank_history_credit.c | 223 ++++++++------------------ src/lib/testing_api_cmd_bank_history_debit.c | 12 +- 2 files changed, 68 insertions(+), 167 deletions(-) (limited to 'src/lib') diff --git a/src/lib/testing_api_cmd_bank_history_credit.c b/src/lib/testing_api_cmd_bank_history_credit.c index 8c85bc17c..1b4a340b5 100644 --- a/src/lib/testing_api_cmd_bank_history_credit.c +++ b/src/lib/testing_api_cmd_bank_history_credit.c @@ -150,9 +150,9 @@ print_expected (struct History *h, unsigned int off) { GNUNET_log (GNUNET_ERROR_TYPE_ERROR, - "Transaction history missmatch at position %u/%llu\n", + "Transaction history missmatch at position %u/%u\n", off, - (unsigned long long) h_len); + h_len); GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Expected history:\n"); for (unsigned int i = 0; icommands[is->ip].cls; unsigned int total; + unsigned int pos; struct History *h; const struct TALER_TESTING_Command *add_incoming_cmd; int inc; @@ -235,192 +236,96 @@ build_history (struct TALER_TESTING_Interpreter *is, end = 0; } - total = 0; ok = GNUNET_NO; - if (NULL == row_id_start) ok = GNUNET_YES; - - // FIXME: simplify logic by folding the TWO loops into ONE, - // (first doubling h if needed, and finally shrinking h to required size) - - /* This loop counts how many commands _later than "start"_ belong - * to the history of the caller. This is stored in the @var total - * variable. */ + h = NULL; + total = 0; + GNUNET_array_grow (h, + total, + 4); + pos = 0; 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 char *credit_account; const char *debit_account; + const struct TALER_Amount *amount; + const struct TALER_ReservePublicKeyP *reserve_pub; + const char *account_url; /* The following command allows us to skip over those CMDs * that do not offer a "row_id" trait. Such skipped CMDs are * not interesting for building a history. */// - if (GNUNET_OK != - TALER_TESTING_get_trait_bank_row (pos, - &row_id)) - continue; - + if ( (GNUNET_OK != + TALER_TESTING_get_trait_bank_row (cmd, + &row_id)) || + (GNUNET_OK != + TALER_TESTING_get_trait_payto (cmd, + TALER_TESTING_PT_CREDIT, + &credit_account)) || + (GNUNET_OK != + TALER_TESTING_get_trait_payto (cmd, + TALER_TESTING_PT_DEBIT, + &debit_account)) || + (GNUNET_OK == + TALER_TESTING_get_trait_amount_obj (cmd, + 0, + &amount)) || + (GNUNET_OK == + TALER_TESTING_get_trait_reserve_pub (cmd, + 0, + &reserve_pub)) || + (GNUNET_OK == + TALER_TESTING_get_trait_url (cmd, + 1, + &account_url)) ) + continue; /* not an interesting event */ /* 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) - { - /* Doesn't count, start is excluded from output. */ - total = 0; - ok = GNUNET_YES; - continue; - } + /* Until here, nothing counted. */ + ok = GNUNET_YES; + continue; } - /* when 'start' was _not_ given, then ok == GNUNET_YES */ if (GNUNET_NO == ok) continue; /* skip until we find the marker */ - - TALER_LOG_DEBUG ("Found first row\n"); + if (0 != strcasecmp (hs->account_url, + credit_account)) + continue; /* account missmatch */ if (total >= GNUNET_MAX (hs->num_results, -hs->num_results) ) { TALER_LOG_DEBUG ("Hit history limit\n"); break; } - GNUNET_assert (GNUNET_OK == - TALER_TESTING_get_trait_payto (pos, - TALER_TESTING_PT_CREDIT, - &credit_account)); - GNUNET_assert (GNUNET_OK == - TALER_TESTING_get_trait_payto (pos, - TALER_TESTING_PT_DEBIT, - &debit_account)); - TALER_LOG_INFO ("Potential history element:" - " %s->%s; my account: %s\n", + TALER_LOG_INFO ("Found history: %s->%s for account %s\n", debit_account, credit_account, hs->account_url); - - if (0 == strcasecmp (hs->account_url, - credit_account)) - { - TALER_LOG_INFO ("+1 my history\n"); - total++; /* found matching record */ - } + /* found matching record, make sure we have room */ + if (pos == total) + GNUNET_array_grow (h, + total, + pos * 2); + h[total].url = GNUNET_strdup (debit_account); + h[total].details.debit_account_url = h[total].url; + h[total].details.amount = *amount; + h[total].row_id = *row_id; + h[total].details.reserve_pub = *reserve_pub; + h[total].details.credit_account_url = account_url; + pos++; } - GNUNET_assert (GNUNET_YES == ok); - - if (0 == total) - { - TALER_LOG_DEBUG ("Checking history with ZERO transactions\n"); - *rh = NULL; - return 0; - } - - - h = GNUNET_new_array (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_CREDIT, - &credit_account)); - GNUNET_assert (GNUNET_OK == - TALER_TESTING_get_trait_payto (pos, - TALER_TESTING_PT_DEBIT, - &debit_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 credit, and account got the credit. */ - if (0 == strcasecmp (hs->account_url, - credit_account)) - { - const struct TALER_Amount *amount; - const struct TALER_ReservePublicKeyP *reserve_pub; - 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_reserve_pub (pos, - 0, - &reserve_pub)); - GNUNET_assert (GNUNET_OK == - TALER_TESTING_get_trait_url (pos, - 1, - &account_url)); - h[total].url = GNUNET_strdup (debit_account); - h[total].details.debit_account_url = h[total].url; - h[total].details.amount = *amount; - h[total].row_id = *row_id; - h[total].details.reserve_pub = *reserve_pub; - h[total].details.credit_account_url = account_url; - TALER_LOG_INFO ("+1-bit of my history\n"); - total++; - } - } + GNUNET_array_grow (h, + total, + pos); + if (0 == pos) + TALER_LOG_DEBUG ("Empty credit history computed\n"); *rh = h; return total; } diff --git a/src/lib/testing_api_cmd_bank_history_debit.c b/src/lib/testing_api_cmd_bank_history_debit.c index 6ac54e2ca..a45c6da89 100644 --- a/src/lib/testing_api_cmd_bank_history_debit.c +++ b/src/lib/testing_api_cmd_bank_history_debit.c @@ -149,9 +149,9 @@ print_expected (struct History *h, unsigned int off) { GNUNET_log (GNUNET_ERROR_TYPE_ERROR, - "Transaction history missmatch at position %u/%llu\n", + "Transaction history missmatch at position %u/%u\n", off, - (unsigned long long) h_len); + h_len); GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Expected history:\n"); for (unsigned int i = 0; i