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, 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 =
106  [&](auto &pubkey, auto &signature, auto &val, const auto &msg) {
107  factory
108  .createSignature(shared_model::crypto::PublicKey(pubkey),
109  shared_model::crypto::Signed(signature))
110  .match(
111  [&](iroha::expected::Value<
112  std::unique_ptr<shared_model::interface::Signature>>
113  &sig) { val = std::move(sig.value); },
114  [&](iroha::expected::Error<std::string> &reason) {
115  log->error(msg, reason.error);
116  });
117  };
118 
119  if (pb_vote.hash().has_block_signature()) {
120  deserialize(pb_vote.hash().block_signature().pubkey(),
121  pb_vote.hash().block_signature().signature(),
122  vote.hash.block_signature,
123  "Cannot build vote hash block signature: {}");
124  }
125 
126  deserialize(pb_vote.signature().pubkey(),
127  pb_vote.signature().signature(),
128  vote.signature,
129  "Cannot build vote signature: {}");
130 
131  return vote;
132  }
133  };
134  } // namespace yac
135  } // namespace consensus
136 } // namespace iroha
137 
138 #endif // IROHA_YAC_PB_CONVERTERS_HPP
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:16
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:33
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