xrpld
Loading...
Searching...
No Matches
xrpl::test::RequestCountingPeerSet Class Referenceabstract

A peer set that counts the requests an acquisition makes through it. More...

#include <AcquireTestHelpers.h>

Inheritance diagram for xrpl::test::RequestCountingPeerSet:
Collaboration diagram for xrpl::test::RequestCountingPeerSet:

Public Member Functions

 RequestCountingPeerSet (std::vector< std::shared_ptr< Peer > > candidates={})
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 tracking.
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.
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 tracking.
int requests () const
int broadcasts () const
 How many of those requests carried no peer of their own.
std::optional< std::size_t > firstLimit () const
 The limit the first addPeers() call asked for, which is init()'s, since onTimer() keeps calling addPeers(1) for as long as an acquisition runs.
std::set< Peer::ID > addedPeers () const
 A set, because onTimer() keeps re-offering the same candidates.
virtual void addPeers (std::size_t limit, std::function< bool(std::shared_ptr< Peer > const &)> hasItem, std::function< void(std::shared_ptr< Peer > const &)> onPeerAdded)=0
 Try add more peers.
template<typename MessageType>
void sendRequest (MessageType const &message, std::shared_ptr< Peer > const &peer)
 send a message
virtual void sendRequest (::google::protobuf::Message const &message, protocol::MessageType type, std::shared_ptr< Peer > const &peer)=0

Private Attributes

std::vector< std::shared_ptr< Peer > > const candidates_
std::mutex mutex_
int requests_ {0}
int broadcasts_ {0}
std::optional< std::size_t > firstLimit_
std::set< Peer::ID > addedPeers_

Detailed Description

A peer set that counts the requests an acquisition makes through it.

Offers peers to a hasItem/onPeerAdded callback pair, hard-filtered by hasItem (which only scores in the real peer set) and deduped by tracked id.

A count is a call, not a delivery: a request naming no peer reaches nobody while this set tracks none. requests() and broadcasts() are counted apart.

Every write here is guarded, since the retry timer drives addPeers() and sendRequest() from a job thread while the test reads the results.

Definition at line 128 of file AcquireTestHelpers.h.

Constructor & Destructor Documentation

◆ RequestCountingPeerSet()

xrpl::test::RequestCountingPeerSet::RequestCountingPeerSet ( std::vector< std::shared_ptr< Peer > > candidates = {})
explicit
Parameters
candidatesThe peers addPeers() may offer, in the order they are considered. Fixed at construction. Empty offers no one.

Definition at line 135 of file AcquireTestHelpers.h.

Member Function Documentation

◆ addPeers() [1/2]

void xrpl::test::RequestCountingPeerSet::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 tracking.

Parameters
limitThe most peers to add, recorded for firstLimit().
hasItemHard-filters the candidates worth asking, where the real peer set only scores with it.
onPeerAddedCalled for each selected candidate.

Definition at line 150 of file AcquireTestHelpers.h.

◆ sendRequest() [1/3]

void xrpl::test::RequestCountingPeerSet::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.

Parameters
peerThe peer to ask, or null to ask every tracked peer.

Definition at line 184 of file AcquireTestHelpers.h.

◆ getPeerIds()

std::set< Peer::ID > const & xrpl::test::RequestCountingPeerSet::getPeerIds ( ) const
nodiscardoverridevirtual

The ids of every peer addPeers() has selected, which is what an acquisition takes for the peers it is tracking.

Unguarded: every caller of this and of addPeers() is an acquisition holding its own mtx_, so the ids stay fixed while a caller iterates. A test thread reads addedPeers() instead. InboundLedger::getPeerCount() reports zero for these, since it resolves ids through the overlay, while these peers live only in this harness.

Returns
The ids.

Implements xrpl::PeerSet.

Definition at line 208 of file AcquireTestHelpers.h.

◆ requests()

int xrpl::test::RequestCountingPeerSet::requests ( ) const
nodiscard
Returns
How many requests have been sent through this peer set, counting a broadcast as one.

Definition at line 218 of file AcquireTestHelpers.h.

◆ broadcasts()

int xrpl::test::RequestCountingPeerSet::broadcasts ( ) const
nodiscard

How many of those requests carried no peer of their own.

Returns
The count.

Definition at line 230 of file AcquireTestHelpers.h.

◆ firstLimit()

std::optional< std::size_t > xrpl::test::RequestCountingPeerSet::firstLimit ( ) const
nodiscard

The limit the first addPeers() call asked for, which is init()'s, since onTimer() keeps calling addPeers(1) for as long as an acquisition runs.

Returns
The limit, or nullopt if addPeers() has not been called.

Definition at line 243 of file AcquireTestHelpers.h.

◆ addedPeers()

std::set< Peer::ID > xrpl::test::RequestCountingPeerSet::addedPeers ( ) const
nodiscard

A set, because onTimer() keeps re-offering the same candidates.

Returns
The ids of every peer addPeers() has selected so far.

Definition at line 255 of file AcquireTestHelpers.h.

◆ addPeers() [2/2]

virtual void xrpl::PeerSet::addPeers ( std::size_t limit,
std::function< bool(std::shared_ptr< Peer > const &)> hasItem,
std::function< void(std::shared_ptr< Peer > const &)> onPeerAdded )
pure virtualinherited

Try add more peers.

Parameters
limitnumber of peers to add
hasItemcallback that helps to select peers
onPeerAddedcallback called when a peer is added

Implemented in xrpl::DummyPeerSet, and xrpl::PeerSetImpl.

◆ sendRequest() [2/3]

template<typename MessageType>
void xrpl::PeerSet::sendRequest ( MessageType const & message,
std::shared_ptr< Peer > const & peer )
inherited

send a message

Definition at line 51 of file PeerSet.h.

◆ sendRequest() [3/3]

virtual void xrpl::PeerSet::sendRequest ( ::google::protobuf::Message const & message,
protocol::MessageType type,
std::shared_ptr< Peer > const & peer )
pure virtualinherited

Implemented in xrpl::DummyPeerSet, and xrpl::PeerSetImpl.

Member Data Documentation

◆ candidates_

std::vector<std::shared_ptr<Peer> > const xrpl::test::RequestCountingPeerSet::candidates_
private

Definition at line 262 of file AcquireTestHelpers.h.

◆ mutex_

std::mutex xrpl::test::RequestCountingPeerSet::mutex_
mutableprivate

Definition at line 264 of file AcquireTestHelpers.h.

◆ requests_

int xrpl::test::RequestCountingPeerSet::requests_ {0}
private

Definition at line 265 of file AcquireTestHelpers.h.

◆ broadcasts_

int xrpl::test::RequestCountingPeerSet::broadcasts_ {0}
private

Definition at line 266 of file AcquireTestHelpers.h.

◆ firstLimit_

std::optional<std::size_t> xrpl::test::RequestCountingPeerSet::firstLimit_
private

Definition at line 267 of file AcquireTestHelpers.h.

◆ addedPeers_

std::set<Peer::ID> xrpl::test::RequestCountingPeerSet::addedPeers_
private

Definition at line 268 of file AcquireTestHelpers.h.