xrpld
Toggle main menu visibility
Loading...
Searching...
No Matches
xrpld
app
ledger
detail
TransactionMaster.cpp
1
#include <xrpld/app/ledger/TransactionMaster.h>
2
3
#include <xrpld/app/main/Application.h>
4
#include <xrpld/app/misc/Transaction.h>
5
6
#include <xrpl/basics/RangeSet.h>
7
#include <xrpl/basics/TaggedCache.ipp>
// IWYU pragma: keep
8
#include <xrpl/basics/base_uint.h>
9
#include <xrpl/basics/chrono.h>
10
#include <xrpl/beast/utility/Zero.h>
11
#include <xrpl/protocol/ErrorCodes.h>
12
#include <xrpl/protocol/STTx.h>
13
#include <xrpl/protocol/Serializer.h>
14
#include <xrpl/protocol/TxMeta.h>
15
#include <xrpl/protocol/TxSearched.h>
16
#include <xrpl/shamap/SHAMapItem.h>
17
#include <xrpl/shamap/SHAMapTreeNode.h>
18
19
#include <boost/smart_ptr/intrusive_ptr.hpp>
20
21
#include <
cstdint
>
22
#include <
functional
>
23
#include <
memory
>
24
#include <
optional
>
25
#include <
utility
>
26
#include <
variant
>
27
28
namespace
xrpl
{
29
30
TransactionMaster::TransactionMaster
(
Application
& app)
31
:
app_
(app)
32
,
cache_
(
33
"TransactionCache"
,
34
65536,
35
std
::chrono::minutes{30},
36
stopwatch
(),
37
app_
.getJournal(
"TaggedCache"
))
38
{
39
}
40
41
bool
42
TransactionMaster::inLedger
(
43
UInt256
const
& hash,
44
std::uint32_t
ledger,
45
std::optional<uint32_t>
tseq,
46
std::optional<uint32_t>
netID)
47
{
48
auto
txn =
cache_
.fetch(hash);
49
50
if
(!txn)
51
return
false
;
52
53
txn->setStatus(
TransStatus::COMMITTED
, ledger, tseq, netID);
54
return
true
;
55
}
56
57
std::shared_ptr<Transaction>
58
TransactionMaster::fetchFromCache
(
UInt256
const
& txnID)
59
{
60
return
cache_
.fetch(txnID);
61
}
62
63
std::variant<std::pair<std::shared_ptr<Transaction>
,
std::shared_ptr<TxMeta>
>,
TxSearched
>
64
TransactionMaster::fetch
(
UInt256
const
& txnID,
ErrorCodeI
& ec)
65
{
66
using
TxPair =
std::pair<std::shared_ptr<Transaction>
,
std::shared_ptr<TxMeta>
>;
67
68
if
(
auto
txn =
fetchFromCache
(txnID); txn && !txn->isValidated())
69
return
std::pair
{std::move(txn),
nullptr
};
70
71
auto
v =
Transaction::load
(txnID,
app_
, ec);
72
73
if
(
std::holds_alternative<TxSearched>
(v))
74
return
v;
75
76
auto
[txn, txnMeta] = std::get<TxPair>(v);
77
78
if
(txn)
79
cache_
.canonicalizeReplaceClient(txnID, txn);
80
81
return
std::pair
{std::move(txn), std::move(txnMeta)};
82
}
83
84
std::variant<std::pair<std::shared_ptr<Transaction>
,
std::shared_ptr<TxMeta>
>,
TxSearched
>
85
TransactionMaster::fetch
(
86
UInt256
const
& txnID,
87
ClosedInterval<uint32_t>
const
&
range
,
88
ErrorCodeI
& ec)
89
{
90
using
TxPair =
std::pair<std::shared_ptr<Transaction>
,
std::shared_ptr<TxMeta>
>;
91
92
if
(
auto
txn =
fetchFromCache
(txnID); txn && !txn->isValidated())
93
return
std::pair
{std::move(txn),
nullptr
};
94
95
auto
v =
Transaction::load
(txnID,
app_
,
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
cache_
.canonicalizeReplaceClient(txnID, txn);
104
105
return
std::pair
{std::move(txn), std::move(txnMeta)};
106
}
107
108
std::shared_ptr<STTx const>
109
TransactionMaster::fetch
(
110
boost::intrusive_ptr<SHAMapItem>
const
& item,
111
SHAMapNodeType
type,
112
std::uint32_t
uCommitLedger)
113
{
114
std::shared_ptr<STTx const>
txn;
115
auto
iTx =
fetchFromCache
(item->key());
116
117
if
(!iTx)
118
{
119
if
(type ==
SHAMapNodeType::TnTransactionNm
)
120
{
121
SerialIter
sit(item->slice());
122
txn =
std::make_shared<STTx const>
(
std::ref
(sit));
123
}
124
else
if
(type ==
SHAMapNodeType::TnTransactionMd
)
125
{
126
auto
blob =
SerialIter
{item->slice()}.
getVL
();
127
txn =
std::make_shared<STTx const>
(
SerialIter
{blob.data(), blob.size()});
128
}
129
}
130
else
131
{
132
if
(uCommitLedger != 0u)
133
iTx->setStatus(
TransStatus::COMMITTED
, uCommitLedger);
134
135
txn = iTx->getSTransaction();
136
}
137
138
return
txn;
139
}
140
141
void
142
TransactionMaster::canonicalize
(
std::shared_ptr<Transaction>
* pTransaction)
143
{
144
UInt256
const
tid = (*pTransaction)->getID();
145
if
(tid !=
beast::kZero
)
146
{
147
auto
txn = *pTransaction;
148
// VFALCO NOTE canonicalize can change the value of txn!
149
cache_
.canonicalizeReplaceClient(tid, txn);
150
*pTransaction = txn;
151
}
152
}
153
154
void
155
TransactionMaster::sweep
()
156
{
157
cache_
.sweep();
158
}
159
160
TaggedCache<UInt256, Transaction>
&
161
TransactionMaster::getCache
()
162
{
163
return
cache_
;
164
}
165
166
}
// namespace xrpl
xrpl::Application
Definition
Application.h:94
xrpl::SerialIter
Definition
Serializer.h:478
xrpl::SerialIter::getVL
Blob getVL()
Definition
libxrpl/protocol/Serializer.cpp:539
xrpl::TaggedCache
Map/cache combination.
Definition
TaggedCache.h:67
xrpl::TransactionMaster::sweep
void sweep()
Definition
TransactionMaster.cpp:155
xrpl::TransactionMaster::getCache
TaggedCache< UInt256, Transaction > & getCache()
Definition
TransactionMaster.cpp:161
xrpl::TransactionMaster::inLedger
bool inLedger(UInt256 const &hash, std::uint32_t ledger, std::optional< uint32_t > tseq, std::optional< uint32_t > netID)
Definition
TransactionMaster.cpp:42
xrpl::TransactionMaster::TransactionMaster
TransactionMaster(Application &app)
Definition
TransactionMaster.cpp:30
xrpl::TransactionMaster::canonicalize
void canonicalize(std::shared_ptr< Transaction > *pTransaction)
Definition
TransactionMaster.cpp:142
xrpl::TransactionMaster::app_
Application & app_
Definition
TransactionMaster.h:77
xrpl::TransactionMaster::fetchFromCache
std::shared_ptr< Transaction > fetchFromCache(UInt256 const &)
Definition
TransactionMaster.cpp:58
xrpl::TransactionMaster::cache_
TaggedCache< UInt256, Transaction > cache_
Definition
TransactionMaster.h:78
xrpl::TransactionMaster::fetch
std::variant< std::pair< std::shared_ptr< Transaction >, std::shared_ptr< TxMeta > >, TxSearched > fetch(UInt256 const &, ErrorCodeI &ec)
Definition
TransactionMaster.cpp:64
xrpl::Transaction::load
static std::variant< std::pair< std::shared_ptr< Transaction >, std::shared_ptr< TxMeta > >, TxSearched > load(UInt256 const &id, Application &app, ErrorCodeI &ec)
Definition
Transaction.cpp:119
cstdint
functional
std::holds_alternative
T holds_alternative(T... args)
std::uint32_t
std::make_shared
T make_shared(T... args)
memory
beast::kZero
constexpr Zero kZero
Definition
Zero.h:30
std
STL namespace.
xrpl
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition
algorithm.h:5
xrpl::ErrorCodeI
ErrorCodeI
Definition
ErrorCodes.h:23
xrpl::SHAMapNodeType
SHAMapNodeType
Definition
SHAMapTreeNode.h:30
xrpl::SHAMapNodeType::TnTransactionMd
@ TnTransactionMd
Definition
SHAMapTreeNode.h:33
xrpl::SHAMapNodeType::TnTransactionNm
@ TnTransactionNm
Definition
SHAMapTreeNode.h:32
xrpl::stopwatch
Stopwatch & stopwatch()
Returns an instance of a wall clock.
Definition
chrono.h:101
xrpl::TransStatus::COMMITTED
@ COMMITTED
Definition
Transaction.h:44
xrpl::range
ClosedInterval< T > range(T low, T high)
Create a closed range interval.
Definition
RangeSet.h:37
xrpl::UInt256
BaseUInt< 256 > UInt256
Definition
base_uint.h:580
xrpl::ClosedInterval
boost::icl::closed_interval< T > ClosedInterval
A closed interval over the domain T.
Definition
RangeSet.h:27
xrpl::TxSearched
TxSearched
Definition
TxSearched.h:5
optional
std::pair
std::ref
T ref(T... args)
std::shared_ptr
utility
variant
Generated by
1.17.0