LCOV - code coverage report
Current view: top level - irohad/consensus/yac/transport - yac_pb_converters.hpp (source / functions) Hit Total Coverage
Test: coverage_cleared.info Lines: 61 63 96.8 %
Date: 2018-12-05 17:11:35 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/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        3563 :           proto::Vote pb_vote;
      26             : 
      27        3563 :           auto hash = pb_vote.mutable_hash();
      28        3563 :           auto hash_round = hash->mutable_vote_round();
      29        3563 :           hash_round->set_block_round(vote.hash.vote_round.block_round);
      30        3563 :           hash_round->set_reject_round(vote.hash.vote_round.reject_round);
      31        3563 :           auto hash_vote_hashes = hash->mutable_vote_hashes();
      32        3563 :           hash_vote_hashes->set_proposal(vote.hash.vote_hashes.proposal_hash);
      33        3563 :           hash_vote_hashes->set_block(vote.hash.vote_hashes.block_hash);
      34             : 
      35        3563 :           return pb_vote;
      36        3563 :         }
      37             : 
      38             :         static inline VoteMessage deserealizeRoundAndHashes(
      39             :             const proto::Vote &pb_vote) {
      40        1429 :           VoteMessage vote;
      41             : 
      42        1429 :           vote.hash.vote_round =
      43        1429 :               Round{pb_vote.hash().vote_round().block_round(),
      44        1429 :                     pb_vote.hash().vote_round().reject_round()};
      45        1429 :           vote.hash.vote_hashes =
      46        1429 :               YacHash::VoteHashes{pb_vote.hash().vote_hashes().proposal(),
      47        1429 :                                   pb_vote.hash().vote_hashes().block()};
      48             : 
      49        1429 :           return vote;
      50        1429 :         }
      51             : 
      52             :        public:
      53             :         static proto::Vote serializeVotePayload(const VoteMessage &vote) {
      54         712 :           auto pb_vote = serializeRoundAndHashes(vote);
      55             : 
      56         712 :           auto block_signature =
      57         712 :               pb_vote.mutable_hash()->mutable_block_signature();
      58         712 :           block_signature->set_signature(shared_model::crypto::toBinaryString(
      59         712 :               vote.hash.block_signature->signedData()));
      60         712 :           block_signature->set_pubkey(shared_model::crypto::toBinaryString(
      61         712 :               vote.hash.block_signature->publicKey()));
      62             : 
      63         712 :           return pb_vote;
      64         712 :         }
      65             : 
      66             :         static proto::Vote serializeVote(const VoteMessage &vote) {
      67        2851 :           auto pb_vote = serializeRoundAndHashes(vote);
      68             : 
      69        2851 :           auto block_signature =
      70        2851 :               pb_vote.mutable_hash()->mutable_block_signature();
      71        2851 :           block_signature->set_signature(shared_model::crypto::toBinaryString(
      72        2851 :               vote.hash.block_signature->signedData()));
      73        2851 :           block_signature->set_pubkey(shared_model::crypto::toBinaryString(
      74        2851 :               vote.hash.block_signature->publicKey()));
      75             : 
      76        2851 :           auto signature = pb_vote.mutable_signature();
      77        2851 :           const auto &sig = *vote.signature;
      78        2851 :           signature->set_signature(
      79        2851 :               shared_model::crypto::toBinaryString(sig.signedData()));
      80        2851 :           signature->set_pubkey(
      81        2851 :               shared_model::crypto::toBinaryString(sig.publicKey()));
      82             : 
      83        2851 :           return pb_vote;
      84        2851 :         }
      85             : 
      86             :         static boost::optional<VoteMessage> deserializeVote(
      87             :             const proto::Vote &pb_vote) {
      88        1429 :           static shared_model::proto::ProtoCommonObjectsFactory<
      89             :               shared_model::validation::FieldValidator>
      90          31 :               factory_;
      91             : 
      92        1429 :           auto vote = deserealizeRoundAndHashes(pb_vote);
      93             : 
      94             :           auto deserialize =
      95             :               [&](auto &pubkey, auto &signature, auto &val, const auto &msg) {
      96        1429 :                 factory_
      97        1428 :                     .createSignature(shared_model::crypto::PublicKey(pubkey),
      98        1427 :                                      shared_model::crypto::Signed(signature))
      99        1429 :                     .match(
     100             :                         [&](iroha::expected::Value<
     101             :                             std::unique_ptr<shared_model::interface::Signature>>
     102        1429 :                                 &sig) { val = std::move(sig.value); },
     103             :                         [&](iroha::expected::Error<std::string> &reason) {
     104           0 :                           logger::log("YacPbConverter::deserializeVote")
     105        1429 :                               ->error(msg, reason.error);
     106           0 :                         });
     107        1429 :               };
     108             : 
     109        1429 :           deserialize(pb_vote.hash().block_signature().pubkey(),
     110        1429 :                       pb_vote.hash().block_signature().signature(),
     111        1426 :                       vote.hash.block_signature,
     112             :                       "Cannot build vote hash block signature: {}");
     113             : 
     114        1429 :           deserialize(pb_vote.signature().pubkey(),
     115        1429 :                       pb_vote.signature().signature(),
     116        1425 :                       vote.signature,
     117             :                       "Cannot build vote signature: {}");
     118             : 
     119        1429 :           return vote;
     120        1429 :         }
     121             :       };
     122             :     }  // namespace yac
     123             :   }    // namespace consensus
     124             : }  // namespace iroha
     125             : 
     126             : #endif  // IROHA_YAC_PB_CONVERTERS_HPP

Generated by: LCOV version 1.13