LCOV - code coverage report
Current view: top level - shared_model/backend/protobuf/impl - block.cpp (source / functions) Hit Total Coverage
Test: cleared_cor.info Lines: 49 51 96.1 %
Date: 2019-03-07 14:46:43 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/signature.hpp"
      10             : #include "backend/protobuf/transaction.hpp"
      11             : #include "backend/protobuf/util.hpp"
      12             : #include "common/byteutils.hpp"
      13             : 
      14             : namespace shared_model {
      15             :   namespace proto {
      16             : 
      17             :     struct Block::Impl {
      18             :       explicit Impl(TransportType &&ref) : proto_(std::move(ref)) {}
      19             :       explicit Impl(const TransportType &ref) : proto_(ref) {}
      20             :       Impl(Impl &&o) noexcept = delete;
      21             :       Impl &operator=(Impl &&o) noexcept = delete;
      22             : 
      23             :       TransportType proto_;
      24        5622 :       iroha::protocol::Block_v1::Payload &payload_{*proto_.mutable_payload()};
      25             : 
      26             :       std::vector<proto::Transaction> transactions_{[this] {
      27        6742 :         return std::vector<proto::Transaction>(
      28        6742 :             payload_.mutable_transactions()->begin(),
      29        6742 :             payload_.mutable_transactions()->end());
      30             :       }()};
      31             : 
      32             :       interface::types::BlobType blob_{[this] { return makeBlob(proto_); }()};
      33             : 
      34             :       interface::types::HashType prev_hash_{[this] {
      35        6742 :         return interface::types::HashType(
      36        6742 :             crypto::Hash::fromHexString(proto_.payload().prev_block_hash()));
      37             :       }()};
      38             : 
      39             :       SignatureSetType<proto::Signature> signatures_{[this] {
      40        6741 :         auto signatures = *proto_.mutable_signatures()
      41        6741 :             | boost::adaptors::transformed(
      42             :                   [](auto &x) { return proto::Signature(x); });
      43        6742 :         return SignatureSetType<proto::Signature>(signatures.begin(),
      44        6742 :                                                   signatures.end());
      45        6742 :       }()};
      46             : 
      47        5622 :       std::vector<interface::types::HashType> rejected_transactions_hashes_{
      48             :           [this] {
      49        6741 :             std::vector<interface::types::HashType> hashes;
      50        7043 :             for (const auto &hash :
      51        6741 :                  *payload_.mutable_rejected_transactions_hashes()) {
      52         301 :               hashes.emplace_back(
      53         301 :                   shared_model::crypto::Hash::fromHexString(hash));
      54             :             }
      55        6741 :             return hashes;
      56        6741 :           }()};
      57             : 
      58        5618 :       interface::types::BlobType payload_blob_{
      59             :           [this] { return makeBlob(payload_); }()};
      60             :     };
      61             : 
      62             :     Block::Block(Block &&o) noexcept = default;
      63             : 
      64             :     Block::Block(const TransportType &ref) {
      65        5623 :       impl_ = std::make_unique<Block::Impl>(ref);
      66        5623 :     }
      67             : 
      68             :     Block::Block(TransportType &&ref) {
      69        1119 :       impl_ = std::make_unique<Block::Impl>(std::move(ref));
      70        1119 :     }
      71             : 
      72             :     interface::types::TransactionsCollectionType Block::transactions() const {
      73        5209 :       return impl_->transactions_;
      74             :     }
      75             : 
      76             :     interface::types::HeightType Block::height() const {
      77        6664 :       return impl_->payload_.height();
      78             :     }
      79             : 
      80             :     const interface::types::HashType &Block::prevHash() const {
      81         274 :       return impl_->prev_hash_;
      82             :     }
      83             : 
      84             :     const interface::types::BlobType &Block::blob() const {
      85           0 :       return impl_->blob_;
      86             :     }
      87             : 
      88             :     interface::types::SignatureRangeType Block::signatures() const {
      89        1050 :       return impl_->signatures_;
      90             :     }
      91             : 
      92             :     bool Block::addSignature(const crypto::Signed &signed_blob,
      93             :                              const crypto::PublicKey &public_key) {
      94             :       // if already has such signature
      95        2472 :       if (std::find_if(impl_->signatures_.begin(),
      96        2472 :                        impl_->signatures_.end(),
      97             :                        [&public_key](const auto &signature) {
      98        1478 :                          return signature.publicKey() == public_key;
      99             :                        })
     100        2472 :           != impl_->signatures_.end()) {
     101        1445 :         return false;
     102             :       }
     103             : 
     104        1027 :       auto sig = impl_->proto_.add_signatures();
     105        1027 :       sig->set_signature(signed_blob.hex());
     106        1027 :       sig->set_public_key(public_key.hex());
     107             : 
     108             :       impl_->signatures_ = [this] {
     109        1027 :         auto signatures = *impl_->proto_.mutable_signatures()
     110        1027 :             | boost::adaptors::transformed(
     111             :                   [](auto &x) { return proto::Signature(x); });
     112        1027 :         return SignatureSetType<proto::Signature>(signatures.begin(),
     113        1027 :                                                   signatures.end());
     114        1027 :       }();
     115        1027 :       return true;
     116        2472 :     }
     117             : 
     118             :     interface::types::TimestampType Block::createdTime() const {
     119         269 :       return impl_->payload_.created_time();
     120             :     }
     121             : 
     122             :     interface::types::TransactionsNumberType Block::txsNumber() const {
     123           1 :       return impl_->payload_.tx_number();
     124             :     }
     125             : 
     126             :     interface::types::HashCollectionType Block::rejected_transactions_hashes()
     127             :         const {
     128        2981 :       return impl_->rejected_transactions_hashes_;
     129             :     }
     130             : 
     131             :     const interface::types::BlobType &Block::payload() const {
     132        3334 :       return impl_->payload_blob_;
     133             :     }
     134             : 
     135             :     const iroha::protocol::Block_v1 &Block::getTransport() const {
     136        2036 :       return impl_->proto_;
     137             :     }
     138             : 
     139             :     Block::ModelType *Block::clone() const {
     140        2576 :       return new Block(impl_->proto_);
     141           0 :     }
     142             : 
     143             :     Block::~Block() = default;
     144             :   }  // namespace proto
     145             : }  // namespace shared_model

Generated by: LCOV version 1.13