xrpld
Loading...
Searching...
No Matches
xrpl::InboundLedger Class Reference

#include <InboundLedger.h>

Inheritance diagram for xrpl::InboundLedger:
Collaboration diagram for xrpl::InboundLedger:

Public Types

enum class  Reason { HISTORY , GENERIC , CONSENSUS }
using ClockType = beast::AbstractClock<std::chrono::steady_clock>
using NeededHashT = std::pair<protocol::TMGetObjectByHash::ObjectType, UInt256>

Public Member Functions

 InboundLedger (Application &app, UInt256 const &hash, std::uint32_t seq, Reason reason, ClockType &clock, std::unique_ptr< PeerSet > peerSet, std::chrono::milliseconds retryInterval=kRetryInterval)
 ~InboundLedger () override
void update (std::uint32_t seq)
bool isComplete () const
 Returns true if we got all the data.
bool isFailed () const
 Returns false if we failed to get the data.
std::shared_ptr< Ledger const > getLedger () const
 The acquired ledger.
std::uint32_t getSeq () const
bool checkLocal ()
void init (ScopedLockType &collectionLock)
bool gotData (std::weak_ptr< Peer >, std::shared_ptr< protocol::TMLedgerData > const &)
 Stash a TMLedgerData received from a peer for later processing Returns 'true' if we need to dispatch.
json::Value getJson (int)
 Return a json::ValueType::Object.
void runData ()
 Process pending TMLedgerData Query the a random sample of the 'best' peers.
void touch ()
ClockType::time_point getLastAction () const
virtual void cancel ()
 Cancel the task by marking it as failed if the task is not done.
T shared_from_this (T... args)
T weak_from_this (T... args)

Public Attributes

friend Object

Static Public Attributes

static constexpr std::chrono::milliseconds kRetryInterval {3000}
 How long to wait between retries, and so how long one timeout takes.

Protected Types

enum class  TriggerReason { Added , Reply , Timeout }
using ScopedLockType = std::unique_lock<std::recursive_mutex>

Protected Member Functions

void trigger (std::shared_ptr< Peer > const &peer, TriggerReason reason)
 Ask for more nodes, or judge what has been collected.
void done ()
 Settle the acquisition and signal whatever is waiting on it.
void setTimer (ScopedLockType &)
 Schedule a call to queueJob() after timerInterval_.
void queueJob (ScopedLockType &)
 Queue a job to call invokeOnTimer().
bool isDone () const

Protected Attributes

Application & app_
beast::Journal journal_
std::recursive_mutex mtx_
UInt256 const hash_
 The hash of the object (in practice, always a ledger) we are trying to fetch.
int timeouts_ {0}
bool complete_ {false}
bool failed_ {false}
bool progress_ {false}
 Whether forward progress has been made.
std::chrono::milliseconds timerInterval_
 The minimum time to wait between calls to execute().
QueueJobParameter queueJobParameter_

Private Member Functions

void filterNodes (std::vector< std::pair< SHAMapNodeID, UInt256 > > &nodes, TriggerReason reason)
std::vector< NeededHashT > getNeededHashes ()
void addPeers ()
 Add more peers to the set, if possible.
void tryDB (node_store::Database &srcDB)
void onTimer (bool progress, ScopedLockType &sl) override
 Called with a lock by the PeerSet when the timer expires.
std::size_t getPeerCount () const
std::weak_ptr< TimeoutCounter > pmDowncast () override
 Return a weak pointer to this.
int processData (std::shared_ptr< Peer > peer, protocol::TMLedgerData const &data)
 Process one TMLedgerData Returns the number of useful nodes.
bool takeHeader (std::string_view data)
 Build the ledger from a header a peer supplied.
bool failOnZeroAccountHash ()
 Fail the acquisition when the header's account hash is zero.
void receiveNode (std::shared_ptr< Peer > const &peer, protocol::TMLedgerData const &packet, SHAMapAddNode &san)
 Process node data received from a peer Call with a lock.
