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/main/Application.h>
24 #include <ripple/app/ledger/Ledger.h>
25 #include <ripple/overlay/PeerSet.h>
26 #include <ripple/basics/CountedObject.h>
27 #include <mutex>
28 #include <set>
29 #include <utility>
30 
31 namespace ripple {
32 
33 // A ledger we are trying to acquire
35  : public PeerSet
36  , public std::enable_shared_from_this <InboundLedger>
37  , public CountedObject <InboundLedger>
38 {
39 public:
40  static char const* getCountedObjectName () { return "InboundLedger"; }
41 
45 
46  // These are the reasons we might acquire a ledger
47  enum class Reason
48  {
49  HISTORY, // Acquiring past ledger
50  SHARD, // Acquiring for shard
51  GENERIC, // Generic other reasons
52  CONSENSUS // We believe the consensus round requires this ledger
53  };
54 
55  InboundLedger(Application& app, uint256 const& hash,
56  std::uint32_t seq, Reason reason, clock_type&);
57 
58  ~InboundLedger ();
59 
60  // Called when the PeerSet timer expires
61  void execute () override;
62 
63  // Called when another attempt is made to fetch this same ledger
64  void update (std::uint32_t seq);
65 
67  getLedger() const
68  {
69  return mLedger;
70  }
71 
73  {
74  return mSeq;
75  }
76 
77  Reason
78  getReason() const
79  {
80  return mReason;
81  }
82 
83  bool checkLocal ();
84  void init (ScopedLockType& collectionLock);
85 
86  bool
89 
90  using neededHash_t =
92 
94  Json::Value getJson (int);
95 
96  void runData ();
97 
98  static
100  deserializeHeader(Slice data, bool hasPrefix);
101 
102 private:
103  enum class TriggerReason
104  {
105  added,
106  reply,
107  timeout
108  };
109 
110  void filterNodes (
112  TriggerReason reason);
113 
115 
117 
118  void addPeers ();
119  void tryDB (Family& f);
120 
121  void done ();
122 
123  void onTimer (bool progress, ScopedLockType& peerSetLock) override;
124 
125  void newPeer (std::shared_ptr<Peer> const& peer) override
126  {
127  // For historical nodes, do not trigger too soon
128  // since a fetch pack is probably coming
129  if (mReason != Reason::HISTORY)
131  }
132 
134 
135  int processData (std::shared_ptr<Peer> peer, protocol::TMLedgerData& data);
136 
137  bool takeHeader (std::string const& data);
138  bool takeTxNode (const std::vector<SHAMapNodeID>& IDs,
139  const std::vector<Blob>& data,
140  SHAMapAddNode&);
141  bool takeTxRootNode (Slice const& data, SHAMapAddNode&);
142 
143  // VFALCO TODO Rename to receiveAccountStateNode
144  // Don't use acronyms, but if we are going to use them at least
145  // capitalize them correctly.
146  //
147  bool takeAsNode (const std::vector<SHAMapNodeID>& IDs,
148  const std::vector<Blob>& data,
149  SHAMapAddNode&);
150  bool takeAsRootNode (Slice const& data, SHAMapAddNode&);
151 
154  int max, SHAMapSyncFilter* filter) const;
155 
158  int max, SHAMapSyncFilter* filter) const;
159 
164  bool mSignaled;
165  bool mByHash;
168 
170 
172 
173  // Data we have received from peers
177 };
178 
179 } // ripple
180 
181 #endif
ripple::InboundLedger::mRecentNodes
std::set< uint256 > mRecentNodes
Definition: InboundLedger.h:169
ripple::Application
Definition: Application.h:85
ripple::SHAMapAddNode
Definition: SHAMapAddNode.h:28
ripple::InboundLedger::Reason::HISTORY
@ HISTORY
ripple::InboundLedger::getNeededHashes
std::vector< neededHash_t > getNeededHashes()
Definition: InboundLedger.cpp:1080
ripple::InboundLedger::mReason
const Reason mReason
Definition: InboundLedger.h:167
ripple::InboundLedger::getJson
Json::Value getJson(int)
Return a Json::objectValue.
Definition: InboundLedger.cpp:1301
ripple::InboundLedger::TriggerReason
TriggerReason
Definition: InboundLedger.h:103
ripple::CountedObject
Tracks the number of instances of an object.
Definition: CountedObject.h:102
std::string
STL class.
ripple::InboundLedger::Reason::CONSENSUS
@ CONSENSUS
std::shared_ptr
STL class.
ripple::InboundLedger::mHaveState
bool mHaveState
Definition: InboundLedger.h:162
ripple::InboundLedger
Definition: InboundLedger.h:34
utility
ripple::InboundLedger::TriggerReason::added
@ added
ripple::InboundLedger::getSeq
std::uint32_t getSeq() const
Definition: InboundLedger.h:72
ripple::Slice
An immutable linear range of bytes.
Definition: Slice.h:43
ripple::InboundLedger::mSignaled
bool mSignaled
Definition: InboundLedger.h:164
std::pair
ripple::InboundLedger::Reason::GENERIC
@ GENERIC
std::vector
STL class.
ripple::InboundLedger::getLedger
std::shared_ptr< Ledger const > getLedger() const
Definition: InboundLedger.h:67
ripple::InboundLedger::update
void update(std::uint32_t seq)
Definition: InboundLedger.cpp:164
ripple::InboundLedger::InboundLedger
InboundLedger(Application &app, uint256 const &hash, std::uint32_t seq, Reason reason, clock_type &)
Definition: InboundLedger.cpp:69
ripple::InboundLedger::~InboundLedger
~InboundLedger()
Definition: InboundLedger.cpp:196
ripple::InboundLedger::newPeer
void newPeer(std::shared_ptr< Peer > const &peer) override
Definition: InboundLedger.h:125
ripple::InboundLedger::mByHash
bool mByHash
Definition: InboundLedger.h:165
ripple::InboundLedger::filterNodes
void filterNodes(std::vector< std::pair< SHAMapNodeID, uint256 >> &nodes, TriggerReason reason)
Definition: InboundLedger.cpp:795
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:1145
ripple::InboundLedger::execute
void execute() override
Definition: InboundLedger.cpp:147
ripple::InboundLedger::neededStateHashes
std::vector< uint256 > neededStateHashes(int max, SHAMapSyncFilter *filter) const
Definition: InboundLedger.cpp:234
ripple::PeerSet::clock_type
beast::abstract_clock< std::chrono::steady_clock > clock_type
Definition: PeerSet.h:51
ripple::InboundLedger::PeerDataPairType
std::pair< std::weak_ptr< Peer >, std::shared_ptr< protocol::TMLedgerData > > PeerDataPairType
Definition: InboundLedger.h:44
ripple::InboundLedger::neededTxHashes
std::vector< uint256 > neededTxHashes(int max, SHAMapSyncFilter *filter) const
Definition: InboundLedger.cpp:217
ripple::base_uint< 256 >
ripple::InboundLedger::takeHeader
bool takeHeader(std::string const &data)
Take ledger header data Call with a lock.
Definition: InboundLedger.cpp:845
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:1120
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:893
ripple::InboundLedger::mLedger
std::shared_ptr< Ledger > mLedger
Definition: InboundLedger.h:160
ripple::InboundLedger::deserializeHeader
static LedgerInfo deserializeHeader(Slice data, bool hasPrefix)
Definition: InboundLedger.cpp:251
ripple::InboundLedger::addPeers
void addPeers()
Add more peers to the set, if possible.
Definition: InboundLedger.cpp:441
ripple::InboundLedger::neededHash_t
std::pair< protocol::TMGetObjectByHash::ObjectType, uint256 > neededHash_t
Definition: InboundLedger.h:91
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:1058
ripple::Family
Definition: Family.h:32
ripple::InboundLedger::pmDowncast
std::weak_ptr< PeerSet > pmDowncast() override
Definition: InboundLedger.cpp:448
std::uint32_t
ripple::PeerSet::app
Application & app()
Definition: PeerSet.h:107
ripple::InboundLedger::mReceiveDispatched
bool mReceiveDispatched
Definition: InboundLedger.h:176
ripple::InboundLedger::mHaveHeader
bool mHaveHeader
Definition: InboundLedger.h:161
ripple::InboundLedger::getCountedObjectName
static char const * getCountedObjectName()
Definition: InboundLedger.h:40
ripple::PeerSet::ScopedLockType
std::unique_lock< std::recursive_mutex > ScopedLockType
Definition: PeerSet.h:113
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:171
ripple::InboundLedger::mSeq
std::uint32_t mSeq
Definition: InboundLedger.h:166
ripple::InboundLedger::takeAsRootNode
bool takeAsRootNode(Slice const &data, SHAMapAddNode &)
Process AS root node received from a peer Call with a lock.
Definition: InboundLedger.cpp:1034
ripple::PeerSet
Supports data retrieval by managing a set of peers.
Definition: PeerSet.h:48
ripple::InboundLedger::done
void done()
Definition: InboundLedger.cpp:453
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:954
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:79
ripple::InboundLedger::Reason
Reason
Definition: InboundLedger.h:47
ripple::InboundLedger::runData
void runData()
Process pending TMLedgerData Query the 'best' peer.
Definition: InboundLedger.cpp:1259
ripple::InboundLedger::mReceivedData
std::vector< PeerDataPairType > mReceivedData
Definition: InboundLedger.h:175
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:389
ripple::InboundLedger::mReceivedDataLock
std::mutex mReceivedDataLock
Definition: InboundLedger.h:174
ripple::InboundLedger::checkLocal
bool checkLocal()
Definition: InboundLedger.cpp:176
set
ripple::InboundLedger::mHaveTransactions
bool mHaveTransactions
Definition: InboundLedger.h:163
ripple::InboundLedger::getReason
Reason getReason() const
Definition: InboundLedger.h:78
ripple::InboundLedger::init
void init(ScopedLockType &collectionLock)
Definition: InboundLedger.cpp:86
ripple::PeerSet::progress
void progress()
Called to indicate that forward progress has been made.
Definition: PeerSet.h:80
ripple::InboundLedger::tryDB
void tryDB(Family &f)
Definition: InboundLedger.cpp:278
Json::Value
Represents a JSON value.
Definition: json_value.h:141