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 : public PeerSet,
35  public std::enable_shared_from_this<InboundLedger>,
36  public CountedObject<InboundLedger>
37 {
38 public:
39  static char const*
41  {
42  return "InboundLedger";
43  }
44 
45  using PeerDataPairType =
47 
48  // These are the reasons we might acquire a ledger
49  enum class Reason {
50  HISTORY, // Acquiring past ledger
51  SHARD, // Acquiring for shard
52  GENERIC, // Generic other reasons
53  CONSENSUS // We believe the consensus round requires this ledger
54  };
55 
58  uint256 const& hash,
59  std::uint32_t seq,
60  Reason reason,
61  clock_type&);
62 
64 
65  // Called when the PeerSet timer expires
66  void
67  execute() override;
68 
69  // Called when another attempt is made to fetch this same ledger
70  void
71  update(std::uint32_t seq);
72 
74  getLedger() const
75  {
76  return mLedger;
77  }
78 
80  getSeq() const
81  {
82  return mSeq;
83  }
84 
85  Reason
86  getReason() const
87  {
88  return mReason;
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  static LedgerInfo
112  deserializeHeader(Slice data, bool hasPrefix);
113 
114 private:
115  enum class TriggerReason { added, reply, timeout };
116 
117  void
118  filterNodes(
120  TriggerReason reason);
121 
122  void
124 
126  getNeededHashes();
127 
128  void
129  addPeers();
130  void
131  tryDB(Family& f);
132 
133  void
134  done();
135 
136  void
137  onTimer(bool progress, ScopedLockType& peerSetLock) override;
138 
139  void
140  newPeer(std::shared_ptr<Peer> const& peer) override
141  {
142  // For historical nodes, do not trigger too soon
143  // since a fetch pack is probably coming
144  if (mReason != Reason::HISTORY)
146  }
147 
149  pmDowncast() override;
150 
151  int
152  processData(std::shared_ptr<Peer> peer, protocol::TMLedgerData& data);
153 
154  bool
155  takeHeader(std::string const& data);
156  bool
157  takeTxNode(
158  const std::vector<SHAMapNodeID>& IDs,
159  const std::vector<Blob>& data,
160  SHAMapAddNode&);
161  bool
162  takeTxRootNode(Slice const& data, SHAMapAddNode&);
163 
164  // VFALCO TODO Rename to receiveAccountStateNode
165  // Don't use acronyms, but if we are going to use them at least
166  // capitalize them correctly.
167  //
168  bool
169  takeAsNode(
170  const std::vector<SHAMapNodeID>& IDs,
171  const std::vector<Blob>& data,
172  SHAMapAddNode&);
173  bool
174  takeAsRootNode(Slice const& data, SHAMapAddNode&);
175 
177  neededTxHashes(int max, SHAMapSyncFilter* filter) const;
178 
180  neededStateHashes(int max, SHAMapSyncFilter* filter) const;
181 
186  bool mSignaled;
187  bool mByHash;
190 
192 
194 
195  // Data we have received from peers
199 };
200 
201 } // namespace ripple
202 
203 #endif
ripple::InboundLedger::mRecentNodes
std::set< uint256 > mRecentNodes
Definition: InboundLedger.h:191
ripple::Application
Definition: Application.h:94
ripple::SHAMapAddNode
Definition: SHAMapAddNode.h:28
ripple::InboundLedger::Reason::HISTORY
@ HISTORY
ripple::InboundLedger::getNeededHashes
std::vector< neededHash_t > getNeededHashes()
Definition: InboundLedger.cpp:1066
ripple::InboundLedger::mReason
const Reason mReason
Definition: InboundLedger.h:189
ripple::InboundLedger::getJson
Json::Value getJson(int)
Return a Json::objectValue.
Definition: InboundLedger.cpp:1283
ripple::InboundLedger::TriggerReason
TriggerReason
Definition: InboundLedger.h:115
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:184
ripple::InboundLedger
Definition: InboundLedger.h:34
utility
ripple::InboundLedger::TriggerReason::added
@ added
ripple::InboundLedger::getSeq
std::uint32_t getSeq() const
Definition: InboundLedger.h:80
ripple::Slice
An immutable linear range of bytes.
Definition: Slice.h:43
ripple::InboundLedger::mSignaled
bool mSignaled
Definition: InboundLedger.h:186
std::pair
ripple::InboundLedger::Reason::GENERIC
@ GENERIC
std::vector
STL class.
ripple::InboundLedger::getLedger
std::shared_ptr< Ledger const > getLedger() const
Definition: InboundLedger.h:74
ripple::InboundLedger::update
void update(std::uint32_t seq)
Definition: InboundLedger.cpp:175
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:208
ripple::InboundLedger::newPeer
void newPeer(std::shared_ptr< Peer > const &peer) override
Definition: InboundLedger.h:140
ripple::InboundLedger::mByHash
bool mByHash
Definition: InboundLedger.h:187
ripple::InboundLedger::filterNodes
void filterNodes(std::vector< std::pair< SHAMapNodeID, uint256 >> &nodes, TriggerReason reason)
Definition: InboundLedger.cpp:781
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:1133
ripple::InboundLedger::execute
void execute() override
Definition: InboundLedger.cpp:160
ripple::InboundLedger::neededStateHashes
std::vector< uint256 > neededStateHashes(int max, SHAMapSyncFilter *filter) const
Definition: InboundLedger.cpp:245
ripple::InboundLedger::neededHash_t
std::pair< protocol::TMGetObjectByHash::ObjectType, uint256 > neededHash_t
Definition: InboundLedger.h:102
ripple::InboundLedger::neededTxHashes
std::vector< uint256 > neededTxHashes(int max, SHAMapSyncFilter *filter) const
Definition: InboundLedger.cpp:229
ripple::base_uint< 256 >
ripple::InboundLedger::takeHeader
bool takeHeader(std::string const &data)
Take ledger header data Call with a lock.
Definition: InboundLedger.cpp:827
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:1106
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:874
ripple::InboundLedger::mLedger
std::shared_ptr< Ledger > mLedger
Definition: InboundLedger.h:182
ripple::InboundLedger::deserializeHeader
static LedgerInfo deserializeHeader(Slice data, bool hasPrefix)
Definition: InboundLedger.cpp:261
ripple::PeerSet::clock_type
beast::abstract_clock< std::chrono::steady_clock > clock_type
Definition: PeerSet.h:51
ripple::InboundLedger::addPeers
void addPeers()
Add more peers to the set, if possible.
Definition: InboundLedger.cpp:441
ripple::PeerSet::ScopedLockType
std::unique_lock< std::recursive_mutex > ScopedLockType
Definition: PeerSet.h:123
ripple::InboundLedger::PeerDataPairType
std::pair< std::weak_ptr< Peer >, std::shared_ptr< protocol::TMLedgerData > > PeerDataPairType
Definition: InboundLedger.h:46
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:1044
ripple::Family
Definition: Family.h:32
ripple::InboundLedger::pmDowncast
std::weak_ptr< PeerSet > pmDowncast() override
Definition: InboundLedger.cpp:450
std::uint32_t
ripple::PeerSet::app
Application & app()
Definition: PeerSet.h:117
ripple::InboundLedger::mReceiveDispatched
bool mReceiveDispatched
Definition: InboundLedger.h:198
ripple::InboundLedger::mHaveHeader
bool mHaveHeader
Definition: InboundLedger.h:183
ripple::InboundLedger::getCountedObjectName
static char const * getCountedObjectName()
Definition: InboundLedger.h:40
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:193
ripple::InboundLedger::mSeq
std::uint32_t mSeq
Definition: InboundLedger.h:188
ripple::InboundLedger::takeAsRootNode
bool takeAsRootNode(Slice const &data, SHAMapAddNode &)
Process AS root node received from a peer Call with a lock.
Definition: InboundLedger.cpp:1019
ripple::PeerSet
Supports data retrieval by managing a set of peers.
Definition: PeerSet.h:48
ripple::InboundLedger::done
void done()
Definition: InboundLedger.cpp:456
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:939
ripple::InboundLedger::trigger
void trigger(std::shared_ptr< Peer > const &, TriggerReason)
Request more nodes, perhaps from a specific peer.
Definition: InboundLedger.cpp:507
mutex
ripple::LedgerInfo
Information about the notional ledger backing the view.
Definition: ReadView.h:80
ripple::InboundLedger::Reason
Reason
Definition: InboundLedger.h:49
ripple::InboundLedger::runData
void runData()
Process pending TMLedgerData Query the 'best' peer.
Definition: InboundLedger.cpp:1240
ripple::InboundLedger::mReceivedData
std::vector< PeerDataPairType > mReceivedData
Definition: InboundLedger.h:197
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:390
ripple::InboundLedger::mReceivedDataLock
std::mutex mReceivedDataLock
Definition: InboundLedger.h:196
ripple::InboundLedger::checkLocal
bool checkLocal()
Definition: InboundLedger.cpp:188
set
ripple::InboundLedger::mHaveTransactions
bool mHaveTransactions
Definition: InboundLedger.h:185
ripple::InboundLedger::getReason
Reason getReason() const
Definition: InboundLedger.h:86
ripple::InboundLedger::init
void init(ScopedLockType &collectionLock)
Definition: InboundLedger.cpp:99
ripple::PeerSet::progress
void progress()
Called to indicate that forward progress has been made.
Definition: PeerSet.h:86
ripple::InboundLedger::tryDB
void tryDB(Family &f)
Definition: InboundLedger.cpp:287
Json::Value
Represents a JSON value.
Definition: json_value.h:145