From 3df1bd49e920ac83868da82a054d34794551510d Mon Sep 17 00:00:00 2001
From: "alexey.stratulat" <alexey.stratulat@demlabs.net>
Date: Mon, 8 Apr 2024 11:31:12 +0700
Subject: [PATCH] [+] Added output with the text of the answer for which a vote
 was given in a certain vote.

---
 modules/common/CMakeLists.txt                 |  2 +-
 modules/common/dap_chain_datum_tx_voting.c    | 45 ++++++++++++++++++-
 .../include/dap_chain_datum_tx_voting.h       |  3 +-
 .../common/dap_json_rpc_chain_datum_tx.c      |  4 +-
 modules/net/dap_chain_node_cli_cmd.c          |  2 +-
 5 files changed, 51 insertions(+), 5 deletions(-)

diff --git a/modules/common/CMakeLists.txt b/modules/common/CMakeLists.txt
index 79ce7eb3e4..e1d05ec18b 100644
--- a/modules/common/CMakeLists.txt
+++ b/modules/common/CMakeLists.txt
@@ -10,7 +10,7 @@ if(BUILD_CELLFRAME_SDK_TESTS)
     add_subdirectory(tests)
 endif()
 
-target_link_libraries(${PROJECT_NAME} dap_core dap_crypto)
+target_link_libraries(${PROJECT_NAME} dap_core dap_crypto dap_chain_net)
 target_include_directories(${PROJECT_NAME} PUBLIC include/ )
 target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/../../../dap-sdk/3rdparty/json-c)
 
diff --git a/modules/common/dap_chain_datum_tx_voting.c b/modules/common/dap_chain_datum_tx_voting.c
index 658407e446..bfd0043bc5 100644
--- a/modules/common/dap_chain_datum_tx_voting.c
+++ b/modules/common/dap_chain_datum_tx_voting.c
@@ -150,6 +150,32 @@ dap_chain_tx_voting_t *dap_chain_datum_tx_item_voting_create(void)
     return l_item;
 }
 
