rippled
Loading...
Searching...
No Matches
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 <xrpld/app/ledger/TransactionMaster.h>
21#include <xrpld/app/main/Application.h>
22#include <xrpld/app/misc/Transaction.h>
23#include <xrpl/basics/chrono.h>
24#include <xrpl/protocol/STTx.h>
25
26namespace 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
39bool
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
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
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
142void
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
155void
157{
158 mCache.sweep();
159}
160
163{
164 return mCache;
165}
166
167} // namespace ripple
Map/cache combination.
Definition: TaggedCache.h:57
std::shared_ptr< Transaction > fetch_from_cache(uint256 const &)
bool inLedger(uint256 const &hash, std::uint32_t ledger)
TransactionMaster(Application &app)
std::variant< std::pair< std::shared_ptr< Transaction >, std::shared_ptr< TxMeta > >, TxSearched > fetch(uint256 const &, error_code_i &ec)
TaggedCache< uint256, Transaction > mCache
void canonicalize(std::shared_ptr< Transaction > *pTransaction)
TaggedCache< uint256, Transaction > & getCache()
static std::variant< std::pair< std::shared_ptr< Transaction >, std::shared_ptr< TxMeta > >, TxSearched > load(uint256 const &id, Application &app, error_code_i &ec)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: algorithm.h:26
@ COMMITTED
Definition: Transaction.h:51
error_code_i
Definition: ErrorCodes.h:40
boost::icl::closed_interval< T > ClosedInterval
A closed interval over the domain T.
Definition: RangeSet.h:45
Stopwatch & stopwatch()
Returns an instance of a wall clock.
Definition: chrono.h:120
ClosedInterval< T > range(T low, T high)
Create a closed range interval.
Definition: RangeSet.h:54
STL namespace.
T ref(T... args)