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 {
60  auto txn = fetch_from_cache(txnID);
61 
62  if (txn)
63  return txn;
64 
65  txn = Transaction::load(txnID, mApp, ec);
66 
67  if (!txn)
68  return txn;
69 
70  mCache.canonicalize_replace_client(txnID, txn);
71 
72  return txn;
73 }
74 
75 boost::variant<Transaction::pointer, bool>
77  uint256 const& txnID,
79  error_code_i& ec)
80 {
81  using pointer = Transaction::pointer;
82 
83  auto txn = mCache.fetch(txnID);
84 
85  if (txn)
86  return txn;
87 
88  boost::variant<Transaction::pointer, bool> v =
89  Transaction::load(txnID, mApp, range, ec);
90 
91  if (v.which() == 0 && boost::get<pointer>(v))
92  mCache.canonicalize_replace_client(txnID, boost::get<pointer>(v));
93 
94  return v;
95 }
96 
99  std::shared_ptr<SHAMapItem> const& item,
101  std::uint32_t uCommitLedger)
102 {
104  auto iTx = fetch_from_cache(item->key());
105 
106  if (!iTx)
107  {
109  {
110  SerialIter sit(item->slice());
111  txn = std::make_shared<STTx const>(std::ref(sit));
112  }
113  else if (type == SHAMapTreeNode::tnTRANSACTION_MD)
114  {
115  auto blob = SerialIter{item->data(), item->size()}.getVL();
116  txn = std::make_shared<STTx const>(
117  SerialIter{blob.data(), blob.size()});
118  }
119  }
120  else
121  {
122  if (uCommitLedger)
123  iTx->setStatus(COMMITTED, uCommitLedger);
124 
125  txn = iTx->getSTransaction();
126  }
127 
128  return txn;
129 }
130 
131 void
133 {
134  uint256 const tid = (*pTransaction)->getID();
135  if (tid != beast::zero)
136  {
137  auto txn = *pTransaction;
138  // VFALCO NOTE canonicalize can change the value of txn!
139  mCache.canonicalize_replace_client(tid, txn);
140  *pTransaction = txn;
141  }
142 }
143 
144 void
146 {
147  mCache.sweep();
148 }
149 
152 {
153  return mCache;
154 }
155 
156 } // namespace ripple
ripple::COMMITTED
@ COMMITTED
Definition: Transaction.h:48
ripple::Application
Definition: Application.h:97
std::shared_ptr< Transaction >
ripple::TaggedCache
Map/cache combination.
Definition: TaggedCache.h:52
ripple::TransactionMaster::sweep
void sweep(void)
Definition: TransactionMaster.cpp:145
ripple::SHAMapAbstractNode::tnTRANSACTION_NM
@ tnTRANSACTION_NM
Definition: SHAMapTreeNode.h:128
ripple::Transaction::load
static pointer load(uint256 const &id, Application &app, error_code_i &ec)
Definition: Transaction.cpp:109
ripple::TransactionMaster::fetch
std::shared_ptr< Transaction > fetch(uint256 const &, error_code_i &ec)
Definition: TransactionMaster.cpp:58
ripple::stopwatch
Stopwatch & stopwatch()
Returns an instance of a wall clock.
Definition: chrono.h:86
ripple::error_code_i
error_code_i
Definition: ErrorCodes.h:40
ripple::base_uint< 256 >
ripple::TransactionMaster::mCache
TaggedCache< uint256, Transaction > mCache
Definition: TransactionMaster.h:86
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::SerialIter
Definition: Serializer.h:308
ripple::TransactionMaster::getCache
TaggedCache< uint256, Transaction > & getCache()
Definition: TransactionMaster.cpp:151
std::uint32_t
ripple::range
ClosedInterval< T > range(T low, T high)
Create a closed range interval.
Definition: RangeSet.h:53
ripple::SHAMapAbstractNode::TNType
TNType
Definition: SHAMapTreeNode.h:125
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::TransactionMaster::canonicalize
void canonicalize(std::shared_ptr< Transaction > *pTransaction)
Definition: TransactionMaster.cpp:132
std
STL namespace.
ripple::Transaction::pointer
std::shared_ptr< Transaction > pointer
Definition: Transaction.h:67
ripple::SHAMapAbstractNode::tnTRANSACTION_MD
@ tnTRANSACTION_MD
Definition: SHAMapTreeNode.h:129
ripple::TransactionMaster::mApp
Application & mApp
Definition: TransactionMaster.h:85
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)