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"
13 #include "logger/logger.hpp"
15 #include "yac.pb.h"
16 
17 namespace iroha {
18  namespace consensus {
19  namespace yac {
20  class PbConverters {
21  private:
22  static inline proto::Vote serializeRoundAndHashes(
23  const VoteMessage &vote) {
24  proto::Vote pb_vote;
25 
26  auto hash = pb_vote.mutable_hash();
27  auto hash_round = hash->mutable_vote_round();
28  hash_round->set_block_round(vote.hash.vote_round.block_round);
29  hash_round->set_reject_round(vote.hash.vote_round.reject_round);
30  auto hash_vote_hashes = hash->mutable_vote_hashes();
31  hash_vote_hashes->set_proposal(vote.hash.vote_hashes.proposal_hash);
32  hash_vote_hashes->set_block(vote.hash.vote_hashes.block_hash);
33 
34  return pb_vote;
35  }
36 
37  static inline VoteMessage deserealizeRoundAndHashes(
38  const proto::Vote &pb_vote) {
39  VoteMessage vote;
40 
41  vote.hash.vote_round =
42  Round{pb_vote.hash().vote_round().block_round(),
43  pb_vote.hash().vote_round().reject_round()};
44  vote.hash.vote_hashes =
45  YacHash::VoteHashes{pb_vote.hash().vote_hashes().proposal(),
46  pb_vote.hash().vote_hashes().block()};
47 
48  return vote;
49  }
50 
51  public:
52  static proto::Vote serializeVotePayload(const VoteMessage &vote) {
53  auto pb_vote = serializeRoundAndHashes(vote);
54 
55  if (vote.hash.block_signature) {
56  auto block_signature =
57  pb_vote.mutable_hash()->mutable_block_signature();
58  auto signature = hexstringToBytestringResult(
59  vote.hash.block_signature->signedData());
60  auto public_key = hexstringToBytestringResult(
61  vote.hash.block_signature->publicKey());
62  block_signature->set_signature(std::move(signature).assumeValue());
63  block_signature->set_pubkey(std::move(public_key).assumeValue());
64  }
65 
66  return pb_vote;
67  }
68 
69  static proto::Vote serializeVote(const VoteMessage &vote) {
70  auto pb_vote = serializeRoundAndHashes(vote);
71 
72  if (vote.hash.block_signature) {
73  auto block_signature =
74  pb_vote.mutable_hash()->mutable_block_signature();
75  auto signature = hexstringToBytestringResult(
76  vote.hash.block_signature->signedData());
77  auto public_key = hexstringToBytestringResult(
78  vote.hash.block_signature->publicKey());
79  block_signature->set_signature(std::move(signature).assumeValue());
80  block_signature->set_pubkey(std::move(public_key).assumeValue());
81  }
82 
83  auto vote_signature = pb_vote.mutable_signature();
84  auto signature =
85  hexstringToBytestringResult(vote.signature->signedData());
86  auto public_key =
87  hexstringToBytestringResult(vote.signature->publicKey());
88  vote_signature->set_signature(std::move(signature).assumeValue());
89  vote_signature->set_pubkey(std::move(public_key).assumeValue());
90 
91  return pb_vote;
92  }
93 
94  static boost::optional<VoteMessage> deserializeVote(
95  const proto::Vote &pb_vote, logger::LoggerPtr log) {
96  // TODO IR-428 igor-egorov refactor PbConverters - do the class
97  // instantiable
98  static const uint64_t kMaxBatchSize{0};
99  // This is a workaround for the following ProtoCommonObjectsFactory.
100  // We able to do this, because we don't have batches in consensus.
103  factory{
104  std::make_shared<shared_model::validation::ValidatorsConfig>(
105  kMaxBatchSize)};
106 
107  auto vote = deserealizeRoundAndHashes(pb_vote);
108 
109  auto deserialize = [&](auto &pubkey,
110  auto &signature,
111  const auto &msg) {
112  auto pubkey_hex = bytestringToHexstring(pubkey);
113  auto signature_hex = bytestringToHexstring(signature);
114  using shared_model::interface::types::PublicKeyHexStringView;
115  using shared_model::interface::types::SignedHexStringView;
116  return factory
117  .createSignature(PublicKeyHexStringView{pubkey_hex},
118  SignedHexStringView{signature_hex})
119  .match(
120  [&](auto &&sig) -> boost::optional<std::unique_ptr<
122  return std::move(sig).value;
123  },
124  [&](const auto &reason)
125  -> boost::optional<std::unique_ptr<
127  log->error(msg, reason.error);
128  return boost::none;
129  });
130  };
131 
132  if (pb_vote.hash().has_block_signature()) {
133  if (auto block_signature =
134  deserialize(pb_vote.hash().block_signature().pubkey(),
135  pb_vote.hash().block_signature().signature(),
136  "Cannot build vote hash block signature: {}")) {
137  vote.hash.block_signature = *std::move(block_signature);
138  } else {
139  return boost::none;
140  }
141  }
142 
143  if (auto vote_signature =
144  deserialize(pb_vote.signature().pubkey(),
145  pb_vote.signature().signature(),
146  "Cannot build vote signature: {}")) {
147  vote.signature = *std::move(vote_signature);
148  } else {
149  return boost::none;
150  }
151 
152  return vote;
153  }
154  };
155  } // namespace yac
156  } // namespace consensus
157 } // namespace iroha
158 
159 #endif // IROHA_YAC_PB_CONVERTERS_HPP
Definition: signature.hpp:18
std::string bytestringToHexstring(std::string_view str)
Definition: hexutils.hpp:51
std::shared_ptr< shared_model::interface::Signature > block_signature
Definition: yac_hash_provider.hpp:72
std::shared_ptr< shared_model::interface::Signature > signature
Definition: vote_message.hpp:24
std::shared_ptr< Logger > LoggerPtr
Definition: logger_fwd.hpp:22
Definition: proto_common_objects_factory.hpp:32
static boost::optional< VoteMessage > deserializeVote(const proto::Vote &pb_vote, logger::LoggerPtr log)
Definition: yac_pb_converters.hpp:94
Definition: block_query.hpp:15
hash256_t hash(const T &pb)
Definition: pb_common.hpp:43
YacHash hash
Definition: vote_message.hpp:23
Definition: yac_hash_provider.hpp:48
BlockRoundType block_round
Definition: round.hpp:32
Definition: field_validator.hpp:41
RejectRoundType reject_round
Definition: round.hpp:33
Definition: yac_pb_converters.hpp:20
ProposalHash proposal_hash
Definition: yac_hash_provider.hpp:52
static proto::Vote serializeVote(const VoteMessage &vote)
Definition: yac_pb_converters.hpp:69
VoteHashes vote_hashes
Definition: yac_hash_provider.hpp:67
Definition: vote_message.hpp:22
BlockHash block_hash
Definition: yac_hash_provider.hpp:57
Definition: round.hpp:31
static proto::Vote serializeVotePayload(const VoteMessage &vote)
Definition: yac_pb_converters.hpp:52
iroha::expected::Result< std::string, const char * > hexstringToBytestringResult(std::string_view str)
Definition: hexutils.hpp:65
Round vote_round
Definition: yac_hash_provider.hpp:43