Line data Source code
1 : /**
2 : * Copyright Soramitsu Co., Ltd. All Rights Reserved.
3 : * SPDX-License-Identifier: Apache-2.0
4 : */
5 :
6 : #include "backend/protobuf/proto_block_factory.hpp"
7 :
8 : #include "backend/protobuf/block.hpp"
9 :
10 : using namespace shared_model::proto;
11 :
12 : ProtoBlockFactory::ProtoBlockFactory(
13 : std::unique_ptr<shared_model::validation::AbstractValidator<
14 : shared_model::interface::Block>> validator)
15 503 : : validator_(std::move(validator)){};
16 :
17 : std::unique_ptr<shared_model::interface::Block>
18 : ProtoBlockFactory::unsafeCreateBlock(
19 : interface::types::HeightType height,
20 : const interface::types::HashType &prev_hash,
21 : interface::types::TimestampType created_time,
22 : const interface::types::TransactionsCollectionType &txs,
23 : const interface::types::HashCollectionType &rejected_hashes) {
24 711 : iroha::protocol::Block block;
25 711 : auto *block_payload = block.mutable_payload();
26 711 : block_payload->set_height(height);
27 711 : block_payload->set_prev_block_hash(crypto::toBinaryString(prev_hash));
28 711 : block_payload->set_created_time(created_time);
29 :
30 : // set accepted transactions
31 711 : std::for_each(
32 : std::begin(txs), std::end(txs), [block_payload](const auto &tx) {
33 668 : auto *transaction = block_payload->add_transactions();
34 668 : (*transaction) = static_cast<const Transaction &>(tx).getTransport();
35 668 : });
36 :
37 : // set rejected transactions
38 711 : std::for_each(std::begin(rejected_hashes),
39 711 : std::end(rejected_hashes),
40 : [block_payload](const auto &hash) {
41 59 : auto *next_hash =
42 59 : block_payload->add_rejected_transactions_hashes();
43 59 : (*next_hash) = crypto::toBinaryString(hash);
44 59 : });
45 :
46 711 : return std::make_unique<shared_model::proto::Block>(std::move(block));
47 711 : }
48 :
49 : iroha::expected::Result<std::unique_ptr<shared_model::interface::Block>,
50 : std::string>
51 : ProtoBlockFactory::createBlock(iroha::protocol::Block block) {
52 6 : std::unique_ptr<shared_model::interface::Block> proto_block =
53 6 : std::make_unique<Block>(std::move(block));
54 :
55 6 : auto errors = validator_->validate(*proto_block);
56 6 : if (errors) {
57 0 : return iroha::expected::makeError(errors.reason());
58 : }
59 6 : return iroha::expected::makeValue(std::move(proto_block));
60 6 : }
|