diff --git a/modules/net/dap_chain_ledger.c b/modules/net/dap_chain_ledger.c index 8c2cfff4169561203b8c2ec0d0c69aeb536ae0c9..a2619ebacf4eebbb6edd98e435eb2bf9104f8922 100644 --- a/modules/net/dap_chain_ledger.c +++ b/modules/net/dap_chain_ledger.c @@ -1803,11 +1803,17 @@ json_object *dap_ledger_threshold_info(dap_ledger_t *a_ledger, size_t a_limit, s uint32_t l_counter = 0; pthread_rwlock_rdlock(&l_ledger_pvt->threshold_txs_rwlock); size_t l_arr_start = 0; - if (a_offset > 1) { - l_arr_start = a_limit * a_offset; + if (a_offset > 0) { + l_arr_start = a_offset; + json_object* json_obj_tx = json_object_new_object(); + json_object_object_add(json_obj_tx, "offset", json_object_new_int(l_arr_start)); + json_object_array_add(json_arr_out, json_obj_tx); } size_t l_arr_end = HASH_COUNT(l_ledger_pvt->threshold_txs); if (a_limit) { + json_object* json_obj_tx = json_object_new_object(); + json_object_object_add(json_obj_tx, "limit", json_object_new_int(a_limit)); + json_object_array_add(json_arr_out, json_obj_tx); l_arr_end = l_arr_start + a_limit; if (l_arr_end > HASH_COUNT(l_ledger_pvt->threshold_txs)) { l_arr_end = HASH_COUNT(l_ledger_pvt->threshold_txs); @@ -1815,7 +1821,7 @@ json_object *dap_ledger_threshold_info(dap_ledger_t *a_ledger, size_t a_limit, s } size_t i_tmp = 0; HASH_ITER(hh, l_ledger_pvt->threshold_txs, l_tx_item, l_tx_tmp){ - if (i_tmp < l_arr_start || i_tmp > l_arr_end) { + if (i_tmp < l_arr_start || i_tmp >= l_arr_end) { i_tmp++; continue; } @@ -1875,11 +1881,17 @@ json_object *dap_ledger_threshold_hash_info(dap_ledger_t *a_ledger, dap_chain_ha return NULL; } size_t l_arr_start = 0; - if (a_offset > 1) { - l_arr_start = a_limit * a_offset; + if (a_offset > 0) { + l_arr_start = a_offset; + json_object* json_obj_tx = json_object_new_object(); + json_object_object_add(json_obj_tx, "offset", json_object_new_int(l_arr_start)); + json_object_array_add(json_arr_out, json_obj_tx); } size_t l_arr_end = HASH_COUNT(l_ledger_pvt->threshold_txs); if (a_limit) { + json_object* json_obj_tx = json_object_new_object(); + json_object_object_add(json_obj_tx, "limit", json_object_new_int(l_arr_start)); + json_object_array_add(json_arr_out, json_obj_tx); l_arr_end = l_arr_start + a_limit; if (l_arr_end > HASH_COUNT(l_ledger_pvt->threshold_txs)) { l_arr_end = HASH_COUNT(l_ledger_pvt->threshold_txs); @@ -1889,7 +1901,7 @@ json_object *dap_ledger_threshold_hash_info(dap_ledger_t *a_ledger, dap_chain_ha pthread_rwlock_rdlock(&l_ledger_pvt->threshold_txs_rwlock); HASH_ITER(hh, l_ledger_pvt->threshold_txs, l_tx_item, l_tx_tmp){ if (!memcmp(l_threshold_hash, &l_tx_item->tx_hash_fast, sizeof(dap_chain_hash_fast_t))){ - if (i_tmp < l_arr_start || i_tmp > l_arr_end) { + if (i_tmp < l_arr_start || i_tmp >= l_arr_end) { i_tmp++; continue; } @@ -1908,7 +1920,7 @@ json_object *dap_ledger_threshold_hash_info(dap_ledger_t *a_ledger, dap_chain_ha dap_ledger_token_emission_item_t *l_emission_item, *l_emission_tmp; HASH_ITER(hh, l_ledger_pvt->threshold_emissions, l_emission_item, l_emission_tmp){ if (!memcmp(&l_emission_item->datum_token_emission_hash,l_threshold_hash, sizeof(dap_chain_hash_fast_t))){ - if (i_tmp < l_arr_start || i_tmp > l_arr_end) { + if (i_tmp < l_arr_start || i_tmp >= l_arr_end) { i_tmp++; continue; } @@ -1936,10 +1948,16 @@ json_object *dap_ledger_balance_info(dap_ledger_t *a_ledger, size_t a_limit, siz dap_ledger_wallet_balance_t *l_balance_item, *l_balance_tmp; size_t l_arr_start = 0; if (a_offset > 1) { - l_arr_start = a_limit * a_offset; + l_arr_start = a_offset; + json_object* json_obj_tx = json_object_new_object(); + json_object_object_add(json_obj_tx, "offset", json_object_new_int(l_arr_start)); + json_object_array_add(json_arr_out, json_obj_tx); } size_t l_arr_end = HASH_COUNT(l_ledger_pvt->balance_accounts); if (a_limit) { + json_object* json_obj_tx = json_object_new_object(); + json_object_object_add(json_obj_tx, "limit", json_object_new_int(l_arr_start)); + json_object_array_add(json_arr_out, json_obj_tx); l_arr_end = l_arr_start + a_limit; if (l_arr_end > HASH_COUNT(l_ledger_pvt->balance_accounts)) { l_arr_end = HASH_COUNT(l_ledger_pvt->balance_accounts); @@ -1947,7 +1965,7 @@ json_object *dap_ledger_balance_info(dap_ledger_t *a_ledger, size_t a_limit, siz } size_t i_tmp = 0; HASH_ITER(hh, l_ledger_pvt->balance_accounts, l_balance_item, l_balance_tmp) { - if (i_tmp < l_arr_start || i_tmp > l_arr_end) { + if (i_tmp < l_arr_start || i_tmp >= l_arr_end) { i_tmp++; continue; } @@ -2048,11 +2066,17 @@ json_object *dap_ledger_token_info(dap_ledger_t *a_ledger, size_t a_limit, size_ dap_ledger_token_item_t *l_token_item, *l_tmp_item; pthread_rwlock_rdlock(&PVT(a_ledger)->tokens_rwlock); size_t l_arr_start = 0; - if (a_offset > 1) { - l_arr_start = a_limit * a_offset; + if (a_offset > 0) { + l_arr_start = a_offset; + json_object* json_obj_tx = json_object_new_object(); + json_object_object_add(json_obj_tx, "offset", json_object_new_int(l_arr_start)); + json_object_array_add(json_arr_out, json_obj_tx); } size_t l_arr_end = HASH_COUNT(PVT(a_ledger)->tokens); if (a_limit) { + json_object* json_obj_tx = json_object_new_object(); + json_object_object_add(json_obj_tx, "limit", json_object_new_int(a_limit)); + json_object_array_add(json_arr_out, json_obj_tx); l_arr_end = l_arr_start + a_limit; if (l_arr_end > HASH_COUNT(PVT(a_ledger)->tokens)) { l_arr_end = HASH_COUNT(PVT(a_ledger)->tokens); @@ -2060,7 +2084,7 @@ json_object *dap_ledger_token_info(dap_ledger_t *a_ledger, size_t a_limit, size_ } size_t i_tmp = 0; HASH_ITER(hh, PVT(a_ledger)->tokens, l_token_item, l_tmp_item) { - if (i_tmp < l_arr_start || i_tmp > l_arr_end) { + if (i_tmp < l_arr_start || i_tmp >= l_arr_end) { i_tmp++; continue; } diff --git a/modules/net/dap_chain_node_cli_cmd.c b/modules/net/dap_chain_node_cli_cmd.c index 6360852ff16532083479f5c094793cad84171d0b..f4ac997c1d5ac57d035775445470c111d9e248b8 100644 --- a/modules/net/dap_chain_node_cli_cmd.c +++ b/modules/net/dap_chain_node_cli_cmd.c @@ -2623,7 +2623,7 @@ void s_com_mempool_list_print_for_chain(dap_chain_net_t * a_net, dap_chain_t * a size_t l_objs_count = 0; dap_global_db_obj_t * l_objs = dap_global_db_get_all_sync(l_gdb_group_mempool, &l_objs_count); json_object *l_jobj_datums; - size_t l_offset = a_limit * a_offset; + size_t l_offset = a_offset; if (l_objs_count == 0 || l_objs_count < l_offset) { l_jobj_datums = json_object_new_null(); } else { @@ -3683,7 +3683,8 @@ int com_mempool(int a_argc, char **a_argv, void **a_str_reply) int arg_index = 1; dap_chain_net_t *l_net = NULL; dap_chain_t *l_chain = NULL; - enum _subcmd {SUBCMD_LIST, SUBCMD_PROC, SUBCMD_PROC_ALL, SUBCMD_DELETE, SUBCMD_ADD_CA, SUBCMD_CHECK, SUBCMD_DUMP}; + enum _subcmd {SUBCMD_LIST, SUBCMD_PROC, SUBCMD_PROC_ALL, SUBCMD_DELETE, SUBCMD_ADD_CA, SUBCMD_CHECK, SUBCMD_DUMP, + SUBCMD_COUNT}; enum _subcmd l_cmd = 0; if (a_argv[1]) { if (!dap_strcmp(a_argv[1], "list")) { @@ -3700,8 +3701,10 @@ int com_mempool(int a_argc, char **a_argv, void **a_str_reply) l_cmd = SUBCMD_DUMP; } else if (!dap_strcmp(a_argv[1], "check")) { l_cmd = SUBCMD_CHECK; + } else if (!dap_strcmp(a_argv[1], "count")) { + l_cmd = SUBCMD_COUNT; } else { - char *l_str_err = dap_strdup_printf("Invalid sub command specified. Ыub command %s " + char *l_str_err = dap_strdup_printf("Invalid sub command specified. Sub command %s " "is not supported.", a_argv[1]); if (!l_str_err) { dap_json_rpc_allocation_error; @@ -3771,7 +3774,7 @@ int com_mempool(int a_argc, char **a_argv, void **a_str_reply) const char *l_limit_str = NULL, *l_offset_str = NULL; dap_cli_server_cmd_find_option_val(a_argv, arg_index, a_argc, "-limit", &l_limit_str); dap_cli_server_cmd_find_option_val(a_argv, arg_index, a_argc, "-offset", &l_offset_str); - l_limit = l_limit_str ? strtoul(l_limit_str, NULL, 10) : 0; + l_limit = l_limit_str ? strtoul(l_limit_str, NULL, 10) : 1000; l_offset = l_offset_str ? strtoul(l_offset_str, NULL, 10) : 0; if(l_chain) { s_com_mempool_list_print_for_chain(l_net, l_chain, l_wallet_addr, l_jobj_chains, l_hash_out_type, l_fast, l_limit, l_offset); @@ -3823,6 +3826,72 @@ int com_mempool(int a_argc, char **a_argv, void **a_str_reply) case SUBCMD_DUMP: { ret = _cmd_mempool_dump(l_net, l_chain, l_datum_hash, l_hash_out_type, a_json_reply); } break; + case SUBCMD_COUNT: { + char *l_mempool_group; + json_object *obj_ret = json_object_new_object(); + json_object *obj_net = json_object_new_string(l_net->pub.name); + if (!obj_ret || !obj_net) { + json_object_put(obj_ret); + json_object_put(obj_net); + dap_json_rpc_allocation_error; + return DAP_JSON_RPC_ERR_CODE_MEMORY_ALLOCATED; + } + json_object_object_add(obj_ret, "net", obj_net); + json_object *l_jobj_chains = json_object_new_array(); + if (!l_jobj_chains) { + json_object_put(obj_ret); + dap_json_rpc_allocation_error; + return DAP_JSON_RPC_ERR_CODE_MEMORY_ALLOCATED; + } + if(l_chain) { + l_mempool_group = dap_chain_net_get_gdb_group_mempool_new(l_chain); + size_t l_objs_count = 0; + dap_global_db_obj_t *l_objs = dap_global_db_get_all_sync(l_mempool_group, &l_objs_count); + dap_global_db_objs_delete(l_objs, l_objs_count); + DAP_DELETE(l_mempool_group); + json_object *l_jobj_chain = json_object_new_object(); + json_object *l_jobj_chain_name = json_object_new_string(l_chain->name); + json_object *l_jobj_count = json_object_new_uint64(l_objs_count); + if (!l_jobj_chain || !l_jobj_chain_name || !l_jobj_count) { + json_object_put(l_jobj_chains); + json_object_put(l_jobj_chain); + json_object_put(l_jobj_chain_name); + json_object_put(l_jobj_count); + json_object_put(obj_ret); + dap_json_rpc_allocation_error; + return DAP_JSON_RPC_ERR_CODE_MEMORY_ALLOCATED; + } + json_object_object_add(l_jobj_chain, "name", l_jobj_chain_name); + json_object_object_add(l_jobj_chain, "count", l_jobj_count); + json_object_array_add(l_jobj_chains, l_jobj_chain); + } else { + DL_FOREACH(l_net->pub.chains, l_chain) { + l_mempool_group = dap_chain_net_get_gdb_group_mempool_new(l_chain); + size_t l_objs_count = 0; + dap_global_db_obj_t *l_objs = dap_global_db_get_all_sync(l_mempool_group, &l_objs_count); + dap_global_db_objs_delete(l_objs, l_objs_count); + DAP_DELETE(l_mempool_group); + json_object *l_jobj_chain = json_object_new_object(); + json_object *l_jobj_chain_name = json_object_new_string(l_chain->name); + json_object *l_jobj_count = json_object_new_uint64(l_objs_count); + if (!l_jobj_chain || !l_jobj_chain_name || !l_jobj_count) { + json_object_put(l_jobj_chains); + json_object_put(l_jobj_chain); + json_object_put(l_jobj_chain_name); + json_object_put(l_jobj_count); + json_object_put(obj_ret); + dap_json_rpc_allocation_error; + return DAP_JSON_RPC_ERR_CODE_MEMORY_ALLOCATED; + } + json_object_object_add(l_jobj_chain, "name", l_jobj_chain_name); + json_object_object_add(l_jobj_chain, "count", l_jobj_count); + json_object_array_add(l_jobj_chains, l_jobj_chain); + } + } + json_object_object_add(obj_ret, "chains", l_jobj_chains); + json_object_array_add(*a_json_reply, obj_ret); + ret = 0; + } break; } DAP_DEL_Z(l_datum_hash); return ret; @@ -7121,17 +7190,24 @@ int com_tx_history(int a_argc, char ** a_argv, void **a_str_reply) } } else if (l_addr) { // history addr and wallet + json_object * json_obj_summary = json_object_new_object(); + if (!json_obj_summary) { + return DAP_CHAIN_NODE_CLI_COM_TX_HISTORY_MEMORY_ERR; + } dap_cli_server_cmd_find_option_val(a_argv, arg_index, a_argc, "-limit", &l_limit_str); dap_cli_server_cmd_find_option_val(a_argv, arg_index, a_argc, "-offset", &l_offset_str); - size_t l_limit = l_limit_str ? strtoul(l_limit_str, NULL, 10) : 0; + size_t l_limit = l_limit_str ? strtoul(l_limit_str, NULL, 10) : 1000; size_t l_offset = l_offset_str ? strtoul(l_offset_str, NULL, 10) : 0; - json_obj_out = dap_db_history_addr(l_addr, l_chain, l_hash_out_type, dap_chain_addr_to_str(l_addr), l_limit, l_offset); + json_obj_out = dap_db_history_addr(l_addr, l_chain, l_hash_out_type, dap_chain_addr_to_str(l_addr), json_obj_summary, l_limit, l_offset); if (!json_obj_out) { dap_json_rpc_error_add(DAP_CHAIN_NODE_CLI_COM_TX_HISTORY_DAP_DB_HISTORY_ADDR_ERR, "something went wrong in tx_history"); + json_object_put(json_obj_summary); return DAP_CHAIN_NODE_CLI_COM_TX_HISTORY_DAP_DB_HISTORY_ADDR_ERR; - } - + } + json_object_array_add(*json_arr_reply, json_obj_out); + json_object_array_add(*json_arr_reply, json_obj_summary); + return DAP_CHAIN_NODE_CLI_COM_TX_HISTORY_OK; } else if (l_is_tx_all) { // history all const char * l_brief_type = NULL; @@ -7146,7 +7222,7 @@ int com_tx_history(int a_argc, char ** a_argv, void **a_str_reply) } dap_cli_server_cmd_find_option_val(a_argv, arg_index, a_argc, "-limit", &l_limit_str); dap_cli_server_cmd_find_option_val(a_argv, arg_index, a_argc, "-offset", &l_offset_str); - size_t l_limit = l_limit_str ? strtoul(l_limit_str, NULL, 10) : 0; + size_t l_limit = l_limit_str ? strtoul(l_limit_str, NULL, 10) : 1000; size_t l_offset = l_offset_str ? strtoul(l_offset_str, NULL, 10) : 0; json_object* json_arr_history_all = dap_db_history_tx_all(l_chain, l_net, l_hash_out_type, json_obj_summary, l_limit, l_offset, l_brief_out); diff --git a/modules/net/dap_chain_node_cli_cmd_tx.c b/modules/net/dap_chain_node_cli_cmd_tx.c index 15e225603ead94df793fc3cb326f20b8a15aad3e..9f8eeb191e0c4bb15ae220501fc0eb4999e758ab 100644 --- a/modules/net/dap_chain_node_cli_cmd_tx.c +++ b/modules/net/dap_chain_node_cli_cmd_tx.c @@ -295,7 +295,8 @@ static void s_tx_header_print(json_object* json_obj_datum, dap_chain_tx_hash_pro * @return char* */ json_object* dap_db_history_addr(dap_chain_addr_t *a_addr, dap_chain_t *a_chain, - const char *a_hash_out_type, const char * l_addr_str, size_t a_limit, size_t a_offset) + const char *a_hash_out_type, const char * l_addr_str, json_object *json_obj_summary, + size_t a_limit, size_t a_offset) { json_object* json_obj_datum = json_object_new_array(); if (!json_obj_datum){ @@ -329,23 +330,38 @@ 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; - uint256_t l_corr_value = {}, l_unstake_value = {}; - json_object *l_corr_object = NULL; - - size_t - l_count = 0, - l_count_tx = 0; + uint256_t l_corr_value = {}, l_unstake_value = {}; + + size_t l_arr_start = 0; + if (a_offset){ + l_arr_start = a_offset; + json_object* json_obj_off = json_object_new_object(); + json_object_object_add(json_obj_off, "offset", json_object_new_int(l_arr_start)); + json_object_array_add(json_obj_datum, json_obj_off); + } + size_t l_arr_end = a_chain->callback_count_atom(a_chain); + if (a_limit) { + json_object* json_obj_lim = json_object_new_object(); + json_object_object_add(json_obj_lim, "limit", json_object_new_int(a_limit)); + json_object_array_add(json_obj_datum, json_obj_lim); + l_arr_end = l_arr_start + a_limit; + size_t l_length = a_chain->callback_count_atom(a_chain); + if (l_arr_end > l_length) + l_arr_end = l_length; + } + size_t i_tmp = 0; // load transactions dap_chain_datum_iter_t *l_datum_iter = a_chain->callback_datum_iter_create(a_chain); for (dap_chain_datum_t *l_datum = a_chain->callback_datum_iter_get_first(l_datum_iter); - l_datum && (a_limit ? l_count_tx < a_limit : true); + l_datum; l_datum = a_chain->callback_datum_iter_get_next(l_datum_iter)) { + 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 + // it's a transaction bool l_is_unstake = false; 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); @@ -421,7 +437,8 @@ json_object* dap_db_history_addr(dap_chain_addr_t *a_addr, dap_chain_t *a_chain, } if (l_src_addr && !dap_chain_addr_compare(l_src_addr, a_addr)) break; //it's not our addr - } + + } dap_list_free(l_list_in_items); // find OUT items @@ -454,7 +471,12 @@ 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); + json_object_put(j_obj_tx); + dap_list_free(l_list_out_items); continue; + } } bool l_count_bool = false; for (dap_list_t *it = l_list_out_items; it; it = it->next) { @@ -492,8 +514,6 @@ json_object* dap_db_history_addr(dap_chain_addr_t *a_addr, dap_chain_t *a_chain, if (l_dst_addr && l_net_fee_used && dap_chain_addr_compare(&l_net_fee_addr, l_dst_addr)) SUM_256_256(l_fee_sum, l_value, &l_fee_sum); if (l_dst_addr && dap_chain_addr_compare(l_dst_addr, a_addr)) { - if (l_count++ < a_offset) - 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); @@ -513,30 +533,34 @@ json_object* dap_db_history_addr(dap_chain_addr_t *a_addr, dap_chain_t *a_chain, l_corr_value = l_value; } 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(); - if (!j_obj_data) { - dap_json_rpc_allocation_error; - json_object_put(j_obj_tx); - return NULL; + + if (i_tmp > l_arr_start && i_tmp <= l_arr_end) { + json_object *j_obj_data = json_object_new_object(); + if (!j_obj_data) { + dap_json_rpc_allocation_error; + json_object_put(j_obj_tx); + json_object_put(j_arr_data); + return NULL; + } + json_object_object_add(j_obj_data, "tx_type", json_object_new_string("recv")); + json_object_object_add(j_obj_data, "recv_coins", json_object_new_string(l_coins_str)); + json_object_object_add(j_obj_data, "recv_datoshi", json_object_new_string(l_value_str)); + 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; + else + json_object_array_add(j_arr_data, j_obj_data); } - json_object_object_add(j_obj_data, "tx_type", json_object_new_string("recv")); - json_object_object_add(j_obj_data, "recv_coins", json_object_new_string(l_coins_str)); - json_object_object_add(j_obj_data, "recv_datoshi", json_object_new_string(l_value_str)); - 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; - else - json_object_array_add(j_arr_data, j_obj_data); + i_tmp++; + l_count_bool = true; } 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) continue; if (!l_src_addr && l_dst_addr && !dap_chain_addr_compare(l_dst_addr, &l_net_fee_addr)) - continue; - if (l_count++ < a_offset) - break; + continue; 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); @@ -547,19 +571,23 @@ json_object* dap_db_history_addr(dap_chain_addr_t *a_addr, dap_chain_t *a_chain, : dap_chain_tx_out_cond_subtype_to_str( ((dap_chain_tx_out_cond_t *)it->data)->header.subtype); 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(); - if (!j_obj_data) { - dap_json_rpc_allocation_error; - json_object_put(j_obj_tx); - return NULL; + if (i_tmp > l_arr_start && i_tmp <= l_arr_end) { + json_object * j_obj_data = json_object_new_object(); + if (!j_obj_data) { + dap_json_rpc_allocation_error; + json_object_put(j_obj_tx); + json_object_put(j_arr_data); + return NULL; + } + json_object_object_add(j_obj_data, "tx_type", json_object_new_string("send")); + json_object_object_add(j_obj_data, "send_coins", json_object_new_string(l_coins_str)); + json_object_object_add(j_obj_data, "send_datoshi", json_object_new_string(l_value_str)); + 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); } - json_object_object_add(j_obj_data, "tx_type", json_object_new_string("send")); - json_object_object_add(j_obj_data, "send_coins", json_object_new_string(l_coins_str)); - json_object_object_add(j_obj_data, "send_datoshi", json_object_new_string(l_value_str)); - 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); + i_tmp++; l_count_bool = true; } } @@ -568,7 +596,7 @@ json_object* dap_db_history_addr(dap_chain_addr_t *a_addr, dap_chain_t *a_chain, json_object_array_add(json_obj_datum, j_obj_tx); } dap_list_free(l_list_out_items); - if (l_is_need_correction) { + if (l_is_need_correction && l_corr_object) { SUM_256_256(l_corr_value, l_fee_sum, &l_corr_value); 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)); @@ -579,11 +607,11 @@ json_object* dap_db_history_addr(dap_chain_addr_t *a_addr, dap_chain_t *a_chain, json_object_array_add(j_arr_data, l_corr_object); l_is_need_correction = false; } - l_count_tx += l_count_bool; } a_chain->callback_datum_iter_delete(l_datum_iter); // delete hashes s_dap_chain_tx_hash_processed_ht_free(&l_tx_data_ht); + // if no history if (json_object_array_length(json_obj_datum) == 1) { json_object * json_empty_tx = json_object_new_object(); @@ -592,9 +620,12 @@ json_object* dap_db_history_addr(dap_chain_addr_t *a_addr, dap_chain_t *a_chain, json_object_put(json_obj_datum); return NULL; } - json_object_object_add(json_empty_tx, "status", json_object_new_string("empty")); + json_object_object_add(json_empty_tx, "status", json_object_new_string("empty")); json_object_array_add(json_obj_datum, json_empty_tx); - } + } + json_object_object_add(json_obj_summary, "network", json_object_new_string(l_net->pub.name)); + json_object_object_add(json_obj_summary, "chain", json_object_new_string(a_chain->name)); + json_object_object_add(json_obj_summary, "tx_sum", json_object_new_int(i_tmp)); return json_obj_datum; } @@ -613,6 +644,24 @@ json_object *dap_db_history_tx_all(dap_chain_t *l_chain, dap_chain_net_t *l_net, *l_cell_tmp = NULL; dap_chain_atom_iter_t *l_iter = NULL; json_object * json_arr_out = json_object_new_array(); + size_t l_arr_start = 0; + if (a_offset) { + l_arr_start = a_offset; + json_object* json_obj_off = json_object_new_object(); + json_object_object_add(json_obj_off, "offset", json_object_new_int(l_arr_start)); + json_object_array_add(json_arr_out, json_obj_off); + } + size_t l_arr_end = l_chain->callback_count_atom(l_chain); + if (a_limit) { + l_arr_end = l_arr_start + a_limit; + json_object* json_obj_lim = json_object_new_object(); + json_object_object_add(json_obj_lim, "limit", json_object_new_int(a_limit)); + json_object_array_add(json_arr_out, json_obj_lim); + if (l_arr_end > l_chain->callback_count_atom(l_chain)) { + l_arr_end = l_chain->callback_count_atom(l_chain); + } + } + size_t i_tmp = 1; HASH_ITER(hh, l_chain->cells, l_cell, l_cell_tmp) { if (a_limit && l_count_tx >= a_limit) break; @@ -624,9 +673,11 @@ json_object *dap_db_history_tx_all(dap_chain_t *l_chain, dap_chain_net_t *l_net, 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 && (a_limit ? l_count_tx < a_limit : true); i++) { if (l_datums[i]->header.type_id == DAP_CHAIN_DATUM_TX) { - if (l_count++ < a_offset) { + if (i_tmp < l_arr_start || i_tmp >= l_arr_end) { + i_tmp++; continue; } + i_tmp++; dap_chain_datum_tx_t *l_tx = (dap_chain_datum_tx_t*)l_datums[i]->data; dap_hash_fast_t l_ttx_hash = {0}; dap_hash_fast(l_tx, l_datums[i]->header.data_size, &l_ttx_hash); @@ -967,7 +1018,7 @@ int com_ledger(int a_argc, char ** a_argv, void **reply) dap_cli_server_cmd_find_option_val(a_argv, 0, a_argc, "-net", &l_net_str); dap_cli_server_cmd_find_option_val(a_argv, arg_index, a_argc, "-limit", &l_limit_str); dap_cli_server_cmd_find_option_val(a_argv, arg_index, a_argc, "-offset", &l_offset_str); - size_t l_limit = l_limit_str ? strtoul(l_limit_str, NULL, 10) : 0; + size_t l_limit = l_limit_str ? strtoul(l_limit_str, NULL, 10) : 1000; size_t l_offset = l_offset_str ? strtoul(l_offset_str, NULL, 10) : 0; if (l_net_str == NULL){ dap_json_rpc_error_add(DAP_CHAIN_NODE_CLI_COM_LEDGER_NET_PARAM_ERR, "Command 'list' requires key -net"); 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 54d784bd94952f9fd1e6581d87e7dce4c978c813..d7e4a356c913937b90d4ce44cd415cc3acb456a7 100644 --- a/modules/net/include/dap_chain_node_cli_cmd_tx.h +++ b/modules/net/include/dap_chain_node_cli_cmd_tx.h @@ -40,7 +40,7 @@ void s_dap_chain_tx_hash_processed_ht_free(dap_chain_tx_hash_processed_ht_t **l_ * return history json */ json_object * dap_db_history_tx(dap_chain_hash_fast_t* a_tx_hash, dap_chain_t * a_chain, const char *a_hash_out_type, dap_chain_net_t * l_net); -json_object * dap_db_history_addr(dap_chain_addr_t * a_addr, dap_chain_t * a_chain, const char *a_hash_out_type, const char * l_addr_str, size_t a_limit, size_t a_offset); +json_object * dap_db_history_addr(dap_chain_addr_t * a_addr, dap_chain_t * a_chain, const char *a_hash_out_type, const char * l_addr_str, json_object *json_obj_summary, size_t a_limit, size_t a_offset); json_object * dap_db_tx_history_to_json(dap_chain_hash_fast_t* a_tx_hash, dap_hash_fast_t * l_atom_hash, dap_chain_datum_tx_t * l_tx, diff --git a/modules/service/xchange/dap_chain_net_srv_xchange.c b/modules/service/xchange/dap_chain_net_srv_xchange.c index c43322ab6688cad2f90b2bd342d4a4439fc07471..388aff570582b34acc57a6d3d5ced7f4c79419a4 100644 --- a/modules/service/xchange/dap_chain_net_srv_xchange.c +++ b/modules/service/xchange/dap_chain_net_srv_xchange.c @@ -1958,27 +1958,25 @@ static int s_cli_srv_xchange(int a_argc, char **a_argv, void **a_str_reply) const char *l_offset_str = NULL; dap_cli_server_cmd_find_option_val(a_argv, l_arg_index, a_argc, "-limit", &l_limit_str); dap_cli_server_cmd_find_option_val(a_argv, l_arg_index, a_argc, "-offset", &l_offset_str); - size_t l_limit = l_limit_str ? strtoul(l_limit_str, NULL, 10) : 0; + size_t l_limit = l_limit_str ? strtoul(l_limit_str, NULL, 10) : 1000; size_t l_offset = l_offset_str ? strtoul(l_offset_str, NULL, 10) : 0; - size_t l_arr_start = 0; - if (l_limit > 1) { - l_arr_start = l_limit * l_offset; - } + size_t l_arr_start = 0; size_t l_arr_end = dap_list_length(l_tx_list); - if (l_offset) { + if (l_offset > 0) { + l_arr_start = l_offset; + dap_string_append_printf(l_reply_str, "offset: %lu\n", l_arr_start); + } + if (l_limit) { + dap_string_append_printf(l_reply_str, "limit: %lu\n", l_limit); l_arr_end = l_arr_start + l_limit; if (l_arr_end > dap_list_length(l_tx_list)) { l_arr_end = dap_list_length(l_tx_list); } - } + } size_t i_tmp = 0; // Print all txs for (dap_list_t *it = l_tx_list; it; it = it->next) { - if (i_tmp < l_arr_start || i_tmp > l_arr_end) { - i_tmp++; - continue; - } - i_tmp++; + dap_chain_datum_tx_t *l_tx = (dap_chain_datum_tx_t *)it->data; dap_chain_tx_out_cond_t *l_out_cond = dap_chain_datum_tx_out_cond_get(l_tx, DAP_CHAIN_TX_OUT_COND_SUBTYPE_SRV_XCHANGE , NULL); if (!l_out_cond || l_out_cond->header.srv_uid.uint64 != DAP_CHAIN_NET_SRV_XCHANGE_ID) @@ -2023,6 +2021,11 @@ static int s_cli_srv_xchange(int a_argc, char **a_argv, void **a_str_reply) l_status_order = "OPENED"; } + if (i_tmp < l_arr_start || i_tmp >= l_arr_end) { + i_tmp++; + continue; + } + i_tmp++; dap_hash_fast_t l_tx_hash = {}; dap_hash_fast(l_tx, dap_chain_datum_tx_get_size(l_tx), &l_tx_hash); const char *l_tx_hash_str = dap_chain_hash_fast_to_str_static(&l_tx_hash); @@ -2352,7 +2355,7 @@ static int s_cli_srv_xchange(int a_argc, char **a_argv, void **a_str_reply) const char *l_limit_str = NULL, *l_offset_str = NULL; dap_cli_server_cmd_find_option_val(a_argv, l_arg_index, a_argc, "-limit", &l_limit_str); dap_cli_server_cmd_find_option_val(a_argv, l_arg_index, a_argc, "-offset", &l_offset_str); - size_t l_limit = l_limit_str ? strtoul(l_limit_str, NULL, 10) : 0; + size_t l_limit = l_limit_str ? strtoul(l_limit_str, NULL, 10) : 1000; size_t l_offset = l_offset_str ? strtoul(l_offset_str, NULL, 10) : 0; dap_string_t *l_reply_str = dap_string_new(""); @@ -2370,22 +2373,19 @@ static int s_cli_srv_xchange(int a_argc, char **a_argv, void **a_str_reply) } size_t l_arr_start = 0; size_t l_arr_end = l_datum_num; - if (l_offset > 1) { - l_arr_start = l_limit * l_offset; + if (l_offset > 0) { + l_arr_start = l_offset; + dap_string_append_printf(l_reply_str, "offset: %lu\n", l_arr_start); } if (l_limit) { l_arr_end = l_arr_start + l_limit; + dap_string_append_printf(l_reply_str, "limit: %lu\n", l_limit); } size_t i_tmp = 0; dap_list_t * l_cur = l_datum_list0; while(l_cur){ - if (i_tmp < l_arr_start || i_tmp > l_arr_end) { - i_tmp++; - l_cur = dap_list_next(l_cur); - continue; - } - i_tmp++; + dap_chain_datum_tx_t *l_tx = (dap_chain_datum_tx_t*) ((dap_chain_datum_t*) l_cur->data)->data; if(l_tx){ dap_hash_fast_t l_tx_hash = {}; @@ -2417,6 +2417,12 @@ static int s_cli_srv_xchange(int a_argc, char **a_argv, void **a_str_reply) l_cur = dap_list_next(l_cur); continue; } + if (i_tmp < l_arr_start || i_tmp >= l_arr_end) { + i_tmp++; + l_cur = dap_list_next(l_cur); + continue; + } + i_tmp++; s_string_append_tx_cond_info(l_reply_str, l_net, l_tx, TX_STATUS_ALL, false, false, true); } diff --git a/modules/type/blocks/dap_chain_cs_blocks.c b/modules/type/blocks/dap_chain_cs_blocks.c index 2312100a12e27b7dc0c337eec410943b2022c5fb..fb9d04f88b4b09446a559b4ab1597e9d257ef37f 100644 --- a/modules/type/blocks/dap_chain_cs_blocks.c +++ b/modules/type/blocks/dap_chain_cs_blocks.c @@ -749,7 +749,6 @@ static int s_cli_blocks(int a_argc, char ** a_argv, void **a_str_reply) const char *l_cert_name = NULL, *l_from_hash_str = NULL, *l_to_hash_str = NULL, *l_from_date_str = NULL, *l_to_date_str = NULL, *l_pkey_hash_str = NULL, *l_limit_str = NULL, *l_offset_str = NULL; bool l_unspent_flag = false, l_first_signed_flag = false, l_signed_flag = false, l_hash_flag = false; - size_t l_block_count = 0; dap_pkey_t * l_pub_key = NULL; dap_hash_fast_t l_from_hash = {}, l_to_hash = {}, l_pkey_hash = {}; dap_time_t l_from_time = 0, l_to_time = 0; @@ -766,7 +765,7 @@ static int s_cli_blocks(int a_argc, char ** a_argv, void **a_str_reply) dap_cli_server_cmd_find_option_val(a_argv, arg_index, a_argc, "-limit", &l_limit_str); dap_cli_server_cmd_find_option_val(a_argv, arg_index, a_argc, "-offset", &l_offset_str); size_t l_offset = l_offset_str ? strtoul(l_offset_str, NULL, 10) : 0; - size_t l_limit = l_limit_str ? strtoul(l_limit_str, NULL, 10) : 0; + size_t l_limit = l_limit_str ? strtoul(l_limit_str, NULL, 10) : 1000; if (l_signed_flag && l_first_signed_flag) { dap_cli_server_cmd_set_reply_text(a_str_reply, "Choose only one option from 'singed' and 'first_signed'"); @@ -834,11 +833,13 @@ static int s_cli_blocks(int a_argc, char ** a_argv, void **a_str_reply) pthread_rwlock_rdlock(&PVT(l_blocks)->rwlock); dap_string_t *l_str_tmp = dap_string_new(NULL); size_t l_start_arr = 0; - if(l_offset > 1) { - l_start_arr = l_offset * l_limit; + if(l_offset > 0) { + l_start_arr = l_offset; + dap_string_append_printf(l_str_tmp, "offset: %lu\n", l_start_arr); } size_t l_arr_end = PVT(l_blocks)->blocks_count; if (l_limit) { + dap_string_append_printf(l_str_tmp, "limit: %lu\n", l_limit); l_arr_end = l_start_arr + l_limit; if (l_arr_end > PVT(l_blocks)->blocks_count) l_arr_end = PVT(l_blocks)->blocks_count; @@ -912,8 +913,7 @@ static int s_cli_blocks(int a_argc, char ** a_argv, void **a_str_reply) } char l_buf[DAP_TIME_STR_SIZE]; dap_time_to_str_rfc822(l_buf, DAP_TIME_STR_SIZE, l_ts); - dap_string_append_printf(l_str_tmp, "\t%s: ts_create=%s\n", l_block_cache->block_hash_str, l_buf); - l_block_count++; + dap_string_append_printf(l_str_tmp, "\t%d\t - %s: ts_create=%s\n",i_tmp-1, l_block_cache->block_hash_str, l_buf); if (l_to_hash_str && dap_hash_fast_compare(&l_to_hash, &l_block_cache->block_hash)) break; } @@ -923,7 +923,7 @@ static int s_cli_blocks(int a_argc, char ** a_argv, void **a_str_reply) if (l_cert_name || l_pkey_hash_str || l_from_hash_str || l_to_hash_str || l_from_date_str || l_to_date_str) l_filtered_criteria = " filtered according to the specified criteria"; dap_string_append_printf(l_str_tmp, "%s.%s: Have %"DAP_UINT64_FORMAT_U" blocks%s\n", - l_net->pub.name, l_chain->name, l_block_count, l_filtered_criteria); + l_net->pub.name, l_chain->name, i_tmp, l_filtered_criteria); dap_cli_server_cmd_set_reply_text(a_str_reply, "%s", l_str_tmp->str); dap_string_free(l_str_tmp, true); } break; diff --git a/modules/type/dag/dap_chain_cs_dag.c b/modules/type/dag/dap_chain_cs_dag.c index e79dc0fe55be431902aca7ba3eec51705ab49815..6b280a00ce8dc6e0ca630d16833d3e8d00533588 100644 --- a/modules/type/dag/dap_chain_cs_dag.c +++ b/modules/type/dag/dap_chain_cs_dag.c @@ -1760,15 +1760,16 @@ static int s_cli_dag(int argc, char ** argv, void **a_str_reply) size_t l_objs_count = 0; l_objs = dap_global_db_get_all_sync(l_gdb_group_events,&l_objs_count); char *ptr; - size_t l_limit = l_limit_str ? strtoull(l_limit_str, &ptr, 10) : 0; + size_t l_limit = l_limit_str ? strtoull(l_limit_str, &ptr, 10) : 1000; size_t l_offset = l_offset_str ? strtoull(l_offset_str, &ptr, 10) : 0; size_t l_arr_start = 0; if (l_offset) { - l_arr_start = l_offset * l_limit; - dap_string_append_printf(l_str_tmp, "limit: %lu", l_arr_start); + l_arr_start = l_offset; + dap_string_append_printf(l_str_tmp, "offset: %lu\n", l_arr_start); } size_t l_arr_end = l_objs_count; if (l_limit) { + dap_string_append_printf(l_str_tmp, "limit: %lu\n", l_limit); l_arr_end = l_arr_start + l_limit; if (l_arr_end > l_objs_count) l_arr_end = l_objs_count; @@ -1785,7 +1786,7 @@ static int s_cli_dag(int argc, char ** argv, void **a_str_reply) ((dap_chain_cs_dag_event_round_item_t *)l_objs[i].value)->event_n_signs; char buf[DAP_TIME_STR_SIZE]; dap_time_to_str_rfc822(buf, DAP_TIME_STR_SIZE, l_event->header.ts_created); - dap_string_append_printf(l_str_tmp, "\t%s: ts_create=%s\n", l_objs[i].key, buf); + dap_string_append_printf(l_str_tmp, "\t%d\t - %s: ts_create=%s\n",i-1, l_objs[i].key, buf); } if (l_objs && l_objs_count ) @@ -1801,15 +1802,16 @@ static int s_cli_dag(int argc, char ** argv, void **a_str_reply) } else if (!l_from_events_str || (strcmp(l_from_events_str,"events") == 0)) { dap_string_t * l_str_tmp = dap_string_new(NULL); pthread_mutex_lock(&PVT(l_dag)->events_mutex); - size_t l_limit = l_limit_str ? strtoul(l_limit_str, NULL, 10) : 0; + size_t l_limit = l_limit_str ? strtoul(l_limit_str, NULL, 10) : 1000; size_t l_offset = l_offset_str ? strtoul(l_offset_str, NULL, 10) : 0; size_t l_arr_start = 0; - if (l_offset > 1) { - l_arr_start = l_offset * l_limit; - dap_string_append_printf(l_str_tmp, "limit: %lu\n", l_arr_start); + if (l_offset > 0) { + l_arr_start = l_offset; + dap_string_append_printf(l_str_tmp, "offset: %lu\n", l_arr_start); } size_t l_arr_end = HASH_COUNT(PVT(l_dag)->events); if (l_limit) { + dap_string_append_printf(l_str_tmp, "limit: %lu\n", l_limit); l_arr_end = l_arr_start + l_limit; if (l_arr_end > HASH_COUNT(PVT(l_dag)->events)) l_arr_end = HASH_COUNT(PVT(l_dag)->events); @@ -1823,7 +1825,7 @@ static int s_cli_dag(int argc, char ** argv, void **a_str_reply) i_tmp++; char buf[DAP_TIME_STR_SIZE]; dap_time_to_str_rfc822(buf, DAP_TIME_STR_SIZE, l_event_item->event->header.ts_created); - dap_string_append_printf(l_str_tmp, "\t%s: ts_create=%s\n", + dap_string_append_printf(l_str_tmp, "\t%d\t- %s: ts_create=%s\n",i_tmp-1, dap_chain_hash_fast_to_str_static(&l_event_item->hash), buf); } @@ -1842,11 +1844,12 @@ static int s_cli_dag(int argc, char ** argv, void **a_str_reply) size_t l_offset = l_offset_str ? strtoul(l_offset_str, NULL, 10) : 0; size_t l_arr_start = 0; if (l_offset) { - l_arr_start = l_offset * l_limit; - dap_string_append_printf(l_str_tmp, "limit: %lu", l_arr_start); + l_arr_start = l_offset; + dap_string_append_printf(l_str_tmp, "offset: %lu\n", l_arr_start); } size_t l_arr_end = HASH_COUNT(PVT(l_dag)->events_treshold); if (l_limit) { + dap_string_append_printf(l_str_tmp, "limit: %lu\n", l_limit); l_arr_end = l_arr_start + l_limit; if (l_arr_end > HASH_COUNT(PVT(l_dag)->events_treshold)) l_arr_end = HASH_COUNT(PVT(l_dag)->events_treshold); @@ -1861,7 +1864,7 @@ static int s_cli_dag(int argc, char ** argv, void **a_str_reply) i_tmp++; char buf[DAP_TIME_STR_SIZE]; dap_time_to_str_rfc822(buf, DAP_TIME_STR_SIZE, l_event_item->event->header.ts_created); - dap_string_append_printf(l_str_tmp,"\t%s: ts_create=%s\n", + dap_string_append_printf(l_str_tmp,"\t%d\t- %s: ts_create=%s\n",i_tmp-1, dap_chain_hash_fast_to_str_static( &l_event_item->hash), buf); } @@ -1890,7 +1893,7 @@ static int s_cli_dag(int argc, char ** argv, void **a_str_reply) } size_t l_event_count = HASH_COUNT(PVT(l_dag)->events); size_t l_event_treshold_count = HASH_COUNT(PVT(l_dag)->events_treshold); - dap_string_append_printf(l_ret_str, "%zu atoms(s) in events\n%zu atom(s) in threshold", l_event_count, l_event_treshold_count); + dap_string_append_printf(l_ret_str, "%zu atom(s) in events\n%zu atom(s) in threshold", l_event_count, l_event_treshold_count); dap_cli_server_cmd_set_reply_text(a_str_reply, "%s", l_ret_str->str); dap_string_free(l_ret_str, true); } break;