From d0b28a6700e8ab96f136eff2a5cfbf96d057d981 Mon Sep 17 00:00:00 2001 From: David Schwartz Date: Fri, 1 May 2015 13:10:14 -0700 Subject: [PATCH] Compute validated ledger age from signing time --- src/ripple/app/ledger/LedgerMaster.cpp | 28 +++++++++++++++++++++----- src/ripple/app/misc/Validations.cpp | 12 +++++++++++ src/ripple/app/misc/Validations.h | 5 +++++ 3 files changed, 40 insertions(+), 5 deletions(-) diff --git a/src/ripple/app/ledger/LedgerMaster.cpp b/src/ripple/app/ledger/LedgerMaster.cpp index 4e4ddc4227..5a14c36383 100644 --- a/src/ripple/app/ledger/LedgerMaster.cpp +++ b/src/ripple/app/ledger/LedgerMaster.cpp @@ -41,6 +41,7 @@ #include #include #include // +#include namespace ripple { @@ -91,7 +92,7 @@ public: std::atomic mPubLedgerClose; std::atomic mPubLedgerSeq; - std::atomic mValidLedgerClose; + std::atomic mValidLedgerSign; std::atomic mValidLedgerSeq; std::atomic mBuildingLedgerSeq; @@ -125,7 +126,7 @@ public: , mPathFindNewRequest (false) , mPubLedgerClose (0) , mPubLedgerSeq (0) - , mValidLedgerClose (0) + , mValidLedgerSign (0) , mValidLedgerSeq (0) , mBuildingLedgerSeq (0) , standalone_ (config.RUN_STANDALONE) @@ -168,7 +169,7 @@ public: int getValidatedLedgerAge () { - std::uint32_t valClose = mValidLedgerClose.load(); + std::uint32_t valClose = mValidLedgerSign.load(); if (!valClose) { WriteLog (lsDEBUG, LedgerMaster) << "No validated ledger"; @@ -190,7 +191,7 @@ public: reason = "No recently-published ledger"; return false; } - std::uint32_t validClose = mValidLedgerClose.load(); + std::uint32_t validClose = mValidLedgerSign.load(); std::uint32_t pubClose = mPubLedgerClose.load(); if (!validClose || !pubClose) { @@ -207,8 +208,25 @@ public: void setValidLedger(Ledger::ref l) { + std::vector times; + std::uint32_t signTime; + if (! getConfig().RUN_STANDALONE) + times = getApp().getValidations().getValidationTimes( + l->getHash()); + if (! times.empty () && times.size() >= mMinValidations) + { + // Calculate the sample median + std::sort (times.begin (), times.end ()); + signTime = (times[times.size() / 2] + + times[(times.size() - 1) / 2]) / 2; + } + else + { + signTime = l->getCloseTimeNC(); + } + mValidLedger.set (l); - mValidLedgerClose = l->getCloseTimeNC(); + mValidLedgerSign = signTime; mValidLedgerSeq = l->getLedgerSeq(); getApp().getOPs().updateLocalTx (l); getApp().getSHAMapStore().onLedgerClosed (getValidatedLedger()); diff --git a/src/ripple/app/misc/Validations.cpp b/src/ripple/app/misc/Validations.cpp index ab5c088092..6d46824329 100644 --- a/src/ripple/app/misc/Validations.cpp +++ b/src/ripple/app/misc/Validations.cpp @@ -387,6 +387,18 @@ private: return ret; } + std::vector + getValidationTimes (uint256 const& hash) + { + std::vector times; + ScopedLockType sl (mLock); + if (auto j = findSet (hash)) + for (auto& it : *j) + if (it.second->isTrusted()) + times.push_back (it.second->getSignTime()); + return times; + } + void flush () { bool anyNew = false; diff --git a/src/ripple/app/misc/Validations.h b/src/ripple/app/misc/Validations.h index fef7843701..a916531f8b 100644 --- a/src/ripple/app/misc/Validations.h +++ b/src/ripple/app/misc/Validations.h @@ -22,6 +22,7 @@ #include #include // +#include namespace ripple { @@ -64,6 +65,10 @@ public: virtual LedgerToValidationCounter getCurrentValidations ( uint256 currentLedger, uint256 previousLedger) = 0; + /** Return the times of all validations for a particular ledger hash. */ + virtual std::vector getValidationTimes ( + uint256 const& ledger) = 0; + virtual std::list getCurrentTrustedValidations () = 0;