diff --git a/CMakeLists.txt b/CMakeLists.txt index a7a98f61469e8f8fcd5b097ead26a764fba887cf..1992d015edfde19a7c35e539ff46e389f60f56c4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -18,7 +18,8 @@ endif() if(NOT DEFINED CELLFRAME_MODULES) include (dap-sdk/cmake/OS_Detection.cmake) - set(CELLFRAME_MODULES "core chains mining network srv cs-dag-poa cs-block-poa cs-block-pos cs-esbocs cs-none srv-app srv-app-db srv-datum srv-stake srv-voting srv-bridge srv-xchange") + set(CELLFRAME_MODULES "core chains mining network srv cs-dag-poa cs-esbocs cs-none + srv-app srv-app-db srv-datum srv-stake srv-voting srv-bridge srv-xchange") if(LINUX OR DARWIN) set(CELLFRAME_MODULES "${CELLFRAME_MODULES} srv-vpn") @@ -116,19 +117,7 @@ if (CELLFRAME_MODULES MATCHES "cs-block-" OR CELLFRAME_MODULES MATCHES "cs-esboc set(CELLFRAME_LIBS ${CELLFRAME_LIBS} dap_chain_cs_blocks) endif() -# PoA consensus for blocks -if (CELLFRAME_MODULES MATCHES "cs-block-poa") - message("[+] Module 'cs-block-poa'") - set(CELLFRAME_LIBS ${CELLFRAME_LIBS} dap_chain_cs_block_poa) -endif() - -# PoS consensus for blocks -if (CELLFRAME_MODULES MATCHES "cs-block-pos") - message("[+] Module 'cs-block-pos'") - set(CELLFRAME_LIBS ${CELLFRAME_LIBS} dap_chain_cs_block_pos) -endif() - -# TON consensus for blocks +# ESBOCS consensus for blocks if (CELLFRAME_MODULES MATCHES "cs-esbocs") message("[+] Module 'cs-esbocs'") set(CELLFRAME_LIBS ${CELLFRAME_LIBS} dap_chain_cs_esbocs) diff --git a/modules/CMakeLists.txt b/modules/CMakeLists.txt index b177cae7600f4d7ac61547dded161a7437ab548e..cd58b14dd0964ba67d41819a0d904d5cae8f0cf7 100644 --- a/modules/CMakeLists.txt +++ b/modules/CMakeLists.txt @@ -50,21 +50,11 @@ if (CELLFRAME_MODULES MATCHES "cs-dag-poa") endif() # Consensus type blocks -if (CELLFRAME_MODULES MATCHES "cs-block-") +if (CELLFRAME_MODULES MATCHES "cs-block-" OR CELLFRAME_MODULES MATCHES "cs-esbocs") add_subdirectory(type/blocks) endif() -# Block PoA -if (CELLFRAME_MODULES MATCHES "cs-block-poa") - add_subdirectory(consensus/block-poa) -endif() - -# Block PoS -if (CELLFRAME_MODULES MATCHES "cs-block-pos") - add_subdirectory(consensus/block-pos) -endif() - -# Block TON +# ESBOCS if (CELLFRAME_MODULES MATCHES "cs-esbocs") add_subdirectory(consensus/esbocs) endif() diff --git a/modules/common/dap_chain_datum_decree.c b/modules/common/dap_chain_datum_decree.c index 83710341f76e0d6a8bd1c39ec49e3053881e5966..150ad8228d141ca0437ccd56afb58e1429f67bd0 100644 --- a/modules/common/dap_chain_datum_decree.c +++ b/modules/common/dap_chain_datum_decree.c @@ -69,7 +69,7 @@ int dap_chain_datum_decree_get_fee_addr(dap_chain_datum_decree_t *a_decree, dap_ } static void *s_cb_copy_pkeys(const void *a_pkey, UNUSED_ARG void *a_data) { - return DAP_DUP((dap_pkey_t*)a_pkey); + return ({ dap_pkey_t *l_pkey = (dap_pkey_t*)a_pkey; DAP_DUP_SIZE(l_pkey, dap_pkey_get_size(l_pkey)); }); } dap_list_t *dap_chain_datum_decree_get_owners(dap_chain_datum_decree_t *a_decree, uint16_t *a_owners_num) diff --git a/modules/consensus/block-poa/CMakeLists.txt b/modules/consensus/block-poa/CMakeLists.txt deleted file mode 100644 index 6d6eac00c8e59288c197a28b56ac871234cb4db8..0000000000000000000000000000000000000000 --- a/modules/consensus/block-poa/CMakeLists.txt +++ /dev/null @@ -1,20 +0,0 @@ -cmake_minimum_required(VERSION 3.10) -project (dap_chain_cs_block_poa) - -file(GLOB DAP_CHAIN_BLOCK_CS_POA_SRCS *.c) -file(GLOB DAP_CHAIN_BLOCK_CS_POA_HEADERS include/*.h) - -add_library(${PROJECT_NAME} STATIC ${DAP_CHAIN_BLOCK_CS_POA_SRCS} ${DAP_CHAIN_BLOCK_CS_POA_HEADERS}) - -target_link_libraries(${PROJECT_NAME} dap_core dap_crypto dap_chain dap_chain_cs_blocks) -target_include_directories(${PROJECT_NAME} INTERFACE .) -target_include_directories(${PROJECT_NAME} PUBLIC include) - -if (INSTALL_SDK) -set_target_properties(${PROJECT_NAME} PROPERTIES PUBLIC_HEADER "${DAP_CHAIN_BLOCK_CS_POA_HEADERS}") -INSTALL(TARGETS ${PROJECT_NAME} - LIBRARY DESTINATION lib/modules/consensus/block-poa/ - ARCHIVE DESTINATION lib/modules/consensus/block-poa/ - PUBLIC_HEADER DESTINATION include/modules/consensus/block-poa/ -) -endif() \ No newline at end of file diff --git a/modules/consensus/block-poa/dap_chain_cs_block_poa.c b/modules/consensus/block-poa/dap_chain_cs_block_poa.c deleted file mode 100644 index 665b97a1957f0aa74ce21b4d75e2831132c95224..0000000000000000000000000000000000000000 --- a/modules/consensus/block-poa/dap_chain_cs_block_poa.c +++ /dev/null @@ -1,380 +0,0 @@ -/* - * Authors: - * Dmitriy A. Gearasimov <gerasimov.dmitriy@demlabs.net> - * DeM Labs Limited https://demlabs.net - * DAP SDK https://gitlab.demlabs.net/dap/dap-sdk - * Copyright (c) 2017 - * All rights reserved. - - This file is part of DAP SDK the open source project - - DAP SDK 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 SDK 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 SDK based project. If not, see <http://www.gnu.org/licenses/>. -*/ - -#include "dap_chain_net.h" -#include "dap_common.h" -#include "dap_strfuncs.h" -#include "dap_enc_base58.h" -#include "dap_cert.h" -#include "dap_chain.h" -#include "dap_chain_block.h" -#include "dap_chain_block_cache.h" -#include "dap_chain_cs_blocks.h" -#include "dap_chain_cs_block_poa.h" -#include "dap_chain_node_cli.h" -#include "dap_chain_node_cli_cmd.h" -#include "dap_global_db.h" -#include "dap_chain_cs.h" -#include "dap_chain_cs_blocks.h" - -#define LOG_TAG "dap_chain_cs_block_poa" - -typedef struct dap_chain_cs_dag_poa_pvt { - dap_enc_key_t *sign_key; - dap_cert_t ** auth_certs; - char * auth_certs_prefix; - uint16_t auth_certs_count; - uint16_t auth_certs_count_verify; // Number of signatures, needed for event verification - - dap_chain_callback_new_cfg_t prev_callback_created; // global network config init -} dap_chain_cs_block_poa_pvt_t; - -#define PVT(a) ((dap_chain_cs_block_poa_pvt_t *) a->_pvt ) - -static void s_callback_delete(dap_chain_cs_blocks_t* a_blocks); -static int s_callback_new(dap_chain_t * a_chain, dap_config_t * a_chain_cfg); -static int s_callback_created(dap_chain_t * a_chain, dap_config_t *a_chain_cfg); -static int s_callback_block_verify(dap_chain_cs_blocks_t * a_blocks, dap_chain_block_t* a_block, size_t a_block_size); -static size_t s_callback_block_sign(dap_chain_cs_blocks_t *a_blocks, dap_chain_block_t **a_block_ptr, size_t a_block_size); - -// CLI commands -static int s_cli_block_poa(int argc, char ** argv, void **a_str_reply); - -static bool s_seed_mode = false; -/** - * @brief dap_chain_cs_block_poa_init - * @return - */ -int dap_chain_cs_block_poa_init(void) -{ - // Add consensus constructor - dap_chain_cs_add ("block_poa", s_callback_new ); - s_seed_mode = dap_config_get_item_bool_default(g_config,"general","seed_mode",false); - dap_cli_server_cmd_add ("block_poa", s_cli_block_poa, "Blockchain PoA commands", - "block_poa -net <net_name> -chain <chain_name> block sign [-cert <priv_cert_name>] \n" - "\tSign new block with certificate <chain_name> or withs own PoA certificate\n\n"); - - return 0; -} - -/** - * @brief dap_chain_cs_block_poa_deinit - */ -void dap_chain_cs_block_poa_deinit(void) -{ - -} - - - -/** - * @brief s_cli_block_poa - * @param argc - * @param argv - * @param arg_func - * @param str_reply - * @return - */ -static int s_cli_block_poa(int argc, char ** argv, void **a_str_reply) -{ - int ret = -666; - int arg_index = 1; - dap_chain_net_t * l_chain_net = NULL; - dap_chain_t * l_chain = NULL; - - const char * l_hash_out_type = NULL; - dap_cli_server_cmd_find_option_val(argv, arg_index, argc, "-H", &l_hash_out_type); - if(!l_hash_out_type) - l_hash_out_type = "hex"; - if(dap_strcmp(l_hash_out_type, "hex") && dap_strcmp(l_hash_out_type, "base58")) { - dap_cli_server_cmd_set_reply_text(a_str_reply, "invalid parameter -H, valid values: -H <hex | base58>"); - return -1; - } - - if (dap_chain_node_cli_cmd_values_parse_net_chain(&arg_index,argc,argv,a_str_reply,&l_chain,&l_chain_net, - CHAIN_TYPE_INVALID)){ - return -3; - } - - const char *l_chain_type = dap_chain_get_cs_type(l_chain); - if (strcmp(l_chain_type, "block_poa")){ - dap_cli_server_cmd_set_reply_text(a_str_reply, - "Type of chain %s is not block_poa. This chain with type %s is not supported by this command", - l_chain->name, l_chain_type); - return -42; - } - - dap_chain_cs_blocks_t *l_blocks = DAP_CHAIN_CS_BLOCKS(l_chain); - dap_chain_cs_block_poa_pvt_t * l_poa_pvt = PVT ( DAP_CHAIN_CS_BLOCK_POA( l_blocks ) ); - - const char * l_block_new_cmd_str = NULL; - const char * l_block_hash_str = NULL; - const char * l_cert_str = NULL; - - dap_cli_server_cmd_find_option_val(argv, arg_index, argc, "block", &l_block_new_cmd_str); - dap_cli_server_cmd_find_option_val(argv, arg_index, argc, "-block", &l_block_hash_str); - dap_cli_server_cmd_find_option_val(argv, arg_index, argc, "-cert", &l_cert_str); - - dap_enc_key_t *l_sign_key = NULL; - // Load cert to sign if its present - if (l_cert_str) { - dap_cert_t *l_cert = dap_cert_find_by_name( l_cert_str); - l_sign_key = l_cert->enc_key; - } else if (l_poa_pvt) { - l_sign_key = l_poa_pvt->sign_key; - } - if (!l_sign_key || !l_sign_key->priv_key_data) { - dap_cli_server_cmd_set_reply_text(a_str_reply, "No certificate to sign blocks\n"); - return -2; - } - - // block hash may be in hex or base58 format - char *l_block_hash_hex_str; - char *l_event_hash_base58_str; - if(!dap_strncmp(l_block_hash_str, "0x", 2) || !dap_strncmp(l_block_hash_str, "0X", 2)) { - l_block_hash_hex_str = dap_strdup(l_block_hash_str); - l_event_hash_base58_str = dap_enc_base58_from_hex_str_to_str(l_block_hash_str); - } - else { - l_block_hash_hex_str = dap_enc_base58_to_hex_str_from_str(l_block_hash_str); - l_event_hash_base58_str = dap_strdup(l_block_hash_str); - } - // TODO make useful CLI for PoA blocks - UNUSED(l_block_hash_hex_str); - UNUSED(l_event_hash_base58_str); - // Parse block cmd - if ( l_block_new_cmd_str != NULL ){ - if ( strcmp(l_block_new_cmd_str,"sign") == 0) { // Sign event command - l_blocks->block_new_size = dap_chain_block_sign_add( &l_blocks->block_new,l_blocks->block_new_size, l_sign_key); - //dap_chain_hash_fast_t l_block_new_hash; - //dap_hash_fast(l_blocks->block_new, l_blocks->block_new_size,&l_block_new_hash); - } - } - return ret; -} - -/** - * @brief s_cs_callback - * @param a_chain - * @param a_chain_cfg - */ -static int s_callback_new(dap_chain_t * a_chain, dap_config_t * a_chain_cfg) -{ - dap_chain_cs_type_create("blocks", a_chain, a_chain_cfg); - dap_chain_cs_blocks_t * l_blocks = DAP_CHAIN_CS_BLOCKS( a_chain ); - dap_chain_cs_block_poa_t * l_poa = DAP_NEW_Z ( dap_chain_cs_block_poa_t); - if (!l_poa) { - log_it(L_CRITICAL, "%s", c_error_memory_alloc); - return -1; - } - l_blocks->_inheritor = l_poa; - l_blocks->callback_delete = s_callback_delete; - l_blocks->callback_block_verify = s_callback_block_verify; - l_blocks->callback_block_sign = s_callback_block_sign; - l_poa->_pvt = DAP_NEW_Z(dap_chain_cs_block_poa_pvt_t); - if (!l_poa->_pvt) { - log_it(L_CRITICAL, "%s", c_error_memory_alloc); - return -1; - } - dap_chain_cs_block_poa_pvt_t *l_poa_pvt = PVT(l_poa); - - if (dap_config_get_item_str(a_chain_cfg,"block-poa","auth_certs_prefix") ) { - l_poa_pvt->auth_certs_count = dap_config_get_item_uint16_default(a_chain_cfg,"block-poa","auth_certs_number",0); - l_poa_pvt->auth_certs_count_verify = dap_config_get_item_uint16_default(a_chain_cfg,"block-poa","auth_certs_number_verify",0); - l_poa_pvt->auth_certs_prefix = strdup ( dap_config_get_item_str(a_chain_cfg,"block-poa","auth_certs_prefix") ); - if (l_poa_pvt->auth_certs_count && l_poa_pvt->auth_certs_count_verify ) { - // Type sizeof's misunderstanding in malloc? - l_poa_pvt->auth_certs = DAP_NEW_Z_SIZE ( dap_cert_t *, l_poa_pvt->auth_certs_count * sizeof(dap_cert_t*)); - if (!l_poa_pvt->auth_certs) { - log_it(L_CRITICAL, "%s", c_error_memory_alloc); - return -1; - } - char l_cert_name[512]; - for (size_t i = 0; i < l_poa_pvt->auth_certs_count ; i++ ){ - snprintf(l_cert_name, sizeof(l_cert_name), "%s.%zu",l_poa_pvt->auth_certs_prefix, i); - if ((l_poa_pvt->auth_certs[i] = dap_cert_find_by_name( l_cert_name)) == NULL) { - snprintf(l_cert_name, sizeof(l_cert_name), "%s.%zu.pub",l_poa_pvt->auth_certs_prefix, i); - if ((l_poa_pvt->auth_certs[i] = dap_cert_find_by_name( l_cert_name)) == NULL) { - log_it(L_ERROR, "Can't find cert \"%s\"", l_cert_name); - return -1; - } - } - log_it(L_NOTICE, "Initialized auth cert \"%s\"", l_cert_name); - } - } - } - - log_it(L_NOTICE,"Initialized Block-PoA consensus with %u/%u minimum consensus",l_poa_pvt->auth_certs_count,l_poa_pvt->auth_certs_count_verify); - // Save old callback if present and set the call of its own (chain callbacks) - l_poa_pvt->prev_callback_created = l_blocks->chain->callback_created; - l_blocks->chain->callback_created = s_callback_created; - return 0; -} - -/** - * @brief s_callback_created - * @param a_chain - * @param a_chain_cfg - * @return - */ -static int s_callback_created(dap_chain_t * a_chain, dap_config_t *a_chain_net_cfg) -{ - dap_chain_cs_blocks_t * l_blocks = DAP_CHAIN_CS_BLOCKS( a_chain ); - dap_chain_cs_block_poa_t * l_poa = DAP_CHAIN_CS_BLOCK_POA( l_blocks ); - - // Call previous callback if present. So the first called is the first in - if (PVT(l_poa)->prev_callback_created ) - PVT(l_poa)->prev_callback_created(a_chain,a_chain_net_cfg); - - const char * l_sign_cert_str = NULL; - if ( ( l_sign_cert_str = dap_config_get_item_str(a_chain_net_cfg,"block-poa","blocks-sign-cert") ) != NULL ) { - dap_cert_t *l_sign_cert = dap_cert_find_by_name(l_sign_cert_str); - if (l_sign_cert == NULL) { - log_it(L_ERROR, "Can't load sign certificate, name \"%s\" is wrong", l_sign_cert_str); - } else if (l_sign_cert->enc_key->priv_key_data) { - PVT(l_poa)->sign_key = l_sign_cert->enc_key; - log_it(L_NOTICE, "Loaded \"%s\" certificate to sign poa blocks", l_sign_cert_str); - } else { - log_it(L_ERROR, "Certificate \"%s\" has no private key", l_sign_cert_str); - } - } else { - log_it(L_ERROR, "No sign certificate provided, can't sign any blocks"); - } - return 0; -} - -/** - * @brief s_chain_cs_dag_callback_delete - * @param a_dag - */ -static void s_callback_delete(dap_chain_cs_blocks_t * a_blocks) -{ - dap_chain_cs_block_poa_t * l_poa = DAP_CHAIN_CS_BLOCK_POA ( a_blocks ); - - if ( l_poa->_pvt ) { - dap_chain_cs_block_poa_pvt_t * l_poa_pvt = PVT ( l_poa ); - - if ( l_poa_pvt->auth_certs ) - DAP_DELETE ( l_poa_pvt->auth_certs); - - if ( l_poa_pvt->auth_certs_prefix ) - DAP_DELETE( l_poa_pvt->auth_certs_prefix ); - - DAP_DELETE ( l_poa->_pvt); - } - - if ( l_poa->_inheritor ) { - DAP_DELETE ( l_poa->_inheritor ); - } -} - -/** - * @brief - * function makes block singing - * @param a_dag a_blocks dap_chain_cs_blocks_t - * @param a_block dap_chain_block_t - * @param a_block_size size_t size of block object - * @return int - */ -static size_t s_callback_block_sign(dap_chain_cs_blocks_t *a_blocks, dap_chain_block_t **a_block_ptr, size_t a_block_size) -{ - assert(a_blocks); - dap_chain_cs_block_poa_t *l_poa = DAP_CHAIN_CS_BLOCK_POA(a_blocks); - dap_chain_cs_block_poa_pvt_t *l_poa_pvt = PVT(l_poa); - if (!l_poa_pvt->sign_key) { - log_it(L_WARNING, "Can't sign block with blocks-sign-cert in [block-poa] section"); - return 0; - } - if (!a_block_ptr || !(*a_block_ptr) || !a_block_size) { - log_it(L_WARNING, "Block size or block pointer is NULL"); - return 0; - } - return dap_chain_block_sign_add(a_block_ptr, a_block_size, l_poa_pvt->sign_key); -} - -/** - * @brief s_callbac_block_verify - * @param a_blocks - * @param a_block - * @param a_block_size - * @return - */ -static int s_callback_block_verify(dap_chain_cs_blocks_t * a_blocks, dap_chain_block_t * a_block, size_t a_block_size) -{ - dap_chain_cs_block_poa_pvt_t * l_poa_pvt = PVT ( DAP_CHAIN_CS_BLOCK_POA( a_blocks ) ); - uint16_t l_signs_verified_count = 0; - - // Check for first signature - dap_sign_t * l_sign = dap_chain_block_sign_get(a_block,a_block_size,0); - if (! l_sign){ - log_it(L_ERROR, "No any signatures at all for block"); - return -2; - } - // Parse the rest signs - size_t l_offset = (byte_t *)l_sign - a_block->meta_n_datum_n_sign; - size_t l_signs_section_size = a_block_size - dap_chain_block_get_sign_offset(a_block, a_block_size); - while (l_offset < a_block_size - sizeof(a_block->hdr)) { - if (!dap_sign_verify_size(l_sign, l_signs_section_size)) { - log_it(L_ERROR, "Corrupted block: sign size is bigger than block size"); - return -3; - } - size_t l_sign_size = dap_sign_get_size(l_sign); - // Check if sign size 0 - if (!l_sign_size){ - log_it(L_ERROR, "Corrupted block: sign size got zero"); - return -4; - } - // Check if sign size too big - if (l_sign_size > a_block_size- sizeof (a_block->hdr)-l_offset ){ - log_it(L_ERROR, "Corrupted block: sign size %zd is too big, out from block size %zd", l_sign_size, a_block_size); - return -5; - } - // Compare signature with auth_certs - for (uint16_t j = 0; j < l_poa_pvt->auth_certs_count; j++) { - if (dap_cert_compare_with_sign ( l_poa_pvt->auth_certs[j], l_sign) == 0){ - l_signs_verified_count++; - break; - } - } - //TODO verify sign itself - l_offset += l_sign_size; - l_sign = (dap_sign_t *)(a_block->meta_n_datum_n_sign + l_offset); - } - if (l_offset != a_block_size - sizeof(a_block->hdr)) { - log_it(L_ERROR, "Corrupted block: sign end exceeded the block bound"); - return -6; - } - return l_signs_verified_count >= l_poa_pvt->auth_certs_count_verify ? 0 : -1; -} - -dap_cert_t **dap_chain_cs_block_poa_get_auth_certs(dap_chain_t *a_chain, size_t *a_auth_certs_count) -{ - dap_chain_pvt_t *l_chain_pvt = DAP_CHAIN_PVT(a_chain); - if (strcmp(l_chain_pvt->cs_name, "block_poa")) - return NULL; - dap_chain_cs_block_poa_pvt_t *l_poa_pvt = PVT(DAP_CHAIN_CS_BLOCK_POA(DAP_CHAIN_CS_BLOCKS(a_chain))); - if (a_auth_certs_count) - *a_auth_certs_count = l_poa_pvt->auth_certs_count; - return l_poa_pvt->auth_certs; -} diff --git a/modules/consensus/block-poa/include/dap_chain_cs_block_poa.h b/modules/consensus/block-poa/include/dap_chain_cs_block_poa.h deleted file mode 100644 index 3e6eff7f93b9a156c1908a3f3c853bf92182545e..0000000000000000000000000000000000000000 --- a/modules/consensus/block-poa/include/dap_chain_cs_block_poa.h +++ /dev/null @@ -1,39 +0,0 @@ -/* - * Authors: - * Dmitriy A. Gearasimov <gerasimov.dmitriy@demlabs.net> - * DeM Labs Limited https://demlabs.net - * DAP SDK https://gitlab.demlabs.net/dap/dap-sdk - * Copyright (c) 2017 - * All rights reserved. - - This file is part of DAP SDK the open source project - - DAP SDK 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 SDK 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 SDK based project. If not, see <http://www.gnu.org/licenses/>. -*/ -#pragma once -#include "dap_chain_cs_blocks.h" - -typedef struct dap_chain_cs_block_poa { - dap_chain_t * chain; - dap_chain_cs_blocks_t * blocks; - void * _pvt; - void * _inheritor; -} dap_chain_cs_block_poa_t; - -#define DAP_CHAIN_CS_BLOCK_POA(a) ( (dap_chain_cs_block_poa_t *) (a)->_inheritor) - - -int dap_chain_cs_block_poa_init(void); -void dap_chain_cs_block_poa_deinit(void); -dap_cert_t **dap_chain_cs_block_poa_get_auth_certs(dap_chain_t *a_chain, size_t *a_auth_certs_count); diff --git a/modules/consensus/block-pos/CMakeLists.txt b/modules/consensus/block-pos/CMakeLists.txt deleted file mode 100644 index dab434da2e84ba52be208f3da5cb7eddb74d4e3a..0000000000000000000000000000000000000000 --- a/modules/consensus/block-pos/CMakeLists.txt +++ /dev/null @@ -1,20 +0,0 @@ -cmake_minimum_required(VERSION 3.10) -project (dap_chain_cs_block_pos) - -file(GLOB DAP_CHAIN_CS_BLOCK_POS_SRCS *.c) -file(GLOB DAP_CHAIN_CS_BLOCK_POS_HEADERS include/*.h) - -add_library(${PROJECT_NAME} STATIC ${DAP_CHAIN_CS_BLOCK_POS_SRCS} ${DAP_CHAIN_CS_BLOCK_POS_HEADERS}) - -target_link_libraries(${PROJECT_NAME} dap_core dap_crypto dap_chain dap_chain_cs_blocks dap_chain_net_srv_stake) -target_include_directories(${PROJECT_NAME} INTERFACE .) -target_include_directories(${PROJECT_NAME} PUBLIC include) - -if (INSTALL_SDK) -set_target_properties(${PROJECT_NAME} PROPERTIES PUBLIC_HEADER "${DAP_CHAIN_CS_BLOCK_POS_HEADERS}") -INSTALL(TARGETS ${PROJECT_NAME} - LIBRARY DESTINATION lib/modules/consensus/block-pos/ - ARCHIVE DESTINATION lib/modules/consensus/block-pos/ - PUBLIC_HEADER DESTINATION include/modules/consensus/block-pos/ -) -endif() \ No newline at end of file diff --git a/modules/consensus/block-pos/dap_chain_cs_block_pos.c b/modules/consensus/block-pos/dap_chain_cs_block_pos.c deleted file mode 100644 index 5fb06747c7c1458036a10c2af1e8dec0669a3aec..0000000000000000000000000000000000000000 --- a/modules/consensus/block-pos/dap_chain_cs_block_pos.c +++ /dev/null @@ -1,279 +0,0 @@ -/* - * Authors: - * Roman Khlopkov <roman.khlopkov@demlabs.net> - * DeM Labs Inc. https://demlabs.net - * Kelvin Project https://github.com/kelvinblockchain - * Copyright (c) 2017-2021 - * All rights reserved. - - This file is part of DAP (Distributed Applications Platform) the open source project - - DAP (Distributed Applications Platform) 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 <stdlib.h> - -#include "dap_chain_net.h" -#include "dap_common.h" -#include "dap_string.h" -#include "dap_strfuncs.h" -#include "dap_chain_cs.h" -#include "dap_chain_cs_blocks.h" -// #include "dap_chain_cs_blocks_session.h" -#include "dap_chain_cs_block_pos.h" -#include "dap_chain_net_srv_stake_pos_delegate.h" -#include "dap_chain_ledger.h" - -#define LOG_TAG "dap_chain_cs_block_pos" - -typedef struct dap_chain_cs_block_pos_pvt -{ - dap_enc_key_t *blocks_sign_key; - char **tokens_hold; - uint64_t *tokens_hold_value; - size_t tokens_hold_size; - uint16_t confirmations_minimum; - dap_chain_callback_new_cfg_t prev_callback_created; -} dap_chain_cs_block_pos_pvt_t; - -#define PVT(a) ((dap_chain_cs_block_pos_pvt_t *)a->_pvt) - -static void s_callback_delete(dap_chain_cs_blocks_t *a_blocks); -static int s_callback_new(dap_chain_t *a_chain, dap_config_t *a_chain_cfg); -static int s_callback_created(dap_chain_t *a_chain, dap_config_t *a_chain_cfg); -static int s_callback_block_verify(dap_chain_cs_blocks_t *a_blocks, dap_chain_block_t *a_block, size_t a_block_size); -static size_t s_callback_block_sign(dap_chain_cs_blocks_t *a_blocks, dap_chain_block_t **a_block_ptr, size_t a_block_size); - -/** - * @brief dap_chain_cs_block_pos_init - * @return - */ -int dap_chain_cs_block_pos_init() -{ - dap_chain_cs_add("block_pos", s_callback_new); - return 0; -} - -/** - * @brief dap_chain_cs_block_pos_deinit - */ -void dap_chain_cs_block_pos_deinit(void) -{ - -} - -/** - * @brief s_cs_callback - * @param a_chain - * @param a_chain_cfg - */ -static int s_callback_new(dap_chain_t *a_chain, dap_config_t *a_chain_cfg) -{ - dap_chain_cs_type_create("blocks", a_chain, a_chain_cfg); - char ** l_tokens_hold = NULL; - char ** l_tokens_hold_value_str = NULL; - uint16_t l_tokens_hold_size = 0; - uint16_t l_tokens_hold_value_size = 0; - dap_chain_cs_blocks_t *l_blocks = DAP_CHAIN_CS_BLOCKS(a_chain); - dap_chain_cs_block_pos_t *l_pos = DAP_NEW_Z(dap_chain_cs_block_pos_t); - if (!l_pos) { - log_it(L_CRITICAL, "%s", c_error_memory_alloc); - return -1; - } - l_blocks->_inheritor = l_pos; - l_blocks->callback_delete = s_callback_delete; - l_blocks->callback_block_verify = s_callback_block_verify; - l_blocks->callback_block_sign = s_callback_block_sign; - - l_pos->_pvt = DAP_NEW_Z(dap_chain_cs_block_pos_pvt_t); - dap_chain_cs_block_pos_pvt_t *l_pos_pvt = PVT(l_pos); - if (!l_pos_pvt) { - log_it(L_CRITICAL, "%s", c_error_memory_alloc); - goto lb_err; - } - - l_tokens_hold = dap_config_get_array_str(a_chain_cfg, "block-pos", "stake_tokens", &l_tokens_hold_size); - l_tokens_hold_value_str = dap_config_get_array_str(a_chain_cfg, "block-pos", "stake_tokens_value", &l_tokens_hold_value_size); - - if (l_tokens_hold_size != l_tokens_hold_value_size){ - log_it(L_CRITICAL, "Entries tokens_hold and tokens_hold_value are different size!"); - goto lb_err; - } - l_pos_pvt->confirmations_minimum = dap_config_get_item_uint16_default(a_chain_cfg, "block-pos", "verifications_minimum", 1); - l_pos_pvt->tokens_hold_size = l_tokens_hold_size; - l_pos_pvt->tokens_hold = DAP_NEW_Z_SIZE(char *, sizeof(char *) * l_tokens_hold_size); - if (!l_pos_pvt->tokens_hold) { - log_it(L_CRITICAL, "%s", c_error_memory_alloc); - goto lb_err; - } - l_pos_pvt->tokens_hold_value = DAP_NEW_Z_SIZE(uint64_t, l_tokens_hold_value_size * sizeof(uint64_t)); - if (!l_pos_pvt->tokens_hold_value) { - log_it(L_CRITICAL, "%s", c_error_memory_alloc); - goto lb_err; - } - for (size_t i = 0; i < l_tokens_hold_value_size; i++) { - l_pos_pvt->tokens_hold[i] = dap_strdup(l_tokens_hold[i]); - if ((l_pos_pvt->tokens_hold_value[i] = - strtoull(l_tokens_hold_value_str[i],NULL,10)) == 0) { - log_it(L_CRITICAL, "Token %s has inproper hold value %s", - l_pos_pvt->tokens_hold[i], l_tokens_hold_value_str[i]); - goto lb_err; - } - } - // Save old callback if present and set the call of its own (chain callbacks) - l_pos_pvt->prev_callback_created = l_blocks->chain->callback_created; - l_blocks->chain->callback_created = s_callback_created; - return 0; - -lb_err: - for (int i = 0; i < l_tokens_hold_size; i++) { - if (l_tokens_hold[i]) - DAP_DELETE(l_tokens_hold[i]); - } - DAP_DEL_Z(l_tokens_hold); - if (l_pos_pvt) { - DAP_DEL_Z(l_pos_pvt->tokens_hold_value); - DAP_DELETE(l_pos_pvt); - } - DAP_DEL_Z(l_pos); - l_blocks->_inheritor = NULL; - l_blocks->callback_delete = NULL; - l_blocks->callback_block_verify = NULL; - return -1; -} - -/** - * @brief s_callback_created - * @param a_chain - * @param a_chain_cfg - * @return - */ -static int s_callback_created(dap_chain_t *a_chain, dap_config_t *a_chain_net_cfg) -{ - dap_chain_cs_blocks_t *l_blocks = DAP_CHAIN_CS_BLOCKS(a_chain); - dap_chain_cs_block_pos_t *l_pos = DAP_CHAIN_CS_BLOCK_POS(l_blocks); - - const char * l_sign_cert_str = NULL; - if ((l_sign_cert_str = dap_config_get_item_str(a_chain_net_cfg,"block-pos","blocks-sign-cert")) != NULL) { - dap_cert_t *l_sign_cert = dap_cert_find_by_name(l_sign_cert_str); - if (l_sign_cert == NULL) { - log_it(L_ERROR, "Can't load sign certificate, name \"%s\" is wrong", l_sign_cert_str); - } else if (l_sign_cert->enc_key->priv_key_data) { - PVT(l_pos)->blocks_sign_key = l_sign_cert->enc_key; - log_it(L_NOTICE, "Loaded \"%s\" certificate to sign PoS blocks", l_sign_cert_str); - } else { - log_it(L_ERROR, "Certificate \"%s\" has no private key", l_sign_cert_str); - } - } else { - log_it(L_ERROR, "No sign certificate provided, can't sign any blocks"); - } - //dap_chain_cs_blocks_session_init(a_chain, PVT(l_pos)->blocks_sign_key ); - return 0; -} - - -/** - * @brief s_chain_cs_block_callback_delete - * @param a_block - */ -static void s_callback_delete(dap_chain_cs_blocks_t *a_blocks) -{ - dap_chain_cs_block_pos_t *l_pos = DAP_CHAIN_CS_BLOCK_POS(a_blocks); - if (l_pos->_pvt) - DAP_DELETE(l_pos->_pvt); -} - -/** - * @brief - * function makes block singing - * @param a_block a_blocks dap_chain_cs_blocks_t - * @param a_block dap_chain_block_t - * @param a_block_size size_t size of block object - * @return int - */ -static size_t s_callback_block_sign(dap_chain_cs_blocks_t *a_blocks, dap_chain_block_t **a_block_ptr, size_t a_block_size) -{ - assert(a_blocks); - dap_chain_cs_block_pos_t *l_pos = DAP_CHAIN_CS_BLOCK_POS(a_blocks); - dap_chain_cs_block_pos_pvt_t *l_pos_pvt = PVT(l_pos); - if (!l_pos_pvt->blocks_sign_key) { - log_it(L_WARNING, "Can't sign block with blocks-sign-cert in [block-pos] section"); - return 0; - } - if (!a_block_ptr || !(*a_block_ptr) || !a_block_size) { - log_it(L_WARNING, "Block size or block pointer is NULL"); - return 0; - } - return dap_chain_block_sign_add(a_block_ptr, a_block_size, l_pos_pvt->blocks_sign_key); -} - -/** - * @brief - * function makes block singing verification - * @param a_block a_blocks dap_chain_cs_blocks_t - * @param a_block dap_chain_block_t - * @param a_block_size size_t size of block object - * @return int - */ -static int s_callback_block_verify(dap_chain_cs_blocks_t *a_blocks, dap_chain_block_t *a_block, size_t a_block_size) -{ - dap_chain_cs_block_pos_t *l_pos = DAP_CHAIN_CS_BLOCK_POS(a_blocks); - dap_chain_cs_block_pos_pvt_t *l_pos_pvt = PVT(l_pos); - - if (sizeof(a_block->hdr) >= a_block_size) { - log_it(L_WARNING,"Incorrect size with block %p on chain %s", a_block, a_blocks->chain->name); - return -7; - } - - size_t l_signs_count = dap_chain_block_get_signs_count(a_block, a_block_size); - if (l_signs_count < l_pos_pvt->confirmations_minimum) { - log_it(L_WARNING,"Wrong signature number with block %p on chain %s", a_block, a_blocks->chain->name); - return -2; // Wrong signatures number - } - - uint16_t l_verified_num = 0; - size_t l_signs_section_size = a_block_size - dap_chain_block_get_sign_offset(a_block, a_block_size); - for (size_t l_sig_pos = 0; l_sig_pos < l_signs_count; l_sig_pos++) { - dap_sign_t *l_sign = dap_chain_block_sign_get(a_block, a_block_size, l_sig_pos); - if (l_sign == NULL) { - log_it(L_WARNING, "Block isn't signed with anything: sig pos %zu, event size %zu", l_sig_pos, a_block_size); - return -4; - } - - bool l_sign_size_correct = dap_sign_verify_size(l_sign, l_signs_section_size); - if (!l_sign_size_correct) { - log_it(L_WARNING, "Block's sign #%zu size is incorrect", l_sig_pos); - return -44; - } - size_t l_block_data_size = dap_chain_block_get_sign_offset(a_block, a_block_size)+sizeof(a_block->hdr); - if (l_block_data_size == a_block_size) { - log_it(L_WARNING,"Block has nothing except sign, nothing to verify so I pass it (who knows why we have it?)"); - return 0; - } - - int l_sign_verified = dap_sign_verify(l_sign, a_block, l_block_data_size); - if (l_sign_verified) { - log_it(L_WARNING, "Block's sign is incorrect: code %d", l_sign_verified); - return -41; - } - } - - // Check number - if (l_verified_num >= l_pos_pvt->confirmations_minimum) { - // Passed all checks - return 0; - } else { - log_it(L_WARNING, "Wrong block: only %hu/%hu signs are valid", l_verified_num, l_pos_pvt->confirmations_minimum); - return -2; - } -} diff --git a/modules/consensus/block-pos/include/dap_chain_cs_block_pos.h b/modules/consensus/block-pos/include/dap_chain_cs_block_pos.h deleted file mode 100644 index 18db27395089919d3a6d3dfc3f6357550be8caea..0000000000000000000000000000000000000000 --- a/modules/consensus/block-pos/include/dap_chain_cs_block_pos.h +++ /dev/null @@ -1,39 +0,0 @@ -/* - * Authors: - * Dmitriy A. Gearasimov <gerasimov.dmitriy@demlabs.net> - * DeM Labs Inc. https://demlabs.net - * Kelvin Project https://github.com/kelvinblockchain - * Copyright (c) 2017-2018 - * All rights reserved. - - This file is part of DAP (Distributed Applications Platform) the open source project - - DAP (Distributed Applications Platform) 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_chain_cs_blocks.h" -#include "dap_cert.h" - -typedef struct dap_chain_cs_block_pos -{ - dap_chain_t *chain; - dap_chain_cs_blocks_t *blocks; - void *_pvt; -} dap_chain_cs_block_pos_t; - -#define DAP_CHAIN_CS_BLOCK_POS(a) ((dap_chain_cs_block_pos_t *)(a)->_inheritor) - - -int dap_chain_cs_block_pos_init(); -void dap_chain_cs_block_pos_deinit(void);