diff --git a/CMakeLists.txt b/CMakeLists.txt old mode 100755 new mode 100644 diff --git a/LICENSE b/LICENSE old mode 100755 new mode 100644 diff --git a/README.md b/README.md old mode 100755 new mode 100644 diff --git a/dap_chain_net.c b/dap_chain_net.c old mode 100755 new mode 100644 index 757671dee935201f6178270ff2ac96e325c93bdf..d411ab796cc3b36baa0092c491035b7c705208e2 --- a/dap_chain_net.c +++ b/dap_chain_net.c @@ -44,12 +44,6 @@ #define LOG_TAG "chain_net" -typedef enum dap_chain_net_state{ - NET_STATE_BEGIN = 0, - NET_STATE_LINKS_CONNECTING, - NET_STATE_LINKS_ESTABLISHED, -} dap_chain_net_state_t; - /** * @struct dap_chain_net_pvt * @details Private part of chain_net dap object @@ -61,7 +55,12 @@ typedef struct dap_chain_net_pvt{ uint8_t padding[4]; dap_chain_node_client_t * clients_by_node_addr; + dap_chain_node_client_t * clients_by_ipv4; + dap_chain_node_client_t * clients_by_ipv6; + size_t clients_count; + dap_chain_net_state_t state; + dap_chain_net_state_t state_target; } dap_chain_net_pvt_t; typedef struct dap_chain_net_item{ @@ -81,6 +80,75 @@ static size_t s_net_configs_count = 0; static pthread_cond_t s_net_proc_loop_cond = PTHREAD_COND_INITIALIZER; static pthread_mutex_t s_net_proc_loop_mutex = PTHREAD_MUTEX_INITIALIZER; +int s_net_states_proc(dap_chain_net_t * l_net); + +static const char * c_net_states[]={ + [NET_STATE_BEGIN] = "NET_STATE_BEGIN", + [NET_STATE_LINKS_CONNECTING] = "NET_STATE_LINKS_CONNECTING", + [NET_STATE_LINKS_ESTABLISHED]= "NET_STATE_LINKS_ESTABLISHED", + [NET_STATE_SYNC_GDB]= "NET_STATE_SYNC_GDB", + [NET_STATE_SYNC_CHAINS]= "NET_STATE_SYNC_CHAINS", + [NET_STATE_STAND_BY]= "NET_STATE_STAND_BY" +}; + +/** + * @brief s_net_state_to_str + * @param l_state + * @return + */ +inline static const char * s_net_state_to_str(dap_chain_net_state_t l_state) +{ + return c_net_states[l_state]; +} + +/** + * @brief dap_chain_net_state_go_to + * @param a_net + * @param a_new_state + */ +int dap_chain_net_state_go_to(dap_chain_net_t * a_net, dap_chain_net_state_t a_new_state) +{ + if (PVT(a_net)->state_target == a_new_state){ + log_it(L_WARNING,"Already going to state %s",s_net_state_to_str(a_new_state)); + } + PVT(a_net)->state_target = a_new_state; + return s_net_states_proc(a_net); +} + + +/** + * @brief s_net_states_proc + * @param l_net + */ +int s_net_states_proc(dap_chain_net_t * l_net) +{ + int ret=0; + switch ( PVT(l_net)->state ){ + case NET_STATE_BEGIN:{ + if ( PVT(l_net)->state_target != NET_STATE_BEGIN ) + dap_chain_net_links_establish(l_net); + }break; + case NET_STATE_LINKS_CONNECTING:{ + + log_it(L_DEBUG,"Connected %u/% links", PVT(l_net)->clients_count ); + ret = 1; + }break; + case NET_STATE_LINKS_ESTABLISHED:{ + + }break; + case NET_STATE_SYNC_GDB:{ + + }break; + case NET_STATE_SYNC_CHAINS:{ + + }break; + case NET_STATE_STAND_BY:{ + + } break; + } + return ret; +} + /** * @brief s_net_proc_thread * @details Brings up and check the Dap Chain Network @@ -96,6 +164,7 @@ static void * s_net_proc_thread ( void * a_net) pthread_cond_wait(&s_net_proc_loop_cond,&s_net_proc_loop_mutex); pthread_mutex_unlock(&s_net_proc_loop_mutex); log_it( L_DEBUG, "Waked up net proc thread"); + s_net_states_proc(l_net); } return NULL; } @@ -104,7 +173,7 @@ static void * s_net_proc_thread ( void * a_net) * @brief net_proc_start * @param a_cfg */ -static void s_net_proc_start( dap_chain_net_t * a_net ) +static void s_net_proc_thread_start( dap_chain_net_t * a_net ) { if ( pthread_create(& PVT(a_net)->proc_tid ,NULL, s_net_proc_thread, a_net) == 0 ){ log_it (L_NOTICE,"Network processing thread started"); @@ -139,13 +208,44 @@ dap_chain_net_t * dap_chain_net_new(const char * a_id, const char * a_name , { dap_chain_net_t * ret = DAP_NEW_Z_SIZE (dap_chain_net_t, sizeof (ret->pub)+ sizeof (dap_chain_net_pvt_t) ); ret->pub.name = strdup( a_name ); + if ( sscanf(a_id,"0x%016lx", &ret->pub.id.uint64 ) == 1 ){ - if (strcmp (a_node_role, "root")==0){ - PVT(ret)->node_role.enums = ROOT; + if (strcmp (a_node_role, "root_master")==0){ + PVT(ret)->node_role.enums = NODE_ROLE_ROOT_MASTER; + log_it (L_NOTICE, "Node role \"root master\" selected"); + } else if (strcmp( a_node_role,"root") == 0){ + PVT(ret)->node_role.enums = NODE_ROLE_ROOT; log_it (L_NOTICE, "Node role \"root\" selected"); + + } else if (strcmp( a_node_role,"archive") == 0){ + PVT(ret)->node_role.enums = NODE_ROLE_ARCHIVE; + log_it (L_NOTICE, "Node role \"archive\" selected"); + + } else if (strcmp( a_node_role,"cell_master") == 0){ + PVT(ret)->node_role.enums = NODE_ROLE_CELL_MASTER; + log_it (L_NOTICE, "Node role \"cell master\" selected"); + + }else if (strcmp( a_node_role,"master") == 0){ + PVT(ret)->node_role.enums = NODE_ROLE_MASTER; + log_it (L_NOTICE, "Node role \"master\" selected"); + + }else if (strcmp( a_node_role,"full") == 0){ + PVT(ret)->node_role.enums = NODE_ROLE_FULL; + log_it (L_NOTICE, "Node role \"full\" selected"); + + }else if (strcmp( a_node_role,"light") == 0){ + PVT(ret)->node_role.enums = NODE_ROLE_LIGHT; + log_it (L_NOTICE, "Node role \"light\" selected"); + + }else{ + log_it(L_ERROR,"Unknown node role \"%s\"",a_node_role); + DAP_DELETE(ret); + return NULL; } } else { log_it (L_ERROR, "Wrong id format (\"%s\"). Must be like \"0x0123456789ABCDE\"" , a_id ); + DAP_DELETE(ret); + return NULL; } return ret; @@ -184,16 +284,16 @@ int dap_chain_net_init() // UTXO model uint16_t l_utxo_flags = 0; switch ( PVT( l_net )->node_role.enums ) { - case ROOT_MASTER: - case ROOT: - case ARCHIVE: + case NODE_ROLE_ROOT_MASTER: + case NODE_ROLE_ROOT: + case NODE_ROLE_ARCHIVE: l_utxo_flags |= DAP_CHAIN_UTXO_CHECK_TOKEN_EMISSION; - case MASTER: + case NODE_ROLE_MASTER: l_utxo_flags |= DAP_CHAIN_UTXO_CHECK_CELLS_DS; - case CELL_MASTER: + case NODE_ROLE_CELL_MASTER: l_utxo_flags |= DAP_CHAIN_UTXO_CHECK_TOKEN_EMISSION; - case FULL: - case LIGHT: + case NODE_ROLE_FULL: + case NODE_ROLE_LIGHT: l_utxo_flags |= DAP_CHAIN_UTXO_CHECK_LOCAL_DS; } dap_chain_utxo_init(l_utxo_flags); @@ -236,21 +336,21 @@ int dap_chain_net_init() // Do specific role actions post-chain created switch ( PVT( l_net )->node_role.enums ) { - case ROOT_MASTER:{ + case NODE_ROLE_ROOT_MASTER:{ // Set to process everything in datum pool dap_chain_t * l_chain = NULL; DL_FOREACH(l_net->pub.chains, l_chain ) l_chain->is_datum_pool_proc = true; log_it(L_INFO,"Root master node role established"); } break; - case ROOT:{ + case NODE_ROLE_ROOT:{ // Set to process only zerochain dap_chain_id_t l_chain_id = {{0}}; dap_chain_t * l_chain = dap_chain_find_by_id(l_net->pub.id,l_chain_id); l_chain->is_datum_pool_proc = true; log_it(L_INFO,"Root node role established"); } break; - case CELL_MASTER: - case MASTER:{ + case NODE_ROLE_CELL_MASTER: + case NODE_ROLE_MASTER:{ // Set to process only plasma chain (id 0x0000000000000001 ) dap_chain_id_t l_chain_id = { .raw = {0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x01} }; dap_chain_t * l_chain = dap_chain_find_by_id(l_net->pub.id, l_chain_id ); @@ -258,10 +358,10 @@ int dap_chain_net_init() log_it(L_INFO,"Master node role established"); } break; - case FULL:{ + case NODE_ROLE_FULL:{ log_it(L_INFO,"Full node role established"); } break; - case LIGHT: + case NODE_ROLE_LIGHT: default: log_it(L_INFO,"Light node role established"); @@ -269,16 +369,19 @@ int dap_chain_net_init() // Add network to the list dap_chain_net_item_t * l_net_item = DAP_NEW_Z( dap_chain_net_item_t); + dap_chain_net_item_t * l_net_item2 = DAP_NEW_Z( dap_chain_net_item_t); snprintf(l_net_item->name,sizeof (l_net_item->name),"%s" ,dap_config_get_item_str(l_cfg , "general" , "name" )); l_net_item->chain_net = l_net; l_net_item->net_id.uint64 = l_net->pub.id.uint64; HASH_ADD_STR(s_net_items,name,l_net_item); - HASH_ADD(hh,s_net_items_ids,net_id,sizeof ( l_net_item->net_id),l_net_item); + + memcpy( l_net_item2,l_net_item,sizeof (*l_net_item)); + HASH_ADD(hh,s_net_items_ids,net_id,sizeof ( l_net_item2->net_id),l_net_item2); // Start the proc thread - s_net_proc_start(l_net); - log_it(L_NOTICE, "Сhain network initialized"); + s_net_proc_thread_start(l_net); + log_it(L_NOTICE, "Сhain network \"%s\" initialized",l_net_item->name); return 0; } } @@ -290,16 +393,6 @@ void dap_chain_net_deinit() { } -/** - * @brief dap_chain_net_load - * @param a_name - * @return - */ -dap_chain_net_t * dap_chain_net_load (const char * a_name) -{ - -} - /** * @brief dap_chain_net_by_name * @param a_name @@ -323,7 +416,7 @@ dap_chain_net_t * dap_chain_net_by_name( const char * a_name) dap_chain_net_t * dap_chain_net_by_id( dap_chain_net_id_t a_id) { dap_chain_net_item_t * l_net_item = NULL; - HASH_FIND(hh,s_net_items,&a_id,sizeof (a_id), l_net_item ); + HASH_FIND(hh,s_net_items_ids,&a_id,sizeof (a_id), l_net_item ); if ( l_net_item ) return l_net_item->chain_net; else diff --git a/dap_chain_net.h b/dap_chain_net.h old mode 100755 new mode 100644 index 7090f1cff1573905105c30ad670a9145ede82242..276a09b08a0b73a7f49bade1f00a554b7846566e --- a/dap_chain_net.h +++ b/dap_chain_net.h @@ -22,17 +22,27 @@ along with any DAP based project. If not, see <http://www.gnu.org/licenses/>. */ #pragma once +#include <sys/socket.h> +#include <netinet/in.h> #include <stdint.h> #include <string.h> #include "dap_chain_common.h" #include "dap_chain.h" -#include <sys/socket.h> -#include <netinet/in.h> #define DAP_CHAIN_NET_NAME_MAX 32 +typedef enum dap_chain_net_state{ + NET_STATE_BEGIN = 0, + NET_STATE_LINKS_CONNECTING, + NET_STATE_LINKS_ESTABLISHED, + NET_STATE_SYNC_GDB, + NET_STATE_SYNC_CHAINS, + NET_STATE_STAND_BY, +} dap_chain_net_state_t; + + typedef struct dap_chain_net{ struct { dap_chain_net_id_t id; @@ -49,6 +59,11 @@ void dap_chain_net_deinit(void); dap_chain_net_t * dap_chain_net_new (const char * a_id, const char * a_name, const char* a_node_role ); +int dap_chain_net_state_go_to(dap_chain_net_t * a_net, dap_chain_net_state_t a_new_state); + +inline static int dap_chain_net_start(dap_chain_net_t * a_net){ return dap_chain_net_state_go_to(a_net,NET_STATE_STAND_BY); } +inline static int dap_chain_net_stop(dap_chain_net_t * a_net) { return dap_chain_net_state_go_to(a_net,NET_STATE_BEGIN); } +inline static int dap_chain_net_links_establish(dap_chain_net_t * a_net) { return dap_chain_net_state_go_to(a_net,NET_STATE_LINKS_ESTABLISHED); } void dap_chain_net_delete( dap_chain_net_t * a_net); void dap_chain_net_proc_datapool (dap_chain_net_t * a_net); @@ -59,6 +74,8 @@ dap_chain_net_id_t dap_chain_net_id_by_name( const char * a_name); dap_chain_t * dap_chain_net_get_chain_by_name( dap_chain_net_t * l_net, const char * a_name); +void dap_chain_net_links_connect(dap_chain_net_t * a_net); + /** * @brief dap_chain_net_get_gdb_group_mempool * @param l_chain @@ -67,10 +84,13 @@ dap_chain_t * dap_chain_net_get_chain_by_name( dap_chain_net_t * l_net, const ch static inline char * dap_chain_net_get_gdb_group_mempool(dap_chain_t * l_chain) { dap_chain_net_t * l_net = dap_chain_net_by_id(l_chain->net_id); - const char c_mempool_group_str[]="mempool"; - size_t l_ret_size = strlen( l_net->pub.gdb_groups_prefix ) + 1 + - strlen( l_chain->name)+1+strlen(c_mempool_group_str)+1; - char * l_ret = DAP_NEW_Z_SIZE(char, l_ret_size); - snprintf( l_ret,l_ret_size,"%s.%s.%s",l_net->pub.gdb_groups_prefix,l_chain->name,c_mempool_group_str); + char * l_ret = NULL; + if ( l_net ) { + const char c_mempool_group_str[]="mempool"; + size_t l_ret_size = strlen( l_net->pub.gdb_groups_prefix ) + 1 + + strlen( l_chain->name)+1+strlen(c_mempool_group_str)+1; + l_ret = DAP_NEW_Z_SIZE(char, l_ret_size); + snprintf( l_ret,l_ret_size,"%s.%s.%s",l_net->pub.gdb_groups_prefix,l_chain->name,c_mempool_group_str); + } return l_ret; } diff --git a/dap_chain_node.c b/dap_chain_node.c old mode 100755 new mode 100644 diff --git a/dap_chain_node.h b/dap_chain_node.h old mode 100755 new mode 100644 diff --git a/dap_chain_node_cli.c b/dap_chain_node_cli.c old mode 100755 new mode 100644 index 068eec716c9b4c11e649b9495834f1200c68168e..22900a6ddeb8af5ef5d0ad4bac37f1a0248fc0b9 --- a/dap_chain_node_cli.c +++ b/dap_chain_node_cli.c @@ -452,16 +452,16 @@ int dap_chain_node_cli_init(dap_config_t * g_config) //server.sun_family = AF_UNIX; //strcpy(server.sun_path, SOCKET_FILE); dap_chain_node_cli_cmd_item_create ("global_db", com_global_db, "Work with global database", - "global_db wallet_info set -addr <wallet address> -cell <cell id> \n\n" - "global_db cells add -cell <cell id> \n\n" - "global_db node add -addr {<node address> | -alias <node alias>} -cell <cell id> {-ipv4 <ipv4 external address> | -ipv6 <ipv6 external address>}\n\n" - "global_db node del -addr <node address> | -alias <node alias>\n\n" - "global_db node link {add|del} {-addr <node address> | -alias <node alias>} -link <node address>\n\n" - "global_db node dump\n\n" - "global_db node dump -addr <node address> | -alias <node alias>\n\n" - "global_db node get\n\n" - "global_db node set -addr <node address> | -alias <node alias>\n\n" - "global_db node remote_set -addr <node address> | -alias <node alias>\n"); + "global_db wallet_info set -addr <wallet address> -cell <cell id> \n\n" + "global_db cells add -cell <cell id> \n\n" + "global_db node add -net <net name> -addr {<node address> | -alias <node alias>} -cell <cell id> {-ipv4 <ipv4 external address> | -ipv6 <ipv6 external address>}\n\n" + "global_db node del -net <net name> -addr <node address> | -alias <node alias>\n\n" + "global_db node link {add|del} -net <net name> {-addr <node address> | -alias <node alias>} -link <node address>\n\n" + "global_db node dump -net <net name>\n\n" + "global_db node dump -net <net name> -addr <node address> | -alias <node alias>\n\n" + "global_db node get -net <net name>\n\n" + "global_db node set -net <net name> -addr <node address> | -alias <node alias>\n\n" + "global_db node remote_set -addr <node address> | -alias <node alias>\n"); dap_chain_node_cli_cmd_item_create ("node", com_node, "Work with node", "node alias {<node address> | -alias <node alias>}\n\n" "node connect {<node address> | -alias <node alias>}\n\n" @@ -480,23 +480,31 @@ int dap_chain_node_cli_init(dap_config_t * g_config) "? [<command>]\n" "\tObtain help for <command> or get the total list of the commands\n" ); - dap_chain_node_cli_cmd_item_create ("wallet", com_tx_wallet, "Wallet info", "wallet [list | info -addr <addr> -w <wallet_name>]\n"); + dap_chain_node_cli_cmd_item_create ("wallet", com_tx_wallet, "Wallet info", "wallet [list | info [ -addr <addr> | -w <wallet_name>] \n"); // Token commands dap_chain_node_cli_cmd_item_create ("token_decl", com_token_decl, "Token declaration", - "token_decl net <net name> chain <chain name> token <token ticker> total_supply <total supply> sign_total <sign total> sign_valid <sign valid> certs <certs list>\n" + "token_decl -net <net name> -chain <chain name> token <token ticker> total_supply <total supply> sign_total <sign total> sign_valid <sign valid> certs <certs list>\n" "\t Declare new token for <netname>:<chain name> with ticker <token ticker>, maximum emission <total supply> and <sign valid> from <sign total> signatures on valid emission\n" - "token_decl_sign net <net name> chain <chain name> datum <datum_hash> certs <certs list>\n" + "token_decl_sign -net <net name> -chain <chain name> datum <datum_hash> certs <certs list>\n" "\t Sign existent <datum hash> in mempool with <certs list>\n" ); dap_chain_node_cli_cmd_item_create ("token_decl_sign", com_token_decl_sign, "Token declaration add sign", - "token_decl_sign net <net name> chain <chain name> datum <datum_hash> certs <certs list>\n" + "token_decl_sign -net <net name> -chain <chain name> datum <datum_hash> certs <certs list>\n" "\t Sign existent <datum hash> in mempool with <certs list>\n" ); dap_chain_node_cli_cmd_item_create ("token_emit", com_token_emit, "Token emission", - "token_emit net <net name> chain_emission <chain for emission> chain_base_tx <chain for base tx> addr <addr> token <token ticker> certs <cert> emission_value <val>\n"); + "token_emit -net <net name> -chain_emission <chain for emission> -chain_base_tx <chain for base tx> addr <addr> token <token ticker> certs <cert> emission_value <val>\n"); + + dap_chain_node_cli_cmd_item_create ("mempool_list", com_mempool_list, "List mempool entries for selected chain network and chain id", + "mempool_list -net <net name> -chain <chain name>\n"); + + dap_chain_node_cli_cmd_item_create ("mempool_proc", com_mempool_proc, "Proc mempool entries for selected chain network and chain id", + "mempool_list -net <net name> -chain <chain name>\n"); + + // Transaction commands dap_chain_node_cli_cmd_item_create ("tx_create", com_tx_create, "Make transaction", diff --git a/dap_chain_node_cli.h b/dap_chain_node_cli.h old mode 100755 new mode 100644 diff --git a/dap_chain_node_cli_cmd.c b/dap_chain_node_cli_cmd.c old mode 100755 new mode 100644 index c4cc08cf5351e55da0a58d1ed45880ca46436f12..e168a31d396a60b49b512b0787ecda423a615304 --- a/dap_chain_node_cli_cmd.c +++ b/dap_chain_node_cli_cmd.c @@ -838,182 +838,195 @@ int com_node(int argc, const char ** argv, char **str_reply) dap_chain_node_addr_t address; memset(&address, 0, sizeof(dap_chain_node_addr_t)); const char *addr_str = NULL, *alias_str = NULL; + const char * l_net_str = NULL; // find addr, alias dap_chain_node_cli_find_option_val(argv, arg_index, argc, "-addr", &addr_str); dap_chain_node_cli_find_option_val(argv, arg_index, argc, "-alias", &alias_str); + dap_chain_node_cli_find_option_val(argv, arg_index, argc, "-net", &l_net_str); dap_digit_from_string(addr_str, address.raw, sizeof(address.raw)); + if( l_net_str == NULL){ + dap_chain_node_cli_set_reply_text(str_reply, "No -net <net name> option in command %s", argv[1]); + return -11; + } + + dap_chain_net_t * l_net = dap_chain_net_by_name(l_net_str); + + if( l_net == NULL){ + dap_chain_node_cli_set_reply_text(str_reply, "%s: Can't find such network %s", argv[1], l_net_str); + return -12; + } + switch (cmd_num) - { - // add alias - case CMD_ALIAS: - if(alias_str) { - if(addr_str) { - // add alias - if(!dap_chain_node_alias_register(alias_str, &address)) - log_it(L_WARNING, "can't save alias %s", alias_str); + { + // add alias + case CMD_ALIAS: + if(alias_str) { + if(addr_str) { + // add alias + if(!dap_chain_node_alias_register(alias_str, &address)) + log_it(L_WARNING, "can't save alias %s", alias_str); + else { + dap_chain_node_cli_set_reply_text(str_reply, "alias mapped successfully"); + } + } else { - dap_chain_node_cli_set_reply_text(str_reply, "alias mapped successfully"); + dap_chain_node_cli_set_reply_text(str_reply, "alias can't be mapped because -addr is not found"); + return -1; } } else { - dap_chain_node_cli_set_reply_text(str_reply, "alias can't be mapped because -addr is not found"); + dap_chain_node_cli_set_reply_text(str_reply, "alias can't be mapped because -alias is not found"); return -1; } - } - else { - dap_chain_node_cli_set_reply_text(str_reply, "alias can't be mapped because -alias is not found"); - return -1; - } - break; - // make connect - case CMD_CONNECT: { - // get address from alias if addr not defined - if(alias_str && !address.uint64) { - dap_chain_node_addr_t *address_tmp = dap_chain_node_addr_get_by_alias(alias_str); - if(address_tmp) { - memcpy(&address, address_tmp, sizeof(address_tmp)); - DAP_DELETE(address_tmp); + break; + // make connect + case CMD_CONNECT: { + // get address from alias if addr not defined + if(alias_str && !address.uint64) { + dap_chain_node_addr_t *address_tmp = dap_chain_node_addr_get_by_alias(alias_str); + if(address_tmp) { + memcpy(&address, address_tmp, sizeof(address_tmp)); + DAP_DELETE(address_tmp); + } + else { + dap_chain_node_cli_set_reply_text(str_reply, "no address found by alias"); + return -1; + } } - else { - dap_chain_node_cli_set_reply_text(str_reply, "no address found by alias"); + if(!address.uint64) { + dap_chain_node_cli_set_reply_text(str_reply, "addr not found"); return -1; } - } - if(!address.uint64) { - dap_chain_node_cli_set_reply_text(str_reply, "addr not found"); - return -1; - } - // get cur node addr - uint64_t l_cur_node_addr = dap_db_get_cur_node_addr(); //0x12345 - if(!l_cur_node_addr) { - dap_chain_node_cli_set_reply_text(str_reply, "node has no address"); - return -1; - } + // get cur node addr + uint64_t l_cur_node_addr = dap_db_get_cur_node_addr(); //0x12345 + if(!l_cur_node_addr) { + dap_chain_node_cli_set_reply_text(str_reply, "node has no address"); + return -1; + } - dap_chain_node_info_t *node_info = dap_chain_node_info_read(&address, str_reply); - if(!node_info) { - return -1; - } - // start connect - dap_chain_node_client_t *client = dap_chain_node_client_connect(node_info); - if(!client) { - dap_chain_node_cli_set_reply_text(str_reply, "can't connect"); - DAP_DELETE(node_info); - return -1; - } - // wait connected - int timeout_ms = 15000; //15 sec = 15000 ms - int res = chain_node_client_wait(client, NODE_CLIENT_STATE_CONNECTED, timeout_ms); - if(res != 1) { - dap_chain_node_cli_set_reply_text(str_reply, "no response from node"); - // clean client struct - dap_chain_node_client_close(client); - DAP_DELETE(node_info); - return -1; - } + dap_chain_node_info_t *node_info = dap_chain_node_info_read(&address, str_reply); + if(!node_info) { + return -1; + } + // start connect + dap_chain_node_client_t *client = dap_chain_node_client_connect(node_info); + if(!client) { + dap_chain_node_cli_set_reply_text(str_reply, "can't connect"); + DAP_DELETE(node_info); + return -1; + } + // wait connected + int timeout_ms = 15000; //15 sec = 15000 ms + int res = chain_node_client_wait(client, NODE_CLIENT_STATE_CONNECTED, timeout_ms); + if(res != 1) { + dap_chain_node_cli_set_reply_text(str_reply, "no response from node"); + // clean client struct + dap_chain_node_client_close(client); + DAP_DELETE(node_info); + return -1; + } - // send request - size_t l_data_size_out = 0; - // Get last timestamp in log - time_t l_timestamp_start = dap_db_log_get_last_timestamp(); - size_t l_data_send_len = 0; - uint8_t *l_data_send = dap_stream_ch_chain_net_make_packet(l_cur_node_addr, node_info->hdr.address.uint64, - l_timestamp_start, NULL, 0, &l_data_send_len); - - uint8_t l_ch_id = dap_stream_ch_chain_net_get_id(); // Channel id for global_db sync - res = dap_chain_node_client_send_chain_net_request(client, l_ch_id, - STREAM_CH_CHAIN_NET_PKT_TYPE_GLOBAL_DB_REQUEST_SYNC, l_data_send, l_data_send_len); //, NULL); - DAP_DELETE(l_data_send); - if(res != 1) { - dap_chain_node_cli_set_reply_text(str_reply, "no request sent"); - // clean client struct - dap_chain_node_client_close(client); + // send request + size_t l_data_size_out = 0; + // Get last timestamp in log + time_t l_timestamp_start = dap_db_log_get_last_timestamp(); + size_t l_data_send_len = 0; + uint8_t *l_data_send = dap_stream_ch_chain_net_make_packet(l_cur_node_addr, node_info->hdr.address.uint64, + l_timestamp_start, NULL, 0, &l_data_send_len); + + uint8_t l_ch_id = dap_stream_ch_chain_net_get_id(); // Channel id for global_db sync + res = dap_chain_node_client_send_chain_net_request(client, l_ch_id, + STREAM_CH_CHAIN_NET_PKT_TYPE_GLOBAL_DB_REQUEST_SYNC, l_data_send, l_data_send_len); //, NULL); + DAP_DELETE(l_data_send); + if(res != 1) { + dap_chain_node_cli_set_reply_text(str_reply, "no request sent"); + // clean client struct + dap_chain_node_client_close(client); + DAP_DELETE(node_info); + return -1; + } + + // wait for finishing of request + timeout_ms = 120000; // 2 min = 120 sec = 120 000 ms + // TODO add progress info to console + res = chain_node_client_wait(client, NODE_CLIENT_STATE_END, timeout_ms); DAP_DELETE(node_info); - return -1; - } + dap_client_disconnect(client->client); + dap_chain_node_client_close(client); + switch (res) { + case 0: + dap_chain_node_cli_set_reply_text(str_reply, "timeout"); + return -1; + case 1: + dap_chain_node_cli_set_reply_text(str_reply, "nodes sync completed"); + return 0; + default: + dap_chain_node_cli_set_reply_text(str_reply, "error"); + return -1; + } - // wait for finishing of request - timeout_ms = 120000; // 2 min = 120 sec = 120 000 ms - // TODO add progress info to console - res = chain_node_client_wait(client, NODE_CLIENT_STATE_END, timeout_ms); - DAP_DELETE(node_info); - dap_client_disconnect(client->client); - dap_chain_node_client_close(client); - switch (res) { - case 0: - dap_chain_node_cli_set_reply_text(str_reply, "timeout"); - return -1; - case 1: - dap_chain_node_cli_set_reply_text(str_reply, "nodes sync completed"); - return 0; - default: - dap_chain_node_cli_set_reply_text(str_reply, "error"); - return -1; } - - } - break; - // make handshake - case CMD_HANDSHAKE: { - // get address from alias if addr not defined - if(alias_str && !address.uint64) { - dap_chain_node_addr_t *address_tmp = dap_chain_node_addr_get_by_alias(alias_str); - if(address_tmp) { - memcpy(&address, address_tmp, sizeof(address_tmp)); - DAP_DELETE(address_tmp); + break; + // make handshake + case CMD_HANDSHAKE: { + // get address from alias if addr not defined + if(alias_str && !address.uint64) { + dap_chain_node_addr_t *address_tmp = dap_chain_node_addr_get_by_alias(alias_str); + if(address_tmp) { + memcpy(&address, address_tmp, sizeof(address_tmp)); + DAP_DELETE(address_tmp); + } + else { + dap_chain_node_cli_set_reply_text(str_reply, "no address found by alias"); + return -1; + } } - else { - dap_chain_node_cli_set_reply_text(str_reply, "no address found by alias"); + if(!address.uint64) { + dap_chain_node_cli_set_reply_text(str_reply, "addr not found"); return -1; } - } - if(!address.uint64) { - dap_chain_node_cli_set_reply_text(str_reply, "addr not found"); - return -1; - } - dap_chain_node_info_t *node_info = dap_chain_node_info_read(&address, str_reply); - if(!node_info) - return -1; - int timeout_ms = 10000; //10 sec = 10000 ms - // start handshake - dap_chain_node_client_t *client = dap_chain_node_client_connect(node_info); - if(!client) { - dap_chain_node_cli_set_reply_text(str_reply, "can't connect"); - DAP_DELETE(node_info); - return -1; - } - // wait handshake - int res = chain_node_client_wait(client, NODE_CLIENT_STATE_CONNECTED, timeout_ms); - if(res != 1) { - dap_chain_node_cli_set_reply_text(str_reply, "no response from node"); - // clean client struct - dap_chain_node_client_close(client); + dap_chain_node_info_t *node_info = dap_chain_node_info_read(&address, str_reply); + if(!node_info) + return -1; + int timeout_ms = 10000; //10 sec = 10000 ms + // start handshake + dap_chain_node_client_t *client = dap_chain_node_client_connect(node_info); + if(!client) { + dap_chain_node_cli_set_reply_text(str_reply, "can't connect"); + DAP_DELETE(node_info); + return -1; + } + // wait handshake + int res = chain_node_client_wait(client, NODE_CLIENT_STATE_CONNECTED, timeout_ms); + if(res != 1) { + dap_chain_node_cli_set_reply_text(str_reply, "no response from node"); + // clean client struct + dap_chain_node_client_close(client); + DAP_DELETE(node_info); + return -1; + } DAP_DELETE(node_info); - return -1; - } - DAP_DELETE(node_info); - //Add new established connection in the list - int ret = chain_node_client_list_add(&address, client); - switch (ret) - { - case -1: - dap_chain_node_client_close(client); - dap_chain_node_cli_set_reply_text(str_reply, "connection established, but not saved"); - return -1; - case -2: - dap_chain_node_client_close(client); - dap_chain_node_cli_set_reply_text(str_reply, "connection already present"); - return -1; - } - dap_chain_node_cli_set_reply_text(str_reply, "connection established"); - } - break; + //Add new established connection in the list + int ret = dap_chain_node_client_list_add(&address, client); + switch (ret) + { + case -1: + dap_chain_node_client_close(client); + dap_chain_node_cli_set_reply_text(str_reply, "connection established, but not saved"); + return -1; + case -2: + dap_chain_node_client_close(client); + dap_chain_node_cli_set_reply_text(str_reply, "connection already present"); + return -1; + } + dap_chain_node_cli_set_reply_text(str_reply, "connection established"); + } break; } return 0; } @@ -1375,29 +1388,30 @@ int s_values_parse_net_chain(int *a_arg_index,int argc, const char ** argv, char const char * l_chain_str = NULL; const char * l_net_str = NULL; // Net name - dap_chain_node_cli_find_option_val(argv, *a_arg_index, argc, "net", &l_net_str); + dap_chain_node_cli_find_option_val(argv, *a_arg_index, argc, "-net", &l_net_str); // Chain name - dap_chain_node_cli_find_option_val(argv, *a_arg_index, argc, "chain", &l_chain_str); + dap_chain_node_cli_find_option_val(argv, *a_arg_index, argc, "-chain", &l_chain_str); // Select chain network if(!l_net_str) { - dap_chain_node_cli_set_reply_text(a_str_reply, "token_create requires parameter 'net'"); + dap_chain_node_cli_set_reply_text(a_str_reply, "%s requires parameter 'net'",argv[0]); return -42; }else { if ( ( *a_net = dap_chain_net_by_name(l_net_str) ) == NULL ){ // Can't find such network - dap_chain_node_cli_set_reply_text(a_str_reply, "token_create requires parameter 'net' to be valid chain network name"); + dap_chain_node_cli_set_reply_text(a_str_reply, "%s cand find network \"%s\"",argv[0],l_net_str); return -43; } } // Select chain if(!l_chain_str) { - dap_chain_node_cli_set_reply_text(a_str_reply, "token_create requires parameter 'chain'"); + dap_chain_node_cli_set_reply_text(a_str_reply, "%s requires parameter 'chain'",argv[0]); return -44; }else { if ( ( *a_chain = dap_chain_net_get_chain_by_name(*a_net, l_chain_str ) ) == NULL ){ // Can't find such chain - dap_chain_node_cli_set_reply_text(a_str_reply, "token_create requires parameter 'chain' to be valid chain name in chain net %s",l_net_str); + dap_chain_node_cli_set_reply_text(a_str_reply, "%s requires parameter 'chain' to be valid chain name in chain net %s", + argv[0] , l_net_str); return -45; } } @@ -1577,16 +1591,83 @@ int com_token_decl_sign(int argc, const char ** argv, char ** a_str_reply) * @param str_reply * @return */ -int com_mempool_list(int argc, const char ** argv, char ** str_reply) +int com_mempool_list(int argc, const char ** argv, char ** a_str_reply) { int arg_index = 1; dap_chain_t * l_chain; dap_chain_net_t * l_net = NULL; - if (s_values_parse_net_chain(&arg_index,argc,argv,str_reply,&l_chain, &l_net) < 0) + if (s_values_parse_net_chain(&arg_index,argc,argv,a_str_reply,&l_chain, &l_net) < 0) return -1; char * l_gdb_group_mempool = dap_chain_net_get_gdb_group_mempool(l_chain); + size_t l_objs_size = 0; + dap_global_db_obj_t ** l_objs = dap_chain_global_db_gr_load(l_gdb_group_mempool,&l_objs_size); + dap_string_t * l_str_tmp = dap_string_new(NULL); + dap_string_append_printf(l_str_tmp,"%s.%s: Found %u records :\n",l_net->pub.name,l_chain->name,l_objs_size); + for ( size_t i = 0; i< l_objs_size; i++){ + dap_chain_datum_t * l_datum =(dap_chain_datum_t* ) l_objs[i]->value; + char buf[50]; + time_t l_ts_create = (time_t) l_datum->header.ts_create; + dap_string_append_printf(l_str_tmp,"0x%s: type_id=%s ts_create=%s data_size=%u\n", + l_objs[i]->key, c_datum_type_str[l_datum->header.type_id], + ctime_r( &l_ts_create,buf ),l_datum->header.data_size ); + } + dap_chain_global_db_objs_delete(l_objs); + dap_chain_node_cli_set_reply_text(a_str_reply, l_str_tmp->str); + dap_string_free(l_str_tmp,false); + return 0; +} + +/** + * @brief com_mempool_proc + * @param argc + * @param argv + * @param a_str_reply + * @return + */ +int com_mempool_proc(int argc, const char ** argv, char ** a_str_reply) +{ + int arg_index = 1; + dap_chain_t * l_chain; + dap_chain_net_t * l_net = NULL; + + if (s_values_parse_net_chain(&arg_index,argc,argv,a_str_reply,&l_chain, &l_net) < 0) + return -1; + + char * l_gdb_group_mempool = dap_chain_net_get_gdb_group_mempool(l_chain); + size_t l_objs_size = 0; + dap_global_db_obj_t ** l_objs = dap_chain_global_db_gr_load(l_gdb_group_mempool,&l_objs_size); + dap_string_t * l_str_tmp = dap_string_new(NULL); + if ( l_objs_size ) { + dap_string_append_printf(l_str_tmp,"%s.%s: Found %u records :\n",l_net->pub.name,l_chain->name); + + + size_t l_datums_size = l_objs_size; + dap_chain_datum_t ** l_datums = DAP_NEW_Z_SIZE(dap_chain_datum_t*,sizeof(dap_chain_datum_t*)*l_datums_size); + for ( size_t i = 0; i< l_objs_size; i++){ + dap_chain_datum_t * l_datum = (dap_chain_datum_t* ) l_objs[i]->value; + l_datums[i] = l_datum; + char buf[50]; + time_t l_ts_create = (time_t) l_datum->header.ts_create; + dap_string_append_printf(l_str_tmp,"0x%s: type_id=%s ts_create=%s data_size=%u\n", + l_objs[i]->key, c_datum_type_str[l_datum->header.type_id], + ctime_r( &l_ts_create,buf ),l_datum->header.data_size ); + } + size_t l_objs_processed = l_chain->callback_datums_pool_proc(l_chain,l_datums,l_datums_size); + // Delete processed objects + for ( size_t i = 0; i< l_objs_processed; i++){ + dap_chain_global_db_gr_del(l_objs[i]->key,l_gdb_group_mempool); + dap_string_append_printf(l_str_tmp,"New event created, removed datum 0x%s from mempool \n",l_objs[i]->key); + } + dap_chain_global_db_objs_delete(l_objs); + + dap_chain_node_cli_set_reply_text(a_str_reply, l_str_tmp->str); + dap_string_free(l_str_tmp,false); + }else { + dap_chain_node_cli_set_reply_text(a_str_reply, "%s.^s: No records in mempool",l_net->pub.name,l_chain->name); + } + return 0; } /** diff --git a/dap_chain_node_cli_cmd.h b/dap_chain_node_cli_cmd.h old mode 100755 new mode 100644 index 301125aa2e3092c6e6188b8cbef1fb931b648b7d..3428ccb42aa6d3c2fb740fa744a795d72687e29c --- a/dap_chain_node_cli_cmd.h +++ b/dap_chain_node_cli_cmd.h @@ -110,3 +110,6 @@ int com_tx_cond_create(int argc, const char ** argv, char **str_reply); * Verifing transaction */ int com_tx_verify(int argc, const char ** argv, char **str_reply); + +int com_mempool_list(int argc, const char ** argv, char ** a_str_reply); +int com_mempool_proc(int argc, const char ** argv, char ** a_str_reply); diff --git a/dap_chain_node_client.c b/dap_chain_node_client.c old mode 100755 new mode 100644 diff --git a/dap_chain_node_client.h b/dap_chain_node_client.h old mode 100755 new mode 100644 diff --git a/dap_chain_node_remote.c b/dap_chain_node_remote.c old mode 100755 new mode 100644 index f3dccdc2f4df3fb5b10f327cfadd8b1344d824db..be119272751f27f5a7cf2cadc868371b8c80c872 --- a/dap_chain_node_remote.c +++ b/dap_chain_node_remote.c @@ -28,14 +28,14 @@ #include "dap_common.h" #include "dap_chain_node_remote.h" -typedef struct dap_chain_utxo_tx_item { +typedef struct dap_chain_node_link_item { dap_chain_node_addr_t address; dap_chain_node_client_t *client; UT_hash_handle hh; -} list_linked_item_t; +} dap_chain_node_link_item_t; // List of connections -static list_linked_item_t *conn_list = NULL; +static dap_chain_node_link_item_t *conn_list = NULL; // for separate access to connect_list static pthread_mutex_t connect_list_mutex = PTHREAD_MUTEX_INITIALIZER; @@ -46,16 +46,16 @@ static pthread_mutex_t connect_list_mutex = PTHREAD_MUTEX_INITIALIZER; * * return 0 OK, -1 error, -2 already present */ -int chain_node_client_list_add(dap_chain_node_addr_t *a_address, dap_chain_node_client_t *a_client) +int dap_chain_node_client_list_add(dap_chain_node_addr_t *a_address, dap_chain_node_client_t *a_client) { int l_ret = 0; if(!a_address || !a_client) return -1; - list_linked_item_t *item_tmp = NULL; + dap_chain_node_link_item_t *item_tmp = NULL; pthread_mutex_lock(&connect_list_mutex); HASH_FIND(hh, conn_list, a_address, sizeof(dap_chain_node_addr_t), item_tmp); // address already in the hash? if(item_tmp == NULL) { - item_tmp = DAP_NEW(list_linked_item_t); + item_tmp = DAP_NEW(dap_chain_node_link_item_t); item_tmp->address.uint64 = a_address->uint64; item_tmp->client = a_client; HASH_ADD(hh, conn_list, address, sizeof(dap_chain_node_addr_t), item_tmp); // address: name of key field @@ -79,7 +79,7 @@ int chain_node_client_list_del(dap_chain_node_addr_t *address) int ret = -1; if(!address) return -1; - list_linked_item_t *item_tmp; + dap_chain_node_link_item_t *item_tmp; pthread_mutex_lock(&connect_list_mutex); HASH_FIND(hh, conn_list, address, sizeof(dap_chain_node_addr_t), item_tmp); if(item_tmp != NULL) { @@ -105,7 +105,7 @@ int chain_node_client_list_del(dap_chain_node_addr_t *address) void chain_node_client_list_del_all(void) { int ret = -1; - list_linked_item_t *iter_current, *item_tmp; + dap_chain_node_link_item_t *iter_current, *item_tmp; pthread_mutex_lock(&connect_list_mutex); HASH_ITER(hh, conn_list , iter_current, item_tmp) { // close connection @@ -127,7 +127,7 @@ const dap_chain_node_client_t* chain_node_client_find(dap_chain_node_addr_t *add if(!address) return NULL; dap_chain_node_client_t *client_ret = NULL; - list_linked_item_t *item_tmp; + dap_chain_node_link_item_t *item_tmp; pthread_mutex_lock(&connect_list_mutex); HASH_FIND(hh, conn_list, address, sizeof(dap_chain_node_addr_t), item_tmp); // address already in the hash? if(item_tmp != NULL) { diff --git a/dap_chain_node_remote.h b/dap_chain_node_remote.h old mode 100755 new mode 100644 index ca175db0d008490f1c57dc5d5487c3039f6ce72a..ce8aedc93262ac6e2a2bf67cb27529d78726d50c --- a/dap_chain_node_remote.h +++ b/dap_chain_node_remote.h @@ -31,7 +31,7 @@ * * return 0 OK, -1 error, -2 already present */ -int chain_node_client_list_add(dap_chain_node_addr_t *address, dap_chain_node_client_t *client); +int dap_chain_node_client_list_add(dap_chain_node_addr_t *address, dap_chain_node_client_t *client); /** * Delete established connection from the list diff --git a/iputils/iputils.c b/iputils/iputils.c old mode 100755 new mode 100644 diff --git a/iputils/iputils.h b/iputils/iputils.h old mode 100755 new mode 100644 diff --git a/iputils/ping.c b/iputils/ping.c old mode 100755 new mode 100644 diff --git a/iputils/ping.h b/iputils/ping.h old mode 100755 new mode 100644 diff --git a/iputils/ping6_common.c b/iputils/ping6_common.c old mode 100755 new mode 100644 diff --git a/iputils/ping_common.c b/iputils/ping_common.c old mode 100755 new mode 100644 diff --git a/iputils/tracepath.c b/iputils/tracepath.c old mode 100755 new mode 100644 diff --git a/iputils/traceroute/as_lookups.c b/iputils/traceroute/as_lookups.c old mode 100755 new mode 100644 diff --git a/iputils/traceroute/clif.c b/iputils/traceroute/clif.c old mode 100755 new mode 100644 diff --git a/iputils/traceroute/clif.h b/iputils/traceroute/clif.h old mode 100755 new mode 100644 diff --git a/iputils/traceroute/csum.c b/iputils/traceroute/csum.c old mode 100755 new mode 100644 diff --git a/iputils/traceroute/extension.c b/iputils/traceroute/extension.c old mode 100755 new mode 100644 diff --git a/iputils/traceroute/flowlabel.h b/iputils/traceroute/flowlabel.h old mode 100755 new mode 100644 diff --git a/iputils/traceroute/mod-dccp.c b/iputils/traceroute/mod-dccp.c old mode 100755 new mode 100644 diff --git a/iputils/traceroute/mod-icmp.c b/iputils/traceroute/mod-icmp.c old mode 100755 new mode 100644 diff --git a/iputils/traceroute/mod-raw.c b/iputils/traceroute/mod-raw.c old mode 100755 new mode 100644 diff --git a/iputils/traceroute/mod-tcp.c b/iputils/traceroute/mod-tcp.c old mode 100755 new mode 100644 diff --git a/iputils/traceroute/mod-tcpconn.c b/iputils/traceroute/mod-tcpconn.c old mode 100755 new mode 100644 diff --git a/iputils/traceroute/mod-udp.c b/iputils/traceroute/mod-udp.c old mode 100755 new mode 100644 diff --git a/iputils/traceroute/module.c b/iputils/traceroute/module.c old mode 100755 new mode 100644 diff --git a/iputils/traceroute/poll.c b/iputils/traceroute/poll.c old mode 100755 new mode 100644 diff --git a/iputils/traceroute/random.c b/iputils/traceroute/random.c old mode 100755 new mode 100644 diff --git a/iputils/traceroute/time.c b/iputils/traceroute/time.c old mode 100755 new mode 100644 diff --git a/iputils/traceroute/traceroute.c b/iputils/traceroute/traceroute.c old mode 100755 new mode 100644 diff --git a/iputils/traceroute/traceroute.h b/iputils/traceroute/traceroute.h old mode 100755 new mode 100644