diff --git a/dap-sdk b/dap-sdk index 2e6ae97b8c453630b52230c87526a1b7da66f3cc..30bfb069b0bcb7ce57befaab68051d0d4feb414e 160000 --- a/dap-sdk +++ b/dap-sdk @@ -1 +1 @@ -Subproject commit 2e6ae97b8c453630b52230c87526a1b7da66f3cc +Subproject commit 30bfb069b0bcb7ce57befaab68051d0d4feb414e diff --git a/modules/chain/dap_chain_ch.c b/modules/chain/dap_chain_ch.c index 661cffe6df1deb570d75247854259a8feb223170..6c71b25332bd29758646d05ce2a4b1fb115d39db 100644 --- a/modules/chain/dap_chain_ch.c +++ b/modules/chain/dap_chain_ch.c @@ -877,7 +877,7 @@ static bool s_stream_ch_packet_in(dap_stream_ch_t* a_ch, void* a_arg) if (l_missed_hash) { l_chain->callback_atom_iter_get(l_iter, DAP_CHAIN_ITER_OP_LAST, NULL); dap_chain_ch_miss_info_t l_miss_info = { .missed_hash = l_request->hash_from, - .last_hash = *l_iter->cur_hash, + .last_hash = l_iter->cur_hash ? *l_iter->cur_hash : (dap_hash_fast_t){ }, .last_num = l_iter->cur_num }; dap_chain_ch_pkt_write_unsafe(a_ch, DAP_CHAIN_CH_PKT_TYPE_CHAIN_MISS, l_chain_pkt->hdr.net_id, l_chain_pkt->hdr.chain_id, diff --git a/modules/consensus/esbocs/dap_chain_cs_esbocs.c b/modules/consensus/esbocs/dap_chain_cs_esbocs.c index 9ec843ca7105ac76258eb40e2b6e84c60f4aea18..8b071846bae4d75233fe77967968636e8dc59837 100644 --- a/modules/consensus/esbocs/dap_chain_cs_esbocs.c +++ b/modules/consensus/esbocs/dap_chain_cs_esbocs.c @@ -427,7 +427,6 @@ static void s_new_atom_notifier(void *a_arg, dap_chain_t *a_chain, dap_chain_cel { dap_chain_esbocs_session_t *l_session = a_arg; assert(l_session->chain == a_chain); - //pthread_mutex_lock(&l_session->mutex); dap_chain_hash_fast_t l_last_block_hash; dap_chain_get_atom_last_hash(l_session->chain, a_id, &l_last_block_hash); if (!dap_hash_fast_compare(&l_last_block_hash, &l_session->cur_round.last_block_hash) && @@ -435,7 +434,6 @@ static void s_new_atom_notifier(void *a_arg, dap_chain_t *a_chain, dap_chain_cel l_session->new_round_enqueued = true; s_session_round_new(l_session); } - //pthread_mutex_unlock(&l_session->mutex); if (!PVT(l_session->esbocs)->collecting_addr) return; dap_chain_esbocs_block_collect_t l_block_collect_params = (dap_chain_esbocs_block_collect_t){ @@ -593,7 +591,6 @@ static int s_callback_created(dap_chain_t *a_chain, dap_config_t *a_chain_net_cf log_it(L_ERROR, "This validator is not allowed to work in emergency mode. Use special decree to supply it"); return -5; } - //pthread_mutex_init(&l_session->mutex, NULL); dap_chain_add_callback_notify(a_chain, s_new_atom_notifier, l_session); s_session_round_new(l_session); @@ -789,7 +786,6 @@ static void s_callback_delete(dap_chain_cs_blocks_t *a_blocks) log_it(L_INFO, "No session found"); return; } - //pthread_mutex_lock(&l_session->mutex); DL_DELETE(s_session_items, l_session); s_session_round_clear(l_session); dap_chain_esbocs_sync_item_t *l_sync_item, *l_sync_tmp; @@ -803,8 +799,6 @@ static void s_callback_delete(dap_chain_cs_blocks_t *a_blocks) HASH_DEL(l_session->penalty, l_pen_item); DAP_DELETE(l_pen_item); } - //pthread_mutex_unlock(&l_session->mutex); - //pthread_mutex_destroy(&l_session->mutex); DAP_DEL_MULTY(l_session, a_blocks->_inheritor); // a_blocks->_inheritor - l_esbocs } @@ -1032,10 +1026,8 @@ static void s_session_send_startsync(dap_chain_esbocs_session_t *a_session) static bool s_session_send_startsync_on_timer(void *a_arg) { dap_chain_esbocs_session_t *l_session = a_arg; - //pthread_mutex_lock(&l_session->mutex); s_session_send_startsync(l_session); l_session->sync_timer = NULL; - //pthread_mutex_unlock(&l_session->mutex); return false; } @@ -1476,8 +1468,6 @@ static void s_session_proc_state(void *a_arg) dap_chain_esbocs_session_t *l_session = a_arg; if (!l_session->cs_timer) return; // Timer is inactive - //if (pthread_mutex_trylock(&l_session->mutex) != 0) - // return; // Session is busy bool l_cs_debug = PVT(l_session->esbocs)->debug; dap_time_t l_time = dap_time_now(); switch (l_session->state) { @@ -1582,8 +1572,6 @@ static void s_session_proc_state(void *a_arg) default: break; } - - //pthread_mutex_unlock(&l_session->mutex); } static void s_message_chain_add(dap_chain_esbocs_session_t *a_session, @@ -2219,10 +2207,8 @@ static void s_session_packet_in(dap_chain_esbocs_session_t *a_session, dap_chain dap_hash_fast(l_message, a_data_size, &l_data_hash); if (a_sender_node_addr) { //Process network messages only - //pthread_mutex_lock(&l_session->mutex); if (l_message->hdr.chain_id.uint64 != l_session->chain->id.uint64) { debug_if(l_cs_debug, L_MSG, "Invalid chain ID %"DAP_UINT64_FORMAT_U, l_message->hdr.chain_id.uint64); - //goto session_unlock; return; } // check hash message dup @@ -2233,7 +2219,6 @@ static void s_session_packet_in(dap_chain_esbocs_session_t *a_session, dap_chain " Message rejected: message hash is exists in chain (duplicate)", l_session->chain->net_name, l_session->chain->name, l_session->cur_round.id, l_message->hdr.attempt_num); - //goto session_unlock; return; } l_message->hdr.sign_size = 0; // restore header on signing time @@ -2242,7 +2227,6 @@ static void s_session_packet_in(dap_chain_esbocs_session_t *a_session, dap_chain " Message rejected from addr:"NODE_ADDR_FP_STR" not passed verification", l_session->chain->net_name, l_session->chain->name, l_session->cur_round.id, l_session->cur_round.attempt_num, NODE_ADDR_FP_ARGS(a_sender_node_addr)); - //goto session_unlock; return; } l_message->hdr.sign_size = l_sign_size; // restore original header @@ -2255,7 +2239,6 @@ static void s_session_packet_in(dap_chain_esbocs_session_t *a_session, dap_chain l_session->chain->net_name, l_session->chain->name, l_session->cur_round.id); s_session_sync_queue_add(l_session, l_message, a_data_size); - //goto session_unlock; return; } } else if (l_message->hdr.round_id != l_session->cur_round.id) { @@ -2291,7 +2274,6 @@ static void s_session_packet_in(dap_chain_esbocs_session_t *a_session, dap_chain l_session->chain->net_name, l_session->chain->name, l_session->cur_round.id, l_message->hdr.attempt_num, s_voting_msg_type_to_str(l_message->hdr.type)); - //goto session_unlock; return; } } @@ -2328,7 +2310,6 @@ static void s_session_packet_in(dap_chain_esbocs_session_t *a_session, dap_chain " Message rejected: validator key:%s not in the current validators list or not synced yet", l_session->chain->net_name, l_session->chain->name, l_session->cur_round.id, l_message->hdr.attempt_num, l_validator_addr_str); - //goto session_unlock; return; } @@ -2468,7 +2449,6 @@ static void s_session_packet_in(dap_chain_esbocs_session_t *a_session, dap_chain l_store = DAP_NEW_Z(dap_chain_esbocs_store_t); if (!l_store) { log_it(L_CRITICAL, "%s", c_error_memory_alloc); - //goto session_unlock; return; } l_store->candidate_size = l_candidate_size; @@ -2695,9 +2675,6 @@ static void s_session_packet_in(dap_chain_esbocs_session_t *a_session, dap_chain default: break; } -//session_unlock: - //if (a_sender_node_addr) //Process network message - // pthread_mutex_unlock(&l_session->mutex); } static void s_message_send(dap_chain_esbocs_session_t *a_session, uint8_t a_message_type, dap_hash_fast_t *a_block_hash, @@ -2743,7 +2720,7 @@ static void s_message_send(dap_chain_esbocs_session_t *a_session, uint8_t a_mess a_message_type, l_message, l_message_size + l_sign_size); continue; } - /*struct esbocs_msg_args *l_args = DAP_NEW_SIZE(struct esbocs_msg_args, + struct esbocs_msg_args *l_args = DAP_NEW_SIZE(struct esbocs_msg_args, sizeof(struct esbocs_msg_args) + l_message_size + l_sign_size); if (!l_args) { log_it(L_CRITICAL, "%s", c_error_memory_alloc); @@ -2754,9 +2731,8 @@ static void s_message_send(dap_chain_esbocs_session_t *a_session, uint8_t a_mess l_args->session = a_session; l_args->message_size = l_message_size + l_sign_size; memcpy(l_args->message, l_message, l_message_size + l_sign_size); - dap_proc_thread_callback_add(a_session->proc_thread, s_process_incoming_message, l_args);*/ - s_session_packet_in(a_session, &a_session->my_addr, (byte_t*)l_message, l_message_size + l_sign_size); - } + dap_proc_thread_callback_add(a_session->proc_thread, s_process_incoming_message, l_args); +\ } } DAP_DELETE(l_message); } diff --git a/modules/net/dap_chain_net_node_list.c b/modules/net/dap_chain_net_node_list.c index 52119f83ee1dcbfbf77d5bbeab3e3543c96f8ac2..9fac56330a9c7038c546068d0c728d0361e11d32 100644 --- a/modules/net/dap_chain_net_node_list.c +++ b/modules/net/dap_chain_net_node_list.c @@ -124,7 +124,7 @@ void dap_chain_net_node_check_http_issue_link(dap_http_simple_t *a_http_simple, uint8_t l_response = ERR_UNKNOWN; switch (l_issue_method) { case 'a': { - uint8_t l_host_size = dap_min(INET6_ADDRSTRLEN, (int)dap_strlen(a_http_simple->es_hostaddr) + 1); + uint8_t l_host_size = (uint8_t)dap_strlen(a_http_simple->es_hostaddr) + 1; l_node_info = DAP_NEW_STACK_SIZE(dap_chain_node_info_t, sizeof(dap_chain_node_info_t) + l_host_size); *l_node_info = (dap_chain_node_info_t) { .address.uint64 = addr, @@ -133,8 +133,7 @@ void dap_chain_net_node_check_http_issue_link(dap_http_simple_t *a_http_simple, }; l_response = !dap_chain_net_balancer_handshake(l_node_info, l_net) ? s_dap_chain_net_node_list_add(l_net, l_node_info) - : ( log_it(L_DEBUG, "Can't do handshake with %s [ %s : %u ]", l_key, l_node_info->ext_host, l_node_info->ext_port), - ERR_HANDSHAKE ); + : ( log_it(L_DEBUG, "Can't do handshake with %s [ %s : %u ]", l_key, l_node_info->ext_host, l_node_info->ext_port), ERR_HANDSHAKE ); *l_return_code = Http_Status_OK; } break; @@ -148,10 +147,8 @@ void dap_chain_net_node_check_http_issue_link(dap_http_simple_t *a_http_simple, *l_return_code = Http_Status_Forbidden; } else { l_response = !dap_global_db_del_sync(l_net->pub.gdb_nodes, l_key) - ? ( log_it(L_DEBUG, "Node %s successfully deleted from nodelist", l_key), - DELETED_OK ) - : ( log_it(L_DEBUG, "Can't delete node %s from nodelist", l_key), - ERR_EXISTS ); + ? ( log_it(L_DEBUG, "Node %s successfully deleted from nodelist", l_key), DELETED_OK ) + : ( log_it(L_DEBUG, "Can't delete node %s from nodelist", l_key), ERR_EXISTS ); *l_return_code = Http_Status_OK; } DAP_DELETE(l_node_info); @@ -159,11 +156,9 @@ void dap_chain_net_node_check_http_issue_link(dap_http_simple_t *a_http_simple, } break; default: - log_it(L_ERROR, "Unsupported protocol version/method in the request to dap_chain_net_node_list module"); - *l_return_code = Http_Status_MethodNotAllowed; - return; + return *l_return_code = Http_Status_MethodNotAllowed, log_it(L_ERROR, "Unsupported protocol version/method"); } - + dap_http_simple_reply(a_http_simple, &l_response, sizeof(uint8_t)); } @@ -171,79 +166,110 @@ static void s_net_node_link_prepare_success(void *a_response, size_t a_response_ http_status_code_t http_status_code) { (void)http_status_code; struct node_link_request *l_node_list_request = (struct node_link_request *)a_arg; +#ifdef DAP_OS_WINDOWS + EnterCriticalSection(&l_node_list_request->wait_crit_sec); + l_node_list_request->response = *(uint8_t*)a_response; + WakeConditionVariable(&l_node_list_request->wait_cond); + LeaveCriticalSection(&l_node_list_request->wait_crit_sec); +#else pthread_mutex_lock(&l_node_list_request->wait_mutex); l_node_list_request->response = *(uint8_t*)a_response; pthread_cond_signal(&l_node_list_request->wait_cond); pthread_mutex_unlock(&l_node_list_request->wait_mutex); +#endif } + static void s_net_node_link_prepare_error(int a_error_code, void *a_arg){ struct node_link_request * l_node_list_request = (struct node_link_request *)a_arg; dap_chain_node_info_t *l_node_info = l_node_list_request->link_info; - if (!l_node_info) { - log_it(L_WARNING, "Link prepare error, code %d", a_error_code); - return; - } + if (!l_node_info) + return log_it(L_WARNING, "Link prepare error, code %d", a_error_code); +#ifdef DAP_OS_WINDOWS + EnterCriticalSection(&l_node_list_request->wait_crit_sec); + l_node_list_request->response = a_error_code; + WakeConditionVariable(&l_node_list_request->wait_cond); + LeaveCriticalSection(&l_node_list_request->wait_crit_sec); +#else pthread_mutex_lock(&l_node_list_request->wait_mutex); l_node_list_request->response = a_error_code; pthread_cond_signal(&l_node_list_request->wait_cond); pthread_mutex_unlock(&l_node_list_request->wait_mutex); +#endif log_it(L_WARNING, "Link from "NODE_ADDR_FP_STR" [ %s : %u ] prepare error with code %d", NODE_ADDR_FP_ARGS_S(l_node_info->address), l_node_info->ext_host, l_node_info->ext_port, a_error_code); } + static struct node_link_request *s_node_list_request_init () { struct node_link_request *l_node_list_request = DAP_NEW_Z(struct node_link_request); - if(!l_node_list_request){ + if (!l_node_list_request) return NULL; - } - l_node_list_request->worker = dap_events_worker_get_auto(); - l_node_list_request->response = 0; - +#ifdef DAP_OS_WINDOWS + InitializeCriticalSection(&l_node_list_request->wait_crit_sec); + InitializeConditionVariable(&l_node_list_request->wait_cond); +#else + l_node_list_request->wait_mutex = PTHREAD_MUTEX_INITIALIZER; +#ifndef DAP_OS_DARWIN pthread_condattr_t attr; pthread_condattr_init(&attr); -#ifdef DAP_OS_DARWIN - struct timespec ts; - ts.tv_sec = 8; - ts.tv_nsec = 0; - pthread_cond_timedwait_relative_np(&l_node_list_request->wait_cond, &l_node_list_request->wait_mutex, - &ts); -#else pthread_condattr_setclock(&attr, CLOCK_MONOTONIC); -#endif pthread_cond_init(&l_node_list_request->wait_cond, &attr); - pthread_mutex_init(&l_node_list_request->wait_mutex, NULL); +#else + l_node_list_request->wait_cond = PTHREAD_COND_INITIALIZER; +#endif +#endif return l_node_list_request; } static void s_node_list_request_deinit (struct node_link_request *a_node_list_request) { +#ifdef DAP_OS_WINDOWS + DeleteCriticalSection(&a_node_list_request->wait_crit_sec); +#else pthread_cond_destroy(&a_node_list_request->wait_cond); pthread_mutex_destroy(&a_node_list_request->wait_mutex); +#endif DAP_DEL_Z(a_node_list_request->link_info); } -static int dap_chain_net_node_list_wait(struct node_link_request *a_node_list_request, int a_timeout_ms){ + +static int dap_chain_net_node_list_wait(struct node_link_request *a_node_list_request, int a_timeout_ms) { +#ifdef DAP_OS_WINDOWS + EnterCriticalSection(&a_node_list_request->wait_crit_sec); + if (a_node_list_request->response) + return LeaveCriticalSection(&a_node_list_request->wait_crit_sec), a_node_list_request->response; + while (!a_node_list_request->response) { + if ( !SleepConditionVariableCS(&a_node_list_request->wait_cond, &a_node_list_request->wait_crit_sec, a_timeout_ms) ) + a_node_list_request->response = GetLastError() == ERROR_TIMEOUT ? ERR_WAIT_TIMEOUT : ERR_UNKNOWN; + } + return LeaveCriticalSection(&a_node_list_request->wait_crit_sec), a_node_list_request->response; +#else pthread_mutex_lock(&a_node_list_request->wait_mutex); if(a_node_list_request->response) - { - pthread_mutex_unlock(&a_node_list_request->wait_mutex); - return a_node_list_request->response; - } + return pthread_mutex_unlock(&a_node_list_request->wait_mutex), a_node_list_request->response; struct timespec l_cond_timeout; - clock_gettime(CLOCK_REALTIME, &l_cond_timeout); - l_cond_timeout.tv_sec += a_timeout_ms/1000; +#ifdef DAP_OS_DARWIN + l_cond_timeout = { .tv_sec = a_timeout_ms / 1000 }; +#else + clock_gettime(CLOCK_MONOTONIC, &l_cond_timeout); + l_cond_timeout.tv_sec += a_timeout_ms / 1000; +#endif while (!a_node_list_request->response) { - int l_wait = pthread_cond_timedwait(&a_node_list_request->wait_cond, &a_node_list_request->wait_mutex, &l_cond_timeout); - if (l_wait == ETIMEDOUT) { - log_it(L_NOTICE, "Waiting for status timeout"); + switch ( +#ifdef DAP_OS_DARWIN + pthread_cond_timedwait_relative_np(&l_node_list_request->wait_cond, &l_node_list_request->wait_mutex, &l_cond_timeout) +#else + pthread_cond_timedwait(&a_node_list_request->wait_cond, &a_node_list_request->wait_mutex, &l_cond_timeout) +#endif + ) { + case ETIMEDOUT: a_node_list_request->response = ERR_WAIT_TIMEOUT; - break; - } else { + default: break; } } - pthread_mutex_unlock(&a_node_list_request->wait_mutex); - return a_node_list_request->response; + return pthread_mutex_unlock(&a_node_list_request->wait_mutex), a_node_list_request->response; +#endif } static int s_cb_node_addr_compare(dap_list_t *a_list_elem, dap_list_t *a_addr_elem) { @@ -258,10 +284,8 @@ int dap_chain_net_node_list_request(dap_chain_net_t *a_net, uint16_t a_port, boo return -1; struct node_link_request *l_link_node_request = s_node_list_request_init(); - if (!l_link_node_request) { - log_it(L_CRITICAL, "%s", c_error_memory_alloc); - return -4; - }; + if (!l_link_node_request) + return log_it(L_CRITICAL, "%s", c_error_memory_alloc), -4; char *l_request = dap_strdup_printf( "%s/%s?version=1,method=%c,addr=%zu,port=%hu,net=%s", DAP_UPLINK_PATH_NODE_LIST, DAP_NODE_LIST_URI_HASH, a_cmd, @@ -273,7 +297,7 @@ int dap_chain_net_node_list_request(dap_chain_net_t *a_net, uint16_t a_port, boo dap_chain_node_info_t *l_remote = dap_chain_node_info_read(a_net, l_seeds_addrs + i); if (!l_remote) continue; - if ( dap_client_http_request(l_link_node_request->worker, l_remote->ext_host, l_remote->ext_port, + if ( dap_client_http_request(dap_worker_get_auto(), l_remote->ext_host, l_remote->ext_port, "GET", "text/text", l_request, NULL, 0, NULL, s_net_node_link_prepare_success, s_net_node_link_prepare_error, l_link_node_request, NULL) ) @@ -311,9 +335,6 @@ int dap_chain_net_node_list_request(dap_chain_net_t *a_net, uint16_t a_port, boo int dap_chain_net_node_list_init() { - /*for (dap_chain_net_t *it = dap_chain_net_iter_start(); it; it = dap_chain_net_iter_next(it)) { - dap_chain_net_add_nodelist_notify_callback(it, s_node_list_callback_notify, it); - }*/ return 0; } diff --git a/modules/net/include/dap_chain_net_node_list.h b/modules/net/include/dap_chain_net_node_list.h index cacbd70f2cb535eabad94cd54c84c746067aab16..8fe6dd6b1b62ef57f0f31f2885210b454b1fa54c 100644 --- a/modules/net/include/dap_chain_net_node_list.h +++ b/modules/net/include/dap_chain_net_node_list.h @@ -32,18 +32,22 @@ along with any CellFrame SDK based project. If not, see <http://www.gnu.org/lic struct node_link_request { dap_chain_node_info_t *link_info; dap_chain_net_t *net; - dap_worker_t *worker; - int link_replace_tries; - int response; +#ifdef DAP_OS_WINDOWS + CONDITION_VARIABLE wait_cond; + CRITICAL_SECTION wait_crit_sec; +#else pthread_cond_t wait_cond; pthread_mutex_t wait_mutex; +#endif + int response; }; + /** * @brief dap_chain_net_node_list_get_gdb_group * @param a_net * @return */ -DAP_STATIC_INLINE char * dap_chain_net_node_list_get_gdb_group(dap_chain_net_t * a_net) +DAP_STATIC_INLINE char* dap_chain_net_node_list_get_gdb_group(dap_chain_net_t * a_net) { return a_net ? dap_strdup_printf("%s.service.orders",a_net->pub.gdb_groups_prefix) : NULL; } diff --git a/modules/type/blocks/dap_chain_cs_blocks.c b/modules/type/blocks/dap_chain_cs_blocks.c index 0b76090d0f15515fc5362c0e5996f22da9e20602..a0feca9322aeb6f880bc3fb2371735949987a467 100644 --- a/modules/type/blocks/dap_chain_cs_blocks.c +++ b/modules/type/blocks/dap_chain_cs_blocks.c @@ -471,12 +471,11 @@ static void s_cli_meta_hash_print(json_object* a_json_obj_out, const char *a_met */ static void s_cli_meta_hex_print(json_object* a_json_obj_out, const char * a_meta_title, dap_chain_block_meta_t * a_meta) { - char *l_data_hex = DAP_NEW_Z_SIZE(char, a_meta->hdr.data_size * 2 + 3); - dap_bin2hex(l_data_hex, a_meta->data, a_meta->hdr.data_size); - char l_tmp_buff[70]={0}; - sprintf(l_tmp_buff,"0x%s\n", l_data_hex); - json_object_object_add(a_json_obj_out, a_meta_title, json_object_new_string(l_tmp_buff)); - DAP_DELETE(l_data_hex); + int l_len = a_meta->hdr.data_size * 2 + 5; + char *l_str = DAP_NEW_STACK_SIZE(char, l_len); + snprintf(l_str, 2, "0x"); + dap_bin2hex(l_str + 2, a_meta->data, a_meta->hdr.data_size); + json_object_object_add(a_json_obj_out, a_meta_title, json_object_new_string(l_str)); } static void s_print_autocollect_table(dap_chain_net_t *a_net, json_object *a_json_obj_out, const char *a_table_name) @@ -498,9 +497,9 @@ static void s_print_autocollect_table(dap_chain_net_t *a_net, json_object *a_jso json_object_array_add(json_arr_out, json_obj_t); SUM_256_256(l_total_value, l_cur_value, &l_total_value); } - char l_tmp_buff[256]; - sprintf(l_tmp_buff,"Autocollect tables content for === %s ===", a_table_name); - json_object_object_add(a_json_obj_out, l_tmp_buff, json_arr_out); + char *l_val = dap_strdup_printf("Autocollect tables content for === %s ===", a_table_name); + json_object_object_add(a_json_obj_out, l_val, json_arr_out); + DAP_DEL_Z(l_val); if (l_objs_count) { dap_global_db_objs_delete(l_objs, l_objs_count); uint256_t l_collect_fee = dap_chain_esbocs_get_fee(a_net->pub.id); @@ -522,14 +521,13 @@ static void s_print_autocollect_table(dap_chain_net_t *a_net, json_object *a_jso char *l_profit_str = dap_chain_balance_to_coins(l_collect_value); char *l_tax_str = dap_chain_balance_to_coins(l_collect_tax); char *l_fee_str = dap_chain_balance_to_coins(l_collect_fee); - sprintf(l_tmp_buff,"Total prepared value: %s %s, where profit is %s, tax is %s, fee is %s\n", + l_val = dap_strdup_printf("Total prepared value: %s %s, where profit is %s, tax is %s, fee is %s\n", l_total_str, a_net->pub.native_ticker, l_profit_str, l_tax_str, l_fee_str); DAP_DEL_MULTY(l_total_str, l_profit_str, l_tax_str, l_fee_str); - } else - strcpy(l_tmp_buff, "Empty"); - char l_status_buf[32]; - sprintf(l_status_buf, "%s status", a_table_name); - json_object_object_add(a_json_obj_out, l_status_buf, json_object_new_string(l_tmp_buff)); + } + char *l_key = dap_strdup_printf("%s status", a_table_name); + json_object_object_add(a_json_obj_out, l_key, json_object_new_string(l_val ? l_val : "Empty")); + DAP_DEL_MULTY(l_key, l_val); } /** @@ -710,20 +708,20 @@ static int s_cli_blocks(int a_argc, char ** a_argv, void **a_str_reply) return DAP_CHAIN_NODE_CLI_COM_BLOCK_FIND_ERR; } dap_chain_block_t *l_block = l_block_cache->block; - char l_tmp_buff[70]={0}; - char l_time_buf[DAP_TIME_STR_SIZE]; - dap_time_to_str_rfc822(l_time_buf, DAP_TIME_STR_SIZE, l_block->hdr.ts_created); + char l_time_buf[DAP_TIME_STR_SIZE], l_hexbuf[32] = { '\0' }; // Header json_object* json_obj_inf = json_object_new_object(); json_object_object_add(json_obj_inf, "Block number", json_object_new_uint64(l_block_cache->block_number)); json_object_object_add(json_obj_inf, "hash", json_object_new_string(l_subcmd_str_arg)); - sprintf(l_tmp_buff,"0x%04X",l_block->hdr.version); - json_object_object_add(json_obj_inf, "version", json_object_new_string(l_tmp_buff)); - sprintf(l_tmp_buff,"0x%016"DAP_UINT64_FORMAT_X"",l_block->hdr.cell_id.uint64); - json_object_object_add(json_obj_inf, "cell_id", json_object_new_string(l_tmp_buff)); - sprintf(l_tmp_buff,"0x%016"DAP_UINT64_FORMAT_X"",l_block->hdr.chain_id.uint64); - json_object_object_add(json_obj_inf, "chain_id", json_object_new_string(l_tmp_buff)); + sprintf(l_hexbuf,"0x%04X",l_block->hdr.version); + + json_object_object_add(json_obj_inf, "version", json_object_new_string(l_hexbuf)); + sprintf(l_hexbuf,"0x%016"DAP_UINT64_FORMAT_X"",l_block->hdr.cell_id.uint64); + json_object_object_add(json_obj_inf, "cell_id", json_object_new_string(l_hexbuf)); + sprintf(l_hexbuf,"0x%016"DAP_UINT64_FORMAT_X"",l_block->hdr.chain_id.uint64); + json_object_object_add(json_obj_inf, "chain_id", json_object_new_string(l_hexbuf)); + dap_time_to_str_rfc822(l_time_buf, DAP_TIME_STR_SIZE, l_block->hdr.ts_created); json_object_object_add(json_obj_inf, "ts_created", json_object_new_string(l_time_buf)); // Dump Metadata @@ -754,14 +752,13 @@ static int s_cli_blocks(int a_argc, char ** a_argv, void **a_str_reply) s_cli_meta_hex_print(json_obj_meta, "NONCE2", l_meta); break; default: { - char * l_data_hex = DAP_NEW_Z_SIZE(char,l_meta->hdr.data_size*2+3); - dap_bin2hex(l_data_hex, l_meta->data, l_meta->hdr.data_size); - sprintf(l_tmp_buff,"0x%0X",i); - json_object_object_add(json_obj_meta, "# -", json_object_new_string(l_tmp_buff)); - sprintf(l_tmp_buff,"0x%s",l_data_hex); - json_object_object_add(json_obj_meta, "Data hex - ", json_object_new_string(l_tmp_buff)); - DAP_DELETE(l_data_hex); - } + sprintf(l_hexbuf, "0x%0X", i); + json_object_object_add(json_obj_meta, "# -", json_object_new_string(l_hexbuf)); + int l_len = l_meta->hdr.data_size * 2 + 5; + char *l_data_hex = DAP_NEW_STACK_SIZE(char, l_len); + snprintf(l_data_hex, 2, "0x"); + dap_bin2hex(l_data_hex + 2, l_meta->data, l_meta->hdr.data_size); + json_object_object_add(json_obj_meta, "Data hex - ", json_object_new_string(l_data_hex)); } } json_object_array_add(json_arr_meta_out, json_obj_meta); l_offset += sizeof(l_meta->hdr) + l_meta->hdr.data_size; @@ -772,7 +769,6 @@ static int s_cli_blocks(int a_argc, char ** a_argv, void **a_str_reply) json_object_array_add(*json_arr_reply, json_obj_datum); json_object* json_arr_datum_out = json_object_new_array(); for (uint32_t i=0; i < l_block_cache->datum_count ; i++){ - char buf[70]; json_object* json_obj_tx = json_object_new_object(); dap_chain_datum_t * l_datum = l_block_cache->datum[i]; size_t l_datum_size = dap_chain_datum_size(l_datum); @@ -783,13 +779,13 @@ static int s_cli_blocks(int a_argc, char ** a_argv, void **a_str_reply) break; } // Nested datums - sprintf(l_tmp_buff,"0x%02X",l_datum->header.version_id); - json_object_object_add(json_obj_tx, "version",json_object_new_string(l_tmp_buff)); - const char * l_datum_type_str="UNKNOWN"; + sprintf(l_hexbuf,"0x%02X",l_datum->header.version_id); + json_object_object_add(json_obj_tx, "version",json_object_new_string(l_hexbuf)); + const char * l_datum_type_str = "UNKNOWN"; DAP_DATUM_TYPE_STR(l_datum->header.type_id, l_datum_type_str); json_object_object_add(json_obj_tx, "type_id",json_object_new_string(l_datum_type_str)); - dap_time_to_str_rfc822(buf, DAP_TIME_STR_SIZE, l_datum->header.ts_create); - json_object_object_add(json_obj_tx, "ts_create",json_object_new_string(buf)); + dap_time_to_str_rfc822(l_time_buf, DAP_TIME_STR_SIZE, l_datum->header.ts_create); + json_object_object_add(json_obj_tx, "ts_create",json_object_new_string(l_time_buf)); json_object_object_add(json_obj_tx, "data_size",json_object_new_int(l_datum->header.data_size)); dap_chain_datum_dump_json(json_obj_tx,l_datum,l_hash_out_type,l_net->pub.id); json_object_array_add(json_arr_datum_out, json_obj_tx); @@ -824,8 +820,6 @@ static int s_cli_blocks(int a_argc, char ** a_argv, void **a_str_reply) dap_pkey_t * l_pub_key = NULL; dap_hash_fast_t l_from_hash = {}, l_to_hash = {}, l_pkey_hash = {}; dap_time_t l_from_time = 0, l_to_time = 0; - char l_tmp_buff[150]={0}; - l_signed_flag = dap_cli_server_cmd_check_option(a_argv, 1, a_argc, "signed") > 0; l_first_signed_flag = dap_cli_server_cmd_check_option(a_argv, 1, a_argc, "first_signed") > 0; l_unspent_flag = dap_cli_server_cmd_check_option(a_argv, 1, a_argc, "-unspent") > 0; @@ -996,12 +990,12 @@ static int s_cli_blocks(int a_argc, char ** a_argv, void **a_str_reply) json_object* json_obj_out = json_object_new_object(); if (l_cert_name || l_pkey_hash_str || l_from_hash_str || l_to_hash_str || l_from_date_str || l_to_date_str) l_filtered_criteria = " filtered according to the specified criteria"; - sprintf(l_tmp_buff,"%s.%s with filter - %s, have blocks",l_net->pub.name,l_chain->name,l_filtered_criteria); - json_object_object_add(json_obj_out, l_tmp_buff, json_object_new_uint64(i_tmp)); + char *l_key = dap_strdup_printf("%s.%s with filter - %s, have blocks",l_net->pub.name,l_chain->name,l_filtered_criteria); + json_object_object_add(json_obj_out, l_key, json_object_new_uint64(i_tmp)); + DAP_DELETE(l_key); json_object_array_add(*json_arr_reply,json_obj_out); } break; case SUBCMD_LAST: { - char l_tmp_buff[70]={0}; json_object* json_obj_out = json_object_new_object(); dap_chain_block_cache_t *l_last_block = HASH_LAST(PVT(l_blocks)->blocks); char l_buf[DAP_TIME_STR_SIZE]; @@ -1011,8 +1005,9 @@ static int s_cli_blocks(int a_argc, char ** a_argv, void **a_str_reply) json_object_object_add(json_obj_out, "Last block hash", json_object_new_string(l_last_block ? l_last_block->block_hash_str : "empty")); json_object_object_add(json_obj_out, "ts_created", json_object_new_string(l_last_block ? l_buf : "never")); - sprintf(l_tmp_buff,"%s.%s has blocks", l_net->pub.name, l_chain->name); - json_object_object_add(json_obj_out, l_tmp_buff, json_object_new_uint64(PVT(l_blocks)->blocks_count)); + char *l_key = dap_strdup_printf("%s.%s has blocks", l_net->pub.name, l_chain->name); + json_object_object_add(json_obj_out, l_key, json_object_new_uint64(PVT(l_blocks)->blocks_count)); + DAP_DELETE(l_key); json_object_array_add(*json_arr_reply, json_obj_out); } break; case SUBCMD_FIND: { @@ -1045,10 +1040,10 @@ static int s_cli_blocks(int a_argc, char ** a_argv, void **a_str_reply) json_object_array_add(*json_arr_reply, json_obj_out); } break; case SUBCMD_COUNT: { - char l_tmp_buff[70]={0}; json_object* json_obj_out = json_object_new_object(); - sprintf(l_tmp_buff,"%s.%s has blocks - ",l_net->pub.name,l_chain->name); - json_object_object_add(json_obj_out, l_tmp_buff, json_object_new_uint64(PVT(l_blocks)->blocks_count)); + char *l_key = dap_strdup_printf("%s.%s has blocks - ", l_net->pub.name,l_chain->name); + json_object_object_add(json_obj_out, l_key, json_object_new_uint64(PVT(l_blocks)->blocks_count)); + DAP_DELETE(l_key); json_object_array_add(*json_arr_reply, json_obj_out); } break; @@ -1060,7 +1055,6 @@ static int s_cli_blocks(int a_argc, char ** a_argv, void **a_str_reply) const char * l_addr_str = NULL; const char * l_hash_out_type = NULL; const char * l_hash_str = NULL; - char l_tmp_buff[70]={0}; uint256_t l_fee_value = {}; size_t l_hashes_count = 0; @@ -1098,9 +1092,13 @@ static int s_cli_blocks(int a_argc, char ** a_argv, void **a_str_reply) } char *l_decree_hash_str = s_blocks_decree_set_reward(l_net, l_chain, l_value, l_cert); if (l_decree_hash_str) { - //добавить вывод - dap_json_rpc_error_add(DAP_CHAIN_NODE_CLI_COM_BLOCK_OK, "Decree with hash %s created to set basic block sign reward", l_decree_hash_str); + //dap_json_rpc_error_add(DAP_CHAIN_NODE_CLI_COM_BLOCK_OK, "Decree with hash %s created to set basic block sign reward", l_decree_hash_str); + json_object* json_obj_out = json_object_new_object(); + char *l_val = dap_strdup_printf("Decree with hash %s created to set basic block sign reward", l_decree_hash_str); DAP_DELETE(l_decree_hash_str); + json_object_object_add(json_obj_out, "status", json_object_new_string(l_val)); + DAP_DELETE(l_val); + json_object_array_add(*json_arr_reply, json_obj_out); } else { dap_json_rpc_error_add(DAP_CHAIN_NODE_CLI_COM_BLOCK_SIGN_ERR, "Basic block sign reward setting failed. Examine log file for details"); return DAP_CHAIN_NODE_CLI_COM_BLOCK_SIGN_ERR; @@ -1110,8 +1108,9 @@ static int s_cli_blocks(int a_argc, char ** a_argv, void **a_str_reply) uint256_t l_cur_reward = dap_chain_net_get_reward(l_net, UINT64_MAX); const char *l_reward_str; dap_uint256_to_char(l_cur_reward, &l_reward_str); json_object* json_obj_out = json_object_new_object(); - sprintf(l_tmp_buff,"Current base block reward is %s\n", l_reward_str); - json_object_object_add(json_obj_out, "status", json_object_new_string(l_tmp_buff)); + char *l_val = dap_strdup_printf("Current base block reward is %s\n", l_reward_str); + json_object_object_add(json_obj_out, "status", json_object_new_string(l_val)); + DAP_DELETE(l_val); json_object_array_add(*json_arr_reply, json_obj_out); break; } else if (dap_cli_server_cmd_check_option(a_argv, arg_index, a_argc, "collect") == -1) { @@ -1177,15 +1176,14 @@ static int s_cli_blocks(int a_argc, char ** a_argv, void **a_str_reply) return DAP_CHAIN_NODE_CLI_COM_BLOCK_HASH_ERR; } - char *l_hash_tx = NULL; - if (l_subcmd == SUBCMD_FEE) - l_hash_tx = dap_chain_mempool_tx_coll_fee_create(l_blocks, l_cert->enc_key, l_addr, l_block_list, l_fee_value, l_hash_out_type); - else - l_hash_tx = dap_chain_mempool_tx_reward_create(l_blocks, l_cert->enc_key, l_addr, l_block_list, l_fee_value, l_hash_out_type); + char *l_hash_tx = l_subcmd == SUBCMD_FEE + ? dap_chain_mempool_tx_coll_fee_create(l_blocks, l_cert->enc_key, l_addr, l_block_list, l_fee_value, l_hash_out_type) + : dap_chain_mempool_tx_reward_create(l_blocks, l_cert->enc_key, l_addr, l_block_list, l_fee_value, l_hash_out_type); if (l_hash_tx) { json_object* json_obj_out = json_object_new_object(); - sprintf(l_tmp_buff, "TX for %s collection created successfully, hash = %s\n", l_subcmd_str, l_hash_tx); - json_object_object_add(json_obj_out, "status", json_object_new_string(l_tmp_buff)); + char *l_val = dap_strdup_printf(l_val, "TX for %s collection created successfully, hash = %s\n", l_subcmd_str, l_hash_tx); + json_object_object_add(json_obj_out, "status", json_object_new_string(l_val)); + DAP_DELETE(l_val); json_object_array_add(*json_arr_reply, json_obj_out); DAP_DELETE(l_hash_tx); } else { @@ -1200,7 +1198,6 @@ static int s_cli_blocks(int a_argc, char ** a_argv, void **a_str_reply) const char *l_cert_name = NULL, *l_addr_str = NULL; dap_hash_fast_t l_pkey_hash = {}; size_t l_block_count = 0; - char l_tmp_buff[128]={0}; if (dap_cli_server_cmd_check_option(a_argv, arg_index,a_argc, "renew") > 0) { dap_cli_server_cmd_find_option_val(a_argv, arg_index, a_argc, "-cert", &l_cert_name); if(!l_cert_name) { @@ -1277,8 +1274,9 @@ static int s_cli_blocks(int a_argc, char ** a_argv, void **a_str_reply) char l_buf[DAP_TIME_STR_SIZE]; json_object* json_obj_bl = json_object_new_object(); dap_time_to_str_rfc822(l_buf, DAP_TIME_STR_SIZE, l_ts); - sprintf(l_tmp_buff, "fee - \t%s: ts_create=%s\n", l_block_cache->block_hash_str, l_buf); - json_object_object_add(json_obj_bl, "block", json_object_new_string(l_tmp_buff)); + char *l_val = dap_strdup_printf("fee - \t%s: ts_create=%s\n", l_block_cache->block_hash_str, l_buf); + json_object_object_add(json_obj_bl, "block", json_object_new_string(l_val)); + DAP_DELETE(l_val); json_object_array_add(json_arr_bl_out, json_obj_bl); l_block_count++; break; @@ -1308,18 +1306,20 @@ static int s_cli_blocks(int a_argc, char ** a_argv, void **a_str_reply) char l_buf[DAP_TIME_STR_SIZE]; json_object* json_obj_bl = json_object_new_object(); dap_time_to_str_rfc822(l_buf, DAP_TIME_STR_SIZE, l_ts); - sprintf(l_tmp_buff, "rewards - \t%s: ts_create=%s\n", l_block_cache->block_hash_str, l_buf); - json_object_object_add(json_obj_bl, "block", json_object_new_string(l_tmp_buff)); + char *l_val = dap_strdup_printf("rewards - \t%s: ts_create=%s\n", l_block_cache->block_hash_str, l_buf); + json_object_object_add(json_obj_bl, "block", json_object_new_string(l_val)); + DAP_DELETE(l_val); json_object_array_add(json_arr_bl_out, json_obj_bl); l_block_count++; } json_object_array_add(*json_arr_reply, json_arr_bl_out); json_object* json_obj_out = json_object_new_object(); - sprintf(l_tmp_buff, "%s.%s: Have %"DAP_UINT64_FORMAT_U" blocks\n", + char *l_val = dap_strdup_printf("%s.%s: Have %"DAP_UINT64_FORMAT_U" blocks\n", l_net->pub.name, l_chain->name, l_block_count); - json_object_object_add(json_obj_out, "status", json_object_new_string(l_tmp_buff)); + json_object_object_add(json_obj_out, "status", json_object_new_string(l_val)); + DAP_DELETE(l_val); json_object_array_add(*json_arr_reply, json_obj_out); - }else{ + } else { if (dap_cli_server_cmd_check_option(a_argv, arg_index, a_argc, "status") == -1) { dap_json_rpc_error_add(DAP_CHAIN_NODE_CLI_COM_BLOCK_PARAM_ERR, "Command 'block autocollect' requires subcommand 'status'"); @@ -1328,10 +1328,10 @@ static int s_cli_blocks(int a_argc, char ** a_argv, void **a_str_reply) json_object* json_obj_out = json_object_new_object(); json_object_array_add(*json_arr_reply, json_obj_out); bool l_status = dap_chain_esbocs_get_autocollect_status(l_net->pub.id); - char l_tmp_buff[150]={0}; - sprintf(l_tmp_buff, "for network %s is %s\n", l_net->pub.name, - l_status ? "active" : "inactive, cause the network config or consensus starting problems"); - json_object_object_add(json_obj_out, "Autocollect status", json_object_new_string(l_tmp_buff)); + char *l_val = dap_strdup_printf("for network %s is %s\n", l_net->pub.name, + l_status ? "active" : "inactive cause of the network config or consensus starting problems"); + json_object_object_add(json_obj_out, "Autocollect status", json_object_new_string(l_val)); + DAP_DELETE(l_val); if (!l_status) break; s_print_autocollect_table(l_net, json_obj_out, "Fees"); @@ -1341,13 +1341,12 @@ static int s_cli_blocks(int a_argc, char ** a_argv, void **a_str_reply) case SUBCMD_UNDEFINED: default: { - char l_tmp_buff[70]={0}; json_object* json_obj_out = json_object_new_object(); - snprintf(l_tmp_buff, sizeof(l_tmp_buff), "Undefined block subcommand \"%s\" ", - l_subcmd_str); - json_object_object_add(json_obj_out, "status", json_object_new_string(l_tmp_buff)); + char *l_val = dap_strdup_printf("Undefined block subcommand \"%s\" ", l_subcmd_str); + json_object_object_add(json_obj_out, "status", json_object_new_string(l_val)); + DAP_DELETE(l_val); json_object_array_add(*json_arr_reply, json_obj_out); - ret=DAP_CHAIN_NODE_CLI_COM_BLOCK_UNKNOWN; + ret = DAP_CHAIN_NODE_CLI_COM_BLOCK_UNKNOWN; } break; }