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