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/misc/Transaction.h>
22 #include <ripple/app/main/Application.h>
23 #include <ripple/protocol/STTx.h>
24 #include <ripple/basics/chrono.h>
25 
26 namespace ripple {
27 
29  : mApp (app)
30  , mCache ("TransactionCache", 65536, std::chrono::minutes {30}, stopwatch(),
31  mApp.journal("TaggedCache"))
32 {
33 }
34 
36 {
37  auto txn = mCache.fetch (hash);
38 
39  if (!txn)
40  return false;
41 
42  txn->setStatus (COMMITTED, ledger);
43  return true;
44 }
45 
48 {
49  return mCache.fetch (txnID);
50 }
51 
54 {
55  auto txn = fetch_from_cache (txnID);
56 
57  if (txn)
58  return txn;
59 
60  txn = Transaction::load (txnID, mApp, ec);
61 
62  if (!txn)
63  return txn;
64 
65  mCache.canonicalize_replace_client(txnID, txn);
66 
67  return txn;
68 }
69 
70 boost::variant<Transaction::pointer, bool>
72  error_code_i& ec)
73 {
74  using pointer = Transaction::pointer;
75 
76  auto txn = mCache.fetch (txnID);
77 
78  if (txn)
79  return txn;
80 
81  boost::variant<Transaction::pointer, bool> v = Transaction::load (
82  txnID, mApp, range, ec);
83 
84  if (v.which () == 0 && boost::get<pointer> (v))
85  mCache.canonicalize_replace_client(txnID, boost::get<pointer> (v));
86 
87  return v;
88 }
89 
92  SHAMapTreeNode::TNType type, std::uint32_t uCommitLedger)
93 {
95  auto iTx = fetch_from_cache (item->key());
96 
97  if (!iTx)
98  {
99 
101  {
102  SerialIter sit (item->slice());
103  txn = std::make_shared<STTx const> (std::ref (sit));
104  }
105  else if (type == SHAMapTreeNode::tnTRANSACTION_MD)
106  {
107  auto blob = SerialIter{item->data(), item->size()}.getVL();
108  txn = std::make_shared<STTx const>(SerialIter{blob.data(), blob.size()});
109  }
110  }
111  else
112  {
113  if (uCommitLedger)
114  iTx->setStatus (COMMITTED, uCommitLedger);
115 
116  txn = iTx->getSTransaction ();
117  }
118 
119  return txn;
120 }
121 
122 void
124 {
125  uint256 const tid = (*pTransaction)->getID();
126  if (tid != beast::zero)
127  {
128  auto txn = *pTransaction;
129  // VFALCO NOTE canonicalize can change the value of txn!
130  mCache.canonicalize_replace_client(tid, txn);
131  *pTransaction = txn;
132  }
133 }
134 
136 {
137  mCache.sweep ();
138 }
139 
141 {
142  return mCache;
143 }
144 
145 } // ripple
ripple::COMMITTED
@ COMMITTED
Definition: Transaction.h:49
ripple::Application
Definition: Application.h:85
std::shared_ptr< Transaction >
ripple::TaggedCache
Map/cache combination.
Definition: TaggedCache.h:55
ripple::TransactionMaster::sweep
void sweep(void)
Definition: TransactionMaster.cpp:135
ripple::SHAMapAbstractNode::tnTRANSACTION_NM
@ tnTRANSACTION_NM
Definition: SHAMapTreeNode.h:99
ripple::Transaction::load
static pointer load(uint256 const &id, Application &app, error_code_i &ec)
Definition: Transaction.cpp:103
ripple::TransactionMaster::fetch
std::shared_ptr< Transaction > fetch(uint256 const &, error_code_i &ec)
Definition: TransactionMaster.cpp:53
ripple::stopwatch
Stopwatch & stopwatch()
Returns an instance of a wall clock.
Definition: chrono.h:87
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:77
ripple::TransactionMaster::fetch_from_cache
std::shared_ptr< Transaction > fetch_from_cache(uint256 const &)
Definition: TransactionMaster.cpp:47
ripple::TransactionMaster::TransactionMaster
TransactionMaster(Application &app)
Definition: TransactionMaster.cpp:28
ripple::SerialIter
Definition: Serializer.h:311
ripple::TransactionMaster::getCache
TaggedCache< uint256, Transaction > & getCache()
Definition: TransactionMaster.cpp:140
std::uint32_t
ripple::range
ClosedInterval< T > range(T low, T high)
Create a closed range interval.
Definition: RangeSet.h:54
ripple::SHAMapAbstractNode::TNType
TNType
Definition: SHAMapTreeNode.h:95
ripple::TransactionMaster::inLedger
bool inLedger(uint256 const &hash, std::uint32_t ledger)
Definition: TransactionMaster.cpp:35
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:123
std
STL namespace.
ripple::Transaction::pointer
std::shared_ptr< Transaction > pointer
Definition: Transaction.h:65
ripple::SHAMapAbstractNode::tnTRANSACTION_MD
@ tnTRANSACTION_MD
Definition: SHAMapTreeNode.h:100
ripple::TransactionMaster::mApp
Application & mApp
Definition: TransactionMaster.h:76
ripple::ClosedInterval
boost::icl::closed_interval< T > ClosedInterval
A closed interval over the domain T.
Definition: RangeSet.h:45
std::ref
T ref(T... args)