diff --git a/dap-sdk b/dap-sdk index e66e1b75e024988a9391dc0ba58694af1813c796..3ef80ce7eefc7777c7fbb94101419318e7c59fce 160000 --- a/dap-sdk +++ b/dap-sdk @@ -1 +1 @@ -Subproject commit e66e1b75e024988a9391dc0ba58694af1813c796 +Subproject commit 3ef80ce7eefc7777c7fbb94101419318e7c59fce diff --git a/modules/chain/dap_chain.c b/modules/chain/dap_chain.c index 88288bb8d8566b27f791c7dc647c2d61330b4a77..4119cdd53186a06eede64983bf0a8eb0a5fdf513 100644 --- a/modules/chain/dap_chain.c +++ b/modules/chain/dap_chain.c @@ -53,12 +53,12 @@ typedef struct dap_chain_item_id { typedef struct dap_chain_item { dap_chain_item_id_t item_id; - dap_chain_t * chain; - UT_hash_handle hh; + dap_chain_t *chain; + UT_hash_handle hh; } dap_chain_item_t; static pthread_rwlock_t s_chain_items_rwlock = PTHREAD_RWLOCK_INITIALIZER; -static dap_chain_item_t * s_chain_items = NULL; +static dap_chain_item_t *s_chain_items = NULL; int s_prepare_env(); @@ -103,29 +103,36 @@ void dap_chain_deinit(void) */ dap_chain_t *dap_chain_create(const char *a_chain_net_name, const char *a_chain_name, dap_chain_net_id_t a_chain_net_id, dap_chain_id_t a_chain_id) { - dap_chain_t * l_ret = DAP_NEW_Z(dap_chain_t); + dap_chain_t *l_ret = DAP_NEW_Z(dap_chain_t); if ( !l_ret ) { log_it(L_CRITICAL, "Memory allocation error"); return NULL; } + *l_ret = (dap_chain_t) { + .rwlock = PTHREAD_RWLOCK_INITIALIZER, + .id = a_chain_id, + .net_id = a_chain_net_id, + .name = dap_strdup(a_chain_name), + .net_name = dap_strdup(a_chain_net_name), + .cell_rwlock = PTHREAD_RWLOCK_INITIALIZER, + .atom_notifiers = NULL + }; DAP_CHAIN_PVT_LOCAL_NEW(l_ret); - memcpy(l_ret->id.raw,a_chain_id.raw,sizeof(a_chain_id)); - memcpy(l_ret->net_id.raw,a_chain_net_id.raw,sizeof(a_chain_net_id)); - l_ret->name = strdup (a_chain_name); - l_ret->net_name = strdup (a_chain_net_name); - pthread_rwlock_init(&l_ret->rwlock, NULL); - pthread_rwlock_init(&l_ret->cell_rwlock,NULL); - dap_chain_item_t * l_ret_item = DAP_NEW_Z(dap_chain_item_t); - if ( !l_ret_item ){ + + dap_chain_item_t *l_ret_item = DAP_NEW_Z(dap_chain_item_t); + if (!l_ret_item) { + DAP_DELETE(l_ret->name); + DAP_DELETE(l_ret->net_name); DAP_DELETE(l_ret); log_it(L_CRITICAL, "Memory allocation error"); return NULL; } - l_ret_item->chain = l_ret; - memcpy(l_ret_item->item_id.id.raw ,a_chain_id.raw,sizeof(a_chain_id)); - memcpy(l_ret_item->item_id.net_id.raw ,a_chain_net_id.raw,sizeof(a_chain_net_id)); + *l_ret_item = (dap_chain_item_t) { + .item_id = { a_chain_id, a_chain_net_id }, + .chain = l_ret + }; pthread_rwlock_wrlock(&s_chain_items_rwlock); - HASH_ADD(hh,s_chain_items,item_id,sizeof(dap_chain_item_id_t),l_ret_item); + HASH_ADD(hh, s_chain_items, item_id, sizeof(dap_chain_item_id_t), l_ret_item); pthread_rwlock_unlock(&s_chain_items_rwlock); return l_ret; } @@ -139,41 +146,35 @@ void dap_chain_delete(dap_chain_t * a_chain) { dap_chain_item_t * l_item = NULL; dap_chain_item_id_t l_chain_item_id = { - .id = a_chain->id, + .id = a_chain->id, .net_id = a_chain->net_id, }; - dap_list_free_full(a_chain->atom_notifiers,NULL); - pthread_rwlock_wrlock(&s_chain_items_rwlock); - HASH_FIND(hh,s_chain_items,&l_chain_item_id,sizeof(dap_chain_item_id_t),l_item); - - if( l_item){ + HASH_FIND(hh, s_chain_items, &l_chain_item_id, sizeof(dap_chain_item_id_t), l_item); + if (l_item) { HASH_DEL(s_chain_items, l_item); - if (a_chain->callback_delete ) - a_chain->callback_delete(a_chain); - if ( a_chain->name) - DAP_DELETE (a_chain->name); - if ( a_chain->net_name) - DAP_DELETE (a_chain->net_name); - if (a_chain->_pvt ){ - DAP_DELETE(DAP_CHAIN_PVT(a_chain)->file_storage_dir); - DAP_DELETE(a_chain->_pvt); - } - if (a_chain->_inheritor ) - DAP_DELETE(a_chain->_inheritor); DAP_DELETE(l_item); - }else - log_it(L_WARNING,"Trying to remove non-existent 0x%16"DAP_UINT64_FORMAT_X":0x%16"DAP_UINT64_FORMAT_X" chain",a_chain->id.uint64, - a_chain->net_id.uint64); - a_chain->datum_types_count = 0; + } else { + log_it(L_WARNING,"Trying to remove non-existent 0x%16"DAP_UINT64_FORMAT_X":0x%16"DAP_UINT64_FORMAT_X" chain", + a_chain->id.uint64, a_chain->net_id.uint64); + } + pthread_rwlock_unlock(&s_chain_items_rwlock); + dap_list_free_full(a_chain->atom_notifiers, NULL); + DAP_DEL_Z(a_chain->name); + DAP_DEL_Z(a_chain->net_name); + if (DAP_CHAIN_PVT(a_chain)){ + DAP_DEL_Z(DAP_CHAIN_PVT(a_chain)->file_storage_dir); + DAP_DELETE(DAP_CHAIN_PVT(a_chain)); + } DAP_DELETE(a_chain->datum_types); - a_chain->autoproc_datum_types_count = 0; DAP_DELETE(a_chain->autoproc_datum_types); + if (a_chain->callback_delete) + a_chain->callback_delete(a_chain); + DAP_DEL_Z(a_chain->_inheritor); pthread_rwlock_destroy(&a_chain->rwlock); pthread_rwlock_destroy(&a_chain->cell_rwlock); pthread_rwlock_destroy(&a_chain->rwlock); - pthread_rwlock_unlock(&s_chain_items_rwlock); } /** @@ -544,7 +545,7 @@ bool dap_chain_has_file_store(dap_chain_t * a_chain) * @param l_chain * @return */ -int dap_chain_save_all (dap_chain_t * l_chain) +int dap_chain_save_all(dap_chain_t *l_chain) { int l_ret = 0; pthread_rwlock_rdlock(&l_chain->cell_rwlock); @@ -591,8 +592,13 @@ int dap_chain_load_all(dap_chain_t *a_chain) l_ret += dap_chain_cell_load(a_chain, l_cell); if (DAP_CHAIN_PVT(a_chain)->need_reorder) { const char *l_filename_backup = dap_strdup_printf("%s.unsorted", l_cell->file_storage_path); - remove(l_filename_backup); - rename(l_cell->file_storage_path, l_filename_backup); + if (remove(l_filename_backup) == -1) { + log_it(L_ERROR, "File %s doesn't exist", l_filename_backup); + } + if (rename(l_cell->file_storage_path, l_filename_backup)) { + log_it(L_ERROR, "Couldn't rename %s to %s", l_cell->file_storage_path, l_filename_backup); + } + DAP_DELETE(l_filename_backup); } } } @@ -710,9 +716,10 @@ ssize_t dap_chain_atom_save(dap_chain_t *a_chain, const uint8_t *a_atom, size_t } ssize_t l_res = dap_chain_cell_file_append(l_cell, a_atom, a_atom_size); if (a_chain->atom_notifiers) { - for (dap_list_t *it = a_chain->atom_notifiers; it; it = it->next) { - dap_chain_atom_notifier_t *l_notifier = (dap_chain_atom_notifier_t *)it->data; - l_notifier->callback(l_notifier->arg, a_chain, l_cell->id, (void *)a_atom, a_atom_size); + dap_list_t *l_iter; + DL_FOREACH(a_chain->atom_notifiers, l_iter) { + dap_chain_atom_notifier_t *l_notifier = (dap_chain_atom_notifier_t*)l_iter->data; + l_notifier->callback(l_notifier->arg, a_chain, l_cell->id, (void*)a_atom, a_atom_size); } } if (a_chain->callback_atom_add_from_treshold) { @@ -721,8 +728,10 @@ ssize_t dap_chain_atom_save(dap_chain_t *a_chain, const uint8_t *a_atom, size_t size_t l_atom_treshold_size; l_atom_treshold = a_chain->callback_atom_add_from_treshold(a_chain, &l_atom_treshold_size); if (l_atom_treshold) { - dap_chain_cell_file_append(l_cell, l_atom_treshold, l_atom_treshold_size); - log_it(L_INFO, "Added atom from treshold"); + if (dap_chain_cell_file_append(l_cell, l_atom_treshold, l_atom_treshold_size) > 0) + log_it(L_INFO, "Added atom from treshold"); + else + log_it(L_ERROR, "Can't add atom from treshold"); } } while(l_atom_treshold); } diff --git a/modules/chain/dap_chain_cell.c b/modules/chain/dap_chain_cell.c index 623c0eee872cff38532e354cdffa67810d3f7725..290f4d9a0eb7429cd31c1c4cddbe47da6aec8bc8 100644 --- a/modules/chain/dap_chain_cell.c +++ b/modules/chain/dap_chain_cell.c @@ -170,7 +170,21 @@ void dap_chain_cell_delete(dap_chain_cell_t *a_cell) a_cell->chain = NULL; a_cell->file_storage_path[0] = '\0'; pthread_rwlock_destroy(&a_cell->storage_rwlock); - DAP_DEL_Z(a_cell); + DAP_DELETE(a_cell); +} + +void dap_chain_cell_delete_all(dap_chain_t *a_chain) { + if (!a_chain) + return; + pthread_rwlock_wrlock(&a_chain->cell_rwlock); + dap_chain_cell_t *l_cell, *l_tmp; + HASH_ITER(hh, a_chain->cells, l_cell, l_tmp) { + dap_chain_cell_close(l_cell); + HASH_DEL(a_chain->cells, l_cell); + pthread_rwlock_destroy(&l_cell->storage_rwlock); + DAP_DELETE(l_cell); + } + pthread_rwlock_unlock(&a_chain->cell_rwlock); } /** @@ -243,42 +257,58 @@ int dap_chain_cell_load(dap_chain_t *a_chain, dap_chain_cell_t *a_cell) } -static bool s_file_write_header(dap_chain_cell_t *a_cell) +static int s_file_write_header(dap_chain_cell_t *a_cell) { - fseek(a_cell->file_storage, 0L, SEEK_SET); + if (!a_cell->file_storage) { + log_it(L_ERROR, "Chain cell \"%s\" 0x%016"DAP_UINT64_FORMAT_X" not opened", + a_cell->file_storage_path, a_cell->id.uint64); + return -2; + } else { + fseek(a_cell->file_storage, 0L, SEEK_END); + if (ftell(a_cell->file_storage) > (ssize_t)sizeof(dap_chain_cell_file_header_t)) { + log_it(L_ERROR, "Chain cell \"%s\" 0x%016"DAP_UINT64_FORMAT_X" is already not empty!", + a_cell->file_storage_path, a_cell->id.uint64); + return -3; + } + } dap_chain_cell_file_header_t l_hdr = { - .signature = DAP_CHAIN_CELL_FILE_SIGNATURE, - .version = DAP_CHAIN_CELL_FILE_VERSION, - .type = DAP_CHAIN_CELL_FILE_TYPE_RAW, - .chain_id = { .uint64 = a_cell->id.uint64 }, - .chain_net_id = a_cell->chain->net_id + .signature = DAP_CHAIN_CELL_FILE_SIGNATURE, + .version = DAP_CHAIN_CELL_FILE_VERSION, + .type = DAP_CHAIN_CELL_FILE_TYPE_RAW, + .chain_id = { .uint64 = a_cell->id.uint64 }, + .chain_net_id = a_cell->chain->net_id }; - if(fwrite(&l_hdr, 1, sizeof(l_hdr), a_cell->file_storage) == sizeof(l_hdr)) { + if(fwrite(&l_hdr, sizeof(l_hdr), 1, a_cell->file_storage) == 1) { log_it(L_NOTICE, "Initialized file storage for cell 0x%016"DAP_UINT64_FORMAT_X" ( %s )", a_cell->id.uint64, a_cell->file_storage_path); + fflush(a_cell->file_storage); + return 0; } else { log_it(L_ERROR, "Can't init file storage for cell 0x%016"DAP_UINT64_FORMAT_X" ( %s )", a_cell->id.uint64, a_cell->file_storage_path); - return false; + return -1; } - return true; } -static ssize_t s_file_atom_add(dap_chain_cell_t *a_cell, dap_chain_atom_ptr_t a_atom, uint64_t a_atom_size) +static int s_file_atom_add(dap_chain_cell_t *a_cell, dap_chain_atom_ptr_t a_atom, uint64_t a_atom_size) { - if (fwrite(&a_atom_size, 1, sizeof(a_atom_size), a_cell->file_storage) != sizeof(a_atom_size)) { - log_it (L_ERROR,"Can't write atom data size from cell 0x%016"DAP_UINT64_FORMAT_X" in \"%s\"", + if (!a_atom || !a_atom_size) { + log_it(L_CRITICAL, "Invalid arguments"); + return -1; + } + if (fwrite(&a_atom_size, sizeof(a_atom_size), 1, a_cell->file_storage) != 1) { + log_it (L_ERROR, "Can't write atom data size from cell 0x%016"DAP_UINT64_FORMAT_X" in \"%s\"", a_cell->id.uint64, a_cell->file_storage_path); return -2; } - if (fwrite(a_atom, 1, a_atom_size, a_cell->file_storage) != a_atom_size) { + if (fwrite(a_atom, a_atom_size, 1, a_cell->file_storage) != 1) { log_it (L_ERROR, "Can't write data from cell 0x%016"DAP_UINT64_FORMAT_X" to the file \"%s\"", a_cell->id.uint64, a_cell->file_storage_path); return -3; } - return a_atom_size + sizeof(a_atom_size); + return 0; } /** @@ -293,7 +323,7 @@ static ssize_t s_file_atom_add(dap_chain_cell_t *a_cell, dap_chain_atom_ptr_t a_ * @param a_atom_size * @return */ -int dap_chain_cell_file_append(dap_chain_cell_t *a_cell, const void *a_atom, size_t a_atom_size) +ssize_t dap_chain_cell_file_append(dap_chain_cell_t *a_cell, const void *a_atom, size_t a_atom_size) { if(!a_cell) return -1; @@ -302,69 +332,84 @@ int dap_chain_cell_file_append(dap_chain_cell_t *a_cell, const void *a_atom, siz a_cell->id.uint64, a_cell->file_storage_path); return -1; } - + size_t l_total_res = 0, l_count = 0; + bool l_err = false; pthread_rwlock_wrlock(&a_cell->storage_rwlock); - if (!a_cell->file_storage) { - a_cell->file_storage = fopen(a_cell->file_storage_path, "r+b"); + if (!a_atom || !a_atom_size) { + a_cell->file_storage = a_cell->file_storage + ? freopen(a_cell->file_storage_path, "w+b", a_cell->file_storage) + : fopen(a_cell->file_storage_path, "w+b"); if (!a_cell->file_storage) { - log_it(L_INFO, "Create chain cell"); - a_cell->file_storage = fopen(a_cell->file_storage_path, "w+b"); - if (!a_cell->file_storage) { - log_it(L_ERROR, "Chain cell \"%s\" (0x%016"DAP_UINT64_FORMAT_X") cannot be created", - a_cell->file_storage_path, - a_cell->id.uint64); - pthread_rwlock_unlock(&a_cell->storage_rwlock); - return -3; - } else if (!s_file_write_header(a_cell)) {// fill the header - pthread_rwlock_unlock(&a_cell->storage_rwlock); - return -4; - } + log_it(L_ERROR, "Chain cell \"%s\" 0x%016"DAP_UINT64_FORMAT_X" cannot be opened", + a_cell->file_storage_path, + a_cell->id.uint64); + pthread_rwlock_unlock(&a_cell->storage_rwlock); + return -3; } - } - - fseek(a_cell->file_storage, 0L, SEEK_END); - if (ftell(a_cell->file_storage) < (long)sizeof(dap_chain_cell_file_header_t)) { - log_it(L_WARNING, "Corrupted chain file header, rewrite it"); - if (!s_file_write_header(a_cell)) { + if (s_file_write_header(a_cell)) { + log_it(L_ERROR, "Chain cell \"%s\" 0x%016"DAP_UINT64_FORMAT_X": can't fill header", a_cell->file_storage_path, a_cell->id.uint64); pthread_rwlock_unlock(&a_cell->storage_rwlock); - return -5; + return -4; } - } - - size_t l_total_wrote_bytes = 0, l_count = 0; - if (a_atom && a_atom_size) - l_total_wrote_bytes = s_file_atom_add(a_cell, a_atom, a_atom_size); - else { // if no atom provided in arguments, we flush all the atoms in given chain - size_t l_atom_size = 0; - fseek(a_cell->file_storage, sizeof(dap_chain_cell_file_header_t), SEEK_SET); dap_chain_atom_iter_t *l_atom_iter = a_cell->chain->callback_atom_iter_create(a_cell->chain, a_cell->id, 0); - dap_chain_atom_ptr_t l_atom = a_cell->chain->callback_atom_iter_get_first(l_atom_iter, &l_atom_size); - while (l_atom && l_atom_size && ++l_count) { - ssize_t l_atom_added_size = s_file_atom_add(a_cell, l_atom, l_atom_size); - if (l_atom_added_size < 0) { - l_total_wrote_bytes = l_atom_added_size; + dap_chain_atom_ptr_t l_atom; + uint64_t l_atom_size = 0; + for (l_atom = a_cell->chain->callback_atom_iter_get_first(l_atom_iter, &l_atom_size); + l_atom && l_atom_size; + l_atom = a_cell->chain->callback_atom_iter_get_next(l_atom_iter, &l_atom_size)) + { + if (s_file_atom_add(a_cell, l_atom, l_atom_size)) { + l_err = true; break; + } else { + l_total_res += l_atom_size + sizeof(uint64_t); + ++l_count; } - l_total_wrote_bytes += l_atom_added_size; - l_atom = a_cell->chain->callback_atom_iter_get_next(l_atom_iter, &l_atom_size); } a_cell->chain->callback_atom_iter_delete(l_atom_iter); + a_cell->file_storage = freopen(a_cell->file_storage_path, "a+b", a_cell->file_storage); + } else { + if (!a_cell->file_storage) + a_cell->file_storage = fopen(a_cell->file_storage_path, "a+b"); + if (!a_cell->file_storage) { + log_it(L_ERROR, "Chain cell \"%s\" 0x%016"DAP_UINT64_FORMAT_X" cannot be opened", + a_cell->file_storage_path, + a_cell->id.uint64); + pthread_rwlock_unlock(&a_cell->storage_rwlock); + return -3; + } else { + fseek(a_cell->file_storage, 0L, SEEK_END); + if (!ftell(a_cell->file_storage)) { // It's not garunteed that header has been yet added or not, regardless the descriptor validity + if (s_file_write_header(a_cell)) { + log_it(L_ERROR, "Chain cell \"%s\" 0x%016"DAP_UINT64_FORMAT_X": can't fill header", a_cell->file_storage_path, a_cell->id.uint64); + pthread_rwlock_unlock(&a_cell->storage_rwlock); + return -4; + } + } + } + if (s_file_atom_add(a_cell, a_atom, a_atom_size)) { + log_it(L_ERROR, "Chain cell \"%s\" 0x%016"DAP_UINT64_FORMAT_X": can't save atom!", + a_cell->file_storage_path, a_cell->id.uint64); + pthread_rwlock_unlock(&a_cell->storage_rwlock); + return -4; + } + ++l_count; + l_total_res = a_atom_size + sizeof(uint64_t); } - if (l_total_wrote_bytes > 0) + if (l_total_res) { fflush(a_cell->file_storage); - if (!a_atom) { - if (l_total_wrote_bytes > 0) { - ftruncate(fileno(a_cell->file_storage), l_total_wrote_bytes + sizeof(dap_chain_cell_file_header_t)); - log_it(L_DEBUG, "Saved %zu atoms (total %zu bytes", l_count, l_total_wrote_bytes); - } else { - ftruncate(fileno(a_cell->file_storage), sizeof(dap_chain_cell_file_header_t)); - s_file_write_header(a_cell); - log_it(L_WARNING, "Nothing to save, event table is empty. Rewrite the file header"); + log_it(L_DEBUG, "Chain cell \"%s\" 0x%016"DAP_UINT64_FORMAT_X": saved %zu atoms (%zu bytes)", + a_cell->file_storage_path, a_cell->id.uint64, l_count, l_total_res); + if (l_err) { + log_it(L_WARNING, "Not all data was saved due to writing error!"); } + } else { + log_it(L_ERROR, "Chain cell \"%s\" 0x%016"DAP_UINT64_FORMAT_X": nothing saved!", + a_cell->file_storage_path, a_cell->id.uint64); } pthread_rwlock_unlock(&a_cell->storage_rwlock); - return (int)l_total_wrote_bytes; + return l_total_res; } /** @@ -373,7 +418,7 @@ int dap_chain_cell_file_append(dap_chain_cell_t *a_cell, const void *a_atom, siz * @param a_cell dap_chain_cell_t * @return */ -int dap_chain_cell_file_update( dap_chain_cell_t * a_cell) +ssize_t dap_chain_cell_file_update(dap_chain_cell_t *a_cell) { return dap_chain_cell_file_append(a_cell, NULL, 0); } diff --git a/modules/chain/dap_chain_ledger.c b/modules/chain/dap_chain_ledger.c index 4beffb11864342c0209529190581ac6f85a8c69f..a5ac3b20fe8a44818fd8744a4ab4e6e98d2988ba 100644 --- a/modules/chain/dap_chain_ledger.c +++ b/modules/chain/dap_chain_ledger.c @@ -576,10 +576,8 @@ static bool s_ledger_token_update_check(dap_chain_ledger_token_item_t *a_cur_tok //And getting lists of TSD sections with the removal and addition of certificates. int l_quantity_tsd_section_edit_signs_emission = 0; dap_tsd_t *l_tsd_signs_valid = NULL; - dap_list_t *l_tsd_list_remote_pkeys = NULL; - int l_quantity_tsd_remote_pkeys = 0; - dap_list_t *l_tsd_list_added_pkeys = NULL; - int l_quantity_tsd_add_pkeys = 0; + dap_list_t *l_tsd_list_remote_pkeys = NULL, *l_tsd_list_added_pkeys = NULL; + int l_quantity_tsd_remote_pkeys = 0, l_quantity_tsd_add_pkeys = 0; for (size_t l_tsd_offset = 0; l_tsd_offset < l_tsd_total_size; ) { dap_tsd_t *l_tsd = (dap_tsd_t*)((byte_t*)a_token_update->data_n_tsd + l_tsd_offset); size_t l_tsd_size = dap_tsd_size(l_tsd); @@ -614,8 +612,8 @@ static bool s_ledger_token_update_check(dap_chain_ledger_token_item_t *a_cur_tok log_it(L_ERROR, "Datum contains %ud TSD sections of type DAP_CHAIN_DATUM_TOKEN_TSD_TYPE_TOTAL_SIGNS_VALID which is not true. " "There can be at most one such TSD section.", l_quantity_tsd_section_edit_signs_emission); } - dap_list_free1(l_tsd_list_added_pkeys); - dap_list_free1(l_tsd_list_remote_pkeys); + dap_list_free(l_tsd_list_added_pkeys); + dap_list_free(l_tsd_list_remote_pkeys); return false; } //Check new count signs @@ -623,14 +621,14 @@ static bool s_ledger_token_update_check(dap_chain_ledger_token_item_t *a_cur_tok if (l_tsd_signs_valid) { size_t l_signs_valid_from_tsd = (size_t)(dap_tsd_get_scalar(l_tsd_signs_valid,uint16_t)); if (l_new_signs_total < l_signs_valid_from_tsd || l_signs_valid_from_tsd < 1) { - dap_list_free1(l_tsd_list_added_pkeys); - dap_list_free1(l_tsd_list_remote_pkeys); + dap_list_free(l_tsd_list_added_pkeys); + dap_list_free(l_tsd_list_remote_pkeys); return false; } } else { if (l_new_signs_total < auth_signs_valid){ - dap_list_free1(l_tsd_list_added_pkeys); - dap_list_free1(l_tsd_list_remote_pkeys); + dap_list_free(l_tsd_list_added_pkeys); + dap_list_free(l_tsd_list_remote_pkeys); return false; } } @@ -663,16 +661,16 @@ static bool s_ledger_token_update_check(dap_chain_ledger_token_item_t *a_cur_tok } } if (!isAccepted) { - dap_list_free1(l_tsd_list_added_pkeys); - dap_list_free1(l_tsd_list_remote_pkeys); + dap_list_free(l_tsd_list_added_pkeys); + dap_list_free(l_tsd_list_remote_pkeys); return false; } //Check added signs dap_chain_datum_token_t *l_token_tmp = DAP_DUP_SIZE(a_token_update, a_token_update_size); if (!l_token_tmp) { log_it(L_CRITICAL, "Memory allocation error"); - dap_list_free1(l_tsd_list_added_pkeys); - dap_list_free1(l_tsd_list_remote_pkeys); + dap_list_free(l_tsd_list_added_pkeys); + dap_list_free(l_tsd_list_remote_pkeys); return false; } @@ -712,8 +710,8 @@ static bool s_ledger_token_update_check(dap_chain_ledger_token_item_t *a_cur_tok break; } } - dap_list_free1(l_tsd_list_added_pkeys); - dap_list_free1(l_tsd_list_remote_pkeys); + dap_list_free(l_tsd_list_added_pkeys); + dap_list_free(l_tsd_list_remote_pkeys); DAP_DELETE(l_token_tmp); return isAccepted; } @@ -879,17 +877,18 @@ char * dap_ledger_token_tx_item_list(dap_ledger_t * a_ledger, dap_chain_addr_t * bool l_base_tx = false; const char *l_src_token = NULL; int l_src_subtype = DAP_CHAIN_TX_OUT_COND_SUBTYPE_UNDEFINED; - for (dap_list_t *it = l_list_in_items; it; it = it->next) { - assert(it->data); + dap_list_t *l_in_item; + DL_FOREACH(l_list_in_items, l_in_item) { + //assert(it->data); dap_chain_hash_fast_t *l_tx_prev_hash; int l_tx_prev_out_idx; dap_chain_datum_tx_t *l_tx_prev = NULL; - if (*(byte_t *)l_list_in_items->data == TX_ITEM_TYPE_IN) { - dap_chain_tx_in_t *l_tx_in = (dap_chain_tx_in_t *)l_list_in_items->data; + if (*(byte_t*)l_in_item->data == TX_ITEM_TYPE_IN) { + dap_chain_tx_in_t *l_tx_in = (dap_chain_tx_in_t*)l_in_item->data; l_tx_prev_hash = &l_tx_in->header.tx_prev_hash; l_tx_prev_out_idx = l_tx_in->header.tx_out_prev_idx; } else { // TX_ITEM_TYPE_IN_COND - dap_chain_tx_in_cond_t *l_tx_in_cond = (dap_chain_tx_in_cond_t *)l_list_in_items->data; + dap_chain_tx_in_cond_t *l_tx_in_cond = (dap_chain_tx_in_cond_t*)l_in_item->data; l_tx_prev_hash = &l_tx_in_cond->header.tx_prev_hash; l_tx_prev_out_idx = l_tx_in_cond->header.tx_out_prev_idx; } @@ -3494,16 +3493,22 @@ bool s_tx_match_sign(dap_chain_datum_token_emission_t *a_datum_emission, dap_cha return false; } -static int s_callback_sign_compare(const void *a, const void *b) +static int s_callback_sign_compare(const void *a_list_elem, const void *a_sign_elem) { - return !dap_pkey_match_sign((dap_pkey_t *)a, (dap_sign_t *)b); + dap_pkey_t* l_key = (dap_pkey_t*)((dap_list_t*)a_list_elem)->data; + dap_sign_t* l_sign = (dap_sign_t*)((dap_list_t*)a_sign_elem)->data; + if (!l_key || !l_sign) { + log_it(L_CRITICAL, "Invalid argument"); + return -1; + } + return !dap_pkey_match_sign(l_key, l_sign); } bool dap_chain_ledger_tx_poa_signed(dap_ledger_t *a_ledger, dap_chain_datum_tx_t *a_tx) { dap_chain_tx_sig_t *l_tx_sig = (dap_chain_tx_sig_t *)dap_chain_datum_tx_item_get(a_tx, NULL, TX_ITEM_TYPE_SIG, NULL); dap_sign_t *l_sign = dap_chain_datum_tx_item_sign_get_sig((dap_chain_tx_sig_t *)l_tx_sig); - return dap_list_find_custom(PVT(a_ledger)->poa_certs, l_sign, s_callback_sign_compare); + return dap_list_find(PVT(a_ledger)->poa_certs, l_sign, s_callback_sign_compare); } @@ -3840,9 +3845,9 @@ int dap_chain_ledger_tx_cache_check(dap_ledger_t *a_ledger, dap_chain_datum_tx_t break; } // 3. Compare out in previous transaction with currently used out - for (dap_list_t *it = l_list_bound_items; it; it = it->next) { - dap_chain_ledger_tx_bound_t *l_bound_tmp = it->data; - if (l_tx_prev_out == l_bound_tmp->out.tx_prev_out) { + dap_list_t *l_bound_item; + DL_FOREACH(l_list_bound_items, l_bound_item) { + if (l_tx_prev_out == ((dap_chain_ledger_tx_bound_t*)l_bound_item->data)->out.tx_prev_out) { debug_if(s_debug_more, L_ERROR, "Previous transaction output already used in current tx"); l_err_num = DAP_CHAIN_LEDGER_TX_CACHE_CHECK_PREV_OUT_ALREADY_USED_IN_CURRENT_TX; break; @@ -4731,12 +4736,12 @@ static inline int s_tx_add(dap_ledger_t *a_ledger, dap_chain_datum_tx_t *a_tx, d l_tx_item->tx = DAP_DUP_SIZE(a_tx, l_tx_size); l_tx_item->cache_data.ts_created = dap_time_now(); // Time of transasction added to ledger int l_outs_count = 0; - dap_list_t *l_tist_tmp = dap_chain_datum_tx_items_get(a_tx, TX_ITEM_TYPE_OUT_ALL, &l_outs_count); + dap_list_t *l_list_tmp2 = dap_chain_datum_tx_items_get(a_tx, TX_ITEM_TYPE_OUT_ALL, &l_outs_count); l_tx_item->cache_data.n_outs = l_outs_count; // TODO: dump the UTXO in debug mode if need - if(l_tist_tmp) - dap_list_free(l_tist_tmp); + if(l_list_tmp2) + dap_list_free(l_list_tmp2); dap_stpcpy(l_tx_item->cache_data.token_ticker, l_main_token_ticker); l_tx_item->cache_data.multichannel = l_multichannel; @@ -4746,13 +4751,15 @@ static inline int s_tx_add(dap_ledger_t *a_ledger, dap_chain_datum_tx_t *a_tx, d l_tx_item, s_sort_ledger_tx_item); // tx_hash_fast: name of key field if(a_safe_call) pthread_rwlock_unlock(&l_ledger_pvt->ledger_rwlock); // Callable callback - for (dap_list_t *notifier = PVT(a_ledger)->tx_add_notifiers; notifier != NULL; notifier = notifier->next) { - dap_chain_ledger_tx_notifier_t *l_notify = (dap_chain_ledger_tx_notifier_t *)notifier->data; + dap_list_t *l_notifier; + DL_FOREACH(PVT(a_ledger)->tx_add_notifiers, l_notifier) { + dap_chain_ledger_tx_notifier_t *l_notify = (dap_chain_ledger_tx_notifier_t*)l_notifier->data; l_notify->callback(l_notify->arg, a_ledger, l_tx_item->tx); } if (l_cross_network) { - for (dap_list_t *it = PVT(a_ledger)->bridged_tx_notificators; it; it = it->next) { - dap_chain_ledger_bridged_tx_notificator_t *l_notify = it->data; + dap_list_t *l_notifier; + DL_FOREACH(PVT(a_ledger)->bridged_tx_notificators, l_notifier) { + dap_chain_ledger_bridged_tx_notificator_t *l_notify = l_notifier->data; l_notify->callback(a_ledger, a_tx, a_tx_hash, l_notify->arg); } } @@ -5385,20 +5392,19 @@ const dap_chain_datum_tx_t* dap_chain_ledger_tx_find_by_pkey(dap_ledger_t *a_led * @param a_srv_uid * @return */ -dap_list_t* dap_chain_ledger_tx_cache_find_out_cond_all(dap_ledger_t *a_ledger,dap_chain_net_srv_uid_t a_srv_uid) +dap_list_t* dap_chain_ledger_tx_cache_find_out_cond_all(dap_ledger_t *a_ledger, dap_chain_net_srv_uid_t a_srv_uid) { dap_list_t * l_ret = NULL; dap_ledger_private_t *l_ledger_pvt = PVT(a_ledger); dap_chain_ledger_tx_item_t *l_iter_current = NULL, *l_item_tmp = NULL; HASH_ITER(hh, l_ledger_pvt->ledger_items, l_iter_current, l_item_tmp) { dap_chain_datum_tx_t *l_tx = l_iter_current->tx; - dap_list_t *l_list_out_items = dap_chain_datum_tx_items_get(l_tx, TX_ITEM_TYPE_OUT_COND, NULL); - for (dap_list_t *it = l_list_out_items; it; it = it->next) { - // Is present cond out - dap_chain_tx_out_cond_t *l_tx_out_cond = it->data; - if (l_tx_out_cond->header.srv_uid.uint64 == a_srv_uid.uint64) // is srv uid is same as we're searching for? - l_ret = dap_list_append(l_ret,l_tx); + dap_list_t *l_list_out_items = dap_chain_datum_tx_items_get(l_tx, TX_ITEM_TYPE_OUT_COND, NULL), *l_out_item; + DL_FOREACH(l_list_out_items, l_out_item) { + if (((dap_chain_tx_out_cond_t*)l_out_item->data)->header.srv_uid.uint64 == a_srv_uid.uint64) // is srv uid is same as we're searching for? + l_ret = dap_list_append(l_ret, l_tx); } + dap_list_free(l_list_out_items); } return l_ret; } @@ -5523,22 +5529,23 @@ dap_list_t *dap_chain_ledger_get_list_tx_outs_with_val(dap_ledger_t *a_ledger, c switch (l_type) { case TX_ITEM_TYPE_OUT_OLD: { dap_chain_tx_out_old_t *l_out = (dap_chain_tx_out_old_t *)l_list_tmp->data; - if (!l_out->header.value || memcmp(a_addr_from, &l_out->addr, sizeof(dap_chain_addr_t))) { + if (!l_out->header.value || memcmp(a_addr_from, &l_out->addr, sizeof(dap_chain_addr_t))) continue; - } l_value = GET_256_FROM_64(l_out->header.value); } break; case TX_ITEM_TYPE_OUT: { dap_chain_tx_out_t *l_out = (dap_chain_tx_out_t *)l_list_tmp->data; - if ( IS_ZERO_256(l_out->header.value) || memcmp(a_addr_from, &l_out->addr, sizeof(dap_chain_addr_t))) { + if (memcmp(a_addr_from, &l_out->addr, sizeof(dap_chain_addr_t)) || + dap_strcmp(dap_chain_ledger_tx_get_token_ticker_by_hash(a_ledger, &l_tx_cur_hash), a_token_ticker) || + IS_ZERO_256(l_out->header.value)) continue; - } l_value = l_out->header.value; } break; case TX_ITEM_TYPE_OUT_EXT: { dap_chain_tx_out_ext_t *l_out_ext = (dap_chain_tx_out_ext_t *)l_list_tmp->data; - if (IS_ZERO_256(l_out_ext->header.value) || memcmp(a_addr_from, &l_out_ext->addr, sizeof(dap_chain_addr_t)) || - strcmp((char *)a_token_ticker, l_out_ext->token)) { + if (memcmp(a_addr_from, &l_out_ext->addr, sizeof(dap_chain_addr_t)) || + strcmp((char *)a_token_ticker, l_out_ext->token) || + IS_ZERO_256(l_out_ext->header.value) ) { continue; } l_value = l_out_ext->header.value; @@ -5552,6 +5559,7 @@ dap_list_t *dap_chain_ledger_get_list_tx_outs_with_val(dap_ledger_t *a_ledger, c if (!dap_chain_ledger_tx_hash_is_used_out_item (a_ledger, &l_tx_cur_hash, l_out_idx_tmp, NULL)) { dap_chain_tx_used_out_item_t *l_item = DAP_NEW_Z(dap_chain_tx_used_out_item_t); if ( !l_item ) { + log_it(L_CRITICAL, "Out of memory"); if (l_list_used_out) dap_list_free_full(l_list_used_out, NULL); dap_list_free(l_list_out_items); @@ -5612,43 +5620,21 @@ int dap_chain_ledger_verificator_add(dap_chain_tx_out_cond_subtype_t a_subtype, dap_list_t * dap_chain_ledger_get_txs(dap_ledger_t *a_ledger, size_t a_count, size_t a_page, bool a_reverse) { dap_ledger_private_t *l_ledger_pvt = PVT(a_ledger); - size_t l_offset = a_count * (a_page - 1); - size_t l_count = HASH_COUNT(l_ledger_pvt->ledger_items); - if (a_page < 2) - l_offset = 0; - if (l_offset > l_count){ + size_t l_offset = a_page < 2 ? 0 : a_count * (a_page - 1); + if (!l_ledger_pvt->ledger_items || l_offset > HASH_COUNT(l_ledger_pvt->ledger_items)){ return NULL; } dap_list_t *l_list = NULL; size_t l_counter = 0; size_t l_end = l_offset + a_count; - if (!l_ledger_pvt->ledger_items) { - return NULL; - } - if (a_reverse) { - dap_chain_ledger_tx_item_t *l_ptr = l_ledger_pvt->ledger_items->hh.tbl->tail->prev; - if (!l_ptr) - l_ptr = l_ledger_pvt->ledger_items; - else - l_ptr = l_ptr->hh.next; - for (dap_chain_ledger_tx_item_t *ptr = l_ptr; ptr != NULL && l_counter < l_end; ptr = ptr->hh.prev) { - if (l_counter >= l_offset) { - dap_chain_datum_tx_t *l_tx = ptr->tx; - l_list = dap_list_append(l_list, l_tx); - } - l_counter++; - } - } else { - dap_chain_ledger_tx_item_t *l_ptr = l_ledger_pvt->ledger_items; - for (dap_chain_ledger_tx_item_t *ptr = l_ptr; ptr != NULL && l_counter < l_end; ptr = ptr->hh.next) { - if (l_counter >= l_offset) { - dap_chain_datum_tx_t *l_tx = ptr->tx; - l_list = dap_list_append(l_list, l_tx); - } - l_counter++; + dap_chain_ledger_tx_item_t *l_item_current, *l_item_tmp; + HASH_ITER(hh, l_ledger_pvt->ledger_items, l_item_current, l_item_tmp) { + if (l_counter++ >= l_offset) { + l_list = a_reverse + ? dap_list_prepend(l_list, l_item_current->tx) + : dap_list_append(l_list, l_item_current->tx); } } - return l_list; } @@ -5694,10 +5680,10 @@ dap_list_t *dap_chain_ledger_get_list_tx_cond_outs_with_val(dap_ledger_t *a_ledg continue; } if (!IS_ZERO_256(l_value)) { - dap_chain_tx_used_out_item_t *l_item = DAP_NEW(dap_chain_tx_used_out_item_t); + dap_chain_tx_used_out_item_t *l_item = DAP_NEW_Z(dap_chain_tx_used_out_item_t); if ( !l_item ) { if (l_list_used_out) - dap_list_free_full(l_list_used_out, free); + dap_list_free_full(l_list_used_out, NULL); dap_list_free(l_list_out_cond_items); return NULL; } @@ -5717,7 +5703,7 @@ dap_list_t *dap_chain_ledger_get_list_tx_cond_outs_with_val(dap_ledger_t *a_ledg // nothing to tranfer (not enough funds) if(!l_list_used_out || compare256(l_value_transfer, a_value_need) == -1) { - dap_list_free_full(l_list_used_out, free); + dap_list_free_full(l_list_used_out, NULL); return NULL; } diff --git a/modules/chain/include/dap_chain.h b/modules/chain/include/dap_chain.h index 35b6cd4c7e20e41f9b21d76000b8ebbff2ad58da..490c9fb6894547d407dad738c54d45b1aa869ad8 100644 --- a/modules/chain/include/dap_chain.h +++ b/modules/chain/include/dap_chain.h @@ -202,7 +202,7 @@ typedef struct dap_chain { dap_chain_callback_load_from_gdb callback_load_from_gdb; - dap_list_t * atom_notifiers; + dap_list_t *atom_notifiers; // dap_chain_callback_notify_t callback_notify; // void *callback_notify_arg; diff --git a/modules/chain/include/dap_chain_cell.h b/modules/chain/include/dap_chain_cell.h index c4e16bd2f90210a20cce0edd653a130596cdc523..bf3cc9a7a54e29499f8553c655cf6983953ac1ae 100644 --- a/modules/chain/include/dap_chain_cell.h +++ b/modules/chain/include/dap_chain_cell.h @@ -77,6 +77,7 @@ dap_chain_cell_t *dap_chain_cell_create_fill2(dap_chain_t *a_chain, const char * dap_chain_cell_t *dap_chain_cell_find_by_id(dap_chain_t *a_chain, dap_chain_cell_id_t a_cell_id); void dap_chain_cell_close(dap_chain_cell_t *a_cell); void dap_chain_cell_delete(dap_chain_cell_t *a_cell); +void dap_chain_cell_delete_all(dap_chain_t *a_chain); int dap_chain_cell_load(dap_chain_t *a_chain, dap_chain_cell_t *a_cell); -int dap_chain_cell_file_update(dap_chain_cell_t *a_cell); -int dap_chain_cell_file_append(dap_chain_cell_t *a_cell,const void *a_atom, size_t a_atom_size); +ssize_t dap_chain_cell_file_update(dap_chain_cell_t *a_cell); +ssize_t dap_chain_cell_file_append(dap_chain_cell_t *a_cell,const void *a_atom, size_t a_atom_size); diff --git a/modules/common/dap_chain_datum.c b/modules/common/dap_chain_datum.c index 296b68449889517c3242d31cd25f516922f120f5..d893124eb3cd5e02f094001da2608217511ec8ef 100644 --- a/modules/common/dap_chain_datum.c +++ b/modules/common/dap_chain_datum.c @@ -43,9 +43,13 @@ * @param a_data_size * @return */ -dap_chain_datum_t * dap_chain_datum_create(uint16_t a_type_id, const void * a_data, size_t a_data_size) +dap_chain_datum_t *dap_chain_datum_create(uint16_t a_type_id, const void *a_data, size_t a_data_size) { - dap_chain_datum_t * l_datum = DAP_NEW_Z_SIZE(dap_chain_datum_t, sizeof(l_datum->header)+ a_data_size); + dap_chain_datum_t *l_datum = DAP_NEW_Z_SIZE(dap_chain_datum_t, sizeof(l_datum->header)+ a_data_size); + if(!l_datum) { + log_it(L_CRITICAL, "Memory allocation error"); + return NULL; + } memcpy(l_datum->data, a_data, (uint32_t)a_data_size); // Compiler warning escape l_datum->header.type_id = a_type_id; l_datum->header.data_size = (uint32_t) a_data_size; diff --git a/modules/common/dap_chain_datum_tx.c b/modules/common/dap_chain_datum_tx.c index e6d5c578517c69502fd4f016ae88a16d1149fff6..01c40f6e37c25a20a0c8d3635ab792a40652e53a 100644 --- a/modules/common/dap_chain_datum_tx.c +++ b/modules/common/dap_chain_datum_tx.c @@ -113,14 +113,13 @@ int dap_chain_datum_tx_add_in_item(dap_chain_datum_tx_t **a_tx, dap_chain_hash_f */ uint256_t dap_chain_datum_tx_add_in_item_list(dap_chain_datum_tx_t **a_tx, dap_list_t *a_list_used_out) { - dap_list_t *l_list_tmp = a_list_used_out; - uint256_t l_value_to_items = {}; // how many datoshi to transfer - while (l_list_tmp) { - dap_chain_tx_used_out_item_t *l_item = l_list_tmp->data; + dap_list_t *l_item_out; + uint256_t l_value_to_items = { }; // how many datoshi to transfer + DL_FOREACH(a_list_used_out, l_item_out) { + dap_chain_tx_used_out_item_t *l_item = l_item_out->data; if (dap_chain_datum_tx_add_in_item(a_tx, &l_item->tx_hash_fast, l_item->num_idx_out) == 1) { SUM_256_256(l_value_to_items, l_item->value, &l_value_to_items); } - l_list_tmp = dap_list_next(l_list_tmp); } return l_value_to_items; } @@ -138,27 +137,24 @@ int dap_chain_datum_tx_add_in_cond_item(dap_chain_datum_tx_t **a_tx, dap_chain_h uint32_t a_tx_out_prev_idx, uint32_t a_receipt_idx) { - dap_chain_tx_in_cond_t *l_tx_in_cond = dap_chain_datum_tx_item_in_cond_create( a_tx_prev_hash, a_tx_out_prev_idx, - a_receipt_idx); - if(l_tx_in_cond) { - dap_chain_datum_tx_add_item(a_tx, (uint8_t *)l_tx_in_cond); - DAP_DELETE(l_tx_in_cond); - return 0; - } - return -1; - + dap_chain_tx_in_cond_t *l_tx_in_cond + = dap_chain_datum_tx_item_in_cond_create(a_tx_prev_hash, a_tx_out_prev_idx, a_receipt_idx); + if (!l_tx_in_cond) + return -1; + dap_chain_datum_tx_add_item(a_tx, (uint8_t*)l_tx_in_cond); + DAP_DELETE(l_tx_in_cond); + return 0; } uint256_t dap_chain_datum_tx_add_in_cond_item_list(dap_chain_datum_tx_t **a_tx, dap_list_t *a_list_used_out_cound) { - dap_list_t *l_list_tmp = a_list_used_out_cound; - uint256_t l_value_to_items = {}; - while (l_list_tmp) { - dap_chain_tx_used_out_item_t *l_item = l_list_tmp->data; + dap_list_t *l_item_out; + uint256_t l_value_to_items = { }; + DL_FOREACH(a_list_used_out_cound, l_item_out) { + dap_chain_tx_used_out_item_t *l_item = l_item_out->data; if (!dap_chain_datum_tx_add_in_cond_item(a_tx, &l_item->tx_hash_fast, l_item->num_idx_out,0)) { SUM_256_256(l_value_to_items, l_item->value, &l_value_to_items); } - l_list_tmp = dap_list_next(l_list_tmp); } return l_value_to_items; } @@ -178,19 +174,22 @@ int dap_chain_datum_tx_add_fee_item(dap_chain_datum_tx_t **a_tx, uint256_t a_val return -1; } -int dap_chain_datum_tx_get_fee_value (dap_chain_datum_tx_t *a_tx, uint256_t *a_value) +int dap_chain_datum_tx_get_fee_value(dap_chain_datum_tx_t *a_tx, uint256_t *a_value) { - dap_list_t *l_items_list = dap_chain_datum_tx_items_get(a_tx, TX_ITEM_TYPE_OUT_COND, NULL); - - for(dap_list_t *l_item=l_items_list; l_item; l_item=l_item->next){ + if (!a_value) + return -2; + int l_ret = -1; + dap_list_t *l_items_list = dap_chain_datum_tx_items_get(a_tx, TX_ITEM_TYPE_OUT_COND, NULL), *l_item; + DL_FOREACH(l_items_list, l_item) { dap_chain_tx_out_cond_t *l_out_item = (dap_chain_tx_out_cond_t*)l_item->data; if (l_out_item->header.subtype == DAP_CHAIN_TX_OUT_COND_SUBTYPE_FEE){ *a_value = l_out_item->header.value; - return 0; + l_ret = 0; + break; } } - - return -1; + dap_list_free(l_items_list); + return l_ret; } /** diff --git a/modules/common/dap_chain_datum_tx_items.c b/modules/common/dap_chain_datum_tx_items.c index 5ec7c2a87bb068da296692742d95cdc411dc469d..22ef1f623df58a70f2f13342758d7292f486f8cc 100644 --- a/modules/common/dap_chain_datum_tx_items.c +++ b/modules/common/dap_chain_datum_tx_items.c @@ -773,8 +773,8 @@ dap_list_t* dap_chain_datum_tx_items_get(dap_chain_datum_tx_t *a_tx, dap_chain_t while ((l_tx_item = dap_chain_datum_tx_item_get(a_tx, &l_item_idx_start, a_type, NULL)) != NULL) { items_list = dap_list_append(items_list, l_tx_item); - l_items_count++; - l_item_idx_start++; + ++l_items_count; + ++l_item_idx_start; } if(a_item_count) @@ -791,7 +791,7 @@ uint8_t *dap_chain_datum_tx_item_get_nth(dap_chain_datum_tx_t *a_tx, dap_chain_t l_tx_item = dap_chain_datum_tx_item_get(a_tx, &l_item_idx, a_type, NULL); if (!l_tx_item) break; - l_item_idx++; + ++l_item_idx; } return l_tx_item; } @@ -806,25 +806,22 @@ uint8_t *dap_chain_datum_tx_item_get_nth(dap_chain_datum_tx_t *a_tx, dap_chain_t */ dap_chain_tx_out_cond_t *dap_chain_datum_tx_out_cond_get(dap_chain_datum_tx_t *a_tx, dap_chain_tx_item_type_t a_cond_type, int *a_out_num) { - dap_list_t *l_list_out_items = dap_chain_datum_tx_items_get(a_tx, TX_ITEM_TYPE_OUT_ALL, NULL); + dap_list_t *l_list_out_items = dap_chain_datum_tx_items_get(a_tx, TX_ITEM_TYPE_OUT_ALL, NULL), *l_item; int l_prev_cond_idx = a_out_num ? *a_out_num : 0; dap_chain_tx_out_cond_t *l_res = NULL; - for (dap_list_t *l_list_tmp = l_list_out_items; l_list_tmp; l_list_tmp = dap_list_next(l_list_tmp), l_prev_cond_idx++) { + DL_FOREACH(l_list_out_items, l_item) { // Start from *a_out_num + 1 item if a_out_num != NULL - if (a_out_num && l_prev_cond_idx < *a_out_num) + if (a_out_num && l_prev_cond_idx++ < *a_out_num) continue; - if (*(uint8_t *)l_list_tmp->data == TX_ITEM_TYPE_OUT_COND && - ((dap_chain_tx_out_cond_t *)l_list_tmp->data)->header.subtype == a_cond_type) { - l_res = l_list_tmp->data; + if (*(byte_t*)l_item->data == TX_ITEM_TYPE_OUT_COND && + ((dap_chain_tx_out_cond_t*)l_item->data)->header.subtype == a_cond_type) { + l_res = l_item->data; break; } } dap_list_free(l_list_out_items); if (a_out_num) { - if (l_res) - *a_out_num = l_prev_cond_idx; - else - *a_out_num = -1; + *a_out_num = l_res ? l_prev_cond_idx : -1; } return l_res; } diff --git a/modules/consensus/dag-poa/dap_chain_cs_dag_poa.c b/modules/consensus/dag-poa/dap_chain_cs_dag_poa.c index c3e41ba0eb4bcf572f3e2a3d66c41cba0d8d799f..758dd3749bc1301999c895f500613a04cd78719a 100644 --- a/modules/consensus/dag-poa/dap_chain_cs_dag_poa.c +++ b/modules/consensus/dag-poa/dap_chain_cs_dag_poa.c @@ -341,9 +341,12 @@ static int s_cli_dag_poa(int argc, char ** argv, char **a_str_reply) */ static int s_callback_new(dap_chain_t * a_chain, dap_config_t * a_chain_cfg) { - dap_chain_cs_dag_new(a_chain,a_chain_cfg); - dap_chain_cs_dag_t *l_dag = DAP_CHAIN_CS_DAG ( a_chain ); - dap_chain_cs_dag_poa_t *l_poa = DAP_NEW_Z ( dap_chain_cs_dag_poa_t); + if (dap_chain_cs_dag_new(a_chain,a_chain_cfg)) { + log_it(L_ERROR, "Couldn't init DAG"); + return -1; + } + dap_chain_cs_dag_t *l_dag = DAP_CHAIN_CS_DAG(a_chain); + dap_chain_cs_dag_poa_t *l_poa = DAP_NEW_Z(dap_chain_cs_dag_poa_t); if (!l_poa) { log_it(L_CRITICAL, "Memory allocation error"); return -1; diff --git a/modules/consensus/esbocs/dap_chain_cs_esbocs.c b/modules/consensus/esbocs/dap_chain_cs_esbocs.c index fe59b002e2e6fd89b32ca2a2f95876a4c6da5462..e9db643116fad13d81b66826d08cc41a10db433a 100644 --- a/modules/consensus/esbocs/dap_chain_cs_esbocs.c +++ b/modules/consensus/esbocs/dap_chain_cs_esbocs.c @@ -106,7 +106,6 @@ DAP_STATIC_INLINE size_t s_get_esbocs_message_size(dap_chain_esbocs_message_t *a } static dap_chain_esbocs_session_t * s_session_items; -static dap_timerfd_t *s_session_cs_timer = NULL; typedef struct dap_chain_esbocs_pvt { // Base params @@ -326,6 +325,16 @@ static void s_session_db_serialize(dap_global_db_context_t *a_context, void *a_a } dap_store_obj_free(l_objs, l_objs_count); + if (l_pkt) + dap_hash_fast(l_pkt->data, l_pkt->data_size, &l_session->db_hash); + else + l_session->db_hash = (dap_hash_fast_t){}; + if (PVT(l_session->esbocs)->debug) { + char l_sync_hash_str[DAP_CHAIN_HASH_FAST_STR_SIZE]; + dap_chain_hash_fast_to_str(&l_session->db_hash, l_sync_hash_str, DAP_CHAIN_HASH_FAST_STR_SIZE); + log_it(L_MSG, "DB changes applied, new DB resync hash is %s", l_sync_hash_str); + } + char *l_del_sync_group = dap_strdup_printf("%s.del", l_sync_group); l_objs_count = 0; l_objs = dap_global_db_get_all_raw_unsafe(a_context, l_del_sync_group, 0, &l_objs_count); @@ -348,15 +357,7 @@ static void s_session_db_serialize(dap_global_db_context_t *a_context, void *a_a DAP_DELETE(l_sync_group); DAP_DEL_Z(l_session->db_serial); l_session->db_serial = l_pkt; - if (l_pkt) - dap_hash_fast(l_pkt->data, l_pkt->data_size, &l_session->db_hash); - else - l_session->db_hash = (dap_hash_fast_t){}; - if (PVT(l_session->esbocs)->debug) { - char l_sync_hash_str[DAP_CHAIN_HASH_FAST_STR_SIZE]; - dap_chain_hash_fast_to_str(&l_session->db_hash, l_sync_hash_str, DAP_CHAIN_HASH_FAST_STR_SIZE); - log_it(L_MSG, "DB changes applied, new DB resync hash is %s", l_sync_hash_str); - } + dap_proc_queue_add_callback(dap_events_worker_get_auto(), s_change_db_broadcast, l_session); } @@ -483,17 +484,16 @@ static int s_callback_created(dap_chain_t *a_chain, dap_config_t *a_chain_net_cf log_it(L_INFO, "Init session for net:%s, chain:%s", a_chain->net_name, a_chain->name); DL_APPEND(s_session_items, l_session); - if (!s_session_cs_timer) { - s_session_cs_timer = dap_timerfd_start(1000, s_session_timer, NULL); - debug_if(l_esbocs_pvt->debug, L_MSG, "Consensus main timer is started"); - } + l_session->cs_timer = dap_timerfd_start(1000, s_session_timer, l_session); + debug_if(l_esbocs_pvt->debug, L_MSG, "Consensus main timer is started"); + DAP_CHAIN_PVT(a_chain)->cs_started = true; return 0; } bool dap_chain_esbocs_started() { - return s_session_cs_timer; + return s_session_items; } static uint256_t s_callback_get_minimum_fee(dap_chain_t *a_chain) @@ -520,8 +520,7 @@ static void s_callback_delete(dap_chain_cs_blocks_t *a_blocks) dap_chain_esbocs_session_t *l_session = l_esbocs->session; pthread_mutex_lock(&l_session->mutex); DL_DELETE(s_session_items, l_session); - if (!s_session_items) - dap_timerfd_delete_mt(s_session_cs_timer->worker, s_session_cs_timer->esocket_uuid); + dap_timerfd_delete_mt(l_session->cs_timer->worker, l_session->cs_timer->esocket_uuid); s_session_round_clear(l_session); dap_chain_esbocs_sync_item_t *l_sync_item, *l_sync_tmp; HASH_ITER(hh, l_session->sync_items, l_sync_item, l_sync_tmp) { @@ -663,27 +662,36 @@ static void s_get_last_block_hash(dap_chain_t *a_chain, dap_chain_hash_fast_t *a a_chain->callback_atom_iter_delete(l_iter); } -static int s_callback_addr_compare(const void *a_list_data, const void *a_user_data) +static int s_callback_addr_compare(const void *a_list_elem, const void *a_addr_elem) { - return memcmp(&((dap_chain_esbocs_validator_t *)a_list_data)->signing_addr, - (dap_chain_addr_t *)a_user_data, sizeof(dap_chain_addr_t)); + dap_chain_esbocs_validator_t *l_validator = (dap_chain_esbocs_validator_t*)((dap_list_t*)a_list_elem)->data; + dap_chain_addr_t *l_addr = (dap_chain_addr_t*)((dap_list_t*)a_addr_elem)->data; + if (!l_validator || !l_addr) { + log_it(L_CRITICAL, "Invalid argument"); + return -1; + } + return memcmp(&l_validator->signing_addr, l_addr, sizeof(dap_chain_addr_t)); } static dap_list_t *s_validator_check(dap_chain_addr_t *a_addr, dap_list_t *a_validators) { - return dap_list_find_custom(a_validators, a_addr, s_callback_addr_compare); + return dap_list_find(a_validators, a_addr, s_callback_addr_compare); } -static int s_callback_addr_compare_synced(const void *a_list_data, const void *a_user_data) +static int s_callback_addr_compare_synced(const void *a_list_elem, const void *a_addr_elem) { - return memcmp(&((dap_chain_esbocs_validator_t *)a_list_data)->signing_addr, - (dap_chain_addr_t *)a_user_data, sizeof(dap_chain_addr_t)) || - !((dap_chain_esbocs_validator_t *)a_list_data)->is_synced; + dap_chain_esbocs_validator_t *l_validator = (dap_chain_esbocs_validator_t*)((dap_list_t*)a_list_elem)->data; + dap_chain_addr_t *l_addr = (dap_chain_addr_t*)((dap_list_t*)a_addr_elem)->data; + if (!l_validator || !l_addr) { + log_it(L_CRITICAL, "Invalid argument"); + return -1; + } + return memcmp(&l_validator->signing_addr, l_addr, sizeof(dap_chain_addr_t)) || !l_validator->is_synced; } static dap_list_t *s_validator_check_synced(dap_chain_addr_t *a_addr, dap_list_t *a_validators) { - return dap_list_find_custom(a_validators, a_addr, s_callback_addr_compare_synced); + return dap_list_find(a_validators, a_addr, s_callback_addr_compare_synced); } @@ -905,7 +913,7 @@ static uint64_t s_session_calc_current_round_id(dap_chain_esbocs_session_t *a_se uint64_t l_id_candidate = 0; for (dap_chain_esbocs_message_item_t *l_item = a_session->cur_round.message_items; l_item; l_item = l_item->hh.next) { if (l_item->message->hdr.type == DAP_CHAIN_ESBOCS_MSG_TYPE_START_SYNC && - l_item->message->hdr.attempt_num == a_session->cur_round.sync_attempt && + ((struct sync_params *)l_item->message->msg_n_sign)->attempt == a_session->cur_round.sync_attempt && dap_chain_addr_compare(&l_item->signing_addr, &l_validator->signing_addr)) { l_id_candidate = l_item->message->hdr.round_id; break; @@ -948,17 +956,22 @@ static uint64_t s_session_calc_current_round_id(dap_chain_esbocs_session_t *a_se return l_ret ? l_ret : a_session->cur_round.id; } -static int s_signs_sort_callback(const void *a_sign1, const void *a_sign2, UNUSED_ARG void *a_user_data) +static int s_signs_sort_callback(const void *a_sign1, const void *a_sign2) { - size_t l_size1 = dap_sign_get_size((dap_sign_t *)a_sign1); - size_t l_size2 = dap_sign_get_size((dap_sign_t *)a_sign2); - size_t l_size_min = MIN(l_size1, l_size2); - int l_ret = memcmp(a_sign1, a_sign2, l_size_min); + dap_sign_t *l_sign1 = (dap_sign_t*)((dap_list_t*)a_sign1)->data, + *l_sign2 = (dap_sign_t*)((dap_list_t*)a_sign1)->data; + if (!l_sign1 || !l_sign2) { + log_it(L_CRITICAL, "Invalid element"); + return 0; + } + + size_t l_size1 = dap_sign_get_size(l_sign1), + l_size2 = dap_sign_get_size(l_sign2), + l_size_min = MIN(l_size1, l_size2); + + int l_ret = memcmp(l_sign1, l_sign2, l_size_min); if (!l_ret) { - if (l_size1 < l_size2) - l_ret = -1; - else if (l_size1 > l_size2) - l_ret = 1; + l_ret = l_size1 == l_size2 ? 0 : l_size1 > l_size2 ? 1 : -1; } return l_ret; } @@ -1003,14 +1016,9 @@ dap_chain_esbocs_directive_t *s_session_directive_ready(dap_chain_esbocs_session static void s_session_state_change(dap_chain_esbocs_session_t *a_session, enum s_esbocs_session_state a_new_state, dap_time_t a_time) { - if (a_new_state != DAP_CHAIN_ESBOCS_SESSION_STATE_PREVIOUS) { - if (a_session->state == DAP_CHAIN_ESBOCS_SESSION_STATE_WAIT_VOTING) { - // Do not change this state, state changing will be applied after return to PREVIOUS state - a_session->old_state = a_new_state; - return; - } + if (a_new_state != DAP_CHAIN_ESBOCS_SESSION_STATE_PREVIOUS) a_session->old_state = a_session->state; - } + a_session->state = a_new_state; a_session->ts_stage_entry = a_time; @@ -1074,6 +1082,24 @@ static void s_session_state_change(dap_chain_esbocs_session_t *a_session, enum s } } } break; + case DAP_CHAIN_ESBOCS_SESSION_STATE_WAIT_VOTING: { + if (a_session->old_state == DAP_CHAIN_ESBOCS_SESSION_STATE_WAIT_PROC) { + // Clear mark of chosen to submit validator + dap_list_t *l_list = s_validator_check( + &a_session->cur_round.attempt_submit_validator, + a_session->cur_round.validators_list + ); + dap_chain_esbocs_validator_t *l_validator = l_list ? l_list->data : NULL; + if (!l_validator || !l_validator->is_chosen) { + char *l_addr = dap_chain_addr_to_str(&a_session->cur_round.attempt_submit_validator); + log_it(L_MSG, "Error: can't find current attmempt submit validator %s in signers list", l_addr); + DAP_DELETE(l_addr); + } + l_validator->is_chosen = false; + } else + a_session->old_state = DAP_CHAIN_ESBOCS_SESSION_STATE_WAIT_PROC; + } break; + case DAP_CHAIN_ESBOCS_SESSION_STATE_WAIT_FINISH: { dap_chain_esbocs_store_t *l_store; HASH_FIND(hh, a_session->cur_round.store_items, &a_session->cur_round.attempt_candidate_hash, sizeof(dap_hash_fast_t), l_store); @@ -1240,11 +1266,8 @@ static void s_session_proc_state(dap_chain_esbocs_session_t *a_session) static bool s_session_timer(void *a_arg) { - UNUSED(a_arg); - dap_chain_esbocs_session_t *l_session = NULL; - DL_FOREACH(s_session_items, l_session) { - s_session_proc_state(l_session); - } + dap_chain_esbocs_session_t *l_session = a_arg; + s_session_proc_state(l_session); return true; } @@ -2333,7 +2356,7 @@ static void s_session_packet_in(void *a_arg, dap_chain_node_addr_t *a_sender_nod case DAP_CHAIN_ESBOCS_MSG_TYPE_DIRECTIVE: { if (l_session->cur_round.directive) { - log_it(L_WARNING, "Only one directive can be processed at a time"); + log_it(L_WARNING, "Only one directive can be processed by round"); break; } dap_chain_esbocs_directive_t *l_directive = l_message_data; diff --git a/modules/consensus/esbocs/include/dap_chain_cs_esbocs.h b/modules/consensus/esbocs/include/dap_chain_cs_esbocs.h index 963ff35b7d13376053f90aea19565316cd957616..7a95d9dbf44c46948e4ad5211081b1832d1b75b9 100644 --- a/modules/consensus/esbocs/include/dap_chain_cs_esbocs.h +++ b/modules/consensus/esbocs/include/dap_chain_cs_esbocs.h @@ -153,6 +153,7 @@ typedef struct dap_chain_esbocs_penalty_item { typedef struct dap_chain_esbocs_session { pthread_mutex_t mutex; + dap_timerfd_t *cs_timer; dap_chain_block_t *processing_candidate; dap_chain_t *chain; diff --git a/modules/consensus/none/dap_chain_cs_none.c b/modules/consensus/none/dap_chain_cs_none.c index c2e60694018c6ad621c2af627dd473c65492c2bb..7680827628308a87b8bf2fc8df8da5950482d464 100644 --- a/modules/consensus/none/dap_chain_cs_none.c +++ b/modules/consensus/none/dap_chain_cs_none.c @@ -404,9 +404,10 @@ static dap_chain_atom_verify_res_t s_chain_callback_atom_add(dap_chain_t * a_cha dap_chain_hash_fast_to_str(&l_hash_item->datum_data_hash, l_hash_item->key, sizeof(l_hash_item->key)); DL_APPEND(l_gdb_priv->hash_items, l_hash_item); if (!l_gdb_priv->is_load_mode && a_chain->atom_notifiers) { - for(dap_list_t *l_iter = a_chain->atom_notifiers; l_iter; l_iter = dap_list_next(l_iter)) { - dap_chain_atom_notifier_t *i = (dap_chain_atom_notifier_t *)l_iter->data; - i->callback(i->arg, a_chain, (dap_chain_cell_id_t){}, (void *)l_datum, l_datum_size); + dap_list_t *l_iter; + DL_FOREACH(a_chain->atom_notifiers, l_iter) { + dap_chain_atom_notifier_t *l_notifier = (dap_chain_atom_notifier_t*)l_iter->data; + l_notifier->callback(l_notifier->arg, a_chain, (dap_chain_cell_id_t){ }, (void*)l_datum, l_datum_size); } } return ATOM_ACCEPT; diff --git a/modules/mempool/dap_chain_mempool.c b/modules/mempool/dap_chain_mempool.c index cd08cb2cc13c5f5933d3336221cded4ef55515a1..29f93abdcf54828e7f5a84d209af5ab04e0059a0 100644 --- a/modules/mempool/dap_chain_mempool.c +++ b/modules/mempool/dap_chain_mempool.c @@ -438,28 +438,24 @@ int dap_chain_mempool_tx_create_massive( dap_chain_t * a_chain, dap_enc_key_t *a dap_chain_datum_tx_t *l_tx_new = dap_chain_datum_tx_create(); uint256_t l_value_back = {}; // add 'in' items - dap_list_t *l_list_tmp = l_list_used_out; uint256_t l_value_to_items = {}; // how many coins to transfer - // Add in and remove out used items - while(l_list_tmp) { - dap_chain_tx_used_out_item_t *l_item = l_list_tmp->data; - char l_in_hash_str[70]; - - dap_chain_hash_fast_to_str(&l_item->tx_hash_fast,l_in_hash_str,sizeof (l_in_hash_str) ); + dap_list_t *l_used_out, *l_tmp; + DL_FOREACH_SAFE(l_list_used_out, l_used_out, l_tmp) { + dap_chain_tx_used_out_item_t *l_item = l_used_out->data; + char l_in_hash_str[DAP_CHAIN_HASH_FAST_STR_SIZE]; + dap_chain_hash_fast_to_str(&l_item->tx_hash_fast, l_in_hash_str, sizeof(l_in_hash_str)); char *l_balance = dap_chain_balance_print(l_item->value); if (dap_chain_datum_tx_add_in_item(&l_tx_new, &l_item->tx_hash_fast, l_item->num_idx_out)) { SUM_256_256(l_value_to_items, l_item->value, &l_value_to_items); log_it(L_DEBUG, "Added input %s with %s datoshi", l_in_hash_str, l_balance); - }else{ + } else { log_it(L_WARNING, "Can't add input from %s with %s datoshi", l_in_hash_str, l_balance); } DAP_DELETE(l_balance); - l_list_used_out = l_list_tmp->next; - DAP_DELETE(l_list_tmp->data); - dap_list_free1(l_list_tmp); - l_list_tmp = l_list_used_out; + DL_DELETE(l_list_used_out, l_used_out); + DAP_DELETE(l_item); if (compare256(l_value_to_items, l_value_transfer) != -1) break; } @@ -548,12 +544,14 @@ int dap_chain_mempool_tx_create_massive( dap_chain_t * a_chain, dap_enc_key_t *a if (!l_item_back) { log_it(L_CRITICAL, "Memory allocation error"); DAP_DELETE(l_objs); - dap_list_free( l_list_out_items); + dap_list_free(l_list_out_items); return -6; } - l_item_back->tx_hash_fast = l_tx_new_hash; - l_item_back->num_idx_out = l_out_idx_tmp; - l_item_back->value = l_value_back; + *l_item_back = (dap_chain_tx_used_out_item_t) { + .tx_hash_fast = l_tx_new_hash, + .num_idx_out = l_out_idx_tmp, + .value = l_value_back + }; l_list_used_out = dap_list_prepend(l_list_used_out, l_item_back); log_it(L_DEBUG,"Found change back output, stored back in UTXO table"); break; @@ -561,8 +559,7 @@ int dap_chain_mempool_tx_create_massive( dap_chain_t * a_chain, dap_enc_key_t *a l_list_tmp = l_list_tmp->next; l_out_idx_tmp++; } - //log_it(L_DEBUG,"Checked all outputs"); - dap_list_free( l_list_out_items); + dap_list_free(l_list_out_items); } SUBTRACT_256_256(l_value_transfer, l_value_pack, &l_value_transfer); @@ -579,7 +576,7 @@ int dap_chain_mempool_tx_create_massive( dap_chain_t * a_chain, dap_enc_key_t *a log_it(L_DEBUG, "Prepared obj with key %s (value_len = %"DAP_UINT64_FORMAT_U")", l_objs[i].key? l_objs[i].key :"NULL" , l_objs[i].value_len ); } - dap_list_free_full(l_list_used_out, free); + dap_list_free_full(l_list_used_out, NULL); char *l_gdb_group = dap_chain_net_get_gdb_group_mempool_new(a_chain); dap_global_db_set_multiple_zc(l_gdb_group, l_objs, a_tx_num, s_tx_create_massive_gdb_save_callback, NULL); @@ -787,7 +784,7 @@ char *dap_chain_mempool_tx_create_cond(dap_chain_net_t *a_net, { uint256_t l_value_to_items = dap_chain_datum_tx_add_in_item_list(&l_tx, l_list_used_out); assert(EQUAL_256(l_value_to_items, l_value_transfer)); - dap_list_free_full(l_list_used_out, free); + dap_list_free_full(l_list_used_out, NULL); } // add 'out_cond' and 'out' items { @@ -1247,19 +1244,16 @@ void chain_mempool_proc(struct dap_http_simple *cl_st, void * arg) DAP_DEL_Z(a_key); } else *return_code = Http_Status_BadRequest; - } - else *return_code = Http_Status_BadRequest; + } else + *return_code = Http_Status_BadRequest; enc_http_delegate_delete(l_enc_delegate); - } - else *return_code = Http_Status_Unauthorized; + } else + *return_code = Http_Status_Unauthorized; - if(hdr_session_close_id && hdr_session_close_id->value && !strcmp(hdr_session_close_id->value, "yes")) { + if (hdr_session_close_id && !strcmp(hdr_session_close_id->value, "yes") && hdr_key_id) // close session - if(hdr_key_id && hdr_key_id->value) { - dap_enc_ks_delete(hdr_key_id->value); - } - } + dap_enc_ks_delete(hdr_key_id->value); } /** diff --git a/modules/net/dap_chain_net.c b/modules/net/dap_chain_net.c index e9dd348f082d05d886f3adfc9baa511ee3294ffb..2b40a6d9118168c2ca27837128141b87fbe796da 100644 --- a/modules/net/dap_chain_net.c +++ b/modules/net/dap_chain_net.c @@ -1488,7 +1488,7 @@ bool dap_chain_net_sync_trylock(dap_chain_net_t *a_net, dap_chain_node_client_t if (!l_found) { l_net_pvt->active_link = a_client; } - if (l_found && !dap_list_find(l_net_pvt->links_queue, a_client)) + if (l_found && !dap_list_find(l_net_pvt->links_queue, a_client, NULL)) l_net_pvt->links_queue = dap_list_append(l_net_pvt->links_queue, a_client); if (a_err != EDEADLK) pthread_mutex_unlock(&l_net_pvt->uplinks_mutex); @@ -2292,18 +2292,17 @@ static void remove_duplicates_in_chain_by_priority(dap_chain_t *l_chain_1, dap_c typedef struct list_priority_{ uint16_t prior; char * chains_path; -}list_priority; +} list_priority; -static int callback_compare_prioritity_list(const void * a_item1, const void * a_item2, void *a_unused) +static int callback_compare_prioritity_list(const void *a_item1, const void *a_item2) { - UNUSED(a_unused); - list_priority *l_item1 = (list_priority*) a_item1; - list_priority *l_item2 = (list_priority*) a_item2; - if(!l_item1 || !l_item2 || l_item1->prior == l_item2->prior) + list_priority *l_item1 = (list_priority*)((dap_list_t*)a_item1)->data, + *l_item2 = (list_priority*)((dap_list_t*)a_item2)->data; + if (!l_item1 || !l_item2) { + log_it(L_CRITICAL, "Invalid arg"); return 0; - if(l_item1->prior > l_item2->prior) - return 1; - return -1; + } + return l_item1->prior == l_item2->prior ? 0 : l_item1->prior > l_item2->prior ? 1 : -1; } void s_main_timer_callback(void *a_arg) @@ -2767,12 +2766,11 @@ int s_net_init(const char * a_net_name, uint16_t a_acl_idx) if(l_cfg_new) { list_priority *l_chain_prior = DAP_NEW_Z(list_priority); if (!l_chain_prior) { - log_it(L_CRITICAL, "Memory allocation error"); - DAP_DELETE (l_entry_name); + log_it(L_CRITICAL, "Memory allocation error"); + DAP_DELETE(l_entry_name); closedir(l_chains_dir); dap_config_close(l_cfg_new); dap_config_close(l_cfg); - closedir(l_chains_dir); return -1; } l_chain_prior->prior = dap_config_get_item_uint16_default(l_cfg_new, "chain", "load_priority", 100); @@ -2898,10 +2896,6 @@ int s_net_load(dap_chain_net_t *a_net) DAP_CHAIN_PVT(l_chain)->need_reorder = false; if (l_chain->callback_purge) { l_chain->callback_purge(l_chain); - pthread_rwlock_wrlock(&l_chain->cell_rwlock); - for (dap_chain_cell_t *it = l_chain->cells; it; it = it->hh.next) - dap_chain_cell_delete(it); - pthread_rwlock_unlock(&l_chain->cell_rwlock); dap_chain_ledger_purge(l_net->pub.ledger, false); dap_chain_ledger_set_fee(l_net->pub.ledger, uint256_0, c_dap_chain_addr_blank); dap_chain_net_decree_purge(l_net); @@ -3314,7 +3308,7 @@ dap_list_t* dap_chain_net_get_link_node_list(dap_chain_net_t * l_net, bool a_is_ DAP_DELETE(l_remote_node_info); } if(l_is_add) { - dap_chain_node_addr_t *l_address = DAP_NEW(dap_chain_node_addr_t); + dap_chain_node_addr_t *l_address = DAP_NEW_Z(dap_chain_node_addr_t); if (!l_address) { log_it(L_CRITICAL, "Memory allocation error"); return NULL; @@ -3323,9 +3317,8 @@ dap_list_t* dap_chain_net_get_link_node_list(dap_chain_net_t * l_net, bool a_is_ l_node_list = dap_list_append(l_node_list, l_address); } } - + DAP_DELETE(l_cur_node_info); } - DAP_DELETE(l_cur_node_info); return l_node_list; } @@ -3409,7 +3402,7 @@ bool dap_chain_net_get_flag_sync_from_zero( dap_chain_net_t * a_net) */ bool dap_chain_net_get_extra_gdb_group(dap_chain_net_t *a_net, dap_chain_node_addr_t a_node_addr) { - if(!a_net || !PVT(a_net) || !PVT(a_net)->gdb_sync_nodes_addrs) + if (!a_net || !PVT(a_net)->gdb_sync_nodes_addrs) return false; for(uint16_t i = 0; i < PVT(a_net)->gdb_sync_nodes_addrs_count; i++) { if(a_node_addr.uint64 == PVT(a_net)->gdb_sync_nodes_addrs[i].uint64) { diff --git a/modules/net/dap_chain_net_balancer.c b/modules/net/dap_chain_net_balancer.c index bb943ff330fd290dbb14092d2ed03f73d3f9f3c5..9b5e456b10156b991748b4ef5dde9c7ea4dacd93 100644 --- a/modules/net/dap_chain_net_balancer.c +++ b/modules/net/dap_chain_net_balancer.c @@ -172,16 +172,17 @@ void dap_chain_net_balancer_prepare_list_links(const char *a_net_name,bool hands dap_list_free(l_node_addr_list); } -static int callback_compare_node_list(const void * a_item1, const void * a_item2, void *a_unused) +static int callback_compare_node_list(const void *a_item1, const void *a_item2) { - UNUSED(a_unused); - if (!a_item1 || !a_item2) { + dap_chain_node_info_t *l_item1 = (dap_chain_node_info_t*)((dap_list_t*)a_item1)->data, + *l_item2 = (dap_chain_node_info_t*)((dap_list_t*)a_item2)->data; + if (!l_item1 || !l_item2) { + log_it(L_CRITICAL, "Invalid element"); return 0; } - dap_chain_node_info_t *l_item1 = (dap_chain_node_info_t*)a_item1, *l_item2 = (dap_chain_node_info_t*)a_item2; + return l_item1->hdr.links_number == l_item2->hdr.links_number - ? 0 : l_item1->hdr.links_number > l_item2->hdr.links_number - ? 1 : -1; + ? 0 : l_item1->hdr.links_number > l_item2->hdr.links_number ? 1 : -1; } dap_chain_net_node_balancer_t *dap_chain_net_balancer_get_node(const char *a_net_name,uint16_t a_links_need) diff --git a/modules/net/dap_chain_net_tx.c b/modules/net/dap_chain_net_tx.c index 45c166b2271dfd500a35eeabb510183210fa0eda..1b83023b261119586544b5d193a85c21b9d5e6a4 100644 --- a/modules/net/dap_chain_net_tx.c +++ b/modules/net/dap_chain_net_tx.c @@ -481,8 +481,8 @@ dap_list_t * dap_chain_net_get_tx_cond_all_by_srv_uid(dap_chain_net_t * a_net, c const dap_time_t a_time_from, const dap_time_t a_time_to, const dap_chain_net_tx_search_type_t a_search_type) { - dap_ledger_t * l_ledger = a_net->pub.ledger; - dap_list_t * l_ret = NULL; + dap_ledger_t *l_ledger = a_net->pub.ledger; + dap_list_t *l_ret = NULL; switch (a_search_type) { case TX_SEARCH_TYPE_NET: @@ -531,16 +531,13 @@ dap_list_t * dap_chain_net_get_tx_cond_all_by_srv_uid(dap_chain_net_t * a_net, c continue; } // Check for OUT_COND items - dap_list_t *l_list_out_cond_items = dap_chain_datum_tx_items_get(l_tx, TX_ITEM_TYPE_OUT_COND , NULL); - if(l_list_out_cond_items){ - dap_list_t *l_list_cur = l_list_out_cond_items; - while(l_list_cur){ // Go through all cond items - dap_chain_tx_out_cond_t * l_tx_out_cond = (dap_chain_tx_out_cond_t *)l_list_cur->data; - if(l_tx_out_cond) // If we found cond out with target srv_uid - if(l_tx_out_cond->header.srv_uid.uint64 == a_srv_uid.uint64) - l_ret = dap_list_append(l_ret, - DAP_DUP_SIZE(l_tx, dap_chain_datum_tx_get_size(l_tx))); - l_list_cur = dap_list_next(l_list_cur); + dap_list_t *l_list_out_cond_items = dap_chain_datum_tx_items_get(l_tx, TX_ITEM_TYPE_OUT_COND, NULL), *l_out_cond_item; + if(l_list_out_cond_items) { + DL_FOREACH(l_list_out_cond_items, l_out_cond_item) { + dap_chain_tx_out_cond_t *l_tx_out_cond = (dap_chain_tx_out_cond_t*)l_out_cond_item->data; + if (l_tx_out_cond && l_tx_out_cond->header.srv_uid.uint64 == a_srv_uid.uint64) { + l_ret = dap_list_append(l_ret, l_tx); + } } dap_list_free(l_list_out_cond_items); } @@ -561,7 +558,6 @@ dap_list_t * dap_chain_net_get_tx_cond_all_by_srv_uid(dap_chain_net_t * a_net, c break; } return l_ret; - } diff --git a/modules/net/dap_chain_node_cli.c b/modules/net/dap_chain_node_cli.c index b31b10ab77ad7cdd0b33f107c79a45afbf6a0efc..e0ebfa55966804c002321d89c724b2c7de1a8b10 100644 --- a/modules/net/dap_chain_node_cli.c +++ b/modules/net/dap_chain_node_cli.c @@ -282,7 +282,7 @@ int dap_chain_node_cli_init(dap_config_t * g_config) dap_cli_server_cmd_add ("tx_create_json", com_tx_create_json, "Make transaction", "tx_create_json -net <net_name> -chain <chain_name> -json <json_file_path>\n" ); dap_cli_server_cmd_add ("tx_cond_create", com_tx_cond_create, "Make cond transaction", - "tx_cond_create -net <net_name> -token <token_ticker> -wallet <wallet_name>" + "tx_cond_create -net <net_name> -token <token_ticker> -w <wallet_name>" " -cert <pub_cert_name> -value <value_datoshi> -fee <value> -unit {mb | kb | b | sec | day} -srv_uid <numeric_uid>\n" ); dap_cli_server_cmd_add ("tx_verify", com_tx_verify, "Verifing transaction in mempool", diff --git a/modules/net/dap_chain_node_cli_cmd.c b/modules/net/dap_chain_node_cli_cmd.c index 7e558f4a3909eba9372c911e794a5216bc66bb16..1f8d035bf0713dbfbc153ae771d82cfe4e46b7aa 100644 --- a/modules/net/dap_chain_node_cli_cmd.c +++ b/modules/net/dap_chain_node_cli_cmd.c @@ -110,7 +110,7 @@ #define LOG_TAG "chain_node_cli_cmd" -static void s_dap_chain_net_purge(dap_chain_net_t * a_net); +static void s_dap_chain_net_purge(dap_chain_net_t *a_net); /** * @brief dap_chain_node_addr_t* dap_chain_node_addr_get_by_alias @@ -133,7 +133,6 @@ dap_chain_node_addr_t* dap_chain_node_addr_get_by_alias(dap_chain_net_t * a_net, DAP_DELETE(l_addr); l_addr = NULL; } -// DAP_DELETE(addr_str); return l_addr; } @@ -182,18 +181,11 @@ static dap_list_t* get_aliases_by_name(dap_chain_net_t * l_net, dap_chain_node_a */ static dap_chain_node_addr_t* s_node_info_get_addr(dap_chain_net_t * a_net, dap_chain_node_addr_t *a_addr, const char *a_alias_str) { - dap_chain_node_addr_t *l_address = NULL; - if(a_alias_str && !a_addr->uint64) { - l_address = dap_chain_node_addr_get_by_alias(a_net, a_alias_str); - } - if(a_addr->uint64) { - l_address = DAP_NEW(dap_chain_node_addr_t); - if (!l_address) { - log_it(L_CRITICAL, "Memory allocation error"); - return NULL; - } - l_address->uint64 = a_addr->uint64; - } + dap_chain_node_addr_t *l_address = a_alias_str + ? dap_chain_node_addr_get_by_alias(a_net, a_alias_str) + : a_addr && a_addr->uint64 ? DAP_DUP(a_addr) : NULL; + if (!l_address) + log_it(L_ERROR, "Node address with specified params not found"); return l_address; } @@ -330,14 +322,10 @@ static int node_info_add_with_reply(dap_chain_net_t * a_net, dap_chain_node_info } // write to base - bool res = node_info_save_and_reply(a_net, a_node_info, a_str_reply); - if(res) - dap_cli_server_cmd_set_reply_text(a_str_reply, "node added"); - else + if(!node_info_save_and_reply(a_net, a_node_info, a_str_reply)) return -1; - if(res) - return 0; - return -1; + dap_cli_server_cmd_set_reply_text(a_str_reply, "node added"); + return 0; } @@ -537,18 +525,14 @@ static int node_info_dump_with_reply(dap_chain_net_t * a_net, dap_chain_node_add int l_ret = 0; dap_string_t *l_string_reply = dap_string_new("Node dump:"); - if((a_addr && a_addr->uint64) || a_alias) { - dap_chain_node_addr_t *l_addr = NULL; - if(a_addr && a_addr->uint64) { - l_addr = DAP_NEW(dap_chain_node_addr_t); - if(!l_addr) { - dap_cli_server_cmd_set_reply_text(a_str_reply, "addr not valid"); - dap_string_free(l_string_reply, true); - return -1; - } - l_addr->uint64 = a_addr->uint64; - } else if(a_alias) { - l_addr = dap_chain_node_alias_find(a_net, a_alias); + if (a_addr || a_alias) { + dap_chain_node_addr_t *l_addr = a_alias + ? dap_chain_node_alias_find(a_net, a_alias) + : a_addr && a_addr->uint64 ? DAP_DUP(a_addr) : NULL; + + if (!l_addr) { + log_it(L_ERROR, "Node address with specified params not found"); + return -1; } // read node @@ -829,7 +813,7 @@ int com_global_db(int a_argc, char ** a_argv, char **a_str_reply) return -1; } dap_chain_cell_t *l_cell = dap_chain_cell_create_fill(l_chain, l_cell_id); - int l_ret = dap_chain_cell_file_update(l_cell); + int l_ret = (int)dap_chain_cell_file_update(l_cell); if(l_ret > 0) dap_cli_server_cmd_set_reply_text(a_str_reply, "cell added successfully"); else @@ -921,6 +905,11 @@ int com_global_db(int a_argc, char ** a_argv, char **a_str_reply) char *l_hash_str; dap_get_data_hash_str_static(l_value, l_value_len, l_hash_str); char *l_value_str = DAP_NEW_Z_SIZE(char, l_value_len * 2 + 2); + if(!l_value_str) { + log_it(L_CRITICAL, "Memory allocation error"); + DAP_DELETE(l_value); + return -1; + } size_t ret = dap_bin2hex(l_value_str, l_value, l_value_len); dap_cli_server_cmd_set_reply_text(a_str_reply, "Record found\n" "lenght:\t%zu byte\n" @@ -1193,9 +1182,13 @@ int com_node(int a_argc, char ** a_argv, char **a_str_reply) dap_chain_node_addr_t l_link = { 0 }; dap_chain_node_info_t *l_node_info = NULL; size_t l_node_info_size = sizeof(l_node_info->hdr) + sizeof(l_link); - - if(cmd_num >= CMD_ADD && cmd_num <= CMD_LINK) + if(cmd_num >= CMD_ADD && cmd_num <= CMD_LINK) { l_node_info = DAP_NEW_Z_SIZE(dap_chain_node_info_t, l_node_info_size); + if (!l_node_info) { + log_it(L_CRITICAL, "Memory allocation error"); + return -1; + } + } if(l_addr_str) { if(dap_chain_node_addr_from_str(&l_node_addr, l_addr_str) != 0) { @@ -1920,8 +1913,6 @@ int com_tx_wallet(int a_argc, char **a_argv, char **a_str_reply) const char *c_wallets_path = dap_chain_wallet_get_path(g_config); enum { CMD_NONE, CMD_WALLET_NEW, CMD_WALLET_LIST, CMD_WALLET_INFO, CMD_WALLET_ACTIVATE, CMD_WALLET_DEACTIVATE, CMD_WALLET_CONVERT }; int l_arg_index = 1, l_rc, cmd_num = CMD_NONE; -char l_buf[1024]; - // find add parameter ('alias' or 'handshake') if(dap_cli_server_cmd_find_option_val(a_argv, l_arg_index, MIN(a_argc, l_arg_index + 1), "new", NULL)) @@ -1952,337 +1943,309 @@ char l_buf[1024]; dap_cli_server_cmd_find_option_val(a_argv, l_arg_index, a_argc, "-addr", &l_addr_str); dap_cli_server_cmd_find_option_val(a_argv, l_arg_index, a_argc, "-w", &l_wallet_name); dap_cli_server_cmd_find_option_val(a_argv, l_arg_index, a_argc, "-net", &l_net_name); + dap_cli_server_cmd_find_option_val(a_argv, l_arg_index, a_argc, "-password", &l_pass_str); + dap_cli_server_cmd_find_option_val(a_argv, l_arg_index, a_argc, "-sign", &l_sign_type_str); + // Check if wallet name has only digits and English letter + if (l_wallet_name && !dap_isstralnum(l_wallet_name)){ + dap_cli_server_cmd_set_reply_text(a_str_reply, "Wallet name must contains digits and aplhabetical symbols"); + return -1; + } + dap_chain_net_t * l_net = l_net_name ? dap_chain_net_by_name(l_net_name) : NULL; + dap_string_t *l_string_ret = dap_string_new(NULL); + dap_chain_wallet_t *l_wallet = NULL; + dap_chain_addr_t *l_addr = NULL; - dap_chain_net_t * l_net = l_net_name ? dap_chain_net_by_name( l_net_name) : NULL; - - dap_string_t *l_l_string_ret = dap_string_new(NULL); - - - switch (cmd_num) - { - case CMD_WALLET_ACTIVATE: - case CMD_WALLET_DEACTIVATE: - dap_cli_server_cmd_find_option_val(a_argv, l_arg_index, a_argc, "-password", &l_pass_str); - dap_cli_server_cmd_find_option_val(a_argv, l_arg_index, a_argc, "-ttl", &l_ttl_str); - - - if( !l_wallet_name ) - return dap_cli_server_cmd_set_reply_text(a_str_reply, "Wallet name option <-w> not defined"), -EINVAL; - - if( !l_pass_str ) - return dap_cli_server_cmd_set_reply_text(a_str_reply, "Wallet password option <-password> not defined"), -EINVAL; - - if ( l_ttl_str ) - l_rc = strtoul(l_ttl_str, NULL, 10); - else l_rc = 60; - l_rc = l_rc ? l_rc : 60; - - if ( cmd_num == CMD_WALLET_ACTIVATE ) - l_rc = dap_chain_wallet_activate (l_wallet_name, strlen(l_wallet_name), l_pass_str, strlen(l_pass_str), l_rc ); - else l_rc = dap_chain_wallet_deactivate (l_wallet_name, strlen(l_wallet_name), l_pass_str, strlen(l_pass_str) ); - - if ( !l_rc ) - dap_string_append_printf(l_l_string_ret, "Wallet: %s is %sactivated\n", - l_wallet_name, cmd_num == CMD_WALLET_ACTIVATE ? "" : "de"); - else - { - switch ( l_rc ) - { - case -EBUSY: - strcpy(l_buf, "already activated"); - break; - - case -EINVAL: - strcpy(l_buf, "wrong password"); - break; - - - default: - strerror_r(l_rc, l_buf, sizeof(l_buf) - 1 ); - break; - } - - dap_string_append_printf(l_l_string_ret, "Wallet: %s %sactivation error, errno=%d (%s)\n", - l_wallet_name, cmd_num == CMD_WALLET_ACTIVATE ? "" : "de", l_rc, l_buf ); - } - - break; - - - // new wallet - case CMD_WALLET_NEW: { - dap_cli_server_cmd_find_option_val(a_argv, l_arg_index, a_argc, "-password", &l_pass_str); - dap_cli_server_cmd_find_option_val(a_argv, l_arg_index, a_argc, "-sign", &l_sign_type_str); - int l_restore_opt = dap_cli_server_cmd_find_option_val(a_argv, l_arg_index, a_argc, "-restore", &l_restore_str); - int l_restore_legacy_opt = 0; - if (!l_restore_str) - l_restore_legacy_opt = dap_cli_server_cmd_find_option_val(a_argv, l_arg_index, a_argc, "-restore_legacy", &l_restore_str); - // rewrite existing wallet - int l_is_force = dap_cli_server_cmd_find_option_val(a_argv, l_arg_index, a_argc, "-force", NULL); - - if(!l_wallet_name) { - dap_cli_server_cmd_set_reply_text(a_str_reply, "Wallet name option <-w> not defined"); - return -1; - } - // Check if wallet name has only digits and English letter - if (!dap_isstralnum(l_wallet_name)){ - dap_cli_server_cmd_set_reply_text(a_str_reply, "Wallet name must contains digits and aplhabetical symbols"); - return -1; - } - - // check wallet existence - if (!l_is_force) { - char *l_file_name = dap_strdup_printf("%s/%s.dwallet", c_wallets_path, l_wallet_name); - FILE *l_exists = fopen(l_file_name, "rb"); - DAP_DELETE(l_file_name); - if (l_exists) { - dap_cli_server_cmd_set_reply_text(a_str_reply, "Wallet %s already exists", l_wallet_name); - fclose(l_exists); - return -1; - } - } - - dap_sign_type_t l_sign_type; - if (!l_sign_type_str) { - l_sign_type.type = SIG_TYPE_DILITHIUM; - l_sign_type_str = dap_sign_type_to_str(l_sign_type); - } else { - l_sign_type = dap_sign_type_from_str(l_sign_type_str); - if (l_sign_type.type == SIG_TYPE_NULL){ - dap_cli_server_cmd_set_reply_text(a_str_reply, "Unknown signature type"); - return -1; - } - } - - // - // Check unsupported tesla algorithm - // - - if (l_sign_type.type == SIG_TYPE_TESLA) - return dap_cli_server_cmd_set_reply_text(a_str_reply, "Tesla algorithm is no longer supported, please, use another variant"), -1; - - uint8_t *l_seed = NULL; - size_t l_seed_size = 0, l_restore_str_size = dap_strlen(l_restore_str); - - if(l_restore_opt || l_restore_legacy_opt) { - if (l_restore_str_size > 3 && !dap_strncmp(l_restore_str, "0x", 2) && (!dap_is_hex_string(l_restore_str + 2, l_restore_str_size - 2) || l_restore_legacy_opt)) { - l_seed_size = (l_restore_str_size - 2) / 2; - l_seed = DAP_NEW_SIZE(uint8_t, l_seed_size); - dap_hex2bin(l_seed, l_restore_str + 2, l_restore_str_size - 2); - if (l_restore_legacy_opt) { - dap_string_append_printf(l_l_string_ret, "CAUTION!!! CAUTION!!! CAUTION!!!\nYour wallet has a low level of protection. Please create a new wallet again with the option -restore\n"); - } - } else { - dap_cli_server_cmd_set_reply_text(a_str_reply, "Restored hash is invalid or too short, wallet is not created. Please use -restore 0x<hex_value> or -restore_legacy 0x<restore_string>"); - return -1; - } - } - - // Creates new wallet - dap_chain_wallet_t *l_wallet = dap_chain_wallet_create_with_seed(l_wallet_name, c_wallets_path, l_sign_type, - l_seed, l_seed_size, l_pass_str); - - if (!l_wallet) - return dap_cli_server_cmd_set_reply_text(a_str_reply, "Wallet is not created because of internal error. Check name or password length (max 64 chars)"), -1; - - dap_chain_addr_t *l_addr = l_net? dap_chain_wallet_get_addr(l_wallet,l_net->pub.id ) : NULL; - - char *l_addr_str = l_addr? dap_chain_addr_to_str(l_addr) : NULL; - dap_string_append_printf(l_l_string_ret, "Wallet: %s (type=%s) successfully created\n", l_wallet->name, l_sign_type_str); - if ( l_addr_str ) { - dap_string_append_printf(l_l_string_ret, "new address %s", l_addr_str); - DAP_DELETE(l_addr_str); - } - dap_chain_wallet_close(l_wallet); - } - break; - + if(l_net_name && !l_net) { + dap_cli_server_cmd_set_reply_text(a_str_reply, "Not found net by name '%s'", l_net_name); + return -1; + } + switch (cmd_num) { // wallet list - case CMD_WALLET_LIST: - { + case CMD_WALLET_LIST: { DIR * l_dir = opendir(c_wallets_path); if(l_dir) { - struct dirent * l_dir_entry; + struct dirent * l_dir_entry = NULL; - while( (l_dir_entry = readdir(l_dir)) ) - { + while( (l_dir_entry = readdir(l_dir)) ) { const char *l_file_name = l_dir_entry->d_name; size_t l_file_name_len = (l_file_name) ? strlen(l_file_name) : 0; - if ( (l_file_name_len > 8) && (!strcmp(l_file_name + l_file_name_len - 8, ".dwallet")) ) - { - + if ( (l_file_name_len > 8) && (!strcmp(l_file_name + l_file_name_len - 8, ".dwallet")) ) { char l_file_path_tmp[MAX_PATH] = {0}; snprintf(l_file_path_tmp, sizeof(l_file_path_tmp) - 1, "%s/%s", c_wallets_path, l_file_name); - dap_chain_wallet_t *l_wallet = dap_chain_wallet_open(l_file_name, c_wallets_path); + l_wallet = dap_chain_wallet_open(l_file_name, c_wallets_path); - if (l_wallet) - { - dap_chain_addr_t *l_addr = l_net? dap_chain_wallet_get_addr(l_wallet, l_net->pub.id) : NULL; + if (l_wallet) { + l_addr = l_net ? dap_chain_wallet_get_addr(l_wallet, l_net->pub.id) : NULL; char *l_addr_str = dap_chain_addr_to_str(l_addr); - dap_string_append_printf(l_l_string_ret, "Wallet: %.*s%s\n", (int) l_file_name_len - 8, l_file_name, - (l_wallet->flags & DAP_WALLET$M_FL_ACTIVE) ? " (Active)" : ""); + dap_string_append_printf(l_string_ret, "Wallet: %.*s%s %s\n", (int) l_file_name_len - 8, l_file_name, + (l_wallet->flags & DAP_WALLET$M_FL_ACTIVE) ? " (Active)" : "", + dap_chain_wallet_check_bliss_sign(l_wallet)); - if (l_addr_str) - { - dap_string_append_printf(l_l_string_ret, "addr: %s\n", (l_addr_str) ? l_addr_str : "-"); + if (l_addr_str) { + dap_string_append_printf(l_string_ret, "addr: %s\n", (l_addr_str) ? l_addr_str : "-"); DAP_DELETE(l_addr_str); } dap_chain_wallet_close(l_wallet); - } else dap_string_append_printf(l_l_string_ret, "Wallet: %.*s (non-Active)\n", (int) l_file_name_len - 8, l_file_name); + } else dap_string_append_printf(l_string_ret, "Wallet: %.*s (non-Active)\n", (int) l_file_name_len - 8, l_file_name); } else if ((l_file_name_len > 7) && (!strcmp(l_file_name + l_file_name_len - 7, ".backup"))) { - dap_string_append_printf(l_l_string_ret, "Wallet: %.*s (Backup)\n", (int) l_file_name_len - 7, l_file_name); + dap_string_append_printf(l_string_ret, "Wallet: %.*s (Backup)\n", (int) l_file_name_len - 7, l_file_name); } } closedir(l_dir); } + break; } - break; - // wallet info case CMD_WALLET_INFO: { - dap_chain_wallet_t *l_wallet = NULL; - dap_chain_addr_t *l_addr = NULL; - if (l_wallet_name && l_addr_str) { - dap_cli_server_cmd_set_reply_text(a_str_reply, "You can use either the -w or -addr option for the wallet info command."); + dap_ledger_t *l_ledger = NULL; + if (l_wallet_name && l_addr_str || !l_wallet_name && !l_addr_str) { + dap_cli_server_cmd_set_reply_text(a_str_reply, "You should use either the -w or -addr option for the wallet info command."); + dap_string_free(l_string_ret, true); return -1; } - if(l_wallet_name) { + if(!l_net) { + dap_cli_server_cmd_set_reply_text(a_str_reply, "Subcommand info requires parameter '-net'"); + dap_string_free(l_string_ret, true); + return -1; + } l_wallet = dap_chain_wallet_open(l_wallet_name, c_wallets_path); - if ( l_net ) - l_addr = (dap_chain_addr_t *) dap_chain_wallet_get_addr(l_wallet, l_net->pub.id ); - } - if(!l_addr && l_addr_str) + l_addr = (dap_chain_addr_t *) dap_chain_wallet_get_addr(l_wallet, l_net->pub.id ); + } else { l_addr = dap_chain_addr_from_str(l_addr_str); - - dap_ledger_t *l_ledger = dap_chain_ledger_by_net_name((const char *) l_net_name); - if(!l_net_name && !l_addr ) { - dap_cli_server_cmd_set_reply_text(a_str_reply, "Subcommand info requires parameter '-net'"); - return -1; } - else if (! l_addr){ - if((l_ledger = dap_chain_ledger_by_net_name(l_net_name)) == NULL) { - dap_cli_server_cmd_set_reply_text(a_str_reply, "Not found net by name '%s'", l_net_name); - return -1; - } - }else{ + + if (!l_addr){ + if(l_wallet) + dap_chain_wallet_close(l_wallet); + dap_string_free(l_string_ret, true); + dap_cli_server_cmd_set_reply_text(a_str_reply, "Wallet not found"); + return -1; + } else { l_net = dap_chain_net_by_id(l_addr->net_id); - if (l_net){ - l_ledger = l_net->pub.ledger; + if(l_net) { + l_ledger = l_net->pub.ledger; l_net_name = l_net->pub.name; - }else{ + } else { dap_cli_server_cmd_set_reply_text(a_str_reply, "Can't find network id 0x%016"DAP_UINT64_FORMAT_X" from address %s", - l_addr->net_id.uint64, l_addr_str); + l_addr->net_id.uint64, l_addr_str); + dap_string_free(l_string_ret, true); return -1; - } } - if(l_addr) { - char *l_addr_str = dap_chain_addr_to_str((dap_chain_addr_t*) l_addr); - if(l_wallet) - dap_string_append_printf(l_l_string_ret, "wallet: %s\n", l_wallet->name); - dap_string_append_printf(l_l_string_ret, "addr: %s\n", (l_addr_str) ? l_addr_str : "-"); - dap_string_append_printf(l_l_string_ret, "network: %s\n", (l_net_name ) ? l_net_name : "-"); - - size_t l_l_addr_tokens_size = 0; - char **l_l_addr_tokens = NULL; - dap_chain_ledger_addr_get_token_ticker_all(l_ledger, l_addr, &l_l_addr_tokens, &l_l_addr_tokens_size); - if(l_l_addr_tokens_size > 0) - dap_string_append_printf(l_l_string_ret, "balance:\n"); - else - dap_string_append_printf(l_l_string_ret, "balance: 0"); - - for(size_t i = 0; i < l_l_addr_tokens_size; i++) { - if(l_l_addr_tokens[i]) { - uint256_t l_balance = dap_chain_ledger_calc_balance(l_ledger, l_addr, l_l_addr_tokens[i]); - char *l_balance_coins = dap_chain_balance_to_coins(l_balance); - char *l_balance_datoshi = dap_chain_balance_print(l_balance); - dap_string_append_printf(l_l_string_ret, "\t%s (%s) %s\n", l_balance_coins, - l_balance_datoshi, l_l_addr_tokens[i]); - if(i < l_l_addr_tokens_size - 1) - dap_string_append_printf(l_l_string_ret, "\n"); - DAP_DELETE(l_balance_coins); - DAP_DELETE(l_balance_datoshi); + char *l_l_addr_str = dap_chain_addr_to_str((dap_chain_addr_t*) l_addr); + if(l_wallet) + dap_string_append_printf(l_string_ret, "%s\nwallet: %s\n", dap_chain_wallet_check_bliss_sign(l_wallet), l_wallet->name); + dap_string_append_printf(l_string_ret, "addr: %s\n", (l_l_addr_str) ? l_l_addr_str : "-"); + dap_string_append_printf(l_string_ret, "network: %s\n", (l_net_name ) ? l_net_name : "-"); + + size_t l_l_addr_tokens_size = 0; + char **l_l_addr_tokens = NULL; + dap_chain_ledger_addr_get_token_ticker_all(l_ledger, l_addr, &l_l_addr_tokens, &l_l_addr_tokens_size); + if(l_l_addr_tokens_size > 0) + dap_string_append_printf(l_string_ret, "balance:\n"); + else + dap_string_append_printf(l_string_ret, "balance: 0"); + + for(size_t i = 0; i < l_l_addr_tokens_size; i++) { + if(l_l_addr_tokens[i]) { + uint256_t l_balance = dap_chain_ledger_calc_balance(l_ledger, l_addr, l_l_addr_tokens[i]); + char *l_balance_coins = dap_chain_balance_to_coins(l_balance); + char *l_balance_datoshi = dap_chain_balance_print(l_balance); + dap_string_append_printf(l_string_ret, "\t%s (%s) %s\n", l_balance_coins, + l_balance_datoshi, l_l_addr_tokens[i]); + if(i < l_l_addr_tokens_size - 1) + dap_string_append_printf(l_string_ret, "\n"); + DAP_DELETE(l_balance_coins); + DAP_DELETE(l_balance_datoshi); - } - DAP_DELETE(l_l_addr_tokens[i]); } - DAP_DELETE(l_l_addr_tokens); - DAP_DELETE(l_addr_str); - if(l_wallet) - dap_chain_wallet_close(l_wallet); - } - else { - if(l_wallet) - dap_chain_wallet_close(l_wallet); - - dap_string_free(l_l_string_ret, true); - dap_cli_server_cmd_set_reply_text(a_str_reply, "Wallet not found"); - return -1; + DAP_DELETE(l_l_addr_tokens[i]); } + DAP_DELETE(l_l_addr_tokens); + DAP_DELETE(l_l_addr_str); + if(l_wallet) + dap_chain_wallet_close(l_wallet); + break; } - break; - - // convert wallet - case CMD_WALLET_CONVERT: { - dap_chain_wallet_t *l_wallet = NULL; - dap_cli_server_cmd_find_option_val(a_argv, l_arg_index, a_argc, "-password", &l_pass_str); - - if(!l_wallet_name) { - dap_cli_server_cmd_set_reply_text(a_str_reply, "Wallet name option <-w> not defined"); - return -EINVAL; + default: { + if( !l_wallet_name ) { + dap_string_free(l_string_ret, true); + return dap_cli_server_cmd_set_reply_text(a_str_reply, "Wallet name option <-w> not defined"), -EINVAL; } - - if(!l_pass_str) { - dap_cli_server_cmd_set_reply_text(a_str_reply, "Wallet password option <-password> not defined"); - return -EINVAL; + if( !l_pass_str && cmd_num != CMD_WALLET_NEW) { + dap_string_free(l_string_ret, true); + return dap_cli_server_cmd_set_reply_text(a_str_reply, "Wallet password option <-password> not defined"), -EINVAL; } - - if ( DAP_WALLET$SZ_PASS < strnlen(l_pass_str, DAP_WALLET$SZ_PASS + 1) ) { + if ( l_pass_str && DAP_WALLET$SZ_PASS < strnlen(l_pass_str, DAP_WALLET$SZ_PASS + 1) ) { dap_cli_server_cmd_set_reply_text(a_str_reply, "Wallet's password is too long ( > %d)", DAP_WALLET$SZ_PASS); log_it(L_ERROR, "Wallet's password is too long ( > %d)", DAP_WALLET$SZ_PASS); + dap_string_free(l_string_ret, true); return -EINVAL; } + switch (cmd_num) { + case CMD_WALLET_ACTIVATE: + case CMD_WALLET_DEACTIVATE: { + const char *l_prefix = cmd_num == CMD_WALLET_ACTIVATE ? "" : "de"; + dap_cli_server_cmd_find_option_val(a_argv, l_arg_index, a_argc, "-ttl", &l_ttl_str); + l_rc = l_ttl_str ? strtoul(l_ttl_str, NULL, 10) : 60; + + l_rc = cmd_num == CMD_WALLET_ACTIVATE + ? dap_chain_wallet_activate(l_wallet_name, strlen(l_wallet_name), l_pass_str, strlen(l_pass_str), l_rc) + : dap_chain_wallet_deactivate (l_wallet_name, strlen(l_wallet_name), l_pass_str, strlen(l_pass_str)); + + switch (l_rc) { + case 0: + dap_string_append_printf(l_string_ret, "Wallet %s is %sactivated\n", l_wallet_name, l_prefix); + break; + case -EBUSY: + dap_string_append_printf(l_string_ret, "Error: wallet %s is already %sactivated\n", l_wallet_name, l_prefix); + break; + case -EAGAIN: + dap_string_append_printf(l_string_ret, "Error: wrong password for wallet %s\n", l_wallet_name); + break; + default: { + char l_buf[512] = { '\0' }; + strerror_r(l_rc, l_buf, sizeof(l_buf) - 1); + dap_string_append_printf(l_string_ret, "Wallet %s %sactivation error %d : %s\n", l_wallet_name, l_prefix, l_rc, l_buf); + break; + } + } + } break; + // convert wallet + case CMD_WALLET_CONVERT: { + l_wallet = dap_chain_wallet_open(l_wallet_name, c_wallets_path); + if (!l_wallet) { + dap_cli_server_cmd_set_reply_text(a_str_reply, "wrong password"); + return -1; + } else if (l_wallet->flags & DAP_WALLET$M_FL_ACTIVE) { + dap_cli_server_cmd_set_reply_text(a_str_reply, "Wallet can't be converted twice"); + dap_string_free(l_string_ret, true); + return -1; + } + // create wallet backup + dap_chain_wallet_internal_t* l_file_name = DAP_CHAIN_WALLET_INTERNAL(l_wallet); + snprintf(l_file_name->file_name, sizeof(l_file_name->file_name) - 1, "%s/%s_%012lu%s", c_wallets_path, l_wallet_name, time(NULL),".backup"); + if ( dap_chain_wallet_save(l_wallet, NULL) ) { + dap_cli_server_cmd_set_reply_text(a_str_reply, "Can't create backup wallet file because of internal error"); + dap_string_free(l_string_ret, true); + return -1; + } + // change to old filename + snprintf(l_file_name->file_name, sizeof(l_file_name->file_name) - 1, "%s/%s%s", c_wallets_path, l_wallet_name, ".dwallet"); + if ( dap_chain_wallet_save(l_wallet, l_pass_str) ) { + dap_cli_server_cmd_set_reply_text(a_str_reply, "Wallet is not converted because of internal error"); + dap_string_free(l_string_ret, true); + return -1; + } - l_wallet = dap_chain_wallet_open(l_wallet_name, c_wallets_path); - if (!l_wallet) { - dap_cli_server_cmd_set_reply_text(a_str_reply, "wrong password"); - return -1; - } else if (l_wallet->flags & DAP_WALLET$M_FL_ACTIVE) { - dap_cli_server_cmd_set_reply_text(a_str_reply, "Wallet can't be converted twice"); - return -1; - } - // create wallet backup - dap_chain_wallet_internal_t* l_file_name = DAP_CHAIN_WALLET_INTERNAL(l_wallet); - snprintf(l_file_name->file_name, sizeof(l_file_name->file_name) - 1, "%s/%s_%012lu%s", c_wallets_path, l_wallet_name, time(NULL),".backup"); - if ( dap_chain_wallet_save(l_wallet, NULL) ) { - dap_cli_server_cmd_set_reply_text(a_str_reply, "Can't create backup wallet file because of internal error"); - return -1; - } - // change to old filename - snprintf(l_file_name->file_name, sizeof(l_file_name->file_name) - 1, "%s/%s%s", c_wallets_path, l_wallet_name, ".dwallet"); - if ( dap_chain_wallet_save(l_wallet, l_pass_str) ) { - dap_cli_server_cmd_set_reply_text(a_str_reply, "Wallet is not converted because of internal error"); - return -1; - } + log_it(L_INFO, "Wallet %s has been converted", l_wallet_name); + dap_string_append_printf(l_string_ret, "%s\nWallet: %s successfully converted\n", dap_chain_wallet_check_bliss_sign(l_wallet), l_wallet_name); + dap_chain_wallet_close(l_wallet); + break; + } + // new wallet + case CMD_WALLET_NEW: { + int l_restore_opt = dap_cli_server_cmd_find_option_val(a_argv, l_arg_index, a_argc, "-restore", &l_restore_str); + int l_restore_legacy_opt = 0; + if (!l_restore_str) + l_restore_legacy_opt = dap_cli_server_cmd_find_option_val(a_argv, l_arg_index, a_argc, "-restore_legacy", &l_restore_str); + // rewrite existing wallet + int l_is_force = dap_cli_server_cmd_find_option_val(a_argv, l_arg_index, a_argc, "-force", NULL); + + // check wallet existence + if (!l_is_force) { + char *l_file_name = dap_strdup_printf("%s/%s.dwallet", c_wallets_path, l_wallet_name); + FILE *l_exists = fopen(l_file_name, "rb"); + DAP_DELETE(l_file_name); + if (l_exists) { + dap_cli_server_cmd_set_reply_text(a_str_reply, "Wallet %s already exists", l_wallet_name); + fclose(l_exists); + dap_string_free(l_string_ret, true); + return -1; + } + } - log_it(L_INFO, "Wallet %s has been converted", l_wallet_name); - dap_string_append_printf(l_l_string_ret, "Wallet: %s successfully converted\n", l_wallet_name); - dap_chain_wallet_close(l_wallet); + dap_sign_type_t l_sign_type; + if (!l_sign_type_str) { + l_sign_type.type = SIG_TYPE_DILITHIUM; + l_sign_type_str = dap_sign_type_to_str(l_sign_type); + } else { + l_sign_type = dap_sign_type_from_str(l_sign_type_str); + if (l_sign_type.type == SIG_TYPE_NULL){ + dap_cli_server_cmd_set_reply_text(a_str_reply, "Unknown signature type, please use:\n sig_picnic\n sig_dil\n sig_falcon\n sig_multi\n sig_multi2\n"); + dap_string_free(l_string_ret, true); + return -1; + } + } + // Check unsupported tesla and bliss algorithm + + if (l_sign_type.type == SIG_TYPE_TESLA || l_sign_type.type == SIG_TYPE_BLISS) { + if (l_sign_type.type == SIG_TYPE_BLISS && (l_restore_opt || l_restore_legacy_opt)) { + dap_string_append_printf(l_string_ret, "CAUTION!!! CAUTION!!! CAUTION!!!\nThe Bliss signature is deprecated. We recommend you to create a new wallet with another available signature and transfer funds there.\n"); + } else { + dap_string_free(l_string_ret, true); + return dap_cli_server_cmd_set_reply_text(a_str_reply, "This signature algorithm is no longer supported, please, use another variant"), -1; + } + } + + uint8_t *l_seed = NULL; + size_t l_seed_size = 0, l_restore_str_size = dap_strlen(l_restore_str); + + if(l_restore_opt || l_restore_legacy_opt) { + if (l_restore_str_size > 3 && !dap_strncmp(l_restore_str, "0x", 2) && (!dap_is_hex_string(l_restore_str + 2, l_restore_str_size - 2) || l_restore_legacy_opt)) { + l_seed_size = (l_restore_str_size - 2) / 2; + l_seed = DAP_NEW_Z_SIZE(uint8_t, l_seed_size); + if(!l_seed) { + log_it(L_CRITICAL, "Memory allocation error"); + dap_string_free(l_string_ret, true); + return -1; + } + dap_hex2bin(l_seed, l_restore_str + 2, l_restore_str_size - 2); + if (l_restore_legacy_opt) { + dap_string_append_printf(l_string_ret, "CAUTION!!! CAUTION!!! CAUTION!!!\nYour wallet has a low level of protection. Please create a new wallet again with the option -restore\n"); + } + } else { + dap_cli_server_cmd_set_reply_text(a_str_reply, "Restored hash is invalid or too short, wallet is not created. Please use -restore 0x<hex_value> or -restore_legacy 0x<restore_string>"); + dap_string_free(l_string_ret, true); + return -1; + } + } + + // Creates new wallet + l_wallet = dap_chain_wallet_create_with_seed(l_wallet_name, c_wallets_path, l_sign_type, + l_seed, l_seed_size, l_pass_str); + DAP_DELETE(l_seed); + if (!l_wallet) { + dap_string_free(l_string_ret, true); + return dap_cli_server_cmd_set_reply_text(a_str_reply, "Wallet is not created because of internal error. Check name or password length (max 64 chars)"), -1; + } + + l_addr = l_net? dap_chain_wallet_get_addr(l_wallet,l_net->pub.id ) : NULL; + + char *l_l_addr_str = l_addr ? dap_chain_addr_to_str(l_addr) : NULL; + dap_string_append_printf(l_string_ret, "Wallet: %s (type=%s) successfully created\n", l_wallet->name, l_sign_type_str); + if ( l_l_addr_str ) { + dap_string_append_printf(l_string_ret, "new address %s", l_l_addr_str); + DAP_DELETE(l_l_addr_str); + } + dap_chain_wallet_close(l_wallet); + break; + } + } } - break; } - *a_str_reply = dap_string_free(l_l_string_ret, false); + *a_str_reply = dap_string_free(l_string_ret, false); return 0; } @@ -2731,36 +2694,33 @@ static bool dap_chain_mempool_find_addr_ledger(dap_ledger_t* a_ledger, dap_chain { dap_chain_datum_tx_t *l_tx; l_tx = dap_chain_ledger_tx_find_by_hash (a_ledger,a_tx_prev_hash); - dap_list_t *l_list_out_items = dap_chain_datum_tx_items_get(l_tx, TX_ITEM_TYPE_OUT_ALL, NULL); + dap_list_t *l_list_out_items = dap_chain_datum_tx_items_get(l_tx, TX_ITEM_TYPE_OUT_ALL, NULL), *l_item; if(!l_list_out_items) return false; - for(dap_list_t *l_list_out = l_list_out_items; l_list_out; l_list_out = dap_list_next(l_list_out)) { - assert(l_list_out->data); + bool l_ret = false; + DL_FOREACH(l_list_out_items, l_item) { + //assert(l_list_out->data); dap_chain_addr_t *l_dst_addr = NULL; - dap_chain_tx_item_type_t l_type = *(uint8_t *)l_list_out->data; + dap_chain_tx_item_type_t l_type = *(uint8_t*)l_item->data; switch (l_type) { case TX_ITEM_TYPE_OUT: - l_dst_addr = &((dap_chain_tx_out_t *)l_list_out->data)->addr; + l_dst_addr = &((dap_chain_tx_out_t*)l_item->data)->addr; break; case TX_ITEM_TYPE_OUT_EXT: - l_dst_addr = &((dap_chain_tx_out_ext_t *)l_list_out->data)->addr; + l_dst_addr = &((dap_chain_tx_out_ext_t*)l_item->data)->addr; break; case TX_ITEM_TYPE_OUT_OLD: - l_dst_addr = &((dap_chain_tx_out_old_t *)l_list_out->data)->addr; + l_dst_addr = &((dap_chain_tx_out_old_t*)l_item->data)->addr; default: break; } - if(l_dst_addr) - { - if(!memcmp(l_dst_addr, a_addr, sizeof(dap_chain_addr_t))) - { - dap_list_free(l_list_out_items); - return true; - } + if(l_dst_addr && !memcmp(l_dst_addr, a_addr, sizeof(dap_chain_addr_t))) { + l_ret = true; + break; } } dap_list_free(l_list_out_items); - return false; + return l_ret; } /** @@ -3570,9 +3530,13 @@ static int s_parse_additional_token_decl_arg(int a_argc, char ** a_argv, char ** } size_t l_tsd_offset = 0; a_params->ext.parsed_tsd = DAP_NEW_SIZE(byte_t, l_tsd_total_size); + if(!a_params->ext.parsed_tsd) { + log_it(L_CRITICAL, "Memory allocation error"); + return -1; + } for (dap_list_t *l_iter = dap_list_first(l_tsd_list); l_iter; l_iter = l_iter->next) { dap_tsd_t * l_tsd = (dap_tsd_t *) l_iter->data; - if (l_tsd == NULL){ + if (!l_tsd){ log_it(L_ERROR, "NULL tsd in list!"); continue; } @@ -3737,8 +3701,10 @@ int com_token_decl(int a_argc, char ** a_argv, char ** a_str_reply) dap_sdk_cli_params* l_params = DAP_NEW_Z(dap_sdk_cli_params); - if (!l_params) + if (!l_params) { + log_it(L_CRITICAL, "Memory allocation error"); return -1; + } l_params->type = DAP_CHAIN_DATUM_TOKEN_TYPE_DECL; l_params->subtype = DAP_CHAIN_DATUM_TOKEN_SUBTYPE_SIMPLE; @@ -4041,8 +4007,10 @@ int com_token_update(int a_argc, char ** a_argv, char ** a_str_reply) dap_sdk_cli_params* l_params = DAP_NEW_Z(dap_sdk_cli_params); - if (!l_params) + if (!l_params) { + log_it(L_CRITICAL, "Memory allocation error"); return -1; + } l_params->type = DAP_CHAIN_DATUM_TOKEN_TYPE_UPDATE; l_params->subtype = DAP_CHAIN_DATUM_TOKEN_SUBTYPE_SIMPLE; @@ -4415,7 +4383,7 @@ int com_tx_cond_create(int a_argc, char ** a_argv, char **a_str_reply) // Token ticker dap_cli_server_cmd_find_option_val(a_argv, arg_index, a_argc, "-token", &l_token_ticker); // Wallet name - from - dap_cli_server_cmd_find_option_val(a_argv, arg_index, a_argc, "-wallet", &l_wallet_str); + dap_cli_server_cmd_find_option_val(a_argv, arg_index, a_argc, "-w", &l_wallet_str); // Public certifiacte of condition owner dap_cli_server_cmd_find_option_val(a_argv, arg_index, a_argc, "-cert", &l_cert_str); // value datoshi @@ -4434,7 +4402,7 @@ int com_tx_cond_create(int a_argc, char ** a_argv, char **a_str_reply) return -1; } if (!l_wallet_str) { - dap_cli_server_cmd_set_reply_text(a_str_reply, "tx_cond_create requires parameter '-wallet'"); + dap_cli_server_cmd_set_reply_text(a_str_reply, "tx_cond_create requires parameter '-w'"); return -2; } if (!l_cert_str) { @@ -4495,9 +4463,12 @@ int com_tx_cond_create(int a_argc, char ** a_argv, char **a_str_reply) return -11; } dap_chain_wallet_t *l_wallet = dap_chain_wallet_open(l_wallet_str, c_wallets_path); + const char* l_sign_str = ""; if(!l_wallet) { - dap_cli_server_cmd_set_reply_text(a_str_reply, "Can't open wallet '%s'", l_wallet->name); + dap_cli_server_cmd_set_reply_text(a_str_reply, "Can't open wallet '%s'", l_wallet_str); return -12; + } else { + l_sign_str = dap_chain_wallet_check_bliss_sign(l_wallet); } dap_cert_t *l_cert_cond = dap_cert_find_by_name(l_cert_str); @@ -4523,11 +4494,11 @@ int com_tx_cond_create(int a_argc, char ** a_argv, char **a_str_reply) DAP_DELETE(l_key_cond); if (l_hash_str) { - dap_cli_server_cmd_set_reply_text(a_str_reply, "Conditional 256bit TX created succefully, hash=%s\n", l_hash_str); + dap_cli_server_cmd_set_reply_text(a_str_reply, "Conditional 256bit TX created succefully, hash=%s\n%s\n", l_hash_str, l_sign_str); DAP_DELETE(l_hash_str); return 0; } - dap_cli_server_cmd_set_reply_text(a_str_reply, "Can't create conditional 256bit TX\n"); + dap_cli_server_cmd_set_reply_text(a_str_reply, "Can't create conditional 256bit TX\n%s\n", l_sign_str); return -1; } @@ -4677,31 +4648,38 @@ int com_chain_ca_pub( int a_argc, char ** a_argv, char ** a_str_reply) // Create empty new cert dap_cert_t * l_cert_new = dap_cert_new(l_ca_name); + if(!l_cert_new) + return -9; l_cert_new->enc_key = dap_enc_key_new( l_cert->enc_key->type); + if(!l_cert_new->enc_key) { + DAP_DELETE(l_cert_new); + return -10; + } // Copy only public key l_cert_new->enc_key->pub_key_data = DAP_NEW_Z_SIZE(uint8_t, l_cert_new->enc_key->pub_key_data_size = l_cert->enc_key->pub_key_data_size ); + if(!l_cert_new->enc_key->pub_key_data) { + log_it(L_CRITICAL, "Memory allocation error"); + DAP_DELETE(l_cert_new->enc_key); + DAP_DELETE(l_cert_new); + return -11; + } memcpy(l_cert_new->enc_key->pub_key_data, l_cert->enc_key->pub_key_data,l_cert->enc_key->pub_key_data_size); // Serialize certificate into memory uint32_t l_cert_serialized_size = 0; byte_t * l_cert_serialized = dap_cert_mem_save( l_cert_new, &l_cert_serialized_size ); - if( l_cert_serialized == NULL){ + if(!l_cert_serialized){ dap_cli_server_cmd_set_reply_text(a_str_reply, "Can't serialize in memory certificate" ); return -7; } - if( l_cert_serialized == NULL){ - dap_cli_server_cmd_set_reply_text(a_str_reply, - "Can't serialize in memory certificate"); - return -7; - } // Now all the chechs passed, forming datum for mempool dap_chain_datum_t * l_datum = dap_chain_datum_create( DAP_CHAIN_DATUM_CA, l_cert_serialized , l_cert_serialized_size); - DAP_DELETE( l_cert_serialized); - if( l_datum == NULL){ + DAP_DELETE(l_cert_serialized); + if(!l_datum){ dap_cli_server_cmd_set_reply_text(a_str_reply, "Can't produce datum from certificate"); return -7; @@ -4930,6 +4908,10 @@ int com_tx_create_json(int a_argc, char ** a_argv, char **a_str_reply) log_it(L_ERROR, "Json TX: found %lu items", l_items_count); // Create transaction dap_chain_datum_tx_t *l_tx = DAP_NEW_Z_SIZE(dap_chain_datum_tx_t, sizeof(dap_chain_datum_tx_t)); + if(!l_tx) { + log_it(L_CRITICAL, "Memory allocation error"); + return -16; + } l_tx->header.ts_created = time(NULL); size_t l_items_ready = 0; size_t l_receipt_count = 0; @@ -5381,6 +5363,7 @@ int com_tx_create_json(int a_argc, char ** a_argv, char **a_str_reply) else{ dap_string_append_printf(l_err_str, "Can't create sign for transactions.\n"); log_it(L_ERROR, "Json TX: Item sign has no wallet or cert of they are invalid "); + l_list = dap_list_next(l_list); continue; } @@ -5388,6 +5371,7 @@ int com_tx_create_json(int a_argc, char ** a_argv, char **a_str_reply) l_items_ready++; } else { log_it(L_ERROR, "Json TX: Item sign has invalid enc_key."); + l_list = dap_list_next(l_list); continue; } @@ -5421,7 +5405,7 @@ int com_tx_create_json(int a_argc, char ** a_argv, char **a_str_reply) dap_get_data_hash_str_static(l_datum_tx->data, l_datum_tx->header.data_size, l_tx_hash_str); bool l_placed = !dap_global_db_set(l_gdb_group_mempool_base_tx,l_tx_hash_str, l_datum_tx, l_datum_tx_size, false, NULL, NULL); - DAP_DELETE(l_datum_tx); + DAP_DEL_Z(l_datum_tx); DAP_DELETE(l_gdb_group_mempool_base_tx); if(!l_placed) { dap_cli_server_cmd_set_reply_text(a_str_reply, "Can't add transaction to mempool"); @@ -5571,7 +5555,7 @@ int com_tx_create(int a_argc, char **a_argv, char **a_str_reply) l_priv_key = l_cert->enc_key; } else { dap_cli_server_cmd_set_reply_text(a_str_reply, - "tx_create requires parameter '-cert' or '-wallet' for create base tx for emission"); + "tx_create requires parameter '-cert' or '-wallet_fee' for create base tx for emission"); return -10; } } @@ -5596,13 +5580,13 @@ int com_tx_create(int a_argc, char **a_argv, char **a_str_reply) } dap_string_t *l_string_ret = dap_string_new(NULL); - int res = 0; + int l_ret = 0; if (l_emission_hash_str) { char *l_tx_hash_str = NULL; if (!l_priv_key) { dap_string_append_printf(l_string_ret, "No private key defined for creating the underlying " "transaction no '-wallet_fee' or ' -cert' parameter specified."); - res = -10; + l_ret = -10; } l_tx_hash_str = dap_chain_mempool_base_tx_create(l_chain, &l_emission_hash, l_emission_chain->id, l_value, l_token_ticker, l_addr_to, l_priv_key, @@ -5612,14 +5596,14 @@ int com_tx_create(int a_argc, char **a_argv, char **a_str_reply) DAP_DELETE(l_tx_hash_str); } else { dap_string_append_printf(l_string_ret, "\nCan't place TX datum in mempool, examine log files\n"); - res = -15; + l_ret = -15; } dap_cli_server_cmd_set_reply_text(a_str_reply, "%s", l_string_ret->str); dap_string_free(l_string_ret, true); DAP_DELETE(l_addr_to); dap_chain_wallet_close(l_wallet_fee); DAP_DEL_Z(l_cert); - return res; + return l_ret; } dap_chain_wallet_t * l_wallet = dap_chain_wallet_open(l_from_wallet_name, c_wallets_path); @@ -5627,6 +5611,8 @@ int com_tx_create(int a_argc, char **a_argv, char **a_str_reply) if(!l_wallet) { dap_cli_server_cmd_set_reply_text(a_str_reply, "wallet %s does not exist", l_from_wallet_name); return -9; + } else { + dap_string_append_printf(l_string_ret, "%s\n", dap_chain_wallet_check_bliss_sign(l_wallet)); } const dap_chain_addr_t *addr_from = (const dap_chain_addr_t *) dap_chain_wallet_get_addr(l_wallet, l_net->pub.id); @@ -5659,20 +5645,20 @@ int com_tx_create(int a_argc, char **a_argv, char **a_str_reply) } if(l_tx_num){ - res = dap_chain_mempool_tx_create_massive(l_chain, dap_chain_wallet_get_key(l_wallet, 0), addr_from, + l_ret = dap_chain_mempool_tx_create_massive(l_chain, dap_chain_wallet_get_key(l_wallet, 0), addr_from, l_addr_to, l_token_ticker, l_value, l_value_fee, l_tx_num); dap_string_append_printf(l_string_ret, "transfer=%s\n", - (res == 0) ? "Ok" : (res == -2) ? "False, not enough funds for transfer" : "False"); - }else{ + (l_ret == 0) ? "Ok" : (l_ret == -2) ? "False, not enough funds for transfer" : "False"); + } else { char *l_tx_hash_str = dap_chain_mempool_tx_create(l_chain, dap_chain_wallet_get_key(l_wallet, 0), addr_from, l_addr_to, l_token_ticker, l_value, l_value_fee, l_hash_out_type); if (l_tx_hash_str) { dap_string_append_printf(l_string_ret, "transfer=Ok\ntx_hash=%s\n",l_tx_hash_str); DAP_DELETE(l_tx_hash_str); - }else{ + } else { dap_string_append_printf(l_string_ret, "transfer=False\n"); - res = -14; + l_ret = -14; } } dap_cli_server_cmd_set_reply_text(a_str_reply, "%s", l_string_ret->str); @@ -5680,7 +5666,7 @@ int com_tx_create(int a_argc, char **a_argv, char **a_str_reply) DAP_DELETE(l_addr_to); dap_chain_wallet_close(l_wallet); - return res; + return l_ret; } @@ -5825,10 +5811,12 @@ int com_tx_history(int a_argc, char ** a_argv, char **a_str_reply) } else l_net = dap_chain_net_by_id(l_addr->net_id); } + const char* l_sign_str = ""; if (l_wallet_name) { const char *c_wallets_path = dap_chain_wallet_get_path(g_config); dap_chain_wallet_t *l_wallet = dap_chain_wallet_open(l_wallet_name, c_wallets_path); if (l_wallet) { + l_sign_str = dap_chain_wallet_check_bliss_sign(l_wallet); dap_chain_addr_t *l_addr_tmp = dap_chain_wallet_get_addr(l_wallet, l_net->pub.id); if (l_addr) { if (!dap_chain_addr_compare(l_addr, l_addr_tmp)) { @@ -5924,17 +5912,17 @@ int com_tx_history(int a_argc, char ** a_argv, char **a_str_reply) dap_string_free(l_tx_all_str, true); } - char *l_str_ret = NULL; + dap_string_t *l_str_ret = dap_string_new(""); if (l_addr) { char *l_addr_str = dap_chain_addr_to_str(l_addr); - l_str_ret = dap_strdup_printf("History for addr %s:\n%s", l_addr_str, - l_str_out ? l_str_out : " empty"); + dap_string_append_printf(l_str_ret, "%s\n%s\n", dap_strdup_printf("History for addr %s:\n%s", l_addr_str, + l_str_out ? l_str_out : " empty"), l_sign_str); DAP_DELETE(l_addr_str); DAP_DELETE(l_str_out); } else l_str_ret = l_str_out; - dap_cli_server_cmd_set_reply_text(a_str_reply, "%s", l_str_ret); - DAP_DELETE(l_str_ret); + dap_cli_server_cmd_set_reply_text(a_str_reply, "%s", l_str_ret->str); + dap_string_free(l_str_ret, true); return 0; } @@ -6125,6 +6113,10 @@ int cmd_gdb_export(int a_argc, char **a_argv, char **a_str_reply) for (size_t i = 0; i < l_store_obj_count; ++i) { size_t l_out_size = DAP_ENC_BASE64_ENCODE_SIZE((int64_t)l_store_obj[i].value_len) + 1; char *l_value_enc_str = DAP_NEW_Z_SIZE(char, l_out_size); + if(!l_value_enc_str) { + log_it(L_CRITICAL, "Memory allocation error"); + return -1; + } dap_enc_base64_encode(l_store_obj[i].value, l_store_obj[i].value_len, l_value_enc_str, DAP_ENC_DATA_TYPE_B64); struct json_object *jobj = json_object_new_object(); json_object_object_add(jobj, "id", json_object_new_int64((int64_t)l_store_obj[i].id)); @@ -6205,6 +6197,10 @@ int cmd_gdb_import(int a_argc, char **a_argv, char ** a_str_reply) struct json_object *l_json_records = json_object_object_get(l_group_obj, "records"); size_t l_records_count = json_object_array_length(l_json_records); pdap_store_obj_t l_group_store = DAP_NEW_Z_SIZE(dap_store_obj_t, l_records_count * sizeof(dap_store_obj_t)); + if(!l_group_store) { + log_it(L_CRITICAL, "Memory allocation error"); + return -1; + } for (size_t j = 0; j < l_records_count; ++j) { struct json_object *l_record, *l_id, *l_key, *l_value, *l_value_len, *l_ts; l_record = json_object_array_get_idx(l_json_records, j); @@ -6223,6 +6219,11 @@ int cmd_gdb_import(int a_argc, char **a_argv, char ** a_str_reply) l_group_store[j].type = 'a'; const char *l_value_str = json_object_get_string(l_value); char *l_val = DAP_NEW_Z_SIZE(char, l_group_store[j].value_len); + if(!l_val) { + log_it(L_CRITICAL, "Memory allocation error"); + l_records_count = j; + break; + } dap_enc_base64_decode(l_value_str, strlen(l_value_str), l_val, DAP_ENC_DATA_TYPE_B64); l_group_store[j].value = (uint8_t*)l_val; } @@ -7005,6 +7006,6 @@ static dap_tsd_t *s_alloc_metadata (const char *a_file, const int a_meta) default: return NULL; } - return NULL; } + diff --git a/modules/net/dap_chain_node_cli_cmd_tx.c b/modules/net/dap_chain_node_cli_cmd_tx.c index fc679140dda0ce37200515b8d74c79d70d7f46a4..c876a3435da80fd65588554e885755fee880b327 100644 --- a/modules/net/dap_chain_node_cli_cmd_tx.c +++ b/modules/net/dap_chain_node_cli_cmd_tx.c @@ -735,20 +735,14 @@ int com_ledger(int a_argc, char ** a_argv, char **a_str_reply) dap_chain_addr_t *l_addr = NULL; // if need addr + const char* l_sign_str = ""; if(l_wallet_name || l_addr_base58) { if(l_wallet_name) { const char *c_wallets_path = dap_chain_wallet_get_path(g_config); dap_chain_wallet_t * l_wallet = dap_chain_wallet_open(l_wallet_name, c_wallets_path); if(l_wallet) { - dap_chain_addr_t *l_addr_tmp = (dap_chain_addr_t *) dap_chain_wallet_get_addr(l_wallet, - l_net->pub.id); - l_addr = DAP_NEW_SIZE(dap_chain_addr_t, sizeof(dap_chain_addr_t)); - if (!l_addr) { - dap_cli_server_cmd_set_reply_text(a_str_reply, "Out of memory!"); - log_it(L_CRITICAL, "Memory allocation error"); - return -1; - } - memcpy(l_addr, l_addr_tmp, sizeof(dap_chain_addr_t)); + l_sign_str = dap_chain_wallet_check_bliss_sign(l_wallet); + l_addr = dap_chain_wallet_get_addr(l_wallet, l_net->pub.id); dap_chain_wallet_close(l_wallet); } } @@ -805,7 +799,7 @@ int com_ledger(int a_argc, char ** a_argv, char **a_str_reply) DAP_DELETE(l_str_out); DAP_DELETE(l_addr); s_dap_chain_tx_hash_processed_ht_free(&l_list_tx_hash_processd); - dap_cli_server_cmd_set_reply_text(a_str_reply, "%s", l_str_ret->str); + dap_cli_server_cmd_set_reply_text(a_str_reply, "%s\n%s", l_str_ret->str, l_sign_str); dap_string_free(l_str_ret, true); return 0; } @@ -873,7 +867,7 @@ int com_ledger(int a_argc, char ** a_argv, char **a_str_reply) } dap_string_t *l_str_ret = dap_string_new(""); dap_list_t *l_token_list = dap_chain_ledger_token_info(l_ledger); - dap_string_append_printf(l_str_ret, "Found %u tokens in %s ledger\n", dap_list_length(l_token_list), l_net_str); + dap_string_append_printf(l_str_ret, "Found %lu tokens in %s ledger\n", dap_list_length(l_token_list), l_net_str); for (dap_list_t *l_list = l_token_list; l_list; l_list = dap_list_next(l_list)) { dap_string_append(l_str_ret, (char *)l_list->data); } @@ -1013,7 +1007,7 @@ int com_token(int a_argc, char ** a_argv, char **a_str_reply) l_subcmd = SUBCMD_TX_ALL; else if(dap_cli_server_cmd_find_option_val(a_argv, 2, a_argc, "-addr", &l_addr_base58_str)) l_subcmd = SUBCMD_TX_ADDR; - else if(dap_cli_server_cmd_find_option_val(a_argv, 2, a_argc, "-wallet", &l_wallet_name)) + else if(dap_cli_server_cmd_find_option_val(a_argv, 2, a_argc, "-w", &l_wallet_name)) l_subcmd = SUBCMD_TX_ADDR; const char *l_token_name_str = NULL; diff --git a/modules/net/srv/dap_chain_net_srv.c b/modules/net/srv/dap_chain_net_srv.c index 109a91a54f79ffd6a1425fcfc9ba5c82bc6da6da..b9d6e27dbd37087bd6083799631aab576d3d21e7 100644 --- a/modules/net/srv/dap_chain_net_srv.c +++ b/modules/net/srv/dap_chain_net_srv.c @@ -794,31 +794,22 @@ static bool s_pay_verificator_callback(dap_ledger_t * a_ledger, dap_chain_tx_out } // Check out value is equal to value in receipt - int items_count = 0; - dap_list_t * items_list = dap_chain_datum_tx_items_get(a_tx_in, TX_ITEM_TYPE_OUT, &items_count); - dap_chain_addr_t l_provider_addr = {}; + dap_list_t *l_items_list = dap_chain_datum_tx_items_get(a_tx_in, TX_ITEM_TYPE_OUT, NULL), *l_item; + dap_chain_addr_t l_provider_addr = { }; dap_chain_addr_fill(&l_provider_addr, l_provider_sign_type, &l_provider_pkey_hash, dap_chain_net_id_by_name(a_ledger->net_name)); - - dap_list_t * list_item = items_list; - for (int i = 0; i < items_count; i++){ - dap_chain_tx_out_t *l_out = (dap_chain_tx_out_t*)list_item->data; - if (dap_chain_addr_compare(&l_provider_addr, &l_out->addr)) - { - if(!compare256(l_out->header.value, l_receipt->receipt_info.value_datoshi)){ - dap_list_free(items_list); - return true; - }else{ - dap_list_free(items_list); - log_it(L_ERROR, "Value in tx out is not equal to value in receipt."); - return false; - + int l_ret = -1; + DL_FOREACH(l_items_list, l_item) { + if (dap_chain_addr_compare(&l_provider_addr, &((dap_chain_tx_out_t*)l_item->data)->addr)) { + l_ret = !compare256(((dap_chain_tx_out_t*)l_item->data)->header.value, l_receipt->receipt_info.value_datoshi) ? 0 : 1; + if (l_ret) { + log_it(L_ERROR, "Value in tx out is not equal to value in receipt"); // TODO: print the balances! } + break; } - items_list = items_list->next; } - dap_list_free(items_list); - log_it(L_ERROR, "Can't find OUT in tx matching provider."); - return false; + dap_list_free(l_items_list); + debug_if(l_ret == -1, L_ERROR, "Not found out in tx matching provider addr"); + return !l_ret; } int dap_chain_net_srv_price_apply_from_my_order(dap_chain_net_srv_t *a_srv, const char *a_config_section){ diff --git a/modules/net/srv/include/dap_chain_net_srv_order.h b/modules/net/srv/include/dap_chain_net_srv_order.h index db9ab023e0494fd1a5a51de13e6ab74bb625bad9..46e12790aa8bc11a8bac3874a6c2d926757a419e 100644 --- a/modules/net/srv/include/dap_chain_net_srv_order.h +++ b/modules/net/srv/include/dap_chain_net_srv_order.h @@ -129,7 +129,7 @@ char *dap_chain_net_srv_order_create(dap_chain_net_t * a_net, dap_chain_hash_fast_t a_tx_cond_hash, // Hash index of conditioned transaction attached with order uint256_t *a_price, // service price in datoshi, for SERV_CLASS_ONCE ONCE for the whole service, for SERV_CLASS_PERMANENT for one unit. dap_chain_net_srv_price_unit_uid_t a_price_unit, // Unit of service (seconds, megabytes, etc.) Only for SERV_CLASS_PERMANENT - const char a_price_ticker[], + const char a_price_ticker[DAP_CHAIN_TICKER_SIZE_MAX], dap_time_t a_expires, // TS when the service expires const uint8_t *a_ext, uint32_t a_ext_size, diff --git a/modules/service/stake/dap_chain_net_srv_stake_lock.c b/modules/service/stake/dap_chain_net_srv_stake_lock.c index 25e102506d113b73b269380ac1df65c408ac5943..ea265e83de2c9515e147c12259dab1404e459aa1 100644 --- a/modules/service/stake/dap_chain_net_srv_stake_lock.c +++ b/modules/service/stake/dap_chain_net_srv_stake_lock.c @@ -120,14 +120,14 @@ int dap_chain_net_srv_stake_lock_init() "Command:" "stake_lock hold\n" "Required parameters:\n" - "-net <net name> -wallet <wallet name> -time_staking <in YYMMDD>\n" + "-net <net name> -w <wallet name> -time_staking <in YYMMDD>\n" "-token <ticker> -value <value> -fee <value>\n" "Optional parameters:\n" "-chain <chain> -reinvest <percentage from 1 to 100>\n" "Command:" "stake_lock take\n" "Required parameters:\n" - "-net <net name> -wallet <wallet name> -tx <transaction hash> -fee <value>\n" + "-net <net name> -w <wallet name> -tx <transaction hash> -fee <value>\n" "Optional parameters:\n" "-chain <chain>\n" ); @@ -244,12 +244,12 @@ static enum error_code s_cli_hold(int a_argc, char **a_argv, int a_arg_index, da if(!l_chain) return CHAIN_ERROR; - if (!dap_cli_server_cmd_find_option_val(a_argv, a_arg_index, a_argc, "-wallet", &l_wallet_str) - || NULL == l_wallet_str) + if (!dap_cli_server_cmd_find_option_val(a_argv, a_arg_index, a_argc, "-w", &l_wallet_str) + || !l_wallet_str) return WALLET_ARG_ERROR; if (!dap_cli_server_cmd_find_option_val(a_argv, a_arg_index, a_argc, "-fee", &l_value_fee_str) - || NULL == l_value_fee_str) + || !l_value_fee_str) return FEE_ARG_ERROR; if (IS_ZERO_256( (l_value_fee = dap_chain_balance_scan(l_value_fee_str)) )) @@ -257,7 +257,7 @@ static enum error_code s_cli_hold(int a_argc, char **a_argv, int a_arg_index, da // Read time staking if (!dap_cli_server_cmd_find_option_val(a_argv, a_arg_index, a_argc, "-time_staking", &l_time_staking_str) - || NULL == l_time_staking_str) + || !l_time_staking_str) return TIME_ERROR; if (dap_strlen(l_time_staking_str) != 6) @@ -299,6 +299,8 @@ static enum error_code s_cli_hold(int a_argc, char **a_argv, int a_arg_index, da if(NULL == (l_wallet = dap_chain_wallet_open(l_wallet_str, l_wallets_path))) { dap_string_append_printf(output_line, "'%s'", l_wallet_str); return WALLET_OPEN_ERROR; + } else { + dap_string_append_printf(output_line, "%s\n", dap_chain_wallet_check_bliss_sign(l_wallet)); } if (compare256(dap_chain_wallet_get_balance(l_wallet, l_net->pub.id, l_ticker_str), l_value) == -1) { @@ -438,12 +440,12 @@ static enum error_code s_cli_take(int a_argc, char **a_argv, int a_arg_index, da } } - if (!dap_cli_server_cmd_find_option_val(a_argv, a_arg_index, a_argc, "-wallet", &l_wallet_str) - || NULL == l_wallet_str) + if (!dap_cli_server_cmd_find_option_val(a_argv, a_arg_index, a_argc, "-w", &l_wallet_str) + || !l_wallet_str) return WALLET_ARG_ERROR; if (!dap_cli_server_cmd_find_option_val(a_argv, a_arg_index, a_argc, "-fee", &l_value_fee_str) - || NULL == l_value_fee_str) + || !l_value_fee_str) return FEE_ARG_ERROR; if (IS_ZERO_256( (l_value_fee = dap_chain_balance_scan(l_value_fee_str)) )) @@ -451,6 +453,9 @@ static enum error_code s_cli_take(int a_argc, char **a_argv, int a_arg_index, da if (NULL == (l_wallet = dap_chain_wallet_open(l_wallet_str, l_wallets_path))) return WALLET_OPEN_ERROR; + else + dap_string_append_printf(output_line, "%s\n", dap_chain_wallet_check_bliss_sign(l_wallet)); + if (NULL == (l_owner_key = dap_chain_wallet_get_key(l_wallet, 0))) { dap_chain_wallet_close(l_wallet); @@ -562,7 +567,7 @@ static void s_error_handler(enum error_code errorCode, dap_string_t *output_line } break; case WALLET_ARG_ERROR: { - dap_string_append_printf(output_line, "stake_lock command requires parameter -wallet"); + dap_string_append_printf(output_line, "stake_lock command requires parameter -w"); } break; case WALLET_OPEN_ERROR: { @@ -943,8 +948,7 @@ static bool s_stake_lock_callback_verificator(dap_ledger_t *a_ledger, dap_chain_ } else l_burning_tx = a_tx_in; - int l_outs_count = 0; - dap_list_t *l_outs_list = dap_chain_datum_tx_items_get(l_burning_tx, TX_ITEM_TYPE_OUT_ALL, &l_outs_count); + dap_list_t *l_outs_list = dap_chain_datum_tx_items_get(l_burning_tx, TX_ITEM_TYPE_OUT_ALL, NULL); uint256_t l_blank_out_value = {}; for (dap_list_t *it = l_outs_list; it; it = it->next) { byte_t l_type = *(byte_t *)it->data; diff --git a/modules/service/stake/dap_chain_net_srv_stake_pos_delegate.c b/modules/service/stake/dap_chain_net_srv_stake_pos_delegate.c index 8c883b33a5cd230e8f5b329ce9c321c7f9f48412..7a06dbb7c8924155d9633708207579b30b1f47b4 100644 --- a/modules/service/stake/dap_chain_net_srv_stake_pos_delegate.c +++ b/modules/service/stake/dap_chain_net_srv_stake_pos_delegate.c @@ -76,7 +76,7 @@ int dap_chain_net_srv_stake_pos_delegate_init() "srv_stake order list -net <net_name>\n" "\tGet the fee orders list within specified net name\n" "\t\t === Commands for work with stake delegate ===\n" - "srv_stake delegate -cert <pub_cert_name> -net <net_name> -wallet <wallet_name> -value <datoshi> [-node_addr <node_addr>] -fee <value> \n" + "srv_stake delegate -cert <pub_cert_name> -net <net_name> -w <wallet_name> -value <datoshi> [-node_addr <node_addr>] -fee <value> \n" "\tDelegate public key in specified certificate with specified net name. Pay with specified value of m-tokens of native net token.\n" "srv_stake approve -net <net_name> -tx <transaction_hash> -poa_cert <priv_cert_name>\n" "\tApprove stake transaction by root node certificate within specified net name\n" @@ -85,7 +85,7 @@ int dap_chain_net_srv_stake_pos_delegate_init() "srv_stake list tx -net <net_name> \n" "\tShow the list of key delegation transactions.\n" "srv_stake invalidate -net <net_name> {-tx <transaction_hash> | -cert <delegated_cert> | -cert_pkey_hash <pkey_hash>}" - " {-wallet <wallet_name> -fee <value> | -poa_cert <cert_name>}\n" + " {-w <wallet_name> -fee <value> | -poa_cert <cert_name>}\n" "\tInvalidate requested delegated stake transaction by hash or cert name or cert pkey hash within net name and" " return m-tokens to specified wallet (if any)\n" "srv_stake min_value -net <net_name> -cert <cert_name> -value <value>" @@ -1311,16 +1311,16 @@ static void s_get_tx_filter_callback(dap_chain_net_t* a_net, dap_chain_datum_tx_ return; } -static int callback_compare_tx_list(const void * a_datum1, const void * a_datum2, void *a_unused) +static int callback_compare_tx_list(const void *a_datum1, const void *a_datum2) { - UNUSED(a_unused); - dap_chain_datum_tx_t *l_datum1 = (dap_chain_datum_tx_t*) a_datum1; - dap_chain_datum_tx_t *l_datum2 = (dap_chain_datum_tx_t*) a_datum2; - if(!l_datum1 || !l_datum2 || l_datum1->header.ts_created == l_datum2->header.ts_created) + dap_chain_datum_tx_t *l_datum1 = (dap_chain_datum_tx_t*)((dap_list_t*)a_datum1)->data, + *l_datum2 = (dap_chain_datum_tx_t*)((dap_list_t*)a_datum2)->data; + if (!l_datum1 || !l_datum2) { + log_it(L_CRITICAL, "Invalid element"); return 0; - if(l_datum1->header.ts_created > l_datum2->header.ts_created) - return 1; - return -1; + } + return l_datum1->header.ts_created == l_datum2->header.ts_created + ? 0 : l_datum1->header.ts_created > l_datum2->header.ts_created ? 1 : -1; } int dap_chain_net_srv_stake_check_validator(dap_chain_net_t * a_net, dap_hash_fast_t *a_tx_hash, dap_stream_ch_chain_validator_test_t * out_data, @@ -1574,15 +1574,18 @@ static int s_cli_srv_stake(int a_argc, char **a_argv, char **a_str_reply) dap_cli_server_cmd_set_reply_text(a_str_reply, "Network %s not found", l_net_str); return -4; } - dap_cli_server_cmd_find_option_val(a_argv, l_arg_index, a_argc, "-wallet", &l_wallet_str); + dap_cli_server_cmd_find_option_val(a_argv, l_arg_index, a_argc, "-w", &l_wallet_str); if (!l_wallet_str) { - dap_cli_server_cmd_set_reply_text(a_str_reply, "Command 'delegate' requires parameter -wallet"); + dap_cli_server_cmd_set_reply_text(a_str_reply, "Command 'delegate' requires parameter -w"); return -17; } + const char* l_sign_str = ""; dap_chain_wallet_t *l_wallet = dap_chain_wallet_open(l_wallet_str, dap_chain_wallet_get_path(g_config)); if (!l_wallet) { dap_cli_server_cmd_set_reply_text(a_str_reply, "Specified wallet not found"); return -18; + } else { + l_sign_str = dap_chain_wallet_check_bliss_sign(l_wallet); } dap_cli_server_cmd_find_option_val(a_argv, l_arg_index, a_argc, "-cert", &l_cert_str); if (!l_cert_str) { @@ -1659,7 +1662,7 @@ static int s_cli_srv_stake(int a_argc, char **a_argv, char **a_str_reply) dap_hash_fast(l_tx, dap_chain_datum_tx_get_size(l_tx), &l_tx_hash); DAP_DELETE(l_tx); char *l_tx_hash_str = dap_hash_fast_to_str_new(&l_tx_hash); - dap_cli_server_cmd_set_reply_text(a_str_reply, "SAVE TO TAKE ===>>> Stake transaction %s has done", l_tx_hash_str); + dap_cli_server_cmd_set_reply_text(a_str_reply, "%s\nSAVE TO TAKE ===>>> Stake transaction %s has done", l_sign_str, l_tx_hash_str); DAP_DELETE(l_tx_hash_str); } break; case CMD_APPROVE: { @@ -1888,11 +1891,11 @@ static int s_cli_srv_stake(int a_argc, char **a_argv, char **a_str_reply) return -4; } uint256_t l_fee = {}; - dap_cli_server_cmd_find_option_val(a_argv, l_arg_index, a_argc, "-wallet", &l_wallet_str); + dap_cli_server_cmd_find_option_val(a_argv, l_arg_index, a_argc, "-w", &l_wallet_str); if (!l_wallet_str) { dap_cli_server_cmd_find_option_val(a_argv, l_arg_index, a_argc, "-poa_cert", &l_poa_cert_str); if (!l_poa_cert_str) { - dap_cli_server_cmd_set_reply_text(a_str_reply, "Command 'invalidate' requires parameter -wallet or -poa_cert"); + dap_cli_server_cmd_set_reply_text(a_str_reply, "Command 'invalidate' requires parameter -w or -poa_cert"); return -17; } } else { @@ -1966,10 +1969,13 @@ static int s_cli_srv_stake(int a_argc, char **a_argv, char **a_str_reply) l_final_tx_hash = &l_stake->tx_hash; } if (l_wallet_str) { + const char* l_sign_str = ""; dap_chain_wallet_t *l_wallet = dap_chain_wallet_open(l_wallet_str, dap_chain_wallet_get_path(g_config)); if (!l_wallet) { dap_cli_server_cmd_set_reply_text(a_str_reply, "Specified wallet not found"); return -18; + } else { + l_sign_str = dap_chain_wallet_check_bliss_sign(l_wallet); } dap_chain_datum_tx_t *l_tx = s_stake_tx_invalidate(l_net, l_final_tx_hash, l_fee, dap_chain_wallet_get_key(l_wallet, 0)); if (l_tx_hash_str) { @@ -1978,13 +1984,13 @@ static int s_cli_srv_stake(int a_argc, char **a_argv, char **a_str_reply) dap_chain_wallet_close(l_wallet); char *l_decree_hash_str = NULL; if (l_tx && (l_decree_hash_str = s_stake_tx_put(l_tx, l_net))) { - dap_cli_server_cmd_set_reply_text(a_str_reply, "All m-tokens successfully returned to " - "owner. Returning tx hash %s.", l_decree_hash_str); + dap_cli_server_cmd_set_reply_text(a_str_reply, "%s\nAll m-tokens successfully returned to " + "owner. Returning tx hash %s.", l_sign_str, l_decree_hash_str); DAP_DEL_Z(l_decree_hash_str); DAP_DELETE(l_tx); } else { char *l_final_tx_hash_str = dap_chain_hash_fast_to_str_new(l_final_tx_hash); - dap_cli_server_cmd_set_reply_text(a_str_reply, "Can't invalidate transaction %s, examine log files for details", l_final_tx_hash_str); + dap_cli_server_cmd_set_reply_text(a_str_reply, "%s\nCan't invalidate transaction %s, examine log files for details", l_sign_str, l_final_tx_hash_str); DAP_DELETE(l_final_tx_hash_str); DAP_DELETE(l_tx); return -21; @@ -2004,7 +2010,7 @@ static int s_cli_srv_stake(int a_argc, char **a_argv, char **a_str_reply) if (l_decree && (l_decree_hash_str = s_stake_decree_put(l_decree, l_net))) { dap_cli_server_cmd_set_reply_text(a_str_reply, "Specified delageted key invalidated. " "Created key invalidation decree %s." - "Try to execute this command with -wallet to return m-tokens to owner", l_decree_hash_str); + "Try to execute this command with -w to return m-tokens to owner", l_decree_hash_str); DAP_DELETE(l_decree); DAP_DELETE(l_decree_hash_str); } else { diff --git a/modules/service/vpn/dap_chain_net_srv_vpn_cmd.c b/modules/service/vpn/dap_chain_net_srv_vpn_cmd.c index d345c384d866da0b29f931d9a9b3bfe2a787c1fa..2850a2b406ca6b0521f038bdc13a6f299a2fb096 100644 --- a/modules/service/vpn/dap_chain_net_srv_vpn_cmd.c +++ b/modules/service/vpn/dap_chain_net_srv_vpn_cmd.c @@ -34,20 +34,19 @@ static void add_value_text(dap_string_t *l_str, char *l_addstr, uintmax_t a_valu int com_vpn_statistics(int a_argc, char ** a_argv, char **a_str_reply) { // get statistics for all actual sessions - dap_list_t *l_list = dap_stream_session_get_list_sessions(); + dap_list_t *l_sessions_list = dap_stream_session_get_list_sessions(), *l_item; dap_string_t *l_str = dap_string_new(NULL); int l_conn = 0; // display statistic for all sessions - while(l_list) { - dap_stream_session_t * l_session = (dap_stream_session_t*) l_list->data; - dap_chain_net_srv_stream_session_t * l_srv_str_session = l_session->_inheritor; - if(l_srv_str_session) { - dap_net_stats_t l_stats = l_srv_str_session->stats; //(dap_net_stats_t*) l_list; - dap_string_append_printf(l_str, "VPN connection %d\n", l_conn); + DL_FOREACH(l_sessions_list, l_item) { + dap_stream_session_t *l_session = (dap_stream_session_t*)l_item->data; + dap_chain_net_srv_stream_session_t *l_srv_str_session = l_session->_inheritor; + if (l_srv_str_session) { + dap_net_stats_t *l_stats = (dap_net_stats_t*)&l_srv_str_session->stats; l_conn++; // time start/length uint32_t l_time_len_sec = time(NULL) - l_session->time_created; - char l_buf[1024]; + char l_buf[70] = { '\0' }; if(dap_time_to_str_rfc822(l_buf, sizeof(l_buf), l_session->time_created) > 0) dap_string_append_printf(l_str, " start at %s (length %02u:%02u:%02u)\n", l_buf, l_time_len_sec / 3600, (l_time_len_sec % 3600) / 60, l_time_len_sec % 60); @@ -58,26 +57,25 @@ int com_vpn_statistics(int a_argc, char ** a_argv, char **a_str_reply) dap_string_append_printf(l_str, " client addr........%s\n", l_tun_client_addr_str); else dap_string_append(l_str, " client addr........???\n"); - add_value_text(l_str, " recv..............", l_stats.bytes_recv); - add_value_text(l_str, " recv lost.........", l_stats.bytes_recv_lost); - add_value_text(l_str, " send..............", l_stats.bytes_sent); - add_value_text(l_str, " send lost.........", l_stats.bytes_sent_lost); - dap_string_append_printf(l_str, " packets recv.......%ld\n", l_stats.packets_recv); - dap_string_append_printf(l_str, " packets recv lost..%ld\n", l_stats.packets_recv_lost); - dap_string_append_printf(l_str, " packets send.......%ld\n", l_stats.packets_sent); - dap_string_append_printf(l_str, " packets send lost..%ld\n", l_stats.packets_sent_lost); + add_value_text(l_str, " recv..............", l_stats->bytes_recv); + add_value_text(l_str, " recv lost.........", l_stats->bytes_recv_lost); + add_value_text(l_str, " send..............", l_stats->bytes_sent); + add_value_text(l_str, " send lost.........", l_stats->bytes_sent_lost); + dap_string_append_printf(l_str, " packets recv.......%ld\n", l_stats->packets_recv); + dap_string_append_printf(l_str, " packets recv lost..%ld\n", l_stats->packets_recv_lost); + dap_string_append_printf(l_str, " packets send.......%ld\n", l_stats->packets_sent); + dap_string_append_printf(l_str, " packets send lost..%ld\n", l_stats->packets_sent_lost); // average bitrate - double l_bitrate = (l_stats.bytes_recv - l_stats.bytes_recv_lost + - l_stats.bytes_sent - l_stats.bytes_sent_lost) * 1. / l_time_len_sec; + double l_bitrate = (l_stats->bytes_recv - l_stats->bytes_recv_lost + + l_stats->bytes_sent - l_stats->bytes_sent_lost) * 1. / l_time_len_sec; dap_string_append_printf(l_str, " average bitrate....%.2lf kbps\n", l_bitrate / 1024.); // not add line break after last session - if(l_list->next) + if (l_item->next) dap_string_append_printf(l_str, "\n"); } - // next session - l_list = dap_list_next(l_list); } // unlock sessions list + dap_list_free(l_sessions_list); dap_stream_session_get_list_sessions_unlock(); if(l_conn>0) dap_cli_server_cmd_set_reply_text(a_str_reply, "%s", l_str->str); diff --git a/modules/service/vpn/dap_chain_net_vpn_client_tun.c b/modules/service/vpn/dap_chain_net_vpn_client_tun.c index 0cc4b3797e8988284a5eca28add84319544bc34b..29e3e883b8baef2732ae60e759485f8b1b62a1ad 100644 --- a/modules/service/vpn/dap_chain_net_vpn_client_tun.c +++ b/modules/service/vpn/dap_chain_net_vpn_client_tun.c @@ -338,14 +338,12 @@ int dap_chain_net_vpn_client_tun_init(const char *a_ipv4_server_str) static void m_client_tun_delete(dap_events_socket_t * a_es, void * arg) { - log_it(L_DEBUG, __PRETTY_FUNCTION__); //dap_chain_net_vpn_client_tun_delete(); log_it(L_NOTICE, "Raw sockets listen thread is stopped"); } static void m_client_tun_write(dap_events_socket_t * a_es, void * arg) { -// log_it(L_WARNING, __PRETTY_FUNCTION__); } void m_client_tun_new(dap_events_socket_t * a_es, void * arg) diff --git a/modules/service/xchange/dap_chain_net_srv_xchange.c b/modules/service/xchange/dap_chain_net_srv_xchange.c index 83315833c2f6abe33fb4969dd3704f7c29587c67..c1252c50292aef73ca536b5a4bf864eabb7c163c 100644 --- a/modules/service/xchange/dap_chain_net_srv_xchange.c +++ b/modules/service/xchange/dap_chain_net_srv_xchange.c @@ -81,12 +81,12 @@ int dap_chain_net_srv_xchange_init() dap_chain_ledger_verificator_add(DAP_CHAIN_TX_OUT_COND_SUBTYPE_SRV_XCHANGE, s_xchange_verificator_callback, NULL); dap_cli_server_cmd_add("srv_xchange", s_cli_srv_xchange, "eXchange service commands", - "srv_xchange order create -net <net_name> -token_sell <token_ticker> -token_buy <token_ticker> -wallet <wallet_name>" + "srv_xchange order create -net <net_name> -token_sell <token_ticker> -token_buy <token_ticker> -w <wallet_name>" " -value <value> -rate <value> -fee <value>\n" "\tCreate a new order and tx with specified amount of datoshi to exchange with specified rate (buy / sell)\n" - "srv_xchange order remove -net <net_name> -order <order_hash> -wallet <wallet_name>\n" + "srv_xchange order remove -net <net_name> -order <order_hash> -w <wallet_name>\n" "\tRemove order with specified order hash in specified net name\n" - "srv_xchange order update -net <net_name> -order <order_hash> -wallet <wallet_name> [-token_sell <token_ticker>] " + "srv_xchange order update -net <net_name> -order <order_hash> -w <wallet_name> [-token_sell <token_ticker>] " "[-net_buy <net_name>] [-token_buy <token_ticker>] [-coins <value>] [-rate <value>]\n" "\tUpdate order with specified order hash in specified net name\n" "srv_xchange order history -net <net_name> {-order <order_hash> | -addr <wallet_addr>}" @@ -96,7 +96,7 @@ int dap_chain_net_srv_xchange_init() "srv_xchange orders -net <net_name>\n" "\tGet the exchange orders list within specified net name\n" - "srv_xchange purchase -order <order hash> -net <net_name> -wallet <wallet_name> -value <value> -fee <value>\n" + "srv_xchange purchase -order <order hash> -net <net_name> -w <wallet_name> -value <value> -fee <value>\n" "\tExchange tokens with specified order within specified net name. Specify how many datoshies to sell with rate specified by order\n" "srv_xchange tx_list -net <net_name> [-time_from <yymmdd> -time_to <yymmdd>]" @@ -637,7 +637,6 @@ static dap_chain_datum_tx_t *s_xchange_tx_create_exchange(dap_chain_net_srv_xcha l_seller_addr, NULL, 0); if (!l_tx_out) { dap_chain_datum_tx_delete(l_tx); - DAP_DELETE(l_seller_addr); log_it(L_WARNING, "Can't add selling coins back conditioned output (cond cashback)"); return NULL; } @@ -918,6 +917,8 @@ char *s_xchange_order_create(dap_chain_net_srv_xchange_price_t *a_price, dap_cha */ dap_chain_net_srv_xchange_price_t *s_xchange_price_from_order(dap_chain_net_t *a_net, dap_chain_net_srv_order_t *a_order, bool a_ret_is_invalid) { + if (!a_net || !a_order) + return NULL; dap_chain_net_srv_xchange_price_t *l_price = DAP_NEW_Z(dap_chain_net_srv_xchange_price_t); if (!l_price) { log_it(L_CRITICAL, "Memory allocation error"); @@ -1046,15 +1047,18 @@ static int s_cli_srv_xchange_order(int a_argc, char **a_argv, int a_arg_index, c dap_cli_server_cmd_set_reply_text(a_str_reply, "Format -fee <unsigned integer 256>"); return -21; } - dap_cli_server_cmd_find_option_val(a_argv, l_arg_index, a_argc, "-wallet", &l_wallet_str); + dap_cli_server_cmd_find_option_val(a_argv, l_arg_index, a_argc, "-w", &l_wallet_str); if (!l_wallet_str) { - dap_cli_server_cmd_set_reply_text(a_str_reply, "Command 'price create' requires parameter -wallet"); + dap_cli_server_cmd_set_reply_text(a_str_reply, "Command 'price create' requires parameter -w"); return -10; } dap_chain_wallet_t *l_wallet = dap_chain_wallet_open(l_wallet_str, dap_chain_wallet_get_path(g_config)); + const char* l_sign_str = ""; if (!l_wallet) { dap_cli_server_cmd_set_reply_text(a_str_reply, "Specified wallet not found"); return -11; + } else { + l_sign_str = dap_chain_wallet_check_bliss_sign(l_wallet); } uint256_t l_value = dap_chain_wallet_get_balance(l_wallet, l_net->pub.id, l_token_sell_str); uint256_t l_value_sell = l_datoshi_sell; @@ -1067,13 +1071,13 @@ static int s_cli_srv_xchange_order(int a_argc, char **a_argv, int a_arg_index, c } else { // sell non-native ticker uint256_t l_fee_value = dap_chain_wallet_get_balance(l_wallet, l_net->pub.id, l_net->pub.native_ticker); if (compare256(l_fee_value, l_fee) == -1) { - dap_cli_server_cmd_set_reply_text(a_str_reply, "Not enough cash for fee in specified wallet"); + dap_cli_server_cmd_set_reply_text(a_str_reply, "%s\nNot enough cash for fee in specified wallet", l_sign_str); dap_chain_wallet_close(l_wallet); return -23; } } if (compare256(l_value, l_value_sell) == -1) { - dap_cli_server_cmd_set_reply_text(a_str_reply, "Not enough cash in specified wallet"); + dap_cli_server_cmd_set_reply_text(a_str_reply, "%s\nNot enough cash in specified wallet", l_sign_str); dap_chain_wallet_close(l_wallet); return -12; } @@ -1095,7 +1099,7 @@ static int s_cli_srv_xchange_order(int a_argc, char **a_argv, int a_arg_index, c // Create conditional transaction dap_chain_datum_tx_t *l_tx = s_xchange_tx_create_request(l_price, l_wallet); if (!l_tx) { - dap_cli_server_cmd_set_reply_text(a_str_reply, "Can't compose the conditional transaction"); + dap_cli_server_cmd_set_reply_text(a_str_reply, "%s\nCan't compose the conditional transaction", l_sign_str); DAP_DELETE(l_price->wallet_str); DAP_DELETE(l_price); dap_chain_wallet_close(l_wallet); @@ -1108,17 +1112,17 @@ static int s_cli_srv_xchange_order(int a_argc, char **a_argv, int a_arg_index, c if (l_order_hash_str) { dap_chain_hash_fast_from_str(l_order_hash_str, &l_price->order_hash); if(!s_xchange_tx_put(l_tx, l_net)) { - dap_cli_server_cmd_set_reply_text(a_str_reply, "Can't put transaction to mempool"); + dap_cli_server_cmd_set_reply_text(a_str_reply, "%s\nCan't put transaction to mempool", l_sign_str); dap_chain_net_srv_order_delete_by_hash_str_sync(l_net, l_order_hash_str); DAP_DELETE(l_order_hash_str); DAP_DELETE(l_price->wallet_str); DAP_DELETE(l_price); return -15; } - dap_cli_server_cmd_set_reply_text(a_str_reply, "Successfully created order %s", l_order_hash_str); + dap_cli_server_cmd_set_reply_text(a_str_reply, "%s\nSuccessfully created order %s", l_sign_str, l_order_hash_str); DAP_DELETE(l_order_hash_str); } else { - dap_cli_server_cmd_set_reply_text(a_str_reply, "Can't compose the order"); + dap_cli_server_cmd_set_reply_text(a_str_reply, "%s\nCan't compose the order", l_sign_str); DAP_DELETE(l_price->wallet_str); DAP_DELETE(l_price); return -18; @@ -1222,16 +1226,19 @@ static int s_cli_srv_xchange_order(int a_argc, char **a_argv, int a_arg_index, c dap_cli_server_cmd_set_reply_text(a_str_reply, "Network %s not found", l_net_str); return -3; } - dap_cli_server_cmd_find_option_val(a_argv, l_arg_index, a_argc, "-wallet", &l_wallet_str); + dap_cli_server_cmd_find_option_val(a_argv, l_arg_index, a_argc, "-w", &l_wallet_str); if (!l_wallet_str) { - dap_cli_server_cmd_set_reply_text(a_str_reply, "Command 'price %s' requires parameter -wallet", + dap_cli_server_cmd_set_reply_text(a_str_reply, "Command 'price %s' requires parameter -w", l_cmd_num == CMD_REMOVE ? "remove" : "update"); return -10; } dap_chain_wallet_t *l_wallet = dap_chain_wallet_open(l_wallet_str, dap_chain_wallet_get_path(g_config)); + const char* l_sign_str = ""; if (!l_wallet) { dap_cli_server_cmd_set_reply_text(a_str_reply, "Specified wallet not found"); return -11; + } else { + l_sign_str = dap_chain_wallet_check_bliss_sign(l_wallet); } dap_cli_server_cmd_find_option_val(a_argv, l_arg_index, a_argc, "-order", &l_order_hash_str); if (!l_order_hash_str) { @@ -1241,17 +1248,17 @@ static int s_cli_srv_xchange_order(int a_argc, char **a_argv, int a_arg_index, c } dap_chain_net_srv_order_t *l_order = dap_chain_net_srv_order_find_by_hash_str(l_net, l_order_hash_str); if (!l_order) { - dap_cli_server_cmd_set_reply_text(a_str_reply, "Specified order not found"); + dap_cli_server_cmd_set_reply_text(a_str_reply, "%s\nSpecified order not found", l_sign_str); return -13; } dap_chain_net_srv_xchange_price_t *l_price = s_xchange_price_from_order(l_net, l_order, false); - if (!l_order) { - dap_cli_server_cmd_set_reply_text(a_str_reply, "Can't create price object from order"); + if (!l_price) { + dap_cli_server_cmd_set_reply_text(a_str_reply, "%s\nCan't create price object from order", l_sign_str); return -13; } if (l_cmd_num == CMD_REMOVE) { - dap_string_t *l_str_reply = dap_string_new(""); + dap_string_t *l_str_reply = dap_string_new(l_sign_str); bool l_ret = s_xchange_tx_invalidate(l_price, l_wallet); dap_chain_wallet_close(l_wallet); if (!l_ret) { @@ -1294,12 +1301,14 @@ static int s_cli_srv_xchange_order(int a_argc, char **a_argv, int a_arg_index, c return -9; } } - dap_cli_server_cmd_find_option_val(a_argv, l_arg_index, a_argc, "-wallet", &l_new_wallet_str); + dap_cli_server_cmd_find_option_val(a_argv, l_arg_index, a_argc, "-w", &l_new_wallet_str); l_wallet_str = l_new_wallet_str ? l_new_wallet_str : l_price->wallet_str; l_wallet = dap_chain_wallet_open(l_wallet_str, dap_chain_wallet_get_path(g_config)); if (!l_wallet) { dap_cli_server_cmd_set_reply_text(a_str_reply, "Specified wallet not found"); return -11; + } else { + l_sign_str = dap_chain_wallet_check_bliss_sign(l_wallet); } if (!l_val_sell_str && !l_val_rate_str && !l_wallet_str) { dap_cli_server_cmd_set_reply_text(a_str_reply, "At least one of updating parameters is mandatory"); @@ -1307,7 +1316,7 @@ static int s_cli_srv_xchange_order(int a_argc, char **a_argv, int a_arg_index, c } uint256_t l_value = dap_chain_wallet_get_balance(l_wallet, l_net->pub.id, l_token_sell_str); if (!IS_ZERO_256(l_datoshi_sell) && compare256(l_value, l_datoshi_sell) == -1) { - dap_cli_server_cmd_set_reply_text(a_str_reply, "Not enough cash in specified wallet"); + dap_cli_server_cmd_set_reply_text(a_str_reply, "%s\nNot enough cash in specified wallet", l_sign_str); dap_chain_wallet_close(l_wallet); return -12; } @@ -1326,14 +1335,14 @@ static int s_cli_srv_xchange_order(int a_argc, char **a_argv, int a_arg_index, c l_price->wallet_str = dap_strdup(l_new_wallet_str); } if (!l_tx) { - dap_cli_server_cmd_set_reply_text(a_str_reply, "Can't compose the conditional transaction"); + dap_cli_server_cmd_set_reply_text(a_str_reply, "%s\nCan't compose the conditional transaction", l_sign_str); return -14; } bool l_ret = s_xchange_tx_invalidate(l_price, l_wallet); // may be changed to old price later dap_chain_wallet_close(l_wallet); if (!l_ret) { char *l_tx_hash_str = dap_chain_hash_fast_to_str_new(&l_price->tx_hash); - dap_cli_server_cmd_set_reply_text(a_str_reply, "Can't invalidate transaction %s\n", l_tx_hash_str); + dap_cli_server_cmd_set_reply_text(a_str_reply, "%s\nCan't invalidate transaction %s\n", l_sign_str, l_tx_hash_str); DAP_DELETE(l_tx_hash_str); return -17; } @@ -1344,15 +1353,15 @@ static int s_cli_srv_xchange_order(int a_argc, char **a_argv, int a_arg_index, c if (l_order_hash_str) { dap_chain_hash_fast_from_str(l_order_hash_str, &l_price->order_hash); if(!s_xchange_tx_put(l_tx, l_net)) { - dap_cli_server_cmd_set_reply_text(a_str_reply, "Can't put transaction to mempool"); + dap_cli_server_cmd_set_reply_text(a_str_reply, "%s\nCan't put transaction to mempool", l_sign_str); dap_chain_net_srv_order_delete_by_hash_str_sync(l_net, l_order_hash_str); DAP_DELETE(l_order_hash_str); return -15; } - dap_cli_server_cmd_set_reply_text(a_str_reply, "Successfully created order %s", l_order_hash_str); + dap_cli_server_cmd_set_reply_text(a_str_reply, "%s\nSuccessfully created order %s", l_sign_str, l_order_hash_str); DAP_DELETE(l_order_hash_str); } else { - dap_cli_server_cmd_set_reply_text(a_str_reply, "Can't compose the order"); + dap_cli_server_cmd_set_reply_text(a_str_reply, "%s\nCan't compose the order", l_sign_str); DAP_DELETE(l_price->wallet_str); DAP_DELETE(l_price); return -18; @@ -1575,7 +1584,7 @@ dap_chain_tx_out_cond_t *l_out_cond_item; if ( !(l_reply_str = dap_string_new("")) ) /* Prepare output string discriptor*/ - return log_it(L_ERROR, "Cannot allocate a memory, errno=%d", errno), -ENOMEM; + return log_it(L_CRITICAL, "Memory allocation error in %s, line %d", __PRETTY_FUNCTION__, __LINE__), -ENOMEM; memset(&l_tx_first_hash, 0, sizeof(dap_chain_hash_fast_t)); /* Initial hash == zero */ @@ -1773,9 +1782,9 @@ static int s_cli_srv_xchange(int a_argc, char **a_argv, char **a_str_reply) dap_cli_server_cmd_set_reply_text(a_str_reply, "Network %s not found", l_net_str); return -3; } - dap_cli_server_cmd_find_option_val(a_argv, l_arg_index, a_argc, "-wallet", &l_wallet_str); + dap_cli_server_cmd_find_option_val(a_argv, l_arg_index, a_argc, "-w", &l_wallet_str); if (!l_wallet_str) { - dap_cli_server_cmd_set_reply_text(a_str_reply, "Command 'purchase' requires parameter -wallet"); + dap_cli_server_cmd_set_reply_text(a_str_reply, "Command 'purchase' requires parameter -w"); return -10; } dap_chain_wallet_t *l_wallet = dap_chain_wallet_open(l_wallet_str, dap_chain_wallet_get_path(g_config)); @@ -2108,7 +2117,7 @@ static int s_cli_srv_xchange(int a_argc, char **a_argv, char **a_str_reply) } l_cur = dap_list_next(l_cur); } - dap_list_free_full(l_tx_cond_list, NULL); + dap_list_free(l_tx_cond_list); uint256_t l_rate_average = {0}; if (!IS_ZERO_256(l_total_rates_count)) DIV_256(l_total_rates,l_total_rates_count,&l_rate_average); diff --git a/modules/type/blocks/dap_chain_cs_blocks.c b/modules/type/blocks/dap_chain_cs_blocks.c index e5227ffdbc8c80b71974660a03fdca5b5a8a09cd..9f4f7a121e01c200ae7030596fe6e69edd3bbc46 100644 --- a/modules/type/blocks/dap_chain_cs_blocks.c +++ b/modules/type/blocks/dap_chain_cs_blocks.c @@ -898,10 +898,7 @@ static void s_callback_cs_blocks_purge(dap_chain_t *a_chain) dap_chain_block_chunks_delete(PVT(l_blocks)->chunks); PVT(l_blocks)->block_cache_last = NULL; PVT(l_blocks)->block_cache_first = NULL; - dap_chain_cell_t *l_cell = NULL, *l_cell_tmp = NULL; - HASH_ITER(hh, a_chain->cells, l_cell, l_cell_tmp) { - dap_chain_cell_delete(l_cell); - } + dap_chain_cell_delete_all(a_chain); PVT(l_blocks)->chunks = dap_chain_block_chunks_create(l_blocks); } diff --git a/modules/type/dag/dap_chain_cs_dag.c b/modules/type/dag/dap_chain_cs_dag.c index 222e9295555555883b61e0ddf652506e331c0a05..69afdc5ea76967739e2092d87c561078f95e3f2c 100644 --- a/modules/type/dag/dap_chain_cs_dag.c +++ b/modules/type/dag/dap_chain_cs_dag.c @@ -402,10 +402,7 @@ static void s_dap_chain_cs_dag_purge(dap_chain_t *a_chain) DAP_DELETE(l_event_current); } pthread_mutex_unlock(&l_dag_pvt->events_mutex); - dap_chain_cell_t *l_cell_cur, *l_cell_tmp; - HASH_ITER(hh, a_chain->cells, l_cell_cur, l_cell_tmp) { - dap_chain_cell_close(l_cell_cur); - } + dap_chain_cell_delete_all(a_chain); } /** @@ -1563,10 +1560,9 @@ static int s_cli_dag(int argc, char ** argv, char **a_str_reply) if(l_list_to_del) { if (dap_chain_cell_file_update(l_chain->cells) > 0) { // delete events from db - dap_list_t *l_list_tmp = l_list_to_del; - while(l_list_tmp) { - dap_global_db_del_sync(l_dag->gdb_group_events_round_new, (char*)l_list_tmp->data); - l_list_tmp = dap_list_next(l_list_tmp); + dap_list_t *l_el; + DL_FOREACH(l_list_to_del, l_el) { + dap_global_db_del_sync(l_dag->gdb_group_events_round_new, (char*)l_el->data); } } dap_chain_cell_close(l_chain->cells); diff --git a/modules/wallet/dap_chain_wallet.c b/modules/wallet/dap_chain_wallet.c index c9d6313e017a2c73256953f845bbe1c1bb7e095b..3c3cff1da755408fe07afcd003d180ba0493d024 100644 --- a/modules/wallet/dap_chain_wallet.c +++ b/modules/wallet/dap_chain_wallet.c @@ -131,8 +131,7 @@ char *c_wallets_path; } *l_prec = l_rec; /* Fill it by data */ HASH_ADD_STR(s_wallet_n_pass, name, l_prec); /* Add into the hash-table */ - } - else { + } else { if ( !l_prec->pass_len ) /* Password field is empty ? */ memcpy(l_prec->pass, a_pass, l_prec->pass_len = a_pass_len);/* Update password with new one */ @@ -276,11 +275,11 @@ dap_chain_wallet_n_pass_t *l_prec; if ( l_prec ) { if ( !l_prec->pass_len ) /* Password is zero - has been reset probably */ - log_it(L_WARNING, "The Wallet %.*s is not active", (int) a_name_len, a_name); + l_rc = -EBUSY, log_it(L_WARNING, "The Wallet %.*s is not active", (int) a_name_len, a_name); else if ( (l_prec->pass_len != a_pass_len) /* Check that passwords is equivalent */ || memcmp(l_prec->pass, a_pass, l_prec->pass_len) ) - l_rc = -EINVAL, log_it(L_ERROR, "Wallet's password does not match"); + l_rc = -EAGAIN, log_it(L_ERROR, "Wallet's password does not match"); else l_rc = 0, memset(l_prec->pass, l_prec->pass_len = 0, sizeof(l_prec->pass)); } @@ -502,9 +501,13 @@ dap_chain_addr_t* dap_chain_wallet_get_addr(dap_chain_wallet_t * a_wallet, dap_c * @param a_net_id * @return */ -dap_chain_addr_t * dap_cert_to_addr(dap_cert_t * a_cert, dap_chain_net_id_t a_net_id) +dap_chain_addr_t *dap_cert_to_addr(dap_cert_t * a_cert, dap_chain_net_id_t a_net_id) { - dap_chain_addr_t * l_addr = DAP_NEW_Z(dap_chain_addr_t); + dap_chain_addr_t *l_addr = DAP_NEW_Z(dap_chain_addr_t); + if(!l_addr) { + log_it(L_CRITICAL, "Memory allocation error"); + return NULL; + } dap_chain_addr_fill_from_key(l_addr, a_cert->enc_key, a_net_id); return l_addr; } @@ -1001,3 +1004,16 @@ uint256_t dap_chain_wallet_get_balance ( return (l_net) ? dap_chain_ledger_calc_balance(l_net->pub.ledger, l_addr, a_token_ticker) : uint256_0; } + +/** + * @brief cheack wallet to the Bliss sign + * @param a_wallet + * @return if sign Bliss - caution message, else "" + */ +const char* dap_chain_wallet_check_bliss_sign(dap_chain_wallet_t *a_wallet) { + dap_chain_wallet_internal_t *l_wallet_internal = DAP_CHAIN_WALLET_INTERNAL(a_wallet); + if (l_wallet_internal && SIG_TYPE_BLISS == dap_sign_type_from_key_type(l_wallet_internal->certs[0]->enc_key->type).type) { + return "The Bliss signature is deprecated. We recommend you to create a new wallet with another available signature and transfer funds there."; + } + return ""; +} diff --git a/modules/wallet/include/dap_chain_wallet.h b/modules/wallet/include/dap_chain_wallet.h index ba0218db51e9dfe8ff6bfd8bcc0b72af9f168730..327d41970f18b4b40a3420b6728e47d6de34bd5f 100644 --- a/modules/wallet/include/dap_chain_wallet.h +++ b/modules/wallet/include/dap_chain_wallet.h @@ -80,3 +80,5 @@ int dap_chain_wallet_save_file( dap_chain_wallet_t * a_wallet); int dap_chain_wallet_activate (const char *a_name, ssize_t a_name_len, const char *a_pass, ssize_t a_pass_len, unsigned a_ttl); int dap_chain_wallet_deactivate (const char *a_name, ssize_t a_name_len, const char *a_pass, ssize_t a_pass_len); + +const char* dap_chain_wallet_check_bliss_sign(dap_chain_wallet_t *a_wallet); \ No newline at end of file