rippled
InboundLedger.h
1 //------------------------------------------------------------------------------
2 /*
3  This file is part of rippled: https://github.com/ripple/rippled
4  Copyright (c) 2012, 2013 Ripple Labs Inc.
5 
6  Permission to use, copy, modify, and/or distribute this software for any
7  purpose with or without fee is hereby granted, provided that the above
8  copyright notice and this permission notice appear in all copies.
9 
10  THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18 //==============================================================================
19 
20 #ifndef RIPPLE_APP_LEDGER_INBOUNDLEDGER_H_INCLUDED
21 #define RIPPLE_APP_LEDGER_INBOUNDLEDGER_H_INCLUDED
22 
23 #include <ripple/app/ledger/Ledger.h>
24 #include <ripple/app/ledger/impl/TimeoutCounter.h>
25 #include <ripple/app/main/Application.h>
26 #include <ripple/basics/CountedObject.h>
27 #include <ripple/overlay/PeerSet.h>
28 #include <mutex>
29 #include <set>
30 #include <utility>
31 
32 namespace ripple {
33 
34 // A ledger we are trying to acquire
35 class InboundLedger final : public TimeoutCounter,
36  public std::enable_shared_from_this<InboundLedger>,
37  public CountedObject<InboundLedger>
38 {
39 public:
41 
42  // These are the reasons we might acquire a ledger
43  enum class Reason {
44  HISTORY, // Acquiring past ledger
45  SHARD, // Acquiring for shard
46  GENERIC, // Generic other reasons
47  CONSENSUS // We believe the consensus round requires this ledger
48  };
49 
51  Application& app,
52  uint256 const& hash,
53  std::uint32_t seq,
54  Reason reason,
55  clock_type&,
56  std::unique_ptr<PeerSet> peerSet);
57 
59 
60  // Called when another attempt is made to fetch this same ledger
61  void
62  update(std::uint32_t seq);
63 
65  bool
66  isComplete() const
67  {
68  return complete_;
69  }
70 
72  bool
73  isFailed() const
74  {
75  return failed_;
76  }
77 
79  getLedger() const
80  {
81  return mLedger;
82  }
83 
85  getSeq() const
86  {
87  return mSeq;
88  }
89 
90  bool
91  checkLocal();
92  void
93  init(ScopedLockType& collectionLock);
94 
95  bool
96  gotData(
99 
100  using neededHash_t =
102 
105  getJson(int);
106 
107  void
108  runData();
109 
110  void
112  {
113  mLastAction = m_clock.now();
114  }
115 
118  {
119  return mLastAction;
120  }
121 
122 private:
123  enum class TriggerReason { added, reply, timeout };
124 
125  void
126  filterNodes(
128  TriggerReason reason);
129 
130  void
132 
134  getNeededHashes();
135 
136  void
137  addPeers();
138 
139  void
140  tryDB(NodeStore::Database& srcDB);
141 
142  void
143  done();
144 
145  void
146  onTimer(bool progress, ScopedLockType& peerSetLock) override;
147 
149  getPeerCount() const;
150 
152  pmDowncast() override;
153 
154  int
155  processData(std::shared_ptr<Peer> peer, protocol::TMLedgerData& data);
156 
157  bool
158  takeHeader(std::string const& data);
159 
160  void
161  receiveNode(protocol::TMLedgerData& packet, SHAMapAddNode&);
162 
163  bool
164  takeTxRootNode(Slice const& data, SHAMapAddNode&);
165 
166  bool
167  takeAsRootNode(Slice const& data, SHAMapAddNode&);
168 
170  neededTxHashes(int max, SHAMapSyncFilter* filter) const;
171 
173  neededStateHashes(int max, SHAMapSyncFilter* filter) const;
174 
177 
182  bool mSignaled;
183  bool mByHash;
186 
188 
190 
191  // Data we have received from peers
193  std::vector<
198 };
199 
202 deserializeHeader(Slice data, bool hasHash = false);
203 
206 deserializePrefixedHeader(Slice data, bool hasHash = false);
207 
208 } // namespace ripple
209 
210 #endif
ripple::InboundLedger::mRecentNodes
std::set< uint256 > mRecentNodes
Definition: InboundLedger.h:187
ripple::Application
Definition: Application.h:115
ripple::SHAMapAddNode
Definition: SHAMapAddNode.h:28
ripple::InboundLedger::Reason::HISTORY
@ HISTORY
ripple::InboundLedger::getNeededHashes
std::vector< neededHash_t > getNeededHashes()
Definition: InboundLedger.cpp:1065
ripple::InboundLedger::mReason
const Reason mReason
Definition: InboundLedger.h:185
ripple::InboundLedger::getJson
Json::Value getJson(int)
Return a Json::objectValue.
Definition: InboundLedger.cpp:1359
ripple::TimeoutCounter::ScopedLockType
std::unique_lock< std::recursive_mutex > ScopedLockType
Definition: TimeoutCounter.h:81
ripple::InboundLedger::TriggerReason
TriggerReason
Definition: InboundLedger.h:123
ripple::CountedObject
Tracks the number of instances of an object.
Definition: CountedObject.h:124
ripple::NodeStore::Database
Persistency layer for NodeObject.
Definition: Database.h:51
std::string
STL class.
ripple::InboundLedger::Reason::CONSENSUS
@ CONSENSUS
std::shared_ptr
STL class.
ripple::InboundLedger::mHaveState
bool mHaveState
Definition: InboundLedger.h:180
ripple::InboundLedger
Definition: InboundLedger.h:35
utility
ripple::InboundLedger::TriggerReason::added
@ added
ripple::InboundLedger::getSeq
std::uint32_t getSeq() const
Definition: InboundLedger.h:85
ripple::Slice
An immutable linear range of bytes.
Definition: Slice.h:44
ripple::InboundLedger::mSignaled
bool mSignaled
Definition: InboundLedger.h:182
std::pair
ripple::InboundLedger::Reason::GENERIC
@ GENERIC
std::vector
STL class.
ripple::InboundLedger::getLedger
std::shared_ptr< Ledger const > getLedger() const
Definition: InboundLedger.h:79
ripple::InboundLedger::update
void update(std::uint32_t seq)
Definition: InboundLedger.cpp:183
ripple::InboundLedger::touch
void touch()
Definition: InboundLedger.h:111
ripple::InboundLedger::~InboundLedger
~InboundLedger()
Definition: InboundLedger.cpp:216
ripple::InboundLedger::mByHash
bool mByHash
Definition: InboundLedger.h:183
ripple::InboundLedger::filterNodes
void filterNodes(std::vector< std::pair< SHAMapNodeID, uint256 >> &nodes, TriggerReason reason)
Definition: InboundLedger.cpp:834
ripple::InboundLedger::isFailed
bool isFailed() const
Returns false if we failed to get the data.
Definition: InboundLedger.h:73
ripple::InboundLedger::processData
int processData(std::shared_ptr< Peer > peer, protocol::TMLedgerData &data)
Process one TMLedgerData Returns the number of useful nodes.
Definition: InboundLedger.cpp:1132
ripple::InboundLedger::neededStateHashes
std::vector< uint256 > neededStateHashes(int max, SHAMapSyncFilter *filter) const
Definition: InboundLedger.cpp:268
ripple::InboundLedger::neededHash_t
std::pair< protocol::TMGetObjectByHash::ObjectType, uint256 > neededHash_t
Definition: InboundLedger.h:101
ripple::InboundLedger::mPeerSet
std::unique_ptr< PeerSet > mPeerSet
Definition: InboundLedger.h:197
ripple::TimeoutCounter
This class is an "active" object.
Definition: TimeoutCounter.h:66
ripple::deserializePrefixedHeader
LedgerInfo deserializePrefixedHeader(Slice data, bool hasHash)
Deserialize a ledger header (prefixed with 4 bytes) from a byte array.
Definition: InboundLedger.cpp:299
beast::abstract_clock::now
virtual time_point now() const =0
Returns the current time.
ripple::InboundLedger::neededTxHashes
std::vector< uint256 > neededTxHashes(int max, SHAMapSyncFilter *filter) const
Definition: InboundLedger.cpp:262
ripple::base_uint< 256 >
ripple::InboundLedger::takeHeader
bool takeHeader(std::string const &data)
Take ledger header data Call with a lock.
Definition: InboundLedger.cpp:880
ripple::InboundLedger::gotData
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.
Definition: InboundLedger.cpp:1105
ripple::InboundLedger::mLedger
std::shared_ptr< Ledger > mLedger
Definition: InboundLedger.h:178
ripple::InboundLedger::clock_type
beast::abstract_clock< std::chrono::steady_clock > clock_type
Definition: InboundLedger.h:40
ripple::InboundLedger::mLastAction
clock_type::time_point mLastAction
Definition: InboundLedger.h:176
ripple::InboundLedger::getLastAction
clock_type::time_point getLastAction() const
Definition: InboundLedger.h:117
ripple::InboundLedger::getPeerCount
std::size_t getPeerCount() const
Definition: InboundLedger.cpp:174
ripple::InboundLedger::addPeers
void addPeers()
Add more peers to the set, if possible.
Definition: InboundLedger.cpp:483
ripple::TimeoutCounter::failed_
bool failed_
Definition: TimeoutCounter.h:132
ripple::InboundLedger::InboundLedger
InboundLedger(Application &app, uint256 const &hash, std::uint32_t seq, Reason reason, clock_type &, std::unique_ptr< PeerSet > peerSet)
Definition: InboundLedger.cpp:77
ripple::InboundLedger::tryDB
void tryDB(NodeStore::Database &srcDB)
Definition: InboundLedger.cpp:307
std::enable_shared_from_this
ripple::InboundLedger::takeTxRootNode
bool takeTxRootNode(Slice const &data, SHAMapAddNode &)
Process AS root node received from a peer Call with a lock.
Definition: InboundLedger.cpp:1043
ripple::InboundLedger::m_clock
clock_type & m_clock
Definition: InboundLedger.h:175
std::uint32_t
ripple::InboundLedger::mReceiveDispatched
bool mReceiveDispatched
Definition: InboundLedger.h:196
ripple::InboundLedger::mHaveHeader
bool mHaveHeader
Definition: InboundLedger.h:179
beast::abstract_clock< std::chrono::steady_clock >
ripple::InboundLedger::isComplete
bool isComplete() const
Returns true if we got all the data.
Definition: InboundLedger.h:66
std::weak_ptr< Peer >
ripple::InboundLedger::TriggerReason::timeout
@ timeout
ripple::InboundLedger::TriggerReason::reply
@ reply
ripple::InboundLedger::pmDowncast
std::weak_ptr< TimeoutCounter > pmDowncast() override
Return a weak pointer to this.
Definition: InboundLedger.cpp:497
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::deserializeHeader
LedgerInfo deserializeHeader(Slice data, bool hasHash)
Deserialize a ledger header from a byte array.
Definition: InboundLedger.cpp:275
ripple::InboundLedger::mStats
SHAMapAddNode mStats
Definition: InboundLedger.h:189
ripple::InboundLedger::mSeq
std::uint32_t mSeq
Definition: InboundLedger.h:184
ripple::InboundLedger::takeAsRootNode
bool takeAsRootNode(Slice const &data, SHAMapAddNode &)
Process AS root node received from a peer Call with a lock.
Definition: InboundLedger.cpp:1018
ripple::InboundLedger::done
void done()
Definition: InboundLedger.cpp:503
ripple::InboundLedger::trigger
void trigger(std::shared_ptr< Peer > const &, TriggerReason)
Request more nodes, perhaps from a specific peer.
Definition: InboundLedger.cpp:557
mutex
std::size_t
ripple::LedgerInfo
Information about the notional ledger backing the view.
Definition: ReadView.h:75
ripple::InboundLedger::Reason
Reason
Definition: InboundLedger.h:43
ripple::TimeoutCounter::complete_
bool complete_
Definition: TimeoutCounter.h:131
ripple::InboundLedger::receiveNode
void receiveNode(protocol::TMLedgerData &packet, SHAMapAddNode &)
Process node data received from a peer Call with a lock.
Definition: InboundLedger.cpp:928
ripple::InboundLedger::runData
void runData()
Process pending TMLedgerData Query the a random sample of the 'best' peers.
Definition: InboundLedger.cpp:1312
ripple::SHAMapSyncFilter
Definition: SHAMapSyncFilter.h:30
std::unique_ptr
STL class.
ripple::InboundLedger::Reason::SHARD
@ SHARD
ripple::InboundLedger::onTimer
void onTimer(bool progress, ScopedLockType &peerSetLock) override
Called with a lock by the PeerSet when the timer expires.
Definition: InboundLedger.cpp:432
beast::abstract_clock< std::chrono::steady_clock >::time_point
typename std::chrono::steady_clock ::time_point time_point
Definition: abstract_clock.h:63
ripple::InboundLedger::mReceivedDataLock
std::mutex mReceivedDataLock
Definition: InboundLedger.h:192
ripple::InboundLedger::checkLocal
bool checkLocal()
Definition: InboundLedger.cpp:196
set
ripple::InboundLedger::mHaveTransactions
bool mHaveTransactions
Definition: InboundLedger.h:181
ripple::InboundLedger::mReceivedData
std::vector< std::pair< std::weak_ptr< Peer >, std::shared_ptr< protocol::TMLedgerData > > > mReceivedData
Definition: InboundLedger.h:195
ripple::InboundLedger::init
void init(ScopedLockType &collectionLock)
Definition: InboundLedger.cpp:106
Json::Value
Represents a JSON value.
Definition: json_value.h:145