xrpld
Loading...
Searching...
No Matches
AcquireTestHelpers.h
1#pragma once
2
3#include <test/jtx/PeerStub.h>
4
5#include <xrpld/app/ledger/ConsensusTransSetSF.h>
6#include <xrpld/overlay/Peer.h>
7#include <xrpld/overlay/PeerSet.h>
8
9#include <xrpl/basics/SHAMapHash.h>
10#include <xrpl/basics/base_uint.h>
11#include <xrpl/protocol/Serializer.h>
12#include <xrpl/resource/Charge.h>
13#include <xrpl/shamap/SHAMapAddNode.h>
14#include <xrpl/shamap/SHAMapNodeID.h>
15#include <xrpl/shamap/SHAMapTreeNode.h>
16
17#include <tests/libxrpl/shamap/DeepChain.h>
18
19#include <xrpl.pb.h>
20
21#include <atomic>
22#include <chrono>
23#include <cstddef>
24#include <cstdint>
25#include <functional>
26#include <memory>
27#include <mutex>
28#include <optional>
29#include <set>
30#include <string>
31#include <thread>
32#include <utility>
33#include <vector>
34
35namespace xrpl::test {
36
37// The chain builder needs only libxrpl, so it is shared with the gtest suites; see the header for
38// what keeps the protobuf reply builder below in this tree.
39using tests::DeepChain;
40
41// A smallest-possible leaf plus its 4-byte HashPrefix lands one byte short of the floor
42// ConsensusTransSetSF::gotNode() parses at, so a chain's leaf stays below the parse threshold.
43static_assert(
45 "a smallest-possible leaf must stay below the resubmission floor");
46
58{
59public:
66 {
67 }
68
69 void
70 charge(resource::Charge const& fee, std::string const&) override
71 {
72 charges_.push_back(fee);
73 }
74
78 [[nodiscard]] std::vector<resource::Charge> const&
79 charges() const
80 {
81 return charges_;
82 }
83
84 // PeerStub returns false for both, and an acquisition asks the peers reporting true.
85
86 [[nodiscard]] bool
87 hasTxSet(UInt256 const&) const override
88 {
89 return hasTxSet_;
90 }
91
92 [[nodiscard]] bool
93 hasLedger(UInt256 const&, std::uint32_t) const override
94 {
95 return true;
96 }
97
98private:
106 [[nodiscard]] static ID
108 {
109 static std::atomic<ID> next{1};
110 return next++;
111 }
112
115};
116
129{
130public:
136 : candidates_(std::move(candidates))
137 {
138 }
139
149 void
151 std::size_t limit,
152 std::function<bool(std::shared_ptr<Peer> const&)> hasItem,
153 std::function<void(std::shared_ptr<Peer> const&)> onPeerAdded) override
154 {
156 {
157 std::scoped_lock const lock(mutex_);
158
159 if (!firstLimit_)
160 firstLimit_ = limit;
161
162 for (auto const& candidate : candidates_)
163 {
164 if (selected.size() >= limit)
165 break;
166 // Dedup by tracked id, like the real peer set: onPeerAdded runs once per candidate.
167 if (hasItem(candidate) && addedPeers_.insert(candidate->id()).second)
168 selected.push_back(candidate);
169 }
170 }
171
172 // Outside the lock: onPeerAdded() reenters this object through sendRequest().
173 for (auto const& peer : selected)
174 onPeerAdded(peer);
175 }
176
183 void
185 ::google::protobuf::Message const&,
186 protocol::MessageType,
187 std::shared_ptr<Peer> const& peer) override
188 {
189 std::scoped_lock const lock(mutex_);
190 ++requests_;
191 if (!peer)
192 ++broadcasts_;
193 }
194
207 [[nodiscard]] std::set<Peer::ID> const&
208 getPeerIds() const override
209 {
210 return addedPeers_;
211 }
212
217 [[nodiscard]] int
218 requests() const
219 {
220 std::scoped_lock const lock(mutex_);
221 return requests_;
222 }
223
229 [[nodiscard]] int
231 {
232 std::scoped_lock const lock(mutex_);
233 return broadcasts_;
234 }
235
242 [[nodiscard]] std::optional<std::size_t>
244 {
245 std::scoped_lock const lock(mutex_);
246 return firstLimit_;
247 }
248
254 [[nodiscard]] std::set<Peer::ID>
256 {
257 std::scoped_lock const lock(mutex_);
258 return addedPeers_;
259 }
260
261private:
263
265 int requests_{0};
269};
270
288 DeepChain const& chain,
290 protocol::TMLedgerInfoType type = protocol::liTS_CANDIDATE,
291 std::optional<UInt256> const& ledgerHash = std::nullopt,
292 std::uint32_t ledgerSeq = 0)
293{
295 auto const hash = ledgerHash.value_or(chain.rootHash.asUInt256());
296 packet->set_ledgerhash(hash.data(), UInt256::size());
297 packet->set_ledgerseq(ledgerSeq);
298 packet->set_type(type);
299
300 for (auto const& [nodeID, node] : data)
301 {
302 Serializer s;
303 node->serializeForWire(s);
304
305 auto* const ledgerNode = packet->add_nodes();
306 ledgerNode->set_nodedata(s.peekData().data(), s.peekData().size());
307
308 // A leaf carries its own key, so the receiver rebuilds its position from that plus a
309 // depth. An inner node has no key and needs the full ID. The two fields are a oneof.
310 if (node->isLeaf())
311 {
312 ledgerNode->set_depth(nodeID.getDepth());
313 }
314 else
315 {
316 ledgerNode->set_id(nodeID.getRawString());
317 }
318 }
319
320 return packet;
321}
322
331[[nodiscard]] inline bool
333 std::function<bool()> const& condition,
334 std::chrono::steady_clock::duration deadline = std::chrono::seconds{10})
335{
336 auto const giveUp = std::chrono::steady_clock::now() + deadline;
337 while (std::chrono::steady_clock::now() < giveUp)
338 {
339 if (condition())
340 return true;
342 }
343 return condition();
344}
345
359[[nodiscard]] inline bool
360tallyIs(SHAMapAddNode const& san, int good, int bad, int duplicate)
361{
362 return san.getGood() == good && san.getBad() == bad && san.getDuplicate() == duplicate;
363}
364
365} // namespace xrpl::test
static constexpr std::size_t size()
Definition base_uint.h:549
static constexpr std::size_t kMinTxNodeBytesToParse
The size a node's hash-prefixed wire data must reach before gotNode() tries to parse and resubmit it ...
Supports data retrieval by managing a set of peers.
Definition PeerSet.h:30
int getDuplicate() const
UInt256 const & asUInt256() const
Definition SHAMapHash.h:26
Blob const & peekData() const
Definition Serializer.h:273
A consumption charge.
Definition Charge.h:13
bool hasLedger(UInt256 const &, std::uint32_t) const override
std::vector< resource::Charge > charges_
ChargeRecordingPeer(bool hasTxSet=true)
void charge(resource::Charge const &fee, std::string const &) override
static ID nextId()
The next id to hand out, distinct per instance because RequestCountingPeerSet dedups by tracked id an...
bool hasTxSet(UInt256 const &) const override
std::vector< resource::Charge > const & charges() const
void addPeers(std::size_t limit, std::function< bool(std::shared_ptr< Peer > const &)> hasItem, std::function< void(std::shared_ptr< Peer > const &)> onPeerAdded) override
Offer the candidates to the caller, the way the real peer set offers the peers the overlay is trackin...
std::set< Peer::ID > addedPeers() const
A set, because onTimer() keeps re-offering the same candidates.
std::vector< std::shared_ptr< Peer > > const candidates_
std::optional< std::size_t > firstLimit() const
The limit the first addPeers() call asked for, which is init()'s, since onTimer() keeps calling addPe...
void sendRequest(::google::protobuf::Message const &, protocol::MessageType, std::shared_ptr< Peer > const &peer) override
Record the request, and record it in the broadcast count as well when it names no peer.
RequestCountingPeerSet(std::vector< std::shared_ptr< Peer > > candidates={})
std::optional< std::size_t > firstLimit_
int broadcasts() const
How many of those requests carried no peer of their own.
std::set< Peer::ID > const & getPeerIds() const override
The ids of every peer addPeers() has selected, which is what an acquisition takes for the peers it is...
T data(T... args)
T make_shared(T... args)
STL namespace.
static UInt256 ledgerHash(LedgerHeader const &info)
bool waitFor(std::function< bool()> const &condition, std::chrono::steady_clock::duration deadline=std::chrono::seconds{10})
Poll until the condition holds, or give up.
std::shared_ptr< protocol::TMLedgerData > packetFor(DeepChain const &chain, std::vector< std::pair< SHAMapNodeID, SHAMapTreeNodePtr > > const &data, protocol::TMLedgerInfoType type=protocol::liTS_CANDIDATE, std::optional< UInt256 > const &ledgerHash=std::nullopt, std::uint32_t ledgerSeq=0)
The given nodes of a chain as a TMLedgerData, so a test can go through the real dispatch.
bool tallyIs(SHAMapAddNode const &san, int good, int bad, int duplicate)
Whether a batch verdict carries exactly the given counts.
BaseUInt< 256 > UInt256
Definition base_uint.h:581
T push_back(T... args)
T size(T... args)
T sleep_for(T... args)
static constexpr std::size_t kLeafItemBytes
The payload size of the leaf toLeaf() builds, which is the smallest a SHAMap item may be.
Definition DeepChain.h:54
A chain of inner nodes in wire form, from the root down to one deepest node, each with one real child...
Definition DeepChain.h:38