From 4a8e2607fa15bcab60ab5746c495d92bd52e4de6 Mon Sep 17 00:00:00 2001
From: Dmitry Gerasimov <dmitriy.gerasimov@demlabs.net>
Date: Mon, 22 Aug 2022 21:05:38 +0700
Subject: [PATCH] [*] Some fixes for plugins [-] Removed modules_dynamic as
 useless now (plugins repeats its functionality)

---
 CMakeLists.txt                                |  10 +-
 dap-sdk/core/include/dap_common.h             |  38 +++----
 dap-sdk/plugin/src/dap_plugin.c               |  25 +++--
 dap-sdk/plugin/src/dap_plugin_binary.c        |  17 +--
 dap-sdk/plugin/src/dap_plugin_manifest.c      |   4 +-
 modules/CMakeLists.txt                        |   5 -
 modules/modules_dynamic/CMakeLists.txt        |   2 -
 modules/modules_dynamic/cdb/CMakeLists.txt    |  14 ---
 .../cdb/dap_modules_dynamic_cdb.c             | 102 ------------------
 .../cdb/include/dap_modules_dynamic_cdb.h     |  30 ------
 10 files changed, 51 insertions(+), 196 deletions(-)
 delete mode 100644 modules/modules_dynamic/CMakeLists.txt
 delete mode 100644 modules/modules_dynamic/cdb/CMakeLists.txt
 delete mode 100644 modules/modules_dynamic/cdb/dap_modules_dynamic_cdb.c
 delete mode 100644 modules/modules_dynamic/cdb/include/dap_modules_dynamic_cdb.h

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 10b6762805..0cf516a405 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -14,17 +14,13 @@ if(NOT DEFINED CELLFRAME_MODULES)
     set(CELLFRAME_MODULES "core chains mining network srv cs-dag-poa cs-block-poa cs-dag-pos cs-block-pos cs-block-ton cs-none srv-app srv-app-db srv-datum srv-stake-pos-delegate srv-stake-lock srv-xchange")
 
     if(LINUX OR DARWIN)
-        set(CELLFRAME_MODULES "${CELLFRAME_MODULES} modules-dynamic srv-vpn")
+        set(CELLFRAME_MODULES "${CELLFRAME_MODULES} srv-vpn")
     endif()
 
 endif()
 
 message("Cellframe modules: ${CELLFRAME_MODULES}")
 
-if (CELLFRAME_MODULES MATCHES "modules-dynamic")
-    add_definitions("-DDAP_MODULES_DYNAMIC")
-endif()
-
 if (CELLFRAME_MODULES MATCHES "srv-stake")
      add_definitions("-DDAP_SRV_STAKE_USED")
 endif()
@@ -126,7 +122,7 @@ set(CELLFRAME_LIBS "")
 # Core libs from dap-sdk
 if (CELLFRAME_MODULES MATCHES "core")
     message("[+] Module 'core'")
-    set(CELLFRAME_LIBS ${CELLFRAME_LIBS} dap_core dap_app_cli dap_crypto m pthread)
+    set(CELLFRAME_LIBS ${CELLFRAME_LIBS} dap_core dap_app_cli dap_plugin dap_crypto m pthread)
 endif()
 
 # General chain libs
@@ -139,7 +135,7 @@ endif()
 if (CELLFRAME_MODULES MATCHES "network")
     message("[+] Module 'network'")
     set(CELLFRAME_LIBS ${CELLFRAME_LIBS} dap_io dap_json_rpc dap_enc_server dap_notify_srv dap_http_server dap_session
-        dap_stream dap_stream_ch dap_client dap_stream_ch_chain dap_stream_ch_chain_net dap_chain_net dap_chain_net_srv dap_stream_ch_chain_voting dap_chain_mempool magic)
+        dap_stream dap_stream_ch dap_client dap_cli_server dap_stream_ch_chain dap_stream_ch_chain_net dap_chain_net dap_chain_net_srv dap_stream_ch_chain_voting dap_chain_mempool magic)
 endif()
 
 # Chain net services
