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 253 : : storage_(std::move(storage)) {}
17 :
18 : boost::optional<TxCacheStatusType> TxPresenceCacheImpl::check(
19 : const shared_model::crypto::Hash &hash) const {
20 5428 : auto res = memory_cache_.findItem(hash);
21 5428 : if (res) {
22 0 : return *res;
23 : }
24 5351 : return checkInStorage(hash);
25 5360 : }
26 :
27 : boost::optional<TxPresenceCache::BatchStatusCollectionType>
28 : TxPresenceCacheImpl::check(
29 : const shared_model::interface::TransactionBatch &batch) const {
30 4076 : TxPresenceCache::BatchStatusCollectionType batch_statuses;
31 8121 : for (const auto &tx : batch.transactions()) {
32 4068 : if (auto status = check(tx->hash())) {
33 3994 : batch_statuses.emplace_back(*status);
34 4053 : } else {
35 4 : return boost::none;
36 : }
37 : }
38 4002 : return batch_statuses;
39 4005 : }
40 :
41 : boost::optional<TxCacheStatusType> TxPresenceCacheImpl::checkInStorage(
42 : const shared_model::crypto::Hash &hash) const {
43 5432 : auto block_query = storage_->getBlockQuery();
44 5432 : if (not block_query) {
45 87 : return boost::none;
46 : }
47 5306 : return block_query->checkTxPresence(hash) |
48 : [this, &hash](const auto &status) {
49 5207 : 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 4707 : },
54 : [this, &hash](const auto &status) {
55 516 : memory_cache_.addItem(hash, status);
56 516 : });
57 5207 : return status;
58 : };
59 5345 : }
60 : } // namespace ametsuchi
61 : } // namespace iroha
|