Line data Source code
1 : /**
2 : * Copyright Soramitsu Co., Ltd. All Rights Reserved.
3 : * SPDX-License-Identifier: Apache-2.0
4 : */
5 :
6 : #include "backend/protobuf/proto_query_response_factory.hpp"
7 : #include "backend/protobuf/permissions.hpp"
8 : #include "backend/protobuf/query_responses/proto_block_query_response.hpp"
9 : #include "backend/protobuf/query_responses/proto_query_response.hpp"
10 : #include "backend/protobuf/transaction.hpp"
11 : #include "cryptography/public_key.hpp"
12 : #include "interfaces/common_objects/amount.hpp"
13 :
14 : namespace {
15 : /**
16 : * Creates a query response using provided lambda and returns unique_ptr to it
17 : * @tparam QueryResponseCreatorLambda - lambda, which specifies, how to create
18 : * a query response
19 : * @param response_creator - that lambda
20 : * @param query_hash - hash of query, for which response is created
21 : * @return unique_ptr to created query response
22 : */
23 : template <typename QueryResponseCreatorLambda>
24 : std::unique_ptr<shared_model::interface::QueryResponse> createQueryResponse(
25 : QueryResponseCreatorLambda response_creator,
26 : const shared_model::crypto::Hash &query_hash) {
27 66 : iroha::protocol::QueryResponse protocol_query_response;
28 66 : protocol_query_response.set_query_hash(query_hash.hex());
29 :
30 66 : response_creator(protocol_query_response);
31 :
32 66 : return std::make_unique<shared_model::proto::QueryResponse>(
33 66 : std::move(protocol_query_response));
34 66 : }
35 :
36 : /**
37 : * Creates a block query response using provided lambda and returns unique_ptr
38 : * to it
39 : * @tparam QueryResponseCreatorLambda - lambda, which specifies, how to
40 : * create a block query response
41 : * @param response_creator - that lambda
42 : * @return unique_ptr to created block query response
43 : */
44 : template <typename QueryResponseCreatorLambda>
45 : std::unique_ptr<shared_model::interface::BlockQueryResponse>
46 : createQueryResponse(QueryResponseCreatorLambda response_creator) {
47 716 : iroha::protocol::BlockQueryResponse protocol_query_response;
48 :
49 716 : response_creator(protocol_query_response);
50 :
51 716 : return std::make_unique<shared_model::proto::BlockQueryResponse>(
52 716 : std::move(protocol_query_response));
53 716 : }
54 : } // namespace
55 :
56 : std::unique_ptr<shared_model::interface::QueryResponse>
57 : shared_model::proto::ProtoQueryResponseFactory::createAccountAssetResponse(
58 : std::vector<std::tuple<interface::types::AccountIdType,
59 : interface::types::AssetIdType,
60 : shared_model::interface::Amount>> assets,
61 : const crypto::Hash &query_hash) const {
62 14 : return createQueryResponse(
63 : [assets = std::move(assets)](
64 : iroha::protocol::QueryResponse &protocol_query_response) {
65 14 : iroha::protocol::AccountAssetResponse *protocol_specific_response =
66 14 : protocol_query_response.mutable_account_assets_response();
67 36 : for (size_t i = 0; i < assets.size(); i++) {
68 22 : auto *asset = protocol_specific_response->add_account_assets();
69 22 : asset->set_account_id(std::move(std::get<0>(assets.at(i))));
70 22 : asset->set_asset_id(std::move(std::get<1>(assets.at(i))));
71 22 : asset->set_balance(std::get<2>(assets.at(i)).toStringRepr());
72 22 : }
73 14 : },
74 14 : query_hash);
75 0 : }
76 :
77 : std::unique_ptr<shared_model::interface::QueryResponse>
78 : shared_model::proto::ProtoQueryResponseFactory::createAccountDetailResponse(
79 : shared_model::interface::types::DetailType account_detail,
80 : const crypto::Hash &query_hash) const {
81 17 : return createQueryResponse(
82 : [account_detail = std::move(account_detail)](
83 : iroha::protocol::QueryResponse &protocol_query_response) {
84 17 : iroha::protocol::AccountDetailResponse *protocol_specific_response =
85 17 : protocol_query_response.mutable_account_detail_response();
86 17 : protocol_specific_response->set_detail(account_detail);
87 17 : },
88 17 : query_hash);
89 0 : }
90 :
91 : std::unique_ptr<shared_model::interface::QueryResponse>
92 : shared_model::proto::ProtoQueryResponseFactory::createAccountResponse(
93 : const shared_model::interface::types::AccountIdType account_id,
94 : const shared_model::interface::types::DomainIdType domain_id,
95 : shared_model::interface::types::QuorumType quorum,
96 : const shared_model::interface::types::JsonType jsonData,
97 : std::vector<shared_model::interface::types::RoleIdType> roles,
98 : const crypto::Hash &query_hash) const {
99 16 : return createQueryResponse(
100 : [account_id = std::move(account_id),
101 16 : domain_id = std::move(domain_id),
102 16 : jsonData = std::move(jsonData),
103 16 : quorum,
104 16 : roles = std::move(roles)](
105 : iroha::protocol::QueryResponse &protocol_query_response) {
106 16 : iroha::protocol::AccountResponse *protocol_specific_response =
107 16 : protocol_query_response.mutable_account_response();
108 16 : auto *account = protocol_specific_response->mutable_account();
109 16 : account->set_account_id(std::move(account_id));
110 16 : account->set_domain_id(std::move(domain_id));
111 16 : account->set_quorum(quorum);
112 16 : account->set_json_data(std::move(jsonData));
113 34 : for (const auto &role : roles) {
114 18 : protocol_specific_response->add_account_roles(std::move(role));
115 : }
116 16 : },
117 16 : query_hash);
118 0 : }
119 :
120 : std::unique_ptr<shared_model::interface::QueryResponse>
121 : shared_model::proto::ProtoQueryResponseFactory::createErrorQueryResponse(
122 : ErrorQueryType error_type,
123 : std::string error_msg,
124 : const crypto::Hash &query_hash) const {
125 66 : return createQueryResponse(
126 : [error_type, error_msg = std::move(error_msg)](
127 : iroha::protocol::QueryResponse &protocol_query_response) mutable {
128 : iroha::protocol::ErrorResponse_Reason reason;
129 132 : switch (error_type) {
130 : case ErrorQueryType::kStatelessFailed:
131 1 : reason = iroha::protocol::ErrorResponse_Reason_STATELESS_INVALID;
132 1 : break;
133 : case ErrorQueryType::kStatefulFailed:
134 58 : reason = iroha::protocol::ErrorResponse_Reason_STATEFUL_INVALID;
135 58 : break;
136 : case ErrorQueryType::kNoAccount:
137 1 : reason = iroha::protocol::ErrorResponse_Reason_NO_ACCOUNT;
138 1 : break;
139 : case ErrorQueryType::kNoAccountAssets:
140 0 : reason = iroha::protocol::ErrorResponse_Reason_NO_ACCOUNT_ASSETS;
141 0 : break;
142 : case ErrorQueryType::kNoAccountDetail:
143 1 : reason = iroha::protocol::ErrorResponse_Reason_NO_ACCOUNT_DETAIL;
144 1 : break;
145 : case ErrorQueryType::kNoSignatories:
146 2 : reason = iroha::protocol::ErrorResponse_Reason_NO_SIGNATORIES;
147 2 : break;
148 : case ErrorQueryType::kNotSupported:
149 0 : reason = iroha::protocol::ErrorResponse_Reason_NOT_SUPPORTED;
150 0 : break;
151 : case ErrorQueryType::kNoAsset:
152 2 : reason = iroha::protocol::ErrorResponse_Reason_NO_ASSET;
153 2 : break;
154 : case ErrorQueryType::kNoRoles:
155 1 : reason = iroha::protocol::ErrorResponse_Reason_NO_ROLES;
156 1 : break;
157 : }
158 66 : iroha::protocol::ErrorResponse *protocol_specific_response =
159 66 : protocol_query_response.mutable_error_response();
160 66 : protocol_specific_response->set_reason(reason);
161 66 : protocol_specific_response->set_message(std::move(error_msg));
162 66 : },
163 66 : query_hash);
164 0 : }
165 :
166 : std::unique_ptr<shared_model::interface::QueryResponse>
167 : shared_model::proto::ProtoQueryResponseFactory::createSignatoriesResponse(
168 : std::vector<shared_model::interface::types::PubkeyType> signatories,
169 : const crypto::Hash &query_hash) const {
170 14 : return createQueryResponse(
171 : [signatories = std::move(signatories)](
172 : iroha::protocol::QueryResponse &protocol_query_response) {
173 14 : iroha::protocol::SignatoriesResponse *protocol_specific_response =
174 14 : protocol_query_response.mutable_signatories_response();
175 60 : for (const auto &key : signatories) {
176 46 : const auto &blob = key.blob();
177 46 : protocol_specific_response->add_keys(blob.data(), blob.size());
178 : }
179 14 : },
180 14 : query_hash);
181 0 : }
182 :
183 : std::unique_ptr<shared_model::interface::QueryResponse>
184 : shared_model::proto::ProtoQueryResponseFactory::createTransactionsResponse(
185 : std::vector<std::unique_ptr<shared_model::interface::Transaction>>
186 : transactions,
187 : const crypto::Hash &query_hash) const {
188 58 : return createQueryResponse(
189 : [transactions = std::move(transactions)](
190 : iroha::protocol::QueryResponse &protocol_query_response) {
191 58 : iroha::protocol::TransactionsResponse *protocol_specific_response =
192 58 : protocol_query_response.mutable_transactions_response();
193 144 : for (const auto &tx : transactions) {
194 86 : *protocol_specific_response->add_transactions() =
195 86 : static_cast<shared_model::proto::Transaction *>(tx.get())
196 86 : ->getTransport();
197 : }
198 58 : },
199 58 : query_hash);
200 0 : }
201 :
202 : std::unique_ptr<shared_model::interface::QueryResponse>
203 : shared_model::proto::ProtoQueryResponseFactory::createAssetResponse(
204 : const interface::types::AssetIdType asset_id,
205 : const interface::types::DomainIdType domain_id,
206 : const interface::types::PrecisionType precision,
207 : const crypto::Hash &query_hash) const {
208 2 : return createQueryResponse(
209 : [asset_id = std::move(asset_id),
210 2 : domain_id = std::move(domain_id),
211 2 : precision](iroha::protocol::QueryResponse &protocol_query_response) {
212 2 : iroha::protocol::AssetResponse *protocol_specific_response =
213 2 : protocol_query_response.mutable_asset_response();
214 2 : auto *asset = protocol_specific_response->mutable_asset();
215 2 : asset->set_asset_id(std::move(asset_id));
216 2 : asset->set_domain_id(std::move(domain_id));
217 2 : asset->set_precision(precision);
218 2 : },
219 2 : query_hash);
220 0 : }
221 :
222 : std::unique_ptr<shared_model::interface::QueryResponse>
223 : shared_model::proto::ProtoQueryResponseFactory::createRolesResponse(
224 : std::vector<shared_model::interface::types::RoleIdType> roles,
225 : const crypto::Hash &query_hash) const {
226 7 : return createQueryResponse(
227 : [roles = std::move(roles)](
228 : iroha::protocol::QueryResponse &protocol_query_response) mutable {
229 7 : iroha::protocol::RolesResponse *protocol_specific_response =
230 7 : protocol_query_response.mutable_roles_response();
231 26 : for (auto &&role : roles) {
232 19 : protocol_specific_response->add_roles(std::move(role));
233 : }
234 7 : },
235 7 : query_hash);
236 0 : }
237 :
238 : std::unique_ptr<shared_model::interface::QueryResponse>
239 : shared_model::proto::ProtoQueryResponseFactory::createRolePermissionsResponse(
240 : shared_model::interface::RolePermissionSet role_permissions,
241 : const crypto::Hash &query_hash) const {
242 3 : return createQueryResponse(
243 : [role_permissions](
244 : iroha::protocol::QueryResponse &protocol_query_response) {
245 3 : iroha::protocol::RolePermissionsResponse *protocol_specific_response =
246 3 : protocol_query_response.mutable_role_permissions_response();
247 132 : for (size_t i = 0; i < role_permissions.size(); ++i) {
248 129 : auto perm = static_cast<interface::permissions::Role>(i);
249 129 : if (role_permissions.test(perm)) {
250 4 : protocol_specific_response->add_permissions(
251 4 : shared_model::proto::permissions::toTransport(perm));
252 4 : }
253 129 : }
254 3 : },
255 3 : query_hash);
256 : }
257 :
258 : std::unique_ptr<shared_model::interface::BlockQueryResponse>
259 : shared_model::proto::ProtoQueryResponseFactory::createBlockQueryResponse(
260 : std::unique_ptr<shared_model::interface::Block> block) const {
261 : return createQueryResponse([block = std::move(block)](
262 : iroha::protocol::BlockQueryResponse
263 : &protocol_query_response) {
264 716 : iroha::protocol::BlockResponse *protocol_specific_response =
265 716 : protocol_query_response.mutable_block_response();
266 716 : *protocol_specific_response->mutable_block() =
267 716 : static_cast<shared_model::proto::Block *>(block.get())->getTransport();
268 716 : });
269 0 : }
270 :
271 : std::unique_ptr<shared_model::interface::BlockQueryResponse>
272 : shared_model::proto::ProtoQueryResponseFactory::createBlockQueryResponse(
273 : std::string error_message) const {
274 2 : return createQueryResponse(
275 : [error_message = std::move(error_message)](
276 : iroha::protocol::BlockQueryResponse &protocol_query_response) {
277 2 : iroha::protocol::BlockErrorResponse *protocol_specific_response =
278 2 : protocol_query_response.mutable_block_error_response();
279 2 : protocol_specific_response->set_message(error_message);
280 2 : });
281 0 : }
|