diff --git a/dap-sdk/core/include/dap_common.h b/dap-sdk/core/include/dap_common.h
index 1269d8eeb2..5b306496c3 100755
--- a/dap-sdk/core/include/dap_common.h
+++ b/dap-sdk/core/include/dap_common.h
@@ -517,43 +517,43 @@ static inline const char *dap_get_arch() { //Get current architecture, detectx n
         #elif defined(i386) || defined(__i386__) || defined(__i386) || defined(_M_IX86)
         return "x86_32";
         #elif defined(__ARM_ARCH_2__)
-        return "ARM2";
+        return "arm2";
         #elif defined(__ARM_ARCH_3__) || defined(__ARM_ARCH_3M__)
-        return "ARM3";
+        return "arm3";
         #elif defined(__ARM_ARCH_4T__) || defined(__TARGET_ARM_4T)
-        return "ARM4T";
+        return "arm4t";
         #elif defined(__ARM_ARCH_5_) || defined(__ARM_ARCH_5E_)
-        return "ARM5"
+        return "arm5"
         #elif defined(__ARM_ARCH_6T2_) || defined(__ARM_ARCH_6T2_)
-        return "ARM6T2";
+        return "arm6t2";
         #elif defined(__ARM_ARCH_6__) || defined(__ARM_ARCH_6J__) || defined(__ARM_ARCH_6K__) || defined(__ARM_ARCH_6Z__) || defined(__ARM_ARCH_6ZK__)
-        return "ARM6";
+        return "arm6";
         #elif defined(__ARM_ARCH_7__) || defined(__ARM_ARCH_7A__) || defined(__ARM_ARCH_7R__) || defined(__ARM_ARCH_7M__) || defined(__ARM_ARCH_7S__)
-        return "ARM7";
+        return "arm7";
         #elif defined(__ARM_ARCH_7A__) || defined(__ARM_ARCH_7R__) || defined(__ARM_ARCH_7M__) || defined(__ARM_ARCH_7S__)
-        return "ARM7A";
+        return "arm7a";
         #elif defined(__ARM_ARCH_7R__) || defined(__ARM_ARCH_7M__) || defined(__ARM_ARCH_7S__)
-        return "ARM7R";
+        return "arm7r";
         #elif defined(__ARM_ARCH_7M__)
-        return "ARM7M";
+        return "arm7m";
         #elif defined(__ARM_ARCH_7S__)
-        return "ARM7S";
+        return "arm7s";
         #elif defined(__aarch64__) || defined(_M_ARM64)
-        return "ARM64";
+        return "arm64";
         #elif defined(mips) || defined(__mips__) || defined(__mips)
-        return "MIPS";
+        return "mips";
         #elif defined(__sh__)
-        return "SUPERH";
+        return "superh";
         #elif defined(__powerpc) || defined(__powerpc__) || defined(__powerpc64__) || defined(__POWERPC__) || defined(__ppc__) || defined(__PPC__) || defined(_ARCH_PPC)
-        return "POWERPC";
+        return "powerpc";
         #elif defined(__PPC64__) || defined(__ppc64__) || defined(_ARCH_PPC64)
-        return "POWERPC64";
+        return "powerpc64";
         #elif defined(__sparc__) || defined(__sparc)
-        return "SPARC";
+        return "sparc";
         #elif defined(__m68k__)
-        return "M68K";
+        return "m68k";
         #else
-        return "UNKNOWN";
+        return "unknown";
         #endif
     }
 
diff --git a/dap-sdk/plugin/src/dap_plugin.c b/dap-sdk/plugin/src/dap_plugin.c
index d00c05c958..812fecf0c4 100644
--- a/dap-sdk/plugin/src/dap_plugin.c
+++ b/dap-sdk/plugin/src/dap_plugin.c
@@ -38,14 +38,14 @@ This file is part of DAP (Deus Applications Prototypes) the open source project
 static char *s_plugins_root_path = NULL;
 
 struct plugin_type{
-    char *name;
+    char name[64];
     dap_plugin_type_callbacks_t callbacks;
     UT_hash_handle hh;
 } * s_types;
 
 
 struct plugin_module{
-    char *name;
+    char name[64];
     struct plugin_type *type;
     dap_plugin_manifest_t *manifest;
 
@@ -75,14 +75,17 @@ int dap_plugin_init(const char * a_root_path)
         log_it(L_ERROR, "Can't find \"%s\" directory", s_plugins_root_path);
         return -1;
     }
