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  using PeerDataPairType =
44 
45  // These are the reasons we might acquire a ledger
46  enum class Reason {
47  HISTORY, // Acquiring past ledger
48  SHARD, // Acquiring for shard
49  GENERIC, // Generic other reasons
50  CONSENSUS // We believe the consensus round requires this ledger
51  };
52 
54  Application& app,
55  uint256 const& hash,
56  std::uint32_t seq,
57  Reason reason,
58  clock_type&,
59  std::unique_ptr<PeerSet> peerSet);
60 
62 
63  // Called when another attempt is made to fetch this same ledger
64  void
65  update(std::uint32_t seq);
66 
68  bool
69  isComplete() const
70  {
71  return complete_;
72  }
73 
75  bool
76  isFailed() const
77  {
78  return failed_;
79  }
80 
82  getLedger() const
83  {
84  return mLedger;
85  }
86 
88  getSeq() const
89  {
90  return mSeq;
91  }
92 
93  bool
94  checkLocal();
95  void
96  init(ScopedLockType& collectionLock);
97 
98  bool
99  gotData(
102 
103  using neededHash_t =
105 
108  getJson(int);
109 
110  void
111  runData();
112 
113  void
115  {
116  mLastAction = m_clock.now();
117  }
118 
121  {
122  return mLastAction;
123  }
124 
125 private:
126  enum class TriggerReason { added, reply, timeout };
127 
128  void
129  filterNodes(
131  TriggerReason reason);
132 
133  void
135 
137  getNeededHashes();
138 
139  void
140  addPeers();
141 
142  void
143  tryDB(NodeStore::Database& srcDB);
144 
145  void
146  done();
147 
148  void
149  onTimer(bool progress, ScopedLockType& peerSetLock) override;
150 
152  getPeerCount() const;
153 
155  pmDowncast() override;
156 
157  int
158  processData(std::shared_ptr<Peer> peer, protocol::TMLedgerData& data);
159 
160  bool
161  takeHeader(std::string const& data);
162 
163  void
164  receiveNode(protocol::TMLedgerData& packet, SHAMapAddNode&);
165 
166  bool
167  takeTxRootNode(Slice const& data, SHAMapAddNode&);
168 
169  bool
170  takeAsRootNode(Slice const& data, SHAMapAddNode&);
171 
173  neededTxHashes(int max, SHAMapSyncFilter* filter) const;
174 
176  neededStateHashes(int max, SHAMapSyncFilter* filter) const;
177 
180 
185  bool mSignaled;
186  bool mByHash;
189 
191 
193 
194  // Data we have received from peers
199 };
200 
203 deserializeHeader(Slice data, bool hasHash = false);
204 
207 deserializePrefixedHeader(Slice data, bool hasHash = false);
208 
209 } // namespace ripple
210 
211 #endif
ripple::InboundLedger::mRecentNodes
std::set< uint256 > mRecentNodes
Definition: InboundLedger.h:190
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:1052
ripple::InboundLedger::mReason
const Reason mReason
Definition: InboundLedger.h:188
ripple::InboundLedger::getJson
Json::Value getJson(int)
Return a Json::objectValue.
Definition: InboundLedger.cpp:1267
ripple::TimeoutCounter::ScopedLockType
std::unique_lock< std::recursive_mutex > ScopedLockType
Definition: TimeoutCounter.h:81
ripple::InboundLedger::TriggerReason
TriggerReason
Definition: InboundLedger.h:126
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:183
ripple::InboundLedger
Definition: InboundLedger.h:35
utility
ripple::InboundLedger::TriggerReason::added
@ added
ripple::InboundLedger::getSeq
std::uint32_t getSeq() const
Definition: InboundLedger.h:88
ripple::Slice
An immutable linear range of bytes.
Definition: Slice.h:44
ripple::InboundLedger::mSignaled
bool mSignaled
Definition: InboundLedger.h:185
std::pair
ripple::InboundLedger::Reason::GENERIC
@ GENERIC
std::vector
STL class.
ripple::InboundLedger::getLedger
std::shared_ptr< Ledger const > getLedger() const
Definition: InboundLedger.h:82
ripple::InboundLedger::update
void update(std::uint32_t seq)
Definition: InboundLedger.cpp:177
ripple::InboundLedger::touch
void touch()
Definition: InboundLedger.h:114
ripple::InboundLedger::~InboundLedger
~InboundLedger()
Definition: InboundLedger.cpp:210
ripple::InboundLedger::mByHash
bool mByHash
Definition: InboundLedger.h:186
ripple::InboundLedger::filterNodes
void filterNodes(std::vector< std::pair< SHAMapNodeID, uint256 >> &nodes, TriggerReason reason)
Definition: InboundLedger.cpp:822
ripple::InboundLedger::isFailed
bool isFailed() const
Returns false if we failed to get the data.
Definition: InboundLedger.h:76
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:1119
ripple::InboundLedger::neededStateHashes
std::vector< uint256 > neededStateHashes(int max, SHAMapSyncFilter *filter) const
Definition: InboundLedger.cpp:262
ripple::InboundLedger::neededHash_t
std::pair< protocol::TMGetObjectByHash::ObjectType, uint256 > neededHash_t
Definition: InboundLedger.h:104
ripple::InboundLedger::mPeerSet
std::unique_ptr< PeerSet > mPeerSet
Definition: InboundLedger.h:198
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:293
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:256
ripple::base_uint
Integers of any length that is a multiple of 32-bits.
Definition: base_uint.h:75
ripple::InboundLedger::takeHeader
bool takeHeader(std::string const &data)
Take ledger header data Call with a lock.
Definition: InboundLedger.cpp:868
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:1092
ripple::InboundLedger::mLedger
std::shared_ptr< Ledger > mLedger
Definition: InboundLedger.h:181
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:179
ripple::InboundLedger::getLastAction
clock_type::time_point getLastAction() const
Definition: InboundLedger.h:120
ripple::InboundLedger::getPeerCount
std::size_t getPeerCount() const
Definition: InboundLedger.cpp:168
ripple::InboundLedger::addPeers
void addPeers()
Add more peers to the set, if possible.
Definition: InboundLedger.cpp:474
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:74
ripple::InboundLedger::tryDB
void tryDB(NodeStore::Database &srcDB)
Definition: InboundLedger.cpp:301
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:1030
ripple::InboundLedger::m_clock
clock_type & m_clock
Definition: InboundLedger.h:178
std::uint32_t
ripple::InboundLedger::mReceiveDispatched
bool mReceiveDispatched
Definition: InboundLedger.h:197
ripple::InboundLedger::mHaveHeader
bool mHaveHeader
Definition: InboundLedger.h:182
beast::abstract_clock< std::chrono::steady_clock >
ripple::InboundLedger::isComplete
bool isComplete() const
Returns true if we got all the data.
Definition: InboundLedger.h:69
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:488
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:269
ripple::InboundLedger::mStats
SHAMapAddNode mStats
Definition: InboundLedger.h:192
ripple::InboundLedger::mSeq
std::uint32_t mSeq
Definition: InboundLedger.h:187
ripple::InboundLedger::takeAsRootNode
bool takeAsRootNode(Slice const &data, SHAMapAddNode &)
Process AS root node received from a peer Call with a lock.
Definition: InboundLedger.cpp:1005
ripple::InboundLedger::done
void done()
Definition: InboundLedger.cpp:494
ripple::InboundLedger::trigger
void trigger(std::shared_ptr< Peer > const &, TriggerReason)
Request more nodes, perhaps from a specific peer.
Definition: InboundLedger.cpp:545
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:46
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:916
ripple::InboundLedger::runData
void runData()
Process pending TMLedgerData Query the 'best' peer.
Definition: InboundLedger.cpp:1224
ripple::InboundLedger::mReceivedData
std::vector< PeerDataPairType > mReceivedData
Definition: InboundLedger.h:196
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:423
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:195
ripple::InboundLedger::checkLocal
bool checkLocal()
Definition: InboundLedger.cpp:190
set
ripple::InboundLedger::mHaveTransactions
bool mHaveTransactions
Definition: InboundLedger.h:184
ripple::InboundLedger::init
void init(ScopedLockType &collectionLock)
Definition: InboundLedger.cpp:103
Json::Value
Represents a JSON value.
Definition: json_value.h:145