diff --git a/dap-sdk b/dap-sdk index f2b762fcfbf3d799029d9a52963b09fefd9e711b..ea267c0f9432a1fb94d70a4d400c265c63405ef8 160000 --- a/dap-sdk +++ b/dap-sdk @@ -1 +1 @@ -Subproject commit f2b762fcfbf3d799029d9a52963b09fefd9e711b +Subproject commit ea267c0f9432a1fb94d70a4d400c265c63405ef8 diff --git a/modules/chain/dap_chain_cell.c b/modules/chain/dap_chain_cell.c index 766175ec0d082ba73c9f25a72f415ac5b7262329..8412b79e584db73e41f71bb03eb3a902ce7c8cec 100644 --- a/modules/chain/dap_chain_cell.c +++ b/modules/chain/dap_chain_cell.c @@ -107,6 +107,17 @@ int dap_chain_cell_init(void) return 0; } +#ifndef DAP_OS_WINDOWS +DAP_STATIC_INLINE void s_cell_reclaim_cur_volume(dap_chain_cell_t *a_cell) { + if ( +#ifdef MADV_PAGEOUT + //madvise(a_cell->map, (size_t)(a_cell->map_end - a_cell->map), MADV_PAGEOUT) && +#endif + madvise(a_cell->map, (size_t)(a_cell->map_end - a_cell->map), MADV_DONTNEED) ) + log_it(L_ERROR, "Unable to reclaim the previous volume, errno %d: \"%s\"", errno, dap_strerror(errno)); +} +#endif + DAP_STATIC_INLINE int s_cell_file_write_header(dap_chain_cell_t *a_cell) { dap_chain_cell_file_header_t l_hdr = { @@ -120,7 +131,7 @@ DAP_STATIC_INLINE int s_cell_file_write_header(dap_chain_cell_t *a_cell) return fwrite(&l_hdr, sizeof(l_hdr), 1, a_cell->file_storage) ? fflush(a_cell->file_storage) : -1; } -DAP_STATIC_INLINE int s_cell_map_new_volume(dap_chain_cell_t *a_cell, size_t a_fpos) { +DAP_STATIC_INLINE int s_cell_map_new_volume(dap_chain_cell_t *a_cell, size_t a_fpos, bool a_load) { int l_fildes = fileno(a_cell->file_storage); #ifdef DAP_OS_WINDOWS HANDLE hSection = NULL; @@ -135,12 +146,9 @@ DAP_STATIC_INLINE int s_cell_map_new_volume(dap_chain_cell_t *a_cell, size_t a_f }; NTSTATUS err = pfnNtCreateSection(&hSection, SECTION_MAP_READ|SECTION_EXTEND_SIZE|SECTION_MAP_WRITE, - NULL, &SectionSize, PAGE_READWRITE, SEC_RESERVE, - (HANDLE)_get_osfhandle(l_fildes)); - if ( !NT_SUCCESS(err) ) { - log_it(L_ERROR, "NtCreateSection() failed, status %lx", err); - return -1; - } + NULL, &SectionSize, PAGE_READWRITE, SEC_RESERVE, (HANDLE)_get_osfhandle(l_fildes)); + if ( !NT_SUCCESS(err) ) + return log_it(L_ERROR, "NtCreateSection() failed, status %lx", err), -1; a_cell->map_range_bounds = dap_list_append(a_cell->map_range_bounds, hSection); } #endif @@ -158,23 +166,17 @@ DAP_STATIC_INLINE int s_cell_map_new_volume(dap_chain_cell_t *a_cell, size_t a_f LARGE_INTEGER Offset = { .QuadPart = l_volume_start }; - if ( !NT_SUCCESS ( err = pfnNtMapViewOfSection(hSection, GetCurrentProcess(), - (HANDLE)&a_cell->map, 0, 0, - &Offset, &l_map_size, - ViewUnmap, MEM_RESERVE, - PAGE_WRITECOPY) ) ) - { - log_it(L_ERROR, "NtMapViewOfSection() failed, status %lx", err); - NtClose(hSection); - return -1; - } + err = pfnNtMapViewOfSection(hSection, GetCurrentProcess(), (HANDLE)&a_cell->map, 0, 0, + &Offset, &l_map_size, ViewUnmap, MEM_RESERVE, PAGE_WRITECOPY); + if ( !NT_SUCCESS(err) ) + return NtClose(hSection), log_it(L_ERROR, "NtMapViewOfSection() failed, status %lx", err), -1; #else - if ( MAP_FAILED == (a_cell->map = mmap(NULL, l_map_size, PROT_READ|PROT_WRITE, - MAP_PRIVATE, fileno(a_cell->file_storage), l_volume_start)) ) { - log_it(L_ERROR, "Chain cell \"%s\" 0x%016"DAP_UINT64_FORMAT_X" cannot be mapped, errno %d", - a_cell->file_storage_path, a_cell->id.uint64, errno); - return -1; - } + if (a_load) + s_cell_reclaim_cur_volume(a_cell); + if (( a_cell->map = mmap(NULL, l_map_size, PROT_READ|PROT_WRITE, MAP_PRIVATE, + fileno(a_cell->file_storage), l_volume_start) ) == MAP_FAILED ) + return log_it(L_ERROR, "Chain cell \"%s\" 0x%016"DAP_UINT64_FORMAT_X" cannot be mapped, errno %d", + a_cell->file_storage_path, a_cell->id.uint64, errno), -1; #ifdef DAP_OS_DARWIN a_cell->cur_vol_start = l_volume_start; #endif @@ -182,6 +184,10 @@ DAP_STATIC_INLINE int s_cell_map_new_volume(dap_chain_cell_t *a_cell, size_t a_f a_cell->map_pos = a_cell->map + l_offset; a_cell->map_range_bounds = dap_list_append(a_cell->map_range_bounds, a_cell->map); a_cell->map_range_bounds = dap_list_append(a_cell->map_range_bounds, a_cell->map_end = a_cell->map + l_map_size); +#ifndef DAP_OS_WINDOWS + if (a_load) + madvise(a_cell->map, l_map_size, MADV_SEQUENTIAL); +#endif return 0; } @@ -259,7 +265,7 @@ dap_chain_cell_t * dap_chain_cell_create_fill(dap_chain_t * a_chain, dap_chain_c fflush(l_file); } - if ( a_chain->is_mapped && s_cell_map_new_volume(l_cell, 0) ) { + if ( a_chain->is_mapped && s_cell_map_new_volume(l_cell, 0, true) ) { CLEANUP_AND_RET; } #undef CLEANUP_AND_RET @@ -454,7 +460,7 @@ int dap_chain_cell_load(dap_chain_t *a_chain, dap_chain_cell_t *a_cell) for ( l_el_size = 0; l_pos < l_full_size; ++q, l_pos += l_el_size + sizeof(uint64_t) ) { l_vol_rest = (uint64_t)( a_cell->map_end - a_cell->map_pos ); if ( l_vol_rest <= sizeof(uint64_t) || l_vol_rest - sizeof(uint64_t) <= *(uint64_t*)a_cell->map_pos) - if ( s_cell_map_new_volume(a_cell, l_pos) ) + if ( s_cell_map_new_volume(a_cell, l_pos, true) ) break; if (*(uint64_t*)a_cell->map_pos > l_full_size - l_pos ) { log_it(L_ERROR, "Atom size exeeds file remainder: %zu > %zu. " @@ -469,6 +475,9 @@ int dap_chain_cell_load(dap_chain_t *a_chain, dap_chain_cell_t *a_cell) a_cell->map_pos += l_el_size; a_chain->load_progress = (int)((float)l_pos/l_full_size * 100 + 0.5); } +#ifndef DAP_OS_WINDOWS + s_cell_reclaim_cur_volume(a_cell); +#endif } else { DAP_DELETE(l_hdr); size_t l_read = 0; @@ -485,7 +494,7 @@ int dap_chain_cell_load(dap_chain_t *a_chain, dap_chain_cell_t *a_cell) break; } l_pos += sizeof(uint64_t) + ( l_read = fread((void*)l_element, 1, l_el_size, a_cell->file_storage) ); - a_chain->load_progress = (int)((double)l_pos/l_full_size * 100 + 0.5); + a_chain->load_progress = (int)((float)l_pos/l_full_size * 100 + 0.5); if (l_read != l_el_size) { log_it(L_ERROR, "Read only %lu of %zu bytes, stop cell loading", l_read, l_el_size); DAP_DELETE(l_element); @@ -519,7 +528,7 @@ static int s_cell_file_atom_add(dap_chain_cell_t *a_cell, dap_chain_atom_ptr_t a debug_if (s_debug_more, L_DEBUG, "Before filling volume for atom size %ld, stream pos of %s is %lu, map pos is %lu, space left in map %lu", a_atom_size, a_cell->file_storage_path, l_pos, (size_t)(a_cell->map_pos - a_cell->map), (size_t)(a_cell->map_end - a_cell->map_pos)); if ( a_atom_size + sizeof(uint64_t) > (size_t)(a_cell->map_end - a_cell->map_pos) ) - if ( s_cell_map_new_volume(a_cell, (size_t)l_pos) ) + if ( s_cell_map_new_volume(a_cell, (size_t)l_pos, false) ) return -2; } diff --git a/modules/chain/dap_chain_ch.c b/modules/chain/dap_chain_ch.c index 98eef65ffe41e75028818a9269659657618a5404..2771e6b3d14dbbdd6468b099400886b5b68d0370 100644 --- a/modules/chain/dap_chain_ch.c +++ b/modules/chain/dap_chain_ch.c @@ -154,39 +154,21 @@ static dap_memstat_rec_t s_memstat [MEMSTAT$K_NR] = { #endif -static const char *s_error_type_to_string(dap_chain_ch_error_type_t a_error) -{ - switch (a_error) { - case DAP_CHAIN_CH_ERROR_SYNC_REQUEST_ALREADY_IN_PROCESS: - return "SYNC_REQUEST_ALREADY_IN_PROCESS"; - case DAP_CHAIN_CH_ERROR_INCORRECT_SYNC_SEQUENCE: - return "INCORRECT_SYNC_SEQUENCE"; - case DAP_CHAIN_CH_ERROR_SYNC_TIMEOUT: - return "SYNCHRONIZATION TIMEOUT"; - case DAP_CHAIN_CH_ERROR_CHAIN_PKT_DATA_SIZE: - return "INVALID_PACKET_SIZE"; - case DAP_CHAIN_CH_ERROR_LEGACY_PKT_DATA_SIZE: - return "INVALID_LEGACY_PACKET_SIZE"; - case DAP_CHAIN_CH_ERROR_NET_INVALID_ID: - return "INVALID_NET_ID"; - case DAP_CHAIN_CH_ERROR_CHAIN_NOT_FOUND: - return "CHAIN_NOT_FOUND"; - case DAP_CHAIN_CH_ERROR_ATOM_NOT_FOUND: - return "ATOM_NOT_FOUND"; - case DAP_CHAIN_CH_ERROR_UNKNOWN_CHAIN_PKT_TYPE: - return "UNKNOWN_CHAIN_PACKET_TYPE"; - case DAP_CHAIN_CH_ERROR_GLOBAL_DB_INTERNAL_NOT_SAVED: - return "GLOBAL_DB_INTERNAL_SAVING_ERROR"; - case DAP_CHAIN_CH_ERROR_NET_IS_OFFLINE: - return "NET_IS_OFFLINE"; - case DAP_CHAIN_CH_ERROR_OUT_OF_MEMORY: - return "OUT_OF_MEMORY"; - case DAP_CHAIN_CH_ERROR_INTERNAL: - return "INTERNAL_ERROR"; - default: - return "UNKNOWN_ERROR"; - } -} +const char* const s_error_type_to_string[] = { + [DAP_CHAIN_CH_ERROR_SYNC_REQUEST_ALREADY_IN_PROCESS]= "SYNC_REQUEST_ALREADY_IN_PROCESS", + [DAP_CHAIN_CH_ERROR_INCORRECT_SYNC_SEQUENCE] = "INCORRECT_SYNC_SEQUENCE", + [DAP_CHAIN_CH_ERROR_SYNC_TIMEOUT] = "SYNCHRONIZATION TIMEOUT", + [DAP_CHAIN_CH_ERROR_CHAIN_PKT_DATA_SIZE] = "INVALID_PACKET_SIZE", + [DAP_CHAIN_CH_ERROR_LEGACY_PKT_DATA_SIZE] = "INVALID_LEGACY_PACKET_SIZE", + [DAP_CHAIN_CH_ERROR_NET_INVALID_ID] = "INVALID_NET_ID", + [DAP_CHAIN_CH_ERROR_CHAIN_NOT_FOUND] = "CHAIN_NOT_FOUND", + [DAP_CHAIN_CH_ERROR_ATOM_NOT_FOUND] = "ATOM_NOT_FOUND", + [DAP_CHAIN_CH_ERROR_UNKNOWN_CHAIN_PKT_TYPE] = "UNKNOWN_CHAIN_PACKET_TYPE", + [DAP_CHAIN_CH_ERROR_GLOBAL_DB_INTERNAL_NOT_SAVED] = "GLOBAL_DB_INTERNAL_SAVING_ERROR", + [DAP_CHAIN_CH_ERROR_NET_IS_OFFLINE] = "NET_IS_OFFLINE", + [DAP_CHAIN_CH_ERROR_OUT_OF_MEMORY] = "OUT_OF_MEMORY", + [DAP_CHAIN_CH_ERROR_INTERNAL] = "INTERNAL_ERROR" +}; /** * @brief dap_chain_ch_init @@ -618,11 +600,9 @@ static bool s_sync_in_chains_callback(void *a_arg) DAP_DELETE(l_args); return false; } - char *l_atom_hash_str = NULL; dap_hash_fast_t l_atom_hash = { }; - dap_hash_fast(l_atom, l_atom_size, &l_atom_hash); - if (s_debug_more) - l_atom_hash_str = dap_hash_fast_to_str_static(&l_atom_hash); + dap_hash_fast(l_atom, l_atom_size, &l_atom_hash); + char *l_atom_hash_str = dap_hash_fast_to_str_static(&l_atom_hash); dap_chain_atom_verify_res_t l_atom_add_res = l_chain->callback_atom_add(l_chain, l_atom, l_atom_size, &l_atom_hash, false); bool l_ack_send = false; switch (l_atom_add_res) { @@ -688,7 +668,7 @@ void dap_stream_ch_write_error_unsafe(dap_stream_ch_t *a_ch, dap_chain_net_id_t { dap_chain_ch_t *l_ch_chain = DAP_CHAIN_CH(a_ch); dap_return_if_fail(l_ch_chain); - const char *l_err_str = s_error_type_to_string(a_error); + const char *l_err_str = a_error < DAP_CHAIN_CH_ERROR_LAST ? s_error_type_to_string[a_error] : "UNDEFINED ERROR"; dap_chain_ch_pkt_write_unsafe(a_ch, DAP_CHAIN_CH_PKT_TYPE_ERROR, a_net_id, a_chain_id, a_cell_id, l_err_str, strlen(l_err_str) + 1, DAP_CHAIN_CH_PKT_VERSION_LEGACY); s_ch_chain_go_idle(l_ch_chain); } @@ -1544,7 +1524,7 @@ static bool s_sync_timer_callback(void *a_arg) } bool l_timer_break = false; - const char *l_err_str = s_error_type_to_string(DAP_CHAIN_CH_ERROR_SYNC_TIMEOUT); + const char* l_err_str = s_error_type_to_string[DAP_CHAIN_CH_ERROR_SYNC_TIMEOUT]; if (l_ch_chain->sync_context) { struct sync_context *l_context = l_ch_chain->sync_context; if (l_context->last_activity + s_sync_timeout <= dap_time_now()) { diff --git a/modules/chain/dap_chain_cs.c b/modules/chain/dap_chain_cs.c index 211dfe40cd8255255e963f59e7512f568dde3ac1..d30576d826c3fa79d9bbaf82d98257b79354061d 100644 --- a/modules/chain/dap_chain_cs.c +++ b/modules/chain/dap_chain_cs.c @@ -134,10 +134,11 @@ int dap_chain_cs_create(dap_chain_t * a_chain, dap_config_t * a_chain_cfg) HASH_FIND_STR(s_cs_callbacks, l_consensus, l_item ); if (l_item) { log_it(L_NOTICE,"Consensus \"%s\" found, prepare to parse config file",l_item->name ); + int res = 0; if (l_item->callback_init) - l_item->callback_init(a_chain, a_chain_cfg); + res = l_item->callback_init(a_chain, a_chain_cfg); DAP_CHAIN_PVT(a_chain)->cs_name = l_item->name; - return 0; + return res; } else { log_it(L_ERROR,"Can't find consensus \"%s\"",dap_config_get_item_str( a_chain_cfg, "chain", "consensus")); return -1; diff --git a/modules/chain/include/dap_chain.h b/modules/chain/include/dap_chain.h index beb262ba03eb3fe930dbbf24599f28b5199d8327..e8e027832c506591033c6de38a4734f08515b731 100644 --- a/modules/chain/include/dap_chain.h +++ b/modules/chain/include/dap_chain.h @@ -62,7 +62,10 @@ typedef struct dap_chain_datum_iter { size_t cur_size; dap_chain_hash_fast_t *cur_hash; dap_chain_hash_fast_t *cur_atom_hash; + uint32_t action; + dap_chain_net_srv_uid_t uid; int ret_code; + char *token_ticker; void *cur_item; } dap_chain_datum_iter_t; diff --git a/modules/chain/include/dap_chain_ch.h b/modules/chain/include/dap_chain_ch.h index 384e5ec3d9154c784f783acb6caa0b6b06122237..da98e7e88fa5c016c40465c49b0c6884e657ba5a 100644 --- a/modules/chain/include/dap_chain_ch.h +++ b/modules/chain/include/dap_chain_ch.h @@ -63,7 +63,9 @@ typedef enum dap_chain_ch_error_type { DAP_CHAIN_CH_ERROR_INTERNAL, // Legacy DAP_CHAIN_CH_ERROR_GLOBAL_DB_INTERNAL_NOT_SAVED, - DAP_CHAIN_CH_ERROR_LEGACY_PKT_DATA_SIZE + DAP_CHAIN_CH_ERROR_LEGACY_PKT_DATA_SIZE, + // + DAP_CHAIN_CH_ERROR_LAST } dap_chain_ch_error_type_t; int dap_chain_ch_init(void); diff --git a/modules/chain/tests/dap_chain_ledger_tests.c b/modules/chain/tests/dap_chain_ledger_tests.c index 1e2a84476aadbf418da3b219a4d5418651019368..d842d8ecf5faceefd37049dd87224231cc16ad09 100644 --- a/modules/chain/tests/dap_chain_ledger_tests.c +++ b/modules/chain/tests/dap_chain_ledger_tests.c @@ -491,7 +491,7 @@ int dap_ledger_test_add_new_datum (uint16_t a_datum_type, void* a_datum, size_t { dap_chain_datum_t* l_new_datum = dap_chain_datum_create(a_datum_type, a_datum, a_datum_size); size_t l_new_datum_size = a_datum_size + sizeof(l_new_datum->header); - int status = dap_chain_datum_add(a_chain, l_new_datum, l_new_datum_size, a_datum_hash); + int status = dap_chain_datum_add(a_chain, l_new_datum, l_new_datum_size, a_datum_hash, NULL); dap_assert(status == 0, "Test of transaction adding to ledger:"); return status; } @@ -531,7 +531,7 @@ void dap_ledger_test_datums_removing(dap_ledger_t *a_ledger, dap_hash_fast_t *a_ dap_assert(l_second_tx, "Test of creating second tx:"); dap_chain_hash_fast_t l_second_tx_hash = {0}; dap_hash_fast(l_second_tx, dap_chain_datum_tx_get_size(l_second_tx), &l_second_tx_hash); - dap_assert(!dap_ledger_tx_add(a_ledger, l_second_tx, &l_second_tx_hash, false), "Test of second transaction adding to ledger:"); + dap_assert(!dap_ledger_tx_add(a_ledger, l_second_tx, &l_second_tx_hash, false, NULL), "Test of second transaction adding to ledger:"); dap_ledger_test_print_balance(a_ledger, &l_addr); // try to remove spent tx @@ -548,7 +548,7 @@ void dap_ledger_test_datums_removing(dap_ledger_t *a_ledger, dap_hash_fast_t *a_ dap_assert_PIF(l_cond_tx, "Test of creating conditional transaction:"); dap_chain_hash_fast_t l_cond_tx_hash = {0}; dap_hash_fast(l_cond_tx, dap_chain_datum_tx_get_size(l_first_tx), &l_cond_tx_hash); - dap_assert(!dap_ledger_tx_add(a_ledger, l_cond_tx, &l_cond_tx_hash, false), "Test of conditional transaction adding to ledger:"); + dap_assert(!dap_ledger_tx_add(a_ledger, l_cond_tx, &l_cond_tx_hash, false, NULL), "Test of conditional transaction adding to ledger:"); dap_ledger_test_print_balance(a_ledger, &l_addr); dap_assert(!dap_ledger_tx_remove(a_ledger, l_cond_tx, &l_cond_tx_hash), "Test of conditional transaction removing from ledger:"); l_balance_after = dap_ledger_test_print_balance(a_ledger, &l_addr); @@ -561,7 +561,7 @@ void dap_ledger_test_datums_removing(dap_ledger_t *a_ledger, dap_hash_fast_t *a_ dap_assert_PIF(l_cond_tx, "Test of creating conditional transaction:"); dap_hash_fast_t l_cond_tx_hash = {}; dap_hash_fast(l_cond_tx, dap_chain_datum_tx_get_size(l_cond_tx), &l_cond_tx_hash); - dap_assert(!dap_ledger_tx_add(a_ledger, l_cond_tx, &l_cond_tx_hash, false), "Test of conditional transaction adding to ledger:"); + dap_assert(!dap_ledger_tx_add(a_ledger, l_cond_tx, &l_cond_tx_hash, false, NULL), "Test of conditional transaction adding to ledger:"); dap_ledger_test_print_balance(a_ledger, &l_addr); dap_cert_t *l_cond_spender_cert = dap_cert_generate_mem_with_seed("newCert", DAP_ENC_KEY_TYPE_SIG_PICNIC, "FMknbirh8*^#$RYU*q", 18); @@ -571,7 +571,7 @@ void dap_ledger_test_datums_removing(dap_ledger_t *a_ledger, dap_hash_fast_t *a_ dap_chain_datum_tx_t *l_cond_spendind_tx = dap_ledger_test_create_spend_tx_cond(a_from_key, &l_cond_tx_hash, l_cond_spender_cert->enc_key, dap_chain_uint256_from(1U),a_ledger); dap_chain_hash_fast_t l_spend_cond_tx_hash = {0}; dap_hash_fast(l_cond_spendind_tx, dap_chain_datum_tx_get_size(l_cond_spendind_tx), &l_spend_cond_tx_hash); - dap_assert(!dap_ledger_tx_add(a_ledger, l_cond_spendind_tx, &l_spend_cond_tx_hash, false), "Test adding of transaction spending to a conditional transaction to ledger:"); + dap_assert(!dap_ledger_tx_add(a_ledger, l_cond_spendind_tx, &l_spend_cond_tx_hash, false, NULL), "Test adding of transaction spending to a conditional transaction to ledger:"); uint256_t l_cond_spending_balance_after = dap_ledger_test_print_balance(a_ledger, &l_cond_spender_addr); dap_assert(!compare256(l_cond_spending_balance_after, dap_chain_uint256_from(1U)), "Check balance after spending:"); dap_ledger_test_print_balance(a_ledger, &l_cond_spender_addr); @@ -588,7 +588,7 @@ void dap_ledger_test_datums_removing(dap_ledger_t *a_ledger, dap_hash_fast_t *a_ dap_chain_datum_tx_t *l_cond_tx = dap_ledger_test_create_tx_cond(a_from_key, a_prev_hash, &l_addr_first, dap_chain_uint256_from(2U),a_ledger); dap_hash_fast_t l_cond_tx_hash = {}; dap_hash_fast(l_cond_tx, dap_chain_datum_tx_get_size(l_cond_tx), &l_cond_tx_hash); - dap_assert(!dap_ledger_tx_add(a_ledger, l_cond_tx, &l_cond_tx_hash, false), "Adding of cond transaction to ledger is"); + dap_assert(!dap_ledger_tx_add(a_ledger, l_cond_tx, &l_cond_tx_hash, false, NULL), "Adding of cond transaction to ledger is"); dap_cert_t *l_cond_spending_cert = dap_cert_generate_mem_with_seed("newCert", DAP_ENC_KEY_TYPE_SIG_PICNIC, "FMknbirh8*^#$RYU*q", 18); dap_chain_addr_t l_cond_spending_addr = {0}; @@ -597,7 +597,7 @@ void dap_ledger_test_datums_removing(dap_ledger_t *a_ledger, dap_hash_fast_t *a_ dap_chain_datum_tx_t *l_cond_returning_tx = dap_ledger_test_create_return_from_tx_cond(&l_cond_tx_hash, a_from_key ,a_ledger); dap_chain_hash_fast_t l_cond_returning_tx_hash = {0}; dap_hash_fast(l_cond_returning_tx, dap_chain_datum_tx_get_size(l_cond_returning_tx), &l_cond_returning_tx_hash); - int err_code = dap_ledger_tx_add(a_ledger, l_cond_returning_tx, &l_cond_returning_tx_hash, false); + int err_code = dap_ledger_tx_add(a_ledger, l_cond_returning_tx, &l_cond_returning_tx_hash, false, NULL); printf("err_code = %s\n", dap_ledger_check_error_str(err_code)); dap_assert(!err_code, "Returning of funds from cond transaction is"); uint256_t l_cond_spending_balance_after = dap_ledger_test_print_balance(a_ledger, &l_cond_spending_addr); @@ -617,7 +617,7 @@ void dap_ledger_test_datums_removing(dap_ledger_t *a_ledger, dap_hash_fast_t *a_ dap_chain_datum_tx_t *l_cond_tx = dap_ledger_test_create_stake_tx_cond(a_from_key, a_prev_hash, dap_chain_uint256_from(20U), a_ledger); dap_hash_fast_t l_cond_tx_hash = {}; dap_hash_fast(l_cond_tx, dap_chain_datum_tx_get_size(l_cond_tx), &l_cond_tx_hash); - int err_code = dap_ledger_tx_add(a_ledger, l_cond_tx, &l_cond_tx_hash, false); + int err_code = dap_ledger_tx_add(a_ledger, l_cond_tx, &l_cond_tx_hash, false, NULL); printf("err_code = %s\n", dap_ledger_check_error_str(err_code)); dap_assert(!err_code, "Adding of stake cond transaction to ledger is"); @@ -632,7 +632,7 @@ void dap_ledger_test_datums_removing(dap_ledger_t *a_ledger, dap_hash_fast_t *a_ dap_chain_datum_tx_t *l_stake_cond_tx = dap_ledger_test_create_stake_tx_cond(a_from_key, a_prev_hash, dap_chain_uint256_from(20U), a_ledger); dap_hash_fast_t l_stake_cond_tx_hash = {}; dap_hash_fast(l_stake_cond_tx, dap_chain_datum_tx_get_size(l_stake_cond_tx), &l_stake_cond_tx_hash); - int err_code = dap_ledger_tx_add(a_ledger, l_stake_cond_tx, &l_stake_cond_tx_hash, false); + int err_code = dap_ledger_tx_add(a_ledger, l_stake_cond_tx, &l_stake_cond_tx_hash, false, NULL); printf("err_code = %s\n", dap_ledger_check_error_str(err_code)); dap_assert(!err_code, "Adding of stake cond transaction to ledger is"); sleep(3); @@ -644,7 +644,7 @@ void dap_ledger_test_datums_removing(dap_ledger_t *a_ledger, dap_hash_fast_t *a_ dap_chain_datum_tx_t *l_unstake_cond_tx = dap_ledger_test_create_unstake_tx_cond(a_from_key, &l_stake_cond_tx_hash, dap_chain_uint256_from(20U), a_ledger); dap_hash_fast_t l_unstake_cond_tx_hash = {}; dap_hash_fast(l_unstake_cond_tx, dap_chain_datum_tx_get_size(l_unstake_cond_tx), &l_unstake_cond_tx_hash); - err_code = dap_ledger_tx_add(a_ledger, l_unstake_cond_tx, &l_unstake_cond_tx_hash, false); + err_code = dap_ledger_tx_add(a_ledger, l_unstake_cond_tx, &l_unstake_cond_tx_hash, false, NULL); printf("err_code = %s\n", dap_ledger_check_error_str(err_code)); dap_assert(!err_code, "Adding of unstake cond transaction to ledger is"); uint256_t l_balance_delegated_after_unstaking = dap_ledger_test_print_delegate_balance(a_ledger, &l_addr); @@ -694,14 +694,14 @@ dap_hash_fast_t dap_ledger_test_double_spending( dap_assert_PIF(l_first_tx, "Can't creating base transaction."); dap_chain_hash_fast_t l_first_tx_hash = {0}; dap_hash_fast(l_first_tx, dap_chain_datum_tx_get_size(l_first_tx), &l_first_tx_hash); - dap_assert_PIF(!dap_ledger_tx_add(a_ledger, l_first_tx, &l_first_tx_hash, false), "Can't add first transaction on ledger"); + dap_assert_PIF(!dap_ledger_tx_add(a_ledger, l_first_tx, &l_first_tx_hash, false, NULL), "Can't add first transaction on ledger"); //uint256_t l_balance = dap_ledger_calc_balance(a_ledger, &l_addr_first, s_token_ticker); // Second tx dap_chain_datum_tx_t *l_second_tx = dap_ledger_test_create_tx(a_from_key, a_prev_hash, &a_addr_to, dap_chain_uint256_from(s_standard_value_tx - s_fee)); dap_chain_hash_fast_t l_second_tx_hash = {0}; dap_hash_fast(l_second_tx, dap_chain_datum_tx_get_size(l_second_tx), &l_second_tx_hash); - dap_assert_PIF(dap_ledger_tx_add(a_ledger, l_second_tx, &l_second_tx_hash, false), "Added second transaction on ledger"); + dap_assert_PIF(dap_ledger_tx_add(a_ledger, l_second_tx, &l_second_tx_hash, false, NULL), "Added second transaction on ledger"); dap_pass_msg("The verification test is not able to make two normal transactions per one basic transaction."); return l_first_tx_hash; } @@ -831,7 +831,7 @@ void dap_ledger_test_write_back_list(dap_ledger_t *a_ledger, dap_cert_t *a_cert, *l_addr_1->addr, a_cert); dap_hash_fast_t l_btx_addr1_hash = {0}; dap_hash_fast(l_btx_addr1, dap_chain_datum_tx_get_size(l_btx_addr1), &l_btx_addr1_hash); - int l_ledger_add_code = dap_ledger_tx_add(a_ledger, l_btx_addr1, &l_btx_addr1_hash, false); + int l_ledger_add_code = dap_ledger_tx_add(a_ledger, l_btx_addr1, &l_btx_addr1_hash, false, NULL); char *l_ledger_tx_add_str = dap_strdup_printf("Can't add base tx in white address. Code: %d", l_ledger_add_code); dap_assert_PIF(!l_ledger_add_code, l_ledger_tx_add_str); DAP_DELETE(l_ledger_tx_add_str); @@ -839,13 +839,13 @@ void dap_ledger_test_write_back_list(dap_ledger_t *a_ledger, dap_cert_t *a_cert, dap_chain_datum_tx_t *l_tx_to_addr4 = dap_ledger_test_create_tx(l_addr_1->enc_key, &l_btx_addr1_hash, l_addr_4->addr, dap_chain_uint256_from(s_total_supply-s_fee)); dap_hash_fast(l_tx_to_addr4, dap_chain_datum_tx_get_size(l_tx_to_addr4), &l_tx_addr4_hash); - dap_assert_PIF(!dap_ledger_tx_add(a_ledger, l_tx_to_addr4, &l_tx_addr4_hash, false), + dap_assert_PIF(!dap_ledger_tx_add(a_ledger, l_tx_to_addr4, &l_tx_addr4_hash, false, NULL), "Can't add transaction to address from white list in ledger"); dap_chain_datum_tx_t *l_tx_to_addr3 = dap_ledger_test_create_tx(l_addr_4->enc_key, &l_tx_addr4_hash, l_addr_3->addr, dap_chain_uint256_from(s_total_supply-s_fee)); dap_hash_fast_t l_tx_addr3_hash = {0}; dap_hash_fast(l_tx_to_addr3, dap_chain_datum_tx_get_size(l_tx_to_addr3), &l_tx_addr3_hash); - int res_add_tx = dap_ledger_tx_add(a_ledger, l_tx_to_addr3, &l_tx_addr3_hash, false); + int res_add_tx = dap_ledger_tx_add(a_ledger, l_tx_to_addr3, &l_tx_addr3_hash, false, NULL); if (!res_add_tx) { dap_fail("It was possible to carry out a transaction to a forbidden address"); } else { @@ -945,20 +945,20 @@ void dap_ledger_test_write_back_list(dap_ledger_t *a_ledger, dap_cert_t *a_cert, *l_addr_2->addr, a_cert); dap_hash_fast_t l_btx_addr2_hash = {0}; dap_hash_fast(l_btx_addr2, dap_chain_datum_tx_get_size(l_btx_addr2), &l_btx_addr2_hash); - dap_assert_PIF(!dap_ledger_tx_add(a_ledger, l_btx_addr2, &l_btx_addr2_hash, false), + dap_assert_PIF(!dap_ledger_tx_add(a_ledger, l_btx_addr2, &l_btx_addr2_hash, false, NULL), "Can't add base tx in white address"); //Check tx in addr from block list dap_chain_datum_tx_t *l_tx_to_addr1 = dap_ledger_test_create_tx(l_addr_4->enc_key, &l_btx_addr2_hash, l_addr_1->addr, dap_chain_uint256_from(s_total_supply)); dap_hash_fast_t l_tx_addr1_hash = {0}; dap_hash_fast(l_tx_to_addr1, dap_chain_datum_tx_get_size(l_tx_to_addr1), &l_tx_addr1_hash); - dap_assert(dap_ledger_tx_add(a_ledger, l_tx_to_addr1, &l_tx_addr1_hash, false), "Transfer test to a forbidden address."); + dap_assert(dap_ledger_tx_add(a_ledger, l_tx_to_addr1, &l_tx_addr1_hash, false, NULL), "Transfer test to a forbidden address."); //Check tx in addr from list dap_chain_datum_tx_t *l_tx_to_addr3 = dap_ledger_test_create_tx(l_addr_4->enc_key, &l_tx_addr1_hash, l_addr_3->addr, dap_chain_uint256_from(s_total_supply)); dap_hash_fast_t l_tx_addr3_hash = {0}; dap_hash_fast(l_tx_to_addr3, dap_chain_datum_tx_get_size(l_tx_to_addr3), &l_tx_addr3_hash); - dap_assert(dap_ledger_tx_add(a_ledger, l_tx_to_addr3, &l_tx_addr3_hash, false), "Transfer test to a not forbidden address."); + dap_assert(dap_ledger_tx_add(a_ledger, l_tx_to_addr3, &l_tx_addr3_hash, false, NULL), "Transfer test to a not forbidden address."); } } @@ -1041,7 +1041,7 @@ void dap_ledger_test_run(void){ dap_hash_fast_t l_hash_btx = {0}; dap_hash_fast(l_base_tx, l_base_tx_size, &l_hash_btx); dap_assert_PIF(!dap_ledger_tx_add_check(l_ledger, l_base_tx, l_base_tx_size, &l_hash_btx), "Check can added base tx in ledger"); - dap_assert_PIF(!dap_ledger_tx_add(l_ledger, l_base_tx, &l_hash_btx, false), "Added base tx in ledger."); + dap_assert_PIF(!dap_ledger_tx_add(l_ledger, l_base_tx, &l_hash_btx, false, NULL), "Added base tx in ledger."); uint256_t l_balance_example = dap_chain_uint256_from(s_standard_value_tx); uint256_t l_balance = dap_ledger_calc_balance(l_ledger, &l_addr, s_token_ticker); uint256_t l_fee = dap_chain_uint256_from(s_fee); @@ -1057,7 +1057,7 @@ void dap_ledger_test_run(void){ if (dap_ledger_tx_add_check(l_ledger, l_base_tx_second, l_base_tx_size2, &l_hash_btx_second)) { dap_pass_msg("Checking can added second base tx in ledger"); } - if (dap_ledger_tx_add(l_ledger, l_base_tx_second, &l_hash_btx_second, false)){ + if (dap_ledger_tx_add(l_ledger, l_base_tx_second, &l_hash_btx_second, false, NULL)){ dap_pass_msg("Checking for a failure to add a second base transaction for the same issue to the ledger."); } else { dap_fail("Checking for a failure to add a second base transaction for the same issue to the ledger."); diff --git a/modules/common/dap_chain_datum.c b/modules/common/dap_chain_datum.c index 8394a83ea5872759fe1a25480b7602c486bfc783..14e8c79d094406edb53ac64d59fd19806dcf9ef6 100644 --- a/modules/common/dap_chain_datum.c +++ b/modules/common/dap_chain_datum.c @@ -150,7 +150,7 @@ void dap_datum_token_dump_tsd_to_json(json_object * json_obj_out, dap_chain_datu DAP_DEL_Z(balance); }continue; case DAP_CHAIN_DATUM_TOKEN_TSD_TYPE_DATUM_TYPE_ALLOWED_ADD : - json_object_object_add(json_obj_out, "datum_type_allowed_add", json_object_new_string(dap_tsd_get_string_const(l_tsd))); + json_object_object_add(json_obj_out, "datum_type_allowed_add", json_object_new_string(dap_tsd_get_string_const(l_tsd))); continue; case DAP_CHAIN_DATUM_TOKEN_TSD_TYPE_DATUM_TYPE_ALLOWED_REMOVE : json_object_object_add(json_obj_out, "datum_type_allowed_remove", json_object_new_string(dap_tsd_get_string_const(l_tsd))); @@ -161,30 +161,38 @@ void dap_datum_token_dump_tsd_to_json(json_object * json_obj_out, dap_chain_datu case DAP_CHAIN_DATUM_TOKEN_TSD_TYPE_DATUM_TYPE_BLOCKED_REMOVE: json_object_object_add(json_obj_out, "datum_type_blocked_remove", json_object_new_string(dap_tsd_get_string_const(l_tsd))); continue; - case DAP_CHAIN_DATUM_TOKEN_TSD_TYPE_TX_SENDER_ALLOWED_ADD: - json_object_object_add(json_obj_out, "tx_sender_allowed_add", json_object_new_string(dap_tsd_get_string_const(l_tsd))); - continue; - case DAP_CHAIN_DATUM_TOKEN_TSD_TYPE_TX_SENDER_ALLOWED_REMOVE: - json_object_object_add(json_obj_out, "tx_sender_allowed_remove", json_object_new_string(dap_tsd_get_string_const(l_tsd))); - continue; - case DAP_CHAIN_DATUM_TOKEN_TSD_TYPE_TX_SENDER_BLOCKED_ADD: - json_object_object_add(json_obj_out, "tx_sender_blocked_add", json_object_new_string(dap_tsd_get_string_const(l_tsd))); - continue; - case DAP_CHAIN_DATUM_TOKEN_TSD_TYPE_TX_SENDER_BLOCKED_REMOVE: - json_object_object_add(json_obj_out, "tx_sender_blocked_remove", json_object_new_string(dap_tsd_get_string_const(l_tsd))); - continue; - case DAP_CHAIN_DATUM_TOKEN_TSD_TYPE_TX_RECEIVER_ALLOWED_ADD: - json_object_object_add(json_obj_out, "tx_receiver_allowed_add", json_object_new_string(dap_tsd_get_string_const(l_tsd))); - continue; - case DAP_CHAIN_DATUM_TOKEN_TSD_TYPE_TX_RECEIVER_ALLOWED_REMOVE: - json_object_object_add(json_obj_out, "tx_receiver_allowed", json_object_new_string(dap_tsd_get_string_const(l_tsd))); - continue; - case DAP_CHAIN_DATUM_TOKEN_TSD_TYPE_TX_RECEIVER_BLOCKED_ADD: - json_object_object_add(json_obj_out, "tx_receiver_blocked_add", json_object_new_string(dap_tsd_get_string_const(l_tsd))); - continue; - case DAP_CHAIN_DATUM_TOKEN_TSD_TYPE_TX_RECEIVER_BLOCKED_REMOVE: - json_object_object_add(json_obj_out, "tx_receiver_blocked_remove", json_object_new_string(dap_tsd_get_string_const(l_tsd))); - continue; + case DAP_CHAIN_DATUM_TOKEN_TSD_TYPE_TX_SENDER_ALLOWED_ADD: { + dap_chain_addr_t *l_addr = dap_tsd_get_object(l_tsd, dap_chain_addr_t); + json_object_object_add(json_obj_out, "tx_sender_allowed_add", json_object_new_string(dap_chain_addr_to_str_static(l_addr))); + } continue; + case DAP_CHAIN_DATUM_TOKEN_TSD_TYPE_TX_SENDER_ALLOWED_REMOVE:{ + dap_chain_addr_t *l_addr = dap_tsd_get_object(l_tsd, dap_chain_addr_t); + json_object_object_add(json_obj_out, "tx_sender_allowed_remove", json_object_new_string(dap_chain_addr_to_str_static(l_addr))); + } continue; + case DAP_CHAIN_DATUM_TOKEN_TSD_TYPE_TX_SENDER_BLOCKED_ADD: { + dap_chain_addr_t *l_addr = dap_tsd_get_object(l_tsd, dap_chain_addr_t); + json_object_object_add(json_obj_out, "tx_sender_blocked_add", json_object_new_string(dap_chain_addr_to_str_static(l_addr))); + } continue; + case DAP_CHAIN_DATUM_TOKEN_TSD_TYPE_TX_SENDER_BLOCKED_REMOVE: { + dap_chain_addr_t *l_addr = dap_tsd_get_object(l_tsd, dap_chain_addr_t); + json_object_object_add(json_obj_out, "tx_sender_blocked_remove", json_object_new_string(dap_chain_addr_to_str_static(l_addr))); + } continue; + case DAP_CHAIN_DATUM_TOKEN_TSD_TYPE_TX_RECEIVER_ALLOWED_ADD: { + dap_chain_addr_t *l_addr = dap_tsd_get_object(l_tsd, dap_chain_addr_t); + json_object_object_add(json_obj_out, "tx_receiver_allowed_add", json_object_new_string(dap_chain_addr_to_str_static(l_addr))); + } continue; + case DAP_CHAIN_DATUM_TOKEN_TSD_TYPE_TX_RECEIVER_ALLOWED_REMOVE: { + dap_chain_addr_t *l_addr = dap_tsd_get_object(l_tsd, dap_chain_addr_t); + json_object_object_add(json_obj_out, "tx_receiver_allowed", json_object_new_string(dap_chain_addr_to_str_static(l_addr))); + } continue; + case DAP_CHAIN_DATUM_TOKEN_TSD_TYPE_TX_RECEIVER_BLOCKED_ADD: { + dap_chain_addr_t *l_addr = dap_tsd_get_object(l_tsd, dap_chain_addr_t); + json_object_object_add(json_obj_out, "tx_receiver_blocked_add", json_object_new_string(dap_chain_addr_to_str_static(l_addr))); + } continue; + case DAP_CHAIN_DATUM_TOKEN_TSD_TYPE_TX_RECEIVER_BLOCKED_REMOVE: { + dap_chain_addr_t *l_addr = dap_tsd_get_object(l_tsd, dap_chain_addr_t); + json_object_object_add(json_obj_out, "tx_receiver_blocked_remove", json_object_new_string(dap_chain_addr_to_str_static(l_addr))); + } continue; case DAP_CHAIN_DATUM_TOKEN_TSD_TOKEN_DESCRIPTION: json_object_object_add(json_obj_out, "description", json_object_new_string(dap_tsd_get_string_const(l_tsd))); continue; @@ -198,313 +206,6 @@ void dap_datum_token_dump_tsd_to_json(json_object * json_obj_out, dap_chain_datu } } -/** - * @brief _dap_chain_datum_tx_out_data - * - * @param a_datum - * @param a_ledger - * @param a_str_out - * @param a_hash_out_type - * @param save_processed_tx - * @param a_tx_hash_processed - * @param l_tx_num - */ -bool dap_chain_datum_dump_tx(dap_chain_datum_tx_t *a_datum, - const char *a_ticker, - dap_string_t *a_str_out, - const char *a_hash_out_type, - dap_hash_fast_t *a_tx_hash, - dap_chain_net_id_t a_net_id) -{ - bool l_is_first = false; - dap_chain_tx_in_t *l_in_item = (dap_chain_tx_in_t *)dap_chain_datum_tx_item_get(a_datum, NULL, NULL, TX_ITEM_TYPE_IN, NULL); - if (l_in_item && dap_hash_fast_is_blank(&l_in_item->header.tx_prev_hash)) - l_is_first = true; - char l_tmp_buf[DAP_TIME_STR_SIZE]; - const char *l_hash_str = dap_strcmp(a_hash_out_type, "hex") - ? dap_enc_base58_encode_hash_to_str_static(a_tx_hash) - : dap_chain_hash_fast_to_str_static(a_tx_hash); - dap_time_to_str_rfc822(l_tmp_buf, DAP_TIME_STR_SIZE, a_datum->header.ts_created); - dap_string_append_printf(a_str_out, "transaction:%s hash %s\n TS Created: %s%s%s\n Items:\n", - l_is_first ? " (emit)" : "", l_hash_str, l_tmp_buf, - a_ticker ? " Token ticker: " : "", a_ticker ? a_ticker : ""); - dap_hash_fast_t l_hash_tmp = { }; - byte_t *item; size_t l_size; - TX_ITEM_ITER_TX(item, l_size, a_datum) { - switch (*item) { - case TX_ITEM_TYPE_IN: - l_hash_tmp = ((dap_chain_tx_in_t*)item)->header.tx_prev_hash; - l_hash_str = !dap_hash_fast_is_blank(&l_hash_tmp) - ? dap_strcmp(a_hash_out_type, "hex") ? dap_enc_base58_encode_hash_to_str_static(&l_hash_tmp) : dap_chain_hash_fast_to_str_static(&l_hash_tmp) - : "BLANK"; - dap_string_append_printf(a_str_out, "\t IN:\nTx_prev_hash: %s\n" - "\t\t Tx_out_prev_idx: %u\n", - l_hash_str, - ((dap_chain_tx_in_t*)item)->header.tx_out_prev_idx); - break; - case TX_ITEM_TYPE_OUT_OLD: { - const char *l_value_str = dap_uint256_to_char( - dap_chain_uint256_from(((dap_chain_tx_out_old_t*)item)->header.value), NULL ); - dap_string_append_printf(a_str_out, "\t OUT OLD (64):\n" - "\t\t Value: %s (%"DAP_UINT64_FORMAT_U")\n" - "\t\t Address: %s\n", - l_value_str, - ((dap_chain_tx_out_old_t*)item)->header.value, - dap_chain_addr_to_str_static(&((dap_chain_tx_out_old_t*)item)->addr)); - } break; - case TX_ITEM_TYPE_OUT: { // 256 - const char *l_coins_str, - *l_value_str = dap_uint256_to_char(((dap_chain_tx_out_t*)item)->header.value, &l_coins_str), - *l_addr_str = dap_chain_addr_to_str_static(&((dap_chain_tx_out_t*)item)->addr); - dap_string_append_printf(a_str_out, "\t OUT:\n" - "\t\t Value: %s (%s)\n" - "\t\t Address: %s\n", - l_coins_str, - l_value_str, - l_addr_str); - } break; - case TX_ITEM_TYPE_IN_EMS: { - l_hash_tmp = ((dap_chain_tx_in_ems_t*)item)->header.token_emission_hash; - l_hash_str = dap_strcmp(a_hash_out_type, "hex") - ? dap_enc_base58_encode_hash_to_str_static(&l_hash_tmp) - : dap_chain_hash_fast_to_str_static(&l_hash_tmp); - dap_string_append_printf(a_str_out, "\t IN_EMS:\n" - "\t\t ticker: %s \n" - "\t\t token_emission_hash: %s\n" - "\t\t token_emission_chain_id: 0x%016"DAP_UINT64_FORMAT_x"\n", - ((dap_chain_tx_in_ems_t*)item)->header.ticker, - l_hash_str, - ((dap_chain_tx_in_ems_t*)item)->header.token_emission_chain_id.uint64); - } break; - /* - case TX_ITEM_TYPE_IN_EMS_EXT: { - l_hash_tmp = &((dap_chain_tx_in_ems_ext_t*)item)->header.ext_tx_hash; - l_hash_str = dap_strcmp(a_hash_out_type, "hex") - ? dap_enc_base58_encode_hash_to_str(l_hash_tmp) - : dap_chain_hash_fast_to_str_new(l_hash_tmp); - dap_string_append_printf(a_str_out, "\t IN_EMS EXT:\n" - "\t\t Version: %u\n" - "\t\t Ticker: %s\n" - "\t\t Ext chain id: 0x%016"DAP_UINT64_FORMAT_x"\n" - "\t\t Ext net id: 0x%016"DAP_UINT64_FORMAT_x"\n" - "\t\t Ext tx hash: %s\n" - "\t\t Ext tx out idx: %u\n", - ((dap_chain_tx_in_ems_ext_t*)item)->header.version, - ((dap_chain_tx_in_ems_ext_t*)item)->header.ticker, - ((dap_chain_tx_in_ems_ext_t*)item)->header.ext_chain_id.uint64, - ((dap_chain_tx_in_ems_ext_t*)item)->header.ext_net_id.uint64, - l_hash_str, - ((dap_chain_tx_in_ems_ext_t*)item)->header.ext_tx_out_idx); - DAP_DELETE(l_hash_str); - } break; */ - - case TX_ITEM_TYPE_IN_REWARD: { - l_hash_tmp = ((dap_chain_tx_in_reward_t *)item)->block_hash; - l_hash_str = dap_strcmp(a_hash_out_type, "hex") - ? dap_enc_base58_encode_hash_to_str_static(&l_hash_tmp) - : dap_chain_hash_fast_to_str_static(&l_hash_tmp); - dap_string_append_printf(a_str_out, "\t IN_REWARD:\n" - "\t\t block_hash: %s\n", - l_hash_str); - } break; - - case TX_ITEM_TYPE_SIG: { - dap_sign_t *l_sign = dap_chain_datum_tx_item_sign_get_sig((dap_chain_tx_sig_t*)item); - dap_sign_get_information(l_sign, a_str_out, a_hash_out_type); - dap_chain_addr_t l_sender_addr; - dap_chain_addr_fill_from_sign(&l_sender_addr, l_sign, a_net_id); - dap_string_append_printf(a_str_out, "\tSender addr: %s\n", dap_chain_addr_to_str_static(&l_sender_addr)); - } break; - case TX_ITEM_TYPE_RECEIPT: { - const char *l_coins_str, *l_value_str = dap_uint256_to_char(((dap_chain_datum_tx_receipt_t*)item)->receipt_info.value_datoshi, &l_coins_str); - dap_string_append_printf(a_str_out, "\t Receipt:\n" - "\t\t size: %"DAP_UINT64_FORMAT_U"\n" - "\t\t ext size: %"DAP_UINT64_FORMAT_U"\n" - "\t\t Info:" - "\t\t\t units: 0x%016"DAP_UINT64_FORMAT_x"\n" - "\t\t\t uid: 0x%016"DAP_UINT64_FORMAT_x"\n" - "\t\t\t units type: %s \n" - "\t\t\t value: %s (%s)\n", - ((dap_chain_datum_tx_receipt_t*)item)->size, - ((dap_chain_datum_tx_receipt_t*)item)->exts_size, - ((dap_chain_datum_tx_receipt_t*)item)->receipt_info.units, - ((dap_chain_datum_tx_receipt_t*)item)->receipt_info.srv_uid.uint64, - dap_chain_srv_unit_enum_to_str(((dap_chain_datum_tx_receipt_t*)item)->receipt_info.units_type.enm), - l_coins_str, - l_value_str); - dap_string_append_printf(a_str_out, "Exts:\n"); - switch ( ((dap_chain_datum_tx_receipt_t*)item)->exts_size ) { - case (sizeof(dap_sign_t) * 2): { - dap_sign_t *l_client = DAP_CAST_PTR( dap_sign_t, ((dap_chain_datum_tx_receipt_t*)item)->exts_n_signs + sizeof(dap_sign_t) ); - dap_string_append_printf(a_str_out, " Client:\n"); - dap_sign_get_information(l_client, a_str_out, a_hash_out_type); - } - case (sizeof(dap_sign_t)): { - dap_sign_t *l_provider = DAP_CAST_PTR( dap_sign_t, ((dap_chain_datum_tx_receipt_t*)item)->exts_n_signs ); - dap_string_append_printf(a_str_out, " Provider:\n"); - dap_sign_get_information(l_provider, a_str_out, a_hash_out_type); - break; - } - } - } break; - case TX_ITEM_TYPE_PKEY: { - dap_pkey_t *l_pkey = (dap_pkey_t*)((dap_chain_tx_pkey_t*)item)->pkey; - dap_chain_hash_fast_t l_pkey_hash; - dap_hash_fast(l_pkey->pkey, l_pkey->header.size, &l_pkey_hash); - l_hash_str = dap_strcmp(a_hash_out_type, "hex") - ? dap_enc_base58_encode_hash_to_str_static(&l_pkey_hash) - : dap_chain_hash_fast_to_str_static(&l_pkey_hash); - dap_string_append_printf(a_str_out, "\t PKey: \n" - "\t\t SIG type: %s\n" - "\t\t SIG size: %u\n" - "\t\t Sequence number: %u \n" - "\t\t Key: \n" - "\t\t\t Type: %s\n" - "\t\t\t Size: %u\n" - "\t\t\t Hash: %s\n", - dap_sign_type_to_str(((dap_chain_tx_pkey_t*)item)->header.sig_type), - ((dap_chain_tx_pkey_t*)item)->header.sig_size, - ((dap_chain_tx_pkey_t*)item)->seq_no, - dap_pkey_type_to_str(l_pkey->header.type), - l_pkey->header.size, - l_hash_str); - } break; - case TX_ITEM_TYPE_TSD: { - dap_string_append_printf(a_str_out, "\t TSD data: \n" - "\t\t type: %d\n" - "\t\t size: %lu\n", - ((dap_chain_tx_tsd_t*)item)->header.type, - ((dap_chain_tx_tsd_t*)item)->header.size); - } break; - case TX_ITEM_TYPE_IN_COND: - l_hash_tmp = ((dap_chain_tx_in_cond_t*)item)->header.tx_prev_hash; - l_hash_str = dap_strcmp(a_hash_out_type, "hex") - ? dap_enc_base58_encode_hash_to_str_static(&l_hash_tmp) - : dap_chain_hash_fast_to_str_static(&l_hash_tmp); - dap_string_append_printf(a_str_out, "\t IN COND:\n\t\tReceipt_idx: %u\n" - "\t\t Tx_prev_hash: %s\n" - "\t\t Tx_out_prev_idx: %u\n", - ((dap_chain_tx_in_cond_t*)item)->header.receipt_idx, - l_hash_str, - ((dap_chain_tx_in_cond_t*)item)->header.tx_out_prev_idx); - break; - case TX_ITEM_TYPE_OUT_COND: { - const char *l_coins_str, *l_value_str = dap_uint256_to_char(((dap_chain_tx_out_cond_t*)item)->header.value, &l_coins_str); - dap_time_t l_ts_exp = ((dap_chain_tx_out_cond_t*)item)->header.ts_expires; - dap_time_to_str_rfc822(l_tmp_buf, DAP_TIME_STR_SIZE, l_ts_exp); - dap_string_append_printf(a_str_out, "\t OUT COND:\n" - "\t Header:\n" - "\t\t ts_expires: %s" - "\t\t value: %s (%s)\n" - "\t\t subtype: %s\n" - "\t\t uid: 0x%016"DAP_UINT64_FORMAT_x"\n", - l_ts_exp ? l_tmp_buf : "never\n", l_coins_str, l_value_str, - dap_chain_tx_out_cond_subtype_to_str(((dap_chain_tx_out_cond_t*)item)->header.subtype), - ((dap_chain_tx_out_cond_t*)item)->header.srv_uid.uint64); - switch (((dap_chain_tx_out_cond_t*)item)->header.subtype) { - case DAP_CHAIN_TX_OUT_COND_SUBTYPE_SRV_PAY: { - const char *l_coins_str, *l_value_str = - dap_uint256_to_char( ((dap_chain_tx_out_cond_t*)item)->subtype.srv_pay.unit_price_max_datoshi, &l_coins_str ); - l_hash_tmp = ((dap_chain_tx_out_cond_t*)item)->subtype.srv_pay.pkey_hash; - l_hash_str = dap_strcmp(a_hash_out_type, "hex") - ? dap_enc_base58_encode_hash_to_str_static(&l_hash_tmp) - : dap_chain_hash_fast_to_str_static(&l_hash_tmp); - dap_string_append_printf(a_str_out, "\t\t\t unit: 0x%08x\n" - "\t\t\t pkey: %s\n" - "\t\t\t max price: %s (%s)\n", - ((dap_chain_tx_out_cond_t*)item)->subtype.srv_pay.unit.uint32, - l_hash_str, - l_coins_str, - l_value_str); - } break; - case DAP_CHAIN_TX_OUT_COND_SUBTYPE_SRV_STAKE_POS_DELEGATE: { - dap_chain_node_addr_t *l_signer_node_addr = &((dap_chain_tx_out_cond_t*)item)->subtype.srv_stake_pos_delegate.signer_node_addr; - dap_chain_addr_t *l_signing_addr = &((dap_chain_tx_out_cond_t*)item)->subtype.srv_stake_pos_delegate.signing_addr; - l_hash_tmp = l_signing_addr->data.hash_fast; - l_hash_str = dap_strcmp(a_hash_out_type, "hex") - ? dap_enc_base58_encode_hash_to_str_static(&l_hash_tmp) - : dap_chain_hash_fast_to_str_static(&l_hash_tmp); - dap_string_append_printf(a_str_out, "\t\t\t signing_addr: %s\n" - "\t\t\t with pkey hash %s\n" - "\t\t\t signer_node_addr: "NODE_ADDR_FP_STR"\n", - dap_chain_addr_to_str_static(l_signing_addr), - l_hash_str, - NODE_ADDR_FP_ARGS(l_signer_node_addr)); - } break; - case DAP_CHAIN_TX_OUT_COND_SUBTYPE_SRV_XCHANGE: { - const char *l_rate_str, *l_tmp_str = - dap_uint256_to_char( (((dap_chain_tx_out_cond_t*)item)->subtype.srv_xchange.rate), &l_rate_str ); - dap_string_append_printf(a_str_out, "\t\t\t net id: 0x%016"DAP_UINT64_FORMAT_x"\n" - "\t\t\t buy_token: %s\n" - "\t\t\t rate: %s\n", - ((dap_chain_tx_out_cond_t*)item)->subtype.srv_xchange.buy_net_id.uint64, - ((dap_chain_tx_out_cond_t*)item)->subtype.srv_xchange.buy_token, - l_rate_str); - } break; - case DAP_CHAIN_TX_OUT_COND_SUBTYPE_SRV_STAKE_LOCK: { - dap_time_t l_ts_unlock = ((dap_chain_tx_out_cond_t*)item)->subtype.srv_stake_lock.time_unlock; - dap_time_to_str_rfc822(l_tmp_buf, DAP_TIME_STR_SIZE, l_ts_unlock); - dap_string_append_printf(a_str_out, "\t\t\t time_unlock %s\n", l_tmp_buf); - } break; - default: break; - } - } break; - case TX_ITEM_TYPE_OUT_EXT: { - const char *l_coins_str, *l_value_str = dap_uint256_to_char( ((dap_chain_tx_out_ext_t*)item)->header.value, &l_coins_str); - dap_string_append_printf(a_str_out, "\t OUT EXT:\n" - "\t\t Addr: %s\n" - "\t\t Token: %s\n" - "\t\t Value: %s (%s)\n", - dap_chain_addr_to_str_static(&((dap_chain_tx_out_ext_t*)item)->addr), - ((dap_chain_tx_out_ext_t*)item)->token, - l_coins_str, - l_value_str); - } break; - case TX_ITEM_TYPE_VOTING:{ - size_t l_tsd_size = 0; - dap_chain_tx_tsd_t *l_item = (dap_chain_tx_tsd_t *)dap_chain_datum_tx_item_get(a_datum, NULL, (byte_t*)item + l_size, TX_ITEM_TYPE_TSD, &l_tsd_size); - if (!l_item || !l_tsd_size) - break; - dap_chain_datum_tx_voting_params_t *l_voting_params = dap_chain_voting_parse_tsd(a_datum); - dap_string_append_printf(a_str_out, "\t VOTING:\n\tVoting question: %s\n\t Answer options:\n", l_voting_params->voting_question); - dap_list_t *l_temp = l_voting_params->answers_list; - uint8_t l_index = 0; - while (l_temp){ - dap_string_append_printf(a_str_out, "\t\t %i) %s\n", l_index, (char*)l_temp->data); - l_index++; - l_temp = l_temp->next; - } - - if (l_voting_params->voting_expire) { - dap_time_to_str_rfc822(l_tmp_buf, DAP_TIME_STR_SIZE, l_voting_params->voting_expire); - dap_string_append_printf(a_str_out, "\t Voting expire: %s\n", l_tmp_buf); - } - if (l_voting_params->votes_max_count) - dap_string_append_printf(a_str_out, "\t Votes max count: %"DAP_UINT64_FORMAT_U"\n", l_voting_params->votes_max_count); - dap_string_append_printf(a_str_out, "\t Changing vote is %s available.\n", l_voting_params->vote_changing_allowed ? "" : "not"); - dap_string_append_printf(a_str_out, "\t A delegated key is%s required to participate in voting. \n", - l_voting_params->delegate_key_required ? "" : " not"); - - dap_list_free_full(l_voting_params->answers_list, NULL); - DAP_DELETE(l_voting_params->voting_question); - DAP_DELETE(l_voting_params); - } break; - case TX_ITEM_TYPE_VOTE:{ - dap_chain_tx_vote_t *l_vote_item = (dap_chain_tx_vote_t *)item; - const char *l_hash_str = dap_chain_hash_fast_to_str_static(&l_vote_item->voting_hash); - dap_string_append_printf(a_str_out, "\t VOTE: \n" - "\t Voting hash: %s\n" - "\t Vote answer idx: %"DAP_UINT64_FORMAT_U"\n", l_hash_str, l_vote_item->answer_idx); - } break; - default: - dap_string_append_printf(a_str_out, " This transaction have unknown item type \n"); - break; - } - } - dap_string_append_printf(a_str_out, "\n"); - return true; -} - /** * @brief _dap_chain_datum_tx_out_data * @@ -583,27 +284,6 @@ bool dap_chain_datum_dump_tx_json(dap_chain_datum_tx_t *a_datum, sprintf(l_tmp_buff,"0x%016"DAP_UINT64_FORMAT_x"",((dap_chain_tx_in_ems_t*)item)->header.token_emission_chain_id.uint64); json_object_object_add(json_obj_item,"token_emission_chain_id", json_object_new_string(l_tmp_buff)); } break; - /* - case TX_ITEM_TYPE_IN_EMS_EXT: { - l_hash_tmp = &((dap_chain_tx_in_ems_ext_t*)item)->header.ext_tx_hash; - l_hash_str = dap_strcmp(a_hash_out_type, "hex") - ? dap_enc_base58_encode_hash_to_str(l_hash_tmp) - : dap_chain_hash_fast_to_str_new(l_hash_tmp); - dap_string_append_printf(a_str_out, "\t IN_EMS EXT:\n" - "\t\t Version: %u\n" - "\t\t Ticker: %s\n" - "\t\t Ext chain id: 0x%016"DAP_UINT64_FORMAT_x"\n" - "\t\t Ext net id: 0x%016"DAP_UINT64_FORMAT_x"\n" - "\t\t Ext tx hash: %s\n" - "\t\t Ext tx out idx: %u\n", - ((dap_chain_tx_in_ems_ext_t*)item)->header.version, - ((dap_chain_tx_in_ems_ext_t*)item)->header.ticker, - ((dap_chain_tx_in_ems_ext_t*)item)->header.ext_chain_id.uint64, - ((dap_chain_tx_in_ems_ext_t*)item)->header.ext_net_id.uint64, - l_hash_str, - ((dap_chain_tx_in_ems_ext_t*)item)->header.ext_tx_out_idx); - DAP_DELETE(l_hash_str); - } break; */ case TX_ITEM_TYPE_IN_REWARD: { l_hash_tmp = ((dap_chain_tx_in_reward_t *)item)->block_hash; @@ -678,7 +358,7 @@ bool dap_chain_datum_dump_tx_json(dap_chain_datum_tx_t *a_datum, l_hash_str = dap_strcmp(a_hash_out_type, "hex") ? dap_enc_base58_encode_hash_to_str_static(&l_hash_tmp) : dap_chain_hash_fast_to_str_static(&l_hash_tmp); - json_object_object_add(json_obj_item,"Receipt_idx", json_object_new_uint64(((dap_chain_tx_in_cond_t*)item)->header.receipt_idx)); + json_object_object_add(json_obj_item,"Receipt_idx", json_object_new_int(((dap_chain_tx_in_cond_t*)item)->header.receipt_idx)); json_object_object_add(json_obj_item,"Tx_prev_hash", json_object_new_string(l_hash_str)); json_object_object_add(json_obj_item,"Tx_out_prev_idx", json_object_new_uint64(((dap_chain_tx_in_cond_t*)item)->header.tx_out_prev_idx)); break; diff --git a/modules/common/dap_chain_datum_decree.c b/modules/common/dap_chain_datum_decree.c index 467d98c872e104cdd1e7229307f1a27e1b09600f..7a33f3351b2d08c9d6b5c9a67a1a00c5e9c1f76e 100644 --- a/modules/common/dap_chain_datum_decree.c +++ b/modules/common/dap_chain_datum_decree.c @@ -255,6 +255,7 @@ void dap_chain_datum_decree_dump_json(json_object *a_json_out, dap_chain_datum_d ? dap_enc_base58_encode_hash_to_str_static(&l_pkey_signing) : dap_chain_hash_fast_to_str_static(&l_pkey_signing); json_object_object_add(a_json_out, "Signing pkey fingerprint", json_object_new_string(l_pkey_signing_str)); + break; case DAP_CHAIN_DATUM_DECREE_TSD_TYPE_NODE_ADDR: if(l_tsd->size > sizeof(dap_chain_node_addr_t)){ json_object_object_add(a_json_out, "Node addr", json_object_new_string("WRONG SIZE")); diff --git a/modules/common/dap_chain_datum_tx_items.c b/modules/common/dap_chain_datum_tx_items.c index 6bcc83c9e4325fe0750c24cbed8c92c97c725687..757c27c11f7893460efa74d3bb8fa397818f8942 100644 --- a/modules/common/dap_chain_datum_tx_items.c +++ b/modules/common/dap_chain_datum_tx_items.c @@ -536,6 +536,8 @@ uint8_t *dap_chain_datum_tx_item_get_nth(dap_chain_datum_tx_t *a_tx, dap_chain_t */ dap_chain_tx_out_cond_t *dap_chain_datum_tx_out_cond_get(dap_chain_datum_tx_t *a_tx, dap_chain_tx_out_cond_subtype_t a_cond_subtype, int *a_out_num) { + if (!a_tx) + return NULL; int l_idx = a_out_num && *a_out_num > 0 ? -*a_out_num : 0; byte_t *l_item; size_t l_tx_item_size; TX_ITEM_ITER_TX(l_item, l_tx_item_size, a_tx) { diff --git a/modules/common/include/dap_chain_common.h b/modules/common/include/dap_chain_common.h index f3c8a93b92ebc504a91ae28894983090920a8539..5d538a307df9b7a82eb7a94b260c34e1b516e013 100644 --- a/modules/common/include/dap_chain_common.h +++ b/modules/common/include/dap_chain_common.h @@ -75,6 +75,20 @@ typedef union dap_chain_node_role{ uint8_t raw[DAP_CHAIN_NODE_ROLE_SIZE]; } DAP_ALIGN_PACKED dap_chain_node_role_t; +DAP_STATIC_INLINE const char *dap_chain_node_role_to_str(dap_chain_node_role_t a_node_role) +{ + switch (a_node_role.enums) { + case NODE_ROLE_ROOT_MASTER: return "NODE_ROLE_ROOT_MASTER"; + case NODE_ROLE_ROOT: return "NODE_ROLE_ROOT"; + case NODE_ROLE_ARCHIVE: return "NODE_ROLE_ARCHIVE"; + case NODE_ROLE_CELL_MASTER: return "NODE_ROLE_CELL_MASTER"; + case NODE_ROLE_MASTER: return "NODE_ROLE_MASTER"; + case NODE_ROLE_FULL: return "NODE_ROLE_FULL"; + case NODE_ROLE_LIGHT: return "NODE_ROLE_LIGHT"; + default: return "UNDEFINED"; + } +} + typedef dap_stream_node_addr_t dap_chain_node_addr_t; #define dap_chain_node_addr_str_check dap_stream_node_addr_str_check #define dap_chain_node_addr_from_str dap_stream_node_addr_from_str diff --git a/modules/common/include/dap_chain_datum.h b/modules/common/include/dap_chain_datum.h index 8227287524f67ead3b3f73a2286552bc7da333c8..a384070e6576e76bbe71b254db267907b667bd75 100644 --- a/modules/common/include/dap_chain_datum.h +++ b/modules/common/include/dap_chain_datum.h @@ -159,13 +159,6 @@ DAP_STATIC_INLINE const char *dap_chain_datum_type_id_to_str(uint16_t a_type_id) } void dap_datum_token_dump_tsd_to_json(json_object * json_obj_out, dap_chain_datum_token_t *a_token, size_t a_token_size, const char *a_hash_out_type); -void dap_chain_datum_dump(dap_string_t *a_str_out, dap_chain_datum_t *a_datum, const char *a_hash_out_type, dap_chain_net_id_t a_net_id); -bool dap_chain_datum_dump_tx(dap_chain_datum_tx_t *a_datum, - const char *a_ticker, - dap_string_t *a_str_out, - const char *a_hash_out_type, - dap_hash_fast_t *a_tx_hash, - dap_chain_net_id_t a_net_id); bool dap_chain_datum_dump_tx_json(dap_chain_datum_tx_t *a_datum, const char *a_ticker, json_object* json_obj_out, diff --git a/modules/common/include/dap_chain_datum_token.h b/modules/common/include/dap_chain_datum_token.h index 53c5ec4b30ceb0c0003cc12ba1baa598acec9364..8cbd69b20fa7d35b9a9eecc6eeabbed1c85d517a 100644 --- a/modules/common/include/dap_chain_datum_token.h +++ b/modules/common/include/dap_chain_datum_token.h @@ -33,6 +33,7 @@ #include "json_object.h" +#define DAP_CHAIN_DATUM_NONCE_SIZE 64 // Token declaration typedef struct dap_chain_datum_token_old { uint16_t type; @@ -124,8 +125,9 @@ typedef struct dap_chain_datum_token { uint256_t premine_supply; dap_chain_addr_t premine_address; } DAP_ALIGN_PACKED header_public; - byte_t header[256]; // For future changes + byte_t header[192]; // For future changes }; + uint8_t nonce[DAP_CHAIN_DATUM_NONCE_SIZE]; byte_t tsd_n_signs[]; // Signs and/or types-size-data sections } DAP_ALIGN_PACKED dap_chain_datum_token_t; @@ -293,7 +295,6 @@ uint32_t dap_chain_datum_token_flag_from_str(const char *a_str); #define DAP_CHAIN_DATUM_TOKEN_TSD_TYPE_TX_SENDER_BLOCKED_REMOVE 0x0024 #define DAP_CHAIN_DATUM_TOKEN_TSD_TYPE_TX_SENDER_BLOCKED_CLEAR 0x0025 -#define DAP_CHAIN_DATUM_NONCE_SIZE 64 // Get delegated ticker DAP_STATIC_INLINE int dap_chain_datum_token_get_delegated_ticker(char *a_buf, const char *a_ticker) diff --git a/modules/consensus/dag-poa/dap_chain_cs_dag_poa.c b/modules/consensus/dag-poa/dap_chain_cs_dag_poa.c index c8523c0bd1a2e785214f3b5b69175514d6b28504..e9eff2ad1bfca05fedcb5e31eec9a9d211a8d73d 100644 --- a/modules/consensus/dag-poa/dap_chain_cs_dag_poa.c +++ b/modules/consensus/dag-poa/dap_chain_cs_dag_poa.c @@ -85,10 +85,10 @@ static dap_chain_cs_dag_event_t * s_callback_event_create(dap_chain_cs_dag_t * a dap_chain_hash_fast_t * a_hashes, size_t a_hashes_count, size_t* a_event_size); static bool s_callback_round_event_to_chain(dap_chain_cs_dag_poa_round_item_t *a_arg); static int s_callback_event_round_sync(dap_chain_cs_dag_t * a_dag, const char a_op_code, const char *a_group, - const char *a_key, const void *a_value, const size_t a_value_size); + const char *a_key, const void *a_value, const size_t a_value_size, bool a_by_us); static bool s_round_event_ready_minimum_check(dap_chain_cs_dag_t *a_dag, dap_chain_cs_dag_event_t *a_event, size_t a_event_size, char *a_event_hash_hex_str); -static void s_round_event_cs_done(dap_chain_cs_dag_poa_round_item_t *a_event_item); +static void s_round_event_cs_done(dap_chain_cs_dag_poa_round_item_t *a_event_item, uint32_t a_timeout_s); // CLI commands static int s_cli_dag_poa(int argc, char ** argv, void **a_str_reply); @@ -291,7 +291,7 @@ static int s_cli_dag_poa(int argc, char ** argv, void **a_str_reply) .datum_hash = l_round_item->round_info.datum_hash, .dag = l_dag }; - s_round_event_cs_done(&l_event_item); + s_round_event_cs_done(&l_event_item, l_poa_pvt->confirmations_timeout); } } else { if(!dap_strcmp(l_hash_out_type, "hex")) { @@ -392,6 +392,10 @@ static int s_callback_new(dap_chain_t * a_chain, dap_config_t * a_chain_cfg) } } } + if (!l_poa_pvt->auth_certs_count) { + log_it(L_ERROR, "Can't find any cert in \"dag_poa\" consensus"); + return -1; + } log_it(L_NOTICE,"Initialized DAG-PoA consensus with %u/%u minimum consensus",l_poa_pvt->auth_certs_count,l_poa_pvt->auth_certs_count_verify); l_dag->chain->callback_created = s_callback_created; @@ -604,8 +608,7 @@ static bool s_callback_round_event_to_chain_callback_get_round_item(dap_global_d } } } else /* !l_chosen_item */ - log_it(L_WARNING, "No valid candidates to wrap datum %s in current round #%"DAP_UINT64_FORMAT_U"", - l_datum_hash_str, l_dag->round_id); + log_it(L_WARNING, "No valid candidates to wrap datum %s in current round", l_datum_hash_str); return DAP_DELETE(l_arg), true; } @@ -620,7 +623,7 @@ static bool s_callback_round_event_to_chain(dap_chain_cs_dag_poa_round_item_t *a false; } -static void s_round_event_cs_done(dap_chain_cs_dag_poa_round_item_t *a_event_item) +static void s_round_event_cs_done(dap_chain_cs_dag_poa_round_item_t *a_event_item, uint32_t a_timeout_s) { dap_chain_cs_dag_poa_pvt_t *l_poa_pvt = PVT( DAP_CHAIN_CS_DAG_POA(a_event_item->dag) ); dap_chain_cs_dag_poa_round_item_t *l_event_item = NULL; @@ -628,11 +631,11 @@ static void s_round_event_cs_done(dap_chain_cs_dag_poa_round_item_t *a_event_ite HASH_FIND(hh, l_poa_pvt->event_items, &a_event_item->datum_hash, sizeof(dap_hash_fast_t), l_event_item); if (!l_event_item) { l_event_item = DAP_DUP(a_event_item); - if ( !dap_timerfd_start(l_poa_pvt->confirmations_timeout * 1000, (dap_timerfd_callback_t)s_callback_round_event_to_chain, l_event_item) ) + if ( !dap_timerfd_start(a_timeout_s * 1000, (dap_timerfd_callback_t)s_callback_round_event_to_chain, l_event_item) ) return DAP_DELETE(l_event_item), pthread_rwlock_unlock(&l_poa_pvt->rounds_rwlock), log_it(L_CRITICAL, "Timer creation failed"); HASH_ADD(hh, l_poa_pvt->event_items, datum_hash, sizeof(dap_hash_fast_t), l_event_item); log_it(L_INFO, "Confirmation timer for datum %s started [%d s]", - dap_chain_hash_fast_to_str_static(&l_event_item->datum_hash), l_poa_pvt->confirmations_timeout); + dap_chain_hash_fast_to_str_static(&l_event_item->datum_hash), a_timeout_s); } pthread_rwlock_unlock(&l_poa_pvt->rounds_rwlock); } @@ -643,7 +646,7 @@ static bool s_callback_sync_all_on_start(dap_global_db_instance_t *a_dbi, int a_ { for (size_t i = 0; i < a_values_count; i++) s_callback_event_round_sync((dap_chain_cs_dag_t *)a_arg, DAP_GLOBAL_DB_OPTYPE_ADD, a_group, - a_values[i].key, a_values[i].value, a_values[i].value_len); + a_values[i].key, a_values[i].value, a_values[i].value_len, true); return false; } @@ -659,7 +662,8 @@ static void s_round_changes_notify(dap_store_obj_t *a_obj, void *a_arg) return; switch ( l_type ) { case DAP_GLOBAL_DB_OPTYPE_ADD: - s_callback_event_round_sync(l_dag, l_type, a_obj->group, a_obj->key, a_obj->value, a_obj->value_len); + s_callback_event_round_sync(l_dag, l_type, a_obj->group, a_obj->key, a_obj->value, a_obj->value_len, + dap_stream_node_addr_from_sign(a_obj->sign).uint64 == g_node_addr.uint64); default: break; } @@ -699,7 +703,6 @@ static int s_callback_created(dap_chain_t * a_chain, dap_config_t *a_chain_net_c dap_global_db_cluster_add_notify_callback(l_dag_cluster, s_round_changes_notify, l_dag); dap_chain_net_add_auth_nodes_to_cluster(l_net, l_dag_cluster); dap_link_manager_add_net_associate(l_net->pub.id.uint64, l_dag_cluster->links_cluster); - l_dag->round_id = 0; PVT(l_poa)->mempool_timer = dap_interval_timer_create(15000, s_timer_process_callback, a_chain); switch ( dap_chain_net_get_role(l_net).enums ) { @@ -762,11 +765,11 @@ static dap_chain_cs_dag_event_t * s_callback_event_create(dap_chain_cs_dag_t * a log_it(L_ERROR, "Can't sign event with events_sign_cert in [dag-poa] section"); else if ( s_seed_mode || (a_hashes && a_hashes_count) ) { if ( !PVT(l_poa)->callback_pre_sign || !PVT(l_poa)->callback_pre_sign->callback) { - return dap_chain_cs_dag_event_new(a_dag->chain->id, a_dag->chain->cells->id, a_dag->round_id, - a_datum, PVT(l_poa)->events_sign_cert->enc_key, - a_hashes, a_hashes_count, a_event_size); + return dap_chain_cs_dag_event_new(a_dag->chain->id, a_dag->chain->cells->id, a_datum, + PVT(l_poa)->events_sign_cert->enc_key, + a_hashes, a_hashes_count, a_event_size); } else { - dap_chain_cs_dag_event_t *l_event = dap_chain_cs_dag_event_new(a_dag->chain->id, a_dag->chain->cells->id, a_dag->round_id, + dap_chain_cs_dag_event_t *l_event = dap_chain_cs_dag_event_new(a_dag->chain->id, a_dag->chain->cells->id, a_datum, NULL, a_hashes, a_hashes_count, a_event_size); if ( PVT(l_poa)->callback_pre_sign->callback(a_dag->chain, l_event, *a_event_size, PVT(l_poa)->callback_pre_sign->arg) ) return DAP_DELETE(l_event), NULL; @@ -778,7 +781,7 @@ static dap_chain_cs_dag_event_t * s_callback_event_create(dap_chain_cs_dag_t * a } static int s_callback_event_round_sync(dap_chain_cs_dag_t * a_dag, const char a_op_code, const char *a_group, - const char *a_key, const void *a_value, const size_t a_value_size) + const char *a_key, const void *a_value, const size_t a_value_size, bool a_by_us) { dap_return_val_if_pass(a_op_code != DAP_GLOBAL_DB_OPTYPE_ADD || !a_key || !a_value || !a_value_size || !strcmp(DAG_ROUND_CURRENT_KEY, a_key), 0); @@ -792,39 +795,35 @@ static int s_callback_event_round_sync(dap_chain_cs_dag_t * a_dag, const char a_ if (!l_poa_pvt->auto_confirmation) return 0; - dap_chain_cs_dag_event_round_item_t *l_round_item = (dap_chain_cs_dag_event_round_item_t *)a_value; + dap_chain_cs_dag_event_round_item_t *l_round_item = (dap_chain_cs_dag_event_round_item_t*)a_value; + dap_chain_cs_dag_event_t *l_event = (dap_chain_cs_dag_event_t*)l_round_item->event_n_signs; size_t l_event_size = l_round_item->event_size; - dap_chain_cs_dag_event_t *l_event = (dap_chain_cs_dag_event_t*)DAP_DUP_SIZE(l_round_item->event_n_signs, l_event_size); - if (!l_event) - return log_it(L_CRITICAL, "Memory allocation failed"), -1; - - if ( dap_chain_cs_dag_event_sign_exists(l_event, l_event_size, l_poa_pvt->events_sign_cert->enc_key) - || dap_chain_cs_dag_event_round_sign_exists(l_round_item, l_poa_pvt->events_sign_cert->enc_key) - && l_poa_pvt->auto_round_complete - && s_round_event_ready_minimum_check(a_dag, l_event, l_event_size, (char*)a_key) ) - { - dap_chain_cs_dag_poa_round_item_t l_event_item = { .datum_hash = l_round_item->round_info.datum_hash, .dag = a_dag }; - return DAP_DELETE(l_event), s_round_event_cs_done(&l_event_item), 0; - } - - size_t l_event_size_new = 0; - int ret = 0; - if ( !l_poa_pvt->callback_pre_sign || !l_poa_pvt->callback_pre_sign->callback - || !(ret = l_poa_pvt->callback_pre_sign->callback(a_dag->chain, l_event, l_event_size, - l_poa_pvt->callback_pre_sign->arg)) ) - { - l_event_size_new = dap_chain_cs_dag_event_sign_add(&l_event, l_event_size, l_poa_pvt->events_sign_cert->enc_key); - dap_chain_cs_dag_event_gdb_set(a_dag, (char*)a_key, l_event, l_event_size_new, l_round_item); - } else { // set sign for reject - l_round_item = DAP_DUP_SIZE(a_value, a_value_size); - if (dap_chain_cs_dag_event_round_sign_add(&l_round_item, a_value_size, l_poa_pvt->events_sign_cert->enc_key)) { - log_it(L_NOTICE,"Can't sign event %s, because sign rejected by pre_sign callback, ret code %d", a_key, ret); - l_round_item->round_info.reject_count++; - dap_chain_cs_dag_event_gdb_set(a_dag, (char*)a_key, l_event, l_event_size, l_round_item); + int l_ret = 0; + if ( dap_chain_cs_dag_event_sign_exists(l_event, l_event_size, l_poa_pvt->events_sign_cert->enc_key) ) { + if (l_poa_pvt->auto_round_complete && s_round_event_ready_minimum_check(a_dag, l_event, l_event_size, (char*)a_key) ) { + dap_chain_cs_dag_poa_round_item_t l_event_item = { .datum_hash = l_round_item->round_info.datum_hash, .dag = a_dag }; + return s_round_event_cs_done(&l_event_item, a_by_us ? l_poa_pvt->confirmations_timeout : 2*l_poa_pvt->confirmations_timeout), l_ret; + } + } else { + if ( !l_poa_pvt->callback_pre_sign + || !l_poa_pvt->callback_pre_sign->callback + || !(l_ret = l_poa_pvt->callback_pre_sign->callback(a_dag->chain, l_event, l_event_size, l_poa_pvt->callback_pre_sign->arg)) + ) { + l_event = DAP_DUP_SIZE(l_round_item->event_n_signs, l_event_size); + if (( l_event_size = dap_chain_cs_dag_event_sign_add(&l_event, l_event_size, l_poa_pvt->events_sign_cert->enc_key) )) + dap_chain_cs_dag_event_gdb_set(a_dag, (char*)a_key, l_event, l_event_size, l_round_item); + DAP_DELETE(l_event); + } else { + l_round_item = DAP_DUP_SIZE(a_value, a_value_size); + if ( dap_chain_cs_dag_event_round_sign_add(&l_round_item, a_value_size, l_poa_pvt->events_sign_cert->enc_key) ) { + log_it(L_NOTICE,"Can't sign event %s, because sign rejected by pre_sign callback, ret code %d", a_key, l_ret); + ++l_round_item->round_info.reject_count; + dap_chain_cs_dag_event_gdb_set(a_dag, (char*)a_key, l_event, l_event_size, l_round_item); + } + DAP_DELETE(l_round_item); } - DAP_DELETE(l_round_item); } - return DAP_DELETE(l_event), ret; + return l_ret; } /** diff --git a/modules/consensus/esbocs/dap_chain_cs_esbocs.c b/modules/consensus/esbocs/dap_chain_cs_esbocs.c index 75e8f9c05ef405b23d2c9254fc0e7dabaf83d648..cd30474fbdd7ad68a12b9e18e0ab5c6e2a7cb599 100644 --- a/modules/consensus/esbocs/dap_chain_cs_esbocs.c +++ b/modules/consensus/esbocs/dap_chain_cs_esbocs.c @@ -207,8 +207,13 @@ 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); +//patch for tests +#ifdef DAP_LEDGER_TEST + return 0; +#endif + dap_chain_cs_blocks_t *l_blocks = DAP_CHAIN_CS_BLOCKS(a_chain); - int l_ret = 0; + int l_ret = 0, l_inited_cert; dap_chain_esbocs_t *l_esbocs = NULL; DAP_NEW_Z_RET_VAL(l_esbocs, dap_chain_esbocs_t, -5, NULL); @@ -232,7 +237,6 @@ static int s_callback_new(dap_chain_t *a_chain, dap_config_t *a_chain_cfg) l_esbocs_pvt->new_round_delay = dap_config_get_item_uint16_default(a_chain_cfg, DAP_CHAIN_ESBOCS_CS_TYPE_STR, "new_round_delay", 10); l_esbocs_pvt->round_attempts_max = dap_config_get_item_uint16_default(a_chain_cfg, DAP_CHAIN_ESBOCS_CS_TYPE_STR, "round_attempts_max", 4); l_esbocs_pvt->round_attempt_timeout = dap_config_get_item_uint16_default(a_chain_cfg, DAP_CHAIN_ESBOCS_CS_TYPE_STR, "round_attempt_timeout", 10); - l_esbocs_pvt->start_validators_min = l_esbocs_pvt->min_validators_count = dap_config_get_item_uint16(a_chain_cfg, DAP_CHAIN_ESBOCS_CS_TYPE_STR, "min_validators_count"); if (!l_esbocs_pvt->min_validators_count) { @@ -241,6 +245,10 @@ static int s_callback_new(dap_chain_t *a_chain, dap_config_t *a_chain_cfg) } const char *l_auth_certs_prefix = dap_config_get_item_str(a_chain_cfg, DAP_CHAIN_ESBOCS_CS_TYPE_STR, "auth_certs_prefix"); + if (!l_auth_certs_prefix) { + l_ret = -6; + goto lb_err; + } uint16_t l_node_addrs_count; const char **l_addrs = dap_config_get_array_str(a_chain_cfg, DAP_CHAIN_ESBOCS_CS_TYPE_STR, "validators_addrs", &l_node_addrs_count); if (l_node_addrs_count < l_esbocs_pvt->min_validators_count) { @@ -261,11 +269,13 @@ static int s_callback_new(dap_chain_t *a_chain, dap_config_t *a_chain_cfg) log_it(L_ERROR, "Can't find cert \"%s\"", l_cert_name); else log_it(L_ERROR, "Can't find cert \"%s\" possibly for address \"%s\"", l_cert_name, l_addrs[i]); - continue; + l_ret = -6; + goto lb_err; } } dap_chain_addr_t l_signing_addr; log_it(L_NOTICE, "Initialized auth cert \"%s\"", l_cert_name); + l_inited_cert++; dap_chain_addr_fill_from_key(&l_signing_addr, l_cert_cur->enc_key, a_chain->net_id); l_esbocs_pvt->emergency_validator_addrs = dap_list_append(l_esbocs_pvt->emergency_validator_addrs, @@ -298,6 +308,10 @@ static int s_callback_new(dap_chain_t *a_chain, dap_config_t *a_chain_cfg) l_weight, &l_signer_node_addr); } } + if (!l_inited_cert) { + l_ret = -1; + goto lb_err; + } // Preset reward for block signs, before first reward decree const char *l_preset_reward_str = dap_config_get_item_str(a_chain_cfg, DAP_CHAIN_ESBOCS_CS_TYPE_STR, "preset_reward"); if (l_preset_reward_str) { diff --git a/modules/net/dap_chain_ledger.c b/modules/net/dap_chain_ledger.c index f20e687aafcc675b94b2f1408712c1c887d8649e..af0c71dcfaa156bc0d662b0b2f729584ec39d343 100644 --- a/modules/net/dap_chain_ledger.c +++ b/modules/net/dap_chain_ledger.c @@ -130,6 +130,7 @@ typedef struct dap_ledger_token_item { time_t last_update_token_time; // for auth operations + dap_pkey_t ** auth_pkeys; dap_chain_hash_fast_t *auth_pkey_hashes; size_t auth_signs_total; @@ -1617,7 +1618,8 @@ int dap_ledger_token_add(dap_ledger_t *a_ledger, byte_t *a_token, size_t a_token .token_emissions_rwlock = PTHREAD_RWLOCK_INITIALIZER, .token_ts_updated_rwlock = PTHREAD_RWLOCK_INITIALIZER, .auth_pkeys = DAP_NEW_Z_SIZE(dap_pkey_t*, sizeof(dap_pkey_t*) * l_token->signs_total), - .auth_pkey_hashes = DAP_NEW_Z_SIZE(dap_chain_hash_fast_t, sizeof(dap_chain_hash_fast_t) * l_token->signs_total) + .auth_pkey_hashes = DAP_NEW_Z_SIZE(dap_chain_hash_fast_t, sizeof(dap_chain_hash_fast_t) * l_token->signs_total), + .flags = 0 }; switch (l_token->subtype) { case DAP_CHAIN_DATUM_TOKEN_SUBTYPE_PRIVATE: @@ -2145,7 +2147,39 @@ json_object *s_token_item_to_json(dap_ledger_token_item_t *a_token_item) json_object_object_add(l_json_obj_out, "bytes", json_object_new_int(a_token_item->auth_pkeys[i]->header.size)); json_object_array_add(l_json_arr_pkeys, l_json_obj_out); } + json_object *l_json_arr_tx_recv_allow = json_object_new_array(); + for (size_t i = 0; i < a_token_item->tx_recv_allow_size; i++) { + dap_chain_addr_t l_addr = a_token_item->tx_recv_allow[i]; + const char *l_addr_str = dap_chain_addr_to_str_static(&l_addr); + json_object_array_add(l_json_arr_tx_recv_allow, json_object_new_string(l_addr_str)); + } + json_object *l_json_arr_tx_recv_block = json_object_new_array(); + for (size_t i = 0; i < a_token_item->tx_recv_block_size; i++) { + dap_chain_addr_t l_addr = a_token_item->tx_recv_block[i]; + const char *l_addr_str = dap_chain_addr_to_str_static(&l_addr); + json_object_array_add(l_json_arr_tx_recv_block, json_object_new_string(l_addr_str)); + } + json_object *l_json_arr_tx_send_allow = json_object_new_array(); + for (size_t i = 0; i < a_token_item->tx_send_allow_size; i++) { + dap_chain_addr_t l_addr = a_token_item->tx_send_allow[i]; + const char *l_addr_str = dap_chain_addr_to_str_static(&l_addr); + json_object_array_add(l_json_arr_tx_send_allow, json_object_new_string(l_addr_str)); + } + json_object *l_json_arr_tx_send_block = json_object_new_array(); + for (size_t i = 0; i < a_token_item->tx_send_block_size; i++) { + dap_chain_addr_t l_addr = a_token_item->tx_send_block[i]; + const char *l_addr_str = dap_chain_addr_to_str_static(&l_addr); + json_object_array_add(l_json_arr_tx_send_block, json_object_new_string(l_addr_str)); + } json_object_object_add(json_obj_datum, "Signatures public keys", l_json_arr_pkeys); + a_token_item->tx_recv_allow_size ? json_object_object_add(json_obj_datum, "tx_recv_allow", l_json_arr_tx_recv_allow) : + json_object_put(l_json_arr_tx_recv_allow); + a_token_item->tx_recv_block_size ? json_object_object_add(json_obj_datum, "tx_recv_block", l_json_arr_tx_recv_block) : + json_object_put(l_json_arr_tx_recv_block); + a_token_item->tx_send_allow_size ? json_object_object_add(json_obj_datum, "tx_send_allow", l_json_arr_tx_send_allow) : + json_object_put(l_json_arr_tx_send_allow); + a_token_item->tx_send_block_size ? json_object_object_add(json_obj_datum, "tx_send_block", l_json_arr_tx_send_block) : + json_object_put(l_json_arr_tx_send_block); json_object_object_add(json_obj_datum, "Total emissions", json_object_new_int(HASH_COUNT(a_token_item->token_emissions))); return json_obj_datum; } @@ -2240,7 +2274,7 @@ static void s_threshold_txs_proc( dap_ledger_t *a_ledger) l_success = false; dap_ledger_tx_item_t *l_tx_item, *l_tx_tmp; HASH_ITER(hh, l_ledger_pvt->threshold_txs, l_tx_item, l_tx_tmp) { - int l_res = dap_ledger_tx_add(a_ledger, l_tx_item->tx, &l_tx_item->tx_hash_fast, true); + int l_res = dap_ledger_tx_add(a_ledger, l_tx_item->tx, &l_tx_item->tx_hash_fast, true, NULL); if (l_res != DAP_CHAIN_CS_VERIFY_CODE_TX_NO_EMISSION && l_res != DAP_CHAIN_CS_VERIFY_CODE_TX_NO_PREVIOUS) { HASH_DEL(l_ledger_pvt->threshold_txs, l_tx_item); @@ -2697,7 +2731,8 @@ int s_emission_add_check(dap_ledger_t *a_ledger, byte_t *a_token_emission, size_ pthread_rwlock_unlock(&l_token_item->token_emissions_rwlock); if (l_token_emission_item) { debug_if(s_debug_more, L_WARNING, "Can't add token emission datum of %s %s ( %s ): already present in cache", - dap_uint256_to_char(l_emission->hdr.value, NULL), l_emission->hdr.ticker, dap_chain_hash_fast_to_str_static(a_emission_hash)); + dap_uint256_to_char(l_emission->hdr.value, NULL), l_emission->hdr.ticker, + dap_chain_hash_fast_to_str_static(a_emission_hash)); DAP_DELETE(l_emission); return DAP_LEDGER_CHECK_ALREADY_CACHED; } @@ -4182,7 +4217,7 @@ static int s_tx_cache_check(dap_ledger_t *a_ledger, } if (a_main_ticker && !l_err_num) - dap_strncpy(a_main_ticker, l_main_ticker, DAP_CHAIN_TICKER_SIZE_MAX); + dap_strncpy(a_main_ticker, l_main_ticker, DAP_CHAIN_TICKER_SIZE_MAX); HASH_ITER(hh, l_values_from_prev_tx, l_value_cur, l_tmp) { HASH_DEL(l_values_from_prev_tx, l_value_cur); @@ -4276,7 +4311,7 @@ static int s_sort_ledger_tx_item(dap_ledger_tx_item_t *a, dap_ledger_tx_item_t * * @param a_from_threshold * @return return 1 OK, -1 error */ -int dap_ledger_tx_add(dap_ledger_t *a_ledger, dap_chain_datum_tx_t *a_tx, dap_hash_fast_t *a_tx_hash, bool a_from_threshold) +int dap_ledger_tx_add(dap_ledger_t *a_ledger, dap_chain_datum_tx_t *a_tx, dap_hash_fast_t *a_tx_hash, bool a_from_threshold, dap_ledger_datum_iter_data_t *a_datum_index_data) { if(!a_tx) { debug_if(s_debug_more, L_ERROR, "NULL tx detected"); @@ -4299,7 +4334,7 @@ int dap_ledger_tx_add(dap_ledger_t *a_ledger, dap_chain_datum_tx_t *a_tx, dap_ha if( (l_ret_check = s_tx_cache_check(a_ledger, a_tx, a_tx_hash, a_from_threshold, &l_list_bound_items, &l_list_tx_out, - l_main_token_ticker, &l_tag, &l_action, false))) { + l_main_token_ticker, &l_tag, &l_action, false))) { if ((l_ret_check == DAP_CHAIN_CS_VERIFY_CODE_TX_NO_PREVIOUS || l_ret_check == DAP_CHAIN_CS_VERIFY_CODE_TX_NO_EMISSION) && l_ledger_pvt->threshold_enabled && !dap_chain_net_get_load_mode(a_ledger->net)) { @@ -4312,7 +4347,7 @@ int dap_ledger_tx_add(dap_ledger_t *a_ledger, dap_chain_datum_tx_t *a_tx, dap_ha if (!l_item_tmp) { if (l_threshold_txs_count >= s_threshold_txs_max) { if(s_debug_more) - log_it(L_WARNING, "Threshold for tranactions is overfulled (%zu max), dropping down tx %s, added nothing", + log_it(L_WARNING, "Threshold for transactions is overfulled (%zu max), dropping down tx %s, added nothing", s_threshold_txs_max, l_tx_hash_str); } else { l_item_tmp = DAP_NEW_Z(dap_ledger_tx_item_t); @@ -4347,7 +4382,11 @@ int dap_ledger_tx_add(dap_ledger_t *a_ledger, dap_chain_datum_tx_t *a_tx, dap_ha return l_ret_check; } debug_if(s_debug_more, L_DEBUG, "dap_ledger_tx_add() check passed for tx %s", l_tx_hash_str); - + if ( a_datum_index_data != NULL){ + dap_strncpy(a_datum_index_data->token_ticker, l_main_token_ticker, DAP_CHAIN_TICKER_SIZE_MAX); + a_datum_index_data->action = l_action; + a_datum_index_data->uid = l_tag; + } // Mark 'out' items in cache if they were used & delete previous transactions from cache if it need // find all bound pairs 'in' and 'out' size_t l_outs_used = dap_list_length(l_list_bound_items); @@ -4951,7 +4990,7 @@ FIN: return l_ret; } -int dap_ledger_tx_load(dap_ledger_t *a_ledger, dap_chain_datum_tx_t *a_tx, dap_chain_hash_fast_t *a_tx_hash) +int dap_ledger_tx_load(dap_ledger_t *a_ledger, dap_chain_datum_tx_t *a_tx, dap_chain_hash_fast_t *a_tx_hash, dap_ledger_datum_iter_data_t *a_datum_index_data) { #ifndef DAP_LEDGER_TEST if (dap_chain_net_get_load_mode(a_ledger->net)) { @@ -4967,7 +5006,7 @@ int dap_ledger_tx_load(dap_ledger_t *a_ledger, dap_chain_datum_tx_t *a_tx, dap_c return DAP_LEDGER_CHECK_ALREADY_CACHED; } #endif - return dap_ledger_tx_add(a_ledger, a_tx, a_tx_hash, false); + return dap_ledger_tx_add(a_ledger, a_tx, a_tx_hash, false, a_datum_index_data); } /** diff --git a/modules/net/dap_chain_net.c b/modules/net/dap_chain_net.c index 50b19e6bcc8f1c8c8226e6ec4d509baf57410b5c..a0954b93f7e0d3a56be11b1210f16527fd640150 100644 --- a/modules/net/dap_chain_net.c +++ b/modules/net/dap_chain_net.c @@ -387,8 +387,10 @@ dap_stream_node_addr_t *dap_chain_net_get_authorized_nodes(dap_chain_net_t *a_ne int dap_chain_net_link_add(dap_chain_net_t *a_net, dap_stream_node_addr_t *a_addr, const char *a_host, uint16_t a_port) { bool l_is_link_present = dap_link_manager_link_find(a_addr, a_net->pub.id.uint64); - if (l_is_link_present || a_addr->uint64 == g_node_addr.uint64) + if (l_is_link_present || a_addr->uint64 == g_node_addr.uint64) { + debug_if(l_is_link_present, L_DEBUG, "Link to addr "NODE_ADDR_FP_STR" is already persent in net %s", NODE_ADDR_FP_ARGS(a_addr), a_net->pub.name); return -3; // Link is already found for this net or link is to yourself + } if (dap_link_manager_link_create(a_addr, a_net->pub.id.uint64)) { log_it(L_ERROR, "Can't create link to addr " NODE_ADDR_FP_STR, NODE_ADDR_FP_ARGS(a_addr)); return -1; @@ -462,8 +464,10 @@ static bool s_link_manager_callback_disconnected(dap_link_t *a_link, uint64_t a_ log_it(L_INFO, "%s."NODE_ADDR_FP_STR" can't connect for now. %s", l_net ? l_net->pub.name : "(unknown)" , NODE_ADDR_FP_ARGS_S(a_link->addr), l_link_is_permanent ? "Setting reconnection pause for it." : "Dropping it."); - if (!a_links_count && l_net_pvt->state == NET_STATE_ONLINE) + if (!a_links_count && l_net_pvt->state != NET_STATE_OFFLINE) { l_net_pvt->state = NET_STATE_LINKS_PREPARE; + s_net_states_proc(l_net); + } return l_link_is_permanent; } @@ -703,6 +707,28 @@ static dap_chain_net_t *s_net_new(const char *a_net_name, dap_config_t *a_cfg) return l_ret; } +bool s_net_disk_load_notify_callback(UNUSED_ARG void *a_arg) { + json_object *json_obj = json_object_new_object(); + json_object_object_add(json_obj, "class", json_object_new_string("nets_init")); + json_object *l_jobj_nets = json_object_new_object(); + for (dap_chain_net_t *net = s_nets_by_name; net; net = net->hh.next) { + json_object *json_chains = json_object_new_object(); + for (dap_chain_t *l_chain = net->pub.chains; l_chain; l_chain = l_chain->next) { + json_object *l_jobj_chain_info = json_object_new_object(); + json_object_object_add(l_jobj_chain_info, "count_atoms", json_object_new_int(l_chain->callback_count_atom(l_chain))); + json_object_object_add(l_jobj_chain_info, "load_process", json_object_new_int(l_chain->load_progress)); + json_object_object_add(json_chains, l_chain->name, l_jobj_chain_info); + log_it(L_DEBUG, "Loading net \"%s\", chain \"%s\", ID 0x%016"DAP_UINT64_FORMAT_x " [%d%%]", + net->pub.name, l_chain->name, l_chain->id.uint64, l_chain->load_progress); + } + json_object_object_add(l_jobj_nets, net->pub.name, json_chains); + } + json_object_object_add(json_obj, "nets", l_jobj_nets); + dap_notify_server_send_mt(json_object_get_string(json_obj)); + json_object_put(json_obj); + return true; +} + /** * @brief * load network config settings @@ -716,23 +742,24 @@ void dap_chain_net_load_all() pthread_mutex_unlock(&s_net_cond_lock); return; } + dap_timerfd_t *l_load_notify_timer = dap_timerfd_start(5000, (dap_timerfd_callback_t)s_net_disk_load_notify_callback, NULL); for (dap_chain_net_t *net = s_nets_by_name; net; net = net->hh.next) dap_proc_thread_callback_add(NULL, s_net_load, net); while (s_net_loading_count) pthread_cond_wait(&s_net_cond, &s_net_cond_lock); pthread_mutex_unlock(&s_net_cond_lock); + dap_timerfd_delete_mt(l_load_notify_timer->worker, l_load_notify_timer->esocket_uuid); } dap_string_t* dap_cli_list_net() { dap_string_t *l_string_ret = dap_string_new(""); - dap_chain_net_t * l_net = NULL; unsigned l_net_i = 0; dap_string_append(l_string_ret, "Available networks and chains:\n"); for (dap_chain_net_t *net = s_nets_by_name; net; net = net->hh.next) { - dap_string_append_printf(l_string_ret, "\t%s:\n", l_net->pub.name); + dap_string_append_printf(l_string_ret, "\t%s:\n", net->pub.name); ++l_net_i; - dap_chain_t *l_chain = l_net->pub.chains; + dap_chain_t *l_chain = net->pub.chains; while (l_chain) { dap_string_append_printf( l_string_ret, "\t\t%s\n", l_chain->name ); l_chain = l_chain->next; @@ -1739,8 +1766,8 @@ void dap_chain_net_delete(dap_chain_net_t *a_net) #ifdef DAP_LEDGER_TEST int dap_chain_net_test_init() { - dap_chain_net_t *l_net = DAP_NEW_Z_SIZE( sizeof(dap_chain_net_t) + sizeof(dap_chain_net_pvt_t) ); - PVT(l_net)->node_info = DAP_NEW_Z_SIZE( sizeof(dap_chain_node_info_t) + DAP_HOSTADDR_STRLEN + 1 ); + dap_chain_net_t *l_net = DAP_NEW_Z_SIZE( dap_chain_net_t, sizeof(dap_chain_net_t) + sizeof(dap_chain_net_pvt_t) ); + PVT(l_net)->node_info = DAP_NEW_Z_SIZE(dap_chain_net_t, sizeof(dap_chain_node_info_t) + DAP_HOSTADDR_STRLEN + 1 ); l_net->pub.id.uint64 = 0xFA0; strcpy(l_net->pub.name, "Snet"); l_net->pub.gdb_groups_prefix = (const char*)l_net->pub.name; @@ -2748,7 +2775,7 @@ dap_list_t* dap_chain_datum_list(dap_chain_net_t *a_net, dap_chain_t *a_chain, d * @param a_datum_size * @return */ -int dap_chain_datum_add(dap_chain_t *a_chain, dap_chain_datum_t *a_datum, size_t a_datum_size, dap_hash_fast_t *a_datum_hash) +int dap_chain_datum_add(dap_chain_t *a_chain, dap_chain_datum_t *a_datum, size_t a_datum_size, dap_hash_fast_t *a_datum_hash, void *a_datum_index_data) { size_t l_datum_data_size = a_datum->header.data_size; if (a_datum_size < l_datum_data_size + sizeof(a_datum->header)) { @@ -2790,8 +2817,8 @@ int dap_chain_datum_add(dap_chain_t *a_chain, dap_chain_datum_t *a_datum, size_t if (l_tx_size != l_datum_data_size) { log_it(L_WARNING, "Corrupted transaction, datum size %zd is not equal to size of TX %zd", l_datum_data_size, l_tx_size); return -102; - } - return dap_ledger_tx_load(l_ledger, l_tx, a_datum_hash); + } + return dap_ledger_tx_load(l_ledger, l_tx, a_datum_hash, (dap_ledger_datum_iter_data_t*)a_datum_index_data); } case DAP_CHAIN_DATUM_CA: return dap_cert_chain_file_save(a_datum, a_chain->net_name); @@ -2911,11 +2938,12 @@ void dap_chain_net_announce_addr(dap_chain_net_t *a_net) dap_return_if_fail(a_net); dap_chain_net_pvt_t *l_net_pvt = PVT(a_net); if ( l_net_pvt->node_info->ext_port ) { - dap_chain_net_node_list_request(a_net, l_net_pvt->node_info->ext_port, false, 'a'); log_it(L_INFO, "Announce our node address "NODE_ADDR_FP_STR" [ %s : %u ] in net %s", NODE_ADDR_FP_ARGS_S(g_node_addr), l_net_pvt->node_info->ext_host, l_net_pvt->node_info->ext_port, a_net->pub.name); + dap_chain_net_node_list_request(a_net, l_net_pvt->node_info->ext_port, true, 'a'); + } } @@ -3026,9 +3054,10 @@ static void s_ch_in_pkt_callback(dap_stream_ch_t *a_ch, uint8_t a_type, const vo if (!dap_hash_fast_compare(&l_miss_info->missed_hash, &l_net_pvt->sync_context.requested_atom_hash)) { char l_missed_hash_str[DAP_HASH_FAST_STR_SIZE]; dap_hash_fast_to_str(&l_miss_info->missed_hash, l_missed_hash_str, DAP_HASH_FAST_STR_SIZE); - log_it(L_WARNING, "Get irrelevant chain sync MISSED packet with missed hash %s, but requested hash is %s", + log_it(L_WARNING, "Get irrelevant chain sync MISSED packet with missed hash %s, but requested hash is %s. Net %s chain %s", l_missed_hash_str, - dap_hash_fast_to_str_static(&l_net_pvt->sync_context.requested_atom_hash)); + dap_hash_fast_to_str_static(&l_net_pvt->sync_context.requested_atom_hash), + l_net->pub.name, l_net_pvt->sync_context.cur_chain->name); dap_stream_ch_write_error_unsafe(a_ch, l_net->pub.id, l_net_pvt->sync_context.cur_chain->id, l_net_pvt->sync_context.cur_cell @@ -3095,10 +3124,11 @@ static void s_ch_in_pkt_callback(dap_stream_ch_t *a_ch, uint8_t a_type, const vo static void s_ch_out_pkt_callback(dap_stream_ch_t *a_ch, uint8_t a_type, const void *a_data, size_t a_data_size, void *a_arg) { - debug_if(s_debug_more, L_DEBUG, "Sent OUT sync packet type %hhu size %zu to addr " NODE_ADDR_FP_STR, - a_type, a_data_size, NODE_ADDR_FP_ARGS_S(a_ch->stream->node)); + dap_chain_net_t *l_net = a_arg; dap_chain_net_pvt_t *l_net_pvt = PVT(l_net); + if (!l_net_pvt->sync_context.cur_chain) + return; switch (a_type) { case DAP_CHAIN_CH_PKT_TYPE_ERROR: l_net_pvt->sync_context.cur_chain->state = CHAIN_SYNC_STATE_ERROR; @@ -3107,6 +3137,8 @@ static void s_ch_out_pkt_callback(dap_stream_ch_t *a_ch, uint8_t a_type, const v break; } l_net_pvt->sync_context.stage_last_activity = dap_time_now(); + debug_if(s_debug_more, L_DEBUG, "Sent OUT sync packet type %hhu size %zu to addr " NODE_ADDR_FP_STR, + a_type, a_data_size, NODE_ADDR_FP_ARGS_S(a_ch->stream->node)); } @@ -3214,6 +3246,12 @@ static void s_sync_timer_callback(void *a_arg) l_net_pvt->sync_context.cur_chain->state = CHAIN_SYNC_STATE_SYNCED; return; } + // if sync more than 3 mins after online state, change state to SYNC + if (l_net_pvt->state == NET_STATE_ONLINE && l_net_pvt->sync_context.state == CHAIN_SYNC_STATE_WAITING && + dap_time_now() - l_net_pvt->sync_context.stage_last_activity > l_net_pvt->sync_context.sync_idle_time ) { + l_net_pvt->state = NET_STATE_SYNC_CHAINS; + s_net_states_proc(l_net); + } l_net_pvt->sync_context.cur_cell = l_net_pvt->sync_context.cur_chain->cells; l_net_pvt->sync_context.cur_chain->state = CHAIN_SYNC_STATE_WAITING; @@ -3366,4 +3404,4 @@ DAP_INLINE dap_chain_net_state_t dap_chain_net_get_target_state(dap_chain_net_t return PVT(a_net)->state_target; } -/*------------------------------------State machine block end---------------------------------*/ +/*------------------------------------State machine block end---------------------------------*/ \ No newline at end of file diff --git a/modules/net/dap_chain_net_balancer.c b/modules/net/dap_chain_net_balancer.c index bbd3dd7320599a509763cbe6ca94d132215b9c75..02dd26c1ad252ccc30f28083cc55c8505ee605c8 100644 --- a/modules/net/dap_chain_net_balancer.c +++ b/modules/net/dap_chain_net_balancer.c @@ -46,7 +46,6 @@ typedef struct dap_balancer_request_info { static_assert(sizeof(dap_chain_net_links_t) + sizeof(dap_chain_node_info_old_t) < DAP_BALANCER_MAX_REPLY_SIZE, "DAP_BALANCER_MAX_REPLY_SIZE cannot accommodate information minimum about 1 link"); static const size_t s_max_links_response_count = (DAP_BALANCER_MAX_REPLY_SIZE - sizeof(dap_chain_net_links_t)) / sizeof(dap_chain_node_info_old_t); -static const dap_time_t s_request_period = 5; // sec static dap_balancer_request_info_t* s_request_info_items = NULL; /** @@ -78,22 +77,30 @@ static dap_chain_net_links_t *s_get_ignored_node_addrs(dap_chain_net_t *a_net, s size_t l_size = 0, l_uplinks_count = 0, + l_downlinks_count = 0, + l_links_count = 0, l_low_availability_count = 0; const dap_stream_node_addr_t *l_curr_addr = &dap_chain_net_get_my_node_info(a_net)->address, - *l_uplinks = dap_link_manager_get_net_links_addrs(a_net->pub.id.uint64, &l_uplinks_count, NULL, true), + *l_links = dap_link_manager_get_net_links_addrs(a_net->pub.id.uint64, &l_uplinks_count, &l_downlinks_count, false), *l_low_availability = dap_link_manager_get_ignored_addrs(&l_low_availability_count, a_net->pub.id.uint64); - if(!l_curr_addr->uint64 && !l_uplinks && !l_low_availability) { + l_links_count = l_uplinks_count + l_downlinks_count; + if(!l_curr_addr->uint64 && !l_links && !l_low_availability) { log_it(L_WARNING, "Error forming ignore list in net %s, please check, should be minimum self addr", a_net->pub.name); return NULL; } + l_size = sizeof(dap_chain_net_links_t) + sizeof(dap_stream_node_addr_t) * (l_links_count + l_low_availability_count + 1); + // memory alloc + dap_chain_net_links_t *l_ret = NULL; + DAP_NEW_Z_SIZE_RET_VAL(l_ret, dap_chain_net_links_t, l_size, NULL, l_links, l_low_availability); + l_ret->count_node = l_links_count + l_low_availability_count + 1; if (dap_log_level_get() <= L_DEBUG ) { char *l_ignored_str = NULL; - DAP_NEW_Z_SIZE_RET_VAL(l_ignored_str, char, 50 * (l_uplinks_count + l_low_availability_count + 1) + 200 + strlen(a_net->pub.name), NULL, l_uplinks, l_low_availability); - sprintf(l_ignored_str + strlen(l_ignored_str), "Second %zu nodes will be ignored in balancer links preparing in net %s:\n\tSelf:\n\t\t"NODE_ADDR_FP_STR"\n", l_uplinks_count + l_low_availability_count + 1, a_net->pub.name, NODE_ADDR_FP_ARGS(l_curr_addr)); - sprintf(l_ignored_str + strlen(l_ignored_str), "\tUplinks (%zu):\n", l_uplinks_count); - for (size_t i = 0; i < l_uplinks_count; ++i) { - sprintf(l_ignored_str + strlen(l_ignored_str), "\t\t"NODE_ADDR_FP_STR"\n", NODE_ADDR_FP_ARGS(l_uplinks + i)); + DAP_NEW_Z_SIZE_RET_VAL(l_ignored_str, char, 50 * (l_ret->count_node) + 200 + strlen(a_net->pub.name), NULL, l_links, l_low_availability); + sprintf(l_ignored_str + strlen(l_ignored_str), "Second %zu nodes will be ignored in balancer links preparing in net %s:\n\tSelf:\n\t\t"NODE_ADDR_FP_STR"\n", l_ret->count_node, a_net->pub.name, NODE_ADDR_FP_ARGS(l_curr_addr)); + sprintf(l_ignored_str + strlen(l_ignored_str), "\tActive links (%zu):\n", l_links_count); + for (size_t i = 0; i < l_links_count; ++i) { + sprintf(l_ignored_str + strlen(l_ignored_str), "\t\t"NODE_ADDR_FP_STR"\n", NODE_ADDR_FP_ARGS(l_links + i)); } sprintf(l_ignored_str + strlen(l_ignored_str), "\tCooling (%zu):\n", l_low_availability_count); for (size_t i = 0; i < l_low_availability_count; ++i) { @@ -102,20 +109,15 @@ static dap_chain_net_links_t *s_get_ignored_node_addrs(dap_chain_net_t *a_net, s log_it(L_DEBUG, "%s", l_ignored_str); DAP_DELETE(l_ignored_str); } - l_size = sizeof(dap_chain_net_links_t) + sizeof(dap_stream_node_addr_t) * (l_uplinks_count + l_low_availability_count + 1); -// memory alloc - dap_chain_net_links_t *l_ret = NULL; - DAP_NEW_Z_SIZE_RET_VAL(l_ret, dap_chain_net_links_t, l_size, NULL, l_uplinks, l_low_availability); // func work memcpy(l_ret->nodes_info, l_curr_addr, sizeof(dap_stream_node_addr_t)); - if(l_uplinks) - memcpy(l_ret->nodes_info + sizeof(dap_stream_node_addr_t), l_uplinks, l_uplinks_count * sizeof(dap_stream_node_addr_t)); + if(l_links) + memcpy(l_ret->nodes_info + sizeof(dap_stream_node_addr_t), l_links, l_links_count * sizeof(dap_stream_node_addr_t)); if(l_low_availability) - memcpy(l_ret->nodes_info + (l_uplinks_count + 1) * sizeof(dap_stream_node_addr_t), l_low_availability, l_low_availability_count * sizeof(dap_stream_node_addr_t)); - l_ret->count_node = l_uplinks_count + l_low_availability_count + 1; + memcpy(l_ret->nodes_info + (l_links_count + 1) * sizeof(dap_stream_node_addr_t), l_low_availability, l_low_availability_count * sizeof(dap_stream_node_addr_t)); if (a_size) *a_size = l_size; - DAP_DEL_MULTY(l_uplinks, l_low_availability); + DAP_DEL_MULTY(l_links, l_low_availability); return l_ret; } diff --git a/modules/net/dap_chain_node.c b/modules/net/dap_chain_node.c index d530ede2b7aee567857a50e890a89168210073f2..48b64cbbe4be9dc8f9fe58d49e9c7997d949c87e 100644 --- a/modules/net/dap_chain_node.c +++ b/modules/net/dap_chain_node.c @@ -47,8 +47,12 @@ #include "dap_chain_ledger.h" #define LOG_TAG "dap_chain_node" +#define DAP_CHAIN_NODE_NET_STATES_INFO_CURRENT_VERSION 2 typedef struct dap_chain_node_net_states_info { + uint16_t version_info; + char version_node[16]; + dap_chain_node_role_t role; dap_chain_node_addr_t address; uint64_t events_count; uint64_t atoms_count; @@ -62,6 +66,8 @@ static const uint64_t s_cmp_delta_event = 0; static const uint64_t s_cmp_delta_atom = 10; static const uint64_t s_timer_update_states_info = 10 /*sec*/ * 1000; static const char s_states_group[] = ".nodes.states"; + // only add in version up, not change!!! +static const uint16_t s_states_version_size[DAP_CHAIN_NODE_NET_STATES_INFO_CURRENT_VERSION] = { 32, 54 }; /** * @brief get states info about current @@ -69,6 +75,10 @@ static const char s_states_group[] = ".nodes.states"; */ static void s_update_node_states_info(UNUSED_ARG void *a_arg) { +#ifndef DAP_VERSION +#pragma message "[!WRN!] DAP_VERSION IS NOT DEFINED. Manual override engaged." +#define DAP_VERSION "0.9-15" +#endif for (dap_chain_net_t *l_net = dap_chain_net_iter_start(); l_net; l_net = dap_chain_net_iter_next(l_net)) { if(dap_chain_net_get_state(l_net) != NET_STATE_OFFLINE) { size_t @@ -76,19 +86,22 @@ static void s_update_node_states_info(UNUSED_ARG void *a_arg) l_downlinks_count = 0, l_info_size = 0; // memory alloc first - dap_stream_node_addr_t *l_linked_node_addrs = dap_link_manager_get_net_links_addrs(l_net->pub.id.uint64, &l_uplinks_count, &l_downlinks_count, false); + dap_stream_node_addr_t *l_linked_node_addrs = dap_link_manager_get_net_links_addrs(l_net->pub.id.uint64, &l_uplinks_count, &l_downlinks_count, true); dap_chain_node_net_states_info_t *l_info = NULL; l_info_size = sizeof(dap_chain_node_net_states_info_t) + (l_uplinks_count + l_downlinks_count) * sizeof(dap_chain_node_addr_t); DAP_NEW_Z_SIZE_RET(l_info, dap_chain_node_net_states_info_t, l_info_size, l_linked_node_addrs); // func work // data preparing + l_info->version_info = DAP_CHAIN_NODE_NET_STATES_INFO_CURRENT_VERSION; + dap_strncpy(l_info->version_node, DAP_VERSION, sizeof(l_info->version_node) - 1); + l_info->role = dap_chain_net_get_role(l_net); l_info->address.uint64 = g_node_addr.uint64; l_info->uplinks_count = l_uplinks_count; l_info->downlinks_count = l_downlinks_count; dap_chain_t *l_chain = dap_chain_find_by_id(l_net->pub.id, (dap_chain_id_t){ .uint64 = 0 }); // zerochain l_info->events_count = (l_chain && l_chain->callback_count_atom) ? l_chain->callback_count_atom(l_chain) : 0; - l_chain =dap_chain_find_by_id(l_net->pub.id, (dap_chain_id_t){ .uint64 = 1 }); // mainchain + l_chain = l_chain ? l_chain->next : NULL; // mainchain l_info->atoms_count = (l_chain && l_chain->callback_count_atom) ? l_chain->callback_count_atom(l_chain) : 0; memcpy(l_info->links_addrs, l_linked_node_addrs, (l_info->uplinks_count + l_info->downlinks_count) * sizeof(dap_chain_node_addr_t)); @@ -110,16 +123,18 @@ static void s_states_info_to_str(dap_chain_net_t *a_net, const char *a_node_addr size_t l_data_size = 0; char *l_gdb_group = dap_strdup_printf("%s%s", a_net->pub.gdb_groups_prefix, s_states_group); dap_chain_node_net_states_info_t *l_store_obj = (dap_chain_node_net_states_info_t *)dap_global_db_get_sync(l_gdb_group, a_node_addr_str, &l_data_size, NULL, &l_timestamp); - if (!l_store_obj || l_data_size != sizeof(dap_chain_node_net_states_info_t) + (l_store_obj->uplinks_count + l_store_obj->downlinks_count) * sizeof(dap_chain_node_addr_t)) { - log_it(L_ERROR, "Can't find state about %s node", a_node_addr_str); + if (!l_store_obj || l_store_obj->version_info != DAP_CHAIN_NODE_NET_STATES_INFO_CURRENT_VERSION || + l_data_size != s_states_version_size[DAP_CHAIN_NODE_NET_STATES_INFO_CURRENT_VERSION - 1] + (l_store_obj->uplinks_count + l_store_obj->downlinks_count) * sizeof(dap_chain_node_addr_t)) + { + log_it(L_ERROR, "Can't find state about %s node in net %s", a_node_addr_str, a_net->pub.name); DAP_DELETE(l_gdb_group); return; } char l_ts[80] = { '\0' }; dap_nanotime_to_str_rfc822(l_ts, sizeof(l_ts), l_timestamp); dap_string_append_printf(l_info_str, - "Record timestamp: %s\nNode addr: %s\nNet: %s\nEvents count: %"DAP_UINT64_FORMAT_U"\nAtoms count: %"DAP_UINT64_FORMAT_U"\nUplinks count: %u\nDownlinks count: %u\n", - l_ts, a_node_addr_str, a_net->pub.name, l_store_obj->events_count, l_store_obj->atoms_count, l_store_obj->uplinks_count, l_store_obj->downlinks_count); + "Record timestamp: %s\nRecord version: %u\nNode version: %s\nNode addr: %s\nNet: %s\nRole: %s\nEvents count: %"DAP_UINT64_FORMAT_U"\nAtoms count: %"DAP_UINT64_FORMAT_U"\nUplinks count: %u\nDownlinks count: %u\n", + l_ts, l_store_obj->version_info, l_store_obj->version_node,a_node_addr_str, a_net->pub.name, dap_chain_node_role_to_str(l_store_obj->role), l_store_obj->events_count, l_store_obj->atoms_count, l_store_obj->uplinks_count, l_store_obj->downlinks_count); size_t l_max_links = dap_max(l_store_obj->uplinks_count, l_store_obj->downlinks_count); if(l_max_links) { dap_string_append_printf(l_info_str, @@ -422,6 +437,8 @@ static int s_node_states_info_cmp(dap_list_t *a_first, dap_list_t *a_second) if(b->events_count > a->events_count && b->events_count - a->events_count > s_cmp_delta_event) return 1; if(a->atoms_count > b->atoms_count && a->atoms_count - b->atoms_count > s_cmp_delta_atom) return -1; if(b->atoms_count > a->atoms_count && b->atoms_count - a->atoms_count > s_cmp_delta_atom) return 1; + if(a->role.enums == NODE_ROLE_ROOT) return 1; + if(b->role.enums == NODE_ROLE_ROOT) return -1; if(a->downlinks_count < b->downlinks_count) return -1; if(b->downlinks_count < a->downlinks_count) return 1; return 0; @@ -473,10 +490,15 @@ dap_list_t *dap_chain_node_get_states_list_sort(dap_chain_net_t *a_net, dap_chai if (!l_state_store_obj) { log_it(L_DEBUG, "Can't find state about %s node, apply low priority", l_objs[i].key); l_item->downlinks_count = (uint32_t)(-1); - } else if (l_data_size != sizeof(dap_chain_node_net_states_info_t) + (l_state_store_obj->uplinks_count + l_state_store_obj->downlinks_count) * sizeof(dap_chain_node_addr_t)) { - log_it(L_DEBUG, "Wrong %s node state record size, expected %zu, get %zu. Apply low priority", l_objs[i].key, sizeof(dap_chain_node_net_states_info_t) + (l_state_store_obj->uplinks_count + l_state_store_obj->downlinks_count) * sizeof(dap_chain_node_addr_t), l_data_size); + } else if ( + l_state_store_obj->version_info != DAP_CHAIN_NODE_NET_STATES_INFO_CURRENT_VERSION || + l_data_size != s_states_version_size[DAP_CHAIN_NODE_NET_STATES_INFO_CURRENT_VERSION - 1] + (l_state_store_obj->uplinks_count + l_state_store_obj->downlinks_count) * sizeof(dap_chain_node_addr_t) + ) { + log_it(L_DEBUG, "Wrong %s node state record size to state version %u, expected %zu, get %zu. Apply low priority", l_objs[i].key, l_state_store_obj->version_info, sizeof(dap_chain_node_net_states_info_t) + (l_state_store_obj->uplinks_count + l_state_store_obj->downlinks_count) * sizeof(dap_chain_node_addr_t), l_data_size); + l_item->role.enums = NODE_ROLE_ROOT; l_item->downlinks_count = (uint32_t)(-1); } else { + l_item->role.enums = l_state_store_obj->role.enums; l_item->atoms_count = l_state_store_obj->atoms_count; l_item->events_count = l_state_store_obj->events_count; l_item->downlinks_count = l_state_store_obj->downlinks_count; diff --git a/modules/net/dap_chain_node_cli.c b/modules/net/dap_chain_node_cli.c index 95620a0b29061ed7bdfdb3839f3a806ae19f4baa..99484040afdbafea0b96c545aa5140b59a76b1e8 100644 --- a/modules/net/dap_chain_node_cli.c +++ b/modules/net/dap_chain_node_cli.c @@ -218,9 +218,9 @@ int dap_chain_node_cli_init(dap_config_t * g_config) "\n" ); - dap_cli_server_cmd_add("token_update_sign", com_token_decl_sign, "Token update add sign and new sign", - "token_update_sign -net <net_name> [-chain <chain_name>] -datum <datum_hash> -certs <certs list> -new_certs <certs list>\n" - "\t Sign existent <datum hash> in mempool with <certs list>\n" + dap_cli_server_cmd_add("token_update_sign", com_token_decl_sign, "Token update add sign to datum", + "token_update_sign -net <net_name> [-chain <chain_name>] -datum <datum_hash> -certs <cert_list>\n" + "\t Sign existent <datum hash> in mempool with <certs_list>\n" ); // Token commands @@ -285,8 +285,9 @@ int dap_chain_node_cli_init(dap_config_t * g_config) // Transaction history dap_cli_server_cmd_add("tx_history", com_tx_history, "Transaction history (for address or by hash)", - "tx_history {-addr <addr> | -w <wallet_name> | -tx <tx_hash>} [-net <net_name>] [-chain <chain_name>] [-limit] [-offset]\n" - "tx_history -all -net <net_name> [-chain <chain_name>] [-limit] [-offset]\n"); + "tx_history {-addr <addr> | -w <wallet_name> | -tx <tx_hash>} [-net <net_name>] [-chain <chain_name>] [-limit] [-offset] [-head]\n" + "tx_history -all -net <net_name> [-chain <chain_name>] [-limit] [-offset] [-head]\n" + "tx_history -count -net <net_name>\n"); // Ledger info dap_cli_server_cmd_add("ledger", com_ledger, "Ledger information", diff --git a/modules/net/dap_chain_node_cli_cmd.c b/modules/net/dap_chain_node_cli_cmd.c index c170dcd8fa114a00f44f8e1d1dc402a09342f477..2aa446bd15e95d5425477696196e8a9ec6aee3a4 100644 --- a/modules/net/dap_chain_node_cli_cmd.c +++ b/modules/net/dap_chain_node_cli_cmd.c @@ -809,8 +809,10 @@ int com_global_db(int a_argc, char ** a_argv, void **a_str_reply) if (!l_objs || !l_objs_count) { - dap_json_rpc_error_add(DAP_CHAIN_NODE_CLI_COM_GLOBAL_DB_NO_DATA_IN_GROUP,"No data in group %s.", l_group_str); - return -DAP_CHAIN_NODE_CLI_COM_GLOBAL_DB_NO_DATA_IN_GROUP; + char *l_str_tmp = dap_strdup_printf("No data in group '%s' or group not found.", l_group_str); + json_object *l_jobj_msg = json_object_new_string(l_str_tmp); + json_object_array_add(*json_arr_reply, l_jobj_msg); + return DAP_CHAIN_NODE_CLI_COM_GLOBAL_DB_JSON_OK; } json_object* json_arr_keys = json_object_new_array(); @@ -2081,11 +2083,15 @@ int l_arg_index = 1, l_rc, cmd_num = CMD_NONE; break; case -EBUSY: dap_json_rpc_error_add(DAP_CHAIN_NODE_CLI_COM_TX_WALLET_ALREADY_ERR, - "Error: wallet %s is already %sactivated\n", l_wallet_name, l_prefix); + "Wallet %s is already %sactivated\n", l_wallet_name, l_prefix); break; case -EAGAIN: dap_json_rpc_error_add(DAP_CHAIN_NODE_CLI_COM_TX_WALLET_PASS_ERR, - "Error: wrong password for wallet %s\n", l_wallet_name); + "Wrong password for wallet %s\n", l_wallet_name); + break; + case -101: + dap_json_rpc_error_add(DAP_CHAIN_NODE_CLI_COM_TX_WALLET_PASS_ERR, + "Can't activate unprotected wallet %s\n", l_wallet_name); break; default: { char l_buf[512] = { '\0' }; @@ -2193,7 +2199,7 @@ int l_arg_index = 1, l_rc, cmd_num = CMD_NONE; "sig_multi_chained. You must specify at least two more " "signatures other than sig_multi_chained.\n" "After sig_multi_chained, you must specify two more signatures " - "from the list: %s", dap_cert_get_str_recommended_sign()); + "from the list:\n%s", dap_cert_get_str_recommended_sign()); json_object_put(json_arr_out); return DAP_CHAIN_NODE_CLI_COM_TX_WALLET_UNKNOWN_SIGN_ERR; } @@ -2619,9 +2625,15 @@ int com_token_decl_sign(int a_argc, char **a_argv, void **a_str_reply) size_t l_data_size = l_tsd_size + l_signs_size; l_datum_token = s_sign_cert_in_cycle(l_certs, l_datum_token, l_certs_count, &l_data_size, &l_sign_counter); + log_it(L_DEBUG, "Apply %zu signs to datum %s", l_sign_counter, l_datum_hash_hex_str); + if (!l_sign_counter) { + dap_cli_server_cmd_set_reply_text(a_str_reply, "Error! Used certs not valid"); + DAP_DEL_MULTY(l_datum_token, l_datum_hash_hex_str, l_datum_hash_base58_str, l_gdb_group_mempool); + return -9; + } l_datum_token->signs_total += l_sign_counter; size_t l_token_size = sizeof(*l_datum_token) + l_data_size; - dap_chain_datum_t * l_datum = dap_chain_datum_create(DAP_CHAIN_DATUM_TOKEN, + l_datum = dap_chain_datum_create(DAP_CHAIN_DATUM_TOKEN, l_datum_token, l_token_size); DAP_DELETE(l_datum_token); // Calc datum's hash @@ -2638,8 +2650,8 @@ int com_token_decl_sign(int a_argc, char **a_argv, void **a_str_reply) char* l_hash_str = l_datum_hash_hex_str; // Remove old datum from pool if( dap_global_db_del_sync(l_gdb_group_mempool, l_hash_str ) == 0) { - dap_cli_server_cmd_set_reply_text(a_str_reply, "Datum %s is replacing the %s in datum pool", - l_key_out_str, l_datum_hash_out_str); + dap_cli_server_cmd_set_reply_text(a_str_reply, "Datum was replaced in datum pool:\n\tOld: %s\n\tNew: %s", + l_datum_hash_out_str, l_key_out_str); } else { dap_cli_server_cmd_set_reply_text(a_str_reply, "Warning! Can't remove old datum %s ( new datum %s added normaly in datum pool)", @@ -2652,11 +2664,7 @@ int com_token_decl_sign(int a_argc, char **a_argv, void **a_str_reply) l_key_out_str, l_datum_hash_out_str); rc = -2; } - DAP_DELETE(l_key_str); - DAP_DELETE(l_datum_hash_hex_str); - DAP_DELETE(l_datum_hash_base58_str); - DAP_DELETE(l_datum); - DAP_DELETE(l_gdb_group_mempool); + DAP_DEL_MULTY(l_key_str, l_datum_hash_hex_str, l_datum_hash_base58_str, l_datum, l_gdb_group_mempool); return rc; } else { dap_cli_server_cmd_set_reply_text(a_str_reply, @@ -2669,8 +2677,7 @@ int com_token_decl_sign(int a_argc, char **a_argv, void **a_str_reply) l_chain?l_chain->name:"<undefined>"); return -5; } - DAP_DELETE(l_datum_hash_hex_str); - DAP_DELETE(l_datum_hash_base58_str); + DAP_DEL_MULTY(l_datum_hash_hex_str, l_datum_hash_base58_str); } else { dap_cli_server_cmd_set_reply_text(a_str_reply, "token_decl_sign need -datum <datum hash> argument"); return -2; @@ -3594,7 +3601,7 @@ int _cmd_mempool_proc(dap_chain_net_t *a_net, dap_chain_t *a_chain, const char * dap_json_rpc_allocation_error; return DAP_JSON_RPC_ERR_CODE_MEMORY_ALLOCATED; } - int l_verify_datum = dap_chain_net_verify_datum_for_add(a_chain, l_datum, &l_datum_hash); + int l_verify_datum = dap_chain_net_verify_datum_for_add(l_chain, l_datum, &l_datum_hash); if (l_verify_datum){ json_object *l_jobj_verify_err = json_object_new_string(dap_chain_net_verify_datum_err_code_to_str(l_datum, l_verify_datum)); json_object *l_jobj_verify_status = json_object_new_boolean(FALSE); @@ -3610,8 +3617,8 @@ int _cmd_mempool_proc(dap_chain_net_t *a_net, dap_chain_t *a_chain, const char * json_object_object_add(l_jobj_verify, "error", l_jobj_verify_err); ret = DAP_COM_MEMPOOL_PROC_LIST_ERROR_FALSE_VERIFY; } else { - if (a_chain->callback_add_datums) { - if (a_chain->callback_add_datums(a_chain, &l_datum, 1) == 0) { + if (l_chain->callback_add_datums) { + if (l_chain->callback_add_datums(l_chain, &l_datum, 1) == 0) { json_object *l_jobj_verify_status = json_object_new_boolean(FALSE); if (!l_jobj_verify_status) { json_object_put(l_jobj_verify_status); @@ -4050,7 +4057,6 @@ typedef struct _dap_cli_token_additional_params { uint16_t parsed_flags; size_t tsd_total_size; byte_t *parsed_tsd; - size_t parsed_tsd_size; } dap_cli_token_additional_params; typedef struct _dap_sdk_cli_params { @@ -4204,7 +4210,6 @@ static int s_parse_additional_token_decl_arg(int a_argc, char ** a_argv, void ** size_t l_tsd_total_size = 0; uint16_t l_flags = 0; char ** l_str_flags = NULL; - a_params->ext.parsed_tsd_size = 0; if (!a_update_token) { if (a_params->ext.flags){ // Flags @@ -4219,7 +4224,6 @@ static int s_parse_additional_token_decl_arg(int a_argc, char ** a_argv, void ** l_str_flags++; } } - a_params->ext.parsed_flags = l_flags; } else { const char *l_set_flags = NULL; const char *l_unset_flags = NULL; @@ -4240,7 +4244,6 @@ static int s_parse_additional_token_decl_arg(int a_argc, char ** a_argv, void ** l_flags = 0; l_tsd_list = dap_list_append(l_tsd_list, l_flag_set_tsd); l_tsd_total_size += dap_tsd_size(l_flag_set_tsd); - a_params->ext.parsed_tsd_size += dap_tsd_size(l_flag_set_tsd); } if (l_unset_flags) { l_str_flags = dap_strsplit(l_unset_flags,",",0xffff ); @@ -4257,10 +4260,41 @@ static int s_parse_additional_token_decl_arg(int a_argc, char ** a_argv, void ** l_flags = 0; l_tsd_list = dap_list_append(l_tsd_list, l_flag_unset_tsd); l_tsd_total_size += dap_tsd_size(l_flag_unset_tsd); - a_params->ext.parsed_tsd_size += dap_tsd_size(l_flag_unset_tsd); } } + if (a_params->ext.total_signs_valid){ // Signs valid + uint16_t l_param_value = (uint16_t)atoi(a_params->ext.total_signs_valid); + dap_tsd_t * l_tsd = dap_tsd_create_scalar( + DAP_CHAIN_DATUM_TOKEN_TSD_TYPE_TOTAL_SIGNS_VALID, l_param_value); + l_tsd_list = dap_list_append(l_tsd_list, l_tsd); + l_tsd_total_size+= dap_tsd_size(l_tsd); + } + if (a_params->ext.datum_type_allowed){ + dap_tsd_t * l_tsd = dap_tsd_create_string( + DAP_CHAIN_DATUM_TOKEN_TSD_TYPE_DATUM_TYPE_ALLOWED_ADD, a_params->ext.datum_type_allowed); + l_tsd_list = dap_list_append(l_tsd_list, l_tsd); + l_tsd_total_size+= dap_tsd_size(l_tsd); + } + if (a_params->ext.datum_type_blocked){ + dap_tsd_t * l_tsd = dap_tsd_create_string( + DAP_CHAIN_DATUM_TOKEN_TSD_TYPE_DATUM_TYPE_BLOCKED_ADD, a_params->ext.datum_type_blocked); + l_tsd_list = dap_list_append(l_tsd_list, l_tsd); + l_tsd_total_size+= dap_tsd_size(l_tsd); + } + if (a_params->ext.tx_receiver_allowed) + l_tsd_list = s_parse_wallet_addresses(a_params->ext.tx_receiver_allowed, l_tsd_list, &l_tsd_total_size, DAP_CHAIN_DATUM_TOKEN_TSD_TYPE_TX_RECEIVER_ALLOWED_ADD); + + if (a_params->ext.tx_receiver_blocked) + l_tsd_list = s_parse_wallet_addresses(a_params->ext.tx_receiver_blocked, l_tsd_list, &l_tsd_total_size, DAP_CHAIN_DATUM_TOKEN_TSD_TYPE_TX_RECEIVER_BLOCKED_ADD); + + if (a_params->ext.tx_sender_allowed) + l_tsd_list = s_parse_wallet_addresses(a_params->ext.tx_sender_allowed, l_tsd_list, &l_tsd_total_size, DAP_CHAIN_DATUM_TOKEN_TSD_TYPE_TX_SENDER_ALLOWED_ADD); + + if (a_params->ext.tx_sender_blocked) + l_tsd_list = s_parse_wallet_addresses(a_params->ext.tx_sender_blocked, l_tsd_list, &l_tsd_total_size, DAP_CHAIN_DATUM_TOKEN_TSD_TYPE_TX_SENDER_BLOCKED_ADD); + + const char* l_new_certs_str = NULL; const char* l_remove_signs = NULL; dap_cli_server_cmd_find_option_val(a_argv, 0, a_argc, "-new_certs", &l_new_certs_str); @@ -4310,7 +4344,6 @@ static int s_parse_additional_token_decl_arg(int a_argc, char ** a_argv, void ** dap_tsd_t *l_desc_token = dap_tsd_create_string(DAP_CHAIN_DATUM_TOKEN_TSD_TOKEN_DESCRIPTION, l_description); l_tsd_list = dap_list_append(l_tsd_list, l_desc_token); l_tsd_total_size += dap_tsd_size(l_desc_token); - a_params->ext.parsed_tsd_size += dap_tsd_size(l_desc_token); } size_t l_tsd_offset = 0; a_params->ext.parsed_tsd = DAP_NEW_SIZE(byte_t, l_tsd_total_size); @@ -4487,24 +4520,9 @@ int com_token_decl(int a_argc, char ** a_argv, void **a_str_reply) case DAP_CHAIN_DATUM_TOKEN_SUBTYPE_NATIVE: { // 256 dap_list_t *l_tsd_list = NULL; - size_t l_tsd_total_size = 0; - uint16_t l_flags = 0; - char ** l_str_flags = NULL; - - if (l_params->ext.flags){ // Flags - l_str_flags = dap_strsplit(l_params->ext.flags,",",0xffff ); - while (l_str_flags && *l_str_flags){ - uint16_t l_flag = dap_chain_datum_token_flag_from_str(*l_str_flags); - if (l_flag == DAP_CHAIN_DATUM_TOKEN_FLAG_UNDEFINED ){ - dap_cli_server_cmd_set_reply_text(a_str_reply, "Flag can't be \"%s\"",*l_str_flags); - DAP_DEL_Z(l_params); - return -20; - } - l_flags |= l_flag; // if we have multiple flags - l_str_flags++; - } - } - if (l_params->ext.delegated_token_from){ + size_t l_tsd_local_list_size = 0; + + if (l_params->ext.delegated_token_from){ dap_chain_datum_token_t *l_delegated_token_from; if (NULL == (l_delegated_token_from = dap_ledger_token_ticker_check(l_net->pub.ledger, l_params->ext.delegated_token_from))) { dap_cli_server_cmd_set_reply_text(a_str_reply,"To create a delegated token %s, can't find token by ticket %s", l_ticker, l_params->ext.delegated_token_from); @@ -4524,42 +4542,19 @@ int com_token_decl(int a_argc, char ** a_argv, void **a_str_reply) dap_tsd_t * l_tsd = dap_tsd_create_scalar( DAP_CHAIN_DATUM_TOKEN_TSD_TYPE_DELEGATE_EMISSION_FROM_STAKE_LOCK, l_tsd_section); l_tsd_list = dap_list_append(l_tsd_list, l_tsd); - l_tsd_total_size+= dap_tsd_size(l_tsd); + l_tsd_local_list_size += dap_tsd_size(l_tsd); } - if (l_params->ext.total_signs_valid){ // Signs valid - uint16_t l_param_value = (uint16_t)atoi(l_params->ext.total_signs_valid); - l_signs_total = l_param_value; - dap_tsd_t * l_tsd = dap_tsd_create_scalar( - DAP_CHAIN_DATUM_TOKEN_TSD_TYPE_TOTAL_SIGNS_VALID, l_param_value); - l_tsd_list = dap_list_append(l_tsd_list, l_tsd); - l_tsd_total_size+= dap_tsd_size(l_tsd); - } - if (l_params->ext.datum_type_allowed){ - dap_tsd_t * l_tsd = dap_tsd_create_string( - DAP_CHAIN_DATUM_TOKEN_TSD_TYPE_DATUM_TYPE_ALLOWED_ADD, l_params->ext.datum_type_allowed); - l_tsd_list = dap_list_append(l_tsd_list, l_tsd); - l_tsd_total_size+= dap_tsd_size(l_tsd); - } - if (l_params->ext.datum_type_blocked){ - dap_tsd_t * l_tsd = dap_tsd_create_string( - DAP_CHAIN_DATUM_TOKEN_TSD_TYPE_DATUM_TYPE_BLOCKED_ADD, l_params->ext.datum_type_blocked); - l_tsd_list = dap_list_append(l_tsd_list, l_tsd); - l_tsd_total_size+= dap_tsd_size(l_tsd); + + if (l_params->ext.total_signs_valid) { + l_signs_total = (uint16_t)atoi(l_params->ext.total_signs_valid); } - if (l_params->ext.tx_receiver_allowed) - l_tsd_list = s_parse_wallet_addresses(l_params->ext.tx_receiver_allowed, l_tsd_list, &l_tsd_total_size, DAP_CHAIN_DATUM_TOKEN_TSD_TYPE_TX_RECEIVER_ALLOWED_ADD); - if (l_params->ext.tx_receiver_blocked) - l_tsd_list = s_parse_wallet_addresses(l_params->ext.tx_receiver_blocked, l_tsd_list, &l_tsd_total_size, DAP_CHAIN_DATUM_TOKEN_TSD_TYPE_TX_RECEIVER_BLOCKED_ADD); - if (l_params->ext.tx_sender_allowed) - l_tsd_list = s_parse_wallet_addresses(l_params->ext.tx_sender_allowed, l_tsd_list, &l_tsd_total_size, DAP_CHAIN_DATUM_TOKEN_TSD_TYPE_TX_SENDER_ALLOWED_ADD); + size_t l_tsd_total_size = l_tsd_local_list_size + l_params->ext.tsd_total_size; - if (l_params->ext.tx_sender_blocked) - l_tsd_list = s_parse_wallet_addresses(l_params->ext.tx_sender_blocked, l_tsd_list, &l_tsd_total_size, DAP_CHAIN_DATUM_TOKEN_TSD_TYPE_TX_SENDER_BLOCKED_ADD); - if (l_params->ext.parsed_tsd) - l_tsd_total_size += l_params->ext.parsed_tsd_size; + // if (l_params->ext.parsed_tsd) + // l_tsd_total_size += l_params->ext.parsed_tsd_size; // Create new datum token @@ -4579,7 +4574,7 @@ int com_token_decl(int a_argc, char ** a_argv, void **a_str_reply) l_datum_token->header_private_decl.flags = l_params->ext.parsed_flags; l_datum_token->total_supply = l_total_supply; l_datum_token->signs_valid = l_signs_emission; - l_datum_token->header_private_decl.tsd_total_size = l_tsd_total_size; + l_datum_token->header_private_decl.tsd_total_size = l_tsd_local_list_size + l_params->ext.tsd_total_size; l_datum_token->header_private_decl.decimals = atoi(l_params->decimals_str); } else { //DAP_CHAIN_DATUM_TOKEN_TYPE_NATIVE_DECL log_it(L_DEBUG,"Prepared TSD sections for CF20 token on %zd total size", l_tsd_total_size); @@ -4676,6 +4671,7 @@ int com_token_decl(int a_argc, char ** a_argv, void **a_str_reply) DAP_DEL_Z(l_params); return -8; } + dap_uuid_generate_nonce(&l_datum_token->nonce, DAP_CHAIN_DATUM_NONCE_SIZE); // If we have more certs than we need signs - use only first part of the list if(l_certs_count > l_signs_total) l_certs_count = l_signs_total; @@ -4856,7 +4852,7 @@ int com_token_update(int a_argc, char ** a_argv, void **a_str_reply) // Add TSD sections in the end // Add TSD sections in the end if (l_params->ext.tsd_total_size) { - memcpy(l_datum_token->tsd_n_signs, l_params->ext.parsed_tsd, l_params->ext.parsed_tsd_size); + memcpy(l_datum_token->tsd_n_signs, l_params->ext.parsed_tsd, l_params->ext.tsd_total_size); DAP_DELETE(l_params->ext.parsed_tsd); } log_it(L_DEBUG, "%s token declaration update '%s' initialized", ( l_params->subtype == DAP_CHAIN_DATUM_TOKEN_SUBTYPE_PRIVATE) ? @@ -4882,6 +4878,7 @@ int com_token_update(int a_argc, char ** a_argv, void **a_str_reply) "Unknown token type"); return -8; } + dap_uuid_generate_nonce(&l_datum_token->nonce, DAP_CHAIN_DATUM_NONCE_SIZE); // If we have more certs than we need signs - use only first part of the list if(l_certs_count > l_signs_total) l_certs_count = l_signs_total; @@ -7264,8 +7261,9 @@ int com_tx_history(int a_argc, char ** a_argv, void **a_str_reply) bool l_brief = (dap_cli_server_cmd_check_option(a_argv, arg_index, a_argc, "-brief") != -1) ? true : false; bool l_is_tx_all = dap_cli_server_cmd_find_option_val(a_argv, arg_index, a_argc, "-all", NULL); + bool l_is_tx_count = dap_cli_server_cmd_find_option_val(a_argv, arg_index, a_argc, "-count", NULL); - if (!l_addr_base58 && !l_wallet_name && !l_tx_hash_str && !l_is_tx_all) { + if (!l_addr_base58 && !l_wallet_name && !l_tx_hash_str && !l_is_tx_all && !l_is_tx_count) { dap_json_rpc_error_add(DAP_CHAIN_NODE_CLI_COM_TX_HISTORY_PARAM_ERR, "tx_history requires parameter '-addr' or '-w' or '-tx'"); return DAP_CHAIN_NODE_CLI_COM_TX_HISTORY_PARAM_ERR; @@ -7404,6 +7402,11 @@ int com_tx_history(int a_argc, char ** a_argv, void **a_str_reply) json_object_array_add(*json_arr_reply, json_arr_history_all); json_object_array_add(*json_arr_reply, json_obj_summary); return DAP_CHAIN_NODE_CLI_COM_TX_HISTORY_OK; + } else if (l_is_tx_count) { + json_object * json_count_obj= json_object_new_object(); + json_object_object_add(json_count_obj, "Number of transaction", json_object_new_uint64(l_chain->callback_count_tx(l_chain))); + json_object_array_add(*json_arr_reply, json_count_obj); + return DAP_CHAIN_NODE_CLI_COM_TX_HISTORY_OK; } if (json_obj_out) { @@ -8263,7 +8266,7 @@ static int s_signer_cmd(int a_arg_index, int a_argc, char **a_argv, void **a_str dap_chain_datum_t * l_datum = dap_chain_datum_create(DAP_CHAIN_DATUM_SIGNER, l_sign->pkey_n_sign, l_sign->header.sign_size); if (!l_datum) return dap_cli_server_cmd_set_reply_text(a_str_reply, "not created datum"), -1; - dap_cli_server_cmd_set_reply_text(a_str_reply, "hash: %s", dap_get_data_hash_str(l_datum->data, l_datum->header.data_size)); + dap_cli_server_cmd_set_reply_text(a_str_reply, "hash: %s", dap_get_data_hash_str(l_datum->data, l_datum->header.data_size).s); return DAP_DELETE(l_datum), l_chain->callback_add_datums(l_chain, &l_datum, 1); } diff --git a/modules/net/dap_chain_node_cli_cmd_tx.c b/modules/net/dap_chain_node_cli_cmd_tx.c index 09a098ebd29497710da77f2f0bbb67c549c9b3bf..a59d2e279bc81a92b9b749a6b4ccbf2ee5b492ab 100644 --- a/modules/net/dap_chain_node_cli_cmd_tx.c +++ b/modules/net/dap_chain_node_cli_cmd_tx.c @@ -128,7 +128,7 @@ json_object * dap_db_tx_history_to_json(dap_chain_hash_fast_t* a_tx_hash, dap_chain_datum_tx_t * l_tx, dap_chain_t * a_chain, const char *a_hash_out_type, - dap_chain_net_t * l_net, + dap_chain_datum_iter_t *a_datum_iter, int l_ret_code, bool *accepted_tx, bool brief_out) @@ -141,7 +141,8 @@ json_object * dap_db_tx_history_to_json(dap_chain_hash_fast_t* a_tx_hash, } dap_ledger_t *l_ledger = dap_chain_net_by_id(a_chain->net_id)->pub.ledger; - l_tx_token_ticker = dap_ledger_tx_get_token_ticker_by_hash(l_ledger, a_tx_hash); + l_tx_token_ticker = a_datum_iter ? a_datum_iter->token_ticker + : dap_ledger_tx_get_token_ticker_by_hash(l_ledger, a_tx_hash); if (l_tx_token_ticker) { json_object_object_add(json_obj_datum, "status", json_object_new_string("ACCEPTED")); l_tx_token_description = dap_ledger_get_description_by_ticker(l_ledger, l_tx_token_ticker); @@ -174,15 +175,18 @@ json_object * dap_db_tx_history_to_json(dap_chain_hash_fast_t* a_tx_hash, dap_chain_net_srv_uid_t uid; char *service_name; dap_chain_tx_tag_action_type_t action; + bool srv_found = a_datum_iter ? a_datum_iter->uid.uint64 ? true : false + : dap_ledger_tx_service_info(l_ledger, a_tx_hash, &uid, &service_name, &action); + if (a_datum_iter)action = a_datum_iter->action; - if (dap_ledger_tx_service_info(l_ledger, a_tx_hash, &uid, &service_name, &action)) + if (srv_found) { - json_object_object_add(json_obj_datum, "service", json_object_new_string(service_name)); + //json_object_object_add(json_obj_datum, "service", json_object_new_string(service_name)); json_object_object_add(json_obj_datum, "action", json_object_new_string(dap_ledger_tx_action_str(action))); } else { - json_object_object_add(json_obj_datum, "service", json_object_new_string("UNKNOWN")); + //json_object_object_add(json_obj_datum, "service", json_object_new_string("UNKNOWN")); json_object_object_add(json_obj_datum, "action", json_object_new_string("UNKNOWN")); } @@ -219,7 +223,7 @@ json_object * dap_db_history_tx(dap_chain_hash_fast_t* a_tx_hash, (dap_chain_datum_tx_t *)l_datum->data : NULL; if (l_tx) { - return dap_db_tx_history_to_json(a_tx_hash, &l_atom_hash,l_tx, a_chain, a_hash_out_type, l_net, l_ret_code, &accepted_tx, false); + return dap_db_tx_history_to_json(a_tx_hash, &l_atom_hash,l_tx, a_chain, a_hash_out_type, NULL, l_ret_code, &accepted_tx, false); } else { const char *l_tx_hash_str = dap_strcmp(a_hash_out_type, "hex") ? dap_enc_base58_encode_hash_to_str_static(a_tx_hash) @@ -230,9 +234,9 @@ json_object * dap_db_history_tx(dap_chain_hash_fast_t* a_tx_hash, } static void s_tx_header_print(json_object* json_obj_datum, dap_chain_tx_hash_processed_ht_t **a_tx_data_ht, - dap_chain_datum_tx_t *a_tx, dap_hash_fast_t *a_atom_hash, + dap_chain_datum_tx_t *a_tx, dap_chain_datum_iter_t *a_datum_iter, const char *a_hash_out_type, dap_ledger_t *a_ledger, - dap_chain_hash_fast_t *a_tx_hash, int a_ret_code) + dap_chain_hash_fast_t *a_tx_hash) { bool l_declined = false; // transaction time @@ -251,37 +255,40 @@ static void s_tx_header_print(json_object* json_obj_datum, dap_chain_tx_hash_pro } l_tx_data->hash = *a_tx_hash; HASH_ADD(hh, *a_tx_data_ht, hash, sizeof(*a_tx_hash), l_tx_data); - const char *l_token_ticker = dap_ledger_tx_get_token_ticker_by_hash(a_ledger, a_tx_hash); + const char *l_token_ticker = a_datum_iter->token_ticker; if (!l_token_ticker) l_declined = true; } char *l_tx_hash_str, *l_atom_hash_str; if (!dap_strcmp(a_hash_out_type, "hex")) { l_tx_hash_str = dap_chain_hash_fast_to_str_new(a_tx_hash); - l_atom_hash_str = dap_chain_hash_fast_to_str_new(a_atom_hash); + l_atom_hash_str = dap_chain_hash_fast_to_str_new(a_datum_iter->cur_atom_hash); } else { l_tx_hash_str = dap_enc_base58_encode_hash_to_str(a_tx_hash); - l_atom_hash_str = dap_enc_base58_encode_hash_to_str(a_atom_hash); + l_atom_hash_str = dap_enc_base58_encode_hash_to_str(a_datum_iter->cur_atom_hash); } json_object_object_add(json_obj_datum, "status", json_object_new_string(l_declined ? "DECLINED" : "ACCEPTED")); json_object_object_add(json_obj_datum, "hash", json_object_new_string(l_tx_hash_str)); json_object_object_add(json_obj_datum, "atom_hash", json_object_new_string(l_atom_hash_str)); - json_object_object_add(json_obj_datum, "ret_code", json_object_new_int(a_ret_code)); - json_object_object_add(json_obj_datum, "ret_code_str", json_object_new_string(dap_ledger_check_error_str(a_ret_code))); + json_object_object_add(json_obj_datum, "ret_code", json_object_new_int(a_datum_iter->ret_code)); + json_object_object_add(json_obj_datum, "ret_code_str", json_object_new_string(dap_ledger_check_error_str(a_datum_iter->ret_code))); dap_chain_net_srv_uid_t uid; char *service_name; dap_chain_tx_tag_action_type_t action; + bool srv_found = a_datum_iter->uid.uint64 ? true : false; + action = a_datum_iter->action; - if (dap_ledger_tx_service_info(a_ledger, a_tx_hash, &uid, &service_name, &action)) + //if (dap_ledger_tx_service_info(a_ledger, a_tx_hash, &uid, &service_name, &action)) + if (srv_found) { - json_object_object_add(json_obj_datum, "service", json_object_new_string(service_name)); + //json_object_object_add(json_obj_datum, "service", json_object_new_string(service_name)); json_object_object_add(json_obj_datum, "action", json_object_new_string(dap_ledger_tx_action_str(action))); } else { - json_object_object_add(json_obj_datum, "service", json_object_new_string("UNKNOWN")); + //json_object_object_add(json_obj_datum, "service", json_object_new_string("UNKNOWN")); json_object_object_add(json_obj_datum, "action", json_object_new_string("UNKNOWN")); } @@ -336,13 +343,10 @@ json_object* dap_db_history_addr(dap_chain_addr_t *a_addr, dap_chain_t *a_chain, dap_chain_addr_t l_net_fee_addr = {}; bool l_net_fee_used = dap_chain_net_tx_get_fee(l_net->pub.id, NULL, &l_net_fee_addr); - bool l_is_need_correction = false; - bool l_continue = false; - uint256_t l_corr_value = {}, l_unstake_value = {}; bool look_for_unknown_service = (a_srv && strcmp(a_srv,"unknown") == 0); size_t l_arr_start = 0; size_t l_arr_end = 0; - s_set_offset_limit_json(json_obj_datum, &l_arr_start, &l_arr_end, a_limit, a_offset, a_chain->callback_count_atom(a_chain)); + s_set_offset_limit_json(json_obj_datum, &l_arr_start, &l_arr_end, a_limit, a_offset, a_chain->callback_count_tx(a_chain)); size_t i_tmp = 0; size_t @@ -365,12 +369,15 @@ json_object* dap_db_history_addr(dap_chain_addr_t *a_addr, dap_chain_t *a_chain, { if (i_tmp >= l_arr_end) break; - json_object *l_corr_object = NULL; if (l_datum->header.type_id != DAP_CHAIN_DATUM_TX) // go to next datum continue; // it's a transaction - bool l_is_unstake = false; + bool l_is_need_correction = false; + bool l_continue = false; + uint256_t l_corr_value = {}, l_cond_value = {}; + bool l_recv_from_cond = false, l_send_to_same_cond = false; + json_object *l_corr_object = NULL, *l_cond_recv_object = NULL, *l_cond_send_object = NULL; dap_chain_datum_tx_t *l_tx = (dap_chain_datum_tx_t *)l_datum->data; dap_list_t *l_list_in_items = dap_chain_datum_tx_items_get(l_tx, TX_ITEM_TYPE_IN_ALL, NULL); if (!l_list_in_items) // a bad tx @@ -382,7 +389,8 @@ json_object* dap_db_history_addr(dap_chain_addr_t *a_addr, dap_chain_t *a_chain, const char *l_noaddr_token = NULL; dap_hash_fast_t l_tx_hash = *l_datum_iter->cur_hash; - const char *l_src_token = dap_ledger_tx_get_token_ticker_by_hash(l_ledger, &l_tx_hash); + const char *l_src_token = l_datum_iter->token_ticker; + //const char *l_src_token = dap_ledger_tx_get_token_ticker_by_hash(l_ledger, &l_tx_hash); int l_src_subtype = DAP_CHAIN_TX_OUT_COND_SUBTYPE_UNDEFINED; for (dap_list_t *it = l_list_in_items; it; it = it->next) { @@ -433,10 +441,8 @@ json_object* dap_db_history_addr(dap_chain_addr_t *a_addr, dap_chain_t *a_chain, if (l_cond_prev->header.subtype == DAP_CHAIN_TX_OUT_COND_SUBTYPE_FEE) l_noaddr_token = l_native_ticker; else { - if (l_cond_prev->header.subtype == DAP_CHAIN_TX_OUT_COND_SUBTYPE_SRV_STAKE_LOCK) { - l_is_unstake = true; - l_unstake_value = l_cond_prev->header.value; - } + l_recv_from_cond = true; + l_cond_value = l_cond_prev->header.value; l_noaddr_token = l_src_token; } } break; @@ -484,6 +490,7 @@ json_object* dap_db_history_addr(dap_chain_addr_t *a_addr, dap_chain_t *a_chain, if (!l_dst_addr_present) { json_object_put(j_arr_data); + j_arr_data = NULL; json_object_put(j_obj_tx); dap_list_free(l_list_out_items); continue; @@ -526,25 +533,20 @@ json_object* dap_db_history_addr(dap_chain_addr_t *a_addr, dap_chain_t *a_chain, SUM_256_256(l_fee_sum, l_value, &l_fee_sum); //tag - char *service_name = NULL; + const char *l_service_name = NULL; dap_chain_tx_tag_action_type_t l_action; - bool srv_found = dap_ledger_tx_service_info(l_ledger, &l_tx_hash, NULL, &service_name, &l_action); + bool srv_found = l_datum_iter->uid.uint64 ? true : false; + l_action = l_datum_iter->action; + l_service_name = dap_ledger_tx_action_str(l_action); if (!(l_action & a_action)) continue; - - if (a_srv) - { - + if (a_srv) { //skip if looking for UNKNOWN + it is known - if (look_for_unknown_service && srv_found) { - continue; - } - + if (look_for_unknown_service && srv_found) + continue; //skip if search condition provided, it not UNKNOWN and found name not match - if (!look_for_unknown_service && (!srv_found || strcmp(service_name, a_srv) != 0)) - { + if (!look_for_unknown_service && (!srv_found || strcmp(l_service_name, a_srv) != 0)) continue; - } } if (l_dst_addr && dap_chain_addr_compare(l_dst_addr, a_addr)) { @@ -554,8 +556,8 @@ json_object* dap_db_history_addr(dap_chain_addr_t *a_addr, dap_chain_t *a_chain, break; } if (!l_header_printed) { - s_tx_header_print(j_obj_tx, &l_tx_data_ht, l_tx, l_datum_iter->cur_atom_hash, - a_hash_out_type, l_ledger, &l_tx_hash, l_datum_iter->ret_code); + s_tx_header_print(j_obj_tx, &l_tx_data_ht, l_tx, l_datum_iter, + a_hash_out_type, l_ledger, &l_tx_hash); l_header_printed = true; l_count++; i_tmp++; @@ -568,8 +570,8 @@ json_object* dap_db_history_addr(dap_chain_addr_t *a_addr, dap_chain_t *a_chain, l_src_str = dap_chain_addr_to_str_static(l_src_addr); else l_src_str = dap_chain_tx_out_cond_subtype_to_str(l_src_subtype); - if (l_is_unstake) - l_value = l_unstake_value; + if (l_recv_from_cond) + l_value = l_cond_value; else if (!dap_strcmp(l_native_ticker, l_noaddr_token)) { l_is_need_correction = true; l_corr_value = l_value; @@ -589,10 +591,12 @@ json_object* dap_db_history_addr(dap_chain_addr_t *a_addr, dap_chain_t *a_chain, json_object_object_add(j_obj_data, "token", l_dst_token ? json_object_new_string(l_dst_token) : json_object_new_string("UNKNOWN")); json_object_object_add(j_obj_data, "source_address", json_object_new_string(l_src_str)); - if (l_is_need_correction) - l_corr_object = j_obj_data; + if (l_recv_from_cond && !l_cond_recv_object) + l_cond_recv_object = j_obj_data; else json_object_array_add(j_arr_data, j_obj_data); + if (l_is_need_correction) + l_corr_object = j_obj_data; } else if (!l_src_addr || dap_chain_addr_compare(l_src_addr, a_addr)) { if (!l_dst_addr && ((dap_chain_tx_out_cond_t *)it->data)->header.subtype == l_src_subtype && l_src_subtype == DAP_CHAIN_TX_OUT_COND_SUBTYPE_FEE) @@ -605,17 +609,23 @@ json_object* dap_db_history_addr(dap_chain_addr_t *a_addr, dap_chain_t *a_chain, break; } if (!l_header_printed) { - s_tx_header_print(j_obj_tx, &l_tx_data_ht, l_tx, l_datum_iter->cur_atom_hash, - a_hash_out_type, l_ledger, &l_tx_hash, l_datum_iter->ret_code); + s_tx_header_print(j_obj_tx, &l_tx_data_ht, l_tx, l_datum_iter, + a_hash_out_type, l_ledger, &l_tx_hash); l_header_printed = true; l_count++; i_tmp++; l_src_token ? l_tx_ledger_accepted++ : l_tx_ledger_rejected++; } - const char *l_dst_addr_str = l_dst_addr ? dap_chain_addr_to_str_static(l_dst_addr) - : dap_chain_tx_out_cond_subtype_to_str( - ((dap_chain_tx_out_cond_t *)it->data)->header.subtype); + const char *l_dst_addr_str = NULL; + if (l_dst_addr) + l_dst_addr_str = dap_chain_addr_to_str_static(l_dst_addr); + else { + dap_chain_tx_out_cond_subtype_t l_dst_subtype = ((dap_chain_tx_out_cond_t *)it->data)->header.subtype; + l_dst_addr_str = dap_chain_tx_out_cond_subtype_to_str(l_dst_subtype); + if (l_recv_from_cond && l_dst_subtype != DAP_CHAIN_TX_OUT_COND_SUBTYPE_FEE && l_dst_subtype == l_src_subtype) + l_send_to_same_cond = true; + } const char *l_coins_str, *l_value_str = dap_uint256_to_char(l_value, &l_coins_str); json_object * j_obj_data = json_object_new_object(); @@ -631,13 +641,45 @@ json_object* dap_db_history_addr(dap_chain_addr_t *a_addr, dap_chain_t *a_chain, json_object_object_add(j_obj_data, "token", l_dst_token ? json_object_new_string(l_dst_token) : json_object_new_string("UNKNOWN")); json_object_object_add(j_obj_data, "destination_address", json_object_new_string(l_dst_addr_str)); - json_object_array_add(j_arr_data, j_obj_data); + if (l_send_to_same_cond && !l_cond_send_object) + l_cond_send_object = j_obj_data; + else + json_object_array_add(j_arr_data, j_obj_data); } } - if (l_continue){ - l_continue = false; + if (l_continue) continue; + + if (l_is_need_correction) { + SUM_256_256(l_corr_value, l_fee_sum, &l_corr_value); + const char *l_coins_str, *l_value_str = dap_uint256_to_char(l_corr_value, &l_coins_str); + json_object_object_add(l_corr_object, "recv_coins", json_object_new_string(l_coins_str)); + json_object_object_add(l_corr_object, "recv_datoshi", json_object_new_string(l_value_str)); } + if (l_send_to_same_cond) { + json_object *l_cond_recv_value_obj = json_object_object_get(l_cond_recv_object, "recv_datoshi"); + const char *l_cond_recv_value_str = json_object_get_string(l_cond_recv_value_obj); + uint256_t l_cond_recv_value = dap_uint256_scan_uninteger(l_cond_recv_value_str); + json_object *l_cond_send_value_obj = json_object_object_get(l_cond_send_object, "send_datoshi"); + const char *l_cond_send_value_str = json_object_get_string(l_cond_send_value_obj); + uint256_t l_cond_send_value = dap_uint256_scan_uninteger(l_cond_send_value_str); + assert(!IS_ZERO_256(l_cond_recv_value) && !IS_ZERO_256(l_cond_send_value)); + int l_direction = compare256(l_cond_recv_value, l_cond_send_value); + if (l_direction > 0) { + SUBTRACT_256_256(l_cond_recv_value, l_cond_send_value, &l_cond_recv_value); + const char *l_coins_str, *l_value_str = dap_uint256_to_char(l_cond_recv_value, &l_coins_str); + json_object_object_add(l_cond_recv_object, "recv_coins", json_object_new_string(l_coins_str)); + json_object_object_add(l_cond_recv_object, "recv_datoshi", json_object_new_string(l_value_str)); + json_object_array_add(j_arr_data, l_cond_recv_object); + } else if (l_direction < 0) { + SUBTRACT_256_256(l_cond_send_value, l_cond_recv_value, &l_cond_send_value); + const char *l_coins_str, *l_value_str = dap_uint256_to_char(l_cond_send_value, &l_coins_str); + json_object_object_add(l_cond_send_object, "send_coins", json_object_new_string(l_coins_str)); + json_object_object_add(l_cond_send_object, "send_datoshi", json_object_new_string(l_value_str)); + json_object_array_add(j_arr_data, l_cond_send_object); + } + } else if (l_recv_from_cond) + json_object_array_add(j_arr_data, l_cond_recv_object); if (json_object_array_length(j_arr_data) > 0) { json_object_object_add(j_obj_tx, "data", j_arr_data); @@ -648,16 +690,6 @@ json_object* dap_db_history_addr(dap_chain_addr_t *a_addr, dap_chain_t *a_chain, json_object_put(j_obj_tx); } dap_list_free(l_list_out_items); - if (l_is_need_correction && l_corr_object) { - SUM_256_256(l_corr_value, l_fee_sum, &l_corr_value); - const char *l_coins_str, *l_value_str = dap_uint256_to_char(l_corr_value, &l_coins_str); - json_object_object_add(l_corr_object, "recv_coins", json_object_new_string(l_coins_str)); - json_object_object_add(l_corr_object, "recv_datoshi", json_object_new_string(l_value_str)); - json_object * j_arr_correct = json_object_new_object(); - json_object_object_add(j_arr_correct, "correction", l_corr_object); - json_object_array_add(json_obj_datum, j_arr_correct); - l_is_need_correction = false; - } } a_chain->callback_datum_iter_delete(l_datum_iter); // delete hashes @@ -682,7 +714,8 @@ json_object* dap_db_history_addr(dap_chain_addr_t *a_addr, dap_chain_t *a_chain, return json_obj_datum; } -static int s_json_tx_history_pack(json_object** a_json_obj_datum, dap_chain_datum_t * a_datum, dap_chain_net_t *a_net, +static int s_json_tx_history_pack(json_object** a_json_obj_datum, dap_chain_datum_iter_t *a_datum_iter, + dap_chain_datum_t * a_datum, dap_chain_t *a_chain, dap_chain_tx_tag_action_type_t a_action, const char *a_hash_out_type, bool a_out_brief, size_t* a_accepted, size_t* a_rejected, bool a_look_for_unknown_service, const char *a_srv) @@ -691,19 +724,18 @@ static int s_json_tx_history_pack(json_object** a_json_obj_datum, dap_chain_datu dap_hash_fast_t l_ttx_hash = {0}; dap_hash_fast(l_tx, a_datum->header.data_size, &l_ttx_hash); - char *service_name = NULL; + const char *service_name = NULL; dap_chain_tx_tag_action_type_t l_action = DAP_CHAIN_TX_TAG_ACTION_UNKNOWN; - - dap_ledger_t *l_ledger = a_net->pub.ledger; - bool srv_found = dap_ledger_tx_service_info(l_ledger, &l_ttx_hash, NULL, &service_name, &l_action); + //bool srv_found = a_datum_iter->uid.uint64 ? true : false; + l_action = a_datum_iter->action; + service_name = dap_ledger_tx_action_str(l_action); if (!(l_action & a_action)) return 1; if (a_srv) { - char *service_name = NULL; - bool srv_found = dap_ledger_tx_service_info(l_ledger, &l_ttx_hash, NULL, &service_name, NULL); + bool srv_found = a_datum_iter->uid.uint64 ? true : false; //skip if looking for UNKNOWN + it is known if (a_look_for_unknown_service && srv_found) { return 1; @@ -717,7 +749,7 @@ static int s_json_tx_history_pack(json_object** a_json_obj_datum, dap_chain_datu } bool accepted_tx; - *a_json_obj_datum = dap_db_tx_history_to_json(&l_ttx_hash, NULL, l_tx, a_chain, a_hash_out_type, a_net, 0, &accepted_tx, a_out_brief); + *a_json_obj_datum = dap_db_tx_history_to_json(&l_ttx_hash, NULL, l_tx, a_chain, a_hash_out_type, a_datum_iter, 0, &accepted_tx, a_out_brief); if (!*a_json_obj_datum) { log_it(L_CRITICAL, "%s", c_error_memory_alloc); return 2; @@ -741,94 +773,55 @@ json_object *dap_db_history_tx_all(dap_chain_t *a_chain, dap_chain_net_t *a_net, l_tx_ledger_accepted = 0, l_tx_ledger_rejected = 0, l_count = 0, - l_count_tx = 0; - int res = 0; - dap_chain_cell_t *l_cell = NULL, - *l_cell_tmp = NULL; - dap_chain_atom_iter_t *l_iter = NULL; + i_tmp = 0; + int res = 0; json_object * json_arr_out = json_object_new_array(); json_object * json_tx_history = NULL; size_t l_arr_start = 0; size_t l_arr_end = 0; - s_set_offset_limit_json(json_arr_out, &l_arr_start, &l_arr_end, a_limit, a_offset, a_chain->callback_count_atom(a_chain)); + s_set_offset_limit_json(json_arr_out, &l_arr_start, &l_arr_end, a_limit, a_offset, a_chain->callback_count_tx(a_chain)); bool look_for_unknown_service = (a_srv && strcmp(a_srv,"unknown") == 0); - if(a_head) - HASH_ITER(hh, a_chain->cells, l_cell, l_cell_tmp) { - if ((l_count_tx >= l_arr_end)&&(l_arr_end)) - break; - l_iter = a_chain->callback_atom_iter_create(a_chain, l_cell->id, NULL); - size_t l_atom_size = 0; - dap_chain_atom_ptr_t l_ptr = a_chain->callback_atom_iter_get(l_iter, DAP_CHAIN_ITER_OP_FIRST, &l_atom_size); - while (l_ptr && l_atom_size && ((l_count_tx < l_arr_end)||(!l_arr_end))) { - size_t l_datums_count = 0; - dap_chain_datum_t **l_datums = l_cell->chain->callback_atom_get_datums(l_ptr, l_atom_size, &l_datums_count); - for (size_t i = 0; i < l_datums_count && ((l_count_tx < l_arr_end)||(!l_arr_end)); i++) { - if (l_datums[i]->header.type_id == DAP_CHAIN_DATUM_TX) { - if (l_count_tx < l_arr_start) { - l_count_tx++; - continue; - } - res = s_json_tx_history_pack(&json_tx_history, l_datums[i], a_net, a_chain, a_action, a_hash_out_type, out_brief, - &l_tx_ledger_accepted, &l_tx_ledger_rejected, look_for_unknown_service, a_srv); - if (res == 1) - continue; - else if (res == 2) - { - json_object_put(json_arr_out); - return NULL; - } - json_object_object_add(json_tx_history, "tx number", json_object_new_uint64(l_count+1)); - json_object_array_add(json_arr_out, json_tx_history); - ++l_count_tx; - l_count++; - } - } - DAP_DEL_Z(l_datums); - l_ptr = a_chain->callback_atom_iter_get(l_iter, DAP_CHAIN_ITER_OP_NEXT, &l_atom_size); - } - l_cell->chain->callback_atom_iter_delete(l_iter); - } - else + // load transactions + dap_chain_datum_iter_t *l_datum_iter = a_chain->callback_datum_iter_create(a_chain); + + dap_chain_datum_callback_iters iter_begin; + dap_chain_datum_callback_iters iter_direc; + iter_begin = a_head ? a_chain->callback_datum_iter_get_first + : a_chain->callback_datum_iter_get_last; + iter_direc = a_head ? a_chain->callback_datum_iter_get_next + : a_chain->callback_datum_iter_get_prev; + + for (dap_chain_datum_t *l_datum = iter_begin(l_datum_iter); + l_datum; + l_datum = iter_direc(l_datum_iter)) { - l_cell_tmp = HASH_LAST(a_chain->cells); - for(; l_cell_tmp; l_cell_tmp = l_cell_tmp->hh.prev){ - if ((l_count_tx >= l_arr_end)&&(l_arr_end)) - break; - l_iter = a_chain->callback_atom_iter_create(a_chain, l_cell_tmp->id, NULL); - size_t l_atom_size = 0; - dap_chain_atom_ptr_t l_ptr = a_chain->callback_atom_iter_get(l_iter, DAP_CHAIN_ITER_OP_LAST, &l_atom_size); - while (l_ptr && l_atom_size && ((l_count_tx < l_arr_end)||(!l_arr_end))) { - size_t l_datums_count = 0; - dap_chain_datum_t **l_datums = l_cell_tmp->chain->callback_atom_get_datums(l_ptr, l_atom_size, &l_datums_count); - for (size_t i = l_datums_count; i && ((l_count_tx < l_arr_end)||(!l_arr_end)); i--) { - if (l_datums[i-1]->header.type_id == DAP_CHAIN_DATUM_TX) { - if (l_count_tx < l_arr_start) { - l_count_tx++; - continue; - } - res = s_json_tx_history_pack(&json_tx_history, l_datums[i-1], a_net, a_chain, a_action, a_hash_out_type, out_brief, - &l_tx_ledger_accepted, &l_tx_ledger_rejected, look_for_unknown_service, a_srv); - if (res == 1) - continue; - else if (res == 2) - { - json_object_put(json_arr_out); - return NULL; - } - json_object_object_add(json_tx_history, "tx number", json_object_new_uint64(l_count+1)); - json_object_array_add(json_arr_out, json_tx_history); - ++l_count_tx; - l_count++; - } - } - DAP_DEL_Z(l_datums); - l_ptr = a_chain->callback_atom_iter_get(l_iter, DAP_CHAIN_ITER_OP_PREV, &l_atom_size); - } - l_cell_tmp->chain->callback_atom_iter_delete(l_iter); + if (i_tmp >= l_arr_end) + break; + if (l_datum->header.type_id != DAP_CHAIN_DATUM_TX) + // go to next datum + continue; + + if (i_tmp < l_arr_start) { + i_tmp++; + continue; } - } + res = s_json_tx_history_pack(&json_tx_history, l_datum_iter, l_datum, a_chain, a_action, a_hash_out_type, out_brief, + &l_tx_ledger_accepted, &l_tx_ledger_rejected, look_for_unknown_service, a_srv); + if (res == 1) + continue; + else if (res == 2) + { + json_object_put(json_arr_out); + return NULL; + } + json_object_object_add(json_tx_history, "tx number", json_object_new_uint64(l_count+1)); + json_object_array_add(json_arr_out, json_tx_history); + ++i_tmp; + l_count++; + } log_it(L_DEBUG, "END getting tx from chain"); + a_chain->callback_datum_iter_delete(l_datum_iter); json_object_object_add(json_obj_summary, "network", json_object_new_string(a_net->pub.name)); json_object_object_add(json_obj_summary, "chain", json_object_new_string(a_chain->name)); diff --git a/modules/net/include/dap_chain_ledger.h b/modules/net/include/dap_chain_ledger.h index f4c28609100125802617db44e4e26717520edcdf..88bde9717b40dbe9cad1153e0ca45228882317da 100644 --- a/modules/net/include/dap_chain_ledger.h +++ b/modules/net/include/dap_chain_ledger.h @@ -200,6 +200,12 @@ typedef struct dap_ledger_datum_iter { void *cur_ledger_tx_item; } dap_ledger_datum_iter_t; +typedef struct dap_ledger_datum_iter_data { + char token_ticker[DAP_CHAIN_TICKER_SIZE_MAX]; + uint32_t action; + dap_chain_net_srv_uid_t uid; +} dap_ledger_datum_iter_data_t; + typedef int (*dap_ledger_verificator_callback_t)(dap_ledger_t *a_ledger, dap_chain_tx_out_cond_t *a_tx_out_cond, dap_chain_datum_tx_t *a_tx_in, bool a_owner); typedef void (*dap_ledger_updater_callback_t)(dap_ledger_t *a_ledger, dap_chain_datum_tx_t *a_tx_in, dap_hash_fast_t *a_tx_in_hash, dap_chain_tx_out_cond_t *a_prev_cond); typedef void (*dap_ledger_delete_callback_t)(dap_ledger_t *a_ledger, dap_chain_datum_tx_t *a_tx_in, dap_chain_tx_out_cond_t *a_prev_cond); @@ -271,8 +277,8 @@ DAP_STATIC_INLINE char *dap_ledger_get_gdb_group(dap_ledger_t *a_ledger, const c * * return 1 OK, -1 error */ -int dap_ledger_tx_add(dap_ledger_t *a_ledger, dap_chain_datum_tx_t *a_tx, dap_hash_fast_t *a_tx_hash, bool a_from_threshold); -int dap_ledger_tx_load(dap_ledger_t *a_ledger, dap_chain_datum_tx_t *a_tx, dap_chain_hash_fast_t *a_tx_hash); +int dap_ledger_tx_add(dap_ledger_t *a_ledger, dap_chain_datum_tx_t *a_tx, dap_hash_fast_t *a_tx_hash, bool a_from_threshold, dap_ledger_datum_iter_data_t *a_datum_index_data); +int dap_ledger_tx_load(dap_ledger_t *a_ledger, dap_chain_datum_tx_t *a_tx, dap_chain_hash_fast_t *a_tx_hash, dap_ledger_datum_iter_data_t *a_datum_index_data); int dap_ledger_tx_remove(dap_ledger_t *a_ledger, dap_chain_datum_tx_t *a_tx, dap_hash_fast_t *a_tx_hash); int dap_ledger_tx_add_check(dap_ledger_t *a_ledger, dap_chain_datum_tx_t *a_tx, size_t a_datum_size, dap_hash_fast_t *a_datum_hash); diff --git a/modules/net/include/dap_chain_net.h b/modules/net/include/dap_chain_net.h index f27d26cb402fc00c46be179daf56bc49985b80fa..efb10ae09034e3c88c516ac61f973900b9e1760b 100644 --- a/modules/net/include/dap_chain_net.h +++ b/modules/net/include/dap_chain_net.h @@ -54,7 +54,7 @@ typedef enum dap_chain_net_state { NET_STATE_ONLINE } dap_chain_net_state_t; -static const char s_gdb_nodes_postfix[] = ".nodes.list"; +static const char s_gdb_nodes_postfix[] = "nodes.list"; typedef struct dap_chain_net { struct { @@ -194,7 +194,7 @@ void dap_chain_net_srv_order_add_notify_callback(dap_chain_net_t *a_net, dap_sto */ dap_list_t *dap_chain_datum_list(dap_chain_net_t *a_net, dap_chain_t *a_chain, dap_chain_datum_filter_func_t *a_filter_func, void *a_filter_func_param); -int dap_chain_datum_add(dap_chain_t * a_chain, dap_chain_datum_t *a_datum, size_t a_datum_size, dap_hash_fast_t *a_datum_hash); +int dap_chain_datum_add(dap_chain_t * a_chain, dap_chain_datum_t *a_datum, size_t a_datum_size, dap_hash_fast_t *a_datum_hash, void *a_datum_index_data); int dap_chain_datum_remove(dap_chain_t *a_chain, dap_chain_datum_t *a_datum, size_t a_datum_size, dap_hash_fast_t *a_datum_hash); bool dap_chain_net_get_load_mode(dap_chain_net_t * a_net); diff --git a/modules/net/include/dap_chain_node.h b/modules/net/include/dap_chain_node.h index 6aa06cce6f52db16434e987487561e8153b4837a..320748529e2f4747b4e5d5d46f30379baa4b87a4 100644 --- a/modules/net/include/dap_chain_node.h +++ b/modules/net/include/dap_chain_node.h @@ -56,6 +56,7 @@ typedef struct dap_chain_node_info { // using to easy sorting and formin in balancer typedef struct dap_chain_node_states_info { dap_link_info_t link_info; + dap_chain_node_role_t role; uint64_t events_count; uint64_t atoms_count; uint32_t downlinks_count; diff --git a/modules/net/include/dap_chain_node_cli_cmd_tx.h b/modules/net/include/dap_chain_node_cli_cmd_tx.h index be6bb5f251298dcc803e00ba26fad4233f23be79..375f1f34f2474baf60f60e02f6916f11028c1834 100644 --- a/modules/net/include/dap_chain_node_cli_cmd_tx.h +++ b/modules/net/include/dap_chain_node_cli_cmd_tx.h @@ -50,7 +50,7 @@ json_object * dap_db_tx_history_to_json(dap_chain_hash_fast_t* a_tx_hash, dap_chain_datum_tx_t * l_tx, dap_chain_t * a_chain, const char *a_hash_out_type, - dap_chain_net_t * l_net, + dap_chain_datum_iter_t *a_datum_iter, int l_ret_code, bool *accepted_tx, bool out_brief); diff --git a/modules/net/srv/dap_chain_net_srv.c b/modules/net/srv/dap_chain_net_srv.c index 88ca4c3c6e5f89a51e53659adb7875bd82ce1804..4ebb5f9c613db910b18c3c5f0132a26b77f52539 100644 --- a/modules/net/srv/dap_chain_net_srv.c +++ b/modules/net/srv/dap_chain_net_srv.c @@ -775,6 +775,10 @@ static int s_pay_verificator_callback(dap_ledger_t * a_ledger, dap_chain_tx_out_ dap_chain_tx_in_cond_t *l_tx_in_cond = (dap_chain_tx_in_cond_t*)dap_chain_datum_tx_item_get(a_tx_in, NULL, NULL, TX_ITEM_TYPE_IN_COND, NULL); dap_chain_datum_tx_t *l_tx_prev = dap_ledger_tx_find_by_hash(a_ledger , &l_tx_in_cond->header.tx_prev_hash); dap_chain_tx_out_cond_t *l_prev_out_cond = dap_chain_datum_tx_out_cond_get(l_tx_prev, DAP_CHAIN_TX_OUT_COND_SUBTYPE_SRV_PAY, NULL); + if (!l_prev_out_cond) { + log_it(L_ERROR, "Can't find datum tx"); + return -15; + } uint256_t l_unit_price = {}; if (!l_receipt->receipt_info.units) { diff --git a/modules/service/stake/dap_chain_net_srv_stake_lock.c b/modules/service/stake/dap_chain_net_srv_stake_lock.c index 7a5d72bcd9cf9762641eb2dd54eb296efec5856c..84ee2e167286c8cbfe14bdbe29dce38215cf5351 100644 --- a/modules/service/stake/dap_chain_net_srv_stake_lock.c +++ b/modules/service/stake/dap_chain_net_srv_stake_lock.c @@ -1109,6 +1109,8 @@ static void s_stake_lock_callback_updater(dap_ledger_t *a_ledger, dap_chain_datu return; int l_out_num = 0; dap_chain_tx_out_cond_t *l_cond = dap_chain_datum_tx_out_cond_get(a_tx_in, DAP_CHAIN_TX_OUT_COND_SUBTYPE_SRV_STAKE_LOCK, &l_out_num); + if (!l_cond) + return; if (l_cond->subtype.srv_stake_lock.flags & DAP_CHAIN_NET_SRV_STAKE_LOCK_FLAG_CREATE_BASE_TX) dap_ledger_emission_for_stake_lock_item_add(a_ledger, a_tx_in_hash); } diff --git a/modules/service/stake/dap_chain_net_srv_stake_pos_delegate.c b/modules/service/stake/dap_chain_net_srv_stake_pos_delegate.c index 6ae42e529b82481abac1dc6d1753efeff1f19e62..52be033cad1ff4caa897d29d92d3edc187003e85 100644 --- a/modules/service/stake/dap_chain_net_srv_stake_pos_delegate.c +++ b/modules/service/stake/dap_chain_net_srv_stake_pos_delegate.c @@ -464,10 +464,10 @@ void dap_chain_net_srv_stake_key_update(dap_chain_addr_t *a_signing_addr, uint25 if (!l_stake) return; // It's update for non delegated key, it's OK HASH_DELETE(ht, l_srv_stake->tx_itemlist, l_stake); + char *l_old_value_str = dap_chain_balance_to_coins(l_stake->locked_value); l_stake->locked_value = l_stake->value = a_new_value; l_stake->tx_hash = *a_new_tx_hash; HASH_ADD(ht, l_srv_stake->tx_itemlist, tx_hash, sizeof(dap_hash_fast_t), l_stake); - char *l_old_value_str = dap_chain_balance_to_coins(l_stake->locked_value); const char *l_new_value_str; dap_uint256_to_char(a_new_value, &l_new_value_str); log_it(L_NOTICE, "Updated key with fingerprint %s and locked value %s to new locked value %s for node " NODE_ADDR_FP_STR, dap_chain_hash_fast_to_str_static(&a_signing_addr->data.hash_fast), l_old_value_str, @@ -1263,7 +1263,7 @@ static dap_chain_datum_decree_t *s_stake_decree_invalidate(dap_chain_net_t *a_ne dap_tsd_t *l_tsd = NULL; l_total_tsd_size += sizeof(dap_tsd_t) + sizeof(dap_chain_addr_t); - l_tsd = DAP_NEW_Z_SIZE(dap_tsd_t, l_total_tsd_size); + l_tsd = DAP_NEW_Z_SIZE(dap_tsd_t, sizeof(dap_tsd_t) + sizeof(dap_chain_addr_t)); if (!l_tsd) { log_it(L_CRITICAL, "%s", c_error_memory_alloc); return NULL; @@ -1273,6 +1273,17 @@ static dap_chain_datum_decree_t *s_stake_decree_invalidate(dap_chain_net_t *a_ne *(dap_chain_addr_t*)(l_tsd->data) = l_tx_out_cond->subtype.srv_stake_pos_delegate.signing_addr; l_tsd_list = dap_list_append(l_tsd_list, l_tsd); + l_total_tsd_size += sizeof(dap_tsd_t) + sizeof(dap_chain_node_addr_t); + l_tsd = DAP_NEW_Z_SIZE(dap_tsd_t, sizeof(dap_tsd_t) + sizeof(dap_chain_node_addr_t)); + if (!l_tsd) { + log_it(L_CRITICAL, "%s", c_error_memory_alloc); + return NULL; + } + l_tsd->type = DAP_CHAIN_DATUM_DECREE_TSD_TYPE_NODE_ADDR; + l_tsd->size = sizeof(dap_chain_node_addr_t); + *(dap_chain_node_addr_t*)(l_tsd->data) = l_tx_out_cond->subtype.srv_stake_pos_delegate.signer_node_addr; + l_tsd_list = dap_list_append(l_tsd_list, l_tsd); + l_decree = DAP_NEW_Z_SIZE(dap_chain_datum_decree_t, sizeof(dap_chain_datum_decree_t) + l_total_tsd_size); if (!l_decree) { log_it(L_CRITICAL, "%s", c_error_memory_alloc); @@ -1434,10 +1445,20 @@ char *s_staker_order_create(dap_chain_net_t *a_net, uint256_t a_value, dap_hash_ return l_order_hash_str; } +static int time_compare_orders(const void *a, const void *b) { + dap_global_db_obj_t *obj_a = (dap_global_db_obj_t*)a; + dap_global_db_obj_t *obj_b = (dap_global_db_obj_t*)b; + + if (obj_a->timestamp < obj_b->timestamp) return -1; + if (obj_a->timestamp > obj_b->timestamp) return 1; + return 0; +} + static int s_cli_srv_stake_order(int a_argc, char **a_argv, int a_arg_index, void **a_str_reply, const char *a_hash_out_type) { enum { - CMD_NONE, CMD_CREATE_FEE, CMD_CREATE_VALIDATOR, CMD_CREATE_STAKER, CMD_UPDATE, CMD_LIST, CMD_REMOVE + CMD_NONE, CMD_CREATE_FEE, CMD_CREATE_VALIDATOR, CMD_CREATE_STAKER, CMD_UPDATE, CMD_LIST, + CMD_LIST_STAKER, CMD_LIST_VALIDATOR, CMD_LIST_FEE, CMD_REMOVE }; int l_cmd_num = CMD_NONE; const char *l_create_type = NULL; @@ -1762,6 +1783,15 @@ static int s_cli_srv_stake_order(int a_argc, char **a_argv, int a_arg_index, voi dap_cli_server_cmd_set_reply_text(a_str_reply, "Command 'order list' requires parameter -net"); return -3; } + const char * l_list_type = NULL; + int l_list_filter = 0; + if (dap_cli_server_cmd_check_option(a_argv, l_arg_index, dap_min(a_argc, l_arg_index + 1), "staker") >= 0) + l_list_filter = CMD_LIST_STAKER; + else if (dap_cli_server_cmd_check_option(a_argv, l_arg_index, dap_min(a_argc, l_arg_index + 1), "validator") >= 0) + l_list_filter = CMD_LIST_VALIDATOR; + else if (dap_cli_server_cmd_check_option(a_argv, l_arg_index, dap_min(a_argc, l_arg_index + 1), "fee") >= 0) + l_list_filter = CMD_LIST_FEE; + dap_chain_net_t *l_net = dap_chain_net_by_name(l_net_str); if (!l_net) { dap_cli_server_cmd_set_reply_text(a_str_reply, "Network %s not found", l_net_str); @@ -1773,6 +1803,7 @@ static int s_cli_srv_stake_order(int a_argc, char **a_argv, int a_arg_index, voi dap_chain_net_srv_order_get_common_group(l_net); size_t l_orders_count = 0; dap_global_db_obj_t * l_orders = dap_global_db_get_all_sync(l_gdb_group_str, &l_orders_count); + qsort(l_orders, l_orders_count, sizeof(dap_global_db_obj_t), time_compare_orders); for (size_t i = 0; i < l_orders_count; i++) { const dap_chain_net_srv_order_t *l_order = dap_chain_net_srv_order_check(l_orders[i].key, l_orders[i].value, l_orders[i].value_len); if (!l_order) { @@ -1782,6 +1813,23 @@ static int s_cli_srv_stake_order(int a_argc, char **a_argv, int a_arg_index, voi if (l_order->srv_uid.uint64 != DAP_CHAIN_NET_SRV_STAKE_POS_DELEGATE_ID && l_order->srv_uid.uint64 != DAP_CHAIN_NET_SRV_STAKE_POS_DELEGATE_ORDERS) continue; + + switch (l_list_filter) { + case CMD_LIST_STAKER: + if (l_order->srv_uid.uint64 != DAP_CHAIN_NET_SRV_STAKE_POS_DELEGATE_ORDERS || l_order->direction != SERV_DIR_BUY ) + continue; + break; + case CMD_LIST_VALIDATOR: + if (l_order->srv_uid.uint64 != DAP_CHAIN_NET_SRV_STAKE_POS_DELEGATE_ORDERS || l_order->direction != SERV_DIR_SELL) + continue; + break; + case CMD_LIST_FEE: + if (l_order->srv_uid.uint64 != DAP_CHAIN_NET_SRV_STAKE_POS_DELEGATE_ID) + continue; + break; + default: + break; + } // TODO add filters to list (token, address, etc.) dap_string_append(l_reply_str, "\n"); dap_chain_net_srv_order_dump_to_string(l_order, l_reply_str, a_hash_out_type, l_net->pub.native_ticker); diff --git a/modules/service/xchange/dap_chain_net_srv_xchange.c b/modules/service/xchange/dap_chain_net_srv_xchange.c index 7e8c19c190c14370b73f1ad8b1a185f8ffa95cd0..bd3fb71ccbde0dae3e710a756d36264af3a8aac8 100644 --- a/modules/service/xchange/dap_chain_net_srv_xchange.c +++ b/modules/service/xchange/dap_chain_net_srv_xchange.c @@ -1146,6 +1146,8 @@ dap_chain_net_srv_xchange_price_t *s_xchange_price_from_order(dap_chain_net_t *a } l_price->creation_date = a_order->header.ts_created; dap_chain_tx_out_cond_t *l_out_cond = dap_chain_datum_tx_out_cond_get(a_order, DAP_CHAIN_TX_OUT_COND_SUBTYPE_SRV_XCHANGE , NULL); + if (!l_out_cond) + return NULL; strcpy(l_price->token_buy, l_out_cond->subtype.srv_xchange.buy_token); MULT_256_COIN(l_out_cond->header.value, l_out_cond->subtype.srv_xchange.rate, &l_price->datoshi_buy); @@ -1773,7 +1775,6 @@ static bool s_string_append_tx_cond_info( dap_string_t * a_reply_str, tx_opt_status_t a_filter_by_status, bool a_print_prev_hash, bool a_print_status, bool a_print_ts) { - enum{TX_TYPE_NONE, TX_TYPE_ORDER, TX_TYPE_EXCHANGE, TX_TYPE_INVALIDATE}; size_t l_tx_size = dap_chain_datum_tx_get_size(a_tx); dap_hash_fast_t l_tx_hash = {0}; @@ -1891,6 +1892,10 @@ static bool s_string_append_tx_cond_info( dap_string_t * a_reply_str, int l_out_num = l_in_cond->header.tx_out_prev_idx; dap_chain_tx_out_cond_t *l_out_cond = dap_chain_datum_tx_out_cond_get(l_prev_tx, DAP_CHAIN_TX_OUT_COND_SUBTYPE_SRV_XCHANGE, &l_out_num); + if (!l_out_cond) { + log_it(L_ERROR, "Can't find datum tx"); + return false; + } dap_hash_fast_t l_order_hash = dap_ledger_get_first_chain_tx_hash(a_net->pub.ledger, a_tx, l_out_cond); if ( dap_hash_fast_is_blank(&l_order_hash) ) l_order_hash = l_in_cond->header.tx_prev_hash; diff --git a/modules/type/blocks/dap_chain_cs_blocks.c b/modules/type/blocks/dap_chain_cs_blocks.c index a0feca9322aeb6f880bc3fb2371735949987a467..3f03db28c6eac948f4d8513835fcc04922cce7ef 100644 --- a/modules/type/blocks/dap_chain_cs_blocks.c +++ b/modules/type/blocks/dap_chain_cs_blocks.c @@ -42,6 +42,9 @@ typedef struct dap_chain_block_datum_index { time_t ts_added; dap_chain_block_cache_t *block_cache; size_t datum_index; + char token_ticker[DAP_CHAIN_TICKER_SIZE_MAX]; + dap_chain_net_srv_uid_t service_uid; + dap_chain_tx_tag_action_type_t action; UT_hash_handle hh; } dap_chain_block_datum_index_t; @@ -189,7 +192,7 @@ int dap_chain_cs_blocks_init() "block -net <net_name> [-chain <chain_name>] dump <block_hash>\n" "\t\tDump block info\n\n" - "block -net <net_name> [-chain <chain_name>] list [{signed | first_signed}] [-limit] [-offset]" + "block -net <net_name> [-chain <chain_name>] list [{signed | first_signed}] [-limit] [-offset] [-head]" " [-from_hash <block_hash>] [-to_hash <block_hash>] [-from_date <YYMMDD>] [-to_date <YYMMDD>]" " [{-cert <signing_cert_name> | -pkey_hash <signing_cert_pkey_hash>}] [-unspent]]\n" "\t\t List blocks\n\n" @@ -210,11 +213,10 @@ int dap_chain_cs_blocks_init() "Reward for block signs:\n" "block -net <net_name> [-chain <chain_name>] reward set" - " -cert <poa_cert_name> -value <value>\n" + " -poa_cert <poa_cert_name> -value <value>\n" "\t\t Set base reward for sign for one block at one minute\n\n" - "block -net <net_name> [-chain <chain_name>] reward show" - " -cert <poa_cert_name> -value <value>\n" + "block -net <net_name> [-chain <chain_name>] reward show\n" "\t\t Show base reward for sign for one block at one minute\n\n" "block -net <net_name> [-chain <chain_name>] reward collect" @@ -473,7 +475,7 @@ static void s_cli_meta_hex_print(json_object* a_json_obj_out, const char * a_met { int l_len = a_meta->hdr.data_size * 2 + 5; char *l_str = DAP_NEW_STACK_SIZE(char, l_len); - snprintf(l_str, 2, "0x"); + strcpy(l_str, "0x"); dap_bin2hex(l_str + 2, a_meta->data, a_meta->hdr.data_size); json_object_object_add(a_json_obj_out, a_meta_title, json_object_new_string(l_str)); } @@ -530,6 +532,22 @@ static void s_print_autocollect_table(dap_chain_net_t *a_net, json_object *a_jso DAP_DEL_MULTY(l_key, l_val); } + + +static int block_list_sort_by_date(const void *a, const void *b) +{ + struct json_object *obj_a = *(struct json_object **)a; + struct json_object *obj_b = *(struct json_object **)b; + + struct json_object *timestamp_a = json_object_object_get(obj_a, "timestamp"); + struct json_object *timestamp_b = json_object_object_get(obj_b, "timestamp"); + + int64_t time_a = json_object_get_int64(timestamp_a); + int64_t time_b = json_object_get_int64(timestamp_b); + + return time_a - time_b; +} + /** * @brief s_cli_blocks * @param argc @@ -756,7 +774,7 @@ static int s_cli_blocks(int a_argc, char ** a_argv, void **a_str_reply) json_object_object_add(json_obj_meta, "# -", json_object_new_string(l_hexbuf)); int l_len = l_meta->hdr.data_size * 2 + 5; char *l_data_hex = DAP_NEW_STACK_SIZE(char, l_len); - snprintf(l_data_hex, 2, "0x"); + strcpy(l_data_hex, "0x"); dap_bin2hex(l_data_hex + 2, l_meta->data, l_meta->hdr.data_size); json_object_object_add(json_obj_meta, "Data hex - ", json_object_new_string(l_data_hex)); } } @@ -976,14 +994,26 @@ static int s_cli_blocks(int a_argc, char ** a_argv, void **a_str_reply) char l_buf[DAP_TIME_STR_SIZE]; json_object* json_obj_bl_cache = json_object_new_object(); dap_time_to_str_rfc822(l_buf, DAP_TIME_STR_SIZE, l_ts); - json_object_object_add(json_obj_bl_cache, "block",json_object_new_uint64(i_tmp)); + json_object_object_add(json_obj_bl_cache, "#",json_object_new_uint64(i_tmp)); + json_object_object_add(json_obj_bl_cache, "block number",json_object_new_uint64(l_block_cache->block_number)); json_object_object_add(json_obj_bl_cache, "hash",json_object_new_string(l_block_cache->block_hash_str)); + json_object_object_add(json_obj_bl_cache, "timestamp", json_object_new_uint64(l_ts)); json_object_object_add(json_obj_bl_cache, "ts_create",json_object_new_string(l_buf)); json_object_array_add(json_arr_bl_cache_out, json_obj_bl_cache); if (l_to_hash_str && dap_hash_fast_compare(&l_to_hash, &l_block_cache->block_hash)) break; } pthread_rwlock_unlock(&PVT(l_blocks)->rwlock); + //sort by time + json_object_array_sort(json_arr_bl_cache_out, block_list_sort_by_date); + // Remove the timestamp and change block num + size_t l_length = json_object_array_length(json_arr_bl_cache_out); + for (size_t i = 0; i < l_length; i++) { + struct json_object *obj = json_object_array_get_idx(json_arr_bl_cache_out, i); + json_object_object_del(obj, "timestamp"); + if (json_object_object_get_ex(obj, "block", NULL)) + json_object_object_add(obj, "block", json_object_new_uint64(i)); + } json_object_array_add(*json_arr_reply, json_arr_bl_cache_out); char *l_filtered_criteria = "none"; @@ -1181,11 +1211,11 @@ static int s_cli_blocks(int a_argc, char ** a_argv, void **a_str_reply) : dap_chain_mempool_tx_reward_create(l_blocks, l_cert->enc_key, l_addr, l_block_list, l_fee_value, l_hash_out_type); if (l_hash_tx) { json_object* json_obj_out = json_object_new_object(); - char *l_val = dap_strdup_printf(l_val, "TX for %s collection created successfully, hash = %s\n", l_subcmd_str, l_hash_tx); - json_object_object_add(json_obj_out, "status", json_object_new_string(l_val)); + char *l_val = dap_strdup_printf("TX for %s collection created successfully, hash = %s\n", l_subcmd_str, l_hash_tx); + DAP_DELETE(l_hash_tx); + json_object_object_add(json_obj_out, "status", json_object_new_string(l_val ? l_val : "(null)")); DAP_DELETE(l_val); json_object_array_add(*json_arr_reply, json_obj_out); - DAP_DELETE(l_hash_tx); } else { dap_json_rpc_error_add(DAP_CHAIN_NODE_CLI_COM_BLOCK_HASH_ERR, "Can't create %s collect TX\n", l_subcmd_str); @@ -1478,10 +1508,12 @@ static int s_add_atom_datums(dap_chain_cs_blocks_t *a_blocks, dap_chain_block_ca break; } dap_hash_fast_t *l_datum_hash = a_block_cache->datum_hash + i; - int l_res = dap_chain_datum_add(a_blocks->chain, l_datum, l_datum_size, l_datum_hash); + dap_ledger_datum_iter_data_t l_datum_index_data = { .token_ticker = "0", .action = DAP_CHAIN_TX_TAG_ACTION_UNKNOWN , .uid.uint64 = 0 }; + + int l_res = dap_chain_datum_add(a_blocks->chain, l_datum, l_datum_size, l_datum_hash, &l_datum_index_data); l_ret++; if (l_datum->header.type_id == DAP_CHAIN_DATUM_TX) - PVT(a_blocks)->tx_count++; + PVT(a_blocks)->tx_count++; // Save datum hash -> block_hash link in hash table dap_chain_block_datum_index_t *l_datum_index = DAP_NEW_Z(dap_chain_block_datum_index_t); if (!l_datum_index) { @@ -1493,6 +1525,9 @@ static int s_add_atom_datums(dap_chain_cs_blocks_t *a_blocks, dap_chain_block_ca l_datum_index->datum_hash = *l_datum_hash; l_datum_index->ret_code = l_res; l_datum_index->datum_index = i; + l_datum_index->action = l_datum_index_data.action; + l_datum_index->service_uid = l_datum_index_data.uid; + dap_strncpy(l_datum_index->token_ticker, l_datum_index_data.token_ticker, DAP_CHAIN_TICKER_SIZE_MAX); pthread_rwlock_wrlock(&PVT(a_blocks)->datums_rwlock); HASH_ADD(hh, PVT(a_blocks)->datum_index, datum_hash, sizeof(*l_datum_hash), l_datum_index); pthread_rwlock_unlock(&PVT(a_blocks)->datums_rwlock); @@ -2212,12 +2247,18 @@ static void s_datum_iter_fill(dap_chain_datum_iter_t *a_datum_iter, dap_chain_bl a_datum_iter->cur_hash = &a_datum_index->datum_hash; a_datum_iter->cur_atom_hash = &a_datum_index->block_cache->block_hash; a_datum_iter->ret_code = a_datum_index->ret_code; + a_datum_iter->action = a_datum_index->action; + a_datum_iter->uid = a_datum_index->service_uid; + a_datum_iter->token_ticker = dap_strcmp(a_datum_index->token_ticker, "0") ? a_datum_index->token_ticker : NULL; } else { a_datum_iter->cur = NULL; a_datum_iter->cur_hash = NULL; a_datum_iter->cur_atom_hash = NULL; a_datum_iter->cur_size = 0; a_datum_iter->ret_code = 0; + a_datum_iter->token_ticker = NULL; + a_datum_iter->action = 0; + a_datum_iter->uid.uint64 = 0; } debug_if(a_datum_index && !a_datum_index->block_cache->datum, L_ERROR, "Chains was deleted with errors"); } diff --git a/modules/type/dag/dap_chain_cs_dag.c b/modules/type/dag/dap_chain_cs_dag.c index b8d6b9cddb7ded3eb9d4013a45bc17f05572fb6c..f81527ebc8f6906e4f61ca7c40e46ac24502da41 100644 --- a/modules/type/dag/dap_chain_cs_dag.c +++ b/modules/type/dag/dap_chain_cs_dag.c @@ -407,7 +407,7 @@ static int s_dap_chain_add_atom_to_events_table(dap_chain_cs_dag_t *a_dag, dap_c } dap_hash_fast_t l_datum_hash; dap_chain_datum_calc_hash(l_datum, &l_datum_hash); - int l_ret = dap_chain_datum_add(a_dag->chain, l_datum, l_datum_size, &l_datum_hash); + int l_ret = dap_chain_datum_add(a_dag->chain, l_datum, l_datum_size, &l_datum_hash, NULL); if (l_datum->header.type_id == DAP_CHAIN_DATUM_TX) // && l_ret == 0 PVT(a_dag)->tx_count++; a_event_item->datum_hash = l_datum_hash; @@ -458,11 +458,7 @@ static dap_chain_atom_verify_res_t s_chain_callback_atom_add(dap_chain_t * a_cha dap_chain_cs_dag_event_t * l_event = (dap_chain_cs_dag_event_t *) a_atom; dap_chain_hash_fast_t l_event_hash = *a_atom_hash; - if(s_debug_more) { - char l_event_hash_str[DAP_CHAIN_HASH_FAST_STR_SIZE] = { '\0' }; - dap_chain_hash_fast_to_str(&l_event_hash, l_event_hash_str, sizeof(l_event_hash_str)); - log_it(L_DEBUG, "Processing event: %s ... (size %zd)", l_event_hash_str,a_atom_size); - } + debug_if(s_debug_more, L_DEBUG, "Processing event: %s ... (size %zd)", dap_chain_hash_fast_to_str_static(&l_event_hash), a_atom_size); pthread_mutex_lock(&PVT(l_dag)->events_mutex); // check if we already have this event dap_chain_atom_verify_res_t ret = s_dap_chain_check_if_event_is_present(PVT(l_dag)->events, &l_event_hash) || @@ -683,7 +679,6 @@ static bool s_chain_callback_datums_pool_proc(dap_chain_t *a_chain, dap_chain_da * or we have successfully chosen the hash(es) to link with. * No additional conditions required. */ - ++l_dag->round_id; uint64_t l_event_size = 0; dap_chain_cs_dag_event_t * l_event = l_dag->callback_cs_event_create ? l_dag->callback_cs_event_create(l_dag, a_datum, l_hashes, l_hashes_linked, &l_event_size) @@ -710,9 +705,9 @@ static bool s_chain_callback_datums_pool_proc(dap_chain_t *a_chain, dap_chain_da l_res = dap_chain_cs_dag_event_gdb_set(l_dag, l_event_hash_hex_str, l_event, l_event_size, &l_round_item); DAP_DELETE(l_event); log_it(l_res ? L_INFO : L_ERROR, - l_res ? "Event %s placed into new forming round [id %"DAP_UINT64_FORMAT_U"]" - : "Can't add new event %s to new events round [id %"DAP_UINT64_FORMAT_U"]", - l_event_hash_hex_str, l_dag->round_id); + l_res ? "Event %s placed into new forming round" + : "Can't add new event %s to new events round", + l_event_hash_hex_str); return l_res; } @@ -808,12 +803,8 @@ static dap_chain_atom_verify_res_t s_chain_callback_atom_verify(dap_chain_t *a_c HASH_FIND(hh, PVT(l_dag)->events ,l_hash ,sizeof (*l_hash), l_event_search); pthread_mutex_unlock(l_events_mutex); if (l_event_search == NULL) { - if(s_debug_more) { - char l_hash_str[DAP_CHAIN_HASH_FAST_STR_SIZE]; - dap_chain_hash_fast_to_str(l_hash, l_hash_str, sizeof(l_hash_str)); - log_it(L_WARNING, "Hash %s wasn't in hashtable of previously parsed, event %s goes to threshold", - l_hash_str, dap_hash_fast_to_str_static(a_atom_hash)); - } + debug_if(s_debug_more, L_WARNING, "Hash %s wasn't in hashtable of previously parsed, event %s goes to threshold", + dap_hash_fast_to_str_static(l_hash), dap_hash_fast_to_str_static(a_atom_hash)); res = ATOM_MOVE_TO_THRESHOLD; break; } @@ -1297,7 +1288,8 @@ static void s_json_dag_pack_event(json_object * a_json_out, dap_chain_cs_dag_eve json_object * json_obj_event_i = json_object_new_object(); char buf[DAP_TIME_STR_SIZE]; dap_time_to_str_rfc822(buf, DAP_TIME_STR_SIZE, a_event_item->event->header.ts_created); - json_object_object_add(json_obj_event_i, "event", json_object_new_string(dap_itoa(i))); + json_object_object_add(json_obj_event_i, "#", json_object_new_string(dap_itoa(i))); + json_object_object_add(json_obj_event_i, "event number", json_object_new_uint64(a_event_item->event_number)); json_object_object_add(json_obj_event_i, "hash", json_object_new_string(dap_chain_hash_fast_to_str_static(&a_event_item->hash))); json_object_object_add(json_obj_event_i, "ts_create", json_object_new_string(buf)); json_object_array_add(a_json_out, json_obj_event_i); diff --git a/modules/type/dag/dap_chain_cs_dag_event.c b/modules/type/dag/dap_chain_cs_dag_event.c index f89726a8330a77a48eb7ff8b83812c22ea743670..c9d769f044cff34697e19f80266e5d095de84779 100644 --- a/modules/type/dag/dap_chain_cs_dag_event.c +++ b/modules/type/dag/dap_chain_cs_dag_event.c @@ -43,9 +43,9 @@ * @param a_hashes_count * @return */ -dap_chain_cs_dag_event_t *dap_chain_cs_dag_event_new(dap_chain_id_t a_chain_id, dap_chain_cell_id_t a_cell_id, uint64_t a_round_id, - dap_chain_datum_t *a_datum, dap_enc_key_t *a_key, - dap_chain_hash_fast_t *a_hashes, size_t a_hashes_count, size_t *a_event_size) +dap_chain_cs_dag_event_t *dap_chain_cs_dag_event_new(dap_chain_id_t a_chain_id, dap_chain_cell_id_t a_cell_id, + dap_chain_datum_t *a_datum, dap_enc_key_t *a_key, dap_chain_hash_fast_t *a_hashes, + size_t a_hashes_count, size_t *a_event_size) { assert(a_event_size); size_t l_hashes_size = sizeof(*a_hashes) * a_hashes_count, @@ -53,7 +53,7 @@ dap_chain_cs_dag_event_t *dap_chain_cs_dag_event_new(dap_chain_id_t a_chain_id, l_event_size = sizeof(dap_chain_class_dag_event_hdr_t) + l_hashes_size + l_datum_size; dap_chain_cs_dag_event_t *l_event_new = DAP_NEW_Z_SIZE(dap_chain_cs_dag_event_t, l_event_size); *l_event_new = (dap_chain_cs_dag_event_t) { - { // .round_id = a_round_id, + { .ts_created = dap_time_now(), .chain_id = a_chain_id, .cell_id = a_cell_id, @@ -149,22 +149,18 @@ size_t dap_chain_cs_dag_event_sign_add(dap_chain_cs_dag_event_t **a_event_ptr, s if (dap_chain_cs_dag_event_sign_exists(l_event, a_event_size, a_key)) { size_t l_pub_key_size = 0; uint8_t *l_pub_key = dap_enc_key_serialize_pub_key(a_key, &l_pub_key_size); - dap_hash_fast_t l_pkey_hash = {}; - dap_hash_fast(l_pub_key, l_pub_key_size, &l_pkey_hash); - DAP_DELETE(l_pub_key); - char l_hash_str[DAP_CHAIN_HASH_FAST_STR_SIZE]; - dap_hash_fast_to_str(&l_pkey_hash, l_hash_str, DAP_CHAIN_HASH_FAST_STR_SIZE); - log_it(L_DEBUG, "Sign from this key exists: %s", l_hash_str); - return 0; + return log_it(L_DEBUG, "Already signed with pkey %s", dap_get_data_hash_str(l_pub_key, l_pub_key_size).s), DAP_DELETE(l_pub_key), 0; } size_t l_event_size_excl_sign = dap_chain_cs_dag_event_calc_size_excl_signs(l_event, a_event_size); dap_sign_t *l_sign = dap_sign_create(a_key, l_event, l_event_size_excl_sign, 0); size_t l_sign_size = dap_sign_get_size(l_sign); - *a_event_ptr = l_event = DAP_REALLOC(l_event, a_event_size + l_sign_size); + if (! (l_event = DAP_REALLOC(*a_event_ptr, a_event_size + l_sign_size) )) + return log_it(L_CRITICAL, "Memory allocation error"), DAP_DELETE(l_sign), a_event_size; size_t l_event_size = a_event_size - sizeof(l_event->header); memcpy(l_event->hashes_n_datum_n_signs + l_event_size, l_sign, l_sign_size); l_event->header.signs_count++; DAP_DELETE(l_sign); + *a_event_ptr = l_event; return a_event_size + l_sign_size; } @@ -192,18 +188,18 @@ static bool s_sign_exists(uint8_t *a_pos, size_t a_len, dap_enc_key_t *a_key) bool dap_chain_cs_dag_event_sign_exists(dap_chain_cs_dag_event_t *a_event, size_t a_event_size, dap_enc_key_t *a_key) { - size_t l_hashes_size = a_event->header.hash_count*sizeof(dap_chain_hash_fast_t); - dap_chain_datum_t * l_datum = (dap_chain_datum_t*)(a_event->hashes_n_datum_n_signs + l_hashes_size); - size_t l_datum_size = dap_chain_datum_size(l_datum); - uint8_t *l_offset = a_event->hashes_n_datum_n_signs + l_hashes_size + l_datum_size; - size_t l_signs_size = a_event_size - sizeof(a_event->header) - l_hashes_size - l_datum_size; - return s_sign_exists(l_offset, l_signs_size, a_key); + size_t l_hashes_size = a_event->header.hash_count * sizeof(dap_chain_hash_fast_t); + dap_chain_datum_t *l_datum = (dap_chain_datum_t*)(a_event->hashes_n_datum_n_signs + l_hashes_size); + size_t l_datum_size = dap_chain_datum_size(l_datum); + return s_sign_exists(a_event->hashes_n_datum_n_signs + l_hashes_size + l_datum_size, + a_event_size - sizeof(a_event->header) - l_hashes_size - l_datum_size, + a_key); } bool dap_chain_cs_dag_event_round_sign_exists(dap_chain_cs_dag_event_round_item_t *a_round_item, dap_enc_key_t *a_key) { - uint8_t *l_offset = a_round_item->event_n_signs + (size_t)a_round_item->event_size; - size_t l_signs_size = (size_t)a_round_item->data_size - (size_t)a_round_item->event_size; - return s_sign_exists(l_offset, l_signs_size, a_key); + return s_sign_exists(a_round_item->event_n_signs + a_round_item->event_size, + (size_t)(a_round_item->data_size - a_round_item->event_size), + a_key); } /** @@ -243,12 +239,14 @@ size_t dap_chain_cs_dag_event_round_sign_add(dap_chain_cs_dag_event_round_item_t return 0; dap_sign_t * l_sign = dap_sign_create(a_key, &l_round_item->round_info.datum_hash, sizeof(dap_chain_hash_fast_t), 0); size_t l_sign_size = dap_sign_get_size(l_sign); - size_t l_offset = (size_t)l_round_item->data_size; - *a_round_item_ptr = l_round_item = DAP_REALLOC(l_round_item, a_round_item_size+l_sign_size); - memcpy(l_round_item->event_n_signs+l_offset, l_sign, l_sign_size); + size_t l_offset = l_round_item->data_size; + if (! (l_round_item = DAP_REALLOC(*a_round_item_ptr, a_round_item_size + l_sign_size)) ) + return log_it(L_CRITICAL, "Memory allocaton error"), DAP_DELETE(l_sign), a_round_item_size; + memcpy(l_round_item->event_n_signs + l_offset, l_sign, l_sign_size); DAP_DELETE(l_sign); l_round_item->data_size += (uint32_t)l_sign_size; - return a_round_item_size+l_sign_size; + *a_round_item_ptr = l_round_item; + return a_round_item_size + l_sign_size; } /** diff --git a/modules/type/dag/include/dap_chain_cs_dag.h b/modules/type/dag/include/dap_chain_cs_dag.h index 42fc9e30ddd3f523e484e0626216b18493da7934..f28f7baf3c6e91287fe49be29591cd1d4a99bde8 100644 --- a/modules/type/dag/include/dap_chain_cs_dag.h +++ b/modules/type/dag/include/dap_chain_cs_dag.h @@ -61,8 +61,6 @@ typedef struct dap_chain_cs_dag dap_chain_hash_fast_t static_genesis_event_hash; dap_chain_cs_dag_hal_item_t *hal; - atomic_uint_fast64_t round_id; - uint16_t datum_add_hashes_count; char * gdb_group_events_round_new; diff --git a/modules/type/dag/include/dap_chain_cs_dag_event.h b/modules/type/dag/include/dap_chain_cs_dag_event.h index 4de1d9055b8eca61d05ca961d6fd6cfcd5a003cc..b56f5ae9b36aa274a0989c0377fb276051cabb9d 100644 --- a/modules/type/dag/include/dap_chain_cs_dag_event.h +++ b/modules/type/dag/include/dap_chain_cs_dag_event.h @@ -60,9 +60,8 @@ typedef struct dap_chain_cs_dag_event_round_item { uint8_t event_n_signs[]; // event // dap_chain_cs_dag_event_t } DAP_ALIGN_PACKED dap_chain_cs_dag_event_round_item_t; -dap_chain_cs_dag_event_t *dap_chain_cs_dag_event_new(dap_chain_id_t a_chain_id, dap_chain_cell_id_t a_cell_id, uint64_t a_round_id, - dap_chain_datum_t *a_datum, dap_enc_key_t *a_key, - dap_chain_hash_fast_t *a_hashes, size_t a_hashes_count, size_t *a_event_size); +dap_chain_cs_dag_event_t *dap_chain_cs_dag_event_new(dap_chain_id_t a_chain_id, dap_chain_cell_id_t a_cell_id, dap_chain_datum_t *a_datum, + dap_enc_key_t *a_key, dap_chain_hash_fast_t *a_hashes, size_t a_hashes_count, size_t *a_event_size); /** * @brief dap_chain_cs_dag_event_get_datum diff --git a/modules/type/none/dap_chain_cs_none.c b/modules/type/none/dap_chain_cs_none.c index 25124d12a4636f168a753953f34e0700ae9dbd40..046eeef125ac5167411d904dd0247bb6f8d23145 100644 --- a/modules/type/none/dap_chain_cs_none.c +++ b/modules/type/none/dap_chain_cs_none.c @@ -318,7 +318,7 @@ static dap_chain_atom_verify_res_t s_nonconsensus_callback_atom_add(dap_chain_t dap_nonconsensus_private_t *l_nochain_priv = PVT(l_nochain); dap_chain_datum_t *l_datum = (dap_chain_datum_t*) a_atom; dap_hash_fast_t l_datum_hash = *a_atom_hash; - if(dap_chain_datum_add(a_chain, l_datum, a_atom_size, &l_datum_hash)) + if(dap_chain_datum_add(a_chain, l_datum, a_atom_size, &l_datum_hash, NULL)) return ATOM_REJECT; dap_nonconsensus_datum_hash_item_t * l_hash_item = DAP_NEW_Z(dap_nonconsensus_datum_hash_item_t); diff --git a/modules/wallet/dap_chain_wallet.c b/modules/wallet/dap_chain_wallet.c index 38986922299ee3aa5c5bd9cd36dcbe8da9f6e7f3..0797c8b6f19f3f536833fa7461633174e55108b9 100644 --- a/modules/wallet/dap_chain_wallet.c +++ b/modules/wallet/dap_chain_wallet.c @@ -169,6 +169,12 @@ char *c_wallets_path; memset(l_prec->pass, 0, l_prec->pass_len), l_prec->pass_len = 0; /* Say <what> again ?! */ return log_it(L_ERROR, "Wallet's password is invalid, say <password> again"), -EAGAIN; } + if(!(l_wallet->flags & DAP_WALLET$M_FL_ACTIVE)) { + HASH_FIND_STR(s_wallet_n_pass, a_name, l_prec); + HASH_DEL(s_wallet_n_pass, l_prec); + log_it(L_ERROR, "Can't activate unprotected wallet"); + l_rc = -101; + } dap_chain_wallet_close( l_wallet);