LCOV - code coverage report
Current view: top level - irohad/model/converters/impl - pb_query_factory.cpp (source / functions) Hit Total Coverage
Test: cleared_cor.info Lines: 155 175 88.6 %
Date: 2019-03-07 14:46:43 Functions: 16 17 94.1 %

          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_factory.hpp"
       7             : 
       8             : #include "logger/logger.hpp"
       9             : #include "model/common.hpp"
      10             : #include "model/queries/get_account.hpp"
      11             : #include "model/queries/get_account_assets.hpp"
      12             : #include "model/queries/get_account_detail.hpp"
      13             : #include "model/queries/get_asset_info.hpp"
      14             : #include "model/queries/get_roles.hpp"
      15             : #include "model/queries/get_signatories.hpp"
      16             : #include "model/queries/get_transactions.hpp"
      17             : #include "queries.pb.h"
      18             : 
      19             : namespace iroha {
      20             :   namespace model {
      21             :     namespace converters {
      22             : 
      23             :       PbQueryFactory::PbQueryFactory(logger::LoggerPtr log)
      24          15 :           : log_{std::move(log)} {
      25          15 :         serializers_[typeid(GetAccount)] = &PbQueryFactory::serializeGetAccount;
      26          15 :         serializers_[typeid(GetAccountAssets)] =
      27             :             &PbQueryFactory::serializeGetAccountAssets;
      28          15 :         serializers_[typeid(GetAccountDetail)] =
      29             :             &PbQueryFactory::serializeGetAccountDetail;
      30          15 :         serializers_[typeid(GetAccountTransactions)] =
      31             :             &PbQueryFactory::serializeGetAccountTransactions;
      32          15 :         serializers_[typeid(GetAccountAssetTransactions)] =
      33             :             &PbQueryFactory::serializeGetAccountAssetTransactions;
      34          15 :         serializers_[typeid(GetTransactions)] =
      35             :             &PbQueryFactory::serializeGetTransactions;
      36          15 :         serializers_[typeid(GetSignatories)] =
      37             :             &PbQueryFactory::serializeGetSignatories;
      38          15 :         serializers_[typeid(GetRolePermissions)] =
      39             :             &PbQueryFactory::serializeGetRolePermissions;
      40          15 :         serializers_[typeid(GetAssetInfo)] =
      41             :             &PbQueryFactory::serializeGetAssetInfo;
      42          15 :         serializers_[typeid(GetRoles)] = &PbQueryFactory::serializeGetRoles;
      43          15 :       }
      44             : 
      45             :       optional_ptr<model::Query> PbQueryFactory::deserialize(
      46             :           const protocol::Query &pb_query) const {
      47           9 :         std::shared_ptr<model::Query> val;
      48             : 
      49           9 :         const auto &pl = pb_query.payload();
      50             : 
      51             :         {
      52             :           using protocol::Query_Payload;
      53           9 :           switch (pl.query_case()) {
      54             :             case Query_Payload::QueryCase::kGetAccount: {
      55             :               // Convert to get Account
      56           1 :               const auto &pb_cast = pl.get_account();
      57           1 :               auto account_query = GetAccount();
      58           1 :               account_query.account_id = pb_cast.account_id();
      59           1 :               val = std::make_shared<model::GetAccount>(account_query);
      60             :               break;
      61           1 :             }
      62             :             case Query_Payload::QueryCase::kGetAccountAssets: {
      63             :               // Convert to get Account Asset
      64           1 :               const auto &pb_cast = pl.get_account_assets();
      65           1 :               auto query = GetAccountAssets();
      66           1 :               query.account_id = pb_cast.account_id();
      67           1 :               val = std::make_shared<model::GetAccountAssets>(query);
      68             :               break;
      69           1 :             }
      70             :             case Query_Payload::QueryCase::kGetAccountDetail: {
      71             :               // Convert to get Account Detail
      72           1 :               const auto &pb_cast = pl.get_account_detail();
      73           1 :               auto query = GetAccountDetail();
      74           1 :               query.account_id = pb_cast.account_id();
      75           1 :               val = std::make_shared<model::GetAccountDetail>(query);
      76             :               break;
      77           1 :             }
      78             :             case Query_Payload::QueryCase::kGetAccountAssetTransactions: {
      79           0 :               const auto &pb_cast = pl.get_account_asset_transactions();
      80           0 :               auto query = GetAccountAssetTransactions();
      81           0 :               query.account_id = pb_cast.account_id();
      82           0 :               query.asset_id = pb_cast.asset_id();
      83           0 :               val = std::make_shared<model::GetAccountAssetTransactions>(query);
      84             :               break;
      85           0 :             }
      86             :             case Query_Payload::QueryCase::kGetSignatories: {
      87             :               // Convert to get Signatories
      88           1 :               const auto &pb_cast = pl.get_signatories();
      89           1 :               auto query = GetSignatories();
      90           1 :               query.account_id = pb_cast.account_id();
      91           1 :               val = std::make_shared<model::GetSignatories>(query);
      92             :               break;
      93           1 :             }
      94             :             case Query_Payload::QueryCase::kGetAccountTransactions: {
      95             :               // Convert to get Signatories
      96           1 :               const auto &pb_cast = pl.get_account_transactions();
      97           1 :               auto query = GetAccountTransactions();
      98           1 :               query.account_id = pb_cast.account_id();
      99           1 :               val = std::make_shared<model::GetAccountTransactions>(query);
     100             :               break;
     101           1 :             }
     102             :             case Query_Payload::QueryCase::kGetTransactions: {
     103             :               // Convert to get transactions
     104           1 :               const auto &pb_cast = pl.get_transactions();
     105           1 :               auto query = GetTransactions();
     106           1 :               std::transform(pb_cast.tx_hashes().begin(),
     107           1 :                              pb_cast.tx_hashes().end(),
     108           1 :                              std::back_inserter(query.tx_hashes),
     109             :                              [](auto tx_hash) {
     110           2 :                                return iroha::hash256_t::from_hexstring(tx_hash);
     111             :                              });
     112           1 :               val = std::make_shared<GetTransactions>(query);
     113             :               break;
     114           1 :             }
     115             :             case Query_Payload::QueryCase::kGetRoles: {
     116           1 :               val = std::make_shared<GetRoles>();
     117           1 :               break;
     118             :             }
     119             :             case Query_Payload::QueryCase::kGetAssetInfo: {
     120             :               // Convert to get asset info
     121           1 :               const auto &pb_cast = pl.get_asset_info();
     122           1 :               val = std::make_shared<GetAssetInfo>(pb_cast.asset_id());
     123           1 :               break;
     124             :             }
     125             :             case Query_Payload::QueryCase::kGetRolePermissions: {
     126           1 :               const auto &pb_cast = pl.get_role_permissions();
     127           1 :               val = std::make_shared<GetRolePermissions>(pb_cast.role_id());
     128           1 :               break;
     129             :             }
     130             :             default: {
     131             :               // Query not implemented
     132           0 :               return boost::none;
     133             :             }
     134             :           }
     135             :         }
     136             : 
     137           9 :         const auto &pb_sign = pb_query.signature();
     138             : 
     139           9 :         Signature sign{};
     140           9 :         sign.pubkey = pubkey_t::from_hexstring(pb_sign.public_key());
     141           9 :         sign.signature = sig_t::from_hexstring(pb_sign.signature());
     142             : 
     143           9 :         val->query_counter = pl.meta().query_counter();
     144           9 :         val->signature = sign;
     145           9 :         val->created_ts = pl.meta().created_time();
     146           9 :         val->creator_account_id = pl.meta().creator_account_id();
     147           9 :         return val;
     148           9 :       }
     149             : 
     150             :       void PbQueryFactory::serializeQueryMetaData(
     151             :           protocol::Query &pb_query, std::shared_ptr<const Query> query) const {
     152          49 :         auto *meta = pb_query.mutable_payload()->mutable_meta();
     153          49 :         meta->set_created_time(query->created_ts);
     154          49 :         meta->set_creator_account_id(query->creator_account_id);
     155          49 :         meta->set_query_counter(query->query_counter);
     156             :         // Set signatures
     157          49 :         auto sig = pb_query.mutable_signature();
     158          49 :         sig->set_signature(query->signature.signature.to_hexstring());
     159          49 :         sig->set_public_key(query->signature.pubkey.to_hexstring());
     160          49 :       }
     161             : 
     162             :       boost::optional<protocol::Query> PbQueryFactory::serialize(
     163             :           std::shared_ptr<const Query> query) const {
     164          49 :         auto it = serializers_.find(typeid(*query));
     165          49 :         if (it != serializers_.end()) {
     166          49 :           return (this->*it->second)(query);
     167             :         }
     168           0 :         log_->error("Query type not found");
     169           0 :         return boost::none;
     170          49 :       }
     171             : 
     172             :       protocol::Query PbQueryFactory::serializeGetAccount(
     173             :           std::shared_ptr<const Query> query) const {
     174          11 :         protocol::Query pb_query;
     175          11 :         auto pl = pb_query.mutable_payload();
     176          11 :         serializeQueryMetaData(pb_query, query);
     177          11 :         auto tmp = std::static_pointer_cast<const GetAccount>(query);
     178          11 :         auto account_id = tmp->account_id;
     179          11 :         auto pb_query_mut = pl->mutable_get_account();
     180          11 :         pb_query_mut->set_account_id(account_id);
     181          11 :         return pb_query;
     182          11 :       }
     183             : 
     184             :       protocol::Query PbQueryFactory::serializeGetAccountAssets(
     185             :           std::shared_ptr<const Query> query) const {
     186           5 :         protocol::Query pb_query;
     187           5 :         serializeQueryMetaData(pb_query, query);
     188           5 :         auto tmp = std::static_pointer_cast<const GetAccountAssets>(query);
     189           5 :         auto pb_query_mut =
     190           5 :             pb_query.mutable_payload()->mutable_get_account_assets();
     191           5 :         pb_query_mut->set_account_id(tmp->account_id);
     192           5 :         return pb_query;
     193           5 :       }
     194             : 
     195             :       protocol::Query PbQueryFactory::serializeGetAccountDetail(
     196             :           std::shared_ptr<const Query> query) const {
     197           3 :         protocol::Query pb_query;
     198           3 :         serializeQueryMetaData(pb_query, query);
     199           3 :         auto tmp = std::static_pointer_cast<const GetAccountDetail>(query);
     200           3 :         auto pb_query_mut =
     201           3 :             pb_query.mutable_payload()->mutable_get_account_detail();
     202           3 :         pb_query_mut->set_account_id(tmp->account_id);
     203           3 :         return pb_query;
     204           3 :       }
     205             : 
     206             :       protocol::Query PbQueryFactory::serializeGetAccountTransactions(
     207             :           std::shared_ptr<const Query> query) const {
     208           5 :         protocol::Query pb_query;
     209           5 :         serializeQueryMetaData(pb_query, query);
     210             :         auto tmp =
     211           5 :             std::static_pointer_cast<const GetAccountTransactions>(query);
     212           5 :         auto pb_query_mut =
     213           5 :             pb_query.mutable_payload()->mutable_get_account_transactions();
     214           5 :         pb_query_mut->set_account_id(tmp->account_id);
     215           5 :         return pb_query;
     216           5 :       }
     217             : 
     218             :       protocol::Query PbQueryFactory::serializeGetAccountAssetTransactions(
     219             :           std::shared_ptr<const Query> query) const {
     220           0 :         protocol::Query pb_query;
     221           0 :         serializeQueryMetaData(pb_query, query);
     222             :         auto tmp =
     223           0 :             std::static_pointer_cast<const GetAccountAssetTransactions>(query);
     224           0 :         auto account_id = tmp->account_id;
     225           0 :         auto asset_id = tmp->asset_id;
     226           0 :         auto pb_query_mut = pb_query.mutable_payload()
     227           0 :                                 ->mutable_get_account_asset_transactions();
     228           0 :         pb_query_mut->set_account_id(account_id);
     229           0 :         pb_query_mut->set_asset_id(asset_id);
     230           0 :         return pb_query;
     231           0 :       }
     232             : 
     233             :       protocol::Query PbQueryFactory::serializeGetTransactions(
     234             :           std::shared_ptr<const Query> query) const {
     235           5 :         protocol::Query pb_query;
     236           5 :         serializeQueryMetaData(pb_query, query);
     237           5 :         auto tmp = std::static_pointer_cast<const GetTransactions>(query);
     238           5 :         auto pb_query_mut =
     239           5 :             pb_query.mutable_payload()->mutable_get_transactions();
     240           5 :         std::for_each(tmp->tx_hashes.begin(),
     241           5 :                       tmp->tx_hashes.end(),
     242             :                       [&pb_query_mut](auto tx_hash) {
     243          10 :                         auto adder = pb_query_mut->add_tx_hashes();
     244          10 :                         *adder = tx_hash.to_hexstring();
     245          10 :                       });
     246           5 :         return pb_query;
     247           5 :       }
     248             : 
     249             :       protocol::Query PbQueryFactory::serializeGetSignatories(
     250             :           std::shared_ptr<const Query> query) const {
     251           5 :         protocol::Query pb_query;
     252           5 :         serializeQueryMetaData(pb_query, query);
     253           5 :         auto tmp = std::static_pointer_cast<const GetSignatories>(query);
     254           5 :         auto pb_query_mut =
     255           5 :             pb_query.mutable_payload()->mutable_get_signatories();
     256           5 :         pb_query_mut->set_account_id(tmp->account_id);
     257           5 :         return pb_query;
     258           5 :       }
     259             : 
     260             :       protocol::Query PbQueryFactory::serializeGetAssetInfo(
     261             :           std::shared_ptr<const Query> query) const {
     262           5 :         protocol::Query pb_query;
     263           5 :         serializeQueryMetaData(pb_query, query);
     264           5 :         auto tmp = std::static_pointer_cast<const GetAssetInfo>(query);
     265           5 :         auto ast_id = tmp->asset_id;
     266           5 :         auto pb_query_mut =
     267           5 :             pb_query.mutable_payload()->mutable_get_asset_info();
     268           5 :         pb_query_mut->set_asset_id(ast_id);
     269           5 :         return pb_query;
     270           5 :       }
     271             : 
     272             :       protocol::Query PbQueryFactory::serializeGetRoles(
     273             :           std::shared_ptr<const Query> query) const {
     274           5 :         protocol::Query pb_query;
     275           5 :         pb_query.mutable_payload()->mutable_get_roles();
     276           5 :         serializeQueryMetaData(pb_query, query);
     277           5 :         auto tmp = std::static_pointer_cast<const GetRoles>(query);
     278           5 :         return pb_query;
     279           5 :       }
     280             : 
     281             :       protocol::Query PbQueryFactory::serializeGetRolePermissions(
     282             :           std::shared_ptr<const Query> query) const {
     283           5 :         protocol::Query pb_query;
     284           5 :         serializeQueryMetaData(pb_query, query);
     285           5 :         auto tmp = std::static_pointer_cast<const GetRolePermissions>(query);
     286           5 :         auto role = tmp->role_id;
     287           5 :         auto pb_query_mut =
     288           5 :             pb_query.mutable_payload()->mutable_get_role_permissions();
     289           5 :         pb_query_mut->set_role_id(role);
     290           5 :         return pb_query;
     291           5 :       }
     292             : 
     293             :     }  // namespace converters
     294             :   }    // namespace model
     295             : }  // namespace iroha

Generated by: LCOV version 1.13