+    dap_plugin_manifest_init();
+    dap_plugin_command_init();
+
+    dap_plugin_binary_init();
+
 
     //Get list files
     dap_list_name_directories_t *l_list_plugins_name = dap_get_subs(s_plugins_root_path);
     dap_list_name_directories_t *l_element;
     // Register manifests
     log_it(L_DEBUG, "Start registration of manifests");
-    dap_plugin_manifest_init();
-    dap_plugin_command_init();
 
     char *l_name_file = NULL;
     LL_FOREACH(l_list_plugins_name, l_element){
@@ -100,6 +103,7 @@ int dap_plugin_init(const char * a_root_path)
 void dap_plugin_deinit(){
     log_it(L_NOTICE, "Deinitialize plugins");
     dap_plugin_stop_all();
+    dap_plugin_binary_deinit();
     dap_plugin_manifest_deinit();
     dap_plugin_command_deinit();
 }
@@ -131,13 +135,15 @@ int dap_plugin_type_create(const char* a_name, dap_plugin_type_callbacks_t* a_ca
         log_it(L_CRITICAL, "Can't create plugin type without callbacks!");
         return -2;
     }
-    struct plugin_type * l_new = DAP_NEW_Z(struct plugin_type);
-    if(!l_new){
+    struct plugin_type * l_type = DAP_NEW_Z(struct plugin_type);
+    if(!l_type){
         log_it(L_CRITICAL, "OOM on new type create");
         return -3;
     }
-    l_new->name = dap_strdup(a_name);
-    memcpy(&l_new->callbacks,a_callbacks,sizeof(l_new->callbacks));
+    strncpy(l_type->name,a_name, sizeof(l_type->name)-1);
+    memcpy(&l_type->callbacks,a_callbacks,sizeof(l_type->callbacks));
+    HASH_ADD_STR(s_types,name,l_type);
+    log_it(L_NOTICE, "Plugin type \"%s\" added", a_name);
     return 0;
 }
 
@@ -231,7 +237,7 @@ static int s_start(dap_plugin_manifest_t * a_manifest)
     struct plugin_type * l_type = NULL;
     HASH_FIND_STR(s_types, a_manifest->type, l_type);
     if(! l_type){
-        log_it(L_ERROR, "Plugin type \"%s\" is not recognized", a_manifest->type);
+        log_it(L_ERROR, "Plugin \"%s\" with type \"%s\" is not recognized", a_manifest->name, a_manifest->type);
         return -1;
     }
     if (a_manifest->dependencies != NULL){
@@ -265,6 +271,7 @@ static int s_start(dap_plugin_manifest_t * a_manifest)
         l_module->type = l_type;
         l_module->manifest = a_manifest;
         HASH_ADD_STR(s_modules,name,l_module);
+        log_it(L_NOTICE, "Plugin \"%s\" is loaded", a_manifest->name);
     }
     return l_ret;
 }
diff --git a/dap-sdk/plugin/src/dap_plugin_binary.c b/dap-sdk/plugin/src/dap_plugin_binary.c
index 6bf031d7fc..76c3e9fd34 100644
--- a/dap-sdk/plugin/src/dap_plugin_binary.c
+++ b/dap-sdk/plugin/src/dap_plugin_binary.c
@@ -23,7 +23,7 @@
  */
 
 #include "dap_strfuncs.h"
-#ifdef DAP_OS_LINUX
+#if defined(DAP_OS_UNIX)
 #include <dlfcn.h>
 #endif
 
@@ -57,7 +57,7 @@ int dap_plugin_binary_init()
     dap_plugin_type_callbacks_t l_callbacks={};
     l_callbacks.load = s_type_callback_load;
     l_callbacks.unload = s_type_callback_unload;
