diff --git a/src/ripple_app/consensus/LedgerConsensus.cpp b/src/ripple_app/consensus/LedgerConsensus.cpp index 1d45d2245..758a78947 100644 --- a/src/ripple_app/consensus/LedgerConsensus.cpp +++ b/src/ripple_app/consensus/LedgerConsensus.cpp @@ -31,9 +31,10 @@ public: static char const* getCountedObjectName () { return "LedgerConsensus"; } - LedgerConsensusImp (LedgerHash const & prevLCLHash, + LedgerConsensusImp (clock_type& clock, LedgerHash const & prevLCLHash, Ledger::ref previousLedger, uint32 closeTime) - : mState (lcsPRE_CLOSE) + : m_clock (clock) + , mState (lcsPRE_CLOSE) , mCloseTime (closeTime) , mPrevLedgerHash (prevLCLHash) , mPreviousLedger (previousLedger) @@ -280,7 +281,8 @@ public: return empty; } - acquiring = boost::make_shared (hash); + acquiring = boost::make_shared ( + std::ref (m_clock), hash); startAcquiring (acquiring); } } @@ -1855,6 +1857,8 @@ private: val->setFieldU32(sfLoadFee, fee); } private: + clock_type& m_clock; + // VFALCO TODO Rename these to look pretty enum LCState { @@ -1910,12 +1914,9 @@ LedgerConsensus::~LedgerConsensus () { } -boost::shared_ptr LedgerConsensus::New(LedgerHash const & prevLCLHash, - Ledger::ref previousLedger, uint32 closeTime) +boost::shared_ptr LedgerConsensus::New (clock_type& clock, + LedgerHash const &prevLCLHash, Ledger::ref previousLedger, uint32 closeTime) { - return boost::make_shared( - prevLCLHash, previousLedger,closeTime); + return boost::make_shared ( + clock, prevLCLHash, previousLedger,closeTime); } - - -// vim:ts=4 diff --git a/src/ripple_app/consensus/LedgerConsensus.h b/src/ripple_app/consensus/LedgerConsensus.h index f03ecb5c8..0767480d7 100644 --- a/src/ripple_app/consensus/LedgerConsensus.h +++ b/src/ripple_app/consensus/LedgerConsensus.h @@ -27,10 +27,12 @@ */ class LedgerConsensus { -public: - static shared_ptr New( +public: + typedef abstract_clock clock_type; + + static boost::shared_ptr New (clock_type& clock, LedgerHash const & prevLCLHash, Ledger::ref previousLedger, - uint32 closeTime); + uint32 closeTime); virtual ~LedgerConsensus () = 0; diff --git a/src/ripple_app/ledger/InboundLedger.cpp b/src/ripple_app/ledger/InboundLedger.cpp index 5c19d6eaa..f91079c4d 100644 --- a/src/ripple_app/ledger/InboundLedger.cpp +++ b/src/ripple_app/ledger/InboundLedger.cpp @@ -25,8 +25,8 @@ SETUP_LOG (InboundLedger) #define LEDGER_TIMEOUT_COUNT 10 // how many timeouts before we giveup #define LEDGER_TIMEOUT_AGGRESSIVE 6 // how many timeouts before we get aggressive -InboundLedger::InboundLedger (uint256 const& hash, uint32 seq) - : PeerSet (hash, LEDGER_ACQUIRE_TIMEOUT, false) +InboundLedger::InboundLedger (clock_type& clock, uint256 const& hash, uint32 seq) + : PeerSet (clock, hash, LEDGER_ACQUIRE_TIMEOUT, false) , mHaveBase (false) , mHaveState (false) , mHaveTransactions (false) diff --git a/src/ripple_app/ledger/InboundLedger.h b/src/ripple_app/ledger/InboundLedger.h index 0b02b4bda..1efa332db 100644 --- a/src/ripple_app/ledger/InboundLedger.h +++ b/src/ripple_app/ledger/InboundLedger.h @@ -34,9 +34,9 @@ public: typedef std::pair < boost::weak_ptr, boost::shared_ptr > PeerDataPairType; public: - InboundLedger (uint256 const& hash, uint32 seq); + InboundLedger (clock_type& clock, uint256 const& hash, uint32 seq); - virtual ~InboundLedger (); + ~InboundLedger (); bool isBase () const { diff --git a/src/ripple_app/ledger/InboundLedgers.cpp b/src/ripple_app/ledger/InboundLedgers.cpp index 193b15168..e54b085cf 100644 --- a/src/ripple_app/ledger/InboundLedgers.cpp +++ b/src/ripple_app/ledger/InboundLedgers.cpp @@ -28,12 +28,12 @@ public: // How long before we try again to acquire the same ledger static const int kReacquireIntervalSeconds = 300; - explicit InboundLedgersImp (Stoppable& parent) + InboundLedgersImp (clock_type& clock, Stoppable& parent) : Stoppable ("InboundLedgers", parent) + , m_clock (clock) , mLock (this, "InboundLedger", __FILE__, __LINE__) , mRecentFailures ("LedgerAcquireRecentFailures", - get_abstract_clock (), - 0, kReacquireIntervalSeconds) + clock, 0, kReacquireIntervalSeconds) { } @@ -57,7 +57,7 @@ public: } else { - ret = boost::make_shared (hash, seq); + ret = boost::make_shared (std::ref (m_clock), hash, seq); assert (ret); mLedgers.insert (std::make_pair (hash, ret)); ret->init (sl, couldBeNew); @@ -335,7 +335,7 @@ public: { mRecentFailures.sweep (); - int const now = UptimeTimer::getInstance ().getElapsedSeconds (); + clock_type::time_point const now (m_clock.now()); // Make a list of things to sweep, while holding the lock std::vector stuffToSweep; @@ -353,7 +353,7 @@ public: it->second->touch (); ++it; } - else if ((it->second->getLastAction () + 60) < now) + else if ((it->second->getLastAction () + std::chrono::minutes (1)) < now) { stuffToSweep.push_back (it->second); // shouldn't cause the actual final delete @@ -383,6 +383,8 @@ public: } private: + clock_type& m_clock; + typedef boost::unordered_map MapType; typedef RippleRecursiveMutex LockType; @@ -390,7 +392,7 @@ private: LockType mLock; MapType mLedgers; - KeyCache mRecentFailures; + KeyCache mRecentFailures; uint256 mConsensusLedger; uint256 mValidationLedger; @@ -402,9 +404,9 @@ InboundLedgers::~InboundLedgers() { } -InboundLedgers* InboundLedgers::New (Stoppable& parent) +InboundLedgers* InboundLedgers::New (clock_type& clock, Stoppable& parent) { - return new InboundLedgersImp (parent); + return new InboundLedgersImp (clock, parent); } diff --git a/src/ripple_app/ledger/InboundLedgers.h b/src/ripple_app/ledger/InboundLedgers.h index 82475405d..a66b3b3b3 100644 --- a/src/ripple_app/ledger/InboundLedgers.h +++ b/src/ripple_app/ledger/InboundLedgers.h @@ -27,9 +27,14 @@ class InboundLedgers { public: + typedef abstract_clock clock_type; + virtual ~InboundLedgers() = 0; - static InboundLedgers* New (Stoppable& parent); + // VFALCO TODO Make this a free function outside the class: + // std::unique_ptr make_InboundLedgers (...) + // + static InboundLedgers* New (clock_type& clock, Stoppable& parent); // VFALCO TODO Should this be called findOrAdd ? // diff --git a/src/ripple_app/main/Application.cpp b/src/ripple_app/main/Application.cpp index 87975bbf6..7dca258b6 100644 --- a/src/ripple_app/main/Application.cpp +++ b/src/ripple_app/main/Application.cpp @@ -220,8 +220,9 @@ public: *m_jobQueue, LogPartition::getJournal ())) // VFALCO NOTE Does NetworkOPs depend on LedgerMaster? - , m_networkOPs (NetworkOPs::New ( - *m_ledgerMaster, *m_jobQueue, LogPartition::getJournal ())) + , m_networkOPs (NetworkOPs::New (get_abstract_clock < + std::chrono::steady_clock, std::chrono::seconds> (), *m_ledgerMaster, + *m_jobQueue, LogPartition::getJournal ())) // VFALCO NOTE LocalCredentials starts the deprecated UNL service , m_deprecatedUNL (UniqueNodeList::New (*m_jobQueue)) @@ -238,7 +239,8 @@ public: , m_sntpClient (SNTPClient::New (*this)) - , m_inboundLedgers (InboundLedgers::New (*m_jobQueue)) + , m_inboundLedgers (InboundLedgers::New (get_abstract_clock < + std::chrono::steady_clock, std::chrono::seconds> (), *m_jobQueue)) , m_txQueue (TxQueue::New ()) diff --git a/src/ripple_app/misc/NetworkOPs.cpp b/src/ripple_app/misc/NetworkOPs.cpp index 6f89761c3..598f8493c 100644 --- a/src/ripple_app/misc/NetworkOPs.cpp +++ b/src/ripple_app/misc/NetworkOPs.cpp @@ -33,8 +33,10 @@ public: public: // VFALCO TODO Make LedgerMaster a SharedPtr or a reference. // - NetworkOPsImp (LedgerMaster& ledgerMaster, Stoppable& parent, Journal journal) + NetworkOPsImp (clock_type& clock, LedgerMaster& ledgerMaster, + Stoppable& parent, Journal journal) : NetworkOPs (parent) + , m_clock (clock) , m_journal (journal) , mLock (this, "NetOPs", __FILE__, __LINE__) , mMode (omDISCONNECTED) @@ -407,6 +409,8 @@ private: void pubServer (); private: + clock_type& m_clock; + typedef boost::unordered_map SubInfoMapType; typedef boost::unordered_map ::iterator SubInfoMapIterator; @@ -1410,7 +1414,7 @@ int NetworkOPsImp::beginConsensus (uint256 const& networkClosed, Ledger::pointer assert (!mConsensus); prevLedger->setImmutable (); - mConsensus = LedgerConsensus::New( + mConsensus = LedgerConsensus::New (m_clock, networkClosed, prevLedger, m_ledgerMaster.getCurrentLedger ()->getCloseTimeNC ()); @@ -3104,10 +3108,14 @@ NetworkOPs::NetworkOPs (Stoppable& parent) { } +NetworkOPs::~NetworkOPs () +{ +} + //------------------------------------------------------------------------------ -NetworkOPs* NetworkOPs::New (LedgerMaster& ledgerMaster, +NetworkOPs* NetworkOPs::New (clock_type& clock, LedgerMaster& ledgerMaster, Stoppable& parent, Journal journal) { - return new NetworkOPsImp (ledgerMaster, parent, journal); + return new NetworkOPsImp (clock, ledgerMaster, parent, journal); } diff --git a/src/ripple_app/misc/NetworkOPs.h b/src/ripple_app/misc/NetworkOPs.h index a37857dd4..03e6fae76 100644 --- a/src/ripple_app/misc/NetworkOPs.h +++ b/src/ripple_app/misc/NetworkOPs.h @@ -56,6 +56,8 @@ protected: explicit NetworkOPs (Stoppable& parent); public: + typedef abstract_clock clock_type; + enum Fault { // exceptions these functions can throw @@ -80,10 +82,10 @@ public: public: // VFALCO TODO Make LedgerMaster a SharedPtr or a reference. // - static NetworkOPs* New (LedgerMaster& ledgerMaster, + static NetworkOPs* New (clock_type& clock, LedgerMaster& ledgerMaster, Stoppable& parent, Journal journal); - virtual ~NetworkOPs () { } + virtual ~NetworkOPs () = 0; //-------------------------------------------------------------------------- // diff --git a/src/ripple_app/peers/PeerSet.cpp b/src/ripple_app/peers/PeerSet.cpp index a1e3a79fa..f2feb8e25 100644 --- a/src/ripple_app/peers/PeerSet.cpp +++ b/src/ripple_app/peers/PeerSet.cpp @@ -19,7 +19,8 @@ class InboundLedger; -PeerSet::PeerSet (uint256 const& hash, int interval, bool txnData) +PeerSet::PeerSet (clock_type& clock_, + uint256 const& hash, int interval, bool txnData) : mLock (this, "PeerSet", __FILE__, __LINE__) , mHash (hash) , mTimerInterval (interval) @@ -30,11 +31,21 @@ PeerSet::PeerSet (uint256 const& hash, int interval, bool txnData) , mTxnData (txnData) , mProgress (false) , mTimer (getApp().getIOService ()) + , m_clock (clock_) { - mLastAction = UptimeTimer::getInstance ().getElapsedSeconds (); + mLastAction = clock().now(); assert ((mTimerInterval > 10) && (mTimerInterval < 30000)); } +PeerSet::~PeerSet () +{ +} + +PeerSet::clock_type& PeerSet::clock () +{ + return m_clock; +} + bool PeerSet::peerHas (Peer::ref ptr) { ScopedLockType sl (mLock, __FILE__, __LINE__); @@ -139,7 +150,7 @@ void PeerSet::sendRequest (const protocol::TMGetLedger& tmGL) PackedMessage::pointer packet = boost::make_shared (tmGL, protocol::mtGET_LEDGER); - for (boost::unordered_map::iterator it = mPeers.begin (), end = mPeers.end (); it != end; ++it) + for (Map::iterator it = mPeers.begin (), end = mPeers.end (); it != end; ++it) { Peer::pointer peer = getApp().getPeers ().getPeerById (it->first); @@ -153,7 +164,7 @@ int PeerSet::takePeerSetFrom (const PeerSet& s) int ret = 0; mPeers.clear (); - for (boost::unordered_map::const_iterator it = s.mPeers.begin (), end = s.mPeers.end (); + for (Map::const_iterator it = s.mPeers.begin (), end = s.mPeers.end (); it != end; ++it) { mPeers.insert (std::make_pair (it->first, 0)); @@ -167,7 +178,7 @@ int PeerSet::getPeerCount () const { int ret = 0; - for (boost::unordered_map::const_iterator it = mPeers.begin (), end = mPeers.end (); it != end; ++it) + for (Map::const_iterator it = mPeers.begin (), end = mPeers.end (); it != end; ++it) if (getApp().getPeers ().hasPeer (it->first)) ++ret; diff --git a/src/ripple_app/peers/PeerSet.h b/src/ripple_app/peers/PeerSet.h index 0f7571d82..a3a394c13 100644 --- a/src/ripple_app/peers/PeerSet.h +++ b/src/ripple_app/peers/PeerSet.h @@ -27,42 +27,52 @@ class PeerSet : LeakChecked { public: + typedef abstract_clock clock_type; + uint256 const& getHash () const { return mHash; } + bool isComplete () const { return mComplete; } + bool isFailed () const { return mFailed; } + int getTimeouts () const { return mTimeouts; } bool isActive (); + void progress () { mProgress = true; mAggressive = false; } + void clearProgress () { mProgress = false; } + bool isProgress () { return mProgress; } + void touch () { - mLastAction = UptimeTimer::getInstance ().getElapsedSeconds (); + mLastAction = clock().now(); } - int getLastAction () + + clock_type::time_point getLastAction () const { return mLastAction; } @@ -92,13 +102,15 @@ private: static void TimerEntry (boost::weak_ptr, const boost::system::error_code& result); static void TimerJobEntry (Job&, boost::shared_ptr); - // VFALCO TODO try to make some of these private protected: + clock_type& clock (); + + // VFALCO TODO try to make some of these private typedef RippleRecursiveMutex LockType; typedef LockType::ScopedLockType ScopedLockType; - PeerSet (uint256 const& hash, int interval, bool txnData); - virtual ~PeerSet () { } + PeerSet (clock_type& clock, uint256 const& hash, int interval, bool txnData); + virtual ~PeerSet () = 0; virtual void newPeer (Peer::ref) = 0; virtual void onTimer (bool progress, ScopedLockType&) = 0; @@ -127,7 +139,7 @@ protected: bool mFailed; bool mAggressive; bool mTxnData; - int mLastAction; + clock_type::time_point mLastAction; bool mProgress; @@ -137,7 +149,11 @@ protected: // VFALCO TODO Verify that these are used in the way that the names suggest. typedef uint64 PeerIdentifier; typedef int ReceivedChunkCount; - boost::unordered_map mPeers; + typedef boost::unordered_map Map; + Map mPeers; + +private: + clock_type& m_clock; }; #endif diff --git a/src/ripple_app/shamap/SHAMap.h b/src/ripple_app/shamap/SHAMap.h index a765201fb..3188cc897 100644 --- a/src/ripple_app/shamap/SHAMap.h +++ b/src/ripple_app/shamap/SHAMap.h @@ -267,7 +267,7 @@ public: typedef std::pair TNIndex; private: - static KeyCache fullBelowCache; + static KeyCache fullBelowCache; static TaggedCacheType treeNodeCache; diff --git a/src/ripple_app/shamap/SHAMapSync.cpp b/src/ripple_app/shamap/SHAMapSync.cpp index ebdc69398..9850e0b35 100644 --- a/src/ripple_app/shamap/SHAMapSync.cpp +++ b/src/ripple_app/shamap/SHAMapSync.cpp @@ -21,7 +21,8 @@ static const uint256 uZero; -KeyCache SHAMap::fullBelowCache ( +// VFALCO TODO Put these globals into a singleton and make the clock a parameter +KeyCache SHAMap::fullBelowCache ( "fullBelowCache", get_abstract_clock (), 524288, 240); diff --git a/src/ripple_app/tx/TransactionAcquire.cpp b/src/ripple_app/tx/TransactionAcquire.cpp index f3091dfa4..2ac993973 100644 --- a/src/ripple_app/tx/TransactionAcquire.cpp +++ b/src/ripple_app/tx/TransactionAcquire.cpp @@ -24,13 +24,17 @@ SETUP_LOG (TransactionAcquire) typedef std::map::value_type u160_prop_pair; typedef std::map::value_type u256_lct_pair; -TransactionAcquire::TransactionAcquire (uint256 const& hash) - : PeerSet (hash, TX_ACQUIRE_TIMEOUT, true) +TransactionAcquire::TransactionAcquire (clock_type& clock, uint256 const& hash) + : PeerSet (clock, hash, TX_ACQUIRE_TIMEOUT, true) , mHaveRoot (false) { mMap = boost::make_shared (smtTRANSACTION, hash); } +TransactionAcquire::~TransactionAcquire () +{ +} + static void TACompletionHandler (uint256 hash, SHAMap::pointer map) { { diff --git a/src/ripple_app/tx/TransactionAcquire.h b/src/ripple_app/tx/TransactionAcquire.h index d7161bd45..9ca7673b8 100644 --- a/src/ripple_app/tx/TransactionAcquire.h +++ b/src/ripple_app/tx/TransactionAcquire.h @@ -33,11 +33,8 @@ public: typedef boost::shared_ptr pointer; public: - explicit TransactionAcquire (uint256 const& hash); - virtual ~TransactionAcquire () - { - ; - } + TransactionAcquire (clock_type& clock, uint256 const& hash); + ~TransactionAcquire (); SHAMap::ref getMap () { diff --git a/src/ripple_basics/containers/KeyCache.h b/src/ripple_basics/containers/KeyCache.h index 8734924be..3487cfad2 100644 --- a/src/ripple_basics/containers/KeyCache.h +++ b/src/ripple_basics/containers/KeyCache.h @@ -58,7 +58,7 @@ private: clock_type::time_point last_access; }; - typedef std::unordered_map map_type; + typedef std::unordered_map map_type; typedef typename map_type::iterator iterator; typedef std::lock_guard lock_guard;