LCOV - code coverage report
Current view: top level - shared_model/backend/protobuf/impl - proto_block_factory.cpp (source / functions) Hit Total Coverage
Test: cleared_cor.info Lines: 25 27 92.6 %
Date: 2019-03-07 14:46:43 Functions: 6 6 100.0 %

          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;
      11             : using namespace shared_model::proto;
      12             : 
      13             : ProtoBlockFactory::ProtoBlockFactory(
      14             :     std::unique_ptr<shared_model::validation::AbstractValidator<
      15             :         shared_model::interface::Block>> interface_validator,
      16             :     std::unique_ptr<
      17             :         shared_model::validation::AbstractValidator<iroha::protocol::Block>>
      18             :         proto_validator)
      19         506 :     : interface_validator_{std::move(interface_validator)},
      20         506 :       proto_validator_{std::move(proto_validator)} {}
      21             : 
      22             : std::unique_ptr<shared_model::interface::Block>
      23             : ProtoBlockFactory::unsafeCreateBlock(
      24             :     interface::types::HeightType height,
      25             :     const interface::types::HashType &prev_hash,
      26             :     interface::types::TimestampType created_time,
      27             :     const interface::types::TransactionsCollectionType &txs,
      28             :     const interface::types::HashCollectionType &rejected_hashes) {
      29         723 :   iroha::protocol::Block_v1 block;
      30         723 :   auto *block_payload = block.mutable_payload();
      31         723 :   block_payload->set_height(height);
      32         723 :   block_payload->set_prev_block_hash(prev_hash.hex());
      33         723 :   block_payload->set_created_time(created_time);
      34             : 
      35             :   // set accepted transactions
      36         723 :   std::for_each(
      37             :       std::begin(txs), std::end(txs), [block_payload](const auto &tx) {
      38         670 :         auto *transaction = block_payload->add_transactions();
      39         670 :         (*transaction) = static_cast<const Transaction &>(tx).getTransport();
      40         670 :       });
      41             : 
      42             :   // set rejected transactions
      43         723 :   std::for_each(std::begin(rejected_hashes),
      44         723 :                 std::end(rejected_hashes),
      45             :                 [block_payload](const auto &hash) {
      46          60 :                   auto *next_hash =
      47          60 :                       block_payload->add_rejected_transactions_hashes();
      48          60 :                   (*next_hash) = hash.hex();
      49          60 :                 });
      50             : 
      51         723 :   return std::make_unique<shared_model::proto::Block>(std::move(block));
      52         723 : }
      53             : 
      54             : iroha::expected::Result<std::unique_ptr<shared_model::interface::Block>,
      55             :                         std::string>
      56             : ProtoBlockFactory::createBlock(iroha::protocol::Block block) {
      57           6 :   if (auto errors = proto_validator_->validate(block)) {
      58           0 :     return iroha::expected::makeError(errors.reason());
      59             :   }
      60             : 
      61           6 :   std::unique_ptr<shared_model::interface::Block> proto_block =
      62           6 :       std::make_unique<Block>(std::move(block.block_v1()));
      63           6 :   if (auto errors = interface_validator_->validate(*proto_block)) {
      64           0 :     return iroha::expected::makeError(errors.reason());
      65             :   }
      66             : 
      67           6 :   return iroha::expected::makeValue(std::move(proto_block));
      68           6 : }

Generated by: LCOV version 1.13