LCOV - code coverage report
Current view: top level - shared_model/backend/protobuf/impl - block.cpp (source / functions) Hit Total Coverage
Test: coverage_cleared.info Lines: 47 49 95.9 %
Date: 2018-12-05 17:11:35 Functions: 36 37 97.3 %

          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/block.hpp"
       7             : 
       8             : #include <boost/range/adaptors.hpp>
       9             : #include "backend/protobuf/common_objects/noncopyable_proto.hpp"
      10             : #include "backend/protobuf/common_objects/signature.hpp"
      11             : #include "backend/protobuf/transaction.hpp"
      12             : #include "backend/protobuf/util.hpp"
      13             : #include "block.pb.h"
      14             : #include "interfaces/common_objects/types.hpp"
      15             : 
      16             : namespace shared_model {
      17             :   namespace proto {
      18             : 
      19             :     struct Block::Impl {
      20             :       explicit Impl(TransportType &&ref) : proto_(std::move(ref)) {}
      21             :       explicit Impl(const TransportType &ref) : proto_(ref) {}
      22             :       Impl(Impl &&o) noexcept = delete;
      23             :       Impl &operator=(Impl &&o) noexcept = delete;
      24             : 
      25             :       TransportType proto_;
      26        2533 :       iroha::protocol::Block::Payload &payload_{*proto_.mutable_payload()};
      27             : 
      28             :       std::vector<proto::Transaction> transactions_{[this] {
      29        5002 :         return std::vector<proto::Transaction>(
      30        5002 :             payload_.mutable_transactions()->begin(),
      31        5002 :             payload_.mutable_transactions()->end());
      32             :       }()};
      33             : 
      34             :       interface::types::BlobType blob_{[this] { return makeBlob(proto_); }()};
      35             : 
      36             :       interface::types::HashType prev_hash_{[this] {
      37        5002 :         return interface::types::HashType(proto_.payload().prev_block_hash());
      38             :       }()};
      39             : 
      40             :       SignatureSetType<proto::Signature> signatures_{[this] {
      41        5002 :         auto signatures = proto_.signatures()
      42             :             | boost::adaptors::transformed([](const auto &x) {
      43        3714 :                             return proto::Signature(x);
      44             :                           });
      45        5003 :         return SignatureSetType<proto::Signature>(signatures.begin(),
      46        5002 :                                                   signatures.end());
      47        5003 :       }()};
      48             : 
      49        2533 :       std::vector<interface::types::HashType> rejected_transactions_hashes_{
      50             :           [this] {
      51        5003 :             std::vector<interface::types::HashType> hashes;
      52        5193 :             for (const auto &hash :
      53        5003 :                  *payload_.mutable_rejected_transactions_hashes()) {
      54         190 :               hashes.emplace_back(shared_model::crypto::Hash(hash));
      55             :             }
      56        5003 :             return hashes;
      57        5003 :           }()};
      58             : 
      59        2533 :       interface::types::BlobType payload_blob_{
      60             :           [this] { return makeBlob(payload_); }()};
      61             :     };
      62             : 
      63             :     Block::Block(Block &&o) noexcept = default;
      64             : 
      65             :     Block::Block(const TransportType &ref) {
      66        2533 :       impl_ = std::make_unique<Block::Impl>(ref);
      67        2533 :     }
      68             : 
      69             :     Block::Block(TransportType &&ref) {
      70        2470 :       impl_ = std::make_unique<Block::Impl>(std::move(ref));
      71        2470 :     }
      72             : 
      73             :     interface::types::TransactionsCollectionType Block::transactions() const {
      74        5616 :       return impl_->transactions_;
      75             :     }
      76             : 
      77             :     interface::types::HeightType Block::height() const {
      78        7044 :       return impl_->payload_.height();
      79             :     }
      80             : 
      81             :     const interface::types::HashType &Block::prevHash() const {
      82         273 :       return impl_->prev_hash_;
      83             :     }
      84             : 
      85             :     const interface::types::BlobType &Block::blob() const {
      86           0 :       return impl_->blob_;
      87             :     }
      88             : 
      89             :     interface::types::SignatureRangeType Block::signatures() const {
      90        1021 :       return impl_->signatures_;
      91             :     }
      92             : 
      93             :     bool Block::addSignature(const crypto::Signed &signed_blob,
      94             :                              const crypto::PublicKey &public_key) {
      95             :       // if already has such signature
      96        1714 :       if (std::find_if(impl_->signatures_.begin(),
      97        1714 :                        impl_->signatures_.end(),
      98             :                        [&public_key](const auto &signature) {
      99         737 :                          return signature.publicKey() == public_key;
     100             :                        })
     101        1714 :           != impl_->signatures_.end()) {
     102         709 :         return false;
     103             :       }
     104             : 
     105        1005 :       auto sig = impl_->proto_.add_signatures();
     106        1005 :       sig->set_signature(crypto::toBinaryString(signed_blob));
     107        1005 :       sig->set_public_key(crypto::toBinaryString(public_key));
     108             : 
     109             :       impl_->signatures_ = [this] {
     110        1005 :         auto signatures = impl_->proto_.signatures()
     111             :             | boost::adaptors::transformed([](const auto &x) {
     112        1033 :                             return proto::Signature(x);
     113             :                           });
     114        1005 :         return SignatureSetType<proto::Signature>(signatures.begin(),
     115        1005 :                                                   signatures.end());
     116        1005 :       }();
     117        1005 :       return true;
     118        1714 :     }
     119             : 
     120             :     interface::types::TimestampType Block::createdTime() const {
     121         268 :       return impl_->payload_.created_time();
     122             :     }
     123             : 
     124             :     interface::types::TransactionsNumberType Block::txsNumber() const {
     125           1 :       return impl_->payload_.tx_number();
     126             :     }
     127             : 
     128             :     interface::types::HashCollectionType Block::rejected_transactions_hashes()
     129             :         const {
     130        1290 :       return impl_->rejected_transactions_hashes_;
     131             :     }
     132             : 
     133             :     const interface::types::BlobType &Block::payload() const {
     134        3029 :       return impl_->payload_blob_;
     135             :     }
     136             : 
     137             :     const iroha::protocol::Block &Block::getTransport() const {
     138        2015 :       return impl_->proto_;
     139             :     }
     140             : 
     141             :     Block::ModelType *Block::clone() const {
     142        2529 :       return new Block(impl_->proto_);
     143           0 :     }
     144             : 
     145             :     Block::~Block() = default;
     146             :   }  // namespace proto
     147             : }  // namespace shared_model

Generated by: LCOV version 1.13