-    dap_plugin_type_create("bin",&l_callbacks);
+    dap_plugin_type_create("binary",&l_callbacks);
     s_manifest = dap_plugin_manifest_add_builtin("binary", "binary", "Demlabs Inc", "1.0","Binary shared library loader",NULL,0,NULL,0);
     return 0;
 }
@@ -84,8 +84,13 @@ static int s_type_callback_load(dap_plugin_manifest_t * a_manifest, void ** a_pv
         return 0;
     struct binary_pvt_data * l_pvt_data= DAP_NEW_Z(struct binary_pvt_data);
     *a_pvt_data = l_pvt_data;
-    #if defined (DAP_OS_LINUX) && !defined (__ANDROID__)
-    char * l_path = dap_strdup_printf("%s/%s.linux.common.%s.so",a_manifest->path,a_manifest->name,dap_get_arch());
+#if defined (DAP_OS_UNIX) && !defined (__ANDROID__)
+
+#if defined (DAP_OS_DARWIN)
+    char * l_path = dap_strdup_printf("%s/%s.darwin.%s.dylib",a_manifest->path,a_manifest->name,dap_get_arch());
+#elif defined (DAP_OS_LINUX)
+    char * l_path = dap_strdup_printf("%s/lib%s.linux.common.%s.so",a_manifest->path,a_manifest->name,dap_get_arch());
+#endif
     l_pvt_data->handle = dlopen(l_path, RTLD_NOW); // Try with specified architecture first
     if(l_pvt_data->handle){
         l_pvt_data->callback_init = dlsym(l_pvt_data->handle, "plugin_init");
@@ -95,7 +100,7 @@ static int s_type_callback_load(dap_plugin_manifest_t * a_manifest, void ** a_pv
         *a_error_str = dap_strdup_printf("Can't load %s module: %s (expected path %s)", a_manifest->name, l_path, dlerror());
         return -5;
     }
-    #endif
+#endif
     if( l_pvt_data->callback_init){
         return l_pvt_data->callback_init(a_manifest->config,a_error_str);
     }else{
@@ -121,7 +126,7 @@ static int s_type_callback_unload(dap_plugin_manifest_t * a_manifest, void * a_p
     assert(l_pvt_data);
     if(l_pvt_data->callback_deinit)
         l_pvt_data->callback_deinit();
-#if defined (DAP_OS_LINUX) && !defined (__ANDROID__)
+#if defined (DAP_OS_UNIX) && !defined (__ANDROID__)
     dlclose(l_pvt_data->handle);
 #endif
     return 0;
diff --git a/dap-sdk/plugin/src/dap_plugin_manifest.c b/dap-sdk/plugin/src/dap_plugin_manifest.c
index 32a511185e..ee015e0051 100644
--- a/dap-sdk/plugin/src/dap_plugin_manifest.c
+++ b/dap-sdk/plugin/src/dap_plugin_manifest.c
@@ -127,7 +127,7 @@ dap_plugin_manifest_t* dap_plugin_manifest_add_from_file(const char *a_file_path
         return NULL;
     if (!json_object_object_get_ex(l_json_obj, "description", &l_json_description))
         return NULL;
-    json_object_object_get_ex(l_json_obj, "description", &l_json_params);
+    json_object_object_get_ex(l_json_obj, "params", &l_json_params);
     json_object_object_get_ex(l_json_obj, "path", &l_json_path);
 
     const char *l_name, *l_type, *l_version, *l_author, *l_description;
@@ -149,7 +149,7 @@ dap_plugin_manifest_t* dap_plugin_manifest_add_from_file(const char *a_file_path
     l_author = json_object_get_string(l_json_author);
     l_description = json_object_get_string(l_json_description);
     l_dependencies_count = (size_t)json_object_array_length(l_json_dependencies);
-    l_params_count = (size_t)json_object_array_length(l_json_params);
+    l_params_count = l_json_params ? (size_t)json_object_array_length(l_json_params) : 0;
 
     // Read dependencies;
     if(l_dependencies_count){
diff --git a/modules/CMakeLists.txt b/modules/CMakeLists.txt
index 5741cde7a0..8c5b31bc5f 100644
--- a/modules/CMakeLists.txt
+++ b/modules/CMakeLists.txt
@@ -117,11 +117,6 @@ if (CELLFRAME_MODULES MATCHES "srv-stake-pos-delegate")
     add_subdirectory(service/stake_pos_delegate)
 endif()
 
-# Support for dynamic modules
-if (CELLFRAME_MODULES MATCHES "modules-dynamic")
-    add_subdirectory(modules_dynamic)
-endif()
-
 # Unit tests
 if( BUILD_TESTS)
     add_subdirectory(test)
diff --git a/modules/modules_dynamic/CMakeLists.txt b/modules/modules_dynamic/CMakeLists.txt
deleted file mode 100644
index f4c99055a8..0000000000
--- a/modules/modules_dynamic/CMakeLists.txt
+++ /dev/null
@@ -1,2 +0,0 @@
-add_subdirectory(cdb)
-
diff --git a/modules/modules_dynamic/cdb/CMakeLists.txt b/modules/modules_dynamic/cdb/CMakeLists.txt
deleted file mode 100644
index 7a387d86a5..0000000000
--- a/modules/modules_dynamic/cdb/CMakeLists.txt
+++ /dev/null
@@ -1,14 +0,0 @@
-cmake_minimum_required(VERSION 3.10)
-project (dap_modules_dynamic_cdb)
-
-file(GLOB DAP_MODULES_DYNAMIC_CDB_SRCS *.c)
-
-file(GLOB DAP_MODULES_DYNAMIC_CDB_HEADERS include/*.h)
-
-add_library(${PROJECT_NAME} STATIC ${DAP_MODULES_DYNAMIC_CDB_SRCS} ${DAP_MODULES_DYNAMIC_CDB_HEADERS})
-
-#target_link_libraries(${PROJECT_NAME} dap_chain_net_srv dap_chain_net_srv_vpn dap_crypto dap_chain dap_chain_net dap_chain_wallet zip)
-target_link_libraries(${PROJECT_NAME} dap_core dap_http_server ${CMAKE_DL_LIBS})
-
-target_include_directories(${PROJECT_NAME} INTERFACE .)
-target_include_directories(${PROJECT_NAME} PUBLIC include)
diff --git a/modules/modules_dynamic/cdb/dap_modules_dynamic_cdb.c b/modules/modules_dynamic/cdb/dap_modules_dynamic_cdb.c
deleted file mode 100644
index 3457c9f8d0..0000000000
--- a/modules/modules_dynamic/cdb/dap_modules_dynamic_cdb.c
+++ /dev/null
@@ -1,102 +0,0 @@
-/*
- * Authors:
- * Aleksei I. Voronin <aleksei.voronin@demlabs.net>
- * DeM Labs Inc.   https://demlabs.net
- * Kelvin Project https://github.com/kelvinblockchain
- * Copyright  (c) 2017-2019
- * All rights reserved.
-
- This file is part of DAP (Deus Applications Prototypes) the open source project
-
-    DAP (Deus Applicaions Prototypes) is free software: you can redistribute it and/or modify
-    it under the terms of the GNU General Public License as published by
-    the Free Software Foundation, either version 3 of the License, or
-    (at your option) any later version.
-
-    DAP is distributed in the hope that it will be useful,
-    but WITHOUT ANY WARRANTY; without even the implied warranty of
-    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-    GNU General Public License for more details.
-
-    You should have received a copy of the GNU General Public License
-    along with any DAP based project.  If not, see <http://www.gnu.org/licenses/>.
-*/
-
-#include "dap_modules_dynamic_cdb.h"
-#include "dap_common.h"
-#include "dap_config.h"
-
-#ifdef DAP_OS_LINUX
-#include <dlfcn.h>
-#endif
-
-#define LOG_TAG "dap_modules_dynamic"
-
-extern dap_config_t *g_config;
-
-
-static const char * s_default_path_modules = "var/modules";
-static void *s_cdb_handle = NULL;
-static bool s_cdb_was_init = false;
-
-void dap_modules_dynamic_close_cdb()
-{
-    if (s_cdb_handle) {
-#if defined (DAP_OS_LINUX) && !defined (__ANDROID__)
-        dlclose(s_cdb_handle);
-#endif
-        s_cdb_handle = NULL;
-    }
-    s_cdb_was_init = false;
-}
-
-void *dap_modules_dynamic_get_cdb_func(const char *a_func_name)
-{
-    if (!s_cdb_was_init)
-        return NULL;
-    char l_lib_path[MAX_PATH] = {'\0'};
-    void *l_ref_func = NULL;
-    //  find func from dynamic library
-#if defined (DAP_OS_LINUX) && !defined (__ANDROID__)
-    const char * l_cdb_so_name = "libcellframe-node-cdb.so";
-    if (!s_cdb_handle) {
-        dap_sprintf(l_lib_path, "%s/%s/%s", g_sys_dir_path, s_default_path_modules, l_cdb_so_name);
-
-        s_cdb_handle = dlopen(l_lib_path, RTLD_NOW);
-        if (!s_cdb_handle) {
-            log_it(L_ERROR,"Can't load %s module: %s", l_cdb_so_name, dlerror());
-            return NULL;
-        }
-    }
-
-    l_ref_func = dlsym(s_cdb_handle, a_func_name);
-
-    if (!l_ref_func) {
-        log_it(L_ERROR,"%s module: %s error loading (%s)", l_cdb_so_name, a_func_name, dlerror());
-        return NULL;
-    }
-#else
-    log_it(L_ERROR,"%s: module is not supported on current platfrom", __PRETTY_FUNCTION__);
-#endif
-    return l_ref_func;
-}
-
-int dap_modules_dynamic_load_cdb(dap_http_t * a_server)
-{
-    s_cdb_was_init = true;
-    int (*dap_chain_net_srv_vpn_cdb_init)(dap_http_t *, dap_config_t *);
-    dap_chain_net_srv_vpn_cdb_init = dap_modules_dynamic_get_cdb_func("dap_chain_net_srv_vpn_cdb_init");
-    if (!dap_chain_net_srv_vpn_cdb_init) {
-        s_cdb_was_init = false;
-        log_it(L_ERROR, "dap_modules_dynamic: dap_chain_net_srv_vpn_cdb_init not found");
-        return -2;
-    }
-    int l_init_res = dap_chain_net_srv_vpn_cdb_init(a_server, g_config);
-    if (l_init_res) {
-        s_cdb_was_init = false;
-        log_it(L_ERROR, "dap_modules_dynamic: dap_chain_net_srv_vpn_cdb_init returns %d", l_init_res);
-        return -3;
-    }
-    s_cdb_was_init = true;
-    return 0;
-}
diff --git a/modules/modules_dynamic/cdb/include/dap_modules_dynamic_cdb.h b/modules/modules_dynamic/cdb/include/dap_modules_dynamic_cdb.h
deleted file mode 100644
index 5f395b833a..0000000000
--- a/modules/modules_dynamic/cdb/include/dap_modules_dynamic_cdb.h
+++ /dev/null
@@ -1,30 +0,0 @@
-/*
- * Authors:
- * Aleksei I. Voronin <aleksei.voronin@demlabs.net>
- * DeM Labs Inc.   https://demlabs.net
- * Kelvin Project https://github.com/kelvinblockchain
- * Copyright  (c) 2017-2019
- * All rights reserved.
-
- This file is part of DAP (Deus Applications Prototypes) the open source project
-
-    DAP (Deus Applicaions Prototypes) is free software: you can redistribute it and/or modify
-    it under the terms of the GNU General Public License as published by
-    the Free Software Foundation, either version 3 of the License, or
-    (at your option) any later version.
-
-    DAP is distributed in the hope that it will be useful,
-    but WITHOUT ANY WARRANTY; without even the implied warranty of
-    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-    GNU General Public License for more details.
-
-    You should have received a copy of the GNU General Public License
-    along with any DAP based project.  If not, see <http://www.gnu.org/licenses/>.
-*/
-
-#pragma once
-#include "dap_http.h"
-
-int dap_modules_dynamic_load_cdb(dap_http_t * a_server);
-void *dap_modules_dynamic_get_cdb_func(const char *a_func_name);
-void dap_modules_dynamic_close_cdb();
-- 
GitLab