Line data Source code
1 : /**
2 : * Copyright Soramitsu Co., Ltd. All Rights Reserved.
3 : * SPDX-License-Identifier: Apache-2.0
4 : */
5 :
6 : #include "model/converters/pb_block_factory.hpp"
7 :
8 : #include <boost/assert.hpp>
9 :
10 : #include "model/converters/pb_common.hpp"
11 : #include "model/converters/pb_transaction_factory.hpp"
12 :
13 : namespace iroha {
14 : namespace model {
15 : namespace converters {
16 :
17 : protocol::Block PbBlockFactory::serialize(
18 : const model::Block &block) const {
19 4 : protocol::Block pb_block{};
20 4 : auto *pb_block_v1 = pb_block.mutable_block_v1();
21 :
22 4 : auto *pl = pb_block_v1->mutable_payload();
23 4 : pl->set_tx_number(block.txs_number);
24 4 : pl->set_height(block.height);
25 4 : pl->set_prev_block_hash(block.prev_hash.to_string());
26 4 : pl->set_created_time(block.created_ts);
27 :
28 8 : for (const auto &sig_obj : block.sigs) {
29 4 : auto sig = pb_block_v1->add_signatures();
30 4 : sig->set_public_key(sig_obj.pubkey.to_string());
31 4 : sig->set_signature(sig_obj.signature.to_string());
32 : }
33 :
34 8 : for (const auto &tx : block.transactions) {
35 4 : auto pb_tx = pl->add_transactions();
36 4 : pb_tx->CopyFrom(PbTransactionFactory::serialize(tx));
37 : }
38 :
39 6 : for (const auto &hash : block.rejected_transactions_hashes) {
40 2 : auto pb_hash = pl->add_rejected_transactions_hashes();
41 2 : *pb_hash = hash.to_string();
42 : }
43 :
44 4 : return pb_block;
45 4 : }
46 :
47 : model::Block PbBlockFactory::deserialize(
48 : protocol::Block const &pb_block) const {
49 1 : model::Block block{};
50 1 : BOOST_ASSERT_MSG(pb_block.has_block_v1(),
51 : "Incompatible version of the block is used");
52 1 : const auto &pl = pb_block.block_v1().payload();
53 :
54 : // in proto we use uint32, but txs_number is uint16
55 1 : auto txn_max = std::numeric_limits<decltype(block.txs_number)>::max();
56 1 : if (pl.tx_number() > txn_max) {
57 0 : throw BadFormatException("too many transactions in block");
58 : }
59 :
60 1 : block.txs_number = static_cast<uint16_t>(pl.tx_number());
61 1 : block.height = pl.height();
62 1 : block.prev_hash = hash256_t::from_string(pl.prev_block_hash());
63 1 : block.created_ts = pl.created_time();
64 :
65 2 : for (const auto &pb_sig : pb_block.block_v1().signatures()) {
66 1 : model::Signature sig;
67 1 : sig.signature = sig_t::from_string(pb_sig.signature());
68 1 : sig.pubkey = pubkey_t::from_string(pb_sig.public_key());
69 1 : block.sigs.push_back(std::move(sig));
70 : }
71 :
72 2 : for (const auto &pb_tx : pl.transactions()) {
73 1 : block.transactions.push_back(
74 1 : *PbTransactionFactory::deserialize(pb_tx));
75 : }
76 :
77 1 : for (const auto &pb_hash : pl.rejected_transactions_hashes()) {
78 0 : BOOST_VERIFY_MSG(
79 : pb_hash.size() == model::Block::HashType::size(),
80 : ("Wrong rejected transaction hash: " + pb_hash).c_str());
81 0 : block.rejected_transactions_hashes.emplace_back();
82 0 : std::copy(pb_hash.begin(),
83 0 : pb_hash.end(),
84 0 : block.rejected_transactions_hashes.back().begin());
85 : }
86 :
87 1 : block.hash = iroha::hash(pb_block.block_v1());
88 :
89 1 : return block;
90 1 : }
91 : } // namespace converters
92 : } // namespace model
93 : } // namespace iroha
|