diff --git a/CMakeLists.txt b/CMakeLists.txt index d07a7b0e3a31e3bbc2b4a00b571e25fd14117ac6..e81252aca6aa606429f281d31aba9bc1c9b4d170 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,7 +3,7 @@ cmake_minimum_required(VERSION 3.10) include (cmake/OS_Detection.cmake) set(CMAKE_C_STANDARD 11) -set(CELLFRAME_SDK_NATIVE_VERSION "3.0-19") +set(CELLFRAME_SDK_NATIVE_VERSION "3.0-20") add_definitions ("-DCELLFRAME_SDK_VERSION=\"${CELLFRAME_SDK_NATIVE_VERSION}\"") set(DAPSDK_MODULES "") diff --git a/dap-sdk/net/core/dap_timerfd.c b/dap-sdk/net/core/dap_timerfd.c index 6a34ba8ada511b5e63ff36f9167b3335954d0c23..d631761774d4085426f1fb740a227c1c01e3f4cb 100644 --- a/dap-sdk/net/core/dap_timerfd.c +++ b/dap-sdk/net/core/dap_timerfd.c @@ -327,6 +327,10 @@ void dap_timerfd_reset(dap_timerfd_t *a_timerfd) */ void dap_timerfd_delete(dap_timerfd_t *a_timerfd) { + #ifdef _WIN32 + DeleteTimerQueueTimer(hTimerQueue, (HANDLE)a_timerfd->th, NULL); + #endif + if (a_timerfd->events_socket->worker) dap_events_socket_remove_and_delete_mt(a_timerfd->events_socket->worker, a_timerfd->esocket_uuid); } diff --git a/modules/chain/dap_chain_ledger.c b/modules/chain/dap_chain_ledger.c index 71d4950d1ca5d8d962d7b60b517bf4ca18f82203..a5e276d477cd2c840c5ebe469ef57244d22cfafb 100644 --- a/modules/chain/dap_chain_ledger.c +++ b/modules/chain/dap_chain_ledger.c @@ -388,7 +388,7 @@ int dap_chain_ledger_token_add(dap_ledger_t * a_ledger, dap_chain_datum_token_t if (!dap_chain_global_db_gr_set(dap_strdup(a_token->ticker), l_token_cache, a_token_size, l_gdb_group)) { if(s_debug_more) log_it(L_WARNING, "Ledger cache mismatch"); - DAP_DELETE(l_token_cache); + //DAP_DELETE(l_token_cache); } DAP_DELETE(l_gdb_group); l_token_item->type = a_token->type; @@ -1040,11 +1040,11 @@ bool s_update_token_cache(dap_ledger_t *a_ledger, dap_chain_ledger_token_item_t { if(s_debug_more) log_it(L_WARNING, "Ledger cache mismatch"); - DAP_DELETE(l_token_cache); + //DAP_DELETE(l_gdb_group); + return false; } DAP_DELETE(l_gdb_group); - return true; } @@ -1324,16 +1324,13 @@ int dap_chain_ledger_token_emission_add(dap_ledger_t *a_ledger, byte_t *a_token_ // update token current_supply_ // - if (!PVT(a_ledger)->load_mode && l_token_item && !s_token_supply_limit_disable) - { + if (!PVT(a_ledger)->load_mode && l_token_item && !s_token_supply_limit_disable){ if (!s_update_token_cache(a_ledger, l_token_item, l_token_emission_item->datum_token_emission->hdr.value)) return DAP_CHAIN_CS_VERIFY_CODE_TX_NO_EMISSION; } if (s_token_supply_limit_disable) - { log_it(L_WARNING,"s_token_supply_limit_disable is enabled in config, please fix it and disable"); - } l_token_emission_item->datum_token_emission_size = a_token_emission_size; pthread_rwlock_wrlock( l_token_item ? &l_token_item->token_emissions_rwlock @@ -1354,7 +1351,7 @@ int dap_chain_ledger_token_emission_add(dap_ledger_t *a_ledger, byte_t *a_token_ char *l_gdb_group = dap_chain_ledger_get_gdb_group(a_ledger, DAP_CHAIN_LEDGER_EMISSIONS_STR); if (!dap_chain_global_db_gr_set(dap_strdup(l_hash_str), l_emission_cache, a_token_emission_size, l_gdb_group)) { log_it(L_WARNING, "Ledger cache mismatch"); - DAP_DELETE(l_emission_cache); + // DAP_DELETE(l_emission_cache); } DAP_DELETE(l_gdb_group); } char * l_token_emission_address_str = dap_chain_addr_to_str(&(l_token_emission_item->datum_token_emission->hdr.address) ); @@ -2175,7 +2172,7 @@ static int s_balance_cache_update(dap_ledger_t *a_ledger, dap_ledger_wallet_bala if (!dap_chain_global_db_gr_set(dap_strdup(a_balance->key), l_balance_value, sizeof(uint128_t), l_gdb_group)) { if(s_debug_more) log_it(L_WARNING, "Ledger cache mismatch"); - DAP_DELETE(l_balance_value); + //DAP_DELETE(l_balance_value); return -1; } DAP_DELETE(l_gdb_group); @@ -2193,7 +2190,7 @@ static int s_tx_cache_update(dap_ledger_t *a_ledger, dap_chain_ledger_tx_item_t if (!dap_chain_global_db_gr_set(l_tx_hash_str, l_tx_cache, l_tx_size + sizeof(a_item->cache_data), l_gdb_group)) { if(s_debug_more) log_it(L_WARNING, "Ledger cache mismatch"); - DAP_DELETE(l_tx_cache); + //DAP_DELETE(l_tx_cache); return -1; } DAP_DELETE(l_gdb_group); @@ -2524,7 +2521,7 @@ int dap_chain_ledger_tx_add(dap_ledger_t *a_ledger, dap_chain_datum_tx_t *a_tx, if (!dap_chain_global_db_gr_set(dap_strdup(l_tx_hash_str), l_tx_cache, l_tx_size + sizeof(l_item_tmp->cache_data), l_gdb_group)) { if(s_debug_more) log_it(L_WARNING, "Ledger cache mismatch"); - DAP_DELETE(l_tx_cache); + // DAP_DELETE(l_tx_cache); } DAP_DELETE(l_gdb_group); if (!l_from_threshold) @@ -2613,7 +2610,7 @@ int dap_chain_ledger_tx_remove(dap_ledger_t *a_ledger, dap_chain_hash_fast_t *a_ if (!dap_chain_global_db_gr_set(l_tx_hash_str, l_cache_data, -1, l_gdb_group)) { if(s_debug_more) log_it(L_WARNING, "Ledger cache mismatch"); - DAP_DELETE(l_cache_data); + // DAP_DELETE(l_cache_data); } DAP_DELETE(l_gdb_group); } diff --git a/modules/channel/chain/dap_stream_ch_chain.c b/modules/channel/chain/dap_stream_ch_chain.c index dcc5238e8299fa08bb602d2fc4615aedd667874a..9bf4cc00e32f35e25c9ece9823d304076424990c 100644 --- a/modules/channel/chain/dap_stream_ch_chain.c +++ b/modules/channel/chain/dap_stream_ch_chain.c @@ -705,26 +705,32 @@ static bool s_gdb_in_pkt_proc_callback(dap_proc_thread_t *a_thread, void *a_arg) } // apply received transaction dap_chain_t *l_chain = dap_chain_find_by_id(l_sync_request->request_hdr.net_id, l_sync_request->request_hdr.chain_id); - if(l_chain) { + // if chain is zero, it can be on of GDB group + if (!l_chain) + l_chain = dap_chain_get_chain_from_group_name(l_sync_request->request_hdr.net_id, l_obj->group); + + if (l_chain) { if(l_chain->callback_add_datums_with_group){ void * restrict l_store_obj_value = l_store_obj[i].value; l_chain->callback_add_datums_with_group(l_chain, - (dap_chain_datum_t** restrict) l_store_obj_value, 1, + (dap_chain_datum_t** restrict) &l_store_obj_value, 1, l_store_obj[i].group); } } - // save data to global_db - dap_store_obj_t *l_obj_copy = dap_store_obj_copy(l_obj, 1); - if(!dap_chain_global_db_obj_save(l_obj_copy, 1)) { - struct sync_request *l_sync_req_err = DAP_DUP(l_sync_request); - dap_proc_thread_worker_exec_callback(a_thread, l_sync_request->worker->id, - s_gdb_in_pkt_error_worker_callback, l_sync_req_err); - } else { - if (s_debug_more) - log_it(L_DEBUG, "Added new GLOBAL_DB synchronization record"); - } - DAP_DELETE(l_obj_copy->group); - DAP_DELETE(l_obj_copy); + else{ + // save data to global_db + dap_store_obj_t *l_obj_copy = dap_store_obj_copy(l_obj, 1); + if(!dap_chain_global_db_obj_save(l_obj_copy, 1)) { + struct sync_request *l_sync_req_err = DAP_DUP(l_sync_request); + dap_proc_thread_worker_exec_callback(a_thread, l_sync_request->worker->id, + s_gdb_in_pkt_error_worker_callback, l_sync_req_err); + } else { + if (s_debug_more) + log_it(L_DEBUG, "Added new GLOBAL_DB synchronization record"); + } + DAP_DELETE(l_obj_copy->group); + DAP_DELETE(l_obj_copy); + } } if(l_store_obj) { dap_store_obj_free(l_store_obj, l_data_obj_count); diff --git a/modules/net/dap_chain_net.c b/modules/net/dap_chain_net.c index 4b93f47323a9287a122009cb9b3952ff4040097d..c67902413a83a42216eb8474bd8047e807752cea 100644 --- a/modules/net/dap_chain_net.c +++ b/modules/net/dap_chain_net.c @@ -430,15 +430,20 @@ void dap_chain_net_sync_gdb_broadcast(void *a_arg, const char a_op_code, const c l_obj->group = dap_strdup(a_group); dap_store_obj_pkt_t *l_data_out = dap_store_packet_single(l_obj); dap_store_obj_free(l_obj, 1); - dap_chain_t *l_chain = dap_chain_net_get_chain_by_name(l_net, "gdb"); - dap_chain_id_t l_chain_id = l_chain ? l_chain->id : (dap_chain_id_t) {}; pthread_rwlock_rdlock(&PVT(l_net)->rwlock); + // get chain id for transfering to client side + dap_chain_id_t l_chain_id; + l_chain_id.uint64 = 0; + dap_chain_t *l_chain = dap_chain_get_chain_from_group_name(l_net->pub.id, a_group); + if (l_chain) + l_chain_id = l_chain ? l_chain->id : (dap_chain_id_t) {}; + for (dap_list_t *l_tmp = PVT(l_net)->net_links; l_tmp; l_tmp = dap_list_next(l_tmp)) { dap_chain_node_client_t *l_node_client = ((struct net_link *)l_tmp->data)->link; if (!l_node_client) continue; dap_stream_worker_t *l_stream_worker = dap_client_get_stream_worker(l_node_client->client); - if (l_stream_worker) + if (!l_stream_worker) continue; dap_stream_ch_chain_pkt_write_mt(l_stream_worker, l_node_client->ch_chain_uuid, DAP_STREAM_CH_CHAIN_PKT_TYPE_GLOBAL_DB, l_net->pub.id.uint64, l_chain_id.uint64, l_net->pub.cell_id.uint64, l_data_out, @@ -496,6 +501,9 @@ static void s_chain_callback_notify(void * a_arg, dap_chain_t *a_chain, dap_chai } } pthread_rwlock_unlock(&PVT(l_net)->rwlock); + }else{ + if (s_debug_more) + log_it(L_WARNING,"Node current state is %d. Real-time syncing is possible when you in NET_STATE_LINKS_ESTABLISHED (and above) state", PVT(l_net)->state); } } @@ -1482,43 +1490,26 @@ bool s_chain_net_reload_ledger_cache_once(dap_chain_net_t *l_net) { if (!l_net) return false; - - // + // create directory for cache checking file (cellframe-node/cache) - // - char *l_cache_dir = dap_strdup_printf( "%s/%s", g_sys_dir_path, "cache"); - - if (dap_mkdir_with_parents(l_cache_dir) != 0) - { + if (dap_mkdir_with_parents(l_cache_dir) != 0){ log_it(L_WARNING,"Error during one time cache reloading check file creation"); return false; } - // // create file, if it not presented. If file exists, ledger cache operation is stopped - // - char *l_cache_file = dap_strdup_printf( "%s/%s.cache", l_cache_dir, "4CFB3928-1A9A-467D-BB5E-3FDB35014E8A"); - - if (dap_file_simple_test(l_cache_file)) - { + if (dap_file_simple_test(l_cache_file)){ log_it(L_DEBUG,"Ledger cache was already reloaded"); return false; } - log_it(L_WARNING,"Start one time ledger cache reloading"); - static FILE *s_cache_file = NULL; - s_cache_file = fopen(l_cache_file, "a"); - - if(!s_cache_file) - { + if(!s_cache_file){ s_cache_file = fopen(l_cache_file, "w"); - - if (!s_cache_file) - { + if (!s_cache_file){ dap_fprintf(stderr, "Can't open cache file %s for one time ledger cache reloading.\ Please, do it manually using command\ cellframe-node-cli net -net <network_name>> ledger reload'\n", l_cache_file); @@ -1526,15 +1517,10 @@ bool s_chain_net_reload_ledger_cache_once(dap_chain_net_t *l_net) } } - // // reload ledger cache (same as net -net <network_name>> ledger reload command) - // - if (dap_file_simple_test(l_cache_file)) s_chain_net_ledger_cache_reload(l_net); - fclose(s_cache_file); - return true; } @@ -2551,6 +2537,62 @@ uint16_t dap_chain_net_acl_idx_by_id(dap_chain_net_id_t a_id) return l_net ? PVT(l_net)->acl_idx : (uint16_t)-1; } +/** + * @brief + * + * @param net_id + * @param group_name + * @return dap_chain_t* + */ +dap_chain_t *dap_chain_get_chain_from_group_name(dap_chain_net_id_t a_net_id, const char *a_group_name) +{ + if (!a_group_name) { + log_it(L_ERROR, "GDB group name is NULL "); + return NULL; + } + dap_chain_net_t *l_net = dap_chain_net_by_id(a_net_id); + if (!l_net) + return false; + dap_chain_t *l_chain = NULL; + DL_FOREACH(l_net->pub.chains, l_chain) { + char *l_chain_group_name = dap_chain_net_get_gdb_group_from_chain(l_chain); + if (!strcmp(a_group_name, l_chain_group_name)) { + DAP_DELETE(l_chain_group_name); + return l_chain; + } + DAP_DELETE(l_chain_group_name); + } + return NULL; +} + +/** + * @brief + * + * @param net_id + * @param group_name + * @return dap_chain_t* + */ +dap_chain_t *dap_chain_get_chain_from_mempool_group(dap_chain_net_id_t a_net_id, const char *a_group_name) +{ + if (!a_group_name) { + log_it(L_ERROR, "GDB group name is NULL "); + return NULL; + } + dap_chain_net_t *l_net = dap_chain_net_by_id(a_net_id); + if (!l_net) + return false; + dap_chain_t *l_chain = NULL; + DL_FOREACH(l_net->pub.chains, l_chain) { + char *l_chain_group_name = dap_chain_net_get_gdb_group_mempool(l_chain); + if (!strcmp(a_group_name, l_chain_group_name)) { + DAP_DELETE(l_chain_group_name); + return l_chain; + } + DAP_DELETE(l_chain_group_name); + } + return NULL; +} + /** * @brief dap_chain_net_id_by_name diff --git a/modules/net/dap_chain_node_client.c b/modules/net/dap_chain_node_client.c index ebb1c62614301866383f129052fdcd83c1c5ee1a..d6a0069024ed8e94310fcb38a5af88f3e0002001 100644 --- a/modules/net/dap_chain_node_client.c +++ b/modules/net/dap_chain_node_client.c @@ -496,15 +496,25 @@ static void s_ch_chain_callback_notify_packet_in(dap_stream_ch_chain_t* a_ch_cha bool l_have_waiting = dap_chain_net_sync_unlock(l_net, l_node_client); l_node_client->state = NODE_CLIENT_STATE_SYNCED; if (dap_chain_net_get_target_state(l_net) == NET_STATE_ONLINE) - dap_chain_net_set_state(l_net, NET_STATE_ONLINE); + { + dap_timerfd_reset(l_node_client->sync_timer); + dap_chain_net_set_state(l_net, NET_STATE_ONLINE); + #ifndef _WIN32 + pthread_cond_broadcast(&l_node_client->wait_cond); + #else + SetEvent( l_node_client->wait_cond ); + #endif + } else if (!l_have_waiting) + { + #ifndef _WIN32 + pthread_cond_broadcast(&l_node_client->wait_cond); + #else + SetEvent( l_node_client->wait_cond ); + #endif + // l_node_client object is not presented after dap_chain_net_state_go_to with NET_STATE_OFFLINE dap_chain_net_state_go_to(l_net, NET_STATE_OFFLINE); - dap_timerfd_reset(l_node_client->sync_timer); -#ifndef _WIN32 - pthread_cond_broadcast(&l_node_client->wait_cond); -#else - SetEvent( l_node_client->wait_cond ); -#endif + } } } break; default: break; @@ -780,6 +790,8 @@ void dap_chain_node_client_close(dap_chain_node_client_t *a_client) dap_chain_node_client_handle_t * l_client_found = NULL; HASH_FIND(hh,s_clients,&a_client->uuid,sizeof(a_client->uuid),l_client_found); if (l_client_found) { + if (l_client_found->client->sync_timer) + dap_timerfd_delete(l_client_found->client->sync_timer); HASH_DEL(s_clients,l_client_found); DAP_DELETE(l_client_found); if (a_client->callbacks.delete) diff --git a/modules/net/include/dap_chain_net.h b/modules/net/include/dap_chain_net.h index 0d806572a5e6c6cf8442fb329acb07661450d8da..f9332d19aafca58b45e9f3833aeb9c5e1be0bd9a 100644 --- a/modules/net/include/dap_chain_net.h +++ b/modules/net/include/dap_chain_net.h @@ -136,6 +136,8 @@ dap_chain_cell_id_t * dap_chain_net_get_cur_cell( dap_chain_net_t * l_net); dap_list_t* dap_chain_net_get_link_node_list(dap_chain_net_t * l_net, bool a_is_only_cur_cell); dap_list_t* dap_chain_net_get_node_list(dap_chain_net_t * l_net); +dap_chain_t * dap_chain_get_chain_from_group_name(dap_chain_net_id_t a_net_id, const char *a_group_name); +dap_chain_t * dap_chain_get_chain_from_mempool_group(dap_chain_net_id_t a_net_id, const char *a_group_name); typedef enum dap_chain_net_tx_search_type { /// Search local, in memory, possible load data from drive to memory @@ -174,6 +176,15 @@ DAP_STATIC_INLINE char * dap_chain_net_get_gdb_group_mempool(dap_chain_t * l_cha return NULL; } +DAP_STATIC_INLINE char * dap_chain_net_get_gdb_group_from_chain(dap_chain_t * l_chain) +{ + dap_chain_net_t * l_net = l_chain ? dap_chain_net_by_id(l_chain->net_id) : NULL; + if ( l_net ) + return dap_strdup_printf( "chain-gdb.%s.chain-%016llX",l_net->pub.name, l_chain->id.uint64); + + return NULL; +} + dap_chain_t * dap_chain_net_get_chain_by_chain_type(dap_chain_net_t * l_net, dap_chain_type_t a_datum_type); char * dap_chain_net_get_gdb_group_mempool_by_chain_type(dap_chain_net_t * l_net, dap_chain_type_t a_datum_type); dap_chain_net_t **dap_chain_net_list(uint16_t *a_size); diff --git a/modules/type/dag/dap_chain_cs_dag_event.c b/modules/type/dag/dap_chain_cs_dag_event.c index badd319df592566f672fa7fdb76efde2776f23d0..0e57cdbb1c6704d46a0c51f813f75e043b0c2ec3 100644 --- a/modules/type/dag/dap_chain_cs_dag_event.c +++ b/modules/type/dag/dap_chain_cs_dag_event.c @@ -200,7 +200,7 @@ bool dap_chain_cs_dag_event_gdb_set(char *a_event_hash_str, dap_chain_cs_dag_eve bool ret = dap_chain_global_db_gr_set(dap_strdup(a_event_hash_str), (uint8_t *)l_event_round_item, dap_chain_cs_dag_event_round_item_get_size(l_event_round_item), a_group); - DAP_DELETE(l_event_round_item); + //DAP_DELETE(l_event_round_item); return ret; }