From a1bafced6e69d3aae7d188c3c5c08f539d2ca09b Mon Sep 17 00:00:00 2001
From: "Dmitriy A. Gerasimov" <dmitriy.gerasimov@demlabs.net>
Date: Mon, 1 Feb 2021 19:51:40 +0700
Subject: [PATCH] [*] Some logs are hided under debug_more in dap_client_http
 [!] Fixed crash in ledger treshold [+] Added treshold processing in sync
 protocol

---
 CMakeLists.txt                              |  2 +-
 dap-sdk/net/client/dap_client_http.c        | 34 +++++++++++++++------
 modules/chain/dap_chain_ledger.c            | 29 ++++++++++++++++--
 modules/channel/chain/dap_stream_ch_chain.c | 12 ++++++--
 4 files changed, 61 insertions(+), 16 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 994a4685ae..5923696c54 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -2,7 +2,7 @@ project(cellframe-sdk C)
 cmake_minimum_required(VERSION 2.8)
 
 set(CMAKE_C_STANDARD 11)
-set(CELLFRAME_SDK_NATIVE_VERSION "2.8-16")
+set(CELLFRAME_SDK_NATIVE_VERSION "2.8-17")
 add_definitions ("-DCELLFRAME_SDK_VERSION=\"${CELLFRAME_SDK_NATIVE_VERSION}\"")
 set(DAPSDK_MODULES "")
 
diff --git a/dap-sdk/net/client/dap_client_http.c b/dap-sdk/net/client/dap_client_http.c
index 7a6c2eb564..3bd0cce1e5 100644
--- a/dap-sdk/net/client/dap_client_http.c
+++ b/dap-sdk/net/client/dap_client_http.c
@@ -88,19 +88,27 @@ static bool s_timer_timeout_check(void * a_arg);
 static bool s_timer_timeout_after_connected_check(void * a_arg);
 
 
-uint64_t s_client_timeout_ms=10000;
-time_t s_client_timeout_read_after_connect_ms=5000;
-uint32_t s_max_attempts = 5;
-
+static bool s_debug_more=false;
+static uint64_t s_client_timeout_ms=10000;
+static time_t s_client_timeout_read_after_connect_ms=5000;
+static uint32_t s_max_attempts = 5;
 
+/**
+ * @brief dap_client_http_init
+ * @return
+ */
 int dap_client_http_init()
 {
+    s_debug_more = dap_config_get_item_bool_default(g_config,"dap_client","debug_more",false);
     s_max_attempts = dap_config_get_item_uint32_default(g_config,"dap_client","max_tries",5);
     s_client_timeout_ms = dap_config_get_item_uint32_default(g_config,"dap_client","timeout",10)*1000;
     s_client_timeout_read_after_connect_ms = (time_t) dap_config_get_item_uint32_default(g_config,"dap_client","timeout_read_after_connect",5);
     return 0;
 }
 
+/**
+ * @brief dap_client_http_deinit
+ */
 void dap_client_http_deinit()
 {
 
@@ -144,7 +152,8 @@ static bool s_timer_timeout_after_connected_check(void * a_arg)
 
     if(dap_events_socket_check_unsafe(l_worker, l_es) ){
         if (!dap_uint128_check_equal(l_es->uuid,l_es_handler->uuid)){
-            log_it(L_DEBUG,"Timer esocket wrong argument, ignore this timeout...");
+            if(s_debug_more)
+                log_it(L_DEBUG,"Timer esocket wrong argument, ignore this timeout...");
             DAP_DEL_Z(l_es_handler)
             return false;
         }
@@ -162,7 +171,8 @@ static bool s_timer_timeout_after_connected_check(void * a_arg)
             dap_events_socket_remove_and_delete_unsafe(l_es, true);
         }
     }else
-        log_it(L_DEBUG,"Esocket %p is finished, close check timer", l_es);
+        if(s_debug_more)
+            log_it(L_DEBUG,"Esocket %p is finished, close check timer", l_es);
 
     DAP_DEL_Z(l_es_handler)
     return false;
@@ -188,7 +198,8 @@ static bool s_timer_timeout_check(void * a_arg)
 
     if(dap_events_socket_check_unsafe(l_worker, l_es) ){
         if (!dap_uint128_check_equal(l_es->uuid,l_es_handler->uuid)){
-            log_it(L_DEBUG,"Timer esocket wrong argument, ignore this timeout...");
+            if(s_debug_more)
+                log_it(L_DEBUG,"Timer esocket wrong argument, ignore this timeout...");
             DAP_DEL_Z(l_es_handler)
             return false;
         }
@@ -205,9 +216,11 @@ static bool s_timer_timeout_check(void * a_arg)
                    l_es->remote_addr_str ? l_es->remote_addr_str : "", l_es->socket, l_es->type);
             dap_events_socket_remove_and_delete_unsafe(l_es, true);
         }else
-            log_it(L_DEBUG,"Socket %d is connected, close check timer", l_es->socket);
+            if(s_debug_more)
+                log_it(L_DEBUG,"Socket %d is connected, close check timer", l_es->socket);
     }else
