From 70a5531d773f4ba0a3990f97b293c5839662ef7a Mon Sep 17 00:00:00 2001
From: "Dmitriy A. Gerasimov" <dmitriy.gerasimov@demlabs.net>
Date: Tue, 5 May 2020 20:24:58 +0700
Subject: [PATCH] [*] Renames and fixes

---
 CMakeLists.txt                           |  2 +-
 modules/chain/dap_chain_ledger.c         |  6 +++---
 modules/chain/include/dap_chain_ledger.h |  6 +++---
 modules/net/dap_chain_net.c              | 25 +++++++++++++++++++++---
 modules/net/dap_chain_node.c             | 15 +++++++-------
 modules/net/dap_chain_node_cli_cmd.c     |  6 +++---
 modules/net/include/dap_chain_net.h      |  2 +-
 modules/type/dag/dap_chain_cs_dag.c      | 11 ++++++++---
 8 files changed, 49 insertions(+), 24 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 4ffafbe6f9..4eb71372f3 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.1-1")
+set(CELLFRAME_SDK_NATIVE_VERSION "2.1-2")
 include(cmake/OS_Detection.cmake)
 add_definitions ("-DCELLFRAME_SDK_VERSION=\"${CELLFRAME_SDK_NATIVE_VERSION}\"")
 
diff --git a/modules/chain/dap_chain_ledger.c b/modules/chain/dap_chain_ledger.c
index a020a853cb..0a9db533b8 100644
--- a/modules/chain/dap_chain_ledger.c
+++ b/modules/chain/dap_chain_ledger.c
@@ -205,7 +205,7 @@ static int compare_datum_items(const void * l_a, const void * l_b)
  * @param a_token
  * @return
  */
