From 3d126902d6452cb04a8b32c80781cc98823d21a8 Mon Sep 17 00:00:00 2001
From: "roman.padenkov" <roman.padenkov@demlabs.net>
Date: Fri, 30 Aug 2024 17:56:36 +0700
Subject: [PATCH] ...

---
 modules/chain/include/dap_chain.h         |  2 ++
 modules/net/dap_chain_ledger.c            | 10 +++++-----
 modules/net/dap_chain_net.c               |  2 +-
 modules/net/dap_chain_node_cli_cmd_tx.c   | 17 +++++++----------
 modules/net/include/dap_chain_ledger.h    |  3 ++-
 modules/type/blocks/dap_chain_cs_blocks.c | 15 ++++++++++++---
 6 files changed, 29 insertions(+), 20 deletions(-)

diff --git a/modules/chain/include/dap_chain.h b/modules/chain/include/dap_chain.h
index 33c5c4dc21..361e30a7d0 100644
--- a/modules/chain/include/dap_chain.h
+++ b/modules/chain/include/dap_chain.h
@@ -56,6 +56,8 @@ typedef struct dap_chain_datum_iter {
     size_t cur_size;
     dap_chain_hash_fast_t *cur_hash;
     dap_chain_hash_fast_t *cur_atom_hash;
+    uint32_t action;
+    dap_chain_net_srv_uid_t uid;
     int ret_code;
     char *token_ticker;
     void *cur_item;
diff --git a/modules/net/dap_chain_ledger.c b/modules/net/dap_chain_ledger.c
index 6076bb6a90..e49ec737f3 100644
--- a/modules/net/dap_chain_ledger.c
+++ b/modules/net/dap_chain_ledger.c
@@ -4293,7 +4293,7 @@ static int s_tx_cache_check(dap_ledger_t *a_ledger,
     }
 
     if (a_main_ticker && !l_err_num)
-        dap_strncpy(a_main_ticker, l_main_ticker, DAP_CHAIN_TICKER_SIZE_MAX);
+        dap_strncpy(a_main_ticker, l_main_ticker, DAP_CHAIN_TICKER_SIZE_MAX);     
 
     HASH_ITER(hh, l_values_from_prev_tx, l_value_cur, l_tmp) {
         HASH_DEL(l_values_from_prev_tx, l_value_cur);
@@ -4410,7 +4410,7 @@ int dap_ledger_tx_add(dap_ledger_t *a_ledger, dap_chain_datum_tx_t *a_tx, dap_ha
 
     if( (l_ret_check = s_tx_cache_check(a_ledger, a_tx, a_tx_hash, a_from_threshold,
                                                        &l_list_bound_items, &l_list_tx_out,
-                                                       l_main_token_ticker, &l_tag, &l_action, false))) {
+                                                       l_main_token_ticker, &l_tag, &l_action, false))) {                                                        
         if ((l_ret_check == DAP_CHAIN_CS_VERIFY_CODE_TX_NO_PREVIOUS ||
                 l_ret_check == DAP_CHAIN_CS_VERIFY_CODE_TX_NO_EMISSION) &&
                 l_ledger_pvt->threshold_enabled && !dap_chain_net_get_load_mode(a_ledger->net)) {
@@ -4451,8 +4451,6 @@ int dap_ledger_tx_add(dap_ledger_t *a_ledger, dap_chain_datum_tx_t *a_tx, dap_ha
             debug_if(s_debug_more, L_WARNING, "dap_ledger_tx_add() tx %s not passed the check: %s ", l_tx_hash_str,
                         dap_ledger_check_error_str(l_ret_check));
         }
-        dap_strncpy(a_datum_index_data->token_ticker, l_main_token_ticker, DAP_CHAIN_TICKER_SIZE_MAX);       
-        a_datum_index_data->action_tag = l_action;
         
         if ( l_list_bound_items )
             dap_list_free_full(l_list_bound_items, NULL);
@@ -4460,7 +4458,9 @@ int dap_ledger_tx_add(dap_ledger_t *a_ledger, dap_chain_datum_tx_t *a_tx, dap_ha
         return l_ret_check;
     }
     debug_if(s_debug_more, L_DEBUG, "dap_ledger_tx_add() check passed for tx %s", l_tx_hash_str);
-
+    dap_strncpy(a_datum_index_data->token_ticker, l_main_token_ticker, DAP_CHAIN_TICKER_SIZE_MAX);
+    a_datum_index_data->action = l_action;
+    a_datum_index_data->uid = l_tag;
     // Mark 'out' items in cache if they were used & delete previous transactions from cache if it need
     // find all bound pairs 'in' and 'out'
     size_t l_outs_used = dap_list_length(l_list_bound_items);
diff --git a/modules/net/dap_chain_net.c b/modules/net/dap_chain_net.c
index 2f427af466..589ec19e04 100644
--- a/modules/net/dap_chain_net.c
+++ b/modules/net/dap_chain_net.c
@@ -3265,7 +3265,7 @@ int dap_chain_datum_add(dap_chain_t *a_chain, dap_chain_datum_t *a_datum, size_t
             if (l_tx_size != l_datum_data_size) {
                 log_it(L_WARNING, "Corrupted transaction, datum size %zd is not equal to size of TX %zd", l_datum_data_size, l_tx_size);
                 return -102;
-            }
+            }            
             return dap_ledger_tx_load(l_ledger, l_tx, a_datum_hash, (dap_ledger_datum_iter_data_t*)a_datum_index_data);
         }
         case DAP_CHAIN_DATUM_CA:
diff --git a/modules/net/dap_chain_node_cli_cmd_tx.c b/modules/net/dap_chain_node_cli_cmd_tx.c
index 3932209805..a1f9bca912 100644
--- a/modules/net/dap_chain_node_cli_cmd_tx.c
+++ b/modules/net/dap_chain_node_cli_cmd_tx.c
@@ -246,7 +246,6 @@ static void s_tx_header_print(json_object* json_obj_datum, dap_chain_tx_hash_pro
                               dap_chain_hash_fast_t *a_tx_hash)
 {
     bool l_declined = false;
-    bool l_declined2 = false;
     // transaction time
     char l_time_str[DAP_TIME_STR_SIZE] = "unknown";                                /* Prefill string */
     if (a_tx->header.ts_created)
@@ -254,7 +253,7 @@ static void s_tx_header_print(json_object* json_obj_datum, dap_chain_tx_hash_pro
     dap_chain_tx_hash_processed_ht_t *l_tx_data = NULL;
     HASH_FIND(hh, *a_tx_data_ht, a_tx_hash, sizeof(*a_tx_hash), l_tx_data);
     if (l_tx_data)  // this tx already present in ledger (double)
-        {l_declined = true;l_declined2 = true;}
+        l_declined = true;
     else {
         l_tx_data = DAP_NEW_Z(dap_chain_tx_hash_processed_ht_t);
         if (!l_tx_data) {
@@ -263,10 +262,7 @@ static void s_tx_header_print(json_object* json_obj_datum, dap_chain_tx_hash_pro
         }
         l_tx_data->hash = *a_tx_hash;
         HASH_ADD(hh, *a_tx_data_ht, hash, sizeof(*a_tx_hash), l_tx_data);
-        const char *l_token_ticker = dap_ledger_tx_get_token_ticker_by_hash(a_ledger, a_tx_hash);
-        const char *l_token_ticker_t = a_datum_iter->token_ticker;
-        if (!l_token_ticker_t)
-            l_declined2 = true;
+        const char *l_token_ticker = a_datum_iter->token_ticker;        
         if (!l_token_ticker)
             l_declined = true;
     }
@@ -278,7 +274,6 @@ static void s_tx_header_print(json_object* json_obj_datum, dap_chain_tx_hash_pro
         l_tx_hash_str = dap_enc_base58_encode_hash_to_str(a_tx_hash);
         l_atom_hash_str = dap_enc_base58_encode_hash_to_str(a_datum_iter->cur_atom_hash);
     }
-    json_object_object_add(json_obj_datum, "test_sts", json_object_new_string(l_declined2 ? "DECLINED" : "ACCEPTED"));
     json_object_object_add(json_obj_datum, "status", json_object_new_string(l_declined ? "DECLINED" : "ACCEPTED"));
     json_object_object_add(json_obj_datum, "hash", json_object_new_string(l_tx_hash_str));
     json_object_object_add(json_obj_datum, "atom_hash", json_object_new_string(l_atom_hash_str));
@@ -398,7 +393,6 @@ json_object* dap_db_history_addr(dap_chain_addr_t *a_addr, dap_chain_t *a_chain,
         const char *l_noaddr_token = NULL;
 
         dap_hash_fast_t l_tx_hash = *l_datum_iter->cur_hash;
-        //const char *l_src_token = dap_ledger_tx_get_token_ticker_by_hash(l_ledger, &l_tx_hash);
         const char *l_src_token = l_datum_iter->token_ticker;
 
         int l_src_subtype = DAP_CHAIN_TX_OUT_COND_SUBTYPE_UNDEFINED;
@@ -545,10 +539,13 @@ json_object* dap_db_history_addr(dap_chain_addr_t *a_addr, dap_chain_t *a_chain,
             //tag
             char *service_name = NULL;
             dap_chain_tx_tag_action_type_t l_action;
+            dap_chain_tx_tag_action_type_t l_action2;
+            bool srv_found2 = l_datum_iter->uid.uint64 ? true : false;
+            l_action2 = l_datum_iter->action; 
             bool srv_found = dap_ledger_tx_service_info(l_ledger, &l_tx_hash, NULL, &service_name, &l_action);
             if (!(l_action & a_action))
                 continue;
-
+            if(l_action2) i_tmp++;
             if (a_srv)
             {
               
@@ -720,7 +717,7 @@ static int s_json_tx_history_pack(json_object** a_json_obj_datum, dap_chain_datu
     if (a_srv)
     {
         char *service_name = NULL;
-        bool srv_found = false;//dap_ledger_tx_service_info(l_ledger, &l_ttx_hash, NULL, &service_name, NULL);
+        bool srv_found = dap_ledger_tx_service_info(l_ledger, &l_ttx_hash, NULL, &service_name, NULL);
         //skip if looking for UNKNOWN + it is known
         if (a_look_for_unknown_service && srv_found) {
             return 1;
diff --git a/modules/net/include/dap_chain_ledger.h b/modules/net/include/dap_chain_ledger.h
index 9227f2e2fc..8acfcb8281 100644
--- a/modules/net/include/dap_chain_ledger.h
+++ b/modules/net/include/dap_chain_ledger.h
@@ -199,7 +199,8 @@ typedef struct dap_ledger_datum_iter {
 
 typedef struct dap_ledger_datum_iter_data {
     char token_ticker[DAP_CHAIN_TICKER_SIZE_MAX];
-    dap_chain_tx_tag_action_type_t action_tag;
+    dap_chain_tx_tag_action_type_t action;
+    dap_chain_net_srv_uid_t uid;
 } dap_ledger_datum_iter_data_t;
 
 typedef int (*dap_ledger_verificator_callback_t)(dap_ledger_t *a_ledger, dap_chain_tx_out_cond_t *a_tx_out_cond, dap_chain_datum_tx_t *a_tx_in, bool a_owner);
diff --git a/modules/type/blocks/dap_chain_cs_blocks.c b/modules/type/blocks/dap_chain_cs_blocks.c
index d54a4dcf95..4d592c6eea 100644
--- a/modules/type/blocks/dap_chain_cs_blocks.c
+++ b/modules/type/blocks/dap_chain_cs_blocks.c
@@ -43,6 +43,8 @@ typedef struct dap_chain_block_datum_index {
     dap_chain_block_cache_t *block_cache;
     size_t datum_index;
     char token_ticker[DAP_CHAIN_TICKER_SIZE_MAX];
+    dap_chain_net_srv_uid_t service_uid;
+    dap_chain_tx_tag_action_type_t action;
     UT_hash_handle hh;
 } dap_chain_block_datum_index_t;
 
@@ -1477,7 +1479,8 @@ static int s_add_atom_datums(dap_chain_cs_blocks_t *a_blocks, dap_chain_block_ca
             break;
         }
         dap_hash_fast_t *l_datum_hash = a_block_cache->datum_hash + i;
-        dap_ledger_datum_iter_data_t l_datum_index_data;
+        dap_ledger_datum_iter_data_t l_datum_index_data = { "0", 0 , 0 };
+
         int l_res = dap_chain_datum_add(a_blocks->chain, l_datum, l_datum_size, l_datum_hash, &l_datum_index_data);
         l_ret++;
         if (l_datum->header.type_id == DAP_CHAIN_DATUM_TX)
@@ -1493,7 +1496,9 @@ static int s_add_atom_datums(dap_chain_cs_blocks_t *a_blocks, dap_chain_block_ca
         l_datum_index->datum_hash = *l_datum_hash;
         l_datum_index->ret_code = l_res;
         l_datum_index->datum_index = i;
-        dap_strncpy(l_datum_index->token_ticker, l_datum_index_data.token_ticker, DAP_CHAIN_TICKER_SIZE_MAX); 
+        l_datum_index->action = l_datum_index_data.action;
+        l_datum_index->service_uid = l_datum_index_data.uid;
+        dap_strncpy(l_datum_index->token_ticker, l_datum_index_data.token_ticker, DAP_CHAIN_TICKER_SIZE_MAX);
         pthread_rwlock_wrlock(&PVT(a_blocks)->datums_rwlock);
         HASH_ADD(hh, PVT(a_blocks)->datum_index, datum_hash, sizeof(*l_datum_hash), l_datum_index);
         pthread_rwlock_unlock(&PVT(a_blocks)->datums_rwlock);
@@ -2214,7 +2219,9 @@ static void s_datum_iter_fill(dap_chain_datum_iter_t *a_datum_iter, dap_chain_bl
         a_datum_iter->cur_hash = &a_datum_index->datum_hash;
         a_datum_iter->cur_atom_hash = &a_datum_index->block_cache->block_hash;
         a_datum_iter->ret_code = a_datum_index->ret_code;
-        a_datum_iter->token_ticker = a_datum_index->token_ticker;
+        a_datum_iter->action = a_datum_index->action;
+        a_datum_iter->uid = a_datum_index->service_uid;    
+        a_datum_iter->token_ticker = dap_strcmp(a_datum_index->token_ticker, "0") ? a_datum_index->token_ticker : NULL;
     } else {
         a_datum_iter->cur = NULL;
         a_datum_iter->cur_hash = NULL;
@@ -2222,6 +2229,8 @@ static void s_datum_iter_fill(dap_chain_datum_iter_t *a_datum_iter, dap_chain_bl
         a_datum_iter->cur_size = 0;
         a_datum_iter->ret_code = 0;
         a_datum_iter->token_ticker = NULL;
+        a_datum_iter->action = NULL;
+        a_datum_iter->uid.uint64 = 0;
     }
     debug_if(a_datum_index && !a_datum_index->block_cache->datum, L_ERROR, "Chains was deleted with errors");
 }
-- 
GitLab