diff --git a/modules/type/blocks/dap_chain_cs_blocks.c b/modules/type/blocks/dap_chain_cs_blocks.c index 9361327d54e2fabb7e6277dd4fc4cd1450615bb6..3f03db28c6eac948f4d8513835fcc04922cce7ef 100644 --- a/modules/type/blocks/dap_chain_cs_blocks.c +++ b/modules/type/blocks/dap_chain_cs_blocks.c @@ -532,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 @@ -981,12 +997,23 @@ static int s_cli_blocks(int a_argc, char ** a_argv, void **a_str_reply) 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"; @@ -1185,10 +1212,10 @@ static int s_cli_blocks(int a_argc, char ** a_argv, void **a_str_reply) if (l_hash_tx) { json_object* json_obj_out = json_object_new_object(); 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_DEL_Z(l_val); + 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);