From 913e016608988aa19e53febdad13e2797b90b063 Mon Sep 17 00:00:00 2001
From: "roman.khlopkov" <roman.khlopkov@demlabs.net>
Date: Fri, 27 Oct 2023 10:01:13 +0300
Subject: [PATCH] [+] Implemented search for chain mempool cluster

---
 .../consensus/esbocs/dap_chain_cs_esbocs.c    |  2 +-
 modules/net/dap_chain_net.c                   | 25 ++++++++++++++-----
 modules/net/include/dap_chain_net.h           |  3 ++-
 3 files changed, 22 insertions(+), 8 deletions(-)

diff --git a/modules/consensus/esbocs/dap_chain_cs_esbocs.c b/modules/consensus/esbocs/dap_chain_cs_esbocs.c
index bcfea3ed21..6aa4622628 100644
--- a/modules/consensus/esbocs/dap_chain_cs_esbocs.c
+++ b/modules/consensus/esbocs/dap_chain_cs_esbocs.c
@@ -373,7 +373,7 @@ static int s_callback_created(dap_chain_t *a_chain, dap_config_t *a_chain_net_cf
     for (dap_list_t *it = l_validators; it; it = it->next) {
         dap_stream_node_addr_t *l_addr = &((dap_chain_net_srv_stake_item_t *)it->data)->node_addr;
         dap_global_db_cluster_member_add(l_session->db_cluster, l_addr, DAP_GDB_MEMBER_ROLE_ROOT);
-        dap_chain_net_add_validator_to_clusters(l_net, l_addr);
+        dap_chain_net_add_validator_to_clusters(a_chain, l_addr);
     }
     //Find order minimum fee
     char * l_gdb_group_str = dap_chain_net_srv_order_get_gdb_group(l_net);
diff --git a/modules/net/dap_chain_net.c b/modules/net/dap_chain_net.c
index 274c3b7ab4..81c41a7129 100644
--- a/modules/net/dap_chain_net.c
+++ b/modules/net/dap_chain_net.c
@@ -2547,17 +2547,29 @@ int s_net_load(dap_chain_net_t *a_net)
     return 0;
 }
 
-void dap_chain_add_mempool_notify_callback(dap_chain_t *a_chain, dap_store_obj_callback_notify_t a_callback, void *a_cb_arg)
+dap_global_db_cluster_t *dap_chain_net_get_mempool_cluster(dap_chain_t *a_chain)
 {
+    dap_return_val_if_fail(a_chain, NULL);
     dap_chain_net_t *l_net = dap_chain_net_by_id(a_chain->net_id);
+    if (!l_net) {
+        log_it(L_ERROR, "Invalid chain specified for mempool cluster search");
+        return NULL;
+    }
     dap_global_db_cluster_t *l_mempool = PVT(l_net)->mempool_clusters;
     dap_chain_t *l_chain;
     DL_FOREACH(l_net->pub.chains, l_chain) {
         if (l_chain == a_chain)
-            dap_global_db_cluster_add_notify_callback(l_mempool, a_callback, a_cb_arg);
+            return l_mempool;
         assert(l_mempool);
         l_mempool = l_mempool->next;
     }
+    log_it(L_ERROR, "No mempool cluster found for chain specified");
+    return NULL;
+}
+
+void dap_chain_add_mempool_notify_callback(dap_chain_t *a_chain, dap_store_obj_callback_notify_t a_callback, void *a_cb_arg)
+{
+    dap_global_db_cluster_add_notify_callback(dap_chain_net_get_mempool_cluster(a_chain), a_callback, a_cb_arg);
 }
 
 static void s_nodelist_change_notify(dap_store_obj_t *a_obj, void *a_arg)
@@ -2599,11 +2611,12 @@ int dap_chain_net_add_poa_certs_to_cluster(dap_chain_net_t *a_net, dap_global_db
     return 0;
 }
 
-bool dap_chain_net_add_validator_to_clusters(dap_chain_net_t *a_net, dap_stream_node_addr_t *a_addr)
+bool dap_chain_net_add_validator_to_clusters(dap_chain_t *a_chain, dap_stream_node_addr_t *a_addr)
 {
-    // Zerochain is always first, so clusters->next is mainchain cluster
-    bool l_ret = dap_global_db_cluster_member_add(PVT(a_net)->mempool_clusters->next, a_addr, DAP_GDB_MEMBER_ROLE_ROOT);
-    return !l_ret ? false : dap_global_db_cluster_member_add(PVT(a_net)->orders_cluster, a_addr, DAP_GDB_MEMBER_ROLE_USER);
+    bool l_ret = dap_global_db_cluster_member_add(
+                dap_chain_net_get_mempool_cluster(a_chain), a_addr, DAP_GDB_MEMBER_ROLE_ROOT);
+    return !l_ret ? false : dap_global_db_cluster_member_add(
+                        PVT(dap_chain_net_by_id(a_chain->net_id))->orders_cluster, a_addr, DAP_GDB_MEMBER_ROLE_USER);
 }
 
 /**
diff --git a/modules/net/include/dap_chain_net.h b/modules/net/include/dap_chain_net.h
index 2610d62eb6..7ac7364bf8 100644
--- a/modules/net/include/dap_chain_net.h
+++ b/modules/net/include/dap_chain_net.h
@@ -153,7 +153,8 @@ dap_list_t* dap_chain_net_get_node_list_cfg(dap_chain_net_t * a_net);
 dap_chain_node_role_t dap_chain_net_get_role(dap_chain_net_t * a_net);
 dap_chain_node_info_t *dap_get_balancer_link_from_cfg(dap_chain_net_t *a_net);
 int dap_chain_net_add_poa_certs_to_cluster(dap_chain_net_t *a_net, dap_global_db_cluster_t *a_cluster);
-bool dap_chain_net_add_validator_to_clusters(dap_chain_net_t *a_net, dap_stream_node_addr_t *a_addr);
+bool dap_chain_net_add_validator_to_clusters(dap_chain_t *a_chain, dap_stream_node_addr_t *a_addr);
+dap_global_db_cluster_t *dap_chain_net_get_mempool_cluster(dap_chain_t *a_chain);
 
 /**
  * @brief dap_chain_net_get_gdb_group_mempool
-- 
GitLab