From d4fd421598e1186ee25de69a22a9d7d1e5aa567b Mon Sep 17 00:00:00 2001 From: Constantin P <papizh.konstantin@demlabs.net> Date: Tue, 12 Sep 2023 08:11:29 +0000 Subject: [PATCH] Hotfix dap list refactoring --- dap-sdk | 2 +- modules/chain/dap_chain.c | 88 ++++++------ modules/chain/dap_chain_cell.c | 12 ++ modules/chain/dap_chain_ledger.c | 132 ++++++++---------- modules/chain/include/dap_chain.h | 2 +- modules/common/dap_chain_datum_tx.c | 51 ++++--- modules/common/dap_chain_datum_tx_items.c | 23 ++- .../consensus/dag-poa/dap_chain_cs_dag_poa.c | 9 +- .../consensus/esbocs/dap_chain_cs_esbocs.c | 50 ++++--- modules/consensus/none/dap_chain_cs_none.c | 7 +- modules/mempool/dap_chain_mempool.c | 37 +++-- modules/net/dap_chain_net.c | 24 ++-- modules/net/dap_chain_net_balancer.c | 13 +- modules/net/dap_chain_net_tx.c | 22 ++- modules/net/dap_chain_node_cli_cmd.c | 27 ++-- modules/net/dap_chain_node_cli_cmd_tx.c | 2 +- modules/net/srv/dap_chain_net_srv.c | 33 ++--- .../stake/dap_chain_net_srv_stake_lock.c | 3 +- .../dap_chain_net_srv_stake_pos_delegate.c | 16 +-- .../service/vpn/dap_chain_net_srv_vpn_cmd.c | 40 +++--- .../vpn/dap_chain_net_vpn_client_tun.c | 2 - .../xchange/dap_chain_net_srv_xchange.c | 2 +- modules/type/dag/dap_chain_cs_dag.c | 7 +- 23 files changed, 295 insertions(+), 309 deletions(-) diff --git a/dap-sdk b/dap-sdk index 3a37d90ee2..5c5f3376d3 160000 --- a/dap-sdk +++ b/dap-sdk @@ -1 +1 @@ -Subproject commit 3a37d90ee2deec59fe1831a6a967a2c23460aed0 +Subproject commit 5c5f3376d3cb3d53c582d82e472cdffce256aac8 diff --git a/modules/chain/dap_chain.c b/modules/chain/dap_chain.c index de9f317ff8..4119cdd531 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); } /** @@ -715,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) { diff --git a/modules/chain/dap_chain_cell.c b/modules/chain/dap_chain_cell.c index d3c4fa3085..290f4d9a0e 100644 --- a/modules/chain/dap_chain_cell.c +++ b/modules/chain/dap_chain_cell.c @@ -259,6 +259,18 @@ int dap_chain_cell_load(dap_chain_t *a_chain, dap_chain_cell_t *a_cell) static int s_file_write_header(dap_chain_cell_t *a_cell) { + 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, diff --git a/modules/chain/dap_chain_ledger.c b/modules/chain/dap_chain_ledger.c index 4f7f934961..a5ac3b20fe 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; } @@ -5614,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; } @@ -5696,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; } @@ -5719,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 35b6cd4c7e..490c9fb689 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/common/dap_chain_datum_tx.c b/modules/common/dap_chain_datum_tx.c index e6d5c57851..01c40f6e37 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 5ec7c2a87b..22ef1f623d 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 c3e41ba0eb..758dd3749b 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 c05c113b49..e9db643116 100644 --- a/modules/consensus/esbocs/dap_chain_cs_esbocs.c +++ b/modules/consensus/esbocs/dap_chain_cs_esbocs.c @@ -662,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); } @@ -947,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; } diff --git a/modules/consensus/none/dap_chain_cs_none.c b/modules/consensus/none/dap_chain_cs_none.c index c2e6069401..7680827628 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 574e66c301..ec7030fe51 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); @@ -785,7 +782,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 { diff --git a/modules/net/dap_chain_net.c b/modules/net/dap_chain_net.c index 0f2d178f16..2b40a6d911 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) @@ -3309,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; @@ -3318,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; } diff --git a/modules/net/dap_chain_net_balancer.c b/modules/net/dap_chain_net_balancer.c index bb943ff330..9b5e456b10 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 45c166b227..1b83023b26 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_cmd.c b/modules/net/dap_chain_node_cli_cmd.c index bb3e14210a..1f8d035bf0 100644 --- a/modules/net/dap_chain_node_cli_cmd.c +++ b/modules/net/dap_chain_node_cli_cmd.c @@ -2694,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; } /** diff --git a/modules/net/dap_chain_node_cli_cmd_tx.c b/modules/net/dap_chain_node_cli_cmd_tx.c index fcd4ba32d1..c876a3435d 100644 --- a/modules/net/dap_chain_node_cli_cmd_tx.c +++ b/modules/net/dap_chain_node_cli_cmd_tx.c @@ -867,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); } diff --git a/modules/net/srv/dap_chain_net_srv.c b/modules/net/srv/dap_chain_net_srv.c index 6e01705d54..a5837ceb21 100644 --- a/modules/net/srv/dap_chain_net_srv.c +++ b/modules/net/srv/dap_chain_net_srv.c @@ -780,31 +780,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/service/stake/dap_chain_net_srv_stake_lock.c b/modules/service/stake/dap_chain_net_srv_stake_lock.c index de5ba884b5..ea265e83de 100644 --- a/modules/service/stake/dap_chain_net_srv_stake_lock.c +++ b/modules/service/stake/dap_chain_net_srv_stake_lock.c @@ -948,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 1da379d516..7a06dbb7c8 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 @@ -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, 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 d345c384d8..2850a2b406 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 0cc4b3797e..29e3e883b8 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 6f663671c9..c1252c5029 100644 --- a/modules/service/xchange/dap_chain_net_srv_xchange.c +++ b/modules/service/xchange/dap_chain_net_srv_xchange.c @@ -2117,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/dag/dap_chain_cs_dag.c b/modules/type/dag/dap_chain_cs_dag.c index 9ba8542152..69afdc5ea7 100644 --- a/modules/type/dag/dap_chain_cs_dag.c +++ b/modules/type/dag/dap_chain_cs_dag.c @@ -1560,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); -- GitLab