From 314d17514cadc68aa90074229f0f5f9de4a38d26 Mon Sep 17 00:00:00 2001
From: cellframe <roman.khlopkov@demlabs.net>
Date: Fri, 25 Nov 2022 19:04:02 +0300
Subject: [PATCH] [*] Conditonal variables usage refactoring

---
 modules/chain/dap_chain_ledger.c              |  8 +-
 .../consensus/dag-poa/dap_chain_cs_dag_poa.c  |  4 +
 modules/consensus/none/dap_chain_cs_none.c    |  6 +-
 modules/net/dap_chain_node_cli_cmd.c          | 24 +++---
 modules/net/dap_chain_node_client.c           | 85 ++++++++-----------
 modules/service/vpn/dap_chain_net_srv_vpn.c   | 36 ++++----
 modules/type/dag/dap_chain_cs_dag.c           |  4 +-
 modules/type/dag/dap_chain_cs_dag_event.c     |  2 +-
 8 files changed, 80 insertions(+), 89 deletions(-)

diff --git a/modules/chain/dap_chain_ledger.c b/modules/chain/dap_chain_ledger.c
index edfa293808..a7fa58dd6a 100644
--- a/modules/chain/dap_chain_ledger.c
+++ b/modules/chain/dap_chain_ledger.c
@@ -240,6 +240,7 @@ typedef struct dap_ledger_private {
     // Save/load operations condition
     pthread_mutex_t load_mutex;
     pthread_cond_t load_cond;
+    bool load_end;
 
     uint16_t check_flags;
     bool check_ds;
@@ -1562,6 +1563,7 @@ static void s_load_cache_gdb_loaded_balances_callback(dap_global_db_context_t *a
         json_object_put(l_json);*/ // TODO: unstable and spammy
     }
     pthread_mutex_lock( &l_ledger_pvt->load_mutex );
+    l_ledger_pvt->load_end = true;
     pthread_cond_broadcast( &l_ledger_pvt->load_cond );
     pthread_mutex_unlock( &l_ledger_pvt->load_mutex );
 }
