From 17da240ddbbc85a01ac0e852cd126fd58c609798 Mon Sep 17 00:00:00 2001
From: "alexey.stratulat" <alexey.stratulat@demlabs.net>
Date: Fri, 30 Dec 2022 22:15:53 +0700
Subject: [PATCH] [*] Moved the implementation of the mempool filter from the
 master.

---
 modules/mempool/dap_chain_mempool.c         | 55 +++++++++++++++++++++
 modules/mempool/include/dap_chain_mempool.h |  2 +
 modules/net/dap_chain_node_cli_cmd.c        |  4 ++
 3 files changed, 61 insertions(+)

diff --git a/modules/mempool/dap_chain_mempool.c b/modules/mempool/dap_chain_mempool.c
index 98f70b3323..5a8b7e0b3c 100644
--- a/modules/mempool/dap_chain_mempool.c
+++ b/modules/mempool/dap_chain_mempool.c
@@ -1043,3 +1043,58 @@ void dap_chain_mempool_add_proc(dap_http_t * a_http_server, const char * a_url)
 {
     dap_http_simple_proc_add(a_http_server, a_url, 4096, chain_mempool_proc);
 }
+
+/**
+ * @breif dap_chain_mempool_filter
+ * @param a_chain chain whose mempool will be filtered.
+ * @param a_removed Pointer to a variable of type int which will store how many remote datums.
+ */
+void dap_chain_mempool_filter(dap_chain_t *a_chain, int *a_removed){
+    int l_removed = 0;
+    if (!a_chain) {
+        if (!a_removed)
+            *a_removed = l_removed;
+        return;
+    }
+    char * l_gdb_group = dap_chain_net_get_gdb_group_mempool_new(a_chain);
+    size_t l_objs_size = 0;
+    dap_time_t l_cut_off_time = dap_time_now() - 2592000; // 2592000 sec = 30 days
+    char l_cut_off_time_str[80] = {'\0'};
+    dap_time_to_str_rfc822(&l_cut_off_time_str, 80, l_cut_off_time);
+    dap_global_db_obj_t * l_objs = dap_global_db_get_all_sync(l_gdb_group, &l_objs_size);
+    for (size_t i = 0; i < l_objs_size; i++) {
+        dap_chain_datum_t *l_datum = (dap_chain_datum_t*)l_objs[i].value;
+        size_t l_datum_size = dap_chain_datum_size(l_datum);
+        //Filter data size
+        if (l_datum_size != l_objs[i].value_len) {
+            l_removed++;
+            log_it(L_NOTICE, "Removed datum from mempool with \"%s\" key group %s. The size of the datum defined by the "
+                             "function and the size specified in the record do not match.", l_objs[i].key, l_gdb_group);
+            dap_global_db_del_sync(l_objs[i].key, l_gdb_group);
+            continue;
+        }
+        //Filter hash
+        dap_hash_fast_t l_hash_content = {0};
+        dap_hash_fast(l_datum->data, l_datum->header.data_size, &l_hash_content);
+        char *l_hash_content_str = dap_hash_fast_to_str_new(&l_hash_content);
+        if (dap_strcmp(l_hash_content_str, l_objs[i].key) != 0) {
+            l_removed++;
+            DAP_DELETE(l_hash_content_str);
+            log_it(L_NOTICE, "Removed datum from mempool with \"%s\" key group %s. The hash of the contents of the "
+                             "datum does not match the key.", l_objs[i].key, l_gdb_group);
+            dap_global_db_del_sync(l_objs[i].key, l_gdb_group);
+            continue;
+        }
+        DAP_DELETE(l_hash_content_str);
+        //Filter time
+        if (l_datum->header.ts_create < l_cut_off_time) {
+            l_removed++;
+            log_it(L_NOTICE, "Removed datum from mempool with \"%s\" key group %s. The datum in the mempool was "
+                             "created after the %s.", l_objs[i].key, l_gdb_group, l_cut_off_time_str);
+            dap_global_db_del_sync(l_objs[i].key, l_gdb_group);
+        }
+    }
+    dap_global_db_objs_delete(l_objs, l_objs_size);
+    log_it(L_NOTICE, "Filter removed: %i records.", l_removed);
+    DAP_DELETE(l_gdb_group);
+}
diff --git a/modules/mempool/include/dap_chain_mempool.h b/modules/mempool/include/dap_chain_mempool.h
index ee5670144b..e5c3740211 100644
--- a/modules/mempool/include/dap_chain_mempool.h
+++ b/modules/mempool/include/dap_chain_mempool.h
@@ -44,6 +44,8 @@ void dap_datum_mempool_free(dap_datum_mempool_t *datum);
 
 void dap_chain_mempool_add_proc(dap_http_t * a_http_server, const char * a_url);
 
+void dap_chain_mempool_filter(dap_chain_t *a_chain, int *a_removed);
+
 char *dap_chain_mempool_datum_add(const dap_chain_datum_t *a_datum, dap_chain_t *a_chain, const char *a_hash_out_type);
 
 char *dap_chain_mempool_tx_create(dap_chain_t *a_chain, dap_enc_key_t *a_key_from,
diff --git a/modules/net/dap_chain_node_cli_cmd.c b/modules/net/dap_chain_node_cli_cmd.c
index 7f4023572f..9d71832568 100644
--- a/modules/net/dap_chain_node_cli_cmd.c
+++ b/modules/net/dap_chain_node_cli_cmd.c
@@ -2349,6 +2349,10 @@ const char *s_ticker_list_get_main_ticker(dap_list_t *a_tickers, const char *a_n
  * @param a_hash_out_type
  */
 void s_com_mempool_list_print_for_chain(dap_chain_net_t * a_net, dap_chain_t * a_chain, const char * a_add, dap_string_t * a_str_tmp, const char *a_hash_out_type){
+    int l_removed = 0;
+    dap_chain_mempool_filter(a_chain, &l_removed);
+    dap_string_append_printf(a_str_tmp, "Removed %i records from the %s chain mempool in %s network. \n\n",
+                             l_removed, a_chain->name, a_net->pub.name);
     char * l_gdb_group_mempool = dap_chain_net_get_gdb_group_mempool_new(a_chain);
     if(!l_gdb_group_mempool){
         dap_string_append_printf(a_str_tmp, "%s.%s: chain not found\n", a_net->pub.name, a_chain->name);
-- 
GitLab