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
24#include <xrpl/basics/chrono.h>
25#include <xrpl/protocol/STTx.h>
26
27namespace ripple {
28
30 : mApp(app)
31 , mCache(
32 "TransactionCache",
33 65536,
34 std::chrono::minutes{30},
35 stopwatch(),
36 mApp.journal("TaggedCache"))
37{
38}
39
40bool
42{
43 auto txn = mCache.fetch(hash);
44
45 if (!txn)
46 return false;
47
48 txn->setStatus(COMMITTED, ledger);
49 return true;
50}
51
54{
55 return mCache.fetch(txnID);
56}
57
62{
63 using TxPair =
65
66 if (auto txn = fetch_from_cache(txnID); txn && !txn->isValidated())
67 return std::pair{std::move(txn), nullptr};
68
69 auto v = Transaction::load(txnID, mApp, ec);
70
71 if (std::holds_alternative<TxSearched>(v))
72 return v;
73
74 auto [txn, txnMeta] = std::get<TxPair>(v);
75
76 if (txn)
77 mCache.canonicalize_replace_client(txnID, txn);
78
79 return std::pair{std::move(txn), std::move(txnMeta)};
80}
81
86 uint256 const& txnID,
88 error_code_i& ec)
89{
90 using TxPair =
92
93 if (auto txn = fetch_from_cache(txnID); txn && !txn->isValidated())
94 return std::pair{std::move(txn), nullptr};
95
96 auto v = Transaction::load(txnID, mApp, range, ec);
97
98 if (std::holds_alternative<TxSearched>(v))
99 return v;
100
101 auto [txn, txnMeta] = std::get<TxPair>(v);
102
103 if (txn)
104 mCache.canonicalize_replace_client(txnID, txn);
105
106 return std::pair{std::move(txn), std::move(txnMeta)};
107}
108
111 boost::intrusive_ptr<SHAMapItem> const& item,
112 SHAMapNodeType type,
113 std::uint32_t uCommitLedger)
114{
116 auto iTx = fetch_from_cache(item->key());
117
118 if (!iTx)
119 {
121 {
122 SerialIter sit(item->slice());
123 txn = std::make_shared<STTx const>(std::ref(sit));
124 }
125 else if (type == SHAMapNodeType::tnTRANSACTION_MD)
126 {
127 auto blob = SerialIter{item->slice()}.getVL();
128 txn = std::make_shared<STTx const>(
129 SerialIter{blob.data(), blob.size()});
130 }
131 }
132 else
133 {
134 if (uCommitLedger)
135 iTx->setStatus(COMMITTED, uCommitLedger);
136
137 txn = iTx->getSTransaction();
138 }
139
140 return txn;
141}
142
143void
145{
146 uint256 const tid = (*pTransaction)->getID();
147 if (tid != beast::zero)
148 {
149 auto txn = *pTransaction;
150 // VFALCO NOTE canonicalize can change the value of txn!
151 mCache.canonicalize_replace_client(tid, txn);
152 *pTransaction = txn;
153 }
154}
155
156void
158{
159 mCache.sweep();
160}
161
164{
165 return mCache;
166}
167
168} // namespace ripple
Map/cache combination.
Definition: TaggedCache.h:58
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:119
ClosedInterval< T > range(T low, T high)
Create a closed range interval.
Definition: RangeSet.h:54
STL namespace.
T ref(T... args)