From ea79b7831035ff5dc65c331bb0c2ece34bf282be Mon Sep 17 00:00:00 2001
From: "ruslan.laishev" <ruslan.laishev@demlabs.net>
Date: Mon, 12 Dec 2022 10:30:35 +0000
Subject: [PATCH] Bugfix 6901 to dev by sys

---
 modules/channel/chain/dap_stream_ch_chain.c | 32 +++++++++++++++++++++
 modules/wallet/dap_chain_wallet.c           | 20 +++++++------
 2 files changed, 43 insertions(+), 9 deletions(-)

diff --git a/modules/channel/chain/dap_stream_ch_chain.c b/modules/channel/chain/dap_stream_ch_chain.c
index d9245ae59a..0acaf7b5f3 100644
--- a/modules/channel/chain/dap_stream_ch_chain.c
+++ b/modules/channel/chain/dap_stream_ch_chain.c
@@ -133,6 +133,18 @@ static char **s_list_white_groups = NULL;
 static uint16_t s_size_ban_groups = 0;
 static uint16_t s_size_white_groups = 0;
 
+
+#ifdef  DAP_SYS_DEBUG
+
+enum    {MEMSTAT$K_STM_CH_CHAIN, MEMSTAT$K_NR};
+static  dap_memstat_rec_t   s_memstat [MEMSTAT$K_NR] = {
+    {.fac_len = sizeof(LOG_TAG) - 1, .fac_name = {LOG_TAG}, .alloc_sz = sizeof(dap_stream_ch_chain_t)},
+};
+
+#endif
+
+
+
 /**
  * @brief dap_stream_ch_chain_init
  * @return
@@ -146,6 +158,12 @@ int dap_stream_ch_chain_init()
     s_update_pack_size = dap_config_get_item_int16_default(g_config,"stream_ch_chain","update_pack_size",100);
     s_list_ban_groups = dap_config_get_array_str(g_config, "stream_ch_chain", "ban_list_sync_groups", &s_size_ban_groups);
     s_list_white_groups = dap_config_get_array_str(g_config, "stream_ch_chain", "white_list_sync_groups", &s_size_white_groups);
+
+#ifdef  DAP_SYS_DEBUG
+    for (int i = 0; i < MEMSTAT$K_NR; i++)
+        dap_memstat_reg(&s_memstat[i]);
+#endif
+
     return 0;
 }
 
@@ -164,11 +182,20 @@ void dap_stream_ch_chain_deinit()
  */
 void s_stream_ch_new(dap_stream_ch_t* a_ch, void* a_arg)
 {
+
     UNUSED(a_arg);
     a_ch->internal = DAP_NEW_Z(dap_stream_ch_chain_t);
     dap_stream_ch_chain_t * l_ch_chain = DAP_STREAM_CH_CHAIN(a_ch);
     l_ch_chain->_inheritor = a_ch;
     a_ch->stream->esocket->callbacks.write_finished_callback = s_stream_ch_io_complete;
+
+#ifdef  DAP_SYS_DEBUG
+    atomic_fetch_add(&s_memstat[MEMSTAT$K_STM_CH_CHAIN].alloc_nr, 1);
+#endif
+
+    debug_if(s_debug_more, L_DEBUG, "[stm_ch_chain:%p] --- created chain:%p", a_ch, l_ch_chain);
+
+
 }
 
 /**
@@ -187,6 +214,11 @@ static void s_stream_ch_delete_in_proc(dap_worker_t *a_worker, void *a_arg)
     s_ch_chain_go_idle(l_ch_chain);
     s_free_log_list_gdb(l_ch_chain);
     DAP_DELETE(l_ch_chain);
+
+#ifdef  DAP_SYS_DEBUG
+        atomic_fetch_add(&s_memstat[MEMSTAT$K_STM_CH_CHAIN].free_nr, 1);
+#endif
+
 }
 
 /**
diff --git a/modules/wallet/dap_chain_wallet.c b/modules/wallet/dap_chain_wallet.c
index fc90865135..f273743b8f 100644
--- a/modules/wallet/dap_chain_wallet.c
+++ b/modules/wallet/dap_chain_wallet.c
@@ -165,7 +165,7 @@ char *c_wallets_path;
 }
 
 /*
- *  DESCRIPTIOB: Lookup and retrieve password for a given wallet. A buffer for a_pass should be enough
+ *  DESCRIPTION: Lookup and retrieve password for a given wallet. A buffer for a_pass should be enough
  *      to accept password string up to DAP_WALLET$SZ_PASS octets
  *
  *  INPUTS:
@@ -454,14 +454,16 @@ dap_chain_wallet_internal_t * l_wallet_internal;
     if(!a_wallet)
         return;
 
-    // TODO Make clean struct dap_chain_wallet_internal_t (certs, addr)
     if ( (l_wallet_internal = a_wallet->_internal) )
     {
         if ( l_wallet_internal->certs )
+        {
             for(size_t i = 0; i < l_wallet_internal->certs_count; i++)
                 dap_cert_delete( l_wallet_internal->certs[i]);
 
-        DAP_DELETE(l_wallet_internal->certs);
+            DAP_DELETE(l_wallet_internal->certs);
+        }
+
         DAP_DELETE(l_wallet_internal);
     }
 
@@ -550,7 +552,7 @@ dap_enc_key_t* dap_chain_wallet_get_key( dap_chain_wallet_t * a_wallet,uint32_t
 }
 
 /*
- *  DESCRIPTION: Save memory wallet's context into the protected by given password.
+ *  DESCRIPTION: Save memory wallet's context into ta file  protected by given password.
  *
  *  INPUTS:
  *      a_wallet:   Wallet's context structure
@@ -620,22 +622,22 @@ enum {
         dap_fileclose(l_fh);
         return -l_err;
     }
-                                                                            /* CRC for file header part */
+                                                                        /* CRC for file header part */
     l_csum = crc32c(l_csum, l_iov[WALLET$K_IOV_HEADER].iov_base, l_iov[WALLET$K_IOV_HEADER].iov_len);
-                                                                            /* CRC for file body part */
+                                                                        /* CRC for file body part */
     l_csum = crc32c(l_csum, l_iov[WALLET$K_IOV_BODY].iov_base, l_iov[WALLET$K_IOV_BODY].iov_len);
 
     /* Write certs */
     for ( size_t i = 0; i < l_wallet_internal->certs_count ; i++)
     {
-                                                                            /* Get ceritificate body */
+                                                                        /* Get ceritificate body */
         if ( !(l_cert_raw  = (char *) dap_cert_mem_save(l_wallet_internal->certs[i], &l_len)) )
         {
             log_it(L_WARNING, "Certificate #%zu cannot be obtained, go next ...", i);
             continue;
         }
 
-        l_csum = crc32c (l_csum, l_cert_raw, l_len);                          /* CRC for every certificate */
+        l_csum = crc32c (l_csum, l_cert_raw, l_len);                        /* CRC for every certificate */
 
         if ( l_enc_key )
         {
@@ -695,7 +697,7 @@ enum {
     {
     dap_chain_wallet_t  *l_wallet;
 
-    if ( l_wallet = dap_chain_wallet_open_file (l_wallet_internal->file_name, a_pass) )
+    if ( (l_wallet = dap_chain_wallet_open_file (l_wallet_internal->file_name, a_pass)) )
         dap_chain_wallet_close(l_wallet);
 
     }
-- 
GitLab