Line data Source code
1 : /**
2 : * Copyright Soramitsu Co., Ltd. All Rights Reserved.
3 : * SPDX-License-Identifier: Apache-2.0
4 : */
5 :
6 : #include "interactive/interactive_status_cli.hpp"
7 :
8 : #include <boost/assert.hpp>
9 :
10 : #include "client.hpp"
11 : #include "common/byteutils.hpp"
12 :
13 : namespace iroha_cli {
14 : namespace interactive {
15 0 : static const std::map<iroha::protocol::TxStatus, std::string>
16 : userMessageMap = {
17 0 : {iroha::protocol::TxStatus::STATELESS_VALIDATION_FAILED,
18 : "Transaction has not passed stateless validation."},
19 0 : {iroha::protocol::TxStatus::STATELESS_VALIDATION_SUCCESS,
20 : "Transaction has successfully passed stateless validation."},
21 0 : {iroha::protocol::TxStatus::STATEFUL_VALIDATION_FAILED,
22 : "Transaction has not passed stateful validation."},
23 0 : {iroha::protocol::TxStatus::STATEFUL_VALIDATION_SUCCESS,
24 : "Transaction has successfully passed stateful validation."},
25 0 : {iroha::protocol::TxStatus::REJECTED,
26 : "Transaction has been rejected."},
27 0 : {iroha::protocol::TxStatus::COMMITTED,
28 : "Transaction was successfully committed."},
29 0 : {iroha::protocol::TxStatus::MST_EXPIRED,
30 : "Transaction has not collected enough signatures in time."},
31 0 : {iroha::protocol::TxStatus::NOT_RECEIVED,
32 : "Transaction was not found in the system."},
33 0 : {iroha::protocol::TxStatus::MST_PENDING,
34 : "Transaction has not collected quorum of signatures."},
35 0 : {iroha::protocol::TxStatus::ENOUGH_SIGNATURES_COLLECTED,
36 : "Transaction has collected all signatures."}};
37 :
38 : InteractiveStatusCli::InteractiveStatusCli(
39 : const std::string &default_peer_ip,
40 : int default_port,
41 : logger::LoggerPtr pb_qry_factory_log)
42 0 : : default_peer_ip_(default_peer_ip),
43 0 : default_port_(default_port),
44 0 : pb_qry_factory_log_(std::move(pb_qry_factory_log)) {
45 0 : createActionsMenu();
46 0 : createResultMenu();
47 0 : }
48 :
49 : void InteractiveStatusCli::createActionsMenu() {
50 0 : descriptionMap_ = {{GET_TX_INFO, "Get status of transaction"}};
51 0 : const auto tx_id = "Requested tx hash";
52 :
53 0 : requestParamsDescriptions_ = {
54 0 : {GET_TX_INFO, makeParamsDescription({tx_id})}};
55 0 : actionHandlers_ = {{GET_TX_INFO, &InteractiveStatusCli::parseGetHash}};
56 :
57 0 : menuPoints_ = formMenu(
58 0 : actionHandlers_, requestParamsDescriptions_, descriptionMap_);
59 0 : addBackOption(menuPoints_);
60 0 : }
61 :
62 : void InteractiveStatusCli::createResultMenu() {
63 0 : resultHandlers_ = {{SEND_CODE, &InteractiveStatusCli::parseSendToIroha},
64 0 : {SAVE_CODE, &InteractiveStatusCli::parseSaveFile}};
65 0 : resultParamsDescriptions_ =
66 0 : getCommonParamsMap(default_peer_ip_, default_port_);
67 :
68 0 : resultPoints_ = formMenu(resultHandlers_,
69 0 : resultParamsDescriptions_,
70 0 : getCommonDescriptionMap());
71 0 : addBackOption(resultPoints_);
72 0 : }
73 :
74 : void InteractiveStatusCli::run() {
75 0 : bool isParsing = true;
76 0 : currentContext_ = MAIN;
77 0 : printMenu("Choose action: ", menuPoints_);
78 0 : while (isParsing) {
79 0 : auto line = promptString("> ");
80 0 : if (not line) {
81 : // line has terminating symbol
82 0 : isParsing = false;
83 0 : break;
84 : }
85 0 : switch (currentContext_) {
86 : case MAIN:
87 0 : isParsing = parseAction(line.value());
88 0 : break;
89 : case RESULT:
90 0 : isParsing = parseResult(line.value());
91 0 : break;
92 : default:
93 : // shouldn't get here
94 0 : BOOST_ASSERT_MSG(false, "not implemented");
95 : break;
96 : }
97 0 : }
98 0 : }
99 :
100 : bool InteractiveStatusCli::parseAction(std::string &line) {
101 0 : if (isBackOption(line)) {
102 0 : return false;
103 : }
104 :
105 0 : auto res = handleParse<std::string>(
106 0 : this, line, actionHandlers_, requestParamsDescriptions_);
107 0 : if (not res) {
108 : // Continue parsing
109 0 : return true;
110 : }
111 :
112 0 : txHash_ = res.value();
113 0 : currentContext_ = RESULT;
114 0 : printMenu("Tx hash is saved. Choose what to do:", resultPoints_);
115 : // Continue parsing
116 0 : return true;
117 0 : }
118 :
119 : bool InteractiveStatusCli::parseResult(std::string &line) {
120 0 : if (isBackOption(line)) {
121 : // Give up the last query and start a new one
122 0 : currentContext_ = MAIN;
123 0 : printEnd();
124 0 : printMenu("Choose action: ", menuPoints_);
125 : // Continue parsing
126 0 : return true;
127 : }
128 :
129 0 : auto res = handleParse<bool>(
130 0 : this, line, resultHandlers_, resultParamsDescriptions_);
131 :
132 0 : return res.value_or(true);
133 0 : }
134 :
135 : bool InteractiveStatusCli::parseSendToIroha(ActionParams line) {
136 : auto address =
137 0 : parseIrohaPeerParams(line, default_peer_ip_, default_port_);
138 0 : if (not address) {
139 0 : return true;
140 : }
141 :
142 0 : auto status = iroha::protocol::TxStatus::NOT_RECEIVED;
143 0 : iroha::protocol::ToriiResponse answer;
144 0 : if (iroha::hexstringToBytestring(txHash_)) {
145 0 : answer = CliClient(address.value().first,
146 0 : address.value().second,
147 0 : pb_qry_factory_log_)
148 0 : .getTxStatus(txHash_)
149 0 : .answer;
150 0 : status = answer.tx_status();
151 0 : }
152 :
153 0 : std::string message;
154 : try {
155 0 : message = userMessageMap.at(status);
156 0 : if (not answer.err_or_cmd_name().empty()) {
157 0 : message += " error '" + answer.err_or_cmd_name() + "'";
158 0 : }
159 0 : } catch (const std::out_of_range &e) {
160 0 : message =
161 : "A problem detected while retrieving transaction status. Please "
162 : "try again later.";
163 0 : }
164 0 : std::cout << message << std::endl;
165 :
166 0 : printEnd();
167 0 : return false;
168 0 : }
169 :
170 : bool InteractiveStatusCli::parseSaveFile(ActionParams line) {
171 0 : std::cout << "Not implemented yet" << std::endl;
172 0 : return true;
173 : }
174 :
175 : std::string InteractiveStatusCli::parseGetHash(ActionParams params) {
176 0 : return params[0];
177 : }
178 : } // namespace interactive
179 : } // namespace iroha_cli
|