From bc34a0c43d82c98927af69f0f3446ddc21656f53 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Al=D0=B5x=D0=B0nder=20Lysik=D0=BEv?=
 <alexander.lysikov@demlabs.net>
Date: Tue, 9 Jul 2019 16:32:00 +0500
Subject: [PATCH] added CHAIN_TYPE_XXXX

---
 dap_chain.c      | 32 ++++++++++++++++++++++++++++++++
 dap_chain.h      | 11 +++++++++++
 dap_chain_cell.c |  7 +++++--
 dap_chain_cell.h |  1 +
 4 files changed, 49 insertions(+), 2 deletions(-)

diff --git a/dap_chain.c b/dap_chain.c
index 2d00c75149..c48ef7ac9f 100755
--- a/dap_chain.c
+++ b/dap_chain.c
@@ -169,6 +169,8 @@ void dap_chain_delete(dap_chain_t * a_chain)
     }else
        log_it(L_WARNING,"Trying to remove non-existent 0x%16llX:0x%16llX chain",a_chain->id.uint64,
               a_chain->net_id.uint64);
+    a_chain->datum_types_count = 0;
+    DAP_DELETE (a_chain->datum_types);
     pthread_rwlock_unlock(&s_chain_items_rwlock);
 }
 
@@ -243,6 +245,15 @@ dap_chain_t * dap_chain_load_from_cfg(dap_ledger_t* a_ledger, const char * a_cha
                 return NULL;
             }
 
+            // Read chain datum types
+            char** l_datum_types = NULL;
+            uint16_t l_datum_types_count = 0;
+            if((l_datum_types = dap_config_get_array_str(l_cfg, "chain", "datum_types", &l_datum_types_count)) == NULL) {
+                log_it(L_ERROR, "Can't read chain datum types ", l_chain_id_str);
+                dap_config_close(l_cfg);
+                return NULL;
+            }
+
             l_chain =  dap_chain_create(a_ledger,a_chain_net_name,l_chain_name, a_chain_net_id,l_chain_id);
             if ( dap_chain_cs_create(l_chain, l_cfg) == 0 ) {
                 log_it (L_NOTICE,"Consensus initialized for chain id 0x%016llX",
@@ -252,6 +263,7 @@ dap_chain_t * dap_chain_load_from_cfg(dap_ledger_t* a_ledger, const char * a_cha
                     DAP_CHAIN_PVT ( l_chain)->file_storage_dir = strdup (
                                 dap_config_get_item_str( l_cfg , "files","storage_dir" ) ) ;
                     if ( dap_chain_load_all( l_chain ) != 0 ){
+                        dap_chain_save_all( l_chain );
                         log_it (L_NOTICE, "Loaded chain files");
                     }else {
                         dap_chain_save_all( l_chain );
@@ -268,6 +280,26 @@ dap_chain_t * dap_chain_load_from_cfg(dap_ledger_t* a_ledger, const char * a_cha
                 dap_chain_delete(l_chain);
                 l_chain = NULL;
             }
+            // add datum types
+            if(l_chain && l_datum_types_count > 0) {
+                l_chain->datum_types = DAP_NEW_SIZE(dap_chain_type_t, l_datum_types_count * sizeof(dap_chain_type_t));
+                uint16_t l_count_recognized = 0;
+                for(uint16_t i = 0; i < l_datum_types_count; i++) {
+                    if(!dap_strcmp(l_datum_types[i], "token")) {
+                        l_chain->datum_types[l_count_recognized] = CHAIN_TYPE_TOKEN;
+                        l_count_recognized++;
+                    }
+                    else if(!dap_strcmp(l_datum_types[i], "emission")) {
+                        l_chain->datum_types[l_count_recognized] = CHAIN_TYPE_EMISSION;
+                        l_count_recognized++;
+                    }
+                    else if(!dap_strcmp(l_datum_types[i], "transaction")) {
+                        l_chain->datum_types[l_count_recognized] = CHAIN_TYPE_TX;
+                        l_count_recognized++;
+                    }
+                }
+                l_chain->datum_types_count = l_count_recognized;
+            }
 
             dap_config_close(l_cfg);
             return l_chain;
diff --git a/dap_chain.h b/dap_chain.h
index 91a0d90b2f..3e6db4748c 100755
--- a/dap_chain.h
+++ b/dap_chain.h
@@ -75,6 +75,14 @@ typedef size_t (*dap_chain_datum_callback_datum_pool_proc_add_t)(dap_chain_t * ,
 typedef size_t (*dap_chain_datum_callback_datum_pool_proc_add_with_group_t)(dap_chain_t * , dap_chain_datum_t **, size_t, const char *);
 
 
+typedef  enum dap_chain_type
+{
+    CHAIN_TYPE_FIRST,
+    CHAIN_TYPE_TOKEN,
+    CHAIN_TYPE_EMISSION,
+    CHAIN_TYPE_TX,
+    CHAIN_TYPE_LAST
+} dap_chain_type_t;
 
 typedef struct dap_chain{
     dap_chain_id_t id;
@@ -87,6 +95,9 @@ typedef struct dap_chain{
     // Nested cells (hashtab by cell_id
     dap_chain_cell_t * cells;
 
+    uint16_t datum_types_count;
+    dap_chain_type_t *datum_types;
+
     // To hold it in double-linked lists
     struct dap_chain * next;
     struct dap_chain * prev;
diff --git a/dap_chain_cell.c b/dap_chain_cell.c
index 2193232b85..752b3b6468 100755
--- a/dap_chain_cell.c
+++ b/dap_chain_cell.c
@@ -53,7 +53,7 @@ typedef struct dap_chain_cell_file_header
 } DAP_ALIGN_PACKED dap_chain_cell_file_header_t;
 
 
-static const char* s_cells_path = NULL;
+//static const char* s_cells_path = NULL;
 
 /**
  * @brief dap_chain_cell_init
@@ -61,7 +61,7 @@ static const char* s_cells_path = NULL;
  */
 int dap_chain_cell_init(void)
 {
-    s_cells_path = dap_config_get_item_str(g_config,"resources","cells_storage");
+    //s_cells_path = dap_config_get_item_str(g_config,"resources","cells_storage");
     return  0;
 }
 
@@ -112,14 +112,17 @@ int dap_chain_cell_load(dap_chain_t * a_chain, const char * a_cell_file_path)
                 return 0;
             } else {
                 log_it (L_ERROR,"Wrong signature in file \"%s\", possible file corrupt",l_cell->file_storage_path);
+                DAP_DELETE(l_cell);
                 return -3;
             }
         } else {
             log_it (L_ERROR,"Can't read dap_chain file header \"%s\"",l_cell->file_storage_path);
+            DAP_DELETE(l_cell);
             return -2;
         }
     }else {
         log_it (L_WARNING,"Can't read dap_chain file \"%s\"",l_cell->file_storage_path);
+        DAP_DELETE(l_cell);
         return -1;
     }
 
diff --git a/dap_chain_cell.h b/dap_chain_cell.h
index eb425343f6..6914d90a95 100755
--- a/dap_chain_cell.h
+++ b/dap_chain_cell.h
@@ -72,6 +72,7 @@ typedef struct dap_chain_cell_decl{
 
 
 int dap_chain_cell_init(void);
+dap_chain_cell_t * dap_chain_cell_create();
 int dap_chain_cell_load(dap_chain_t * a_chain, const char * a_cell_file_path);
 int dap_chain_cell_file_update( dap_chain_cell_t * a_cell);
 int dap_chain_cell_file_append( dap_chain_cell_t * a_cell,const void* a_atom, size_t a_atom_size);
-- 
GitLab