bool takeTxRootNode (std::string_view data, SHAMapAddNode &san)
 Process AS root node received from a peer Call with a lock.
bool takeAsRootNode (std::string_view data, SHAMapAddNode &san)
 Process AS root node received from a peer Call with a lock.
std::vector< UInt256 > neededTxHashes (int max, SHAMapSyncFilter const *filter) const
std::vector< UInt256 > neededStateHashes (int max, SHAMapSyncFilter const *filter) const
void invokeOnTimer ()
 Calls onTimer() if in the right state.

Static Private Member Functions

static auto & getCounter () noexcept

Private Attributes

ClockType & clock_
ClockType::time_point lastAction_
std::shared_ptr< Ledger > ledger_
bool haveHeader_ {false}
bool haveState_ {false}
bool haveTransactions_ {false}
bool signaled_ {false}
bool byHash_ {true}
std::uint32_t seq_
Reason const reason_
std::set< UInt256 > recentNodes_
SHAMapAddNode stats_
std::mutex receivedDataLock_
std::vector< std::pair< std::weak_ptr< Peer >, std::shared_ptr< protocol::TMLedgerData > > > receivedData_
bool receiveDispatched_ {false}
std::unique_ptr< PeerSet > peerSet_
boost::asio::basic_waitable_timer< std::chrono::steady_clock > timer_

Detailed Description

Definition at line 33 of file InboundLedger.h.

Member Typedef Documentation

◆ ClockType

◆ NeededHashT

using xrpl::InboundLedger::NeededHashT = std::pair<protocol::TMGetObjectByHash::ObjectType, UInt256>

Definition at line 124 of file InboundLedger.h.

◆ ScopedLockType

Definition at line 71 of file TimeoutCounter.h.

Member Enumeration Documentation

◆ Reason

enum class xrpl::InboundLedger::Reason
strong
Enumerator
HISTORY 
GENERIC 
CONSENSUS 

Definition at line 41 of file InboundLedger.h.

◆ TriggerReason

enum class xrpl::InboundLedger::TriggerReason
strongprotected
Enumerator
Added 
Reply 
Timeout 

Definition at line 152 of file InboundLedger.h.

Constructor & Destructor Documentation

◆ InboundLedger()

xrpl::InboundLedger::InboundLedger ( Application & app,
UInt256 const & hash,
std::uint32_t seq,
Reason reason,
ClockType & clock,
std::unique_ptr< PeerSet > peerSet,
std::chrono::milliseconds retryInterval = kRetryInterval )
Parameters
appThe application to run in.
hashThe ledger to acquire.
seqIts sequence, or zero if not known yet.
reasonWhy it is being acquired.
clockThe clock touch() records against.
peerSetWhich peers to ask, and how to reach them.
retryIntervalHow long to wait between retries. TimeoutCounter requires more than 10ms and less than 30s.

Definition at line 67 of file InboundLedger.cpp.

◆ ~InboundLedger()

xrpl::InboundLedger::~InboundLedger ( )
override

Definition at line 186 of file InboundLedger.cpp.

Member Function Documentation

◆ update()

void xrpl::InboundLedger::update ( std::uint32_t seq)

Definition at line 151 of file InboundLedger.cpp.

◆ isComplete()

bool xrpl::InboundLedger::isComplete ( ) const

Returns true if we got all the data.

Definition at line 81 of file InboundLedger.h.

◆ isFailed()

bool xrpl::InboundLedger::isFailed ( ) const

Returns false if we failed to get the data.

Definition at line 90 of file InboundLedger.h.

◆ getLedger()

std::shared_ptr< Ledger const > xrpl::InboundLedger::getLedger ( ) const

The acquired ledger.

A failed acquisition may still hold a partially built ledger, which getJson() reports on, so ledger_ is kept while this answers nullptr.

Returns
The ledger, or nullptr before a header is obtained and once the acquisition has failed.

Definition at line 105 of file InboundLedger.h.

◆ getSeq()

std::uint32_t xrpl::InboundLedger::getSeq ( ) const

