xrpld
Toggle main menu visibility
Loading...
Searching...
No Matches
xrpld
app
ledger
detail
TransactionAcquire.cpp
1
#include <xrpld/app/ledger/detail/TransactionAcquire.h>
2
3
#include <xrpld/app/ledger/ConsensusTransSetSF.h>
4
#include <xrpld/app/ledger/InboundTransactions.h>
5
#include <xrpld/app/ledger/detail/TimeoutCounter.h>
6
#include <xrpld/app/main/Application.h>
7
#include <xrpld/overlay/PeerSet.h>
8
9
#include <xrpl/basics/Log.h>
10
#include <xrpl/basics/base_uint.h>
11
#include <xrpl/beast/utility/instrumentation.h>
12
#include <xrpl/core/Job.h>
13
#include <xrpl/server/NetworkOPs.h>
14
#include <xrpl/shamap/SHAMap.h>
15
#include <xrpl/shamap/SHAMapAddNode.h>
16
#include <xrpl/shamap/SHAMapMissingNode.h>
17
#include <xrpl/shamap/SHAMapTreeNode.h>
18
19
#include <xrpl.pb.h>
20
21
#include <
algorithm
>
22
#include <
chrono
>
23
#include <
cstddef
>
24
#include <
exception
>
25
#include <
memory
>
26
#include <
utility
>
27
#include <
vector
>
28
29
namespace
xrpl
{
30
31
static
constexpr
auto
kNormTimeouts
= 4;
32
static
constexpr
auto
kMaxTimeouts
= 20;
33
34
TransactionAcquire::TransactionAcquire
(
35
Application
& app,
36
UInt256
const
& hash,
37
std::unique_ptr<PeerSet>
peerSet,
38
std::chrono::milliseconds
retryInterval)
39
:
TimeoutCounter
(
40
app,
41
hash,
42
retryInterval,
43
{.jobType =
JtTxnData
, .jobName =
"TxAcq"
, .jobLimit = {}},
44
app.getJournal(
"TransactionAcquire"
))
45
,
peerSet_
(std::move(peerSet))
46
{
47
map_
=
std::make_shared<SHAMap>
(
SHAMapType::TRANSACTION
, hash,
app_
.getNodeFamily());
48
map_
->setUnbacked();
49
}
50
51
void
52
TransactionAcquire::done
()
53
{
54
// mtx_ is held, so this may only post real work rather than do it.
55
56
if
(
failed_
)
57
{
58
JLOG(
journal_
.debug()) <<
"Failed to acquire TX set "
<<
hash_
;
59
}
60
else
if
(!
map_
->setImmutable())
61
{
62
// trigger() verified the map before setting complete_ and mtx_ has been held since, and
63
// nothing walks this map with the lock released, so it is still valid. UNREACHABLE for
64
// that reason, with the flags still withdrawn, since UNREACHABLE need not stop here.
65
// LCOV_EXCL_START
66
// Withdraw complete_ alongside the failure, for trigger() and takeNodes(), which both
67
// check complete_ before failed_.
68
complete_
=
false
;
69
failed_
=
true
;
70
JLOG(
journal_
.debug()) <<
"Failed to acquire TX set "
<<
hash_
;
71
UNREACHABLE(
"xrpl::TransactionAcquire::done : map is invalid"
);
72
// LCOV_EXCL_STOP
73
}
74
else
75
{
76
JLOG(
journal_
.debug()) <<
"Acquired TX set "
<<
hash_
;
77
78
UInt256
const
& hash(
hash_
);
79
std::shared_ptr<SHAMap>
const
& map(
map_
);
80
auto
const
pap = &
app_
;
81
// Note that, when we're in the process of shutting down, addJob()
82
// may reject the request. If that happens then giveSet() will
83
// not be called. That's fine. According to David the giveSet() call
84
// just updates the consensus and related structures when we acquire
85
// a transaction set. No need to update them if we're shutting down.
86
app_
.getJobQueue().addJob(
JtTxnData
,
"ComplAcquire"
, [pap, hash, map] {
87
pap->getInboundTransactions().giveSet(hash, map,
true
);
88
});
89
}
90
}
91
92
void
93
TransactionAcquire::onTimer
(
bool
progress,
ScopedLockType
&)
94
{
95
if
(
timeouts_
>
kMaxTimeouts
)
96
{
97
failed_
=
true
;
98
done
();
99
return
;
100
}
101
102
if
(
timeouts_
>=
kNormTimeouts
)
103
trigger
(
nullptr
);
104
105
addPeers
(1);
106
}
107
108
std::weak_ptr<TimeoutCounter>
109
TransactionAcquire::pmDowncast
()
110
{
111
return
shared_from_this
();
112
}
113
114
void
115
TransactionAcquire::trigger
(
std::shared_ptr<Peer>
const
& peer)
116
{
117
if
(
complete_
)
118
{
119
JLOG(
journal_
.info()) <<
"trigger after complete"
;
120
return
;
121
}
122
if
(
failed_
)
123
{
124
JLOG(
journal_
.info()) <<
"trigger after fail"
;
125
return
;
126
}
127
128
if
(!
haveRoot_
)
129
{
130
JLOG(
journal_
.trace()) <<
"TransactionAcquire::trigger "
<< (peer ?
"havePeer"
:
"noPeer"
)
131
<<
" no root"
;
132
protocol::TMGetLedger tmGL;
133
tmGL.set_ledgerhash(
hash_
.begin(),
hash_
.size());
134
tmGL.set_itype(protocol::liTS_CANDIDATE);
135
tmGL.set_querydepth(3);
// We probably need the whole thing
136
137
if
(
timeouts_
!= 0)
138
tmGL.set_querytype(protocol::qtINDIRECT);
139
140
*tmGL.add_nodeids() =
SHAMapNodeID
().
getRawString
();
141
peerSet_
->sendRequest(tmGL, peer);
142
}
143
else
if
(!
map_
->isValid())
144
{
145
failed_
=
true
;
146
done
();
147
}
148
else
149
{
150
ConsensusTransSetSF
sf(
app_
,
app_
.getTempNodeCache());
151
auto
const
nodes =
map_
->getMissingNodes(256, &sf);
152
153
if
(nodes.empty())
154
{
155
if
(
map_
->isValid())
156
{
157
complete_
=
true
;
158
}
159
else
160
{
161
failed_
=
true
;
162
}
163
164
done
();
165
return
;
166
}
167
168
protocol::TMGetLedger tmGL;
169
tmGL.set_ledgerhash(
hash_
.begin(),
hash_
.size());
170
tmGL.set_itype(protocol::liTS_CANDIDATE);
171
172
if
(
timeouts_
!= 0)
173
tmGL.set_querytype(protocol::qtINDIRECT);
174
175
for
(
auto
const
& node : nodes)
176
{
177
*tmGL.add_nodeids() = node.first.getRawString();
178
}
179
peerSet_
->sendRequest(tmGL, peer);
180
}
181
}
182
183
SHAMapAddNode
184
TransactionAcquire::takeNodes
(
185
std::vector
<
std::pair<SHAMapNodeID, SHAMapTreeNodePtr>
> data,
186
std::shared_ptr<Peer>
const
& peer)
187
{
188
ScopedLockType
const
sl(
mtx_
);
189
190
if
(
complete_
)
191
{
192
JLOG(
journal_
.trace()) <<
"TX set complete"
;
193
return
SHAMapAddNode
();
194
}
195
196
if
(
failed_
)
197
{
198
JLOG(
journal_
.trace()) <<
"TX set failed"
;
199
return
SHAMapAddNode
();
200
}
201
202
try
203
{
204
if
(data.empty())
205
return
SHAMapAddNode::invalid
();
206
207
ConsensusTransSetSF
sf(
app_
,
app_
.getTempNodeCache());
208
209
for
(
auto
& d : data)
210
{
211
if
(d.first.isRoot())
212
{
213
if
(
haveRoot_
)
214
{
215
JLOG(
journal_
.debug()) <<
"Got root TXS node, already have it"
;
216
}
217
else
if
(!
map_
->addRootNode(
SHAMapHash
{hash_}, std::move(d.second),
nullptr
)
218
.isGood())
219
{
220
JLOG(
journal_
.warn()) <<
"TX acquire got bad root node for TX set "
<<
hash_
221
<<
" from peer "
<< peer->id();
222
return
SHAMapAddNode::invalid
();
223
}
224
else
225
{
226
haveRoot_
=
true
;
227
}
228
}
229
else
if
(!
map_
->addKnownNode(d.first, std::move(d.second), &sf).isGood())
230
{
231
JLOG(
journal_
.warn()) <<
"TX acquire got bad non-root node "
<< d.first
232
<<
" for TX set "
<<
hash_
<<
" from peer "
<< peer->id();
233
return
SHAMapAddNode::invalid
();
234
}
235
}
236
237
trigger
(peer);
238
progress_
=
true
;
239
return
SHAMapAddNode::useful
();
240
}
241
catch
(
std::exception
const
& ex)
242
{
243
JLOG(
journal_
.error()) <<
"Peer "
<< peer->id()
244
<<
" sent us junky transaction node data: "
<< ex.
what
();
245
return
SHAMapAddNode::invalid
();
246
}
247
}
248
249
void
250
TransactionAcquire::addPeers
(
std::size_t
limit)
251
{
252
peerSet_
->addPeers(
253
limit,
254
[
this
](
auto
peer) {
return
peer->hasTxSet(
hash_
); },
255
[
this
](
auto
peer) {
trigger
(peer); });
256
}
257
258
void
259
TransactionAcquire::init
(
int
numPeers)
260
{
261
ScopedLockType
sl(
mtx_
);
262
263
addPeers
(numPeers);
264
265
setTimer
(sl);
266
}
267
268
void
269
TransactionAcquire::stillNeed
()
270
{
271
ScopedLockType
const
sl(
mtx_
);
272
273
timeouts_
=
std::min<int>
(
timeouts_
,
kNormTimeouts
);
274
failed_
=
false
;
275
}
276
277
}
// namespace xrpl
algorithm
chrono
xrpl::Application
Definition
Application.h:94
xrpl::ConsensusTransSetSF
Definition
ConsensusTransSetSF.h:24
xrpl::SHAMapAddNode
Definition
SHAMapAddNode.h:9
xrpl::SHAMapAddNode::useful
static SHAMapAddNode useful()
Definition
SHAMapAddNode.h:197
xrpl::SHAMapAddNode::invalid
static SHAMapAddNode invalid()
Definition
SHAMapAddNode.h:203
xrpl::SHAMapHash
Definition
SHAMapHash.h:16
xrpl::SHAMapNodeID
Identifies a node inside a SHAMap.
Definition
SHAMapNodeID.h:20
xrpl::SHAMapNodeID::getRawString
std::string getRawString() const
Definition
libxrpl/shamap/SHAMapNodeID.cpp:85
xrpl::TimeoutCounter::mtx_
std::recursive_mutex mtx_
Definition
TimeoutCounter.h:126
xrpl::TimeoutCounter::ScopedLockType
std::unique_lock< std::recursive_mutex > ScopedLockType
Definition
TimeoutCounter.h:71
xrpl::TimeoutCounter::hash_
UInt256 const hash_
The hash of the object (in practice, always a ledger) we are trying to fetch.
Definition
TimeoutCounter.h:132
xrpl::TimeoutCounter::TimeoutCounter
TimeoutCounter(Application &app, UInt256 const &targetHash, std::chrono::milliseconds timeoutInterval, QueueJobParameter &&jobParameter, beast::Journal journal)
Definition
TimeoutCounter.cpp:21
xrpl::TimeoutCounter::progress_
bool progress_
Whether forward progress has been made.
Definition
TimeoutCounter.h:139
xrpl::TimeoutCounter::app_
Application & app_
Definition
TimeoutCounter.h:124
xrpl::TimeoutCounter::journal_
beast::Journal journal_
Definition
TimeoutCounter.h:125
xrpl::TimeoutCounter::complete_
bool complete_
Definition
TimeoutCounter.h:134
xrpl::TimeoutCounter::failed_
bool failed_
Definition
TimeoutCounter.h:135
xrpl::TimeoutCounter::setTimer
void setTimer(ScopedLockType &)
Schedule a call to queueJob() after timerInterval_.
Definition
TimeoutCounter.cpp:40
xrpl::TimeoutCounter::timeouts_
int timeouts_
Definition
TimeoutCounter.h:133
xrpl::TransactionAcquire::haveRoot_
bool haveRoot_
Definition
TransactionAcquire.h:68
xrpl::TransactionAcquire::done
void done()
Settle the acquired set and hand it on, or report the failure.
Definition
TransactionAcquire.cpp:52
xrpl::TransactionAcquire::init
void init(int startPeers)
Definition
TransactionAcquire.cpp:259
xrpl::TransactionAcquire::takeNodes
SHAMapAddNode takeNodes(std::vector< std::pair< SHAMapNodeID, SHAMapTreeNodePtr > > data, std::shared_ptr< Peer > const &peer)
Definition
TransactionAcquire.cpp:184
xrpl::TransactionAcquire::map_
std::shared_ptr< SHAMap > map_
Definition
TransactionAcquire.h:65
xrpl::TransactionAcquire::addPeers
void addPeers(std::size_t limit)
Definition
TransactionAcquire.cpp:250
xrpl::TransactionAcquire::onTimer
void onTimer(bool progress, ScopedLockType &sl) override
Hook called from invokeOnTimer().
Definition
TransactionAcquire.cpp:93
xrpl::TransactionAcquire::trigger
void trigger(std::shared_ptr< Peer > const &)
Definition
TransactionAcquire.cpp:115
xrpl::TransactionAcquire::TransactionAcquire
TransactionAcquire(Application &app, UInt256 const &hash, std::unique_ptr< PeerSet > peerSet, std::chrono::milliseconds retryInterval=kRetryInterval)
Definition
TransactionAcquire.cpp:34
xrpl::TransactionAcquire::pmDowncast
std::weak_ptr< TimeoutCounter > pmDowncast() override
Return a weak pointer to this.
Definition
TransactionAcquire.cpp:109
xrpl::TransactionAcquire::stillNeed
void stillNeed()
Definition
TransactionAcquire.cpp:269
xrpl::TransactionAcquire::peerSet_
std::unique_ptr< PeerSet > peerSet_
Definition
TransactionAcquire.h:69
cstddef
std::chrono::milliseconds
exception
std::make_shared
T make_shared(T... args)
memory
std::min
T min(T... args)
xrpl
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition
algorithm.h:5
xrpl::UInt256
BaseUInt< 256 > UInt256
Definition
base_uint.h:581
xrpl::JtTxnData
@ JtTxnData
Definition
Job.h:55
xrpl::kNormTimeouts
static constexpr auto kNormTimeouts
Definition
TransactionAcquire.cpp:31
xrpl::kMaxTimeouts
static constexpr auto kMaxTimeouts
Definition
TransactionAcquire.cpp:32
xrpl::SHAMapType::TRANSACTION
@ TRANSACTION
Definition
SHAMapMissingNode.h:14
std::pair
std::enable_shared_from_this< TransactionAcquire >::shared_from_this
T shared_from_this(T... args)
std::shared_ptr
std::size_t
std::unique_ptr
utility
vector
std::weak_ptr
std::exception::what
T what(T... args)
Generated by
1.17.0