From b1996a8c3dddcb7d1b7acb3f05874c414a9b3177 Mon Sep 17 00:00:00 2001 From: "alexey.stratulat" <alexey.stratulat@demlabs.net> Date: Wed, 11 May 2022 13:13:14 +0000 Subject: [PATCH] Features-6081 --- CellFrame/python-cellframe_common.c | 34 ++++++ include/python-cellframe_common.h | 7 ++ .../include/wrapping_dap_chain_datum_token.h | 4 +- .../include/wrapping_dap_chain_datum_tx.h | 2 + .../include/wrapping_dap_chain_tx_sig.h | 6 +- .../src/wrapping_dap_chain_datum_token.c | 4 + .../common/src/wrapping_dap_chain_datum_tx.c | 4 + .../mempool/src/wrapping_dap_mempool.c | 2 +- modules/cellframe-sdk/net/CMakeLists.txt | 2 +- .../net/include/wrapping_dap_app_cli_server.h | 1 + .../net/src/wrapping_dap_app_cli_server.c | 2 +- .../srv/include/wrapping_dap_chain_net_srv.h | 1 + .../net/srv/src/wrapping_dap_chain_net_srv.c | 10 +- modules/dap-sdk/core/src/libdap-python.c | 90 +++++++++++---- modules/dap-sdk/core/src/libdap-python.h | 1 + modules/dap-sdk/crypto/CMakeLists.txt | 5 +- .../crypto/include/wrapping_dap_hash.h | 5 + .../crypto/include/wrapping_dap_sign.h | 7 ++ .../dap-sdk/crypto/src/wrapping_dap_hash.c | 79 ++++++++++++- .../dap-sdk/crypto/src/wrapping_dap_sign.c | 104 +++++++++++++++++- .../http/src/wrapping_dap_http_simple.c | 5 +- .../net/server/json_rpc/CMakeLists.txt | 1 + .../include/wrapping_json_rpc_request.h | 1 + .../json_rpc/src/wrapping_json_rpc_request.c | 2 +- modules/plugins/src/dap_chain_plugins.c | 12 +- 25 files changed, 346 insertions(+), 45 deletions(-) create mode 100644 CellFrame/python-cellframe_common.c create mode 100644 include/python-cellframe_common.h diff --git a/CellFrame/python-cellframe_common.c b/CellFrame/python-cellframe_common.c new file mode 100644 index 00000000..546085a7 --- /dev/null +++ b/CellFrame/python-cellframe_common.c @@ -0,0 +1,34 @@ +#include "python-cellframe_common.h" + +void _PyErr_logIt(const dap_log_level_t a_level, const char *a_tag, const char *a_msg){ + _log_it(a_tag, a_level, (char*)a_msg); +} + +char* _PyErr_get_stacktrace(PyObject *a_obj){ + assert(PyTraceBack_Check(a_obj)); + PyTracebackObject *l_traceback = (PyTracebackObject*)a_obj; + char *s = "\tStack trace:\n"; + size_t cnt = 0; + while (l_traceback != NULL) { + PyCodeObject *l_code = PyFrame_GetCode(l_traceback->tb_frame); + const char *l_name = PyUnicode_AsUTF8(l_code->co_name); + const char *l_file = PyUnicode_AsUTF8(l_code->co_filename); + int l_lineo = ((PyTracebackObject *) a_obj)->tb_lineno; + s = dap_strdup_printf("%s\t\t(%zu) File \"%s\", line %d, in %s\n", s, cnt, l_file, l_lineo, l_name); + l_traceback = l_traceback->tb_next; + cnt++; + } + return s; +} + +void python_error_in_log_it(const char *a_tag){ + PyObject *type, *value, *trackback; + PyErr_Fetch(&type, &value, &trackback); + const char *l_str_value = PyUnicode_AsUTF8(value); + const char *l_str_type = PyExceptionClass_Name(type); + _PyErr_logIt(L_ERROR, a_tag, dap_strdup_printf( + "An exception occurred while executing a Python script.\n" + "\t%s: %s\n%s", l_str_type, l_str_value, _PyErr_get_stacktrace(trackback) + )); + PyErr_Restore(type, value, trackback); +} diff --git a/include/python-cellframe_common.h b/include/python-cellframe_common.h new file mode 100644 index 00000000..b3a1d11b --- /dev/null +++ b/include/python-cellframe_common.h @@ -0,0 +1,7 @@ +#pragma once + +#include "Python.h" +#include "dap_common.h" +#include "dap_strfuncs.h" + +void python_error_in_log_it(const char *a_tag); diff --git a/modules/cellframe-sdk/common/include/wrapping_dap_chain_datum_token.h b/modules/cellframe-sdk/common/include/wrapping_dap_chain_datum_token.h index 99376a70..bd7e00a0 100644 --- a/modules/cellframe-sdk/common/include/wrapping_dap_chain_datum_token.h +++ b/modules/cellframe-sdk/common/include/wrapping_dap_chain_datum_token.h @@ -48,6 +48,8 @@ PyObject *wrapping_dap_chain_datum_token_get_type_str(PyObject *self, void *clos //PyObject *wrapping_dap_chain_datum_token_get_size(PyObject *self, void *closure); PyObject *wrapping_dap_chain_datum_token_get_data(PyObject *self, void *closure); +bool DapChainDatumToken_Check(PyObject *self); + extern PyTypeObject DapChainDatumTokenObjectType; /* ------------------------------------------- */ @@ -78,7 +80,7 @@ PyObject *wrapping_dap_chain_datum_emission_get_tsd(PyObject*self, PyObject *arg extern PyTypeObject DapChainDatumTokenEmissionObjectType; -DAP_STATIC_INLINE bool PyDapChainDatumTokenEmissionObject_check(PyDapChainDatumTokenEmissionObject *self){ +static bool PyDapChainDatumTokenEmissionObject_check(PyObject *self){ return PyObject_TypeCheck(self, &DapChainDatumTokenEmissionObjectType); } diff --git a/modules/cellframe-sdk/common/include/wrapping_dap_chain_datum_tx.h b/modules/cellframe-sdk/common/include/wrapping_dap_chain_datum_tx.h index 513893c2..4e1415f3 100644 --- a/modules/cellframe-sdk/common/include/wrapping_dap_chain_datum_tx.h +++ b/modules/cellframe-sdk/common/include/wrapping_dap_chain_datum_tx.h @@ -106,6 +106,8 @@ PyObject *wrapping_dap_chain_datum_tx_get_items(PyObject *self, PyObject *args); PyObject *wrapping_dap_chain_datum_tx_get_hash(PyObject *self, void* closure); PyObject *wrapping_dap_chain_datum_tx_get_tsCreated(PyObject *self, void* closure); +bool DapChainDatumTx_Check(PyObject *self); + extern PyTypeObject DapChainDatumTxObjectType; /* -------------------------------------- */ diff --git a/modules/cellframe-sdk/common/include/wrapping_dap_chain_tx_sig.h b/modules/cellframe-sdk/common/include/wrapping_dap_chain_tx_sig.h index 7f00ea5b..876e8f88 100644 --- a/modules/cellframe-sdk/common/include/wrapping_dap_chain_tx_sig.h +++ b/modules/cellframe-sdk/common/include/wrapping_dap_chain_tx_sig.h @@ -25,7 +25,7 @@ #ifndef _WRAPPING_DAP_CHAIN_TX_SIG #define _WRAPPING_DAP_CHAIN_TX_SIG -#endif + #include <Python.h> #include "dap_chain_datum_tx_sig.h" @@ -48,4 +48,6 @@ extern PyTypeObject DapChainTxSigObject_DapChainTxSigTypeObjectType; //#ifdef __cplusplus //} -//#endif \ No newline at end of file +//#endif + +#endif //_WRAPPING_DAP_CHAIN_TX_SIG \ No newline at end of file diff --git a/modules/cellframe-sdk/common/src/wrapping_dap_chain_datum_token.c b/modules/cellframe-sdk/common/src/wrapping_dap_chain_datum_token.c index b54664af..78182769 100644 --- a/modules/cellframe-sdk/common/src/wrapping_dap_chain_datum_token.c +++ b/modules/cellframe-sdk/common/src/wrapping_dap_chain_datum_token.c @@ -126,6 +126,10 @@ PyObject *wrapping_dap_chain_datum_token_get_data(PyObject *self, void *closure) return obj_dict; } +bool DapChainDatumToken_Check(PyObject *self){ + return PyObject_TypeCheck(self, &DapChainDatumTokenObjectType); +} + /* Token Emission */ PyGetSetDef PyDapChainDatumTokenEmissionGetsSetsDef[]={ {"hash", (getter)wrapping_dap_chain_datum_token_emission_get_hash, NULL, NULL, NULL}, diff --git a/modules/cellframe-sdk/common/src/wrapping_dap_chain_datum_tx.c b/modules/cellframe-sdk/common/src/wrapping_dap_chain_datum_tx.c index 1771bcb4..c29b85ee 100644 --- a/modules/cellframe-sdk/common/src/wrapping_dap_chain_datum_tx.c +++ b/modules/cellframe-sdk/common/src/wrapping_dap_chain_datum_tx.c @@ -206,6 +206,10 @@ PyTypeObject DapChainDatumTxObjectType = { PyDapChainDatumTxObject_create, /* tp_new */ }; +bool DapChainDatumTx_Check(PyObject *self){ + return PyObject_TypeCheck(self, &DapChainDatumTxObjectType); +} + PyObject *PyDapChainDatumTxObject_create(PyTypeObject *type_object, PyObject *args, PyObject *kwds){ PyDapChainDatumTxObject *obj = (PyDapChainDatumTxObject*)PyType_GenericNew(type_object, args, kwds); obj->datum_tx = dap_chain_datum_tx_create(); diff --git a/modules/cellframe-sdk/mempool/src/wrapping_dap_mempool.c b/modules/cellframe-sdk/mempool/src/wrapping_dap_mempool.c index 98d9cc44..8846285d 100644 --- a/modules/cellframe-sdk/mempool/src/wrapping_dap_mempool.c +++ b/modules/cellframe-sdk/mempool/src/wrapping_dap_mempool.c @@ -70,7 +70,7 @@ PyObject *wrapping_dap_mempool_emission_place(PyObject *self, PyObject *args){ "CellFrame.Chain.Chain."); return NULL; } - if (!PyDapChainDatumTokenEmissionObject_check(obj_emission)){ + if (!PyDapChainDatumTokenEmissionObject_check((PyObject*)obj_emission)){ PyErr_SetString(PyExc_AttributeError, "The second argument was incorrectly passed" " to this function, the second argument must be an object of " "type ChainDatumTokenEmission. "); diff --git a/modules/cellframe-sdk/net/CMakeLists.txt b/modules/cellframe-sdk/net/CMakeLists.txt index 6616ff36..fce4f2fa 100644 --- a/modules/cellframe-sdk/net/CMakeLists.txt +++ b/modules/cellframe-sdk/net/CMakeLists.txt @@ -197,7 +197,7 @@ target_link_libraries(${PROJECT_NAME}) #) -target_link_libraries(${PROJECT_NAME} dap_core dap_crypto dap_chain dap_server_core dap_chain_net dap_chain_python_module dap_client_python_module) +target_link_libraries(${PROJECT_NAME} dap_core dap_crypto dap_chain dap_server_core dap_chain_net dap_chain_python_module dap_client_python_module CellFrame) #target_link_libraries(${PROJECT_NAME} dap_core dap_crypto dap_chain dap_chain_global_db dap_chain_crypto dap_chain_mempool # # ) #dap_chain_crypto dap_chain_mempool dap_chain_global_db ) diff --git a/modules/cellframe-sdk/net/include/wrapping_dap_app_cli_server.h b/modules/cellframe-sdk/net/include/wrapping_dap_app_cli_server.h index 5d370e3c..b276e16e 100644 --- a/modules/cellframe-sdk/net/include/wrapping_dap_app_cli_server.h +++ b/modules/cellframe-sdk/net/include/wrapping_dap_app_cli_server.h @@ -7,6 +7,7 @@ #include "wrapping_dap_chain_common.h" #include "wrapping_dap_chain_net_node.h" #include "utlist.h" +#include "python-cellframe_common.h" typedef struct PyDapAppCliServer{ PyObject_HEAD diff --git a/modules/cellframe-sdk/net/src/wrapping_dap_app_cli_server.c b/modules/cellframe-sdk/net/src/wrapping_dap_app_cli_server.c index b924711d..50c39b07 100644 --- a/modules/cellframe-sdk/net/src/wrapping_dap_app_cli_server.c +++ b/modules/cellframe-sdk/net/src/wrapping_dap_app_cli_server.c @@ -159,7 +159,7 @@ static int wrapping_cmdfunc(int argc, char **argv, char **str_reply){ PyObject *result = PyObject_CallObject(binden_obj_cmdfunc, arglist); if (!result){ log_it(L_DEBUG, "Function can't be called"); - PyErr_Print(); + python_error_in_log_it(LOG_TAG); } Py_XDECREF(arglist); Py_XDECREF(obj_argv); diff --git a/modules/cellframe-sdk/net/srv/include/wrapping_dap_chain_net_srv.h b/modules/cellframe-sdk/net/srv/include/wrapping_dap_chain_net_srv.h index b7e52245..a9a99aa5 100644 --- a/modules/cellframe-sdk/net/srv/include/wrapping_dap_chain_net_srv.h +++ b/modules/cellframe-sdk/net/srv/include/wrapping_dap_chain_net_srv.h @@ -6,6 +6,7 @@ #include "uthash.h" #include "wrapping_dap_chain_common.h" #include "wrapping_dap_chain_net_srv_client_remote.h" +#include "python-cellframe_common.h" typedef struct PyDapChainNetSrv{ PyObject_HEAD diff --git a/modules/cellframe-sdk/net/srv/src/wrapping_dap_chain_net_srv.c b/modules/cellframe-sdk/net/srv/src/wrapping_dap_chain_net_srv.c index c6fa681d..07fe1c09 100644 --- a/modules/cellframe-sdk/net/srv/src/wrapping_dap_chain_net_srv.c +++ b/modules/cellframe-sdk/net/srv/src/wrapping_dap_chain_net_srv.c @@ -95,7 +95,7 @@ int _w_dap_chain_callback_data_t_requested( PyObject *result = PyObject_CallObject(l_func, l_arg); PyGILState_Release(state); if(result == NULL){ - PyErr_Print(); + python_error_in_log_it(LOG_TAG); return -1; } if (!PyLong_Check(result)){ @@ -121,7 +121,7 @@ int _w_dap_chain_callback_data_t_response_success( PyObject *result = PyObject_CallObject(l_func, l_arg); PyGILState_Release(state); if(result == NULL){ - PyErr_Print(); + python_error_in_log_it(LOG_TAG); return -1; } if (!PyLong_Check(result)){ @@ -148,7 +148,7 @@ int _w_dap_chain_callback_data_t_response_error( PyObject *result = PyObject_CallObject(l_func, l_arg); PyGILState_Release(state); if(result == NULL){ - PyErr_Print(); + python_error_in_log_it(LOG_TAG); return -1; } if (!PyLong_Check(result)){ @@ -175,7 +175,7 @@ int _w_dap_chain_callback_data_t_receipt_next_success( PyObject *result = PyObject_CallObject(l_func, l_arg); PyGILState_Release(state); if(result == NULL){ - PyErr_Print(); + python_error_in_log_it(LOG_TAG); return -1; } if (!PyLong_Check(result)){ @@ -204,7 +204,7 @@ void *_w_dap_chain_callback_data_t_custom_data(dap_chain_net_srv_t *a_srv, PyObject *result = PyObject_CallObject(l_func, l_arg); PyGILState_Release(state); if(result == NULL){ - PyErr_Print(); + python_error_in_log_it(LOG_TAG); return NULL; } if (!PyBytes_Check(result)){ diff --git a/modules/dap-sdk/core/src/libdap-python.c b/modules/dap-sdk/core/src/libdap-python.c index ae15428f..346022f4 100644 --- a/modules/dap-sdk/core/src/libdap-python.c +++ b/modules/dap-sdk/core/src/libdap-python.c @@ -96,86 +96,136 @@ PyObject *dap_set_log_level(PyObject *self, PyObject *args){ PyObject* dap_log_it(PyObject* self, PyObject* args){ short int log_level; const char* string_output; - if (!PyArg_ParseTuple(args, "h|s", &log_level, &string_output)) + const char* string_name_plugin = NULL; + if (!PyArg_ParseTuple(args, "hs|s", &log_level, &string_output, &string_name_plugin)) return NULL; if (log_level < 0 || log_level > 10 ) { return PyLong_FromLong(-1); } else { - log_it(log_level, string_output); + if (string_name_plugin){ + log_it(log_level, dap_strdup_printf("[plugin: %s] %s", string_output, string_name_plugin)); + } else { + log_it(log_level, string_output); + } return PyLong_FromLong(0); } } PyObject* dap_log_it_debug(PyObject* self, PyObject* args){ const char* string_output; - if (!PyArg_ParseTuple(args, "s", &string_output)){ + const char* string_name_plugin = NULL; + if (!PyArg_ParseTuple(args, "s|s", &string_output, &string_name_plugin)){ return NULL; } - log_it(L_DEBUG, string_output); + if (string_name_plugin) { + log_it(L_DEBUG, dap_strdup_printf("[plugin: %s] %s", string_output, string_name_plugin)); + } else { + log_it(L_DEBUG, string_output); + } return PyLong_FromLong(0); } PyObject* dap_log_it_info(PyObject* self, PyObject* args){ const char* string_output; - if (!PyArg_ParseTuple(args, "s", &string_output)){ + const char* string_name_plugin = NULL; + if (!PyArg_ParseTuple(args, "s|s", &string_output, &string_name_plugin)){ return NULL; } - log_it(L_INFO, string_output); + if (string_name_plugin) { + log_it(L_INFO, dap_strdup_printf("[plugin: %s] %s", string_output, string_name_plugin)); + } else { + log_it(L_INFO, string_output); + } return PyLong_FromLong(0); } PyObject* dap_log_it_notice(PyObject* self, PyObject* args){ const char* string_output; - if (!PyArg_ParseTuple(args, "s", &string_output)){ + const char* string_name_plugin = NULL; + if (!PyArg_ParseTuple(args, "s|s", &string_output, &string_name_plugin)){ return NULL; } - log_it(L_NOTICE, string_output); + if (string_name_plugin){ + log_it(L_NOTICE, dap_strdup_printf("[plugin: %s] %s", string_output, string_name_plugin)); + }else{ + log_it(L_NOTICE, string_output); + } return PyLong_FromLong(0); } PyObject* dap_log_it_message(PyObject* self, PyObject* args){ const char* string_output; - if (!PyArg_ParseTuple(args, "s", &string_output)){ + const char* string_name_plugin = NULL; + if (!PyArg_ParseTuple(args, "s|s", &string_output, &string_name_plugin)){ return NULL; } - log_it(L_MSG, string_output); + if (string_name_plugin) { + log_it(L_MSG, dap_strdup_printf("[plugin: %s] %s", string_output, string_name_plugin)); + } else { + log_it(L_MSG, string_output); + } return PyLong_FromLong(0); } PyObject* dap_log_it_dap(PyObject* self, PyObject* args){ const char* string_output; - if (!PyArg_ParseTuple(args, "s", &string_output)){ + const char* string_name_plugin = NULL; + if (!PyArg_ParseTuple(args, "s|s", &string_output, &string_name_plugin)){ return NULL; } - log_it(L_DAP, string_output); + if (string_name_plugin) { + log_it(L_DAP, dap_strdup_printf("[plugin: %s] %s", string_output, string_name_plugin)); + } else { + log_it(L_DAP, string_output); + } return PyLong_FromLong(0); } PyObject* dap_log_it_warning(PyObject* self, PyObject* args){ const char* string_output; - if (!PyArg_ParseTuple(args, "s", &string_output)){ + const char* string_name_plugin = NULL; + if (!PyArg_ParseTuple(args, "s|s", &string_output, &string_name_plugin)){ return NULL; } - log_it(L_WARNING, string_output); + if (string_name_plugin) { + log_it(L_WARNING, dap_strdup_printf("[plugin: %s] %s", string_output, string_name_plugin)); + } else { + log_it(L_WARNING, string_output); + } return PyLong_FromLong(0); } PyObject* dap_log_it_att(PyObject* self, PyObject* args){ const char* string_output; - if (!PyArg_ParseTuple(args, "s", &string_output)){ + const char* string_name_plugin = NULL; + if (!PyArg_ParseTuple(args, "s|s", &string_output, &string_name_plugin)){ return NULL; } - log_it(L_ATT, string_output); + if (string_name_plugin) { + log_it(L_ATT, dap_strdup_printf("[plugin: %s] %s", string_output, string_name_plugin)); + } else { + log_it(L_ATT, string_output); + } return PyLong_FromLong(0); } PyObject* dap_log_it_error(PyObject* self, PyObject* args){ const char* string_output; - if (!PyArg_ParseTuple(args, "s", &string_output)){ + const char* string_name_plugin = NULL; + if (!PyArg_ParseTuple(args, "s|s", &string_output, &string_name_plugin)){ return NULL; } - log_it(L_ERROR, string_output); + if (string_name_plugin) { + log_it(L_ERROR, dap_strdup_printf("[plugin: %s] %s", string_output, string_name_plugin)); + } else { + log_it(L_ERROR, string_output); + } return PyLong_FromLong(0); } PyObject* dap_log_it_critical(PyObject* self, PyObject* args){ const char* string_output; - if (!PyArg_ParseTuple(args, "s", &string_output)){ + const char* string_name_plugin = NULL; + if (!PyArg_ParseTuple(args, "s|s", &string_output, &string_name_plugin)){ return NULL; } - log_it(L_CRITICAL, string_output); + if (string_name_plugin) { + log_it(L_CRITICAL, dap_strdup_printf("[plugin: %s] %s", string_output, string_name_plugin)); + } else { + log_it(L_CRITICAL, string_output); + } return PyLong_FromLong(0); } diff --git a/modules/dap-sdk/core/src/libdap-python.h b/modules/dap-sdk/core/src/libdap-python.h index 28de923a..48649d3e 100644 --- a/modules/dap-sdk/core/src/libdap-python.h +++ b/modules/dap-sdk/core/src/libdap-python.h @@ -4,6 +4,7 @@ #include "dap_config.h" #include "dap_common.h" #include "dap_list.h" +#include "dap_strfuncs.h" typedef struct PyDap{ PyObject_HEAD diff --git a/modules/dap-sdk/crypto/CMakeLists.txt b/modules/dap-sdk/crypto/CMakeLists.txt index 49e20a62..cd8ef325 100644 --- a/modules/dap-sdk/crypto/CMakeLists.txt +++ b/modules/dap-sdk/crypto/CMakeLists.txt @@ -36,7 +36,10 @@ add_library(${PROJECT_NAME} STATIC ${CRYPTO_PYTHON_SRCS} ${CRYPTO_PYTHON_HEADERS target_link_libraries(${PROJECT_NAME}) -target_link_libraries(${PROJECT_NAME} dap_core dap_crypto) +target_link_libraries(${PROJECT_NAME} + dap_core + dap_crypto + dap_chain_common_python_module) target_include_directories(${PROJECT_NAME} PUBLIC include/ ) diff --git a/modules/dap-sdk/crypto/include/wrapping_dap_hash.h b/modules/dap-sdk/crypto/include/wrapping_dap_hash.h index e4ce791b..0f4cd5b0 100644 --- a/modules/dap-sdk/crypto/include/wrapping_dap_hash.h +++ b/modules/dap-sdk/crypto/include/wrapping_dap_hash.h @@ -26,6 +26,7 @@ #pragma once #include <Python.h> #include "dap_hash.h" +#include "wrapping_dap_chain_common.h" #ifdef __cplusplus extern "C" { @@ -49,6 +50,10 @@ typedef struct PyDapHashFast{ dap_chain_hash_fast_t *hash_fast; }PyDapHashFastObject; +PyObject* PyDapHashFast_compare(PyObject *self, PyObject *other, int op); + +int PyDapHashFast_init(PyObject *self, PyObject *args, PyObject *kwds); + PyObject *dap_chain_str_to_hash_fast_py(PyObject *self, PyObject *args); PyObject *dap_hash_fast_py(PyObject *self, PyObject *args); PyObject *dap_hash_fast_compare_py(PyObject *self, PyObject *args); diff --git a/modules/dap-sdk/crypto/include/wrapping_dap_sign.h b/modules/dap-sdk/crypto/include/wrapping_dap_sign.h index 99593131..b3847a4c 100644 --- a/modules/dap-sdk/crypto/include/wrapping_dap_sign.h +++ b/modules/dap-sdk/crypto/include/wrapping_dap_sign.h @@ -29,6 +29,10 @@ #include <Python.h> #include "dap_sign.h" #include "wrapping_dap_pkey.h" +#include "libdap_crypto_key_python.h" +#include "wrapping_dap_chain_datum.h" +#include "wrapping_dap_chain_datum_token.h" +#include "wrapping_dap_chain_datum_tx.h" typedef struct PyDapSignType{ PyObject_HEAD @@ -48,6 +52,9 @@ PyObject *wrapping_dap_sign_get_type(PyObject *self, void *closure); PyObject *wrapping_dap_sign_get_pkey(PyObject *self, void *closure); PyObject *wrapping_dap_sign_get_pkey_hash(PyObject *self, void *closure); PyObject *wrapping_dap_sign_get_size(PyObject *self, void *closure); +int wrapping_dap_sign_create(PyObject *self, PyObject* args, PyObject *kwds); + +PyObject *wrapping_dap_sign_verify(PyObject *self, PyObject *args); extern PyTypeObject DapCryptoSignObjectType; diff --git a/modules/dap-sdk/crypto/src/wrapping_dap_hash.c b/modules/dap-sdk/crypto/src/wrapping_dap_hash.c index 2b6856be..85b5fb22 100644 --- a/modules/dap-sdk/crypto/src/wrapping_dap_hash.c +++ b/modules/dap-sdk/crypto/src/wrapping_dap_hash.c @@ -97,7 +97,7 @@ PyTypeObject DapChainHashFastObjectType = { "Hash fast object", /* tp_doc */ 0, /* tp_traverse */ 0, /* tp_clear */ - 0, /* tp_richcompare */ + PyDapHashFast_compare, /* tp_richcompare */ 0, /* tp_weaklistoffset */ 0, /* tp_iter */ 0, /* tp_iternext */ @@ -109,11 +109,86 @@ PyTypeObject DapChainHashFastObjectType = { 0, /* tp_descr_get */ 0, /* tp_descr_set */ 0, /* tp_dictoffset */ - 0, /* tp_init */ + PyDapHashFast_init, /* tp_init */ 0, /* tp_alloc */ PyType_GenericNew, /* tp_new */ }; +PyObject* PyDapHashFast_compare(PyObject *self, PyObject *other, int op){ + if (!PyDapHashFast_Check((PyDapHashFastObject*)other)){ + PyErr_SetString(PyExc_NotImplementedError, "The comparison works for instances of the HashFast " + "object."); + return NULL; + } + if (op == Py_EQ){ + bool res = dap_hash_fast_compare( + ((PyDapHashFastObject *)self)->hash_fast, + ((PyDapHashFastObject*)other)->hash_fast + ); + if (res) { + Py_RETURN_TRUE; + } else { + Py_RETURN_FALSE; + } + } + if (op == Py_NE){ + bool res = dap_hash_fast_compare( + ((PyDapHashFastObject*)self)->hash_fast, + ((PyDapHashFastObject*)other)->hash_fast + ); + if (!res){ + Py_RETURN_TRUE; + } else { + Py_RETURN_FALSE; + } + } + PyErr_SetString(PyExc_NotImplementedError, "Two instances of an object of type HashFast can only be " + "tested for equality and not equality relative to each other."); + return NULL; +} + +int PyDapHashFast_init(PyObject *self, PyObject *args, PyObject *kwds){ + const char *kwlist[] = { + "data", + NULL + }; + PyObject *obj_data; + if (!PyArg_ParseTupleAndKeywords(args, kwds, "O", (char**)kwlist, &obj_data)){ + return -1; + } + void *l_data = NULL; + size_t l_data_size = 0; + if (PyBytes_Check(obj_data)){ + l_data = PyBytes_AsString(obj_data); + l_data_size = PyBytes_Size(obj_data); + } + if (DapChainDatumToken_Check(obj_data)){ + l_data = ((PyDapChainDatumTokenObject*)obj_data)->token; + l_data_size = ((PyDapChainDatumTokenObject*)obj_data)->token_size; + } + if (PyDapChainDatumTokenEmissionObject_check(obj_data)){ + l_data = ((PyDapChainDatumTokenEmissionObject*)obj_data)->token_emission; + l_data_size = ((PyDapChainDatumTokenEmissionObject*)obj_data)->token_size; + } + if (PyDapChainDatum_Check(obj_data)){ + l_data = ((PyDapChainDatumObject*)obj_data)->datum; + l_data_size = dap_chain_datum_size(((PyDapChainDatumObject*)obj_data)->datum); + } + if (DapChainDatumTx_Check(obj_data)){ + l_data = ((PyDapChainDatumTxObject*)obj_data)->datum_tx; + l_data_size = dap_chain_datum_tx_get_size(((PyDapChainDatumTxObject*)obj_data)->datum_tx); + } + if (!l_data || l_data_size == 0){ + PyErr_SetString(PyExc_AttributeError, "The attribute of this function was passed incorrectly, " + "the function accepts the attribute Datum, Datum Token, " + "DatumTokenEmission, DatumTx or byte."); + return -1; + } + ((PyDapHashFastObject*)self)->hash_fast = DAP_NEW(dap_hash_fast_t); + dap_hash_fast(l_data, l_data_size, ((PyDapHashFastObject*)self)->hash_fast); + return 0; +} + PyObject *dap_chain_str_to_hash_fast_py(PyObject *self, PyObject *args){ const char *hash_str; if (!PyArg_ParseTuple(args, "s", &hash_str)) diff --git a/modules/dap-sdk/crypto/src/wrapping_dap_sign.c b/modules/dap-sdk/crypto/src/wrapping_dap_sign.c index eb08103e..742298b1 100644 --- a/modules/dap-sdk/crypto/src/wrapping_dap_sign.c +++ b/modules/dap-sdk/crypto/src/wrapping_dap_sign.c @@ -56,6 +56,11 @@ PyGetSetDef DapSignObjectGetsSetsDef[] = { {NULL} }; +PyMethodDef DapSignObjectMethods[]= { + {"verify", wrapping_dap_sign_verify, METH_VARARGS, ""}, + {NULL, NULL, 0, NULL} +}; + PyTypeObject DapCryptoSignObjectType = { PyVarObject_HEAD_INIT(NULL, 0) "DAP.Crypto.Sign", /* tp_name */ @@ -85,7 +90,7 @@ PyTypeObject DapCryptoSignObjectType = { 0, /* tp_weaklistoffset */ 0, /* tp_iter */ 0, /* tp_iternext */ - 0, /* tp_methods */ + DapSignObjectMethods, /* tp_methods */ 0, /* tp_members */ DapSignObjectGetsSetsDef, /* tp_getset */ 0, /* tp_base */ @@ -93,7 +98,7 @@ PyTypeObject DapCryptoSignObjectType = { 0, /* tp_descr_get */ 0, /* tp_descr_set */ 0, /* tp_dictoffset */ - 0, /* tp_init */ + wrapping_dap_sign_create, /* tp_init */ 0, /* tp_alloc */ PyType_GenericNew, /* tp_new */ }; @@ -126,3 +131,98 @@ PyObject *wrapping_dap_sign_get_size(PyObject *self, void *closure){ (void)closure; return Py_BuildValue("I", ((PyDapSignObject*)self)->sign->header.sign_size); } + +int wrapping_dap_sign_create(PyObject *self, PyObject* args, PyObject *kwds){ + //DATUM, DATUM_TX, DATUM_TOKEN, Bytes + const char *kwlist[] = { + "key", + "data", + NULL + }; + PyObject *obj_key; + PyObject *obj_data; + if (!PyArg_ParseTupleAndKeywords(args, kwds, "OO", (char**)kwlist, &obj_key, &obj_data)) + return -1; + if (!PyCryptoKeyObject_check(obj_key)){ + PyErr_SetString(PyExc_AttributeError, "The first argument was passed incorrectly, the first " + "argument must be an object of type Crypto.Key."); + return -1; + } + dap_sign_t *l_sign = NULL; + //DATA = BYTES + if (PyBytes_Check(obj_data)){ + void *l_bytes = PyBytes_AsString(obj_data); + size_t l_bytes_size = PyBytes_Size(obj_data); + l_sign = dap_sign_create(((PyCryptoKeyObject*)obj_key)->key, l_bytes, l_bytes_size,0 ); + } + if (DapChainDatumToken_Check(obj_data)){ + l_sign = dap_sign_create(((PyCryptoKeyObject*)obj_key)->key, + ((PyDapChainDatumTokenObject*)obj_data)->token, + ((PyDapChainDatumTokenObject*)obj_data)->token_size, 0); + } + if (PyDapChainDatumTokenEmissionObject_check(obj_data)){ + l_sign = dap_sign_create(((PyCryptoKeyObject*)obj_key)->key, + ((PyDapChainDatumTokenEmissionObject*)obj_data)->token_emission, + ((PyDapChainDatumTokenEmissionObject*)obj_data)->token_size, 0); + } + if (PyDapChainDatum_Check(obj_data)){ + size_t l_datum_size = dap_chain_datum_size(((PyDapChainDatumObject*)obj_data)->datum); + l_sign = dap_sign_create( + ((PyCryptoKeyObject*)obj_key)->key, + ((PyDapChainDatumObject*)obj_data)->datum, l_datum_size, 0); + } + if (DapChainDatumTx_Check(obj_data)){ + size_t l_datum_tx_size = dap_chain_datum_tx_get_size(((PyDapChainDatumTxObject*)obj_data)->datum_tx); + l_sign = dap_sign_create(((PyCryptoKeyObject*)obj_key)->key, + ((PyDapChainDatumTxObject*)obj_data)->datum_tx, l_datum_tx_size, 0); + } + if (!l_sign){ + PyErr_SetString(PyExc_AttributeError, "The signature could not be created, the second argument " + "may have been passed incorrectly, or an unsupported argument may have " + "been passed."); + return -1; + } + ((PyDapSignObject*)self)->sign = l_sign; + return 0; +} + +PyObject *wrapping_dap_sign_verify(PyObject *self, PyObject *args){ +// (void)args; + PyObject *obj_data; + if (!PyArg_ParseTuple(args, "O", &obj_data)){ + return NULL; + } + void *l_data = NULL; + size_t l_data_size = 0; + if (PyBytes_Check(obj_data)){ + l_data = PyBytes_AsString(obj_data); + l_data_size = PyBytes_Size(obj_data); + } + if (DapChainDatumToken_Check(obj_data)){ + l_data = ((PyDapChainDatumTokenObject*)obj_data)->token; + l_data_size = ((PyDapChainDatumTokenObject*)obj_data)->token_size; + } + if (PyDapChainDatumTokenEmissionObject_check(obj_data)){ + l_data = ((PyDapChainDatumTokenEmissionObject*)obj_data)->token_emission; + l_data_size = ((PyDapChainDatumTokenEmissionObject*)obj_data)->token_size; + } + if (PyDapChainDatum_Check(obj_data)){ + l_data = ((PyDapChainDatumObject*)obj_data)->datum; + l_data_size = dap_chain_datum_size(((PyDapChainDatumObject*)obj_data)->datum); + } + if (DapChainDatumTx_Check(obj_data)){ + l_data = ((PyDapChainDatumTxObject*)obj_data)->datum_tx; + l_data_size = dap_chain_datum_tx_get_size(((PyDapChainDatumTxObject*)obj_data)->datum_tx); + } + if (!l_data || l_data_size == 0){ + PyErr_SetString(PyExc_AttributeError, "The attribute of this function was passed incorrectly, " + "the function accepts the attribute Datum, Datum Token, " + "DatumTokenEmission, DatumTx, byte."); + return NULL; + } + if(dap_sign_verify(((PyDapSignObject*)self)->sign, l_data, l_data_size)){ + Py_RETURN_TRUE; + } else { + Py_RETURN_FALSE; + } +} diff --git a/modules/dap-sdk/net/server/http/src/wrapping_dap_http_simple.c b/modules/dap-sdk/net/server/http/src/wrapping_dap_http_simple.c index ee0135d3..1aa071dd 100644 --- a/modules/dap-sdk/net/server/http/src/wrapping_dap_http_simple.c +++ b/modules/dap-sdk/net/server/http/src/wrapping_dap_http_simple.c @@ -1,4 +1,5 @@ #include "wrapping_dap_http_simple.h" +#include "python-cellframe_common.h" #define LOG_TAG "wrapping_dap_http_simple" @@ -96,11 +97,11 @@ void wrapping_dap_http_simple_callback(dap_http_simple_t *sh, void *obj){ ((PyHttpStatusCodeObject*)obj_http_status_code)->http_status = *ret; PyObject_Dir((PyObject*)obj_http_status_code); PyObject *obj_argv = Py_BuildValue("OO", obj_http_simple, obj_http_status_code); - PyErr_Print(); +// python_error_in_log_it(LOG_TAG); PyObject *result = PyObject_CallObject(obj_func, obj_argv); if (!result){ log_it(L_DEBUG, "Function can't be called"); - PyErr_Print(); + python_error_in_log_it(LOG_TAG); *ret = Http_Status_InternalServerError; } *ret = ((PyHttpStatusCodeObject*)obj_http_status_code)->http_status; diff --git a/modules/dap-sdk/net/server/json_rpc/CMakeLists.txt b/modules/dap-sdk/net/server/json_rpc/CMakeLists.txt index 6b09ef11..af8e0736 100644 --- a/modules/dap-sdk/net/server/json_rpc/CMakeLists.txt +++ b/modules/dap-sdk/net/server/json_rpc/CMakeLists.txt @@ -37,6 +37,7 @@ target_link_libraries(${PROJECT_NAME} dap_server_core_python_module dap_json_rpc dap_server_http_python_module + CellFrame ${PYTHON_LIBRARIES}) target_include_directories(${PROJECT_NAME} PUBLIC include/ ) diff --git a/modules/dap-sdk/net/server/json_rpc/include/wrapping_json_rpc_request.h b/modules/dap-sdk/net/server/json_rpc/include/wrapping_json_rpc_request.h index 0c2b94ec..28fd4cd2 100644 --- a/modules/dap-sdk/net/server/json_rpc/include/wrapping_json_rpc_request.h +++ b/modules/dap-sdk/net/server/json_rpc/include/wrapping_json_rpc_request.h @@ -7,6 +7,7 @@ #include "dap_json_rpc_request_handler.h" #include "dap_json_rpc_params.h" #include "wrapping_json_rpc_response.h" +#include "python-cellframe_common.h" //#include "wrapping_dap_json_rpc_ #ifdef __cplusplus diff --git a/modules/dap-sdk/net/server/json_rpc/src/wrapping_json_rpc_request.c b/modules/dap-sdk/net/server/json_rpc/src/wrapping_json_rpc_request.c index 320281a2..532d75bb 100644 --- a/modules/dap-sdk/net/server/json_rpc/src/wrapping_json_rpc_request.c +++ b/modules/dap-sdk/net/server/json_rpc/src/wrapping_json_rpc_request.c @@ -94,7 +94,7 @@ void _w_dap_json_rpc_request_handler(dap_json_rpc_params_t *a_params, dap_json_r PyObject_Dir((PyObject*)obj_response); //Called python func PyObject *obj_result = PyObject_CallObject(func->call_func, args); - PyErr_Print(); + python_error_in_log_it(LOG_TAG); PyGILState_Release(GILState); if (!obj_result){ log_it(L_ERROR, "Can't call method: %s", a_method); diff --git a/modules/plugins/src/dap_chain_plugins.c b/modules/plugins/src/dap_chain_plugins.c index cfb88865..dfe7c7dd 100644 --- a/modules/plugins/src/dap_chain_plugins.c +++ b/modules/plugins/src/dap_chain_plugins.c @@ -131,7 +131,7 @@ void dap_chain_plugins_load_plugin_importing(const char *a_dir_path, const char Py_XDECREF(l_obj_dir_path); PyObject *l_module = PyImport_ImportModule(a_name); if (!l_module){ - PyErr_Print(); + python_error_in_log_it(LOG_TAG); PyErr_Clear(); return; } @@ -156,12 +156,12 @@ void dap_chain_plugins_load_plugin_initialization(){ dap_chain_plugins_list_add(l_container->module, l_container->name); Py_INCREF(l_container->module); } else { - PyErr_Print(); + python_error_in_log_it(LOG_TAG); log_it(L_ERROR, "Can't initialize \"%s\" plugin. Code error: %i", l_container->name, _PyLong_AsInt(l_res_int)); } } else { - PyErr_Print(); + python_error_in_log_it(LOG_TAG); log_it(L_ERROR, "The 'init' function of \"%s\" plugin didn't return an integer value", l_container->name); } Py_XDECREF(l_res_int); @@ -182,7 +182,7 @@ void dap_chain_plugins_load_plugin(const char *a_dir_path, const char *a_name){ PyList_Append(s_sys_path, l_obj_dir_path); Py_XDECREF(l_obj_dir_path); PyObject *l_module = PyImport_ImportModule(a_name); - PyErr_Print(); + python_error_in_log_it(LOG_TAG); PyObject *l_func_init = PyObject_GetAttrString(l_module, "init"); PyObject *l_func_deinit = PyObject_GetAttrString(l_module, "deinit"); PyObject *l_res_int = NULL; @@ -193,11 +193,11 @@ void dap_chain_plugins_load_plugin(const char *a_dir_path, const char *a_name){ if (_PyLong_AsInt(l_res_int) == 0){ dap_chain_plugins_list_add(l_module, a_name); } else { - PyErr_Print(); + python_error_in_log_it(LOG_TAG); log_it(L_ERROR, "Can't initialize \"%s\" plugin. Code error: %i", a_name, _PyLong_AsInt(l_res_int)); } } else { - PyErr_Print(); + python_error_in_log_it(LOG_TAG); log_it(L_ERROR, "The 'init' function of \"%s\" plugin didn't return an integer value", a_name); } Py_XDECREF(l_res_int); -- GitLab