LCOV - code coverage report
Current view: top level - shared_model/interfaces/iroha_internal - transaction_batch_impl.cpp (source / functions) Hit Total Coverage
Test: coverage_cleared.info Lines: 25 38 65.8 %
Date: 2018-12-05 17:11:35 Functions: 12 14 85.7 %

          Line data    Source code
       1             : /**
       2             :  * Copyright Soramitsu Co., Ltd. All Rights Reserved.
       3             :  * SPDX-License-Identifier: Apache-2.0
       4             :  */
       5             : 
       6             : #include "interfaces/iroha_internal/transaction_batch_impl.hpp"
       7             : 
       8             : #include <algorithm>
       9             : #include <numeric>
      10             : 
      11             : #include <boost/range/adaptor/transformed.hpp>
      12             : 
      13             : #include "interfaces/iroha_internal/transaction_batch_helpers.hpp"
      14             : #include "interfaces/transaction.hpp"
      15             : #include "utils/string_builder.hpp"
      16             : 
      17             : namespace shared_model {
      18             :   namespace interface {
      19             : 
      20             :     TransactionBatchImpl::TransactionBatchImpl(
      21             :         types::SharedTxsCollectionType transactions)
      22        3683 :         : transactions_(std::move(transactions)) {}
      23             : 
      24             :     const types::SharedTxsCollectionType &TransactionBatchImpl::transactions()
      25             :         const {
      26       15269 :       return transactions_;
      27             :     }
      28             : 
      29             :     const types::HashType &TransactionBatchImpl::reducedHash() const {
      30         999 :       if (not reduced_hash_) {
      31         158 :         reduced_hash_ = TransactionBatchHelpers::calculateReducedBatchHash(
      32             :             transactions_ | boost::adaptors::transformed([](const auto &tx) {
      33         166 :               return tx->reducedHash();
      34             :             }));
      35         158 :       }
      36         999 :       return reduced_hash_.value();
      37           0 :     }
      38             : 
      39             :     bool TransactionBatchImpl::hasAllSignatures() const {
      40         882 :       return std::all_of(
      41             :           transactions_.begin(), transactions_.end(), [](const auto tx) {
      42         892 :             return boost::size(tx->signatures()) >= tx->quorum();
      43           0 :           });
      44             :     }
      45             : 
      46             :     std::string TransactionBatchImpl::toString() const {
      47         122 :       return detail::PrettyStringBuilder()
      48         122 :           .init("Batch")
      49         122 :           .append("reducedHash", reducedHash().toString())
      50         122 :           .append("hasAllSignatures", hasAllSignatures() ? "true" : "false")
      51         122 :           .append("transactions")
      52             :           .appendAll(transactions(), [](auto &tx) { return tx->toString(); })
      53         122 :           .finalize();
      54           0 :     }
      55             : 
      56             :     bool TransactionBatchImpl::addSignature(
      57             :         size_t number_of_tx,
      58             :         const shared_model::crypto::Signed &signed_blob,
      59             :         const shared_model::crypto::PublicKey &public_key) {
      60          70 :       if (number_of_tx >= transactions_.size()) {
      61           0 :         return false;
      62             :       } else {
      63          70 :         return transactions_.at(number_of_tx)
      64          70 :             ->addSignature(signed_blob, public_key);
      65             :       }
      66          70 :     }
      67             : 
      68             :     bool TransactionBatchImpl::operator==(const TransactionBatch &rhs) const {
      69           5 :       return reducedHash() == rhs.reducedHash()
      70           5 :           and std::equal(transactions().begin(),
      71           5 :                          transactions().end(),
      72           5 :                          rhs.transactions().begin(),
      73           5 :                          rhs.transactions().end(),
      74             :                          [](auto const &left, auto const &right) {
      75           6 :                            return left->equalsByValue(*right);
      76             :                          });
      77             :     }
      78             : 
      79             :     TransactionBatch *TransactionBatchImpl::clone() const {
      80           0 :       const auto &original_txs = this->transactions_;
      81             :       types::SharedTxsCollectionType copy_txs =
      82           0 :           std::accumulate(std::begin(original_txs),
      83           0 :                           std::end(original_txs),
      84           0 :                           types::SharedTxsCollectionType{},
      85             :                           [](types::SharedTxsCollectionType acc,
      86             :                              std::shared_ptr<Transaction> tx) {
      87           0 :                             acc.push_back(::clone(*tx));
      88           0 :                             return acc;
      89           0 :                           });
      90           0 :       return new TransactionBatchImpl(std::move(copy_txs));
      91           0 :     }
      92             : 
      93             :   }  // namespace interface
      94             : }  // namespace shared_model

Generated by: LCOV version 1.13