From b11d850f9f82582603d7d8bb8dfe2b8121ff7850 Mon Sep 17 00:00:00 2001
From: "Dmitriy A. Gerasimov" <dmitriy.gerasimov@demlabs.net>
Date: Wed, 28 Oct 2020 18:53:46 +0700
Subject: [PATCH] [+] Added some more checks

---
 modules/channel/chain/dap_stream_ch_chain.c | 140 +++++++++++---------
 1 file changed, 74 insertions(+), 66 deletions(-)

diff --git a/modules/channel/chain/dap_stream_ch_chain.c b/modules/channel/chain/dap_stream_ch_chain.c
index 0ed9f4c479..386db368a3 100644
--- a/modules/channel/chain/dap_stream_ch_chain.c
+++ b/modules/channel/chain/dap_stream_ch_chain.c
@@ -235,81 +235,89 @@ bool s_chain_pkt_callback(dap_proc_thread_t *a_thread, void *a_arg)
     dap_list_t *l_pkt_copy_list = l_ch_chain->pkt_copy_list;
     if (l_pkt_copy_list) {
         l_ch_chain->pkt_copy_list = l_ch_chain->pkt_copy_list->next;
-        dap_chain_pkt_copy_t *l_pkt_copy = (dap_chain_pkt_copy_t *)l_pkt_copy_list->data;
-        dap_chain_t *l_chain = dap_chain_find_by_id(l_pkt_copy->pkt_hdr.net_id, l_pkt_copy->pkt_hdr.chain_id);
-        if (!l_chain) {
-            log_it(L_WARNING, "No chain found for DAP_STREAM_CH_CHAIN_PKT_TYPE_CHAIN");
-            return true;
-        }
-        dap_chain_atom_ptr_t l_atom_copy = (dap_chain_atom_ptr_t)l_pkt_copy->pkt_data;
-        uint64_t l_atom_copy_size = l_pkt_copy->pkt_data_size;
-        if ( l_atom_copy_size && l_pkt_copy && l_atom_copy ){
-            dap_hash_fast(l_atom_copy, l_atom_copy_size, &l_atom_hash);
-            dap_chain_atom_iter_t *l_atom_iter = l_chain->callback_atom_iter_create(l_chain);
-            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)) {
-                    // append to file
-                    dap_chain_cell_t *l_cell = dap_chain_cell_create_fill(l_chain, l_pkt_copy->pkt_hdr.cell_id);
-                    int l_res;
-                    if (l_cell) {
-                        // add one atom only
-                        l_res = dap_chain_cell_file_append(l_cell, l_atom_copy, l_atom_copy_size);
-                        // rewrite all file
-                        //l_res = dap_chain_cell_file_update(l_cell);
-                        if(l_res < 0) {
-                            log_it(L_ERROR, "Can't save event 0x%x to the file '%s'", l_atom_hash,
-                                    l_cell ? l_cell->file_storage_path : "[null]");
-                        } else {
-                            dap_db_set_last_hash_remote(l_ch_chain->request.node_addr.uint64, l_chain, &l_atom_hash);
-                        }
-                        // add all atoms from treshold
-                        if (l_chain->callback_atom_add_from_treshold){
-                            dap_chain_atom_ptr_t l_atom_treshold;
-                            do{
-                                size_t l_atom_treshold_size;
-                                // add into ledger
-                                log_it(L_DEBUG, "Try to add atom from treshold");
-                                l_atom_treshold = l_chain->callback_atom_add_from_treshold(l_chain, &l_atom_treshold_size);
-                                // add into file
-                                if(l_atom_treshold) {
-                                    l_res = dap_chain_cell_file_append(l_cell, l_atom_treshold, l_atom_treshold_size);
-                                    log_it(L_DEBUG, "Added atom from treshold");
-                                    if(l_res < 0) {
-                                        log_it(L_ERROR, "Can't save event 0x%x from treshold to the file '%s'",
-                                                l_atom_treshold, l_cell ? l_cell->file_storage_path : "[null]");
+        if ( l_ch_chain->pkt_copy_list ) {
+            dap_chain_pkt_copy_t *l_pkt_copy = (dap_chain_pkt_copy_t *)l_pkt_copy_list->data;
+            if (l_pkt_copy){
+                dap_chain_t *l_chain = dap_chain_find_by_id(l_pkt_copy->pkt_hdr.net_id, l_pkt_copy->pkt_hdr.chain_id);
+                if (!l_chain) {
+                    log_it(L_WARNING, "No chain found for DAP_STREAM_CH_CHAIN_PKT_TYPE_CHAIN");
+                    return true;
+                }
+                dap_chain_atom_ptr_t l_atom_copy = (dap_chain_atom_ptr_t)l_pkt_copy->pkt_data;
+                uint64_t l_atom_copy_size = l_pkt_copy->pkt_data_size;
+                if ( l_atom_copy_size && l_pkt_copy && l_atom_copy ){
+                    dap_hash_fast(l_atom_copy, l_atom_copy_size, &l_atom_hash);
+                    dap_chain_atom_iter_t *l_atom_iter = l_chain->callback_atom_iter_create(l_chain);
+                    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)) {
+                            // append to file
+                            dap_chain_cell_t *l_cell = dap_chain_cell_create_fill(l_chain, l_pkt_copy->pkt_hdr.cell_id);
+                            int l_res;
+                            if (l_cell) {
+                                // add one atom only
+                                l_res = dap_chain_cell_file_append(l_cell, l_atom_copy, l_atom_copy_size);
+                                // rewrite all file
+                                //l_res = dap_chain_cell_file_update(l_cell);
+                                if(l_res < 0) {
+                                    log_it(L_ERROR, "Can't save event 0x%x to the file '%s'", l_atom_hash,
+                                            l_cell ? l_cell->file_storage_path : "[null]");
+                                } else {
+                                    dap_db_set_last_hash_remote(l_ch_chain->request.node_addr.uint64, l_chain, &l_atom_hash);
+                                }
+                                // add all atoms from treshold
+                                if (l_chain->callback_atom_add_from_treshold){
+                                    dap_chain_atom_ptr_t l_atom_treshold;
+                                    do{
+                                        size_t l_atom_treshold_size;
+                                        // add into ledger
+                                        log_it(L_DEBUG, "Try to add atom from treshold");
+                                        l_atom_treshold = l_chain->callback_atom_add_from_treshold(l_chain, &l_atom_treshold_size);
+                                        // add into file
+                                        if(l_atom_treshold) {
+                                            l_res = dap_chain_cell_file_append(l_cell, l_atom_treshold, l_atom_treshold_size);
+                                            log_it(L_DEBUG, "Added atom from treshold");
+                                            if(l_res < 0) {
+                                                log_it(L_ERROR, "Can't save event 0x%x from treshold to the file '%s'",
+                                                        l_atom_treshold, l_cell ? l_cell->file_storage_path : "[null]");
+                                            }
+                                        }
                                     }
+                                    while(l_atom_treshold);
                                 }
-                            }
-                            while(l_atom_treshold);
-                        }
 
-                        // delete cell and close file
-                        dap_chain_cell_delete(l_cell);
-                    }
-                    else{
-                        log_it(L_ERROR, "Can't get cell for cell_id 0x%x for save event to file", l_pkt_copy->pkt_hdr.cell_id);
+                                // delete cell and close file
+                                dap_chain_cell_delete(l_cell);
+                            }
+                            else{
+                                log_it(L_ERROR, "Can't get cell for cell_id 0x%x for save event to file", l_pkt_copy->pkt_hdr.cell_id);
 
+                            }
+                        }
+                        if(l_atom_add_res == ATOM_PASS)
+                            DAP_DELETE(l_atom_copy);
+                    } else {
+                        dap_db_set_last_hash_remote(l_ch_chain->request.node_addr.uint64, l_chain, &l_atom_hash);
+                        DAP_DELETE(l_atom_copy);
                     }
+                    l_chain->callback_atom_iter_delete(l_atom_iter);
+                }else{
+                    if (!l_pkt_copy)
+                        log_it(L_WARNING, "packet copy is NULL");
+                    if (!l_pkt_copy_list)
+                        log_it(L_WARNING, "packet copy list is NULL");
+                    if (l_atom_copy_size)
+                        log_it(L_WARNING, "Atom copy size is zero");
                 }
-                if(l_atom_add_res == ATOM_PASS)
-                    DAP_DELETE(l_atom_copy);
-            } else {
-                dap_db_set_last_hash_remote(l_ch_chain->request.node_addr.uint64, l_chain, &l_atom_hash);
-                DAP_DELETE(l_atom_copy);
+            }else{
+                log_it(L_WARNING, "pkt copy is NULL");
             }
-            l_chain->callback_atom_iter_delete(l_atom_iter);
+            if (l_pkt_copy)
+                DAP_DELETE(l_pkt_copy);
         }else{
-            if (!l_pkt_copy)
-                log_it(L_WARNING, "packet copy is NULL");
-            if (!l_pkt_copy_list)
-                log_it(L_WARNING, "packet copy list is NULL");
-            if (l_atom_copy_size)
-                log_it(L_WARNING, "Atom copy size is zero");
+         //   log_it(L_WARNING, "Next pkt copy list is NULL");
         }
-        if (l_pkt_copy)
-            DAP_DELETE(l_pkt_copy);
         if (l_pkt_copy_list)
             DAP_DELETE(l_pkt_copy_list);
     }else
-- 
GitLab