xrpld
Loading...
Searching...
No Matches
InboundLedger.h
1#pragma once
2
3#include <xrpld/app/ledger/detail/TimeoutCounter.h>
4#include <xrpld/app/main/Application.h>
5#include <xrpld/overlay/Peer.h>
6#include <xrpld/overlay/PeerSet.h>
7
8#include <xrpl/basics/CountedObject.h>
9#include <xrpl/basics/base_uint.h>
10#include <xrpl/beast/clock/abstract_clock.h>
11#include <xrpl/json/json_value.h>
12#include <xrpl/ledger/Ledger.h>
13#include <xrpl/nodestore/Database.h>
14#include <xrpl/shamap/SHAMap.h>
15#include <xrpl/shamap/SHAMapAddNode.h>
16#include <xrpl/shamap/SHAMapNodeID.h>
17
18#include <xrpl.pb.h>
19
20#include <chrono>
21#include <cstddef>
22#include <cstdint>
23#include <memory>
24#include <mutex>
25#include <set>
26#include <string_view>
27#include <utility>
28#include <vector>
29
30namespace xrpl {
31
32// A ledger we are trying to acquire
34 public std::enable_shared_from_this<InboundLedger>,
35 public CountedObject<InboundLedger>
36{
37public:
39
40 // These are the reasons we might acquire a ledger
41 enum class Reason {
42 HISTORY, // Acquiring past ledger
43 GENERIC, // Generic other reasons
44 CONSENSUS, // We believe the consensus round requires this ledger
45 };
46
51
63 Application& app,
64 UInt256 const& hash,
65 std::uint32_t seq,
66 Reason reason,
67 ClockType& clock,
70
71 ~InboundLedger() override;
72
73 // Called when another attempt is made to fetch this same ledger
74 void
76
80 bool
81 isComplete() const
82 {
83 return complete_;
84 }
85
89 bool
90 isFailed() const
91 {
92 return failed_;
93 }
94
105 getLedger() const
106 {
107 return failed_ ? nullptr : ledger_;
108 }
109
111 getSeq() const
112 {
113 return seq_;
114 }
115
116 bool
117 checkLocal();
118 void
119 init(ScopedLockType& collectionLock);
120
121 bool
123
125
130 getJson(int);
131
132 void
133 runData();
134
135 void
137 {
138 lastAction_ = clock_.now();
139 }
140
143 {
144 return lastAction_;
145 }
146
147protected:
148 // Protected so a test subclass can drive an acquisition through trigger() and done().
149
150 // Why trigger() is being run, which decides how deep a request goes and whether an
151 // aggressive retry applies.
153
160 void
161 trigger(std::shared_ptr<Peer> const& peer, TriggerReason reason);
162
167 void
168 done();
169
170private:
171 void
173
176
177 void
178 addPeers();
179
180 void
182
183 void
184 onTimer(bool progress, ScopedLockType& sl) override;
185
187 getPeerCount() const;
188
190 pmDowncast() override;
191
192 int
193 processData(std::shared_ptr<Peer> peer, protocol::TMLedgerData const& data);
194
195 bool
197
206 bool
208
209 void
211 std::shared_ptr<Peer> const& peer,
212 protocol::TMLedgerData const& packet,
213 SHAMapAddNode& san);
214
215 bool
217
218 bool
220
222 neededTxHashes(int max, SHAMapSyncFilter const* filter) const;
223
225 neededStateHashes(int max, SHAMapSyncFilter const* filter) const;
226
229
231 bool haveHeader_{false};
232 bool haveState_{false};
233 bool haveTransactions_{false};
234 bool signaled_{false};
235 bool byHash_{true};
238
240
242
243 // Data we have received from peers
249};
250
251} // namespace xrpl
Abstract interface to a clock.
std::chrono::steady_clock::time_point time_point
Represents a JSON value.
Definition json_value.h:121
InboundLedger(Application &app, UInt256 const &hash, std::uint32_t seq, Reason reason, ClockType &clock, std::unique_ptr< PeerSet > peerSet, std::chrono::milliseconds retryInterval=kRetryInterval)
json::Value getJson(int)
Return a json::ValueType::Object.
void tryDB(node_store::Database &srcDB)
std::weak_ptr< TimeoutCounter > pmDowncast() override
Return a weak pointer to this.
void trigger(std::shared_ptr< Peer > const &peer, TriggerReason reason)
Ask for more nodes, or judge what has been collected.
void runData()
Process pending TMLedgerData Query the a random sample of the 'best' peers.
void filterNodes(std::vector< std::pair< SHAMapNodeID, UInt256 > > &nodes, TriggerReason reason)
static constexpr std::chrono::milliseconds kRetryInterval
How long to wait between retries, and so how long one timeout takes.
std::size_t getPeerCount() const
bool failOnZeroAccountHash()
Fail the acquisition when the header's account hash is zero.
std::set< UInt256 > recentNodes_
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.
std::vector< UInt256 > neededStateHashes(int max, SHAMapSyncFilter const *filter) const
SHAMapAddNode stats_
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 takeAsRootNode(std::string_view data, SHAMapAddNode &san)
Process AS root node received from a peer Call with a lock.
std::shared_ptr< Ledger > ledger_
bool isComplete() const
Returns true if we got all the data.
std::vector< std::pair< std::weak_ptr< Peer >, std::shared_ptr< protocol::TMLedgerData > > > receivedData_
std::pair< protocol::TMGetObjectByHash::ObjectType, UInt256 > NeededHashT
ClockType::time_point getLastAction() const
std::uint32_t getSeq() const
std::mutex receivedDataLock_
ClockType::time_point lastAction_
std::unique_ptr< PeerSet > peerSet_
void addPeers()
Add more peers to the set, if possible.
bool takeTxRootNode(std::string_view data, SHAMapAddNode &san)
Process AS root node received from a peer Call with a lock.
void onTimer(bool progress, ScopedLockType &sl) override
Called with a lock by the PeerSet when the timer expires.
void init(ScopedLockType &collectionLock)
void update(std::uint32_t seq)
void done()
Settle the acquisition and signal whatever is waiting on it.
bool isFailed() const
Returns false if we failed to get the data.
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.
std::vector< NeededHashT > getNeededHashes()
std::shared_ptr< Ledger const > getLedger() const
The acquired ledger.
beast::AbstractClock< std::chrono::steady_clock > ClockType
std::vector< UInt256 > neededTxHashes(int max, SHAMapSyncFilter const *filter) const
std::unique_lock< std::recursive_mutex > ScopedLockType
TimeoutCounter(Application &app, UInt256 const &targetHash, std::chrono::milliseconds timeoutInterval, QueueJobParameter &&jobParameter, beast::Journal journal)
Persistency layer for NodeObject.
Definition Database.h:45
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
BaseUInt< 256 > UInt256
Definition base_uint.h:581