Line data Source code
1 : /**
2 : * Copyright Soramitsu Co., Ltd. All Rights Reserved.
3 : * SPDX-License-Identifier: Apache-2.0
4 : */
5 :
6 : #include "model/converters/pb_query_response_factory.hpp"
7 : #include "common/byteutils.hpp"
8 : #include "common/instanceof.hpp"
9 : #include "model/converters/pb_common.hpp"
10 : #include "model/converters/pb_transaction_factory.hpp"
11 :
12 : namespace iroha {
13 : namespace model {
14 : namespace converters {
15 :
16 : boost::optional<protocol::QueryResponse>
17 : PbQueryResponseFactory::serialize(
18 : const std::shared_ptr<QueryResponse> query_response) const {
19 7 : boost::optional<protocol::QueryResponse> response = boost::none;
20 : // TODO 26/09/17 grimadas: refactor #VARIANT
21 7 : if (instanceof <model::ErrorResponse>(*query_response)) {
22 0 : response = boost::make_optional(protocol::QueryResponse());
23 0 : auto er = static_cast<model::ErrorResponse &>(*query_response);
24 0 : auto pb_er = serializeErrorResponse(er);
25 0 : response->mutable_error_response()->CopyFrom(pb_er);
26 0 : }
27 7 : if (instanceof <model::AccountAssetResponse>(*query_response)) {
28 1 : response = boost::make_optional(protocol::QueryResponse());
29 1 : response->mutable_account_assets_response()->CopyFrom(
30 1 : serializeAccountAssetResponse(
31 1 : static_cast<model::AccountAssetResponse &>(*query_response)));
32 1 : }
33 7 : if (instanceof <model::AccountDetailResponse>(*query_response)) {
34 1 : response = boost::make_optional(protocol::QueryResponse());
35 1 : response->mutable_account_detail_response()->CopyFrom(
36 1 : serializeAccountDetailResponse(
37 1 : static_cast<model::AccountDetailResponse &>(
38 1 : *query_response)));
39 1 : }
40 7 : if (instanceof <model::AccountResponse>(*query_response)) {
41 0 : response = boost::make_optional(protocol::QueryResponse());
42 0 : response->mutable_account_response()->CopyFrom(
43 0 : serializeAccountResponse(
44 0 : static_cast<model::AccountResponse &>(*query_response)));
45 0 : }
46 7 : if (instanceof <model::SignatoriesResponse>(*query_response)) {
47 1 : response = boost::make_optional(protocol::QueryResponse());
48 1 : response->mutable_signatories_response()->CopyFrom(
49 1 : serializeSignatoriesResponse(
50 1 : static_cast<model::SignatoriesResponse &>(*query_response)));
51 1 : }
52 7 : if (instanceof <model::TransactionsResponse>(*query_response)) {
53 1 : response = boost::make_optional(protocol::QueryResponse());
54 1 : response->mutable_transactions_response()->CopyFrom(
55 1 : serializeTransactionsResponse(
56 1 : static_cast<model::TransactionsResponse &>(*query_response)));
57 1 : }
58 7 : if (instanceof <model::AssetResponse>(*query_response)) {
59 1 : response = boost::make_optional(protocol::QueryResponse());
60 1 : response->mutable_asset_response()->CopyFrom(serializeAssetResponse(
61 1 : static_cast<model::AssetResponse &>(*query_response)));
62 1 : }
63 7 : if (instanceof <model::RolesResponse>(*query_response)) {
64 1 : response = boost::make_optional(protocol::QueryResponse());
65 1 : response->mutable_roles_response()->CopyFrom(serializeRolesResponse(
66 1 : static_cast<model::RolesResponse &>(*query_response)));
67 1 : }
68 7 : if (instanceof <model::RolePermissionsResponse>(*query_response)) {
69 1 : response = boost::make_optional(protocol::QueryResponse());
70 1 : response->mutable_role_permissions_response()->CopyFrom(
71 1 : serializeRolePermissionsResponse(
72 1 : static_cast<model::RolePermissionsResponse &>(
73 1 : *query_response)));
74 1 : }
75 :
76 7 : if (response) {
77 7 : response->set_query_hash(query_response->query_hash.to_hexstring());
78 7 : }
79 7 : return response;
80 7 : }
81 :
82 : optional_ptr<model::QueryResponse> PbQueryResponseFactory::deserialize(
83 : const protocol::QueryResponse &response) const {
84 : auto p = [](auto &&o) {
85 : using ResponseType = decltype(o);
86 0 : std::shared_ptr<QueryResponse> result =
87 0 : std::make_shared<std::remove_reference_t<ResponseType>>(
88 0 : std::forward<ResponseType>(o));
89 0 : return result;
90 0 : };
91 0 : switch (response.response_case()) {
92 : case protocol::QueryResponse::kAccountAssetsResponse:
93 0 : return p(deserializeAccountAssetResponse(
94 0 : response.account_assets_response()));
95 : case protocol::QueryResponse::kAccountDetailResponse:
96 0 : return p(deserializeAccountDetailResponse(
97 0 : response.account_detail_response()));
98 : case protocol::QueryResponse::kAccountResponse:
99 0 : return p(deserializeAccountResponse(response.account_response()));
100 : case protocol::QueryResponse::kErrorResponse:
101 0 : return p(deserializeErrorResponse(response.error_response()));
102 : case protocol::QueryResponse::kSignatoriesResponse:
103 0 : return p(deserializeSignatoriesResponse(
104 0 : response.signatories_response()));
105 : case protocol::QueryResponse::kAssetResponse:
106 0 : return p(deserializeAssetResponse(response.asset_response()));
107 : case protocol::QueryResponse::kRolesResponse:
108 0 : return p(deserializeRolesResponse(response.roles_response()));
109 : case protocol::QueryResponse::kRolePermissionsResponse:
110 0 : return p(deserializeRolePermissionsResponse(
111 0 : response.role_permissions_response()));
112 : default:
113 0 : return boost::none;
114 : }
115 0 : }
116 :
117 : protocol::Account PbQueryResponseFactory::serializeAccount(
118 : const model::Account &account) const {
119 2 : protocol::Account pb_account;
120 2 : pb_account.set_quorum(account.quorum);
121 2 : pb_account.set_account_id(account.account_id);
122 2 : pb_account.set_domain_id(account.domain_id);
123 2 : pb_account.set_json_data(account.json_data);
124 2 : return pb_account;
125 2 : }
126 :
127 : model::Account PbQueryResponseFactory::deserializeAccount(
128 : const protocol::Account &pb_account) const {
129 2 : model::Account res;
130 2 : res.account_id = pb_account.account_id();
131 2 : res.quorum = pb_account.quorum();
132 2 : res.domain_id = pb_account.domain_id();
133 2 : res.json_data = pb_account.json_data();
134 2 : return res;
135 2 : }
136 :
137 : protocol::AccountResponse
138 : PbQueryResponseFactory::serializeAccountResponse(
139 : const model::AccountResponse &accountResponse) const {
140 1 : protocol::AccountResponse pb_response;
141 1 : pb_response.mutable_account()->CopyFrom(
142 1 : serializeAccount(accountResponse.account));
143 1 : for (auto role : accountResponse.roles) {
144 0 : pb_response.add_account_roles(role);
145 0 : }
146 1 : return pb_response;
147 1 : }
148 :
149 : model::AccountResponse PbQueryResponseFactory::deserializeAccountResponse(
150 : const protocol::AccountResponse pb_response) const {
151 1 : model::AccountResponse accountResponse;
152 1 : accountResponse.account = deserializeAccount(pb_response.account());
153 1 : return accountResponse;
154 1 : }
155 :
156 : protocol::AccountAsset PbQueryResponseFactory::serializeAccountAsset(
157 : const model::AccountAsset &account_asset) const {
158 1 : protocol::AccountAsset pb_account_asset;
159 1 : pb_account_asset.set_account_id(account_asset.account_id);
160 1 : pb_account_asset.set_asset_id(account_asset.asset_id);
161 1 : pb_account_asset.set_balance(account_asset.balance);
162 1 : return pb_account_asset;
163 1 : }
164 :
165 : model::AccountAsset PbQueryResponseFactory::deserializeAccountAsset(
166 : const protocol::AccountAsset &account_asset) const {
167 1 : model::AccountAsset res;
168 1 : res.account_id = account_asset.account_id();
169 1 : res.balance = account_asset.balance();
170 1 : res.asset_id = account_asset.asset_id();
171 1 : return res;
172 1 : }
173 :
174 : protocol::AccountAssetResponse
175 : PbQueryResponseFactory::serializeAccountAssetResponse(
176 : const model::AccountAssetResponse &accountAssetResponse) const {
177 1 : protocol::AccountAssetResponse pb_response;
178 1 : auto pb_account_asset = pb_response.mutable_account_assets();
179 2 : for (auto &asset : accountAssetResponse.acct_assets) {
180 1 : auto pb_asset = new iroha::protocol::AccountAsset();
181 1 : pb_asset->set_asset_id(asset.asset_id);
182 1 : pb_asset->set_account_id(asset.account_id);
183 1 : pb_asset->set_balance(asset.balance);
184 1 : pb_account_asset->AddAllocated(pb_asset);
185 : }
186 1 : return pb_response;
187 1 : }
188 :
189 : model::AccountAssetResponse
190 : PbQueryResponseFactory::deserializeAccountAssetResponse(
191 : const protocol::AccountAssetResponse &account_asset_response) const {
192 1 : model::AccountAssetResponse res;
193 2 : for (int i = 0; i < account_asset_response.account_assets().size();
194 1 : i++) {
195 1 : auto model_asset = iroha::model::AccountAsset();
196 1 : model_asset.balance =
197 1 : account_asset_response.account_assets(i).balance();
198 1 : model_asset.account_id =
199 1 : account_asset_response.account_assets(i).account_id();
200 1 : model_asset.asset_id =
201 1 : account_asset_response.account_assets(i).asset_id();
202 1 : res.acct_assets.push_back(model_asset);
203 1 : }
204 1 : return res;
205 1 : }
206 :
207 : protocol::AccountDetailResponse
208 : PbQueryResponseFactory::serializeAccountDetailResponse(
209 : const model::AccountDetailResponse &accountDetailResponse) const {
210 1 : protocol::AccountDetailResponse pb_response;
211 1 : pb_response.set_detail(accountDetailResponse.detail);
212 1 : return pb_response;
213 1 : }
214 :
215 : model::AccountDetailResponse
216 : PbQueryResponseFactory::deserializeAccountDetailResponse(
217 : const protocol::AccountDetailResponse &account_detail_response)
218 : const {
219 1 : model::AccountDetailResponse res;
220 1 : res.detail = account_detail_response.detail();
221 1 : return res;
222 1 : }
223 :
224 : protocol::SignatoriesResponse
225 : PbQueryResponseFactory::serializeSignatoriesResponse(
226 : const model::SignatoriesResponse &signatoriesResponse) const {
227 1 : protocol::SignatoriesResponse pb_response;
228 :
229 2 : for (auto key : signatoriesResponse.keys) {
230 1 : pb_response.add_keys(key.to_hexstring());
231 : }
232 1 : return pb_response;
233 1 : }
234 :
235 : model::SignatoriesResponse
236 : PbQueryResponseFactory::deserializeSignatoriesResponse(
237 : const protocol::SignatoriesResponse &signatoriesResponse) const {
238 1 : model::SignatoriesResponse res{};
239 2 : for (const auto &key : signatoriesResponse.keys()) {
240 1 : iroha::hexstringToArray<pubkey_t::size()>(key) |
241 : [&](const auto &pubkey) { res.keys.push_back(pubkey); };
242 : }
243 1 : return res;
244 1 : }
245 :
246 : protocol::AssetResponse PbQueryResponseFactory::serializeAssetResponse(
247 : const model::AssetResponse &response) const {
248 1 : protocol::AssetResponse res;
249 1 : auto asset = res.mutable_asset();
250 1 : asset->set_asset_id(response.asset.asset_id);
251 1 : asset->set_domain_id(response.asset.domain_id);
252 1 : asset->set_precision(response.asset.precision);
253 1 : return res;
254 1 : }
255 :
256 : model::AssetResponse PbQueryResponseFactory::deserializeAssetResponse(
257 : const protocol::AssetResponse &response) const {
258 0 : model::AssetResponse res;
259 0 : auto asset = response.asset();
260 0 : res.asset =
261 0 : Asset(asset.asset_id(), asset.domain_id(), asset.precision());
262 0 : return res;
263 0 : }
264 :
265 : protocol::RolesResponse PbQueryResponseFactory::serializeRolesResponse(
266 : const model::RolesResponse &response) const {
267 1 : protocol::RolesResponse res;
268 4 : for (auto role : response.roles) {
269 3 : res.add_roles(role);
270 3 : }
271 1 : return res;
272 1 : }
273 :
274 : model::RolesResponse PbQueryResponseFactory::deserializeRolesResponse(
275 : const protocol::RolesResponse &response) const {
276 0 : model::RolesResponse res{};
277 0 : std::copy(response.roles().begin(),
278 0 : response.roles().end(),
279 0 : res.roles.begin());
280 0 : return res;
281 0 : }
282 :
283 : protocol::RolePermissionsResponse
284 : PbQueryResponseFactory::serializeRolePermissionsResponse(
285 : const model::RolePermissionsResponse &response) const {
286 1 : protocol::RolePermissionsResponse res;
287 3 : for (auto perm : response.role_permissions) {
288 2 : res.add_permissions(iroha::protocol::RolePermission(perm));
289 : }
290 1 : return res;
291 1 : }
292 :
293 : model::RolePermissionsResponse
294 : PbQueryResponseFactory::deserializeRolePermissionsResponse(
295 : const protocol::RolePermissionsResponse &response) const {
296 0 : model::RolePermissionsResponse res;
297 0 : std::copy(response.permissions().begin(),
298 0 : response.permissions().end(),
299 0 : res.role_permissions.begin());
300 0 : return res;
301 0 : }
302 :
303 : protocol::TransactionsResponse
304 : PbQueryResponseFactory::serializeTransactionsResponse(
305 : const model::TransactionsResponse &transactionsResponse) const {
306 1 : PbTransactionFactory pb_transaction_factory;
307 :
308 : // converting observable to the vector using reduce
309 1 : return transactionsResponse.transactions
310 1 : .reduce(protocol::TransactionsResponse(),
311 : [&pb_transaction_factory](auto &&response, auto tx) {
312 3 : response.add_transactions()->CopyFrom(
313 3 : pb_transaction_factory.serialize(tx));
314 3 : return response;
315 0 : },
316 : [](auto &&response) { return response; })
317 1 : .as_blocking() // we need to wait when on_complete happens
318 1 : .first();
319 0 : }
320 :
321 : protocol::ErrorResponse PbQueryResponseFactory::serializeErrorResponse(
322 : const model::ErrorResponse &errorResponse) const {
323 0 : protocol::ErrorResponse pb_response;
324 0 : switch (errorResponse.reason) {
325 : case ErrorResponse::NO_ASSET:
326 0 : pb_response.set_reason(protocol::ErrorResponse::NO_ASSET);
327 0 : break;
328 : case ErrorResponse::STATELESS_INVALID:
329 0 : pb_response.set_reason(protocol::ErrorResponse::STATELESS_INVALID);
330 0 : break;
331 : case ErrorResponse::STATEFUL_INVALID:
332 0 : pb_response.set_reason(protocol::ErrorResponse::STATEFUL_INVALID);
333 0 : break;
334 : case ErrorResponse::NO_ACCOUNT:
335 0 : pb_response.set_reason(protocol::ErrorResponse::NO_ACCOUNT);
336 0 : break;
337 : case ErrorResponse::NO_ACCOUNT_ASSETS:
338 0 : pb_response.set_reason(protocol::ErrorResponse::NO_ACCOUNT_ASSETS);
339 0 : break;
340 : case ErrorResponse::NO_ACCOUNT_DETAIL:
341 0 : pb_response.set_reason(protocol::ErrorResponse::NO_ACCOUNT_DETAIL);
342 0 : break;
343 : case ErrorResponse::NO_SIGNATORIES:
344 0 : pb_response.set_reason(protocol::ErrorResponse::NO_SIGNATORIES);
345 0 : break;
346 : case ErrorResponse::NOT_SUPPORTED:
347 0 : pb_response.set_reason(protocol::ErrorResponse::NOT_SUPPORTED);
348 0 : break;
349 : case ErrorResponse::NO_ROLES:
350 0 : pb_response.set_reason(protocol::ErrorResponse::NO_ROLES);
351 0 : break;
352 : }
353 0 : return pb_response;
354 0 : }
355 :
356 : model::ErrorResponse PbQueryResponseFactory::deserializeErrorResponse(
357 : const protocol::ErrorResponse &response) const {
358 0 : model::ErrorResponse result{};
359 0 : switch (response.reason()) {
360 : case protocol::ErrorResponse_Reason_STATELESS_INVALID:
361 0 : result.reason = ErrorResponse::STATELESS_INVALID;
362 0 : break;
363 : case protocol::ErrorResponse_Reason_STATEFUL_INVALID:
364 0 : result.reason = ErrorResponse::STATEFUL_INVALID;
365 0 : break;
366 : case protocol::ErrorResponse_Reason_NO_ACCOUNT:
367 0 : result.reason = ErrorResponse::NO_ACCOUNT;
368 0 : break;
369 : case protocol::ErrorResponse_Reason_NO_ACCOUNT_ASSETS:
370 0 : result.reason = ErrorResponse::NO_ACCOUNT_ASSETS;
371 0 : break;
372 : case protocol::ErrorResponse_Reason_NO_ACCOUNT_DETAIL:
373 0 : result.reason = ErrorResponse::NO_ACCOUNT_DETAIL;
374 0 : break;
375 : case protocol::ErrorResponse_Reason_NO_SIGNATORIES:
376 0 : result.reason = ErrorResponse::NO_SIGNATORIES;
377 0 : break;
378 : case protocol::ErrorResponse_Reason_NOT_SUPPORTED:
379 0 : result.reason = ErrorResponse::NOT_SUPPORTED;
380 0 : break;
381 : case protocol::ErrorResponse_Reason_NO_ASSET:
382 0 : result.reason = ErrorResponse::NO_ASSET;
383 0 : break;
384 : case protocol::ErrorResponse_Reason_NO_ROLES:
385 0 : result.reason = ErrorResponse::NO_ROLES;
386 0 : break;
387 : default:
388 0 : break;
389 : }
390 0 : return result;
391 0 : }
392 : } // namespace converters
393 : } // namespace model
394 : } // namespace iroha
|