hyperledger/iroha
Iroha - A simple, decentralized ledger http://iroha.tech
yac_pb_converters.hpp
Go to the documentation of this file.
1 
6 #ifndef IROHA_YAC_PB_CONVERTERS_HPP
7 #define IROHA_YAC_PB_CONVERTERS_HPP
8 
10 #include "common/byteutils.hpp"
14 #include "logger/logger.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  proto::Vote pb_vote;
26 
27  auto hash = pb_vote.mutable_hash();
28  auto hash_round = hash->mutable_vote_round();
29  hash_round->set_block_round(vote.hash.vote_round.block_round);
30  hash_round->set_reject_round(vote.hash.vote_round.reject_round);
31  auto hash_vote_hashes = hash->mutable_vote_hashes();
32  hash_vote_hashes->set_proposal(vote.hash.vote_hashes.proposal_hash);
33  hash_vote_hashes->set_block(vote.hash.vote_hashes.block_hash);
34 
35  return pb_vote;
36  }
37 
38  static inline VoteMessage deserealizeRoundAndHashes(
39  const proto::Vote &pb_vote) {
40  VoteMessage vote;
41 
42  vote.hash.vote_round =
43  Round{pb_vote.hash().vote_round().block_round(),
44  pb_vote.hash().vote_round().reject_round()};
45  vote.hash.vote_hashes =
46  YacHash::VoteHashes{pb_vote.hash().vote_hashes().proposal(),
47  pb_vote.hash().vote_hashes().block()};
48 
49  return vote;
50  }
51 
52  public:
53  static proto::Vote serializeVotePayload(const VoteMessage &vote) {
54  auto pb_vote = serializeRoundAndHashes(vote);
55 
56  if (vote.hash.block_signature) {
57  auto block_signature =
58  pb_vote.mutable_hash()->mutable_block_signature();
59  block_signature->set_signature(shared_model::crypto::toBinaryString(
60  vote.hash.block_signature->signedData()));
61  block_signature->set_pubkey(shared_model::crypto::toBinaryString(
62  vote.hash.block_signature->publicKey()));
63  }
64 
65  return pb_vote;
66  }
67 
68  static proto::Vote serializeVote(const VoteMessage &vote) {
69  auto pb_vote = serializeRoundAndHashes(vote);
70 
71  if (vote.hash.block_signature) {
72  auto block_signature =
73  pb_vote.mutable_hash()->mutable_block_signature();
74  block_signature->set_signature(shared_model::crypto::toBinaryString(
75  vote.hash.block_signature->signedData()));
76  block_signature->set_pubkey(shared_model::crypto::toBinaryString(
77  vote.hash.block_signature->publicKey()));
78  }
79 
80  auto signature = pb_vote.mutable_signature();
81  const auto &sig = *vote.signature;
82  signature->set_signature(
83  shared_model::crypto::toBinaryString(sig.signedData()));
84  signature->set_pubkey(
85  shared_model::crypto::toBinaryString(sig.publicKey()));
86 
87  return pb_vote;
88  }
89 
90  static boost::optional<VoteMessage> deserializeVote(
91  const proto::Vote &pb_vote) {
94  factory_;
95 
96  auto vote = deserealizeRoundAndHashes(pb_vote);
97 
98  auto deserialize =
99  [&](auto &pubkey, auto &signature, auto &val, const auto &msg) {
100  factory_
101  .createSignature(shared_model::crypto::PublicKey(pubkey),
102  shared_model::crypto::Signed(signature))
103  .match(
104  [&](iroha::expected::Value<
105  std::unique_ptr<shared_model::interface::Signature>>
106  &sig) { val = std::move(sig.value); },
107  [&](iroha::expected::Error<std::string> &reason) {
108  logger::log("YacPbConverter::deserializeVote")
109  ->error(msg, reason.error);
110  });
111  };
112 
113  if (pb_vote.hash().has_block_signature()) {
114  deserialize(pb_vote.hash().block_signature().pubkey(),
115  pb_vote.hash().block_signature().signature(),
116  vote.hash.block_signature,
117  "Cannot build vote hash block signature: {}");
118  }
119 
120  deserialize(pb_vote.signature().pubkey(),
121  pb_vote.signature().signature(),
122  vote.signature,
123  "Cannot build vote signature: {}");
124 
125  return vote;
126  }
127  };
128  } // namespace yac
129  } // namespace consensus
130 } // namespace iroha
131 
132 #endif // IROHA_YAC_PB_CONVERTERS_HPP
static boost::optional< VoteMessage > deserializeVote(const proto::Vote &pb_vote)
Definition: yac_pb_converters.hpp:90
std::shared_ptr< shared_model::interface::Signature > block_signature
Definition: yac_hash_provider.hpp:70
std::shared_ptr< shared_model::interface::Signature > signature
Definition: vote_message.hpp:24
Definition: proto_common_objects_factory.hpp:30
Definition: block_query.hpp:16
hash256_t hash(const T &pb)
Definition: pb_common.hpp:43
YacHash hash
Definition: vote_message.hpp:23
Logger log(const std::string &tag)
Definition: logger.cpp:48
Definition: signed.hpp:17
Definition: yac_hash_provider.hpp:46
BlockRoundType block_round
Definition: round.hpp:32
Definition: field_validator.hpp:32
RejectRoundType reject_round
Definition: round.hpp:33
Definition: yac_pb_converters.hpp:21
ProposalHash proposal_hash
Definition: yac_hash_provider.hpp:50
static proto::Vote serializeVote(const VoteMessage &vote)
Definition: yac_pb_converters.hpp:68
VoteHashes vote_hashes
Definition: yac_hash_provider.hpp:65
Definition: vote_message.hpp:22
BlockHash block_hash
Definition: yac_hash_provider.hpp:55
Definition: public_key.hpp:16
Definition: round.hpp:31
static proto::Vote serializeVotePayload(const VoteMessage &vote)
Definition: yac_pb_converters.hpp:53
Round vote_round
Definition: yac_hash_provider.hpp:41
std::string toBinaryString(const Blob &b)
Definition: blob.cpp:13