Skip to content

Commit

Permalink
Allow a newly-started validator to participate in consensus.
Browse files Browse the repository at this point in the history
  • Loading branch information
mtrippled authored and seelabs committed Jul 31, 2017
1 parent 463b154 commit c96c423
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 35 deletions.
11 changes: 5 additions & 6 deletions src/ripple/app/consensus/RCLConsensus.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -934,12 +934,11 @@ RCLConsensus::peerProposal(
bool
RCLConsensus::Adaptor::preStartRound(RCLCxLedger const & prevLgr)
{
// We have a key, we have some idea what the ledger is, and we are not
// amendment blocked
validating_ =
!app_.getOPs().isNeedNetworkLedger() &&
(valPublic_.size() != 0) &&
!app_.getOPs().isAmendmentBlocked();
// We have a key and do not want out of sync validations after a restart,
// and are not amendment blocked.
validating_ = valPublic_.size() != 0 &&
prevLgr.seq() >= app_.getMaxDisallowedLedger() &&
!app_.getOPs().isAmendmentBlocked();

if (validating_)
{
Expand Down
33 changes: 19 additions & 14 deletions src/ripple/app/ledger/LedgerMaster.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#include <ripple/protocol/STValidation.h>
#include <ripple/beast/insight/Collector.h>
#include <ripple/core/Stoppable.h>
#include <ripple/protocol/Protocol.h>
#include <ripple/beast/utility/PropertyStream.h>
#include <mutex>

Expand Down Expand Up @@ -299,11 +300,11 @@ class LedgerMaster
std::shared_ptr<Ledger const> mHistLedger;

// Fully validated ledger, whether or not we have the ledger resident.
std::pair <uint256, LedgerIndex> mLastValidLedger;
std::pair <uint256, LedgerIndex> mLastValidLedger {uint256(), 0};

LedgerHistory mLedgerHistory;

CanonicalTXSet mHeldTransactions;
CanonicalTXSet mHeldTransactions {uint256()};

// A set of transactions to replay during the next close
std::unique_ptr<LedgerReplay> replayData;
Expand All @@ -314,23 +315,23 @@ class LedgerMaster
std::unique_ptr <detail::LedgerCleaner> mLedgerCleaner;

uint256 mLastValidateHash;
std::uint32_t mLastValidateSeq;
std::uint32_t mLastValidateSeq {0};

// Publish thread is running.
bool mAdvanceThread;
bool mAdvanceThread {false};

// Publish thread has work to do.
bool mAdvanceWork;
int mFillInProgress;
bool mAdvanceWork {false};
int mFillInProgress {0};

int mPathFindThread; // Pathfinder jobs dispatched
bool mPathFindNewRequest;
int mPathFindThread {0}; // Pathfinder jobs dispatched
bool mPathFindNewRequest {false};

std::atomic <std::uint32_t> mPubLedgerClose;
std::atomic <std::uint32_t> mPubLedgerSeq;
std::atomic <std::uint32_t> mValidLedgerSign;
std::atomic <std::uint32_t> mValidLedgerSeq;
std::atomic <std::uint32_t> mBuildingLedgerSeq;
std::atomic <std::uint32_t> mPubLedgerClose {0};
std::atomic <LedgerIndex> mPubLedgerSeq {0};
std::atomic <std::uint32_t> mValidLedgerSign {0};
std::atomic <LedgerIndex> mValidLedgerSeq {0};
std::atomic <LedgerIndex> mBuildingLedgerSeq {0};

// The server is in standalone mode
bool const standalone_;
Expand All @@ -345,7 +346,11 @@ class LedgerMaster

TaggedCache<uint256, Blob> fetch_packs_;

std::uint32_t fetch_seq_;
std::uint32_t fetch_seq_ {0};

// Try to keep a validator from switching from test to live network
// without first wiping the database.
LedgerIndex const max_ledger_difference_ {1000000};

};

Expand Down
18 changes: 4 additions & 14 deletions src/ripple/app/ledger/impl/LedgerMaster.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,30 +66,16 @@ LedgerMaster::LedgerMaster (Application& app, Stopwatch& stopwatch,
: Stoppable ("LedgerMaster", parent)
, app_ (app)
, m_journal (journal)
, mLastValidLedger (std::make_pair (uint256(), 0))
, mLedgerHistory (collector, app)
, mHeldTransactions (uint256 ())
, mLedgerCleaner (detail::make_LedgerCleaner (
app, *this, app_.journal("LedgerCleaner")))
, mLastValidateSeq (0)
, mAdvanceThread (false)
, mAdvanceWork (false)
, mFillInProgress (0)
, mPathFindThread (0)
, mPathFindNewRequest (false)
, mPubLedgerClose (0)
, mPubLedgerSeq (0)
, mValidLedgerSign (0)
, mValidLedgerSeq (0)
, mBuildingLedgerSeq (0)
, standalone_ (app_.config().standalone())
, fetch_depth_ (app_.getSHAMapStore ().clampFetchDepth (
app_.config().FETCH_DEPTH))
, ledger_history_ (app_.config().LEDGER_HISTORY)
, ledger_fetch_size_ (app_.config().getSize (siLedgerFetch))
, fetch_packs_ ("FetchPack", 65536, 45, stopwatch,
app_.journal("TaggedCache"))
, fetch_seq_ (0)
{
}

Expand Down Expand Up @@ -221,6 +207,10 @@ LedgerMaster::setValidLedger(

mValidLedger.set (l);
mValidLedgerSign = signTime.time_since_epoch().count();
assert (mValidLedgerSeq ||
!app_.getMaxDisallowedLedger() ||
l->info().seq + max_ledger_difference_ >
app_.getMaxDisallowedLedger());
mValidLedgerSeq = l->info().seq;

app_.getOPs().updateLocalTx (*l);
Expand Down
34 changes: 33 additions & 1 deletion src/ripple/app/main/Application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,14 @@
#include <ripple/overlay/Cluster.h>
#include <ripple/overlay/make_Overlay.h>
#include <ripple/protocol/STParsedJSON.h>
#include <ripple/protocol/Protocol.h>
#include <ripple/resource/Fees.h>
#include <ripple/beast/asio/io_latency_probe.h>
#include <ripple/beast/core/LexicalCast.h>
#include <boost/asio/steady_timer.hpp>
#include <fstream>
#include <sstream>
#include <iostream>

namespace ripple {

Expand All @@ -77,7 +80,7 @@ class AppFamily : public Family
beast::Journal j_;

// missing node handler
std::uint32_t maxSeq = 0;
LedgerIndex maxSeq = 0;
std::mutex maxSeqLock;

void acquire (
Expand Down Expand Up @@ -1006,8 +1009,17 @@ class ApplicationImp
setSweepTimer();
}

LedgerIndex getMaxDisallowedLedger() override
{
return maxDisallowedLedger_;
}


private:
// For a newly-started validator, this is the greatest persisted ledger
// and new validations must be greater than this.
std::atomic<LedgerIndex> maxDisallowedLedger_ {0};

void addTxnSeqField();
void addValidationSeqFields();
bool updateTables ();
Expand All @@ -1024,6 +1036,8 @@ class ApplicationImp
std::string const& ledgerID,
bool replay,
bool isFilename);

void setMaxDisallowedLedger();
};

//------------------------------------------------------------------------------
Expand Down Expand Up @@ -1073,6 +1087,9 @@ bool ApplicationImp::setup()
return false;
}

if (validatorKeys_.publicKey.size())
setMaxDisallowedLedger();

getLedgerDB ().getSession ()
<< boost::str (boost::format ("PRAGMA cache_size=-%d;") %
(config_->getSize (siLgrDBCache) * 1024));
Expand Down Expand Up @@ -1988,6 +2005,21 @@ bool ApplicationImp::updateTables ()
return true;
}

void ApplicationImp::setMaxDisallowedLedger()
{
boost::optional <LedgerIndex> seq;
{
auto db = getLedgerDB().checkoutDb();
*db << "SELECT MAX(LedgerSeq) FROM Ledgers;", soci::into(seq);
}
if (seq)
maxDisallowedLedger_ = *seq;

JLOG (m_journal.trace()) << "Max persisted ledger is "
<< maxDisallowedLedger_;
}


//------------------------------------------------------------------------------

Application::Application ()
Expand Down
5 changes: 5 additions & 0 deletions src/ripple/app/main/Application.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include <ripple/shamap/TreeNodeCache.h>
#include <ripple/basics/TaggedCache.h>
#include <ripple/core/Config.h>
#include <ripple/protocol/Protocol.h>
#include <ripple/beast/utility/PropertyStream.h>
#include <memory>
#include <mutex>
Expand Down Expand Up @@ -175,6 +176,10 @@ class Application : public beast::PropertyStream::Source

/** Retrieve the "wallet database" */
virtual DatabaseCon& getWalletDB () = 0;

/** Ensure that a newly-started validator does not sign proposals older
* than the last ledger it persisted. */
virtual LedgerIndex getMaxDisallowedLedger() = 0;
};

std::unique_ptr <Application>
Expand Down

0 comments on commit c96c423

Please sign in to comment.