From 6b31cdfca8a783e065ddb16007d764159c63b912 Mon Sep 17 00:00:00 2001
From: "roman.padenkov" <roman.padenkov@demlabs.net>
Date: Thu, 25 May 2023 12:04:03 +0000
Subject: [PATCH] Feature 7926

---
 .../chain-net/dap_stream_ch_chain_net.c       |  4 +-
 .../include/dap_stream_ch_chain_net.h         | 12 ++---
 modules/net/dap_chain_net.c                   | 12 +++++
 modules/net/dap_chain_net_balancer.c          | 49 +++++++++++++++----
 modules/net/include/dap_chain_net_balancer.h  |  1 +
 5 files changed, 60 insertions(+), 18 deletions(-)

diff --git a/modules/channel/chain-net/dap_stream_ch_chain_net.c b/modules/channel/chain-net/dap_stream_ch_chain_net.c
index 411e6b0ef5..8508fc08b2 100644
--- a/modules/channel/chain-net/dap_stream_ch_chain_net.c
+++ b/modules/channel/chain-net/dap_stream_ch_chain_net.c
@@ -371,8 +371,8 @@ void s_stream_ch_packet_in(dap_stream_ch_t* a_ch, void* a_arg)
                         dap_chain_net_srv_order_find_all_by(l_net,SERV_DIR_UNDEFINED,l_uid,
                                                            l_price_unit,NULL,l_price_min,l_price_max,&l_orders,&l_orders_num);
                         flags = l_orders_num ? flags | F_ORDR : flags & ~F_ORDR;
-                        bool auto_online = dap_config_get_item_bool_default( g_config, "general", "auto_online", true );
-                        bool auto_update = dap_config_get_item_bool_default( g_config, "general", "auto_update", true );
+                        bool auto_online = dap_config_get_item_bool_default( g_config, "general", "auto_online", false );
+                        bool auto_update = dap_config_get_item_bool_default( g_config, "general", "auto_update", false );
                         flags = auto_online ? flags | A_ONLN : flags & ~A_ONLN;
                         flags = auto_update ? flags | A_UPDT : flags & ~A_UPDT;
                         send->header.flags = flags;
diff --git a/modules/channel/chain-net/include/dap_stream_ch_chain_net.h b/modules/channel/chain-net/include/dap_stream_ch_chain_net.h
index e79bf2f993..639f65044c 100644
--- a/modules/channel/chain-net/include/dap_stream_ch_chain_net.h
+++ b/modules/channel/chain-net/include/dap_stream_ch_chain_net.h
@@ -55,12 +55,12 @@ typedef struct dap_stream_ch_chain_validator_test{
     byte_t sign[];
 } DAP_ALIGN_PACKED dap_stream_ch_chain_validator_test_t;
 
-#define A_PROC 0x01
-#define F_ORDR 0x02
-#define A_ONLN 0x04
-#define A_UPDT 0x08
-#define D_SIGN 0x40
-#define F_CERT 0x80
+#define A_PROC 0x01//autoproc set
+#define F_ORDR 0x02//order exist
+#define A_ONLN 0x04//auto online
+#define A_UPDT 0x08//auto update
+#define D_SIGN 0x40//data signed
+#define F_CERT 0x80//faund sert
 
 
 #define DAP_STREAM_CH_CHAIN_NET(a) ((dap_stream_ch_chain_net_t *) ((a)->internal) )
diff --git a/modules/net/dap_chain_net.c b/modules/net/dap_chain_net.c
index c3a0b62580..afb2be7b76 100644
--- a/modules/net/dap_chain_net.c
+++ b/modules/net/dap_chain_net.c
@@ -1097,6 +1097,18 @@ static bool s_new_balancer_link_request(dap_chain_net_t *a_net, int a_link_repla
         pthread_mutex_unlock(&l_net_pvt->uplinks_mutex);
         return false;
     }
+    dap_chain_node_info_t *l_link_full_node = dap_chain_net_balancer_get_node(a_net->pub.name);
+    if(l_link_full_node)
+    {
+        pthread_rwlock_rdlock(&PVT(a_net)->states_lock);
+        s_net_link_add(a_net, l_link_full_node);
+        DAP_DELETE(l_link_full_node);
+        struct net_link *l_free_link = s_get_free_link(a_net);
+        if (l_free_link)
+            s_net_link_start(a_net, l_free_link, l_net_pvt->reconnect_delay);
+        pthread_rwlock_unlock(&PVT(a_net)->states_lock);
+        return false;
+    }
     dap_chain_node_info_t *l_link_node_info = s_get_balancer_link_from_cfg(a_net);
     if (!l_link_node_info)
         return false;
