LCOV - code coverage report
Current view: top level - irohad/consensus/yac/transport - yac_pb_converters.hpp (source / functions) Hit Total Coverage
Test: cleared_cor.info Lines: 67 68 98.5 %
Date: 2019-03-07 14:46:43 Functions: 9 11 81.8 %

          Line data    Source code
       1             : /**
       2             :  * Copyright Soramitsu Co., Ltd. All Rights Reserved.
       3             :  * SPDX-License-Identifier: Apache-2.0
       4             :  */
       5             : 
       6             : #ifndef IROHA_YAC_PB_CONVERTERS_HPP
       7             : #define IROHA_YAC_PB_CONVERTERS_HPP
       8             : 
       9             : #include "backend/protobuf/common_objects/proto_common_objects_factory.hpp"
      10             : #include "common/byteutils.hpp"
      11             : #include "consensus/yac/outcome_messages.hpp"
      12             : #include "cryptography/crypto_provider/crypto_defaults.hpp"
      13             : #include "interfaces/common_objects/signature.hpp"
      14             : #include "logger/logger.hpp"
      15             : #include "validators/field_validator.hpp"
      16             : #include "yac.pb.h"
      17             : 
      18             : namespace iroha {
      19             :   namespace consensus {
      20             :     namespace yac {
      21             :       class PbConverters {
      22             :        private:
      23             :         static inline proto::Vote serializeRoundAndHashes(
      24             :             const VoteMessage &vote) {
      25       19099 :           proto::Vote pb_vote;
      26             : 
      27       19100 :           auto hash = pb_vote.mutable_hash();
      28       19101 :           auto hash_round = hash->mutable_vote_round();
      29       19101 :           hash_round->set_block_round(vote.hash.vote_round.block_round);
      30       19101 :           hash_round->set_reject_round(vote.hash.vote_round.reject_round);
      31       19101 :           auto hash_vote_hashes = hash->mutable_vote_hashes();
      32       19101 :           hash_vote_hashes->set_proposal(vote.hash.vote_hashes.proposal_hash);
      33       19101 :           hash_vote_hashes->set_block(vote.hash.vote_hashes.block_hash);
      34             : 
      35       19101 :           return pb_vote;
      36       19101 :         }
      37             : 
      38             :         static inline VoteMessage deserealizeRoundAndHashes(
      39             :             const proto::Vote &pb_vote) {
      40        8406 :           VoteMessage vote;
      41             : 
      42        8407 :           vote.hash.vote_round =
      43        8410 :               Round{pb_vote.hash().vote_round().block_round(),
      44        8410 :                     pb_vote.hash().vote_round().reject_round()};
      45        8411 :           vote.hash.vote_hashes =
      46        8411 :               YacHash::VoteHashes{pb_vote.hash().vote_hashes().proposal(),
      47        8411 :                                   pb_vote.hash().vote_hashes().block()};
      48             : 
      49        8411 :           return vote;
      50        8411 :         }
      51             : 
      52             :        public:
      53             :         static proto::Vote serializeVotePayload(const VoteMessage &vote) {
      54        3564 :           auto pb_vote = serializeRoundAndHashes(vote);
      55             : 
      56        3564 :           if (vote.hash.block_signature) {
      57        1153 :             auto block_signature =
      58        1153 :                 pb_vote.mutable_hash()->mutable_block_signature();
      59        1153 :             block_signature->set_signature(shared_model::crypto::toBinaryString(
      60        1153 :                 vote.hash.block_signature->signedData()));
      61        1153 :             block_signature->set_pubkey(shared_model::crypto::toBinaryString(
      62        1153 :                 vote.hash.block_signature->publicKey()));
      63        1153 :           }
      64             : 
      65        3564 :           return pb_vote;
      66        3564 :         }
      67             : 
      68             :         static proto::Vote serializeVote(const VoteMessage &vote) {
      69       15537 :           auto pb_vote = serializeRoundAndHashes(vote);
      70             : 
      71       15537 :           if (vote.hash.block_signature) {
      72        5043 :             auto block_signature =
      73        5043 :                 pb_vote.mutable_hash()->mutable_block_signature();
      74        5043 :             block_signature->set_signature(shared_model::crypto::toBinaryString(
      75        5043 :                 vote.hash.block_signature->signedData()));
      76        5043 :             block_signature->set_pubkey(shared_model::crypto::toBinaryString(
      77        5043 :                 vote.hash.block_signature->publicKey()));
      78        5043 :           }
      79             : 
      80       15537 :           auto signature = pb_vote.mutable_signature();
      81       15537 :           const auto &sig = *vote.signature;
      82       15537 :           signature->set_signature(
      83       15537 :               shared_model::crypto::toBinaryString(sig.signedData()));
      84       15537 :           signature->set_pubkey(
      85       15537 :               shared_model::crypto::toBinaryString(sig.publicKey()));
      86             : 
      87       15537 :           return pb_vote;
      88       15537 :         }
      89             : 
      90             :         static boost::optional<VoteMessage> deserializeVote(
      91             :             const proto::Vote &pb_vote, logger::LoggerPtr log) {
      92        8412 :           static shared_model::proto::ProtoCommonObjectsFactory<
      93             :               shared_model::validation::FieldValidator>
      94          34 :               factory_;
      95             : 
      96        8396 :           auto vote = deserealizeRoundAndHashes(pb_vote);
      97             : 
      98             :           auto deserialize =
      99             :               [&](auto &pubkey, auto &signature, auto &val, const auto &msg) {
     100        8413 :                 factory_
     101        7935 :                     .createSignature(shared_model::crypto::PublicKey(pubkey),
     102        7935 :                                      shared_model::crypto::Signed(signature))
     103        8397 :                     .match(
     104             :                         [&](iroha::expected::Value<
     105             :                             std::unique_ptr<shared_model::interface::Signature>>
     106        8411 :                                 &sig) { val = std::move(sig.value); },
     107             :                         [&](iroha::expected::Error<std::string> &reason) {
     108        8397 :                           log->error(msg, reason.error);
     109           0 :                         });
     110        8413 :               };
     111             : 
     112        8413 :           if (pb_vote.hash().has_block_signature()) {
     113        2737 :             deserialize(pb_vote.hash().block_signature().pubkey(),
     114        2736 :                         pb_vote.hash().block_signature().signature(),
     115        2283 :                         vote.hash.block_signature,
     116             :                         "Cannot build vote hash block signature: {}");
     117        2738 :           }
     118             : 
     119        8411 :           deserialize(pb_vote.signature().pubkey(),
     120        8410 :                       pb_vote.signature().signature(),
     121        7606 :                       vote.signature,
     122             :                       "Cannot build vote signature: {}");
     123             : 
     124        8406 :           return vote;
     125        8412 :         }
     126             :       };
     127             :     }  // namespace yac
     128             :   }    // namespace consensus
     129             : }  // namespace iroha
     130             : 
     131             : #endif  // IROHA_YAC_PB_CONVERTERS_HPP

Generated by: LCOV version 1.13