Compute validated ledger age from signing time

This commit is contained in:
David Schwartz
2015-05-01 13:10:14 -07:00
committed by Nik Bougalis
parent 18299c3f7a
commit d0b28a6700
3 changed files with 40 additions and 5 deletions

View File

@@ -41,6 +41,7 @@
#include <algorithm>
#include <cassert>
#include <beast/cxx14/memory.h> // <memory>
#include <vector>
namespace ripple {
@@ -91,7 +92,7 @@ public:
std::atomic <std::uint32_t> mPubLedgerClose;
std::atomic <std::uint32_t> mPubLedgerSeq;
std::atomic <std::uint32_t> mValidLedgerClose;
std::atomic <std::uint32_t> mValidLedgerSign;
std::atomic <std::uint32_t> mValidLedgerSeq;
std::atomic <std::uint32_t> 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 <std::uint32_t> 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());

View File

@@ -387,6 +387,18 @@ private:
return ret;
}
std::vector<uint32_t>
getValidationTimes (uint256 const& hash)
{
std::vector <std::uint32_t> 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;

View File

@@ -22,6 +22,7 @@
#include <ripple/protocol/STValidation.h>
#include <beast/cxx14/memory.h> // <memory>
#include <vector>
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<std::uint32_t> getValidationTimes (
uint256 const& ledger) = 0;
virtual std::list <STValidation::pointer>
getCurrentTrustedValidations () = 0;