LCOV - code coverage report
Current view: top level - shared_model/backend/protobuf/impl - proto_query_response_factory.cpp (source / functions) Hit Total Coverage
Test: cleared_cor.info Lines: 155 172 90.1 %
Date: 2019-03-07 14:46:43 Functions: 68 68 100.0 %

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

Generated by: LCOV version 1.13