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/main/Application.h>
25 #include <ripple/basics/CountedObject.h>
26 #include <ripple/overlay/PeerSet.h>
27 #include <mutex>
28 #include <set>
29 #include <utility>
30 
31 namespace ripple {
32 
33 // A ledger we are trying to acquire
34 class InboundLedger final : public PeerSet,
35  public std::enable_shared_from_this<InboundLedger>,
36  public CountedObject<InboundLedger>
37 {
38 public:
40 
41  static char const*
43  {
44  return "InboundLedger";
45  }
46 
47  using PeerDataPairType =
49 
50  // These are the reasons we might acquire a ledger
51  enum class Reason {
52  HISTORY, // Acquiring past ledger
53  SHARD, // Acquiring for shard
54  GENERIC, // Generic other reasons
55  CONSENSUS // We believe the consensus round requires this ledger
56  };
57 
59  Application& app,
60  uint256 const& hash,
61  std::uint32_t seq,
62  Reason reason,
63  clock_type&);
64 
66 
67  // Called when another attempt is made to fetch this same ledger
68  void
69  update(std::uint32_t seq);
70 
72  bool
73  isComplete() const
74  {
75  return mComplete;
76  }
77 
79  bool
80  isFailed() const
81  {
82  return mFailed;
83  }
84 
86  getLedger() const
87  {
88  return mLedger;
89  }
90 
92  getSeq() const
93  {
94  return mSeq;
95  }
96 
97  bool
98  checkLocal();
99  void
100  init(ScopedLockType& collectionLock);
101 
102  bool
103  gotData(
106 
107  using neededHash_t =
109 
112  getJson(int);
113 
114  void
115  runData();
116 
117  void
119  {
120  mLastAction = m_clock.now();
121  }
122 
125  {
126  return mLastAction;
127  }
128 
129 private:
130  enum class TriggerReason { added, reply, timeout };
131 
132  void
133  filterNodes(
135  TriggerReason reason);
136 
137  void
139 
141  getNeededHashes();
142 
143  void
144  addPeers();
145  void
146  tryDB(Family& f);
147 
148  void
149  done();
150 
151  void
152  onTimer(bool progress, ScopedLockType& peerSetLock) override;
153 
154  void
155  queueJob() override;
156 
157  void
158  onPeerAdded(std::shared_ptr<Peer> const& peer) override
159  {
160  // For historical nodes, do not trigger too soon
161  // since a fetch pack is probably coming
162  if (mReason != Reason::HISTORY)
164  }
165 
167  getPeerCount() const;
168 
170  pmDowncast() override;
171 
172  int
173  processData(std::shared_ptr<Peer> peer, protocol::TMLedgerData& data);
174 
175  bool
176  takeHeader(std::string const& data);
177  bool
178  takeTxNode(
179  const std::vector<SHAMapNodeID>& IDs,
180  const std::vector<Blob>& data,
181  SHAMapAddNode&);
182  bool
183  takeTxRootNode(Slice const& data, SHAMapAddNode&);
184 
185  // VFALCO TODO Rename to receiveAccountStateNode
186  // Don't use acronyms, but if we are going to use them at least
187  // capitalize them correctly.
188  //
189  bool
190  takeAsNode(
191  const std::vector<SHAMapNodeID>& IDs,
192  const std::vector<Blob>& data,
193  SHAMapAddNode&);
194  bool
195  takeAsRootNode(Slice const& data, SHAMapAddNode&);
196 
198  neededTxHashes(int max, SHAMapSyncFilter* filter) const;
199 
201  neededStateHashes(int max, SHAMapSyncFilter* filter) const;
202 
205 
210  bool mSignaled;
211  bool mByHash;
214 
216 
218 
219  // Data we have received from peers
223 };
224 
228 
232 
233 } // namespace ripple
234 
235 #endif
ripple::InboundLedger::mRecentNodes
std::set< uint256 > mRecentNodes
Definition: InboundLedger.h:215
ripple::Application
Definition: Application.h:97
ripple::SHAMapAddNode
Definition: SHAMapAddNode.h:28
ripple::InboundLedger::Reason::HISTORY
@ HISTORY
ripple::InboundLedger::getNeededHashes
std::vector< neededHash_t > getNeededHashes()
Definition: InboundLedger.cpp:1075
ripple::InboundLedger::mReason
const Reason mReason
Definition: InboundLedger.h:213
ripple::InboundLedger::getJson
Json::Value getJson(int)
Return a Json::objectValue.
Definition: InboundLedger.cpp:1292
ripple::InboundLedger::TriggerReason
TriggerReason
Definition: InboundLedger.h:130
ripple::CountedObject
Tracks the number of instances of an object.
Definition: CountedObject.h:110
std::string
STL class.
ripple::InboundLedger::Reason::CONSENSUS
@ CONSENSUS
std::shared_ptr
STL class.
ripple::InboundLedger::mHaveState
bool mHaveState
Definition: InboundLedger.h:208
ripple::InboundLedger
Definition: InboundLedger.h:34
utility
ripple::InboundLedger::TriggerReason::added
@ added
ripple::InboundLedger::getSeq
std::uint32_t getSeq() const
Definition: InboundLedger.h:92
ripple::Slice
An immutable linear range of bytes.
Definition: Slice.h:43
ripple::InboundLedger::mSignaled
bool mSignaled
Definition: InboundLedger.h:210
std::pair
ripple::InboundLedger::Reason::GENERIC
@ GENERIC
std::vector
STL class.
ripple::InboundLedger::getLedger
std::shared_ptr< Ledger const > getLedger() const
Definition: InboundLedger.h:86
ripple::InboundLedger::update
void update(std::uint32_t seq)
Definition: InboundLedger.cpp:181
ripple::InboundLedger::touch
void touch()
Definition: InboundLedger.h:118
ripple::InboundLedger::InboundLedger
InboundLedger(Application &app, uint256 const &hash, std::uint32_t seq, Reason reason, clock_type &)
Definition: InboundLedger.cpp:74
ripple::InboundLedger::~InboundLedger
~InboundLedger()
Definition: InboundLedger.cpp:214
ripple::InboundLedger::mByHash
bool mByHash
Definition: InboundLedger.h:211
ripple::InboundLedger::onPeerAdded
void onPeerAdded(std::shared_ptr< Peer > const &peer) override
Hook called from addPeers().
Definition: InboundLedger.h:158
ripple::InboundLedger::filterNodes
void filterNodes(std::vector< std::pair< SHAMapNodeID, uint256 >> &nodes, TriggerReason reason)
Definition: InboundLedger.cpp:790
ripple::InboundLedger::isFailed
bool isFailed() const
Returns false if we failed to get the data.
Definition: InboundLedger.h:80
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:1142
ripple::PeerSet::mComplete
bool mComplete
Definition: PeerSet.h:114
ripple::InboundLedger::queueJob
void queueJob() override
Queue a job to call invokeOnTimer().
Definition: InboundLedger.cpp:165
ripple::InboundLedger::neededStateHashes
std::vector< uint256 > neededStateHashes(int max, SHAMapSyncFilter *filter) const
Definition: InboundLedger.cpp:251
ripple::InboundLedger::neededHash_t
std::pair< protocol::TMGetObjectByHash::ObjectType, uint256 > neededHash_t
Definition: InboundLedger.h:108
ripple::deserializeHeader
LedgerInfo deserializeHeader(Slice data)
Deserialize a ledger header from a byte array.
Definition: InboundLedger.cpp:267
beast::abstract_clock::now
virtual time_point now() const =0
Returns the current time.
ripple::deserializePrefixedHeader
LedgerInfo deserializePrefixedHeader(Slice data)
Deserialize a ledger header (prefixed with 4 bytes) from a byte array.
Definition: InboundLedger.cpp:288
ripple::InboundLedger::neededTxHashes
std::vector< uint256 > neededTxHashes(int max, SHAMapSyncFilter *filter) const
Definition: InboundLedger.cpp:235
ripple::base_uint< 256 >
ripple::InboundLedger::takeHeader
bool takeHeader(std::string const &data)
Take ledger header data Call with a lock.
Definition: InboundLedger.cpp:836
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:1115
ripple::InboundLedger::takeTxNode
bool takeTxNode(const std::vector< SHAMapNodeID > &IDs, const std::vector< Blob > &data, SHAMapAddNode &)
Process TX data received from a peer Call with a lock.
Definition: InboundLedger.cpp:883
ripple::InboundLedger::mLedger
std::shared_ptr< Ledger > mLedger
Definition: InboundLedger.h:206
ripple::InboundLedger::clock_type
beast::abstract_clock< std::chrono::steady_clock > clock_type
Definition: InboundLedger.h:39
ripple::InboundLedger::mLastAction
clock_type::time_point mLastAction
Definition: InboundLedger.h:204
ripple::InboundLedger::getLastAction
clock_type::time_point getLastAction() const
Definition: InboundLedger.h:124
ripple::InboundLedger::getPeerCount
std::size_t getPeerCount() const
Definition: InboundLedger.cpp:157
ripple::InboundLedger::addPeers
void addPeers()
Add more peers to the set, if possible.
Definition: InboundLedger.cpp:450
ripple::PeerSet::ScopedLockType
std::unique_lock< std::recursive_mutex > ScopedLockType
Definition: PeerSet.h:51
ripple::InboundLedger::PeerDataPairType
std::pair< std::weak_ptr< Peer >, std::shared_ptr< protocol::TMLedgerData > > PeerDataPairType
Definition: InboundLedger.h:48
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:1053
ripple::InboundLedger::m_clock
clock_type & m_clock
Definition: InboundLedger.h:203
ripple::Family
Definition: Family.h:32
ripple::InboundLedger::pmDowncast
std::weak_ptr< PeerSet > pmDowncast() override
Return a weak pointer to this.
Definition: InboundLedger.cpp:458
std::uint32_t
ripple::InboundLedger::mReceiveDispatched
bool mReceiveDispatched
Definition: InboundLedger.h:222
ripple::InboundLedger::mHaveHeader
bool mHaveHeader
Definition: InboundLedger.h:207
beast::abstract_clock< std::chrono::steady_clock >
ripple::InboundLedger::getCountedObjectName
static char const * getCountedObjectName()
Definition: InboundLedger.h:42
ripple::InboundLedger::isComplete
bool isComplete() const
Returns true if we got all the data.
Definition: InboundLedger.h:73
std::weak_ptr
STL class.
ripple::InboundLedger::TriggerReason::timeout
@ timeout
ripple::InboundLedger::TriggerReason::reply
@ reply
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::InboundLedger::mStats
SHAMapAddNode mStats
Definition: InboundLedger.h:217
ripple::InboundLedger::mSeq
std::uint32_t mSeq
Definition: InboundLedger.h:212
ripple::InboundLedger::takeAsRootNode
bool takeAsRootNode(Slice const &data, SHAMapAddNode &)
Process AS root node received from a peer Call with a lock.
Definition: InboundLedger.cpp:1028
ripple::PeerSet
Supports data retrieval by managing a set of peers.
Definition: PeerSet.h:48
ripple::PeerSet::mFailed
bool mFailed
Definition: PeerSet.h:115
ripple::InboundLedger::done
void done()
Definition: InboundLedger.cpp:464
ripple::InboundLedger::takeAsNode
bool takeAsNode(const std::vector< SHAMapNodeID > &IDs, const std::vector< Blob > &data, SHAMapAddNode &)
Process AS data received from a peer Call with a lock.
Definition: InboundLedger.cpp:948
ripple::InboundLedger::trigger
void trigger(std::shared_ptr< Peer > const &, TriggerReason)
Request more nodes, perhaps from a specific peer.
Definition: InboundLedger.cpp:515
mutex
std::size_t
ripple::LedgerInfo
Information about the notional ledger backing the view.
Definition: ReadView.h:80
ripple::InboundLedger::Reason
Reason
Definition: InboundLedger.h:51
ripple::InboundLedger::runData
void runData()
Process pending TMLedgerData Query the 'best' peer.
Definition: InboundLedger.cpp:1249
ripple::InboundLedger::mReceivedData
std::vector< PeerDataPairType > mReceivedData
Definition: InboundLedger.h:221
ripple::SHAMapSyncFilter
Definition: SHAMapSyncFilter.h:30
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:399
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:220
ripple::InboundLedger::checkLocal
bool checkLocal()
Definition: InboundLedger.cpp:194
set
ripple::InboundLedger::mHaveTransactions
bool mHaveTransactions
Definition: InboundLedger.h:209
ripple::InboundLedger::init
void init(ScopedLockType &collectionLock)
Definition: InboundLedger.cpp:96
ripple::InboundLedger::tryDB
void tryDB(Family &f)
Definition: InboundLedger.cpp:296
Json::Value
Represents a JSON value.
Definition: json_value.h:145