Definition at line 111 of file InboundLedger.h.

◆ checkLocal()

bool xrpl::InboundLedger::checkLocal ( )

Definition at line 164 of file InboundLedger.cpp.

◆ init()

void xrpl::InboundLedger::init ( ScopedLockType & collectionLock)

Definition at line 91 of file InboundLedger.cpp.

◆ gotData()

bool xrpl::InboundLedger::gotData ( std::weak_ptr< Peer > peer,
std::shared_ptr< protocol::TMLedgerData > const & data )

Stash a TMLedgerData received from a peer for later processing Returns 'true' if we need to dispatch.

Definition at line 1108 of file InboundLedger.cpp.

◆ getJson()

json::Value xrpl::InboundLedger::getJson ( int )

Return a json::ValueType::Object.

Definition at line 1370 of file InboundLedger.cpp.

◆ runData()

void xrpl::InboundLedger::runData ( )

Process pending TMLedgerData Query the a random sample of the 'best' peers.

Definition at line 1323 of file InboundLedger.cpp.

◆ touch()

void xrpl::InboundLedger::touch ( )

Definition at line 136 of file InboundLedger.h.

◆ getLastAction()

ClockType::time_point xrpl::InboundLedger::getLastAction ( ) const

Definition at line 142 of file InboundLedger.h.

◆ trigger()

void xrpl::InboundLedger::trigger ( std::shared_ptr< Peer > const & peer,
TriggerReason reason )
protected

Ask for more nodes, or judge what has been collected.

Request more nodes, perhaps from a specific peer.

Parameters
peerThe peer to ask, or nullptr to ask everyone being tracked.
reasonWhy the acquisition is being triggered.

Definition at line 504 of file InboundLedger.cpp.

◆ done()

void xrpl::InboundLedger::done ( )
protected

Settle the acquisition and signal whatever is waiting on it.

Runs at most once. Call under mtx_, which the flags written here require.

Definition at line 436 of file InboundLedger.cpp.

◆ filterNodes()

void xrpl::InboundLedger::filterNodes ( std::vector< std::pair< SHAMapNodeID, UInt256 > > & nodes,
TriggerReason reason )
private

Definition at line 780 of file InboundLedger.cpp.

◆ getNeededHashes()

std::vector< InboundLedger::NeededHashT > xrpl::InboundLedger::getNeededHashes ( )
private

Definition at line 1072 of file InboundLedger.cpp.

◆ addPeers()

void xrpl::InboundLedger::addPeers ( )
private

Add more peers to the set, if possible.

Definition at line 416 of file InboundLedger.cpp.

◆ tryDB()

void xrpl::InboundLedger::tryDB ( node_store::Database & srcDB)
private

Definition at line 243 of file InboundLedger.cpp.

◆ onTimer()

void xrpl::InboundLedger::onTimer ( bool progress,
ScopedLockType & sl )
overrideprivatevirtual

Called with a lock by the PeerSet when the timer expires.

Implements xrpl::TimeoutCounter.

Definition at line 366 of file InboundLedger.cpp.

◆ getPeerCount()

std::size_t xrpl::InboundLedger::getPeerCount ( ) const
private

Definition at line 143 of file InboundLedger.cpp.

◆ pmDowncast()

std::weak_ptr< TimeoutCounter > xrpl::InboundLedger::pmDowncast ( )
overrideprivatevirtual

Return a weak pointer to this.

Implements xrpl::TimeoutCounter.

Definition at line 430 of file InboundLedger.cpp.

◆ processData()

int xrpl::InboundLedger::processData ( std::shared_ptr< Peer > peer,
protocol::TMLedgerData const & data )
private

Process one TMLedgerData Returns the number of useful nodes.

Definition at line 1136 of file InboundLedger.cpp.

◆ takeHeader()

bool xrpl::InboundLedger::takeHeader ( std::string_view data)
private

Build the ledger from a header a peer supplied.

Call with mtx_ held.

