From 94ff5a32e30465d3e5986a11753caf57f67e5096 Mon Sep 17 00:00:00 2001
From: "Dmitriy A. Gerasimov" <dmitriy.gerasimov@demlabs.net>
Date: Mon, 25 Nov 2019 17:08:03 +0700
Subject: [PATCH] [+] Single tx search function

---
 dap_chain_net.c | 39 +++++++++++++++++++++++++++++++++++++++
 dap_chain_net.h | 15 +++++++++++++++
 2 files changed, 54 insertions(+)

diff --git a/dap_chain_net.c b/dap_chain_net.c
index b87c97b62e..54b24eb00d 100644
--- a/dap_chain_net.c
+++ b/dap_chain_net.c
@@ -1617,3 +1617,42 @@ void dap_chain_net_proc_datapool (dap_chain_net_t * a_net)
 {
 
 }
+
+/**
+ * @brief dap_chain_net_tx_get_by_hash
+ * @param a_net
+ * @param a_tx_hash
+ * @param a_search_type
+ * @return
+ */
+dap_chain_datum_tx_t * dap_chain_net_get_tx_by_hash(dap_chain_net_t * a_net, dap_chain_hash_fast_t * a_tx_hash,
+                                                     dap_chain_net_tx_search_type_t a_search_type)
+{
+    dap_ledger_t * l_ledger = dap_chain_ledger_by_net_name( a_net->pub.name );
+    dap_chain_datum_tx_t * l_tx = NULL;
+
+    switch (a_search_type) {
+        case TX_SEARCH_TYPE_NET:
+        case TX_SEARCH_TYPE_CELL:
+        case TX_SEARCH_TYPE_LOCAL:{
+            if ( ! l_tx ){
+                // pass all chains
+                for ( dap_chain_t * l_chain = a_net->pub.chains; l_chain; l_chain = l_chain->next){
+                    if ( l_chain->callback_tx_find_by_hash ){
+                        // try to find transaction in chain ( inside shard )
+                        l_tx = l_chain->callback_tx_find_by_hash( l_chain, a_tx_hash );
+                        if (l_tx)
+                            break;
+                    }
+                }
+            }
+        }break;
+
+        case TX_SEARCH_TYPE_NET_UNSPENT:
+        case TX_SEARCH_TYPE_CELL_UNSPENT:{
+            l_tx = dap_chain_ledger_tx_find_by_hash( l_ledger, a_tx_hash );
+        }break;
+    }
+    return l_tx;
+}
+
diff --git a/dap_chain_net.h b/dap_chain_net.h
index 953a3d9990..99cd058fc8 100644
--- a/dap_chain_net.h
+++ b/dap_chain_net.h
@@ -108,6 +108,21 @@ dap_chain_cell_id_t * dap_chain_net_get_cur_cell( dap_chain_net_t * l_net);
 
 void dap_chain_net_links_connect(dap_chain_net_t * a_net);
 
+typedef enum dap_chain_net_tx_search_type {
+    /// Search local, in memory, possible load data from drive to memory
+    TX_SEARCH_TYPE_LOCAL,
+    /// Do the request to the network if its not full node, search inside shard
+    TX_SEARCH_TYPE_CELL,
+    /// Do the request for unspent txs in cell
+    TX_SEARCH_TYPE_CELL_UNSPENT,
+    /// Do the search in whole network and request tx from others cells if need
+    TX_SEARCH_TYPE_NET,
+    /// Do the search in whole network but search only unspent
+    TX_SEARCH_TYPE_NET_UNSPENT
+}dap_chain_net_tx_search_type_t;
+
+dap_chain_datum_tx_t * dap_chain_net_get_tx_by_hash(dap_chain_net_t * a_net, dap_chain_hash_fast_t * a_tx_hash,
+                                                     dap_chain_net_tx_search_type_t a_search_type);
 
 /**
  * @brief dap_chain_net_get_gdb_group_mempool
-- 
GitLab