rippled
TransactionMaster.cpp
1 //------------------------------------------------------------------------------
2 /*
3  This file is part of rippled: https://github.com/ripple/rippled
4  Copyright (c) 2012, 2013 Ripple Labs Inc.
5 
6  Permission to use, copy, modify, and/or distribute this software for any
7  purpose with or without fee is hereby granted, provided that the above
8  copyright notice and this permission notice appear in all copies.
9 
10  THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18 //==============================================================================
19 
20 #include <ripple/app/ledger/TransactionMaster.h>
21 #include <ripple/app/main/Application.h>
22 #include <ripple/app/misc/Transaction.h>
23 #include <ripple/basics/chrono.h>
24 #include <ripple/protocol/STTx.h>
25 
26 namespace ripple {
27 
29  : mApp(app)
30  , mCache(
31  "TransactionCache",
32  65536,
33  std::chrono::minutes{30},
34  stopwatch(),
35  mApp.journal("TaggedCache"))
36 {
37 }
38 
39 bool
41 {
42  auto txn = mCache.fetch(hash);
43 
44  if (!txn)
45  return false;
46 
47  txn->setStatus(COMMITTED, ledger);
48  return true;
49 }
50 
53 {
54  return mCache.fetch(txnID);
55 }
56 
59  TxSearched>
61 {
62  using TxPair =
64 
65  if (auto txn = fetch_from_cache(txnID); txn && !txn->isValidated())
66  return std::pair{std::move(txn), nullptr};
67 
68  auto v = Transaction::load(txnID, mApp, ec);
69 
70  if (std::holds_alternative<TxSearched>(v))
71  return v;
72 
73  auto [txn, txnMeta] = std::get<TxPair>(v);
74 
75  if (txn)
76  mCache.canonicalize_replace_client(txnID, txn);
77 
78  return std::pair{std::move(txn), std::move(txnMeta)};
79 }
80 
83  TxSearched>
85  uint256 const& txnID,
87  error_code_i& ec)
88 {
89  using TxPair =
91 
92  if (auto txn = fetch_from_cache(txnID); txn && !txn->isValidated())
93  return std::pair{std::move(txn), nullptr};
94 
95  auto v = Transaction::load(txnID, mApp, range, ec);
96 
97  if (std::holds_alternative<TxSearched>(v))
98  return v;
99 
100  auto [txn, txnMeta] = std::get<TxPair>(v);
101 
102  if (txn)
103  mCache.canonicalize_replace_client(txnID, txn);
104 
105  return std::pair{std::move(txn), std::move(txnMeta)};
106 }
107 
110  boost::intrusive_ptr<SHAMapItem> const& item,
111  SHAMapNodeType type,
112  std::uint32_t uCommitLedger)
113 {
115  auto iTx = fetch_from_cache(item->key());
116 
117  if (!iTx)
118  {
120  {
121  SerialIter sit(item->slice());
122  txn = std::make_shared<STTx const>(std::ref(sit));
123  }
124  else if (type == SHAMapNodeType::tnTRANSACTION_MD)
125  {
126  auto blob = SerialIter{item->slice()}.getVL();
127  txn = std::make_shared<STTx const>(
128  SerialIter{blob.data(), blob.size()});
129  }
130  }
131  else
132  {
133  if (uCommitLedger)
134  iTx->setStatus(COMMITTED, uCommitLedger);
135 
136  txn = iTx->getSTransaction();
137  }
138 
139  return txn;
140 }
141 
142 void
144 {
145  uint256 const tid = (*pTransaction)->getID();
146  if (tid != beast::zero)
147  {
148  auto txn = *pTransaction;
149  // VFALCO NOTE canonicalize can change the value of txn!
150  mCache.canonicalize_replace_client(tid, txn);
151  *pTransaction = txn;
152  }
153 }
154 
155 void
157 {
158  mCache.sweep();
159 }
160 
163 {
164  return mCache;
165 }
166 
167 } // namespace ripple
ripple::COMMITTED
@ COMMITTED
Definition: Transaction.h:50
ripple::Application
Definition: Application.h:115
std::shared_ptr< Transaction >
ripple::TaggedCache
Map/cache combination.
Definition: Application.h:64
ripple::TransactionMaster::sweep
void sweep(void)
Definition: TransactionMaster.cpp:156
std::pair
ripple::TxSearched
TxSearched
Definition: Transaction.h:57
ripple::SHAMapNodeType
SHAMapNodeType
Definition: SHAMapTreeNode.h:46
ripple::stopwatch
Stopwatch & stopwatch()
Returns an instance of a wall clock.
Definition: chrono.h:88
ripple::error_code_i
error_code_i
Definition: ErrorCodes.h:40
ripple::SHAMapNodeType::tnTRANSACTION_NM
@ tnTRANSACTION_NM
ripple::base_uint< 256 >
ripple::TransactionMaster::mCache
TaggedCache< uint256, Transaction > mCache
Definition: TransactionMaster.h:90
ripple::TransactionMaster::fetch_from_cache
std::shared_ptr< Transaction > fetch_from_cache(uint256 const &)
Definition: TransactionMaster.cpp:52
ripple::TransactionMaster::TransactionMaster
TransactionMaster(Application &app)
Definition: TransactionMaster.cpp:28
ripple::SHAMapNodeType::tnTRANSACTION_MD
@ tnTRANSACTION_MD
ripple::SerialIter
Definition: Serializer.h:310
ripple::TransactionMaster::getCache
TaggedCache< uint256, Transaction > & getCache()
Definition: TransactionMaster.cpp:162
ripple::SerialIter::getVL
Blob getVL()
Definition: Serializer.cpp:508
std::uint32_t
ripple::range
ClosedInterval< T > range(T low, T high)
Create a closed range interval.
Definition: RangeSet.h:53
ripple::TransactionMaster::inLedger
bool inLedger(uint256 const &hash, std::uint32_t ledger)
Definition: TransactionMaster.cpp:40
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::Transaction::load
static std::variant< std::pair< std::shared_ptr< Transaction >, std::shared_ptr< TxMeta > >, TxSearched > load(uint256 const &id, Application &app, error_code_i &ec)
Definition: Transaction.cpp:114
ripple::TransactionMaster::canonicalize
void canonicalize(std::shared_ptr< Transaction > *pTransaction)
Definition: TransactionMaster.cpp:143
std
STL namespace.
ripple::TransactionMaster::fetch
std::variant< std::pair< std::shared_ptr< Transaction >, std::shared_ptr< TxMeta > >, TxSearched > fetch(uint256 const &, error_code_i &ec)
Definition: TransactionMaster.cpp:60
ripple::TransactionMaster::mApp
Application & mApp
Definition: TransactionMaster.h:89
ripple::ClosedInterval
boost::icl::closed_interval< T > ClosedInterval
A closed interval over the domain T.
Definition: RangeSet.h:44
std::ref
T ref(T... args)
std::variant