Files
rippled/src/ripple/app/ledger/impl/LedgerTiming.cpp
2015-08-18 11:16:18 -07:00

74 lines
2.6 KiB
C++

//------------------------------------------------------------------------------
/*
This file is part of rippled: https://github.com/ripple/rippled
Copyright (c) 2012, 2013 Ripple Labs Inc.
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
//==============================================================================
#include <BeastConfig.h>
#include <ripple/app/ledger/LedgerTiming.h>
#include <ripple/basics/Log.h>
#include <algorithm>
#include <iterator>
namespace ripple {
int getNextLedgerTimeResolution (
int previousResolution,
bool previousAgree,
std::uint32_t ledgerSeq)
{
assert (ledgerSeq);
// Find the current resolution:
auto iter = std::find (std::begin (ledgerPossibleTimeResolutions),
std::end (ledgerPossibleTimeResolutions), previousResolution);
assert (iter != std::end (ledgerPossibleTimeResolutions));
// This should never happen, but just as a precaution
if (iter == std::end (ledgerPossibleTimeResolutions))
return previousResolution;
// If we did not previously agree, we try to decrease the resolution to
// improve the chance that we will agree now.
if (!previousAgree && ledgerSeq % decreaseLedgerTimeResolutionEvery == 0)
{
if (++iter != std::end (ledgerPossibleTimeResolutions))
return *iter;
}
// If we previously agreed, we try to increase the resolution to determine
// if we can continue to agree.
if (previousAgree && ledgerSeq % increaseLedgerTimeResolutionEvery == 0)
{
if (iter-- != std::begin (ledgerPossibleTimeResolutions))
return *iter;
}
return previousResolution;
}
std::uint32_t roundCloseTime (
std::uint32_t closeTime,
std::uint32_t closeResolution)
{
if (closeTime == 0)
return 0;
closeTime += (closeResolution / 2);
return closeTime - (closeTime % closeResolution);
}
} // ripple