Parameters
dataThe serialized header, without a hash prefix.
Returns
False for a header other than the one asked for. True otherwise, also when the header cannot name a ledger, and then failed_ is set, ledger_ is null, haveHeader_ stays false, and the caller calls done().

Definition at line 841 of file InboundLedger.cpp.

◆ failOnZeroAccountHash()

bool xrpl::InboundLedger::failOnZeroAccountHash ( )
private

Fail the acquisition when the header's account hash is zero.

No ledger has an empty state map, so such a header cannot name a ledger. Both tryDB() and takeHeader() judge the header here.

Returns
Whether the acquisition was failed. Then failed_ is set and ledger_ is null.

Definition at line 819 of file InboundLedger.cpp.

◆ receiveNode()

void xrpl::InboundLedger::receiveNode ( std::shared_ptr< Peer > const & peer,
protocol::TMLedgerData const & packet,
SHAMapAddNode & san )
private

Process node data received from a peer Call with a lock.

Definition at line 889 of file InboundLedger.cpp.

◆ takeTxRootNode()

bool xrpl::InboundLedger::takeTxRootNode ( std::string_view data,
SHAMapAddNode & san )
private

Process AS root node received from a peer Call with a lock.

Definition at line 1040 of file InboundLedger.cpp.

◆ takeAsRootNode()

bool xrpl::InboundLedger::takeAsRootNode ( std::string_view data,
SHAMapAddNode & san )
private

Process AS root node received from a peer Call with a lock.

Definition at line 1004 of file InboundLedger.cpp.

◆ neededTxHashes()

std::vector< UInt256 > xrpl::InboundLedger::neededTxHashes ( int max,
SHAMapSyncFilter const * filter ) const
private

Definition at line 229 of file InboundLedger.cpp.

◆ neededStateHashes()

std::vector< UInt256 > xrpl::InboundLedger::neededStateHashes ( int max,
SHAMapSyncFilter const * filter ) const
private

Definition at line 235 of file InboundLedger.cpp.

◆ cancel()

void xrpl::TimeoutCounter::cancel ( )
virtualinherited

Cancel the task by marking it as failed if the task is not done.

Note
this function does not attempt to cancel the scheduled timer or to remove the queued job if any. When the timer expires or the queued job starts, however, the code will see that the task is done and returns immediately, if it can lock the weak pointer of the task.

Definition at line 105 of file TimeoutCounter.cpp.

◆ setTimer()

void xrpl::TimeoutCounter::setTimer ( ScopedLockType & sl)
protectedinherited

Schedule a call to queueJob() after timerInterval_.

Definition at line 40 of file TimeoutCounter.cpp.

◆ queueJob()

void xrpl::TimeoutCounter::queueJob ( ScopedLockType & sl)
protectedinherited

Queue a job to call invokeOnTimer().

Definition at line 58 of file TimeoutCounter.cpp.

◆ isDone()

bool xrpl::TimeoutCounter::isDone ( ) const
protectedinherited

Definition at line 117 of file TimeoutCounter.h.

◆ invokeOnTimer()

void xrpl::TimeoutCounter::invokeOnTimer ( )
privateinherited

Calls onTimer() if in the right state.

Only called by queueJob().

Definition at line 80 of file TimeoutCounter.cpp.

◆ getCounter()

auto & xrpl::CountedObject< InboundLedger >::getCounter ( )
staticprivatenoexceptinherited

Definition at line 113 of file CountedObject.h.

Member Data Documentation

◆ kRetryInterval

std::chrono::milliseconds xrpl::InboundLedger::kRetryInterval {3000}
staticconstexpr

How long to wait between retries, and so how long one timeout takes.

Definition at line 50 of file InboundLedger.h.

◆ clock_

ClockType& xrpl::InboundLedger::clock_
private

Definition at line 227 of file InboundLedger.h.

◆ lastAction_

ClockType::time_point xrpl::InboundLedger::lastAction_
private

Definition at line 228 of file InboundLedger.h.

◆ ledger_

std::shared_ptr<Ledger> xrpl::InboundLedger::ledger_
private

