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"
12 #include "cryptography/crypto_provider/crypto_defaults.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, 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{
100  std::make_shared<shared_model::validation::ValidatorsConfig>(
101  kMaxBatchSize)};
102 
103  auto vote = deserealizeRoundAndHashes(pb_vote);
104 
105  auto deserialize = [&](auto &pubkey,
106  auto &signature,
107  const auto &msg) {
108  return factory
109  .createSignature(shared_model::crypto::PublicKey(pubkey),
110  shared_model::crypto::Signed(signature))
111  .match(
112  [&](auto &&sig) -> boost::optional<std::unique_ptr<
114  return std::move(sig).value;
115  },
116  [&](const auto &reason)
117  -> boost::optional<std::unique_ptr<
119  log->error(msg, reason.error);
120  return boost::none;
121  });
122  };
123 
124  if (pb_vote.hash().has_block_signature()) {
125  if (auto block_signature =
126  deserialize(pb_vote.hash().block_signature().pubkey(),
127  pb_vote.hash().block_signature().signature(),
128  "Cannot build vote hash block signature: {}")) {
129  vote.hash.block_signature = *std::move(block_signature);
130  } else {
131  return boost::none;
132  }
133  }
134 
135  if (auto vote_signature =
136  deserialize(pb_vote.signature().pubkey(),
137  pb_vote.signature().signature(),
138  "Cannot build vote signature: {}")) {
139  vote.signature = *std::move(vote_signature);
140  } else {
141  return boost::none;
142  }
143 
144  return vote;
145  }
146  };
147  } // namespace yac
148  } // namespace consensus
149 } // namespace iroha
150 
151 #endif // IROHA_YAC_PB_CONVERTERS_HPP
Definition: signature.hpp:25
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:31
static boost::optional< VoteMessage > deserializeVote(const proto::Vote &pb_vote, logger::LoggerPtr log)
Definition: yac_pb_converters.hpp:90
Definition: block_query.hpp:14
hash256_t hash(const T &pb)
Definition: pb_common.hpp:43
YacHash hash
Definition: vote_message.hpp:23
Definition: signed.hpp:17
Definition: yac_hash_provider.hpp:48
BlockRoundType block_round
Definition: round.hpp:32
Definition: field_validator.hpp:35
RejectRoundType reject_round
Definition: round.hpp:33
Definition: yac_pb_converters.hpp:21
ProposalHash proposal_hash
Definition: yac_hash_provider.hpp:52
static proto::Vote serializeVote(const VoteMessage &vote)
Definition: yac_pb_converters.hpp:68
VoteHashes vote_hashes
Definition: yac_hash_provider.hpp:67
Definition: vote_message.hpp:22
BlockHash block_hash
Definition: yac_hash_provider.hpp:57
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:43
std::string toBinaryString(const Blob &b)
Definition: blob.cpp:13