-        log_it(L_DEBUG,"Esocket %p is finished, close check timer", l_es);
+        if(s_debug_more)
+            log_it(L_DEBUG,"Esocket %p is finished, close check timer", l_es);
 
     DAP_DEL_Z(l_es_handler)
     return false;
@@ -391,7 +404,8 @@ static void s_es_delete(dap_events_socket_t * a_es, void * a_arg)
 static void s_client_http_delete(dap_client_http_pvt_t * a_http_pvt)
 {
     // call from dap_events_socket_delete(ev_socket, true);
-    log_it(L_DEBUG, "HTTP client delete");
+    if(s_debug_more)
+        log_it(L_DEBUG, "HTTP client delete");
 
     if(!a_http_pvt) {
         log_it(L_ERROR, "s_http_write: l_client_http_internal is NULL!");
diff --git a/modules/chain/dap_chain_ledger.c b/modules/chain/dap_chain_ledger.c
index 6535c3227a..3fbc350705 100644
--- a/modules/chain/dap_chain_ledger.c
+++ b/modules/chain/dap_chain_ledger.c
@@ -851,16 +851,22 @@ static void s_treshold_emissions_proc(dap_ledger_t * a_ledger)
     do {
         l_success = false;
         dap_chain_ledger_token_emission_item_t *l_emission_item, *l_emission_tmp;
-        pthread_rwlock_wrlock(&PVT(a_ledger)->treshold_emissions_rwlock);
+        pthread_rwlock_rdlock(&PVT(a_ledger)->treshold_emissions_rwlock);
         HASH_ITER(hh, PVT(a_ledger)->treshold_emissions, l_emission_item, l_emission_tmp) {
+            pthread_rwlock_unlock(&PVT(a_ledger)->treshold_emissions_rwlock);
             int l_res = dap_chain_ledger_token_emission_add(a_ledger, l_emission_item->datum_token_emission,
                                                             l_emission_item->datum_token_emission_size);
             if (!l_res) {
+                pthread_rwlock_wrlock(&PVT(a_ledger)->treshold_emissions_rwlock);
                 HASH_DEL(PVT(a_ledger)->treshold_emissions, l_emission_item);
+                pthread_rwlock_unlock(&PVT(a_ledger)->treshold_emissions_rwlock);
+
                 DAP_DELETE(l_emission_item->datum_token_emission);
                 DAP_DELETE(l_emission_item);
                 l_success = true;
             }
+
+            pthread_rwlock_rdlock(&PVT(a_ledger)->treshold_emissions_rwlock);
         }
         pthread_rwlock_unlock(&PVT(a_ledger)->treshold_emissions_rwlock);
     } while (l_success); 
@@ -877,15 +883,20 @@ static void s_treshold_txs_proc( dap_ledger_t *a_ledger)
     do {
         l_success = false;
         dap_chain_ledger_tx_item_t *l_tx_item, *l_tx_tmp;
-        pthread_rwlock_wrlock(&l_ledger_pvt->treshold_txs_rwlock);
+        pthread_rwlock_rdlock(&l_ledger_pvt->treshold_txs_rwlock);
         HASH_ITER(hh, l_ledger_pvt->treshold_txs, l_tx_item, l_tx_tmp) {
+            pthread_rwlock_unlock(& l_ledger_pvt->treshold_txs_rwlock );
             int l_res = dap_chain_ledger_tx_add(a_ledger, l_tx_item->tx, true);
             if (l_res == 1) {
+                pthread_rwlock_wrlock(&l_ledger_pvt->treshold_txs_rwlock);
                 HASH_DEL(l_ledger_pvt->treshold_txs, l_tx_item);
+                pthread_rwlock_unlock(& l_ledger_pvt->treshold_txs_rwlock );
+
                 DAP_DELETE(l_tx_item->tx);
                 DAP_DELETE(l_tx_item);
                 l_success = true;
             }
+            pthread_rwlock_rdlock(&l_ledger_pvt->treshold_txs_rwlock);
         }
         pthread_rwlock_unlock(& l_ledger_pvt->treshold_txs_rwlock );
     } while (l_success);
@@ -2718,6 +2729,8 @@ const dap_chain_datum_tx_t* dap_chain_ledger_tx_find_by_pkey(dap_ledger_t *a_led
     pthread_rwlock_rdlock(&l_ledger_priv->ledger_rwlock);
     HASH_ITER(hh, l_ledger_priv->ledger_items , l_iter_current, l_item_tmp)
     {
+        pthread_rwlock_unlock(&l_ledger_priv->ledger_rwlock);
+
         dap_chain_datum_tx_t *l_tx_tmp = l_iter_current->tx;
         dap_chain_hash_fast_t *l_tx_hash_tmp = &l_iter_current->tx_hash_fast;
         // start searching from the next hash after a_tx_first_hash
@@ -2740,6 +2753,7 @@ const dap_chain_datum_tx_t* dap_chain_ledger_tx_find_by_pkey(dap_ledger_t *a_led
                 break;
             }
         }
+        pthread_rwlock_rdlock(&l_ledger_priv->ledger_rwlock);
     }
     pthread_rwlock_unlock(&l_ledger_priv->ledger_rwlock);
     return l_cur_tx;
@@ -2765,6 +2779,7 @@ dap_chain_datum_tx_t* dap_chain_ledger_tx_cache_find_out_cond(dap_ledger_t *a_le
     pthread_rwlock_rdlock(&l_ledger_priv->ledger_rwlock);
     HASH_ITER(hh, l_ledger_priv->ledger_items, l_iter_current, l_item_tmp)
     {
+        pthread_rwlock_unlock(&l_ledger_priv->ledger_rwlock);
         dap_chain_datum_tx_t *l_tx_tmp = l_iter_current->tx;
         dap_chain_hash_fast_t *l_tx_hash_tmp = &l_iter_current->tx_hash_fast;
         // start searching from the next hash after a_tx_first_hash
@@ -2784,6 +2799,7 @@ dap_chain_datum_tx_t* dap_chain_ledger_tx_cache_find_out_cond(dap_ledger_t *a_le
             }
             break;
         }
+        pthread_rwlock_rdlock(&l_ledger_priv->ledger_rwlock);
     }
     pthread_rwlock_unlock(&l_ledger_priv->ledger_rwlock);
     if (a_out_cond) {
@@ -2836,6 +2852,15 @@ uint64_t dap_chain_ledger_tx_cache_get_out_cond_value(dap_ledger_t *a_ledger, da
     return l_ret_value;
 }
 
+/**
+ * @brief dap_chain_ledger_get_list_tx_outs_with_val
+ * @param a_ledger
+ * @param a_token_ticker
+ * @param a_addr_from
+ * @param a_value_need
+ * @param a_value_transfer
+ * @return
+ */
 dap_list_t *dap_chain_ledger_get_list_tx_outs_with_val(dap_ledger_t *a_ledger, const char *a_token_ticker, const dap_chain_addr_t *a_addr_from,
                                                        uint64_t a_value_need, uint64_t *a_value_transfer)
 {
diff --git a/modules/channel/chain/dap_stream_ch_chain.c b/modules/channel/chain/dap_stream_ch_chain.c
index 2209157536..939474bba4 100644
--- a/modules/channel/chain/dap_stream_ch_chain.c
+++ b/modules/channel/chain/dap_stream_ch_chain.c
@@ -447,7 +447,7 @@ static bool s_sync_in_chains_callback(dap_proc_thread_t *a_thread, void *a_arg)
                 size_t l_atom_size =0;
                 if ( l_chain->callback_atom_find_by_hash(l_atom_iter, &l_atom_hash, &l_atom_size) == NULL ) {
                     dap_chain_atom_verify_res_t l_atom_add_res = l_chain->callback_atom_add(l_chain, l_atom_copy, l_atom_copy_size);
-                    if (l_atom_add_res == ATOM_ACCEPT && dap_chain_has_file_store(l_chain)) {
+                    if ( l_atom_add_res != ATOM_REJECT && dap_chain_has_file_store(l_chain)) {
                         if (s_debug_more){
                             char l_atom_hash_str[72]={[0]='\0'};
                             dap_chain_hash_fast_to_str(&l_atom_hash,l_atom_hash_str,sizeof (l_atom_hash_str)-1 );
@@ -497,6 +497,13 @@ static bool s_sync_in_chains_callback(dap_proc_thread_t *a_thread, void *a_arg)
                             log_it(L_ERROR, "Can't get cell for cell_id 0x%x for save event to file", l_pkt_item->pkt_hdr.cell_id);
 
                         }
+                    }else if(l_atom_add_res == ATOM_PASS){
+                        if (s_debug_more){
+                            char l_atom_hash_str[72]={[0]='\0'};
+                            dap_chain_hash_fast_to_str(&l_atom_hash,l_atom_hash_str,sizeof (l_atom_hash_str)-1 );
+                            log_it(L_WARNING,"Not accepted atom (code ATOM_PASS) with hash %s for %s:%s and moved into the treshold",  l_atom_hash_str, l_chain->net_name, l_chain->name);
+                        }
+                        DAP_DELETE(l_atom_copy);
                     }else{
                         if (s_debug_more){
                             char l_atom_hash_str[72]={[0]='\0'};
@@ -504,8 +511,7 @@ static bool s_sync_in_chains_callback(dap_proc_thread_t *a_thread, void *a_arg)
                             log_it(L_WARNING,"Not accepted atom (code %d) with hash %s for %s:%s", l_atom_add_res, l_atom_hash_str, l_chain->net_name, l_chain->name);
                         }
                     }
-                    if(l_atom_add_res == ATOM_PASS)
-                        DAP_DELETE(l_atom_copy);
+
                 } else {
                     if (s_debug_more){
                         char l_atom_hash_str[72]={[0]='\0'};
-- 
GitLab