From 752cb1613e54b9dfec7c349e1d8bb582f3a20290 Mon Sep 17 00:00:00 2001
From: Dmitriy Naidolinskiy <dmitriy.naidolinskiy@demlabs.net>
Date: Mon, 3 Jan 2022 16:58:38 +0000
Subject: [PATCH] [+] add white groups

---
 modules/channel/chain/dap_stream_ch_chain.c  | 14 ++++++++-
 modules/global-db/dap_chain_global_db_hist.c | 33 ++++++++++++++++++--
 2 files changed, 44 insertions(+), 3 deletions(-)

diff --git a/modules/channel/chain/dap_stream_ch_chain.c b/modules/channel/chain/dap_stream_ch_chain.c
index bc8c77212f..270a3de6e2 100644
--- a/modules/channel/chain/dap_stream_ch_chain.c
+++ b/modules/channel/chain/dap_stream_ch_chain.c
@@ -114,7 +114,9 @@ static bool s_debug_more=false;
 static uint_fast16_t s_update_pack_size=100; // Number of hashes packed into the one packet
 static uint_fast16_t s_skip_in_reactor_count=50; // Number of hashes packed to skip in one reactor loop callback out packet
 static char **s_list_ban_groups = NULL;
+static char **s_list_white_groups = NULL;
 static uint16_t s_size_ban_groups = 0;
+static uint16_t s_size_white_groups = 0;
 
 /**
  * @brief dap_stream_ch_chain_init
@@ -128,6 +130,7 @@ int dap_stream_ch_chain_init()
     s_debug_more = dap_config_get_item_bool_default(g_config,"stream_ch_chain","debug_more",false);
     s_update_pack_size = dap_config_get_item_int16_default(g_config,"stream_ch_chain","update_pack_size",100);
     s_list_ban_groups = dap_config_get_array_str(g_config, "stream_ch_chain", "ban_list_sync_groups", &s_size_ban_groups);
+	s_list_white_groups = dap_config_get_array_str(g_config, "stream_ch_chain", "white_list_sync_groups", &s_size_white_groups);
 
     return 0;
 }
@@ -661,7 +664,16 @@ static bool s_gdb_in_pkt_proc_callback(dap_proc_thread_t *a_thread, void *a_arg)
             // obj to add
             dap_store_obj_t *l_obj = l_store_obj + i;
 
-            if (s_list_ban_groups) {
+            if (s_list_white_groups) {
+                int l_ret = -1;
+                for (int i = 0; i < s_size_white_groups; i++) {
+                    if (!dap_fnmatch(s_list_white_groups[i], l_obj->group, FNM_NOESCAPE)) {
+                        l_ret = 0;
+                        break;
+                    }
+                }
+                if (l_ret == -1) continue;
+            } else if (s_list_ban_groups) {
                 int l_ret = 0;
                 for (int i = 0; i < s_size_ban_groups; i++) {
                     if (!dap_fnmatch(s_list_ban_groups[i], l_obj->group, FNM_NOESCAPE)) {
diff --git a/modules/global-db/dap_chain_global_db_hist.c b/modules/global-db/dap_chain_global_db_hist.c
index 25691ff0ef..ae23e302ad 100644
--- a/modules/global-db/dap_chain_global_db_hist.c
+++ b/modules/global-db/dap_chain_global_db_hist.c
@@ -28,8 +28,12 @@ typedef struct dap_tx_data{
 
 #define LOG_TAG "dap_chain_global_db_hist"
 
+#if 0
 static char **s_ban_list = NULL;
 static uint16_t s_size_ban_list = 0;
+static char **s_white_list = NULL;
+static uint16_t s_size_white_list = 0;
+#endif
 
 /**
  * @brief Packs members of a_rec structure into a single string.
@@ -279,15 +283,40 @@ dap_db_log_list_t* dap_db_log_list_start(dap_chain_node_addr_t a_addr, int a_fla
 
     static uint16_t s_size_ban_list = 0;
     static char **s_ban_list = NULL;
+
+	static uint16_t s_size_white_list = 0;
+	static char **s_white_list = NULL;
+
     static bool l_try_read_ban_list = false;
+	static bool l_try_read_white_list = false;
 
     if (!l_try_read_ban_list) {
             s_ban_list = dap_config_get_array_str(g_config, "stream_ch_chain", "ban_list_sync_groups", &s_size_ban_list);
             l_try_read_ban_list = true;
     }
+	if (!l_try_read_white_list) {
+			s_white_list = dap_config_get_array_str(g_config, "stream_ch_chain", "white_list_sync_groups", &s_size_white_list);
+			l_try_read_white_list = true;
+	}
 
-    /* delete groups from ban list */
-    if (s_size_ban_list > 0) {
+	/* delete if not condition */
+	if (s_size_white_list > 0) {
+        for (dap_list_t *l_groups = l_dap_db_log_list->groups; l_groups; ) {
+            bool l_found = false;
+            for (int i = 0; i < s_size_white_list; i++) {
+                if (!dap_fnmatch(s_white_list[i], l_groups->data, FNM_NOESCAPE)) {
+                    l_found = true;
+                    break;
+                }
+            }
+            if (!l_found) {
+                    dap_list_t *l_tmp = l_groups->next;
+                    l_dap_db_log_list->groups = dap_list_delete_link(l_dap_db_log_list->groups, l_groups);
+                    l_groups = l_tmp;
+			}
+            l_groups = dap_list_next(l_groups);
+        }
+	} else if (s_size_ban_list > 0) {
         for (dap_list_t *l_groups = l_dap_db_log_list->groups; l_groups; ) {
             bool l_found = false;
             for (int i = 0; i < s_size_ban_list; i++) {
-- 
GitLab