-int dap_chain_ledger_token_check(dap_ledger_t * a_ledger,  dap_chain_datum_token_t *a_token)
+int dap_chain_ledger_token_decl_add_check(dap_ledger_t * a_ledger,  dap_chain_datum_token_t *a_token)
 {
     if ( !a_ledger){
         log_it(L_ERROR, "NULL ledger, can't add datum with token declaration!");
@@ -307,7 +307,7 @@ dap_ledger_t* dap_chain_ledger_create(uint16_t a_check_flags)
     return l_ledger; //dap_chain_ledger_load(l_ledger, "kelvin-testnet", "plasma");
 }
 
-int dap_chain_ledger_token_emission_check(dap_ledger_t *a_ledger, const dap_chain_datum_token_emission_t *a_token_emission
+int dap_chain_ledger_token_emission_add_check(dap_ledger_t *a_ledger, const dap_chain_datum_token_emission_t *a_token_emission
                                         , size_t a_token_emission_size)
 {
     int ret = 0;
@@ -853,7 +853,7 @@ int dap_chain_ledger_tx_cache_check(dap_ledger_t *a_ledger, dap_chain_datum_tx_t
  * @param a_tx
  * @return
  */
-int dap_chain_ledger_tx_check(dap_ledger_t *a_ledger, dap_chain_datum_tx_t *a_tx)
+int dap_chain_ledger_tx_add_check(dap_ledger_t *a_ledger, dap_chain_datum_tx_t *a_tx)
 {
     if(!a_tx)
         return -2;
diff --git a/modules/chain/include/dap_chain_ledger.h b/modules/chain/include/dap_chain_ledger.h
index 58281f9073..4dbcedb0f5 100644
--- a/modules/chain/include/dap_chain_ledger.h
+++ b/modules/chain/include/dap_chain_ledger.h
@@ -80,7 +80,7 @@ static inline dap_chain_hash_fast_t* dap_chain_node_datum_tx_calc_hash(dap_chain
 int dap_chain_ledger_tx_add(dap_ledger_t *a_ledger, dap_chain_datum_tx_t *a_tx);
 
 
-int dap_chain_ledger_tx_check(dap_ledger_t *a_ledger, dap_chain_datum_tx_t *a_tx);
+int dap_chain_ledger_tx_add_check(dap_ledger_t *a_ledger, dap_chain_datum_tx_t *a_tx);
 
 /**
  * Add new token datum
@@ -88,7 +88,7 @@ int dap_chain_ledger_tx_check(dap_ledger_t *a_ledger, dap_chain_datum_tx_t *a_tx
  */
 
 int dap_chain_ledger_token_add(dap_ledger_t * a_ledger,dap_chain_datum_token_t *a_token, size_t a_token_size);
-int dap_chain_ledger_token_check(dap_ledger_t * a_ledger,dap_chain_datum_token_t *a_token);
+int dap_chain_ledger_token_decl_add_check(dap_ledger_t * a_ledger,dap_chain_datum_token_t *a_token);
 
 /**
  * Add token emission datum
@@ -97,7 +97,7 @@ int dap_chain_ledger_token_emission_add(dap_ledger_t *a_ledger,
         const dap_chain_datum_token_emission_t *a_token_emission, size_t a_token_emission_size);
 
 // Check if it addable
-int dap_chain_ledger_token_emission_check(dap_ledger_t *a_ledger,
+int dap_chain_ledger_token_emission_add_check(dap_ledger_t *a_ledger,
         const dap_chain_datum_token_emission_t *a_token_emission, size_t a_token_emission_size);
 
 dap_chain_datum_token_emission_t * dap_chain_ledger_token_emission_find(dap_ledger_t *a_ledger,
diff --git a/modules/net/dap_chain_net.c b/modules/net/dap_chain_net.c
index f56276cff1..817a8666ef 100644
--- a/modules/net/dap_chain_net.c
+++ b/modules/net/dap_chain_net.c
@@ -1966,7 +1966,7 @@ void dap_chain_net_proc_mempool (dap_chain_net_t * a_net)
             size_t l_objs_size_tmp = (l_objs_size > 15) ? min(l_objs_size, 10) : l_objs_size;
             for(size_t i = 0; i < l_objs_size; i++) {
                 dap_chain_datum_t * l_datum = (dap_chain_datum_t*) l_objs[i].value;
-                int l_verify_datum= dap_chain_net_verify_datum( a_net, l_datum) ;
+                int l_verify_datum= dap_chain_net_verify_datum_for_add( a_net, l_datum) ;
                 if (l_verify_datum != 0){
                     log_it(L_WARNING, "Datum doesn't pass verifications (code %d), delete such datum from pool",
                                              l_verify_datum);
@@ -2073,7 +2073,26 @@ dap_list_t * dap_chain_net_get_add_gdb_group(dap_chain_net_t * a_net, dap_chain_
     return l_list_groups;
 }
 
-int dap_chain_net_verify_datum(dap_chain_net_t *a_net, dap_chain_datum_t * a_datum )
+/**
+ * @brief dap_chain_net_verify_datum_for_add
+ * @param a_net
+ * @param a_datum
+ * @return
+ */
+int dap_chain_net_verify_datum_for_add(dap_chain_net_t *a_net, dap_chain_datum_t * a_datum )
 {
-
+    if( ! a_datum)
+        return -10;
+    if( ! a_net )
+        return -11;
+
+    switch ( a_datum->header.type_id) {
+        case DAP_CHAIN_DATUM_TX: return dap_chain_ledger_tx_add_check( a_net->pub.ledger,
+                                                                   (dap_chain_datum_tx_t*) a_datum->data );
+        case DAP_CHAIN_DATUM_TOKEN_DECL: return dap_chain_ledger_token_decl_add_check( a_net->pub.ledger,
+                                                                   (dap_chain_datum_token_t*) a_datum->data );
+        case DAP_CHAIN_DATUM_TOKEN_EMISSION : return dap_chain_ledger_token_emission_add_check( a_net->pub.ledger,
+                                                                   (dap_chain_datum_token_emission_t*) a_datum->data, a_datum->header.data_size );
+        default: return 0;
+    }
 }
diff --git a/modules/net/dap_chain_node.c b/modules/net/dap_chain_node.c
index 1f95a00da8..bcdfbdd0f1 100644
--- a/modules/net/dap_chain_node.c
+++ b/modules/net/dap_chain_node.c
@@ -270,22 +270,23 @@ void dap_chain_node_mempool_periodic(void *a_param)
     UNUSED(a_param);
     size_t l_net_count;
     bool l_mempool_auto;
+    bool l_mempool_auto_default = false;
     dap_chain_net_t **l_net_list = dap_chain_net_list(&l_net_count);
     for (int i = 0; i < l_net_count; i++) {
-        l_mempool_auto = dap_config_get_item_bool_default(g_config, "mempool", "auto_proc", false);
-        if (!l_mempool_auto) {
-            dap_chain_node_role_t l_role = dap_chain_net_get_role(l_net_list[i]);
-            switch (l_role.enums) {
+        dap_chain_node_role_t l_role = dap_chain_net_get_role(l_net_list[i]);
+
+        switch (l_role.enums) {
             case NODE_ROLE_ROOT:
             case NODE_ROLE_MASTER:
             case NODE_ROLE_ROOT_MASTER:
             case NODE_ROLE_CELL_MASTER:
-                l_mempool_auto = true;
+                l_mempool_auto_default = true;
                 break;
             default:
-                break;
-            }
+                l_mempool_auto_default = false;
         }
+
+        l_mempool_auto = dap_config_get_item_bool_default(g_config, "mempool", "auto_proc", l_mempool_auto_default);
         if (l_mempool_auto) {
             dap_chain_t *l_chain;
             DL_FOREACH(l_net_list[i]->pub.chains, l_chain) {
diff --git a/modules/net/dap_chain_node_cli_cmd.c b/modules/net/dap_chain_node_cli_cmd.c
index 52d7365202..14c7a183cb 100644
--- a/modules/net/dap_chain_node_cli_cmd.c
+++ b/modules/net/dap_chain_node_cli_cmd.c
@@ -1,4 +1,4 @@
-/*
+/*
  * Authors:
  * Dmitriy A. Gerasimov <gerasimov.dmitriy@demlabs.net>
  * Alexander Lysikov <alexander.lysikov@demlabs.net>
@@ -2124,7 +2124,7 @@ int com_mempool_proc(int argc, char ** argv, void *arg_func, char ** a_str_reply
         size_t l_datum_size=0;
         dap_chain_datum_t * l_datum = (dap_chain_datum_t*) dap_chain_global_db_gr_get(l_datum_hash_str,
                                                                                        &l_datum_size, l_gdb_group_mempool);
-        size_t l_datum_size2= dap_chain_datum_size( l_datum);
+        size_t l_datum_size2= l_datum? dap_chain_datum_size( l_datum): 0;
         if (l_datum_size != l_datum_size2 ){
             ret = -8;
             dap_chain_node_cli_set_reply_text(a_str_reply, "Error! Corrupted datum %s, size by datum headers is %zd when in mempool is only %zd bytes",
@@ -2136,7 +2136,7 @@ int com_mempool_proc(int argc, char ** argv, void *arg_func, char ** a_str_reply
                 dap_string_append_printf(l_str_tmp, "0x%s: type_id=%s ts_create=%s data_size=%u\n",
                         l_datum_hash_str, c_datum_type_str[l_datum->header.type_id],
                         ctime_r(&l_ts_create, buf), l_datum->header.data_size);
-                int l_verify_datum= dap_chain_net_verify_datum( l_net, l_datum) ;
+                int l_verify_datum= dap_chain_net_verify_datum_for_add( l_net, l_datum) ;
                 if (l_verify_datum != 0){
                     dap_string_append_printf(l_str_tmp, "Error! Datum doesn't pass verifications (code %d) examine node log files",
                                              l_verify_datum);
diff --git a/modules/net/include/dap_chain_net.h b/modules/net/include/dap_chain_net.h
index 2247a0107e..463a6e777c 100644
--- a/modules/net/include/dap_chain_net.h
+++ b/modules/net/include/dap_chain_net.h
@@ -152,4 +152,4 @@ char * dap_chain_net_get_gdb_group_mempool_by_chain_type(dap_chain_net_t * l_net
 dap_chain_net_t **dap_chain_net_list(size_t *a_size);
 dap_list_t * dap_chain_net_get_add_gdb_group(dap_chain_net_t * a_net, dap_chain_node_addr_t a_node_addr);
 
-int dap_chain_net_verify_datum(dap_chain_net_t *a_net, dap_chain_datum_t * a_datum );
+int dap_chain_net_verify_datum_for_add(dap_chain_net_t *a_net, dap_chain_datum_t * a_datum );
diff --git a/modules/type/dag/dap_chain_cs_dag.c b/modules/type/dag/dap_chain_cs_dag.c
index 8a4290424d..873be6125e 100644
--- a/modules/type/dag/dap_chain_cs_dag.c
+++ b/modules/type/dag/dap_chain_cs_dag.c
@@ -208,7 +208,8 @@ int dap_chain_cs_dag_new(dap_chain_t * a_chain, dap_config_t * a_chain_cfg)
         }
     }
 
-    l_dag->is_static_genesis_event = dap_config_get_item_bool_default(a_chain_cfg,"dag","is_static_genesis_event",false);
+    l_dag->is_static_genesis_event = (l_static_genesis_event_hash_str != NULL) && dap_config_get_item_bool_default(a_chain_cfg,"dag","is_static_genesis_event",false);
+
     l_dag->is_single_line = dap_config_get_item_bool_default(a_chain_cfg,"dag","is_single_line",false);
     l_dag->is_celled = dap_config_get_item_bool_default(a_chain_cfg,"dag","is_celled",false);
     l_dag->is_add_directy = dap_config_get_item_bool_default(a_chain_cfg,"dag","is_add_directly",false);
@@ -395,7 +396,7 @@ static size_t s_chain_callback_datums_pool_proc(dap_chain_t * a_chain, dap_chain
 
         // Verify for correctness
         dap_chain_net_t * l_net = dap_chain_net_by_id( a_chain->net_id);
-        int l_verify_datum= dap_chain_net_verify_datum( l_net, l_datum) ;
+        int l_verify_datum= dap_chain_net_verify_datum_for_add( l_net, l_datum) ;
         if (l_verify_datum != 0){
             log_it(L_WARNING, "Datum doesn't pass verifications (code %d)",
                                      l_verify_datum);
@@ -567,13 +568,16 @@ static int s_chain_callback_atom_verify(dap_chain_t * a_chain, dap_chain_atom_pt
             char * l_genesis_event_hash_str = dap_chain_hash_fast_to_str_new(&l_dag->static_genesis_event_hash);
 
             log_it(L_WARNING, "Wrong genesis block %s (staticly predefined %s)",l_event_hash_str, l_genesis_event_hash_str);
+            DAP_DELETE(l_event_hash_str);
+            DAP_DELETE(l_genesis_event_hash_str);
             return -22;
         }
+        return 0;
     }
 
     int ret = l_dag->callback_cs_verify ( l_dag, l_event );
     if (ret == 0 ){
-        if ( PVT(l_dag)->events )
+        if ( PVT(l_dag)->events ){
             for (size_t i = 0; i< l_event->header.hash_count; i++) {
                 dap_chain_hash_fast_t * l_hash =  ((dap_chain_hash_fast_t *) l_event->hashes_n_datum_n_signs) + i;
                 dap_chain_cs_dag_event_item_t * l_event_search = NULL;
@@ -584,6 +588,7 @@ static int s_chain_callback_atom_verify(dap_chain_t * a_chain, dap_chain_atom_pt
                 }
 
             }
+        }
 
         return 0;
     }else {
-- 
GitLab