diff --git a/.gitignore b/.gitignore index 1ad5473be9b293b8a38a14161e0a8d2c6717bd3e..b2d5801ceba70247eda22e1b3c304a81bfa64409 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,6 @@ build/ +testdir/ +*.text _skbuild/ dist/ CellFrame.egg-info/ diff --git a/CMakeLists.txt b/CMakeLists.txt index edcbaa62adc9c31d22e10f0b12ca9631354c2780..5c8f0fb7d69044b52a34ff8970ada797177c1eb7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -11,8 +11,8 @@ if (UNIX) endif() add_definitions("-DDAP_LOG_MT") -add_definitions("-DNODE_NETNAME=\"cellframe\"") -add_definitions("-DDAP_APP_NAME=\"pythonCellframe\"") +#add_definitions("-DNODE_NETNAME=\"cellframe\"") +#add_definitions("-DDAP_APP_NAME=\"pythonCellframe\"") add_subdirectory(libdap) target_compile_options(dap_core PRIVATE "-fpic") @@ -141,6 +141,7 @@ target_link_libraries(${PROJECT_NAME} dap_python_module DapServerCore dap_chain_python_module dap_chain_net_python_module + dap_chain_gdb dap_chain_cs_dag dap_chain_cs_dag_pos diff --git a/include/generator_config_file.h b/include/generator_config_file.h new file mode 100644 index 0000000000000000000000000000000000000000..56b9c014710ef4930894cbee92547ad44438cd7f --- /dev/null +++ b/include/generator_config_file.h @@ -0,0 +1,42 @@ +#include <Python.h> +#include "dap_common.h" +#include "dap_string.h" +#include"dap_file_utils.h" + +#ifdef __cplusplus +extern "C" { +#endif + +#undef LOG_TAG +#define LOG_TAG "generator_config_file" + +char* getCharFromPyObject(PyObject *obj); + +/** + * @brief writeSectorToFile + * @param file + * @param sector + * @param exception + */ +void writeSectorToFile(FILE *file, PyObject *sector); + +/** + * @brief writeSectorsToFile + * @param file + * @param sectors + * @param count + */ +void writeSectorsToFile(FILE *file, PyObject *sectors, int count, ...); + +/** + * @brief gen_config_files + * @param cfgDir + * @param app_name + * @param cfg_JSON + * @return + */ +int gen_config_files(const char *cfgDir, const char *app_name, PyObject *cfg_JSON); + +#ifdef __cplusplus +} +#endif diff --git a/include/python-cellframe.h b/include/python-cellframe.h index dfb788c9ec007f0f298e5e6ef9b2df2e1ba37138..014fb9385e586cf29ef6351309747b9e376276e5 100644 --- a/include/python-cellframe.h +++ b/include/python-cellframe.h @@ -50,6 +50,10 @@ #include "dap_chain_gdb.h" #include "libdap_chain_net_python.h" +#include "dap_file_utils.h" +#include "dap_string.h" +#include "generator_config_file.h" + #include "dap_common.h" @@ -67,6 +71,7 @@ static bool init_stream_ctl; static bool init_http_folder; static bool init_http; static bool init_http_enc; +static bool init_server_core; static bool init_mempool; static bool init_http_client_simple; diff --git a/libdap b/libdap index 45c54da0b1033098715fdeb1ebf90b47e7003511..9c1afb9e17817deb61ecafdddd415d249b6b6547 160000 --- a/libdap +++ b/libdap @@ -1 +1 @@ -Subproject commit 45c54da0b1033098715fdeb1ebf90b47e7003511 +Subproject commit 9c1afb9e17817deb61ecafdddd415d249b6b6547 diff --git a/libdap-chain-global-db b/libdap-chain-global-db index 8907deee30e3bdc01d6991b5d409a070fab48384..71b87bfae6cc5d9c44e1f15478d71476cbfdd432 160000 --- a/libdap-chain-global-db +++ b/libdap-chain-global-db @@ -1 +1 @@ -Subproject commit 8907deee30e3bdc01d6991b5d409a070fab48384 +Subproject commit 71b87bfae6cc5d9c44e1f15478d71476cbfdd432 diff --git a/libdap-chain-net b/libdap-chain-net index a3646f9eb51b892bd92bd30d16e0c97e5ae1dbcb..c3c5901b40ad4aa5cd6babca83d5b8ec2f23050a 160000 --- a/libdap-chain-net +++ b/libdap-chain-net @@ -1 +1 @@ -Subproject commit a3646f9eb51b892bd92bd30d16e0c97e5ae1dbcb +Subproject commit c3c5901b40ad4aa5cd6babca83d5b8ec2f23050a diff --git a/libdap-chain-net-srv b/libdap-chain-net-srv index fe315f31c9bc668c342c295a4298281c61f2a169..ea8d445e4a92d615d71112e41121af064aeed14f 160000 --- a/libdap-chain-net-srv +++ b/libdap-chain-net-srv @@ -1 +1 @@ -Subproject commit fe315f31c9bc668c342c295a4298281c61f2a169 +Subproject commit ea8d445e4a92d615d71112e41121af064aeed14f diff --git a/libdap-chain-net-srv-vpn b/libdap-chain-net-srv-vpn index b1d19f4bf3452077fae5e3e8c8bc8e48b1e4cc16..b67f362e974daf9980d1832357d59030ef45952a 160000 --- a/libdap-chain-net-srv-vpn +++ b/libdap-chain-net-srv-vpn @@ -1 +1 @@ -Subproject commit b1d19f4bf3452077fae5e3e8c8bc8e48b1e4cc16 +Subproject commit b67f362e974daf9980d1832357d59030ef45952a diff --git a/libdap-chain-python b/libdap-chain-python index 5801131107cc799b31482dc1b02d92d00d8cfb2e..95cbc3329247ee600361e6f0963e838a7df79aa0 160000 --- a/libdap-chain-python +++ b/libdap-chain-python @@ -1 +1 @@ -Subproject commit 5801131107cc799b31482dc1b02d92d00d8cfb2e +Subproject commit 95cbc3329247ee600361e6f0963e838a7df79aa0 diff --git a/libdap-chain-wallet b/libdap-chain-wallet index 881cadba56a912e9ec52f5f10d8cc9ec510f556b..20b720870059589bd1e1b079426d1777bdfac91a 160000 --- a/libdap-chain-wallet +++ b/libdap-chain-wallet @@ -1 +1 @@ -Subproject commit 881cadba56a912e9ec52f5f10d8cc9ec510f556b +Subproject commit 20b720870059589bd1e1b079426d1777bdfac91a diff --git a/libdap-crypto b/libdap-crypto index 699139db02bf4079269cda9c1e65c1d8efec2c5d..0c0c9cd65736c4f129fb477cf4ddec2ecfe51add 160000 --- a/libdap-crypto +++ b/libdap-crypto @@ -1 +1 @@ -Subproject commit 699139db02bf4079269cda9c1e65c1d8efec2c5d +Subproject commit 0c0c9cd65736c4f129fb477cf4ddec2ecfe51add diff --git a/libdap-server-core b/libdap-server-core index 3536156bde9494a0215d56283c0739c80b87ffe4..686c334f320154c2abb1faedacfd8a60be6ae5e4 160000 --- a/libdap-server-core +++ b/libdap-server-core @@ -1 +1 @@ -Subproject commit 3536156bde9494a0215d56283c0739c80b87ffe4 +Subproject commit 686c334f320154c2abb1faedacfd8a60be6ae5e4 diff --git a/libdap-stream b/libdap-stream index 895ece6e7bc594ac47ff5506c63f598f988b8132..4528b31243af178fb981c5caeec5e281fb6e9b82 160000 --- a/libdap-stream +++ b/libdap-stream @@ -1 +1 @@ -Subproject commit 895ece6e7bc594ac47ff5506c63f598f988b8132 +Subproject commit 4528b31243af178fb981c5caeec5e281fb6e9b82 diff --git a/libdap-stream-ch b/libdap-stream-ch index 3bdf48f5401b54b582c2d28b71c9e596bcc6c090..ad0f3e360023e023cd5cfa312db2dd3c441f8b66 160000 --- a/libdap-stream-ch +++ b/libdap-stream-ch @@ -1 +1 @@ -Subproject commit 3bdf48f5401b54b582c2d28b71c9e596bcc6c090 +Subproject commit ad0f3e360023e023cd5cfa312db2dd3c441f8b66 diff --git a/libdap-stream-ch-chain b/libdap-stream-ch-chain index 1277331831fe417815eca352a0e03c7cac54d908..496ff626c4c17bbdf103f9b49b01a4fc95a5bf7c 160000 --- a/libdap-stream-ch-chain +++ b/libdap-stream-ch-chain @@ -1 +1 @@ -Subproject commit 1277331831fe417815eca352a0e03c7cac54d908 +Subproject commit 496ff626c4c17bbdf103f9b49b01a4fc95a5bf7c diff --git a/libdap-stream-ch-chain-net-srv b/libdap-stream-ch-chain-net-srv index e70339a6784aa37b0923804330dbdccae770556d..fdf6603ba778bee50131ec3cfacfbabe79d1651b 160000 --- a/libdap-stream-ch-chain-net-srv +++ b/libdap-stream-ch-chain-net-srv @@ -1 +1 @@ -Subproject commit e70339a6784aa37b0923804330dbdccae770556d +Subproject commit fdf6603ba778bee50131ec3cfacfbabe79d1651b diff --git a/src/generator_config_file.c b/src/generator_config_file.c new file mode 100644 index 0000000000000000000000000000000000000000..7ab4d6abbfa6153d937f6dd1cb3c3798bd06bb32 --- /dev/null +++ b/src/generator_config_file.c @@ -0,0 +1,169 @@ +#include "generator_config_file.h" + +static void writeGroupInFile(FILE *file, char *group){ + fputs("[", file); + fputs(group, file); + fputs("]\n", file); +} + +static void writeKeyAndValueInFile(FILE *file, char *key, char *value){ + fputs(key, file); + fputs("=", file); + fputs(value, file); + fputs("\n", file); +} + + +char* getCharFromPyObject(PyObject *obj){ + char *res = "NULL\0"; + if (PyUnicode_Check(obj)){ + res = strdup(PyUnicode_AsUTF8(obj)); + } + if (PyLong_Check(obj)){ + int i = _PyLong_AsInt(obj); + res = dap_itoa(i); + } + if (PyBool_Check(obj)){ + res = (obj == Py_True) ? "true\0" : "false\0"; + } + if (PyBytes_Check(obj)){ + size_t size_bytes = (size_t)PyBytes_Size(obj); + res = calloc(size_bytes+1, sizeof(char)); + memcpy(res, PyBytes_AsString(obj), size_bytes); + res[size_bytes+1] = '\0'; + } + if (PyList_Check(obj)){ + dap_string_t *str = dap_string_new("["); + Py_ssize_t size_list = PyList_Size(obj); + for (Py_ssize_t i = 0; i < size_list;i++){ + char *d = getCharFromPyObject(PyList_GetItem(obj, i)); + str = dap_string_append(str, d); + if (i != size_list - 1){ + str = dap_string_append(str, ","); + } + } + str = dap_string_append(str, "]"); + res = strdup(str->str); + dap_string_free(str, true); + } + return res; +} + +void writeContectSectorToFile(FILE *file, PyObject *sector){ + PyObject *list_keys = PyDict_Keys(sector); + Py_ssize_t count_keys = PyList_Size(list_keys); + char *key; + PyObject *obj_value; + char *value; + for (Py_ssize_t i = 0; i < count_keys; i++){ + key = getCharFromPyObject(PyList_GetItem(list_keys, i)); + obj_value = PyDict_GetItemString(sector, key); + value = getCharFromPyObject(obj_value); + writeKeyAndValueInFile(file, key, value); + } +} + +void writeSectorsToFile(FILE *file, PyObject *sectors, int count, ...){ + PyObject *list_sectors = PyDict_Keys(sectors); + Py_ssize_t count_sectors = PyList_Size(list_sectors); + char *name_sector; + PyObject *sector; + bool _this_obj_not_processes; + for (Py_ssize_t i = 0; i < count_sectors; i++){ + name_sector = getCharFromPyObject(PyList_GetItem(list_sectors, i)); + va_list args; + va_start(args, count); + for (int l=0; l < count;l++){ + _this_obj_not_processes = true; + if (strcmp(name_sector, va_arg(args, char*)) == 0){ + _this_obj_not_processes = false; + va_end(args); + break; + } + } + if (!_this_obj_not_processes){ + break; + } + va_end(args); + sector = PyDict_GetItemString(sectors, name_sector); + writeGroupInFile(file, name_sector); + writeContectSectorToFile(file, sector); + } +} + +void createConfNetworks(dap_string_t *base_path, PyObject *nets){ + PyObject *name_nets = PyDict_Keys(nets); + Py_ssize_t count_nets = PyList_Size(name_nets); + char *name_net; + PyObject *obj_net; + dap_string_t *path_net; + dap_string_t *base_path_net_file; + FILE *file_net; + PyObject *list_files; + PyObject *obj_conf_files; + Py_ssize_t count_list_file; + dap_string_t *path_cfg_net_file; + char *name_cfg_net_file; + FILE *cfg_net_file; + for (Py_ssize_t i =0; i < count_nets; i++){ + name_net = getCharFromPyObject(PyList_GetItem(name_nets, i)); + path_net = dap_string_new(base_path->str); + path_net = dap_string_append(path_net, "/"); + path_net = dap_string_append(path_net, name_net); + base_path_net_file = dap_string_new(path_net->str); + base_path_net_file = dap_string_append(base_path_net_file, ".cfg"); + path_net = dap_string_append(path_net, "/"); + if (!dap_valid_ascii_symbols(path_net->str)){ + return; + } + dap_mkdir_with_parents(path_net->str); + if ((file_net = fopen(base_path_net_file->str, "w")) == NULL){ + return; + } + obj_net = PyDict_GetItemString(nets, name_net); + writeSectorsToFile(file_net, obj_net, 2, "name_cfg_files", "conf_files"); + fclose(file_net); + list_files = PyDict_GetItemString(obj_net, "name_cfg_files"); + count_list_file = PyList_Size(list_files); + obj_conf_files = PyDict_GetItemString(obj_net, "conf_files"); + for (Py_ssize_t index = 0; index < count_list_file; index++){ + name_cfg_net_file = getCharFromPyObject(PyList_GetItem(list_files, index)); + path_cfg_net_file = dap_string_new(path_net->str); + path_cfg_net_file = dap_string_append(path_cfg_net_file, name_cfg_net_file); + path_cfg_net_file = dap_string_append(path_cfg_net_file, ".cfg"); + if ((cfg_net_file = fopen(path_cfg_net_file->str, "w")) == NULL){ + return; + } + writeSectorsToFile(cfg_net_file, PyDict_GetItemString(obj_conf_files, name_cfg_net_file), 0); + fclose(cfg_net_file); + dap_string_free(path_cfg_net_file, true); + } + dap_string_free(base_path_net_file, true); + dap_string_free(path_net, true); + } +} + +int gen_config_files(const char *cfgDir, const char *app_name, PyObject *cfg_JSON){ + dap_string_t *string_cfg_dir = dap_string_new(cfgDir); + dap_string_t *networks_dir = dap_string_new(string_cfg_dir->str); + networks_dir = dap_string_append(networks_dir, "/network"); + if (!dap_valid_ascii_symbols(cfgDir)){ + return -1; + } + dap_mkdir_with_parents(networks_dir->str); + dap_string_t *main_cfg_file = dap_string_new(string_cfg_dir->str); + main_cfg_file = dap_string_append(main_cfg_file, "/"); + main_cfg_file = dap_string_append(main_cfg_file, app_name); + main_cfg_file = dap_string_append(main_cfg_file, ".cfg"); + FILE *main_file; + if ((main_file = fopen(main_cfg_file->str, "w")) == NULL){ + return -3; + } + writeSectorsToFile(main_file, cfg_JSON, 1, "networks"); + fclose(main_file); + createConfNetworks(networks_dir, PyDict_GetItemString(cfg_JSON, "networks")); + dap_string_free(main_cfg_file, true); + dap_string_free(networks_dir, true); + dap_string_free(string_cfg_dir, true); + return 0; +} diff --git a/src/python-cellframe.c b/src/python-cellframe.c index 8a4db5386eb8bc964af2135e852aedb5a8d13a5f..68dc7f60a1d34347490cb50a3480a4bf51ceff66 100644 --- a/src/python-cellframe.c +++ b/src/python-cellframe.c @@ -61,7 +61,19 @@ static PyObject *python_cellframe_init(PyObject *self, PyObject *args){ PyErr_SetString(CellFrame_error, "Can't init common functions module"); return NULL; } - + dap_set_appname(app_name); + //generation config files + PyObject *configure = PyDict_GetItemString(result, "Configuration"); + int res_gen_config_file = gen_config_files(config_dir, app_name, configure); + switch (res_gen_config_file) { + case -1: + PyErr_SetString(CellFrame_error, "Can't generate configuration files. Can name directory path contains not ASCII simbols."); + return NULL; + case -3: + PyErr_SetString(CellFrame_error, "Can't generate configuration files. Can't open file stream."); + return NULL; + } + //Init config dap_config_init(config_dir); if ((g_config = dap_config_open(app_name) ) == NULL){ PyErr_SetString(CellFrame_error, "Can't init general configurations"); @@ -109,6 +121,7 @@ static PyObject *python_cellframe_init(PyObject *self, PyObject *args){ PyErr_SetString(CellFrame_error, "Failed to initialize ServerCore."); return NULL; } + init_server_core = true; } if (strcmp(c_value, "Chain") == 0){ if(init_chain_py() != 0){ @@ -400,8 +413,6 @@ PyMODINIT_FUNC PyInit_CellFrame(void){ } static PyObject *python_cellframe_deinit(PyObject *self, PyObject *args){ - dap_config_close(g_config); - dap_config_deinit(); if (init_crypto) dap_crypto_deinit(); if (init_chain){ @@ -420,9 +431,15 @@ static PyObject *python_cellframe_deinit(PyObject *self, PyObject *args){ if (init_http){ dap_http_deinit(); } + if (init_server_core){ + dap_server_core_deinit(); + } if (init_ks){ dap_enc_ks_deinit(); } + dap_config_close(g_config); + dap_config_deinit(); + dap_common_deinit(); return PyLong_FromLong(0); } diff --git a/test/main_test.py b/test/main_test.py index d9baa5cba7fa8f567c2a423de9178daf74c62877..ea75ac17159c2940d72176ed565b49e137ea1106 100644 --- a/test/main_test.py +++ b/test/main_test.py @@ -2,6 +2,7 @@ from CellFrame import * import pickle import os import sys +from time import sleep def create_config_file(app_name): f = open(app_name+".cfg", "w") @@ -10,37 +11,102 @@ def create_config_file(app_name): print("Start main test") app_name = "testAPP" -print("Create config file") -create_config_file(app_name) - -#json_string = """{ -# "modules": ["Crypto", "ServerCore" ], -# "DAP": { -# "config_dir": \""""+os.getcwd()+"""\", -# "log_level": "L_DEBUG", -# "application_name": \""""+app_name+"""\", -# "file_name_log": \""""+app_name+""".text\" -# }, -# "Stream" : { -# "DebugDumpStreamHeaders": false -# }, -# "ServerCore" : { -# "thread_cnt": 0, -# "conn": 0 -# } -# }""" +dir_cfg = os.getcwd() + "/testdir" +print (dir_cfg) json_string = """{ "modules": ["Crypto", "ServerCore", "Http", "HttpFolder", "GlobalDB", "Client", "HttpClientSimple", "Mempool", "Chain", "Wallet", "ChainCSDag", "ChainCSDagPoa", "ChainCSDagPos", "GDB", "Net", "ChainNetSrv", "EncHttp", "Stream", "StreamCtl", "HttpSimple", "StreamChChain", "StreamChChainNet", "StreamChChainNetSrv"], "DAP": { - "config_dir": \""""+os.getcwd()+"""\", + "config_dir": \""""+dir_cfg+"""\", "log_level": "L_DEBUG", "application_name": \""""+app_name+"""\", "file_name_log": \""""+app_name+""".text\" }, + "Configuration" : { + "general": { + "debug_mode": false, + "debug_dump_stream_headers": false, + "wallets_default": "default" + }, + "server": { + "enabled": false, + "listen_address": "0.0.0.0", + "listen_port_tcp": 8079 + }, + "mempool": { + "accept": false + }, + "cdb": { + "enabled": false, + "db_path": "mongodb://localhost/db", + "servers_list_enabled": false, + "servers_list_networks": ["kelvin-testnet", "private"] + }, + "cdb_auth": { + "enabled": false, + "collection_name": "mycollection", + "domain": "mydomain", + "tx_cond_create": false + }, + "srv_vpn": { + "enabled": false, + "network_address": "10.11.12.0", + "network_mask": "255.255.255.0", + "pricelist": [ + "kelvin-testnet:0.00001:KELT:3600:SEC:mywallet0", "kelvin-testnet:0.00001:cETH:3600:SEC:mywallet1", "private:1:WOOD:10:SEC:mywallet0" + ] + }, + "conserver": { + "enabled": true, + "listen_unix_socket_path": \""""+dir_cfg+"""/run/node_cli\" + }, + "resources": { + "threads_cnt": 0, + "pid_path": \""""+dir_cfg+"""/run/cellframe-node.pid\", + "log_file": \""""+dir_cfg+"""/log/cellframe-node.log\", + "wallets_path": \""""+dir_cfg+"""/lib/wallet\", + "ca_folders": [ + \""""+dir_cfg+"""/lib/ca\", + \""""+dir_cfg+"""/share/ca\" + ], + "dap_global_db_path": \""""+dir_cfg+"""/lib/global_db\", + "dap_global_db_driver": "cdb" + }, + "networks":{ + "private": { + "general":{ + "id": "0xFF00000000000001", + "name": "private", + "type": "testing", + "node-role": "full", + "gdb_groups_prefix": "private", + "node-addr-expired": 168, + "seed_nodes_ipv4": ["153.256.133.160", "62.216.90.227"], + "seed_nodes_port": [8079, 8079], + "seed_nodes_aliases": ["kelvin.testnet.root.0", "kelvin.testnet.root.1"], + "seed_nodes_addrs": ["ffff::0000::0000::0001","ffff::0000::0000::0002"] + }, + "name_cfg_files": ["chain-gdb"], + "conf_files":{ + "chain-gdb": { + "general": { + "id": "0xf00000000000000f", + "name": "gdb", + "consensus": "gdb", + "class": "gdb", + "datum_types": ["token", "emission", "shard", "ca", "transaction"] + }, + "gdb":{ + "celled": false + } + } + } + } + } + }, "Stream" : { "DebugDumpStreamHeaders": false }, @@ -134,11 +200,10 @@ else: sys.exit(1) - +sleep deinit() logItInfo("Deinitialization done") -os.remove(app_name+".cfg") logItInfo( "Main test done"); sys.exit(0)