LCOV - code coverage report
Current view: top level - irohad/ametsuchi/impl - tx_presence_cache_impl.cpp (source / functions) Hit Total Coverage
Test: coverage_cleared.info Lines: 23 24 95.8 %
Date: 2018-12-05 17:11:35 Functions: 9 9 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 "ametsuchi/impl/tx_presence_cache_impl.hpp"
       7             : 
       8             : #include "common/bind.hpp"
       9             : #include "common/visitor.hpp"
      10             : #include "interfaces/iroha_internal/transaction_batch.hpp"
      11             : #include "interfaces/transaction.hpp"
      12             : 
      13             : namespace iroha {
      14             :   namespace ametsuchi {
      15             :     TxPresenceCacheImpl::TxPresenceCacheImpl(std::shared_ptr<Storage> storage)
      16         251 :         : storage_(std::move(storage)) {}
      17             : 
      18             :     boost::optional<TxCacheStatusType> TxPresenceCacheImpl::check(
      19             :         const shared_model::crypto::Hash &hash) const {
      20          10 :       auto res = memory_cache_.findItem(hash);
      21          10 :       if (res) {
      22           0 :         return *res;
      23             :       }
      24          10 :       return checkInStorage(hash);
      25          10 :     }
      26             : 
      27             :     boost::optional<TxPresenceCache::BatchStatusCollectionType>
      28             :     TxPresenceCacheImpl::check(
      29             :         const shared_model::interface::TransactionBatch &batch) const {
      30           2 :       TxPresenceCache::BatchStatusCollectionType batch_statuses;
      31           5 :       for (const auto &tx : batch.transactions()) {
      32           4 :         if (auto status = check(tx->hash())) {
      33           3 :           batch_statuses.emplace_back(*status);
      34           3 :         } else {
      35           1 :           return boost::none;
      36             :         }
      37             :       }
      38           1 :       return batch_statuses;
      39           2 :     }
      40             : 
      41             :     boost::optional<TxCacheStatusType> TxPresenceCacheImpl::checkInStorage(
      42             :         const shared_model::crypto::Hash &hash) const {
      43          10 :       auto block_query = storage_->getBlockQuery();
      44          10 :       if (not block_query) {
      45           2 :         return boost::none;
      46             :       }
      47           8 :       return block_query->checkTxPresence(hash) |
      48             :           [this, &hash](const auto &status) {
      49           8 :             visit_in_place(status,
      50             :                            [](const tx_cache_status_responses::Missing &) {
      51             :                              // don't put this hash into cache since "Missing"
      52             :                              // can become "Committed" or "Rejected" later
      53           3 :                            },
      54             :                            [this, &hash](const auto &status) {
      55           3 :                              memory_cache_.addItem(hash, status);
      56           3 :                            });
      57           8 :             return status;
      58             :           };
      59          10 :     }
      60             :   }  // namespace ametsuchi
      61             : }  // namespace iroha

Generated by: LCOV version 1.13