diff --git a/modules/net/dap_chain_net_balancer.c b/modules/net/dap_chain_net_balancer.c
index 5851af3429..a3a90668f5 100644
--- a/modules/net/dap_chain_net_balancer.c
+++ b/modules/net/dap_chain_net_balancer.c
@@ -27,8 +27,7 @@ along with any CellFrame SDK based project.  If not, see <http://www.gnu.org/lic
 #include "http_status_code.h"
 
 #define LOG_TAG "dap_chain_net_balancer"
-
-dap_chain_node_info_t *s_balancer_issue_link(const char *a_net_name)
+dap_chain_node_info_t *dap_chain_net_balancer_get_node(const char *a_net_name)
 {
     dap_list_t *l_node_list = NULL,*l_objs_list = NULL;
     dap_chain_net_t *l_net = dap_chain_net_by_name(a_net_name);
@@ -48,7 +47,7 @@ dap_chain_node_info_t *s_balancer_issue_link(const char *a_net_name)
     // read all node
     l_objs = dap_global_db_get_all_sync(l_net->pub.gdb_nodes, &l_nodes_count);
     if (!l_nodes_count || !l_objs)
-        return NULL;    
+        return NULL;
     l_node_list = dap_chain_net_get_node_list_cfg(l_net);
     for(size_t i=0;i<l_nodes_count;i++)
     {
@@ -66,22 +65,52 @@ dap_chain_node_info_t *s_balancer_issue_link(const char *a_net_name)
         }
     }
     dap_global_db_objs_delete(l_objs, l_nodes_count);
+    dap_list_free(l_node_list);
     l_nodes_count = l_node_num;
     dap_chain_node_info_t *l_node_candidate;
     if(l_nodes_count)
     {
         l_node_num = rand() % l_nodes_count;
         l_node_candidate = (dap_chain_node_info_t *)dap_list_nth_data(l_objs_list,l_node_num);
+        dap_chain_node_info_t *l_node_info = DAP_NEW_Z(dap_chain_node_info_t);
+        memcpy(l_node_info, l_node_candidate, sizeof(dap_chain_node_info_t));
+        dap_list_free(l_objs_list);
+        return l_node_info;
     }
     else
-        l_node_candidate = (dap_chain_node_info_t *)dap_list_nth_data(l_node_list,0);
+    {
+        dap_list_free(l_objs_list);
+        log_it(L_DEBUG, "Node list is empty");
+        return NULL;
+    }
+}
 
-    dap_chain_node_info_t *l_node_info = DAP_NEW_Z(dap_chain_node_info_t);
-    memcpy(l_node_info, l_node_candidate, sizeof(dap_chain_node_info_t));
-    dap_list_free(l_objs_list);
-    dap_list_free(l_node_list);
-    log_it(L_DEBUG, "Network balancer issues ip %s", inet_ntoa(l_node_info->hdr.ext_addr_v4));
-    return l_node_info;
+dap_chain_node_info_t *s_balancer_issue_link(const char *a_net_name)
+{
+    dap_chain_net_t *l_net = dap_chain_net_by_name(a_net_name);
+    dap_chain_node_info_t *l_node_candidate = dap_chain_net_balancer_get_node(a_net_name);
+    if(l_node_candidate)
+    {
+        log_it(L_DEBUG, "Network balancer issues ip %s",inet_ntoa(l_node_candidate->hdr.ext_addr_v4));
+        return l_node_candidate;
+    }
+    else
+    {
+        dap_list_t *l_node_list = dap_chain_net_get_node_list_cfg(l_net);
+        uint16_t l_nods_cnt = dap_list_length(l_node_list);
+        if(l_node_list)
+        {
+            dap_chain_node_info_t *l_node_info = DAP_NEW_Z(dap_chain_node_info_t);
+            int i = rand() % l_nods_cnt;
+            dap_list_t *nl = l_node_list;
+            while(i--) nl = nl->next;
+            l_node_candidate = (dap_chain_node_info_t*)nl->data;
+            memcpy(l_node_info, l_node_candidate, sizeof(dap_chain_node_info_t));
+            dap_list_free(l_node_list);
+            return l_node_info;
+        }
+    }
+    return NULL;
 }
 
 void dap_chain_net_balancer_http_issue_link(dap_http_simple_t *a_http_simple, void *a_arg)
diff --git a/modules/net/include/dap_chain_net_balancer.h b/modules/net/include/dap_chain_net_balancer.h
index 9b4f545078..5390af1a5d 100644
--- a/modules/net/include/dap_chain_net_balancer.h
+++ b/modules/net/include/dap_chain_net_balancer.h
@@ -30,3 +30,4 @@ along with any CellFrame SDK based project.  If not, see <http://www.gnu.org/lic
 
 void dap_chain_net_balancer_http_issue_link(dap_http_simple_t *a_http_simple, void *a_arg);
 dap_chain_node_info_t *dap_chain_net_balancer_dns_issue_link(char *a_str);
+dap_chain_node_info_t *dap_chain_net_balancer_get_node(const char *a_net_name);
-- 
GitLab