Line data Source code
1 : /**
2 : * Copyright Soramitsu Co., Ltd. All Rights Reserved.
3 : * SPDX-License-Identifier: Apache-2.0
4 : */
5 :
6 : #include "query_response_handler.hpp"
7 : #include "backend/protobuf/commands/proto_command.hpp"
8 : #include "backend/protobuf/permissions.hpp"
9 : #include "backend/protobuf/query_responses/proto_query_response.hpp"
10 : #include "backend/protobuf/query_responses/proto_transaction_response.hpp"
11 : #include "interfaces/permissions.hpp"
12 : #include "logger/logger.hpp"
13 : #include "model/converters/pb_common.hpp"
14 :
15 : using namespace iroha::protocol;
16 : namespace iroha_cli {
17 :
18 : QueryResponseHandler::QueryResponseHandler(logger::LoggerPtr log)
19 0 : : log_(std::move(log)) {
20 0 : handler_map_[QueryResponse::ResponseCase::kErrorResponse] =
21 : &QueryResponseHandler::handleErrorResponse;
22 0 : handler_map_[QueryResponse::ResponseCase::kAccountResponse] =
23 : &QueryResponseHandler::handleAccountResponse;
24 0 : handler_map_[QueryResponse::ResponseCase::kAccountAssetsResponse] =
25 : &QueryResponseHandler::handleAccountAssetsResponse;
26 0 : handler_map_[QueryResponse::ResponseCase::kSignatoriesResponse] =
27 : &QueryResponseHandler::handleSignatoriesResponse;
28 0 : handler_map_[QueryResponse::ResponseCase::kTransactionsResponse] =
29 : &QueryResponseHandler::handleTransactionsResponse;
30 0 : handler_map_[QueryResponse::ResponseCase::kRolesResponse] =
31 : &QueryResponseHandler::handleRolesResponse;
32 0 : handler_map_[QueryResponse::ResponseCase::kRolePermissionsResponse] =
33 : &QueryResponseHandler::handleRolePermissionsResponse;
34 0 : handler_map_[QueryResponse::ResponseCase::kAssetResponse] =
35 : &QueryResponseHandler::handleAssetResponse;
36 :
37 : // Error responses:
38 0 : error_handler_map_[ErrorResponse::STATEFUL_INVALID] =
39 : "Query is stateful invalid";
40 0 : error_handler_map_[ErrorResponse::STATELESS_INVALID] =
41 : "Query is stateless invalid";
42 0 : error_handler_map_[ErrorResponse::NO_ACCOUNT] = "Account not found";
43 0 : error_handler_map_[ErrorResponse::NO_ACCOUNT_ASSETS] =
44 : "Account assets not found";
45 0 : error_handler_map_[ErrorResponse::NO_SIGNATORIES] = "No signatories found";
46 0 : error_handler_map_[ErrorResponse::NOT_SUPPORTED] = "Query not supported";
47 0 : error_handler_map_[ErrorResponse::NO_ROLES] = "No roles in the system";
48 0 : error_handler_map_[ErrorResponse::NO_ASSET] = "No asset found";
49 0 : }
50 :
51 : void QueryResponseHandler::handle(
52 : const iroha::protocol::QueryResponse &response) {
53 0 : auto it = handler_map_.find(response.response_case());
54 0 : if (it != handler_map_.end()) {
55 0 : (this->*it->second)(response);
56 0 : } else {
57 0 : log_->error("Response Handle {} not Implemented",
58 0 : response.response_case());
59 : }
60 0 : }
61 :
62 : enum PrefixId {
63 : kAccountId,
64 : kAssetId,
65 : kAmount,
66 : kDomainId,
67 : kSignatories,
68 : kPrecision,
69 : kRoles,
70 : kJsonData,
71 : kCreatorId,
72 : kCreatedTime,
73 : kHash,
74 : kCommands,
75 : kDefault,
76 : };
77 :
78 : const std::map<PrefixId, const char *> prefix{
79 : {kAccountId, "-Account Id:- {}"},
80 : {kAssetId, "-Asset Id- {}"},
81 : {kAmount, "-Balance- {}"},
82 : {kDomainId, "-Domain- {}"},
83 : {kSignatories, "-Signatory- {}"},
84 : {kPrecision, "-Precision- {}"},
85 : {kRoles, "-Roles-: "},
86 : {kJsonData, "-Data-: {}"},
87 : {kCreatorId, "-Creator Id- {}"},
88 : {kCreatedTime, "-Created Time- {}"},
89 : {kHash, "-Hash- {}"},
90 : {kCommands, "-Commands- {}"},
91 : {kDefault, " {} "}};
92 :
93 : void QueryResponseHandler::handleErrorResponse(
94 : const iroha::protocol::QueryResponse &response) {
95 0 : auto it = error_handler_map_.find((response.error_response().reason()));
96 0 : if (it != error_handler_map_.end()) {
97 0 : log_->error(it->second);
98 0 : } else {
99 : // Response of some other type received
100 0 : log_->error("Error Response Handle of type {} not Implemented",
101 0 : response.error_response().reason());
102 : }
103 0 : }
104 :
105 : void QueryResponseHandler::handleAccountResponse(
106 : const iroha::protocol::QueryResponse &response) {
107 0 : auto account = response.account_response().account();
108 0 : log_->info("[Account]:");
109 0 : log_->info(prefix.at(kAccountId), account.account_id());
110 0 : log_->info(prefix.at(kDomainId), account.domain_id());
111 :
112 0 : log_->info(prefix.at(kRoles));
113 0 : auto roles = response.account_response().account_roles();
114 : std::for_each(roles.begin(), roles.end(), [this](auto role) {
115 0 : log_->info(prefix.at(kDefault), role);
116 0 : });
117 0 : log_->info(prefix.at(kJsonData), account.json_data());
118 0 : }
119 :
120 : void QueryResponseHandler::handleRolesResponse(
121 : const iroha::protocol::QueryResponse &response) {
122 0 : auto roles = response.roles_response().roles();
123 : std::for_each(roles.begin(), roles.end(), [this](auto role) {
124 0 : log_->info(prefix.at(kDefault), role);
125 0 : });
126 0 : }
127 :
128 : void QueryResponseHandler::handleRolePermissionsResponse(
129 : const iroha::protocol::QueryResponse &response) {
130 0 : auto perms = response.role_permissions_response().permissions();
131 : std::for_each(perms.begin(), perms.end(), [this](auto perm) {
132 0 : log_->info(
133 0 : prefix.at(kDefault),
134 0 : shared_model::proto::permissions::toString(
135 0 : static_cast<shared_model::interface::permissions::Role>(perm)));
136 0 : });
137 0 : }
138 :
139 : void QueryResponseHandler::handleAccountAssetsResponse(
140 : const iroha::protocol::QueryResponse &response) {
141 0 : auto acc_assets = response.account_assets_response().account_assets();
142 0 : log_->info("[Account Assets]");
143 0 : for (auto &acc_asset : acc_assets) {
144 0 : log_->info(prefix.at(kAccountId), acc_asset.account_id());
145 0 : log_->info(prefix.at(kAssetId), acc_asset.asset_id());
146 0 : log_->info(prefix.at(kAmount), acc_asset.balance());
147 : }
148 0 : }
149 :
150 : void QueryResponseHandler::handleSignatoriesResponse(
151 : const iroha::protocol::QueryResponse &response) {
152 0 : auto signatories = response.signatories_response().keys();
153 0 : log_->info("[Signatories]");
154 0 : std::for_each(
155 : signatories.begin(), signatories.end(), [this](auto signatory) {
156 0 : log_->info(prefix.at(kSignatories), signatory);
157 0 : });
158 0 : }
159 :
160 : void QueryResponseHandler::handleAssetResponse(
161 : const iroha::protocol::QueryResponse &response) {
162 0 : auto asset = response.asset_response().asset();
163 0 : log_->info("[Asset]");
164 0 : log_->info(prefix.at(kAssetId), asset.asset_id());
165 0 : log_->info(prefix.at(kDomainId), asset.domain_id());
166 0 : log_->info(prefix.at(kPrecision), asset.precision());
167 0 : }
168 :
169 : void QueryResponseHandler::handleTransactionsResponse(
170 : const iroha::protocol::QueryResponse &response) {
171 0 : auto resp = shared_model::proto::TransactionsResponse(response);
172 0 : auto txs = resp.transactions();
173 : std::for_each(txs.begin(), txs.end(), [this](auto &tx) {
174 0 : log_->info("[Transaction]");
175 0 : log_->info(prefix.at(kHash), tx.hash().hex());
176 0 : log_->info(prefix.at(kCreatorId), tx.creatorAccountId());
177 0 : log_->info(prefix.at(kCreatedTime), tx.createdTime());
178 0 : log_->info(prefix.at(kCommands), tx.commands().size());
179 :
180 0 : auto cmds = tx.commands();
181 : std::for_each(cmds.begin(), cmds.end(), [this](auto &cmd) {
182 0 : log_->info(prefix.at(kDefault), cmd);
183 0 : });
184 0 : });
185 0 : }
186 :
187 : } // namespace iroha_cli
|