+const char *s_tx_voting_get_answer_text_by_idx(dap_chain_datum_tx_t *a_tx, uint64_t a_idx) {
+    dap_list_t *l_answers_list = NULL;
+    size_t l_anwers_count = 0;
+    dap_list_t* l_tsd_list = dap_chain_datum_tx_items_get(a_tx, TX_ITEM_TYPE_TSD, NULL);
+    dap_list_t* l_temp = l_tsd_list;
+    while (l_temp){
+        dap_tsd_t* l_tsd = (dap_tsd_t *)((dap_chain_tx_tsd_t*)l_temp->data)->tsd;
+        if (l_tsd->type == VOTING_TSD_TYPE_ANSWER) {
+            char *l_buf_string = DAP_NEW_Z_SIZE(char, l_tsd->size + 1);
+            memcpy(l_buf_string, l_tsd->data, l_tsd->size);
+            l_buf_string[l_tsd->size] = '\0';
+            l_answers_list = dap_list_append(l_answers_list, l_buf_string);
+            l_anwers_count++;
+        }
+        l_temp = l_temp->next;
+    }
+    dap_list_free(l_tsd_list);
+    if (l_anwers_count < a_idx) {
+        dap_list_free_full(l_answers_list, NULL);
+        return NULL;
+    }
+    char *l_ret = dap_strdup(dap_list_nth_data(l_answers_list, a_idx));
+    dap_list_free_full(l_answers_list, NULL);
+    return l_ret;
+}
+
 json_object *dap_chain_datum_tx_item_voting_tsd_to_json(dap_chain_datum_tx_t* a_tx)
 {
     if (!a_tx)
@@ -210,14 +236,31 @@ dap_chain_tx_vote_t *dap_chain_datum_tx_item_vote_create(dap_chain_hash_fast_t *
     return l_item;
 }
 
-json_object *dap_chain_datum_tx_item_vote_to_json(dap_chain_tx_vote_t *a_vote)
+const char *s_get_vote_answer_text(dap_hash_fast_t *a_vote, uint64_t a_idx, dap_ledger_t *a_ledger) {
+    dap_chain_datum_tx_t *l_tx = dap_ledger_tx_find_by_hash(a_ledger, a_vote);
+    if (!l_tx || !a_ledger) {
+        return NULL;
+    }
+    return s_tx_voting_get_answer_text_by_idx(l_tx, a_idx);
+}
+
+json_object *dap_chain_datum_tx_item_vote_to_json(dap_chain_tx_vote_t *a_vote, dap_ledger_t *a_ledger)
 {
     json_object *l_object = json_object_new_object();
     char *l_voting_hash_str = dap_hash_fast_to_str_new(&a_vote->voting_hash);
     json_object *l_voting_hash = json_object_new_string(l_voting_hash_str);
     DAP_DELETE(l_voting_hash_str);
     json_object *l_answer_idx = json_object_new_uint64(a_vote->answer_idx);
+    char *l_answer_text_str = s_get_vote_answer_text(&a_vote->voting_hash, a_vote->answer_idx, a_ledger);
+    json_object *l_answer_text = NULL;
+    if (!l_answer_text_str) {
+        l_answer_text = json_object_new_string("{UNDEFINED}");
+    } else {
+        l_answer_text = json_object_new_string(l_answer_text_str);
+        DAP_DELETE(l_answer_text_str);
+    }
     json_object_object_add(l_object, "votingHash", l_voting_hash);
     json_object_object_add(l_object, "answer_idx", l_answer_idx);
+    json_object_object_add(l_object, "answer_text", l_answer_text);
     return l_object;
 }
diff --git a/modules/common/include/dap_chain_datum_tx_voting.h b/modules/common/include/dap_chain_datum_tx_voting.h
index 1eaf8b5b42..331cb806f8 100644
--- a/modules/common/include/dap_chain_datum_tx_voting.h
+++ b/modules/common/include/dap_chain_datum_tx_voting.h
@@ -25,6 +25,7 @@
 #include "dap_chain_common.h"
 #include "dap_chain_datum_tx.h"
 #include "dap_chain_datum_tx_items.h"
+#include "dap_chain_ledger.h"
 #include "dap_time.h"
 #include "dap_list.h"
 #include "dap_tsd.h"
@@ -87,4 +88,4 @@ json_object *dap_chain_datum_tx_item_voting_tsd_to_json(dap_chain_datum_tx_t* a_
 
 
 dap_chain_tx_vote_t *dap_chain_datum_tx_item_vote_create(dap_chain_hash_fast_t *a_voting_hash, uint64_t *a_answer_idx);
-json_object *dap_chain_datum_tx_item_vote_to_json(dap_chain_tx_vote_t *a_vote);
+json_object *dap_chain_datum_tx_item_vote_to_json(dap_chain_tx_vote_t *a_vote, dap_ledger_t *a_ledger);
diff --git a/modules/json_rpc/common/dap_json_rpc_chain_datum_tx.c b/modules/json_rpc/common/dap_json_rpc_chain_datum_tx.c
index 5197480121..9b3d640405 100644
--- a/modules/json_rpc/common/dap_json_rpc_chain_datum_tx.c
+++ b/modules/json_rpc/common/dap_json_rpc_chain_datum_tx.c
@@ -10,6 +10,7 @@
 #include "dap_json_rpc_chain_datum_tx_receipt.h"
 #include "json.h"
 #include "dap_chain_datum_tx_voting.h"
+#include "dap_chain_net.h"
 
 #define LOG_TAG "dap_json_rpc_chain_datum_tx"
 
@@ -110,7 +111,8 @@ json_object *dap_chain_datum_tx_to_json(dap_chain_datum_tx_t *a_tx,dap_chain_net
                 break;
             case TX_ITEM_TYPE_VOTE:
                 l_obj_item_type = json_object_new_string("TX_ITEM_TYPE_VOTE");
-                l_obj_item_data = dap_chain_datum_tx_item_vote_to_json((dap_chain_tx_vote_t*)item);
+                dap_chain_net_t *l_net = dap_chain_net_by_id(*a_net_id);
+                l_obj_item_data = dap_chain_datum_tx_item_vote_to_json((dap_chain_tx_vote_t*)item, l_net->pub.ledger);
             break;
             case TX_ITEM_TYPE_VOTING:
                 l_obj_item_type = json_object_new_string("TX_ITEM_TYPE_VOTING");
diff --git a/modules/net/dap_chain_node_cli_cmd.c b/modules/net/dap_chain_node_cli_cmd.c
index 238688ea5e..b9936ed4bc 100644
--- a/modules/net/dap_chain_node_cli_cmd.c
+++ b/modules/net/dap_chain_node_cli_cmd.c
@@ -3099,7 +3099,7 @@ void s_com_mempool_list_print_for_chain(dap_chain_net_t * a_net, dap_chain_t * a
                         }
                         for (dap_list_t *it = l_vote_list; it; it = it->next) {
                             json_object *l_jobj_vote = dap_chain_datum_tx_item_vote_to_json(
-                                    (dap_chain_tx_vote_t *) it->data);
+                                    (dap_chain_tx_vote_t *) it->data, a_net->pub.ledger);
                             json_object_array_add(l_jobj_tx_vote, l_jobj_vote);
                         }
                         for (dap_list_t *it = l_voting_list; it; it = it->next) {
-- 
GitLab