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 
146  void
147  tryDB(NodeStore::Database& srcDB);
148 
149  void
150  done();
151 
152  void
153  onTimer(bool progress, ScopedLockType& peerSetLock) override;
154 
155  void
156  queueJob() override;
157 
158  void
159  onPeerAdded(std::shared_ptr<Peer> const& peer) override
160  {
161  // For historical nodes, do not trigger too soon
162  // since a fetch pack is probably coming
163  if (mReason != Reason::HISTORY)
165  }
166 
168  getPeerCount() const;
169 
171  pmDowncast() override;
172 
173  int
174  processData(std::shared_ptr<Peer> peer, protocol::TMLedgerData& data);
175 
176  bool
177  takeHeader(std::string const& data);
178 
179  void
180  receiveNode(protocol::TMLedgerData& packet, SHAMapAddNode&);
181 
182  bool
183  takeTxRootNode(Slice const& data, SHAMapAddNode&);
184 
185  bool
186  takeAsRootNode(Slice const& data, SHAMapAddNode&);
187 
189  neededTxHashes(int max, SHAMapSyncFilter* filter) const;
190 
192  neededStateHashes(int max, SHAMapSyncFilter* filter) const;
193 
196 
201  bool mSignaled;
202  bool mByHash;
205 
207 
209 
210  // Data we have received from peers
214 };
215 
219 
223 
224 } // namespace ripple
225 
226 #endif
ripple::InboundLedger::mRecentNodes
std::set< uint256 > mRecentNodes
Definition: InboundLedger.h:206
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:1039
ripple::InboundLedger::mReason
const Reason mReason
Definition: InboundLedger.h:204
ripple::InboundLedger::getJson
Json::Value getJson(int)
Return a Json::objectValue.
Definition: InboundLedger.cpp:1254
ripple::InboundLedger::TriggerReason
TriggerReason
Definition: InboundLedger.h:130
ripple::CountedObject
Tracks the number of instances of an object.
Definition: CountedObject.h:110
ripple::NodeStore::Database
Persistency layer for NodeObject.
Definition: Database.h:53
std::string
STL class.
ripple::InboundLedger::Reason::CONSENSUS
@ CONSENSUS
std::shared_ptr
STL class.
ripple::InboundLedger::mHaveState
bool mHaveState
Definition: InboundLedger.h:199
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:44
ripple::InboundLedger::mSignaled
bool mSignaled
Definition: InboundLedger.h:201
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:185
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:218
ripple::InboundLedger::mByHash
bool mByHash
Definition: InboundLedger.h:202
ripple::InboundLedger::onPeerAdded
void onPeerAdded(std::shared_ptr< Peer > const &peer) override
Hook called from addPeers().
Definition: InboundLedger.h:159
ripple::InboundLedger::filterNodes
void filterNodes(std::vector< std::pair< SHAMapNodeID, uint256 >> &nodes, TriggerReason reason)
Definition: InboundLedger.cpp:815
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:1106
ripple::PeerSet::mComplete
bool mComplete
Definition: PeerSet.h:114
ripple::InboundLedger::queueJob
void queueJob() override
Queue a job to call invokeOnTimer().
Definition: InboundLedger.cpp:169
ripple::InboundLedger::neededStateHashes
std::vector< uint256 > neededStateHashes(int max, SHAMapSyncFilter *filter) const
Definition: InboundLedger.cpp:255
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:271
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:292
ripple::InboundLedger::neededTxHashes
std::vector< uint256 > neededTxHashes(int max, SHAMapSyncFilter *filter) const
Definition: InboundLedger.cpp:239
ripple::base_uint< 256 >
ripple::InboundLedger::takeHeader
bool takeHeader(std::string const &data)
Take ledger header data Call with a lock.
Definition: InboundLedger.cpp:861
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:1079
ripple::InboundLedger::mLedger
std::shared_ptr< Ledger > mLedger
Definition: InboundLedger.h:197
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:195
ripple::InboundLedger::getLastAction
clock_type::time_point getLastAction() const
Definition: InboundLedger.h:124
ripple::InboundLedger::getPeerCount
std::size_t getPeerCount() const
Definition: InboundLedger.cpp:161
ripple::InboundLedger::addPeers
void addPeers()
Add more peers to the set, if possible.
Definition: InboundLedger.cpp:473
ripple::PeerSet::ScopedLockType
std::unique_lock< std::recursive_mutex > ScopedLockType
Definition: PeerSet.h:51
ripple::InboundLedger::tryDB
void tryDB(NodeStore::Database &srcDB)
Definition: InboundLedger.cpp:300
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:1017
ripple::InboundLedger::m_clock
clock_type & m_clock
Definition: InboundLedger.h:194
ripple::InboundLedger::pmDowncast
std::weak_ptr< PeerSet > pmDowncast() override
Return a weak pointer to this.
Definition: InboundLedger.cpp:481
std::uint32_t
ripple::InboundLedger::mReceiveDispatched
bool mReceiveDispatched
Definition: InboundLedger.h:213
ripple::InboundLedger::mHaveHeader
bool mHaveHeader
Definition: InboundLedger.h:198
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:208
ripple::InboundLedger::mSeq
std::uint32_t mSeq
Definition: InboundLedger.h:203
ripple::InboundLedger::takeAsRootNode
bool takeAsRootNode(Slice const &data, SHAMapAddNode &)
Process AS root node received from a peer Call with a lock.
Definition: InboundLedger.cpp:992
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:487
ripple::InboundLedger::trigger
void trigger(std::shared_ptr< Peer > const &, TriggerReason)
Request more nodes, perhaps from a specific peer.
Definition: InboundLedger.cpp:538
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::receiveNode
void receiveNode(protocol::TMLedgerData &packet, SHAMapAddNode &)
Process node data received from a peer Call with a lock.
Definition: InboundLedger.cpp:909
ripple::InboundLedger::runData
void runData()
Process pending TMLedgerData Query the 'best' peer.
Definition: InboundLedger.cpp:1211
ripple::InboundLedger::mReceivedData
std::vector< PeerDataPairType > mReceivedData
Definition: InboundLedger.h:212
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:422
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:211
ripple::InboundLedger::checkLocal
bool checkLocal()
Definition: InboundLedger.cpp:198
set
ripple::InboundLedger::mHaveTransactions
bool mHaveTransactions
Definition: InboundLedger.h:200
ripple::InboundLedger::init
void init(ScopedLockType &collectionLock)
Definition: InboundLedger.cpp:96
Json::Value
Represents a JSON value.
Definition: json_value.h:145