diff options
author | Christian Grothoff <christian@grothoff.org> | 2015-06-30 21:25:26 +0200 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2015-06-30 21:25:26 +0200 |
commit | de430aa4646312b0f68b4e03fb1c8f557ef99c2b (patch) | |
tree | 62982f70157a4fb4ca67bd9648b3cf25806d14c8 /src/mintdb/perf_taler_mintdb_interpreter.c | |
parent | c154e50148794f498259b835d9ac064660e769aa (diff) | |
parent | d0a6b47099384e39e820687fe7b83628514a6382 (diff) | |
download | exchange-de430aa4646312b0f68b4e03fb1c8f557ef99c2b.tar.xz |
Merge branch 'master' of git+ssh://git.taler.net/var/git/mint
Diffstat (limited to 'src/mintdb/perf_taler_mintdb_interpreter.c')
-rw-r--r-- | src/mintdb/perf_taler_mintdb_interpreter.c | 340 |
1 files changed, 206 insertions, 134 deletions
diff --git a/src/mintdb/perf_taler_mintdb_interpreter.c b/src/mintdb/perf_taler_mintdb_interpreter.c index d7d3a6d00..5a1ec2921 100644 --- a/src/mintdb/perf_taler_mintdb_interpreter.c +++ b/src/mintdb/perf_taler_mintdb_interpreter.c @@ -47,9 +47,10 @@ struct PERF_TALER_MINTDB_interpreter_state /** * The current index of the interpreter */ - int i; + unsigned int i; }; + /** * Free the memory of @a data, with data of type @a type */ @@ -86,8 +87,7 @@ data_free (union PERF_TALER_MINTDB_Data *data, enum PERF_TALER_MINTDB_Type type) default: return; } -} - +} /** @@ -99,7 +99,7 @@ data_free (union PERF_TALER_MINTDB_Data *data, enum PERF_TALER_MINTDB_Type type) static int cmd_find (const struct PERF_TALER_MINTDB_Cmd *cmd, const char *search) { - int i; + unsigned int i; for (i=0; PERF_TALER_MINTDB_CMD_END != cmd[i].command; i++) if (0 == strcmp (cmd[i].label, search)) @@ -109,39 +109,52 @@ cmd_find (const struct PERF_TALER_MINTDB_Cmd *cmd, const char *search) /** - * Initialization of a command array + * Initialization of a command array */ static int cmd_init (struct PERF_TALER_MINTDB_Cmd cmd[]) { - int i = 0; + unsigned int i = 0; for (i=0; PERF_TALER_MINTDB_CMD_END != cmd[i].command; i++) { switch (cmd[i].command) { case PERF_TALER_MINTDB_CMD_SAVE_ARRAY: - // Allocation of memory for saving data - cmd[i].details.save_array.data_saved = - GNUNET_new_array (cmd[i].details.save_array.nb_saved, + { + int save_label; + + GNUNET_assert (GNUNET_SYSERR != + (save_label = cmd_find (cmd, + cmd[i].details.save_array.label_save))); + /* Allocation of memory for saving data */ + cmd[i].details.save_array.data_saved = + GNUNET_new_array (cmd[i].details.save_array.nb_saved, union PERF_TALER_MINTDB_Data); + /* Getting the type saved from the given label */ + cmd[i].details.save_array.type_saved = + cmd[save_label].exposed_type; + } + break; case PERF_TALER_MINTDB_CMD_LOAD_ARRAY: - // Creating the permutation array to randomize the data order - GNUNET_assert (NULL != - (cmd[i].details.load_array.permutation = - GNUNET_CRYPTO_random_permute ( - GNUNET_CRYPTO_QUALITY_WEAK, - cmd[cmd_find (cmd, - cmd[i].details - .load_array.label_save)] - .details.save_array.nb_saved))); - - // Initializing the type based on the type of the saved array - cmd[i].exposed_type = cmd[cmd_find (cmd, - cmd[i].details.load_array.label_save)] - .details.save_array.type_saved; + /* Creating the permutation array to randomize the data order */ + { + int save_index ; + + GNUNET_assert (GNUNET_SYSERR != + (save_index = cmd_find ( + cmd, + cmd[i].details.load_array.label_save))); + GNUNET_assert (NULL != + (cmd[i].details.load_array.permutation = + GNUNET_CRYPTO_random_permute ( + GNUNET_CRYPTO_QUALITY_WEAK, + cmd[save_index].details.save_array.nb_saved))); + /* Initializing the type based on the type of the saved array */ + cmd[i].exposed_type = cmd[save_index].details.save_array.type_saved; + } break; default: @@ -158,7 +171,7 @@ cmd_init (struct PERF_TALER_MINTDB_Cmd cmd[]) static int cmd_clean (struct PERF_TALER_MINTDB_Cmd cmd[]) { - int i = 0; + unsigned int i = 0; for (i = 0; PERF_TALER_MINTDB_CMD_END != cmd[i].command; i++) { @@ -166,7 +179,7 @@ cmd_clean (struct PERF_TALER_MINTDB_Cmd cmd[]) { case PERF_TALER_MINTDB_CMD_SAVE_ARRAY: { - int j; + unsigned int j; for (j = 0; j < cmd[i].details.save_array.nb_saved; j++) { data_free (&cmd[i].details.save_array.data_saved[j], @@ -198,30 +211,29 @@ cmd_clean (struct PERF_TALER_MINTDB_Cmd cmd[]) static void interpret_end_loop (struct PERF_TALER_MINTDB_interpreter_state *state) { - int i; + unsigned int i; union PERF_TALER_MINTDB_Data zero = {0}; - int jump = cmd_find (state->cmd, - state->cmd[state->i].details.end_loop.label_loop); + int jump; + GNUNET_assert (GNUNET_SYSERR != + (jump = cmd_find (state->cmd, + state->cmd[state->i] + .details.end_loop.label_loop))); // Cleaning up the memory in the loop for (i = jump; i < state->i; i++) { - // If the exposed variable has not been copied it is freed - if ( GNUNET_NO == state->cmd[i].exposed_saved) - data_free (&state->cmd[i].exposed, state->cmd[i].exposed_type); - state->cmd[i].exposed_saved = GNUNET_NO; - // Anyway we need to make the data zero. + data_free (&state->cmd[i].exposed, state->cmd[i].exposed_type); state->cmd[i].exposed = zero; } state->cmd[jump].details.loop.curr_iteration++; - // If the loop is not finished - if (state->cmd[jump].details.loop.max_iterations > + /* If the loop is not finished */ + if (state->cmd[jump].details.loop.max_iterations > state->cmd[jump].details.loop.curr_iteration) { - // jump back to the start + /* jump back to the start */ state->i = jump; }else{ - // Reset the loop counter and continue running + /* Reset the loop counter and continue running */ state->cmd[jump].details.loop.curr_iteration = 0; } } @@ -234,37 +246,40 @@ interpret_end_loop (struct PERF_TALER_MINTDB_interpreter_state *state) static void interpret_save_array (struct PERF_TALER_MINTDB_interpreter_state *state) { - int loop_index; - int selection_chance; - - // Array initialization on first loop iteration - // Alows for nested loops - if (0 == state->cmd[cmd_find (state->cmd, - state->cmd[state->i] - .details.save_array.label_loop)] - .details.loop.curr_iteration) + int loop_index, save_index; + unsigned int selection_chance; + + GNUNET_assert (GNUNET_SYSERR != + (loop_index = cmd_find (state->cmd, + state->cmd[state->i] + .details.save_array.label_loop))); + GNUNET_assert (GNUNET_SYSERR != + (save_index = cmd_find (state->cmd, + state->cmd[state->i] + .details.save_array.label_save))); + /* Array initialization on first loop iteration + Alows for nested loops */ + if (0 == state->cmd[loop_index].details.loop.curr_iteration) { state->cmd[state->i].details.save_array.index = 0; } - loop_index = cmd_find (state->cmd, - state->cmd[state->i].details.save_array.label_loop); - // The probobility distribution of the saved items will be a little biased - // against the few last items but it should not be a big problem. + /* The probobility distribution of the saved items will be a little biased + against the few last items but it should not be a big problem. */ selection_chance = state->cmd[loop_index].details.loop.max_iterations / state->cmd[state->i].details.save_array.nb_saved; /* - * If the remaining sapce is equal to the remaining number of + * If the remaining space is equal to the remaining number of * iterations, the item is automaticly saved. * - * Else it is saved only if rdn is 0 + * Else it is saved only if the random numbre generated is 0 */ - if ((0 < (state->cmd[state->i].details.save_array.nb_saved - + if ((0 < (state->cmd[state->i].details.save_array.nb_saved - state->cmd[state->i].details.save_array.index)) && - (((state->cmd[loop_index].details.loop.max_iterations - + (((state->cmd[loop_index].details.loop.max_iterations - state->cmd[loop_index].details.loop.curr_iteration) == - (state->cmd[state->i].details.save_array.nb_saved - + (state->cmd[state->i].details.save_array.nb_saved - state->cmd[state->i].details.save_array.index)) - || (0 == GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, + || (0 == GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, selection_chance)))) { union PERF_TALER_MINTDB_Data *save_location; @@ -272,10 +287,7 @@ interpret_save_array (struct PERF_TALER_MINTDB_interpreter_state *state) save_location = &state->cmd[state->i].details.save_array .data_saved[state->cmd[state->i].details.save_array.index]; - item_saved = &state->cmd[cmd_find (state->cmd, - state->cmd[state->i] - .details.save_array.label_save)] - .exposed; + item_saved = &state->cmd[save_index].exposed; switch (state->cmd[state->i].details.save_array.type_saved) { case PERF_TALER_MINTDB_TIME: @@ -283,19 +295,19 @@ interpret_save_array (struct PERF_TALER_MINTDB_interpreter_state *state) break; case PERF_TALER_MINTDB_DEPOSIT: - save_location->deposit = item_saved->deposit; + save_location->deposit = PERF_TALER_MINTDB_deposit_copy (item_saved->deposit); break; case PERF_TALER_MINTDB_BLINDCOIN: - save_location->blindcoin = item_saved->blindcoin; + save_location->blindcoin = PERF_TALER_MINTDB_collectable_blindcoin_copy (item_saved->blindcoin); break; case PERF_TALER_MINTDB_RESERVE: - save_location->reserve = item_saved->reserve; + save_location->reserve = PERF_TALER_MINTDB_reserve_copy (item_saved->reserve); break; case PERF_TALER_MINTDB_DENOMINATION_INFO: - save_location->dki = item_saved->dki; + save_location->dki = PERF_TALER_MINTDB_denomination_copy (item_saved->dki); break; case PERF_TALER_MINTDB_COIN_INFO: @@ -305,32 +317,45 @@ interpret_save_array (struct PERF_TALER_MINTDB_interpreter_state *state) default: break; } - state->cmd[cmd_find (state->cmd, - state->cmd[state->i].details.save_array.label_save)] - .exposed_saved = GNUNET_YES; state->cmd[state->i].details.save_array.index++; } } +/** + * Run when the current command is LOAD_ARRAY + * Get data from a SAVE_ARRAY and exposes a copy + */ static void interpret_load_array (struct PERF_TALER_MINTDB_interpreter_state *state) { + unsigned int loop_iter; int loop_index, save_index; - union PERF_TALER_MINTDB_Data zero = {0}; union PERF_TALER_MINTDB_Data *loaded_data; - loop_index = cmd_find (state->cmd, - state->cmd[state->i].details.load_array.label_loop); - save_index = cmd_find (state->cmd, - state->cmd[state->i].details.load_array.label_save); + GNUNET_assert (GNUNET_SYSERR != + (loop_index = cmd_find (state->cmd, + state->cmd[state->i] + .details.load_array.label_loop))); + GNUNET_assert (GNUNET_SYSERR != + (save_index = cmd_find (state->cmd, + state->cmd[state->i] + .details.load_array.label_save))); + loop_iter = state->cmd[loop_index].details.loop.curr_iteration; + { + int i, quotient; + + /* in case the iteration number is higher than the amount saved, + * the number is run several times in the permutation array */ + quotient = loop_iter / state->cmd[save_index].details.save_array.nb_saved; + loop_iter = loop_iter % state->cmd[save_index].details.save_array.nb_saved; + for (i=0; i<=quotient; i++) + loop_iter = state->cmd[state->i].details.load_array.permutation[loop_iter]; + } /* Extracting the data from the loop_indexth indice in save_index * array. */ - loaded_data = &state->cmd[save_index].details.save_array.data_saved[ - state->cmd[state->i].details.load_array.permutation[ - state->cmd[loop_index].details.loop.curr_iteration]]; - + loaded_data = &state->cmd[save_index].details.save_array.data_saved[loop_iter]; switch (state->cmd[state->i].exposed_type) { case PERF_TALER_MINTDB_TIME: @@ -338,31 +363,37 @@ interpret_load_array (struct PERF_TALER_MINTDB_interpreter_state *state) break; case PERF_TALER_MINTDB_DEPOSIT: - state->cmd[state->i].exposed.deposit = loaded_data->deposit; + state->cmd[state->i].exposed.deposit = + PERF_TALER_MINTDB_deposit_copy (loaded_data->deposit); break; case PERF_TALER_MINTDB_BLINDCOIN: - state->cmd[state->i].exposed.blindcoin = loaded_data->blindcoin; + state->cmd[state->i].exposed.blindcoin = + PERF_TALER_MINTDB_collectable_blindcoin_copy (loaded_data->blindcoin); break; case PERF_TALER_MINTDB_RESERVE: - state->cmd[state->i].exposed.reserve = loaded_data->reserve; + state->cmd[state->i].exposed.reserve = + PERF_TALER_MINTDB_reserve_copy (loaded_data->reserve); break; case PERF_TALER_MINTDB_DENOMINATION_INFO: - state->cmd[state->i].exposed.dki = loaded_data->dki; + state->cmd[state->i].exposed.dki = + PERF_TALER_MINTDB_denomination_copy (loaded_data->dki); + break; case PERF_TALER_MINTDB_COIN_INFO: state->cmd[state->i].exposed.cpi = loaded_data->cpi; + break; + default: break; } - *loaded_data = zero; } /** * Main interpreter loop. - * + * */ static int interpret (struct PERF_TALER_MINTDB_interpreter_state *state) @@ -376,7 +407,7 @@ interpret (struct PERF_TALER_MINTDB_interpreter_state *state) return GNUNET_YES; case PERF_TALER_MINTDB_CMD_DEBUG: - printf ("%s\n", state->cmd[state->i].label); + GNUNET_log (GNUNET_ERROR_TYPE_INFO, "%s\n", state->cmd[state->i].label); break; case PERF_TALER_MINTDB_CMD_LOOP: @@ -392,22 +423,26 @@ interpret (struct PERF_TALER_MINTDB_interpreter_state *state) case PERF_TALER_MINTDB_CMD_GAUGER: { - int start_index, stop_index; + int start_index, stop_index; struct timespec start, stop; unsigned long elapsed_ms; - start_index = cmd_find (state->cmd, - state->cmd[state->i].details.gauger.label_start); - stop_index = cmd_find (state->cmd, - state->cmd[state->i].details.gauger.label_stop); + GNUNET_assert (GNUNET_SYSERR != + (start_index = cmd_find (state->cmd, + state->cmd[state->i] + .details.gauger.label_start))); + GNUNET_assert (GNUNET_SYSERR != + (stop_index = cmd_find (state->cmd, + state->cmd[state->i] + .details.gauger.label_stop))); start = state->cmd[start_index].exposed.time; stop = state->cmd[stop_index].exposed.time; - elapsed_ms = (start.tv_sec - stop.tv_sec) * 1000 + + elapsed_ms = (start.tv_sec - stop.tv_sec) * 1000 + (start.tv_nsec - stop.tv_nsec) / 1000000; - GAUGER ("MINTDB", - state->cmd[state->i].details.gauger.description, - elapsed_ms, + GAUGER ("MINTDB", + state->cmd[state->i].details.gauger.description, + elapsed_ms / state->cmd[state->i].details.gauger.divide, "milliseconds"); } break; @@ -437,12 +472,19 @@ interpret (struct PERF_TALER_MINTDB_interpreter_state *state) case PERF_TALER_MINTDB_CMD_INSERT_DEPOSIT: { - struct TALER_MINTDB_Deposit *deposit = - PERF_TALER_MINTDB_deposit_init (); + int dki_index; + struct TALER_MINTDB_Deposit *deposit; + GNUNET_assert (GNUNET_SYSERR != + (dki_index = cmd_find(state->cmd, + state->cmd[state->i] + .details.insert_deposit.label_dki))); + GNUNET_assert (NULL != + (deposit = PERF_TALER_MINTDB_deposit_init ( + state->cmd[dki_index].exposed.dki))); GNUNET_assert ( - state->plugin->insert_deposit (state->plugin->cls, - state->session, + state->plugin->insert_deposit (state->plugin->cls, + state->session, deposit)); state->cmd[state->i].exposed.deposit = deposit; } @@ -450,25 +492,30 @@ interpret (struct PERF_TALER_MINTDB_interpreter_state *state) case PERF_TALER_MINTDB_CMD_GET_DEPOSIT: { - struct TALER_MINTDB_Deposit *deposit = - state->cmd[cmd_find (state->cmd, - state->cmd[state->i] - .details.get_deposit.label_source)] - .exposed.deposit; // Get the deposit from the source - - state->plugin->have_deposit (state->plugin->cls, - state->session, + int source_index; + struct TALER_MINTDB_Deposit *deposit; + + GNUNET_assert (GNUNET_SYSERR != + (source_index = cmd_find (state->cmd, + state->cmd[state->i] + .details.get_deposit.label_source))); + GNUNET_assert (NULL != + (deposit = state->cmd[source_index].exposed.deposit)); + state->plugin->have_deposit (state->plugin->cls, + state->session, deposit); } break; - + case PERF_TALER_MINTDB_CMD_INSERT_RESERVE: { struct TALER_MINTDB_Reserve *reserve; - json_t *details = json_pack ("si","justification", - GNUNET_CRYPTO_random_u32 ( - GNUNET_CRYPTO_QUALITY_WEAK, - UINT32_MAX)); + json_t *details = NULL; + GNUNET_assert (NULL != + (details = json_pack ("{s:i}","justification", + GNUNET_CRYPTO_random_u32 ( + GNUNET_CRYPTO_QUALITY_WEAK, + UINT32_MAX)))); reserve = PERF_TALER_MINTDB_reserve_init (); state->plugin->reserves_in_insert ( state->plugin->cls, @@ -476,7 +523,7 @@ interpret (struct PERF_TALER_MINTDB_interpreter_state *state) &reserve->pub, &reserve->balance, details - ); + ); json_decref (details); state->cmd[state->i].exposed.reserve = reserve; } @@ -484,17 +531,21 @@ interpret (struct PERF_TALER_MINTDB_interpreter_state *state) case PERF_TALER_MINTDB_CMD_GET_RESERVE: { - struct TALER_MINTDB_Reserve *reserve = - state->cmd[cmd_find (state->cmd, - state->cmd[state->i] - .details.get_reserve.label_source)] - .exposed.reserve; // Get the deposit from the source - - state->plugin->reserve_get (state->plugin->cls, - state->session, - reserve); + int source_index; + struct TALER_MINTDB_Reserve *reserve; + + GNUNET_assert (GNUNET_SYSERR != + (source_index = cmd_find (state->cmd, + state->cmd[state->i] + .details.get_reserve.label_source))); + GNUNET_assert (NULL != + (reserve = state->cmd[source_index].exposed.reserve)); + GNUNET_assert (GNUNET_OK == + (state->plugin->reserve_get (state->plugin->cls, + state->session, + reserve))); } - break; + break; case PERF_TALER_MINTDB_CMD_INSERT_DENOMINATION: { @@ -511,11 +562,15 @@ interpret (struct PERF_TALER_MINTDB_interpreter_state *state) case PERF_TALER_MINTDB_CMD_GET_DENOMINATION: { - struct TALER_MINTDB_DenominationKeyIssueInformation *dki = - state->cmd[cmd_find (state->cmd, - state->cmd[state->i] - .details.get_denomination.label_source)] - .exposed.dki; + int source_index; + struct TALER_MINTDB_DenominationKeyIssueInformation *dki; + + GNUNET_assert (GNUNET_SYSERR != + (source_index = cmd_find (state->cmd, + state->cmd[state->i] + .details.get_denomination.label_source))); + GNUNET_assert (NULL != + (dki = state->cmd[source_index].exposed.dki)); state->plugin->get_denomination_info (state->plugin->cls, state->session, &dki->denom_pub, @@ -525,8 +580,22 @@ interpret (struct PERF_TALER_MINTDB_interpreter_state *state) case PERF_TALER_MINTDB_CMD_INSERT_WITHDRAW: { - struct TALER_MINTDB_CollectableBlindcoin *blindcoin = - PERF_TALER_MINTDB_collectable_blindcoin_init (); + int dki_index, reserve_index; + struct TALER_MINTDB_CollectableBlindcoin *blindcoin ; + + GNUNET_assert (GNUNET_SYSERR != + (dki_index = cmd_find ( + state->cmd, + state->cmd[state->i].details.insert_withdraw.label_dki))); + GNUNET_assert (GNUNET_SYSERR != + (reserve_index = cmd_find ( + state->cmd, + state->cmd[state->i].details.insert_withdraw.label_reserve))); + GNUNET_assert (NULL != + (blindcoin = + PERF_TALER_MINTDB_collectable_blindcoin_init ( + state->cmd[dki_index].exposed.dki, + state->cmd[reserve_index].exposed.reserve))); state->plugin->insert_withdraw_info (state->plugin->cls, state->session, @@ -537,12 +606,15 @@ interpret (struct PERF_TALER_MINTDB_interpreter_state *state) case PERF_TALER_MINTDB_CMD_GET_WITHDRAW: { - struct TALER_MINTDB_CollectableBlindcoin *blindcoin = - state->cmd[cmd_find (state->cmd, - state->cmd[state->i] - .details.get_denomination.label_source)] - .exposed.blindcoin; - + int source_index; + struct TALER_MINTDB_CollectableBlindcoin *blindcoin ; + + GNUNET_assert (GNUNET_SYSERR != + (source_index = cmd_find (state->cmd, + state->cmd[state->i] + .details.get_denomination.label_source))); + GNUNET_assert (NULL != + (blindcoin = state->cmd[source_index].exposed.blindcoin)); state->plugin->get_withdraw_info (state->plugin->cls, state->session, &blindcoin->h_coin_envelope, @@ -566,7 +638,7 @@ int PERF_TALER_MINTDB_interpret (struct TALER_MINTDB_Plugin *db_plugin, struct PERF_TALER_MINTDB_Cmd cmd[]) { - struct PERF_TALER_MINTDB_interpreter_state state = + struct PERF_TALER_MINTDB_interpreter_state state = {.i = 0, .cmd = cmd, .plugin = db_plugin}; // Initializing commands |