Definition at line 230 of file InboundLedger.h.

◆ haveHeader_

bool xrpl::InboundLedger::haveHeader_ {false}
private

Definition at line 231 of file InboundLedger.h.

◆ haveState_

bool xrpl::InboundLedger::haveState_ {false}
private

Definition at line 232 of file InboundLedger.h.

◆ haveTransactions_

bool xrpl::InboundLedger::haveTransactions_ {false}
private

Definition at line 233 of file InboundLedger.h.

◆ signaled_

bool xrpl::InboundLedger::signaled_ {false}
private

Definition at line 234 of file InboundLedger.h.

◆ byHash_

bool xrpl::InboundLedger::byHash_ {true}
private

Definition at line 235 of file InboundLedger.h.

◆ seq_

std::uint32_t xrpl::InboundLedger::seq_
private

Definition at line 236 of file InboundLedger.h.

◆ reason_

Reason const xrpl::InboundLedger::reason_
private

Definition at line 237 of file InboundLedger.h.

◆ recentNodes_

std::set<UInt256> xrpl::InboundLedger::recentNodes_
private

Definition at line 239 of file InboundLedger.h.

◆ stats_

SHAMapAddNode xrpl::InboundLedger::stats_
private

Definition at line 241 of file InboundLedger.h.

◆ receivedDataLock_

std::mutex xrpl::InboundLedger::receivedDataLock_
private

Definition at line 244 of file InboundLedger.h.

◆ receivedData_

std::vector<std::pair<std::weak_ptr<Peer>, std::shared_ptr<protocol::TMLedgerData> > > xrpl::InboundLedger::receivedData_
private

Definition at line 246 of file InboundLedger.h.

◆ receiveDispatched_

bool xrpl::InboundLedger::receiveDispatched_ {false}
private

Definition at line 247 of file InboundLedger.h.

◆ peerSet_

std::unique_ptr<PeerSet> xrpl::InboundLedger::peerSet_
private

Definition at line 248 of file InboundLedger.h.

◆ app_

Application& xrpl::TimeoutCounter::app_
protectedinherited

Definition at line 124 of file TimeoutCounter.h.

◆ journal_

beast::Journal xrpl::TimeoutCounter::journal_
protectedinherited

Definition at line 125 of file TimeoutCounter.h.

◆ mtx_

std::recursive_mutex xrpl::TimeoutCounter::mtx_
mutableprotectedinherited

Definition at line 126 of file TimeoutCounter.h.

◆ hash_

UInt256 const xrpl::TimeoutCounter::hash_
protectedinherited

The hash of the object (in practice, always a ledger) we are trying to fetch.

Definition at line 132 of file TimeoutCounter.h.

◆ timeouts_

int xrpl::TimeoutCounter::timeouts_ {0}
protectedinherited

Definition at line 133 of file TimeoutCounter.h.

◆ complete_

bool xrpl::TimeoutCounter::complete_ {false}
protectedinherited

Definition at line 134 of file TimeoutCounter.h.

◆ failed_

bool xrpl::TimeoutCounter::failed_ {false}
protectedinherited

Definition at line 135 of file TimeoutCounter.h.

◆ progress_

bool xrpl::TimeoutCounter::progress_ {false}
protectedinherited

Whether forward progress has been made.

Definition at line 139 of file TimeoutCounter.h.

◆ timerInterval_

std::chrono::milliseconds xrpl::TimeoutCounter::timerInterval_
protectedinherited

The minimum time to wait between calls to execute().

Definition at line 143 of file TimeoutCounter.h.

◆ queueJobParameter_

QueueJobParameter xrpl::TimeoutCounter::queueJobParameter_
protectedinherited

Definition at line 145 of file TimeoutCounter.h.

◆ timer_

boost::asio::basic_waitable_timer<std::chrono::steady_clock> xrpl::TimeoutCounter::timer_
privateinherited

Definition at line 155 of file TimeoutCounter.h.

◆ Object

Definition at line 138 of file CountedObject.h.