@@ -1728,6 +1730,7 @@ static void s_load_cache_gdb_loaded_tokens_callback(dap_global_db_context_t *a_g
     if( a_rc != 0){
         log_it(L_NOTICE, "No ledger cache found");
         pthread_mutex_lock(&l_ledger_pvt->load_mutex);
+        l_ledger_pvt->load_end = true;
         pthread_cond_broadcast(&l_ledger_pvt->load_cond );
         pthread_mutex_unlock(&l_ledger_pvt->load_mutex);
 
@@ -1767,7 +1770,8 @@ void dap_chain_ledger_load_cache(dap_ledger_t *a_ledger)
 
     pthread_mutex_lock(& l_ledger_pvt->load_mutex);
     dap_global_db_get_all(l_gdb_group,0,s_load_cache_gdb_loaded_tokens_callback, a_ledger);
-    pthread_cond_wait(& l_ledger_pvt->load_cond, &l_ledger_pvt->load_mutex);
+    while (!l_ledger_pvt->load_end)
+        pthread_cond_wait(& l_ledger_pvt->load_cond, &l_ledger_pvt->load_mutex);
     pthread_mutex_unlock(& l_ledger_pvt->load_mutex);
 
     DAP_DELETE(l_gdb_group);
@@ -4031,6 +4035,8 @@ void dap_chain_ledger_purge(dap_ledger_t *a_ledger, bool a_preserve_db)
     pthread_rwlock_unlock(&l_ledger_priv->threshold_txs_rwlock);
     pthread_rwlock_unlock(&l_ledger_priv->balance_accounts_rwlock);
     pthread_rwlock_unlock(&l_ledger_priv->stake_lock_rwlock);
+
+    l_ledger_priv->load_end = false;
 }
 
 /**
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 c5d2ca1402..bab47b98f4 100644
--- a/modules/consensus/dag-poa/dap_chain_cs_dag_poa.c
+++ b/modules/consensus/dag-poa/dap_chain_cs_dag_poa.c
@@ -406,6 +406,10 @@ static void s_poa_round_check_callback_round_clean(dap_global_db_context_t *a_gl
         for (size_t i = 0; i < a_values_count; i++) {
             if (!strcmp(DAG_ROUND_CURRENT_KEY, a_values[i].key))
                 continue;
+            if (a_values[i].value_len <= sizeof(dap_chain_cs_dag_event_round_item_t) + sizeof(dap_chain_cs_dag_event_t)) {
+                log_it(L_WARNING, "Too small round item in DAG PoA rounds GDB group");
+                continue;
+            }
             dap_chain_cs_dag_event_round_item_t *l_event_round_item = (dap_chain_cs_dag_event_round_item_t *)a_values[i].value;
             uint64_t l_time_diff = dap_nanotime_now() - l_event_round_item->round_info.ts_update;
             uint64_t l_timeuot = dap_nanotime_from_sec(l_poa_pvt->confirmations_timeout + l_poa_pvt->wait_sync_before_complete + 10);
diff --git a/modules/consensus/none/dap_chain_cs_none.c b/modules/consensus/none/dap_chain_cs_none.c
index b726246407..5c39ea8389 100644
--- a/modules/consensus/none/dap_chain_cs_none.c
+++ b/modules/consensus/none/dap_chain_cs_none.c
@@ -297,9 +297,9 @@ static void s_ledger_load_callback(dap_global_db_context_t *a_global_db_context,
     for(size_t i = 0; i < a_values_count; i++) {
         s_chain_callback_atom_add(l_chain, a_values[i].value, a_values[i].value_len);
     }
-    l_gdb_pvt->is_load_mode = false;
 
     pthread_mutex_lock(&l_gdb_pvt->load_mutex);
+    l_gdb_pvt->is_load_mode = false;
     pthread_cond_broadcast(&l_gdb_pvt->load_cond);
     pthread_mutex_unlock(&l_gdb_pvt->load_mutex);
 }
@@ -315,11 +315,11 @@ int dap_chain_gdb_ledger_load(char *a_gdb_group, dap_chain_t *a_chain)
 {
     dap_chain_gdb_t * l_gdb = DAP_CHAIN_GDB(a_chain);
     dap_chain_gdb_private_t * l_gdb_pvt = PVT(l_gdb);
-    size_t l_data_size = 0;
     //  Read the entire database into an array of size bytes
     pthread_mutex_lock(&l_gdb_pvt->load_mutex);
     dap_global_db_get_all(a_gdb_group, 0, s_ledger_load_callback, a_chain);
-    pthread_cond_wait(&l_gdb_pvt->load_cond, &l_gdb_pvt->load_mutex);
+    while (l_gdb_pvt->is_load_mode)
+        pthread_cond_wait(&l_gdb_pvt->load_cond, &l_gdb_pvt->load_mutex);
     pthread_mutex_unlock(&l_gdb_pvt->load_mutex);
 
     return 0;
diff --git a/modules/net/dap_chain_node_cli_cmd.c b/modules/net/dap_chain_node_cli_cmd.c
index 8b744f7334..783ef4d99e 100644
--- a/modules/net/dap_chain_node_cli_cmd.c
+++ b/modules/net/dap_chain_node_cli_cmd.c
@@ -2595,6 +2595,10 @@ void s_com_mempool_list_print_for_chain(dap_chain_net_t * a_net, dap_chain_t * a
             char l_data_hash_str[DAP_CHAIN_HASH_FAST_STR_SIZE] = {[0]='\0'};
             dap_hash_fast(l_datum->data,l_datum->header.data_size,&l_data_hash);
             dap_hash_fast_to_str(&l_data_hash,l_data_hash_str,DAP_CHAIN_HASH_FAST_STR_SIZE);
+            if (strcmp(l_data_hash_str, l_objs[i].key))
+                dap_string_append_printf(a_str_tmp,
+                                         "WARNING: key field in DB %s do not match datums hash %s",
+                                         l_objs[i].key, l_data_hash_str);
             const char *l_type = NULL;
             DAP_DATUM_TYPE_STR(l_datum->header.type_id, l_type)
             const char *l_token_ticker = NULL;
@@ -2602,18 +2606,14 @@ void s_com_mempool_list_print_for_chain(dap_chain_net_t * a_net, dap_chain_t * a
                 dap_chain_tx_in_t *obj_in = (dap_chain_tx_in_t *)dap_chain_datum_tx_item_get((dap_chain_datum_tx_t*)l_datum->data, NULL, TX_ITEM_TYPE_IN, NULL);
                 l_token_ticker = dap_chain_ledger_tx_get_token_ticker_by_hash(a_net->pub.ledger, &obj_in->header.tx_prev_hash);
             }
-            if (l_token_ticker) {
-                dap_string_append_printf(a_str_tmp,
-                                         "hash %s : type_id=%s  data_size=%u data_hash=%s ticker=%s ts_create=%s", // \n included in timestamp
-                                         l_objs[i].key, l_type,
-                                         l_datum->header.data_size, l_data_hash_str,
-                                         l_token_ticker, dap_ctime_r(&l_ts_create, buf));
-            } else {
-                dap_string_append_printf(a_str_tmp,
-                                         "hash %s : type_id=%s  data_size=%u data_hash=%s ts_create=%s", // \n included in timestamp
-                                         l_objs[i].key, l_type,
-                                         l_datum->header.data_size, l_data_hash_str, dap_ctime_r(&l_ts_create, buf));
-            }
+            dap_string_append_printf(a_str_tmp,
+                                     "type_id=%s%s%s data_size=%u ts_create=%s", // \n included in timestamp
+                                     l_type,
+                                     l_datum->header.type_id == DAP_CHAIN_DATUM_TX ? " ticker=" : "",
+                                     l_token_ticker ? l_token_ticker :
+                                                      (l_datum->header.type_id == DAP_CHAIN_DATUM_TX ) ? "" : "UNKNOWN",
+                                     l_datum->header.data_size,
+                                     dap_ctime_r(&l_ts_create, buf));
             dap_chain_datum_dump(a_str_tmp, l_datum, a_hash_out_type);
         }
         dap_global_db_objs_delete(l_objs, l_objs_size);
diff --git a/modules/net/dap_chain_node_client.c b/modules/net/dap_chain_node_client.c
index faa6313fbd..18e9cd3da2 100644
--- a/modules/net/dap_chain_node_client.c
+++ b/modules/net/dap_chain_node_client.c
@@ -97,6 +97,12 @@ static bool dap_chain_node_client_connect_internal(dap_chain_node_client_t *a_no
 bool s_stream_ch_chain_debug_more = false;
 uint32_t s_timer_update_states = 600;
 
+#ifdef DAP_OS_WINDOWS
+#define dap_cond_signal(x) SetEvent(x)
+#else
+#define dap_cond_signal(x) pthread_cond_broadcast(&x)
+#endif
+
 /**
  * @brief dap_chain_node_client_init
  * @return
@@ -178,13 +184,10 @@ static void s_stage_status_error_callback(dap_client_t *a_client, void *a_arg)
     bool l_is_last_attempt = a_arg ? true : false;
     if (l_is_last_attempt) {
         pthread_mutex_lock(&l_node_client->wait_mutex);
-        pthread_mutex_unlock(&l_node_client->wait_mutex);
         l_node_client->state = NODE_CLIENT_STATE_DISCONNECTED;
-#ifndef _WIN32
-        pthread_cond_broadcast(&l_node_client->wait_cond);
-#else
-        SetEvent( l_node_client->wait_cond );
-#endif
+        dap_cond_signal(l_node_client->wait_cond);
+        pthread_mutex_unlock(&l_node_client->wait_mutex);
+
         l_node_client->esocket_uuid = 0;
 
         dap_chain_net_sync_unlock(l_node_client->net, l_node_client);
@@ -347,9 +350,13 @@ static void s_stage_connected_callback(dap_client_t *a_client, void *a_arg)
         }
         if(l_node_client->callbacks.connected)
             l_node_client->callbacks.connected(l_node_client, l_node_client->callbacks_arg);
-        if(s_stream_ch_chain_debug_more)
-            log_it(L_DEBUG, "Wakeup all who waits");
+
+        pthread_mutex_lock(&l_node_client->wait_mutex);
         l_node_client->state = NODE_CLIENT_STATE_ESTABLISHED;
+        if (s_stream_ch_chain_debug_more)
+            log_it(L_DEBUG, "Wakeup all who waits");
+        dap_cond_signal(l_node_client->wait_cond);
+        pthread_mutex_unlock(&l_node_client->wait_mutex);
 
         dap_stream_t * l_stream  = dap_client_get_stream(a_client);
         if (l_stream) {
@@ -369,13 +376,6 @@ static void s_stage_connected_callback(dap_client_t *a_client, void *a_arg)
                 }
             }
         }
-        pthread_mutex_lock(&l_node_client->wait_mutex);
-        pthread_mutex_unlock(&l_node_client->wait_mutex);
-#ifndef _WIN32
-        pthread_cond_broadcast(&l_node_client->wait_cond);
-#else
-        SetEvent( l_node_client->wait_cond );
-#endif
     }
 }
 
@@ -397,12 +397,10 @@ static void s_ch_chain_callback_notify_packet_in2(dap_stream_ch_chain_net_t* a_c
         if(a_pkt_net_data_size == sizeof(dap_chain_node_addr_t)) {
             l_node_client->cur_node_addr = *(dap_chain_node_addr_t*)a_pkt_net->data;
         }
+        pthread_mutex_lock(&l_node_client->wait_mutex);
         l_node_client->state = NODE_CLIENT_STATE_NODE_ADDR_LEASED;
-#ifndef _WIN32
-        pthread_cond_broadcast(&l_node_client->wait_cond);
-#else
-        SetEvent( l_node_client->wait_cond );
-#endif
+        dap_cond_signal(l_node_client->wait_cond);
+        pthread_mutex_lock(&l_node_client->wait_mutex);
         break;
     }
     // get remote node address
@@ -411,14 +409,13 @@ static void s_ch_chain_callback_notify_packet_in2(dap_stream_ch_chain_net_t* a_c
         if(a_pkt_net_data_size == sizeof(dap_chain_node_addr_t)) {
             l_node_client->remote_node_addr = *(dap_chain_node_addr_t*)a_pkt_net->data;
         }
+        pthread_mutex_lock(&l_node_client->wait_mutex);
         l_node_client->state = NODE_CLIENT_STATE_GET_NODE_ADDR;
-#ifndef _WIN32
-        pthread_cond_broadcast(&l_node_client->wait_cond);
-#else
-            SetEvent( l_node_client->wait_cond );
-#endif
-            break;
+        dap_cond_signal(l_node_client->wait_cond);
+        pthread_mutex_lock(&l_node_client->wait_mutex);
+        break;
     }
+    default:;
     }
 }
 
@@ -535,13 +532,11 @@ static void s_ch_chain_callback_notify_packet_in(dap_stream_ch_chain_t* a_ch_cha
         default: break;
     }
     if (l_finished) {
-#ifndef _WIN32
-        pthread_cond_broadcast(&l_node_client->wait_cond);
-#else
-        SetEvent( l_node_client->wait_cond);
-#endif
-        bool l_have_waiting = dap_chain_net_sync_unlock(l_net, l_node_client);
+        pthread_mutex_lock(&l_node_client->wait_mutex);
         l_node_client->state = NODE_CLIENT_STATE_SYNCED;
+        dap_cond_signal(l_node_client->wait_cond);
+        pthread_mutex_lock(&l_node_client->wait_mutex);
+        bool l_have_waiting = dap_chain_net_sync_unlock(l_net, l_node_client);
         if (dap_chain_net_get_target_state(l_net) == NET_STATE_ONLINE) {
             dap_timerfd_reset(l_node_client->sync_timer);
             dap_chain_net_set_state(l_net, NET_STATE_ONLINE);
@@ -719,12 +714,10 @@ static void s_ch_chain_callback_notify_packet_R(dap_stream_ch_chain_net_srv_t* a
                 break;
             }
             s_save_stat_to_database(l_request, l_node_client);
+            pthread_mutex_lock(&l_node_client->wait_mutex);
             l_node_client->state = NODE_CLIENT_STATE_CHECKED;
-#ifndef _WIN32
-            pthread_cond_broadcast(&l_node_client->wait_cond);
-#else
-            SetEvent( l_node_client->wait_cond );
-#endif
+            dap_cond_signal(l_node_client->wait_cond);
+            pthread_mutex_lock(&l_node_client->wait_mutex);
             break;
         }
     case DAP_STREAM_CH_CHAIN_NET_SRV_PKT_TYPE_SIGN_RESPONSE:
@@ -988,15 +981,9 @@ int dap_chain_node_client_wait(dap_chain_node_client_t *a_client, int a_waited_s
     struct timespec l_cond_timeout;
     clock_gettime( CLOCK_MONOTONIC, &l_cond_timeout);
     l_cond_timeout.tv_sec += a_timeout_ms/1000;
-#else
-    pthread_mutex_unlock( &a_client->wait_mutex );
-#endif
-
     // signal waiting
-
-
-#ifndef DAP_OS_WINDOWS
-    do {
+    dap_chain_node_client_state_t l_clinet_state = a_client->state;
+    while (a_client->state == l_clinet_state) {
         int l_ret_wait = pthread_cond_timedwait(&a_client->wait_cond, &a_client->wait_mutex, &l_cond_timeout);
         if(l_ret_wait == 0 && (
                 a_client->state == a_waited_state ||
@@ -1015,8 +1002,10 @@ int dap_chain_node_client_wait(dap_chain_node_client_t *a_client, int a_waited_s
             strerror_r(l_ret_wait,l_errbuf,sizeof (l_errbuf));
             log_it(L_ERROR, "Pthread condition timed wait returned \"%s\"(code %d)", l_errbuf, l_ret_wait);
         }
-    } while(1);
+    }
+    pthread_mutex_unlock(&a_client->wait_mutex);
 #else
+    pthread_mutex_unlock( &a_client->wait_mutex );
     DWORD wait = WaitForSingleObject( a_client->wait_cond, (uint32_t)a_timeout_ms);
     if ( wait == WAIT_OBJECT_0 && (
              a_client->state == a_waited_state ||
@@ -1028,10 +1017,6 @@ int dap_chain_node_client_wait(dap_chain_node_client_t *a_client, int a_waited_s
         return -1;
     }
 #endif
-
-#ifndef DAP_OS_WINDOWS
-    pthread_mutex_unlock(&a_client->wait_mutex);
-#endif
     return ret;
 }
 
diff --git a/modules/service/vpn/dap_chain_net_srv_vpn.c b/modules/service/vpn/dap_chain_net_srv_vpn.c
index 0007cce71c..1c27d9d61c 100644
--- a/modules/service/vpn/dap_chain_net_srv_vpn.c
+++ b/modules/service/vpn/dap_chain_net_srv_vpn.c
@@ -163,9 +163,10 @@ typedef struct tun_socket_msg{
 
 dap_chain_net_srv_vpn_tun_socket_t ** s_tun_sockets = NULL;
 dap_events_socket_t ** s_tun_sockets_queue_msg = NULL;
-pthread_mutex_t * s_tun_sockets_mutex_started = NULL;
-pthread_cond_t * s_tun_sockets_cond_started = NULL;
 
+pthread_mutex_t s_tun_sockets_mutex_started;
+pthread_cond_t s_tun_sockets_cond_started;
+uint32_t s_tun_sockets_started = 0;
 
 uint32_t s_tun_sockets_count = 0;
 bool s_debug_more = false;
@@ -174,8 +175,6 @@ static usage_client_t * s_clients = NULL;
 static dap_chain_net_srv_ch_vpn_t * s_ch_vpn_addrs  = NULL;
 static pthread_rwlock_t s_clients_rwlock = PTHREAD_RWLOCK_INITIALIZER;
 
-static pthread_mutex_t s_sf_socks_mutex;
-static pthread_cond_t s_sf_socks_cond;
 static vpn_local_network_t *s_raw_server = NULL;
 static pthread_rwlock_t s_raw_server_rwlock = PTHREAD_RWLOCK_INITIALIZER;
 
@@ -651,8 +650,6 @@ static int s_vpn_tun_create(dap_config_t * g_config)
     log_it(L_NOTICE,"%s: trying to initialize multiqueue for %u workers", __PRETTY_FUNCTION__, s_tun_sockets_count);
     s_tun_sockets = DAP_NEW_Z_SIZE(dap_chain_net_srv_vpn_tun_socket_t*,s_tun_sockets_count*sizeof(dap_chain_net_srv_vpn_tun_socket_t*));
     s_tun_sockets_queue_msg =  DAP_NEW_Z_SIZE(dap_events_socket_t*,s_tun_sockets_count*sizeof(dap_events_socket_t*));
-    s_tun_sockets_mutex_started = DAP_NEW_Z_SIZE(pthread_mutex_t,s_tun_sockets_count*sizeof(pthread_mutex_t));
-    s_tun_sockets_cond_started = DAP_NEW_Z_SIZE(pthread_cond_t,s_tun_sockets_count*sizeof(pthread_cond_t));
 
     int l_err = 0;
 #if defined (DAP_OS_DARWIN)
@@ -732,6 +729,8 @@ static int s_vpn_tun_create(dap_config_t * g_config)
     s_raw_server->tun_device_name = strndup(l_utunname, l_utunname_len);
     log_it(L_NOTICE, "Utun device name \"%s\"", s_raw_server->tun_device_name);
 #endif
+
+    pthread_mutex_lock(&s_tun_sockets_mutex_started);
     for( uint8_t i =0; i< s_tun_sockets_count; i++){
         dap_worker_t * l_worker = dap_events_worker_get(i);
         assert( l_worker );
@@ -753,19 +752,15 @@ static int s_vpn_tun_create(dap_config_t * g_config)
 #elif !defined (DAP_OS_DARWIN)
 #error "Undefined tun interface attach for your platform"
 #endif
-        pthread_mutex_init(&s_tun_sockets_mutex_started[i],NULL);
-        pthread_cond_init(&s_tun_sockets_cond_started[i],NULL);
-        pthread_mutex_lock(&s_tun_sockets_mutex_started[i]);
         s_tun_event_stream_create(l_worker, l_tun_fd);
     }
     if (l_err)
         goto lb_err;
 
     // Waiting for all the tun sockets
-    for( uint8_t i =0; i< s_tun_sockets_count; i++){
-        pthread_cond_wait(&s_tun_sockets_cond_started[i], &s_tun_sockets_mutex_started[i]);
-        pthread_mutex_unlock(&s_tun_sockets_mutex_started[i]);
-    }
+    while (s_tun_sockets_started != s_tun_sockets_count)
+        pthread_cond_wait(&s_tun_sockets_cond_started, &s_tun_sockets_mutex_started);
+    pthread_mutex_unlock(&s_tun_sockets_mutex_started);
 
     // Fill inter tun qyueue
     // Create for all previous created sockets the input queue
@@ -811,8 +806,8 @@ static int s_vpn_tun_init()
     s_raw_server=DAP_NEW_Z(vpn_local_network_t);
     pthread_rwlock_init(&s_raw_server->rwlock, NULL);
     pthread_mutex_init(&s_raw_server->pkt_out_mutex,NULL);
-    pthread_mutex_init(&s_sf_socks_mutex, NULL);
-    pthread_cond_init(&s_sf_socks_cond, NULL);
+    pthread_mutex_init(&s_tun_sockets_mutex_started, NULL);
+    pthread_cond_init(&s_tun_sockets_cond_started, NULL);
 
     return 0;
 }
@@ -875,8 +870,8 @@ int dap_chain_net_srv_vpn_init(dap_config_t * g_config) {
  */
 void dap_chain_net_srv_vpn_deinit(void)
 {
-    pthread_mutex_destroy(&s_sf_socks_mutex);
-    pthread_cond_destroy(&s_sf_socks_cond);
+    pthread_mutex_destroy(&s_tun_sockets_mutex_started);
+    pthread_cond_destroy(&s_tun_sockets_cond_started);
     DAP_DELETE(s_srv_vpn_addr);
     DAP_DELETE(s_srv_vpn_mask);
     if(s_raw_server)
@@ -1763,9 +1758,10 @@ static void s_es_tun_new(dap_events_socket_t * a_es, void * arg)
         s_tun_attach_queue( a_es->fd );
 #endif
         // Signal thats its ready
-        pthread_mutex_lock(&s_tun_sockets_mutex_started[l_worker_id]);
-        pthread_mutex_unlock(&s_tun_sockets_mutex_started[l_worker_id]);
-        pthread_cond_broadcast(&s_tun_sockets_cond_started[l_worker_id]);
+        pthread_mutex_lock(&s_tun_sockets_mutex_started);
+        s_tun_sockets_started++;
+        pthread_cond_broadcast(&s_tun_sockets_cond_started);
+        pthread_mutex_unlock(&s_tun_sockets_mutex_started);
 
         log_it(L_NOTICE,"New TUN event socket initialized for worker %u" , l_tun_socket->worker_id);
 
diff --git a/modules/type/dag/dap_chain_cs_dag.c b/modules/type/dag/dap_chain_cs_dag.c
index 68279cda20..8f5c80179e 100644
--- a/modules/type/dag/dap_chain_cs_dag.c
+++ b/modules/type/dag/dap_chain_cs_dag.c
@@ -281,8 +281,8 @@ int dap_chain_cs_dag_new(dap_chain_t * a_chain, dap_config_t * a_chain_cfg)
     DAP_DELETE(l_round_new_str);
     dap_global_db_add_sync_extra_group(l_net->pub.name, l_dag->gdb_group_events_round_new, s_history_callback_round_notify, l_dag);
     l_dag->broadcast_disable = false;
-    byte_t *l_current_round = dap_global_db_get_sync(l_gdb_group, DAG_ROUND_CURRENT_KEY, NULL, NULL, NULL);
-    l_dag->round_current = l_current_round? *(uint64_t *)l_current_round : 0;
+    byte_t *l_current_round = dap_global_db_get_sync(l_dag->gdb_group_events_round_new, DAG_ROUND_CURRENT_KEY, NULL, NULL, NULL);
+    l_dag->round_current = l_current_round ? *(uint64_t *)l_current_round : 0;
     DAP_DELETE(l_current_round);
     PVT(l_dag)->mempool_timer = dap_interval_timer_create(5000, (dap_timer_callback_t)dap_chain_node_mempool_process_all, a_chain);
     if (l_dag->is_single_line)
diff --git a/modules/type/dag/dap_chain_cs_dag_event.c b/modules/type/dag/dap_chain_cs_dag_event.c
index 92b4a616fd..0d055be5c3 100644
--- a/modules/type/dag/dap_chain_cs_dag_event.c
+++ b/modules/type/dag/dap_chain_cs_dag_event.c
@@ -289,7 +289,7 @@ bool dap_chain_cs_dag_event_gdb_set(dap_chain_cs_dag_t *a_dag, char *a_event_has
 
     l_round_item->round_info.ts_update = dap_nanotime_now();
 
-    size_t l_round_item_size = dap_chain_cs_dag_event_round_item_get_size(a_round_item);
+    size_t l_round_item_size = dap_chain_cs_dag_event_round_item_get_size(l_round_item);
     bool ret = dap_global_db_set(a_dag->gdb_group_events_round_new, a_event_hash_str, l_round_item,
                                  l_round_item_size,true, NULL, NULL) == 0;
     DAP_DELETE(l_round_item);
-- 
GitLab