Newer
Older
* Dmitriy A. Gearasimov <gerasimov.dmitriy@demlabs.net>
* Kelvin Project https://github.com/kelvinblockchain
* Copyright (c) 2017-2019
* All rights reserved.
This file is part of DAP (Deus Applications Prototypes) the open source project
DAP (Deus Applicaions Prototypes) is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
DAP is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with any DAP based project. If not, see <http://www.gnu.org/licenses/>.
*/
#include <sys/types.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <errno.h>
#include "uthash.h"
#include "utlist.h"
#include "dap_string.h"
#include "dap_chain_common.h"
#include "dap_strfuncs.h"
#include "dap_list.h"
#include "dap_string.h"
#include "dap_chain_node.h"
#include "dap_chain_global_db.h"
#include "dap_chain_node_cli_cmd.h"
#include "dap_chain_datum_tx_items.h"
#include "dap_chain_utxo.h"
#include "dap_chain_mempool.h"
#include "dap_chain_global_db.h"
#include "dap_chain_global_db_remote.h"
#include "dap_stream_ch_chain_net.h"
#include "dap_stream_ch_chain.h"
#include "dap_stream_ch_chain_pkt.h"
#include "dap_stream_ch_chain_net_pkt.h"
/**
* Find in base addr by alias
*
* return addr, NULL if not found
*/
dap_chain_node_addr_t* dap_chain_node_addr_get_by_alias(const char *a_alias)
dap_chain_node_addr_t *l_addr = NULL;
if(!a_alias)
const char *a_key = a_alias;
l_addr = (dap_chain_node_addr_t*) (void*) dap_chain_global_db_gr_get(a_key, &l_addr_size, GROUP_GLOBAL_ALIAS);
if(l_addr_size != sizeof(dap_chain_node_addr_t)) {
// l_addr = DAP_NEW_Z(dap_chain_node_addr_t);
// if(hex2bin((char*) l_addr, (const unsigned char *) addr_str, sizeof(dap_chain_node_addr_t) * 2) == -1) {
DAP_DELETE(l_addr);
// l_addr = NULL;
// }
// DAP_DELETE(addr_str);
return l_addr;
* return list of addr, NULL if not found
static dap_list_t* get_aliases_by_name(dap_chain_node_addr_t *a_addr)
dap_list_t *list_aliases = NULL;
size_t data_size = 0;
// read all aliases
dap_global_db_obj_t **objs = dap_chain_global_db_gr_load(GROUP_GLOBAL_ALIAS, &data_size);
for(size_t i = 0; i < data_size; i++) {
//dap_chain_node_addr_t addr_i;
dap_global_db_obj_t *obj = objs[i];
dap_chain_node_addr_t *l_addr = (dap_chain_node_addr_t*) (void*) obj->value;
if(l_addr && obj->value_len == sizeof(dap_chain_node_addr_t) && a_addr->uint64 == l_addr->uint64) {
list_aliases = dap_list_prepend(list_aliases, strdup(obj->key));
/* char *addr_str = obj->value;
if(addr_str && strlen(addr_str) == sizeof(dap_chain_node_addr_t) * 2) {
//addr_i = DAP_NEW_Z(dap_chain_node_addr_t);
if(hex2bin((char*) &addr_i, (const unsigned char *) addr_str, sizeof(dap_chain_node_addr_t) * 2) == -1) {
continue;
}
if(a_addr->uint64 == addr_i.uint64) {
list_aliases = dap_list_prepend(list_aliases, strdup(obj->key));
}
}*/
dap_chain_global_db_objs_delete(objs);
return list_aliases;
}
static dap_chain_node_addr_t* com_global_db_get_addr(dap_chain_node_info_t *node_info,
dap_chain_node_addr_t *addr, const char *alias_str)
{
dap_chain_node_addr_t *address = NULL;
address = dap_chain_node_addr_get_by_alias(alias_str);
static dap_chain_node_info_t* dap_chain_node_info_read_and_reply(dap_chain_node_addr_t *address, char **str_reply)
char *l_key = dap_chain_node_addr_to_hash_str(address);
dap_chain_node_cli_set_reply_text(str_reply, "can't calculate hash of addr");
size_t node_info_size = 0;
dap_chain_node_info_t *node_info;
node_info = (dap_chain_node_info_t *) dap_chain_global_db_gr_get(l_key, &node_info_size, GROUP_GLOBAL_ADDRS_LEASED);
dap_chain_node_cli_set_reply_text(str_reply, "node not found in base");
size_t node_info_size_must_be = dap_chain_node_info_get_size(node_info);
if(node_info_size_must_be != node_info_size) {
dap_chain_node_cli_set_reply_text(str_reply, "node has bad size in base=%u (must be %u)", node_info_size,
node_info_size_must_be);
DAP_DELETE(node_info);
DAP_DELETE(l_key);
return NULL;
// dap_chain_node_info_t *node_info = dap_chain_node_info_deserialize(str, (str) ? strlen(str) : 0);
// if(!node_info) {
// set_reply_text(str_reply, "node has invalid format in base");
// }
// DAP_DELETE(str);
DAP_DELETE(l_key);
static bool dap_chain_node_info_save_and_reply(dap_chain_node_info_t *node_info, char **str_reply)
{
if(!node_info || !node_info->hdr.address.uint64) {
dap_chain_node_cli_set_reply_text(str_reply, "node addr not found");
char *a_key = dap_chain_node_addr_to_hash_str(&node_info->hdr.address);
dap_chain_node_cli_set_reply_text(str_reply, "can't calculate hash for addr");
//char *a_value = dap_chain_node_info_serialize(node_info, NULL);
size_t node_info_size = dap_chain_node_info_get_size(node_info);
bool res = dap_chain_global_db_gr_set(a_key, (const uint8_t *) node_info, node_info_size, GROUP_GLOBAL_ADDRS_LEASED);
//DAP_DELETE(a_value);
/**
* Handler of command 'global_db node add'
*
* str_reply[out] for reply
* return 0 Ok, -1 error
*/
static int com_global_db_add(dap_chain_node_info_t *a_node_info, const char *alias_str,
const char *cell_str, const char *ipv4_str, const char *ipv6_str, char **str_reply)
if(!a_node_info->hdr.address.uint64) {
dap_chain_node_cli_set_reply_text(str_reply, "not found -addr parameter");
if(!cell_str) {
dap_chain_node_cli_set_reply_text(str_reply, "not found -cell parameter");
return -1;
}
if(!ipv4_str && !ipv6_str) {
dap_chain_node_cli_set_reply_text(str_reply, "not found -ipv4 or -ipv6 parameter");
return -1;
}
else {
if(ipv4_str)
inet_pton(AF_INET, ipv4_str, &(a_node_info->hdr.ext_addr_v4));
inet_pton(AF_INET6, ipv6_str, &(a_node_info->hdr.ext_addr_v6));
// check match addr to cell or no
/*dap_chain_node_addr_t *addr = dap_chain_node_gen_addr(&node_info->hdr.cell_id);
if(!dap_chain_node_check_addr(&node_info->hdr.address, &node_info->hdr.cell_id)) {
set_reply_text(str_reply, "cell does not match addr");
if(!dap_chain_node_alias_register(alias_str, &a_node_info->hdr.address)) {
log_it(L_WARNING, "can't save alias %s", alias_str);
dap_chain_node_cli_set_reply_text(str_reply, "alias '%s' can't be mapped to addr=0x%lld",
alias_str, a_node_info->hdr.address.uint64);
bool res = dap_chain_node_info_save_and_reply(a_node_info, str_reply);
dap_chain_node_cli_set_reply_text(str_reply, "node added");
if(res)
return 0;
return -1;
}
/**
* Handler of command 'global_db node add'
*
* str_reply[out] for reply
* return 0 Ok, -1 error
*/
static int com_global_db_del(dap_chain_node_info_t *node_info, const char *alias_str, char **str_reply)
if(!node_info->hdr.address.uint64 && !alias_str) {
dap_chain_node_cli_set_reply_text(str_reply, "addr not found");
// check, current node have this addr or no
uint64_t l_cur_addr = dap_db_get_cur_node_addr();
if(l_cur_addr && l_cur_addr == node_info->hdr.address.uint64) {
dap_chain_node_cli_set_reply_text(str_reply, "current node cannot be deleted");
// find addr by alias or addr_str
dap_chain_node_addr_t *address = com_global_db_get_addr(node_info, &node_info->hdr.address, alias_str);
if(!address) {
dap_chain_node_cli_set_reply_text(str_reply, "alias not found");
char *a_key = dap_chain_node_addr_to_hash_str(address);
bool res = dap_chain_global_db_gr_del(a_key, GROUP_GLOBAL_ADDRS_LEASED);
if(res) {
// delete all aliases for node address
{
dap_list_t *list_aliases = get_aliases_by_name(address);
dap_list_t *list = list_aliases;
while(list)
{
const char *alias = (const char *) list->data;
dap_list_free_full(list_aliases, (dap_callback_destroyed_t) free);
dap_chain_node_cli_set_reply_text(str_reply, "node deleted");
dap_chain_node_cli_set_reply_text(str_reply, "node not deleted");
dap_chain_node_cli_set_reply_text(str_reply, "addr to delete can't be defined");
DAP_DELETE(address);
return -1;
}
/**
* Handler of command 'global_db node link'
*
* str_reply[out] for reply
* return 0 Ok, -1 error
*/
static int com_global_db_link(dap_chain_node_info_t *node_info, const char *cmd, const char *alias_str,
dap_chain_node_addr_t *link, char **str_reply)
if(!node_info->hdr.address.uint64 && !alias_str) {
dap_chain_node_cli_set_reply_text(str_reply, "addr not found");
return -1;
}
if(!link->uint64) {
dap_chain_node_cli_set_reply_text(str_reply, "link not found");
return -1;
}
// TODO check the presence of link in the node base
if(0) {
dap_chain_node_cli_set_reply_text(str_reply, "node 0x%016llx not found in base", link->uint64);
// find addr by alias or addr_str
dap_chain_node_addr_t *address = com_global_db_get_addr(node_info, &node_info->hdr.address, alias_str);
if(!address) {
dap_chain_node_cli_set_reply_text(str_reply, "alias not found");
dap_chain_node_info_t * l_node_info_read = dap_chain_node_info_read_and_reply(address, str_reply);
size_t l_node_info_read_size = dap_chain_node_info_get_size( l_node_info_read);
if(! l_node_info_read)
return -1;
int cmd_int = 0;
if(!strcmp(cmd, "add"))
cmd_int = 1;
else if(!strcmp(cmd, "del"))
cmd_int = 2;
// find link in node_info_read
int index_link = -1;
for(size_t i = 0; i < l_node_info_read->hdr.links_number; i++) {
if( l_node_info_read->links[i].uint64 == link->uint64) {
bool res_successful = false; // is successful whether add/del
// add link
if(cmd_int == 1) {
if(index_link == -1) {
l_node_info_read = DAP_REALLOC ( l_node_info_read,l_node_info_read_size += sizeof (*link) );
memcpy(&( l_node_info_read->links[ l_node_info_read->hdr.links_number]), link, sizeof(dap_chain_node_addr_t));
l_node_info_read->hdr.links_number++;
res_successful = true;
}
}
// delete link
else if(cmd_int == 2) {
// move link list to one item prev
if(index_link >= 0) {
for(unsigned int j = (unsigned int) index_link; j < ( l_node_info_read->hdr.links_number - 1); j++) {
memcpy(&( l_node_info_read->links[j]), &( l_node_info_read->links[j + 1]), sizeof(dap_chain_node_addr_t));
l_node_info_read->hdr.links_number--;
l_node_info_read = DAP_REALLOC ( l_node_info_read,l_node_info_read_size -= sizeof (*link) );
}
}
// save edited node_info
if(res_successful) {
bool res = dap_chain_node_info_save_and_reply( l_node_info_read, str_reply);
if(res) {
res_successful = true;
if(cmd_int == 1)
dap_chain_node_cli_set_reply_text(str_reply, "link added");
dap_chain_node_cli_set_reply_text(str_reply, "link deleted");
else {
res_successful = false;
}
else {
if(cmd_int == 1) {
if(index_link >= 0)
dap_chain_node_cli_set_reply_text(str_reply, "link not added because it is already present");
dap_chain_node_cli_set_reply_text(str_reply, "link not added");
}
if(cmd_int == 2) {
if(index_link == -1)
dap_chain_node_cli_set_reply_text(str_reply, "link not deleted because not found");
dap_chain_node_cli_set_reply_text(str_reply, "link not deleted");
return -1;
}
/**
* Handler of command 'global_db node dump'
*
* str_reply[out] for reply
* return 0 Ok, -1 error
*/
static int com_global_db_dump(dap_chain_node_info_t *a_node_info, const char *alias_str, char **str_reply)
size_t l_nodes_count = 1;
//bool show_all_addr = false;
dap_global_db_obj_t **l_objs = NULL;
if(!a_node_info->hdr.address.uint64 && !alias_str) {
//set_reply_text(str_reply, "addr not found");
//return -1;
//show_all_addr = true;
// read all nodes
dap_chain_node_info_t *node_info;
// read all node
l_objs = dap_chain_global_db_gr_load(GROUP_GLOBAL_ADDRS_LEASED, &l_nodes_count);
/*for(size_t i = 0; i < l_nodes_count; i++) {
dap_global_db_obj_t *l_obj = l_objs[i];
dap_chain_node_info_t *node_info = (dap_chain_node_info_t *) l_obj->value;
node_info->
}*/
if(!l_nodes_count || !l_objs) {
dap_chain_node_cli_set_reply_text(str_reply, "nodes not found");
return -1;
}
}
size_t i;
dap_string_t *l_string_reply = dap_string_new(NULL);
for(i = 0; i < l_nodes_count; i++) {
dap_chain_node_info_t *node_info = (!l_objs) ? a_node_info : (dap_chain_node_info_t *) l_objs[i]->value;
// find addr by alias or addr_str
dap_chain_node_addr_t *address = com_global_db_get_addr(node_info, &node_info->hdr.address, alias_str);
if(!address) {
dap_chain_node_cli_set_reply_text(str_reply, "alias not found");
break;
}
// read node
dap_chain_node_info_t *node_info_read = dap_chain_node_info_read_and_reply(address, str_reply);
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
if(!node_info_read) {
DAP_DELETE(address);
break;
}
int hostlen = 128;
char host4[hostlen];
char host6[hostlen];
struct sockaddr_in sa4 = { .sin_family = AF_INET, .sin_addr = node_info_read->hdr.ext_addr_v4 };
const char* str_ip4 = inet_ntop(AF_INET, &(((struct sockaddr_in *) &sa4)->sin_addr), host4, hostlen);
struct sockaddr_in6 sa6 = { .sin6_family = AF_INET6, .sin6_addr = node_info_read->hdr.ext_addr_v6 };
const char* str_ip6 = inet_ntop(AF_INET6, &(((struct sockaddr_in6 *) &sa6)->sin6_addr), host6, hostlen);
// get aliases in form of string
dap_string_t *aliases_string = dap_string_new(NULL);
dap_list_t *list_aliases = get_aliases_by_name(address);
if(list_aliases)
{
dap_list_t *list = list_aliases;
while(list)
{
const char *alias = (const char *) list->data;
dap_string_append_printf(aliases_string, "\nalias %s", alias);
list = dap_list_next(list);
}
dap_list_free_full(list_aliases, (dap_callback_destroyed_t) free);
}
else
dap_string_append(aliases_string, "\nno aliases");
// get links in form of string
dap_string_t *links_string = dap_string_new(NULL);
for(unsigned int i = 0; i < node_info_read->hdr.links_number; i++) {
dap_chain_node_addr_t link_addr = node_info_read->links[i];
dap_string_append_printf(links_string, "\nlink%02d address : 0x%016llx", i, link_addr.uint64);
}
if(i)
dap_string_append_printf(l_string_reply, "\n");
// set short reply with node param
if(l_objs)
dap_string_append_printf(l_string_reply,
"node address 0x%016llx\tcell 0x%016llx\tipv4 %s\tnumber of links %u",
node_info_read->hdr.address.uint64, node_info_read->hdr.cell_id.uint64,
str_ip4, node_info_read->hdr.links_number);
else
// set full reply with node param
dap_string_append_printf(l_string_reply,
"node address 0x%016llx\ncell 0x%016llx%s\nipv4 %s\nipv6 %s\nlinks %u%s",
node_info_read->hdr.address.uint64, node_info_read->hdr.cell_id.uint64, aliases_string->str,
str_ip4, str_ip6,
node_info_read->hdr.links_number, links_string->str);
dap_string_free(aliases_string, true);
dap_string_free(links_string, true);
DAP_DELETE(address);
DAP_DELETE(node_info_read);
}
if(i == l_nodes_count) {
// set full reply with node param
dap_chain_node_cli_set_reply_text(str_reply, l_string_reply->str);
}
dap_string_free(l_string_reply, true);
if(i < l_nodes_count)
return -1;
else
return 0;
}
/**
* Handler of command 'global_db node cur_node_get'
*
* str_reply[out] for reply
* return 0 Ok, -1 error
*/
static int com_global_db_cur_node_get(char **a_str_reply)
{
// get cur node addr
uint64_t l_addr = dap_db_get_cur_node_addr();
if(l_addr) {
dap_chain_node_cli_set_reply_text(a_str_reply, "address for current node is 0x%llu", l_addr);
dap_chain_node_cli_set_reply_text(a_str_reply, "address for node has not been set.");
return -1;
}
/**
* Handler of command 'global_db node cur_node_set'
*
* str_reply[out] for reply
* return 0 Ok, -1 error
*/
static int com_global_db_cur_node_set(dap_chain_node_info_t *a_node_info, const char *a_alias_str, char **a_str_reply)
{
if(!a_node_info->hdr.address.uint64 && !a_alias_str) {
dap_chain_node_cli_set_reply_text(a_str_reply, "addr not found");
dap_chain_node_addr_t *l_address = com_global_db_get_addr(a_node_info, &a_node_info->hdr.address, a_alias_str);
if(!l_address) {
dap_chain_node_cli_set_reply_text(a_str_reply, "alias not found");
dap_chain_node_info_t *l_node_info_read = dap_chain_node_info_read_and_reply(l_address, a_str_reply);
if(!l_node_info_read) {
DAP_DELETE(l_address);
// set cur node addr
if(dap_db_set_cur_node_addr(l_node_info_read->hdr.address.uint64)) {
dap_chain_node_cli_set_reply_text(a_str_reply, "new address for current node has been set");
dap_chain_node_cli_set_reply_text(a_str_reply, "new address for current node has not been set");
/**
* Handler of command 'global_db node remote_set'
*
* str_reply[out] for reply
* return 0 Ok, -1 error
*/
static int com_node_request_addr(dap_chain_node_info_t *a_node_info, const char *a_alias_str, char **a_str_reply)
{
if(!a_node_info->hdr.address.uint64 && !a_alias_str) {
dap_chain_node_cli_set_reply_text(a_str_reply, "addr not found");
return -1;
}
// find addr by alias or addr_str
dap_chain_node_addr_t *l_address = com_global_db_get_addr(a_node_info, &a_node_info->hdr.address, a_alias_str);
if(!l_address) {
dap_chain_node_cli_set_reply_text(a_str_reply, "alias not found");
return -1;
}
// read node
dap_chain_node_info_t *l_node_info_read = dap_chain_node_info_read_and_reply(l_address, a_str_reply);
if(!l_node_info_read) {
DAP_DELETE(l_address);
dap_chain_node_info_t *l_node_info = dap_chain_node_info_read_and_reply(l_address, a_str_reply);
return -1;
}
// start connect
dap_chain_node_client_t *client = dap_chain_node_client_connect(l_node_info);
if(!client) {
dap_chain_node_cli_set_reply_text(a_str_reply, "can't connect");
DAP_DELETE(l_node_info);
return -1;
}
// wait connected
int timeout_ms = 15000; //15 sec = 15000 ms
int res = dap_chain_node_client_wait(client, NODE_CLIENT_STATE_CONNECTED, timeout_ms);
dap_chain_node_cli_set_reply_text(a_str_reply, "no response from node");
// clean client struct
dap_chain_node_client_close(client);
DAP_DELETE(l_node_info);
return -1;
res = dap_chain_node_client_send_ch_pkt(client, dap_stream_ch_chain_net_get_id(),
DAP_STREAM_CH_CHAIN_NET_PKT_TYPE_NODE_ADDR_LEASE_REQUEST,NULL,0);
dap_chain_node_cli_set_reply_text(a_str_reply, "no request sent");
// clean client struct
dap_chain_node_client_close(client);
DAP_DELETE(l_node_info);
return -1;
}
// wait for finishing of request
timeout_ms = 120000; // 2 min = 120 sec = 120 000 ms
res = dap_chain_node_client_wait(client, NODE_CLIENT_STATE_GET_NODE_ADDR, timeout_ms);
DAP_DELETE(l_node_info);
dap_client_disconnect(client->client);
dap_chain_node_client_close(client);
switch (res) {
case 0:
dap_chain_node_cli_set_reply_text(a_str_reply, "timeout");
return -1;
case 1: {
uint64_t addr = dap_db_get_cur_node_addr();
dap_chain_node_cli_set_reply_text(a_str_reply, "new address for remote node has been set 0x%x", addr);
}
return 0;
default:
dap_chain_node_cli_set_reply_text(a_str_reply, "error");
/**
* global_db command
*
* return 0 OK, -1 Err
*/
int com_global_db(int a_argc, const char ** a_argv, char **a_str_reply)
CMD_NONE, CMD_ADD, CMD_DEL, CMD_LINK, CMD_DUMP, CMD_CUR_NODE_GET, CMD_CUR_NODE_SET, CMD_CUR_NODE_SET_FROM_REMOTE
//printf("com_global_db\n");
arg_index = dap_chain_node_cli_find_option_val(a_argv, arg_index, min(a_argc, arg_index + 1), "node", NULL);
if(!arg_index || a_argc < 3) {
dap_chain_node_cli_set_reply_text(a_str_reply, "parameters are not valid");
// find command (add, delete, etc) as second parameter only
if((arg_index_n = dap_chain_node_cli_find_option_val(a_argv, arg_index, min(a_argc, arg_index + 1), "add", NULL))
else if((arg_index_n = dap_chain_node_cli_find_option_val(a_argv, arg_index, min(a_argc, arg_index + 1), "del", NULL))
else if((arg_index_n = dap_chain_node_cli_find_option_val(a_argv, arg_index, min(a_argc, arg_index + 1), "link", NULL))
else if((arg_index_n = dap_chain_node_cli_find_option_val(a_argv, arg_index, min(a_argc, arg_index + 1), "dump", NULL))
else if((arg_index_n = dap_chain_node_cli_find_option_val(a_argv, arg_index, min(a_argc, arg_index + 1), "cur_node_get", NULL)) != 0) {
cmd_num = CMD_CUR_NODE_GET;
else if((arg_index_n = dap_chain_node_cli_find_option_val(a_argv, arg_index, min(a_argc, arg_index + 1), "cur_node_set", NULL)) != 0) {
cmd_num = CMD_CUR_NODE_SET;
else if((arg_index_n = dap_chain_node_cli_find_option_val(a_argv, arg_index, min(a_argc, arg_index + 1), "cur_node_set_from_remote",
cmd_num = CMD_CUR_NODE_SET_FROM_REMOTE;
dap_chain_node_cli_set_reply_text(a_str_reply, "command %s not recognized", a_argv[1]);
//arg_index = arg_index_n; // no need, they are already equal must be
assert(arg_index == arg_index_n);
arg_index++;
const char *l_addr_str = NULL, *alias_str = NULL, *l_cell_str = NULL, *l_link_str = NULL;
const char *ipv4_str = NULL, *ipv6_str = NULL;
dap_chain_node_cli_find_option_val(a_argv, arg_index, a_argc, "-addr", &l_addr_str);
dap_chain_node_cli_find_option_val(a_argv, arg_index, a_argc, "-alias", &alias_str);
dap_chain_node_cli_find_option_val(a_argv, arg_index, a_argc, "-cell", &l_cell_str);
dap_chain_node_cli_find_option_val(a_argv, arg_index, a_argc, "-ipv4", &ipv4_str);
dap_chain_node_cli_find_option_val(a_argv, arg_index, a_argc, "-ipv6", &ipv6_str);
dap_chain_node_cli_find_option_val(a_argv, arg_index, a_argc, "-link", &l_link_str);
dap_chain_node_addr_t l_link={0};
dap_chain_node_info_t *l_node_info;
size_t l_node_info_size = sizeof (l_node_info->hdr)+sizeof(l_link);
l_node_info = DAP_NEW_Z_SIZE(dap_chain_node_info_t,l_node_info_size);
if(l_addr_str) {
dap_digit_from_string(l_addr_str, l_node_info->hdr.address.raw, sizeof(l_node_info->hdr.address.raw) );
if(l_cell_str) {
dap_digit_from_string(l_cell_str, l_node_info->hdr.cell_id.raw, sizeof(l_node_info->hdr.cell_id.raw)); //DAP_CHAIN_CELL_ID_SIZE);
if(l_link_str) {
dap_digit_from_string(l_link_str, l_link.raw, sizeof(l_link.raw));
if(!arg_index || a_argc < 8) {
dap_chain_node_cli_set_reply_text(a_str_reply, "invalid parameters");
// handler of command 'global_db node add'
return com_global_db_add(l_node_info, alias_str, l_cell_str, ipv4_str, ipv6_str, a_str_reply);
return com_global_db_del(l_node_info, alias_str, a_str_reply);
if(dap_chain_node_cli_find_option_val(a_argv, arg_index, min(a_argc, arg_index + 1), "add", NULL))
// handler of command 'global_db node link add -addr <node address> -link <node address>'
return com_global_db_link(l_node_info, "add", alias_str, &l_link, a_str_reply);
else if(dap_chain_node_cli_find_option_val(a_argv, arg_index, min(a_argc, arg_index + 1), "del", NULL))
// handler of command 'global_db node link del -addr <node address> -link <node address>'
return com_global_db_link(l_node_info, "del", alias_str, &l_link, a_str_reply);
dap_chain_node_cli_set_reply_text(a_str_reply, "command not recognize, supported format:\n"
"global_db node link <add|del] [-addr <node address> | -alias <node alias>] -link <node address>");
case CMD_DUMP:
// handler of command 'global_db node dump'
return com_global_db_dump(l_node_info, alias_str, a_str_reply);
case CMD_CUR_NODE_GET:
// handler of command 'global_db cur_node get'
return com_global_db_cur_node_get(a_str_reply);
case CMD_CUR_NODE_SET:
// handler of command 'global_db cur_node set'
return com_global_db_cur_node_set(l_node_info, alias_str, a_str_reply);
case CMD_CUR_NODE_SET_FROM_REMOTE:
// handler of command 'global_db node remote_set'
return com_node_request_addr(l_node_info, alias_str, a_str_reply);
dap_chain_node_cli_set_reply_text(a_str_reply, "command %s not recognized", a_argv[1]);
}
return -1;
}
/**
* Node command
*/
int com_node(int argc, const char ** argv, char **str_reply)
{
CMD_NONE, CMD_ALIAS, CMD_HANDSHAKE, CMD_CONNECT
};
int arg_index = 1;
int cmd_num = CMD_NONE;
const char *cmd_str = NULL;
// find add parameter ('alias' or 'handshake')
if(dap_chain_node_cli_find_option_val(argv, arg_index, min(argc, arg_index + 1), "handshake", NULL)) {
else if(dap_chain_node_cli_find_option_val(argv, arg_index, min(argc, arg_index + 1), "connect", NULL)) {
else if(dap_chain_node_cli_find_option_val(argv, arg_index, min(argc, arg_index + 1), "alias", NULL)) {
cmd_num = CMD_ALIAS;
}
arg_index++;
if(cmd_num == CMD_NONE) {
dap_chain_node_cli_set_reply_text(str_reply, "command %s not recognized", argv[1]);
return -1;
}
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;
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;
}
{
// 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");
}
}
dap_chain_node_cli_set_reply_text(str_reply, "alias can't be mapped because -addr is not found");
return -1;
dap_chain_node_cli_set_reply_text(str_reply, "alias can't be mapped because -alias is not found");
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;
}
if(!address.uint64) {
dap_chain_node_cli_set_reply_text(str_reply, "addr not found");
// get cur node addr
dap_chain_node_addr_t l_cur_node_addr;
l_cur_node_addr.uint64 = dap_db_get_cur_node_addr(); //0x12345
if(!l_cur_node_addr.uint64) {
dap_chain_node_cli_set_reply_text(str_reply, "Current node has no address");
return -1;
}
dap_chain_node_info_t *l_remote_node_info = dap_chain_node_info_read_and_reply(&address, str_reply);
if(!l_remote_node_info) {
return -1;
}
// start connect
dap_chain_node_client_t *l_node_client = dap_chain_node_client_connect(l_remote_node_info);
if(!l_node_client) {
dap_chain_node_cli_set_reply_text(str_reply, "can't connect");
return -1;
}
// wait connected
int timeout_ms = 15000; //15 sec = 15000 ms
int res = dap_chain_node_client_wait(l_node_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(l_node_client);
DAP_DELETE(l_remote_node_info);
return -1;
}
log_it(L_NOTICE, "Stream connection established, now lets sync all");
dap_stream_ch_chain_sync_request_t l_sync_request = {{0}};
dap_stream_ch_t * l_ch_chain = dap_client_get_stream_ch(l_node_client->client, dap_stream_ch_chain_get_id() );
l_sync_request.ts_start = (uint64_t) dap_db_log_get_last_timestamp_remote( l_remote_node_info->hdr.address.uint64 );
//l_sync_request.ts_end = (time_t) time(NULL);
l_sync_request.node_addr.uint64 = dap_chain_net_get_cur_addr(l_net)?dap_chain_net_get_cur_addr(l_net)->uint64:
dap_db_get_cur_node_addr();
dap_chain_id_t l_chain_id_null = {{0}};
dap_chain_cell_id_t l_chain_cell_id_null = {{0}};
log_it(L_INFO,"Requested GLOBAL_DB syncronizatoin, %llu:%llu period", l_sync_request.ts_start,
l_sync_request.ts_end ) ;
if( 0 == dap_stream_ch_chain_pkt_write(l_ch_chain,DAP_STREAM_CH_CHAIN_PKT_TYPE_SYNC_GLOBAL_DB,
l_net->pub.id, l_chain_id_null ,l_chain_cell_id_null ,&l_sync_request,
sizeof (l_sync_request))) {
dap_chain_node_cli_set_reply_text(str_reply, "Error: Cant send sync chains request");
// clean client struct
dap_chain_node_client_close(l_node_client);
DAP_DELETE(l_remote_node_info);
return -1;
}
dap_stream_ch_set_ready_to_write(l_ch_chain,true);
// wait for finishing of request
timeout_ms = 120000; // 2 min = 120 sec = 120 000 ms
// TODO add progress info to console
res = dap_chain_node_client_wait(l_node_client, NODE_CLIENT_STATE_SYNCED, timeout_ms);
// Requesting chains
dap_chain_t *l_chain = NULL;
DL_FOREACH(l_net->pub.chains, l_chain) {
// send request
dap_stream_ch_chain_sync_request_t l_sync_request = {{0}};
if( 0 == dap_stream_ch_chain_pkt_write(l_ch_chain,DAP_STREAM_CH_CHAIN_PKT_TYPE_SYNC_CHAINS,
l_net->pub.id, l_chain->id ,l_remote_node_info->hdr.cell_id,&l_sync_request,
sizeof (l_sync_request))) {
dap_chain_node_cli_set_reply_text(str_reply, "Error: Cant send sync chains request");
// clean client struct
dap_chain_node_client_close(l_node_client);
DAP_DELETE(l_remote_node_info);
return -1;
}
log_it(L_NOTICE, "Requested syncronization for chain \"%s\"",l_chain->name);
dap_stream_ch_set_ready_to_write(l_ch_chain,true);
// wait for finishing of request
timeout_ms = 120000; // 2 min = 120 sec = 120 000 ms
// TODO add progress info to console
res = dap_chain_node_client_wait(l_node_client, NODE_CLIENT_STATE_SYNCED, timeout_ms);
log_it(L_INFO,"Chains and gdb are synced");
DAP_DELETE(l_remote_node_info);
dap_client_disconnect(l_node_client->client);
dap_chain_node_client_close(l_node_client);
dap_chain_node_cli_set_reply_text(str_reply, "Node sync completed: Chains and gdb are synced");
// 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);
}