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  using PeerDataPairType =
43 
44  // These are the reasons we might acquire a ledger
45  enum class Reason {
46  HISTORY, // Acquiring past ledger
47  SHARD, // Acquiring for shard
48  GENERIC, // Generic other reasons
49  CONSENSUS // We believe the consensus round requires this ledger
50  };
51 
53  Application& app,
54  uint256 const& hash,
55  std::uint32_t seq,
56  Reason reason,
57  clock_type&);
58 
60 
61  // Called when another attempt is made to fetch this same ledger
62  void
63  update(std::uint32_t seq);
64 
66  bool
67  isComplete() const
68  {
69  return mComplete;
70  }
71 
73  bool
74  isFailed() const
75  {
76  return mFailed;
77  }
78 
80  getLedger() const
81  {
82  return mLedger;
83  }
84 
86  getSeq() const
87  {
88  return mSeq;
89  }
90 
91  bool
92  checkLocal();
93  void
94  init(ScopedLockType& collectionLock);
95 
96  bool
97  gotData(
100 
101  using neededHash_t =
103 
106  getJson(int);
107 
108  void
109  runData();
110 
111  void
113  {
114  mLastAction = m_clock.now();
115  }
116 
119  {
120  return mLastAction;
121  }
122 
123 private:
124  enum class TriggerReason { added, reply, timeout };
125 
126  void
127  filterNodes(
129  TriggerReason reason);
130 
131  void
133 
135  getNeededHashes();
136 
137  void
138  addPeers();
139 
140  void
141  tryDB(NodeStore::Database& srcDB);
142 
143  void
144  done();
145 
146  void
147  onTimer(bool progress, ScopedLockType& peerSetLock) override;
148 
149  void
150  queueJob() override;
151 
152  void
153  onPeerAdded(std::shared_ptr<Peer> const& peer) override
154  {
155  // For historical nodes, do not trigger too soon
156  // since a fetch pack is probably coming
157  if (mReason != Reason::HISTORY)
159  }
160 
162  getPeerCount() const;
163 
165  pmDowncast() override;
166 
167  int
168  processData(std::shared_ptr<Peer> peer, protocol::TMLedgerData& data);
169 
170  bool
171  takeHeader(std::string const& data);
172 
173  void
174  receiveNode(protocol::TMLedgerData& packet, SHAMapAddNode&);
175 
176  bool
177  takeTxRootNode(Slice const& data, SHAMapAddNode&);
178 
179  bool
180  takeAsRootNode(Slice const& data, SHAMapAddNode&);
181 
183  neededTxHashes(int max, SHAMapSyncFilter* filter) const;
184 
186  neededStateHashes(int max, SHAMapSyncFilter* filter) const;
187 
190 
195  bool mSignaled;
196  bool mByHash;
199 
201 
203 
204  // Data we have received from peers
208 };
209 
213 
217 
218 } // namespace ripple
219 
220 #endif
ripple::InboundLedger::mRecentNodes
std::set< uint256 > mRecentNodes
Definition: InboundLedger.h:200
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:1051
ripple::InboundLedger::mReason
const Reason mReason
Definition: InboundLedger.h:198
ripple::InboundLedger::getJson
Json::Value getJson(int)
Return a Json::objectValue.
Definition: InboundLedger.cpp:1266
ripple::InboundLedger::TriggerReason
TriggerReason
Definition: InboundLedger.h:124
ripple::CountedObject
Tracks the number of instances of an object.
Definition: CountedObject.h:124
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:193
ripple::InboundLedger
Definition: InboundLedger.h:34
utility
ripple::InboundLedger::TriggerReason::added
@ added
ripple::InboundLedger::getSeq
std::uint32_t getSeq() const
Definition: InboundLedger.h:86
ripple::Slice
An immutable linear range of bytes.
Definition: Slice.h:44
ripple::InboundLedger::mSignaled
bool mSignaled
Definition: InboundLedger.h:195
std::pair
ripple::InboundLedger::Reason::GENERIC
@ GENERIC
std::vector
STL class.
ripple::InboundLedger::getLedger
std::shared_ptr< Ledger const > getLedger() const
Definition: InboundLedger.h:80
ripple::InboundLedger::update
void update(std::uint32_t seq)
Definition: InboundLedger.cpp:185
ripple::InboundLedger::touch
void touch()
Definition: InboundLedger.h:112
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:196
ripple::InboundLedger::onPeerAdded
void onPeerAdded(std::shared_ptr< Peer > const &peer) override
Hook called from addPeers().
Definition: InboundLedger.h:153
ripple::InboundLedger::filterNodes
void filterNodes(std::vector< std::pair< SHAMapNodeID, uint256 >> &nodes, TriggerReason reason)
Definition: InboundLedger.cpp:821
ripple::InboundLedger::isFailed
bool isFailed() const
Returns false if we failed to get the data.
Definition: InboundLedger.h:74
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:1118
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:270
ripple::InboundLedger::neededHash_t
std::pair< protocol::TMGetObjectByHash::ObjectType, uint256 > neededHash_t
Definition: InboundLedger.h:102
ripple::deserializeHeader
LedgerInfo deserializeHeader(Slice data)
Deserialize a ledger header from a byte array.
Definition: InboundLedger.cpp:277
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:298
ripple::InboundLedger::neededTxHashes
std::vector< uint256 > neededTxHashes(int max, SHAMapSyncFilter *filter) const
Definition: InboundLedger.cpp:264
ripple::base_uint
Integers of any length that is a multiple of 32-bits.
Definition: base_uint.h:73
ripple::InboundLedger::takeHeader
bool takeHeader(std::string const &data)
Take ledger header data Call with a lock.
Definition: InboundLedger.cpp:867
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:1091
ripple::InboundLedger::mLedger
std::shared_ptr< Ledger > mLedger
Definition: InboundLedger.h:191
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:189
ripple::InboundLedger::getLastAction
clock_type::time_point getLastAction() const
Definition: InboundLedger.h:118
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:479
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:306
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:1029
ripple::InboundLedger::m_clock
clock_type & m_clock
Definition: InboundLedger.h:188
ripple::InboundLedger::pmDowncast
std::weak_ptr< PeerSet > pmDowncast() override
Return a weak pointer to this.
Definition: InboundLedger.cpp:487
std::uint32_t
ripple::InboundLedger::mReceiveDispatched
bool mReceiveDispatched
Definition: InboundLedger.h:207
ripple::InboundLedger::mHaveHeader
bool mHaveHeader
Definition: InboundLedger.h:192
beast::abstract_clock< std::chrono::steady_clock >
ripple::InboundLedger::isComplete
bool isComplete() const
Returns true if we got all the data.
Definition: InboundLedger.h:67
std::weak_ptr< Peer >
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:202
ripple::InboundLedger::mSeq
std::uint32_t mSeq
Definition: InboundLedger.h:197
ripple::InboundLedger::takeAsRootNode
bool takeAsRootNode(Slice const &data, SHAMapAddNode &)
Process AS root node received from a peer Call with a lock.
Definition: InboundLedger.cpp:1004
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:493
ripple::InboundLedger::trigger
void trigger(std::shared_ptr< Peer > const &, TriggerReason)
Request more nodes, perhaps from a specific peer.
Definition: InboundLedger.cpp:544
mutex
std::size_t
ripple::LedgerInfo
Information about the notional ledger backing the view.
Definition: ReadView.h:84
ripple::InboundLedger::Reason
Reason
Definition: InboundLedger.h:45
ripple::InboundLedger::receiveNode
void receiveNode(protocol::TMLedgerData &packet, SHAMapAddNode &)
Process node data received from a peer Call with a lock.
Definition: InboundLedger.cpp:915
ripple::InboundLedger::runData
void runData()
Process pending TMLedgerData Query the 'best' peer.
Definition: InboundLedger.cpp:1223
ripple::InboundLedger::mReceivedData
std::vector< PeerDataPairType > mReceivedData
Definition: InboundLedger.h:206
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:428
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:205
ripple::InboundLedger::checkLocal
bool checkLocal()
Definition: InboundLedger.cpp:198
set
ripple::InboundLedger::mHaveTransactions
bool mHaveTransactions
Definition: InboundLedger.h:194
ripple::InboundLedger::init
void init(ScopedLockType &collectionLock)
Definition: InboundLedger.cpp:96
Json::Value
Represents a JSON value.
Definition: json_value.h:145