Skip to content

Commit

Permalink
[FOLD] Get rid of the ValidatorList callbacks entirely
Browse files Browse the repository at this point in the history
  • Loading branch information
ximinez committed Oct 22, 2019
1 parent f90141b commit a50bb59
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 79 deletions.
39 changes: 0 additions & 39 deletions src/ripple/app/main/Application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -573,12 +573,6 @@ class ApplicationImp
m_nodeStoreScheduler.setJobQueue (*m_jobQueue);

add (m_ledgerMaster->getPropertySource ());

validators_->setLoadSitesCallback(
[&](std::vector<std::string> const& sites) {
assert(validatorSites_);
validatorSites_->load(sites);
});
}

//--------------------------------------------------------------------------
Expand Down Expand Up @@ -1472,39 +1466,6 @@ bool ApplicationImp::setup()
*config_);
add (*overlay_); // add to PropertyStream

validators_->setMessageCallback(
[&](uint256 const& hash,
protocol::TMValidatorList const& msg,
PublicKey const& publisherKey,
size_t sequence) {
assert(overlay_);
assert(hashRouter_);
auto const toSkip = hashRouter_->shouldRelay(hash);

if (toSkip)
{
overlay_->foreach_mutable(send_and_modify_if_not(
std::make_shared<Message>(msg, protocol::mtVALIDATORLIST),
peer_in_set_or_condition(*toSkip,
[&](std::shared_ptr<Peer> const& peer) {
return peer->publisherListSequence(publisherKey)
== sequence;
}),
[&](std::shared_ptr<Peer>& peer) {
JLOG(m_journal.debug()) << "Sent validator list for " <<
strHex(publisherKey) << " with sequence " <<
sequence << " to " <<
peer->getRemoteAddress().to_string() << " (" <<
peer->id() << ")";
// Don't send it next time.
hashRouter_->addSuppressionPeer(
hash, peer->id());
peer->setPublisherListSequence(
publisherKey, sequence);
}));
}
});

if (!config_->standalone())
{
// validation and node import require the sqlite db
Expand Down
44 changes: 15 additions & 29 deletions src/ripple/app/misc/ValidatorList.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ class TMValidatorList;

namespace ripple {

// predeclaration
class Overlay;
class HashRouter;

enum class ListDisposition
{
/// List is valid
Expand Down Expand Up @@ -139,16 +143,6 @@ class ValidatorList
ManifestCache& validatorManifests_;
ManifestCache& publisherManifests_;
TimeKeeper& timeKeeper_;
// Typically, the Overlay and ValidatorSite objects haven't been created
// when this object is created, so references to them can't be provided
// to the ctor. Instead, the creator will provide these callback functions
// to do the appropriate work when necessary.
std::function<void(uint256 const& hash,
protocol::TMValidatorList const& msg,
PublicKey const& publisherKey,
size_t sequence)> sendMessage_;
std::function<void(std::vector<std::string> const& sites)>
loadValidatorSites_;
boost::filesystem::path const dataPath_;
beast::Journal j_;
std::shared_timed_mutex mutable mutex_;
Expand Down Expand Up @@ -231,23 +225,6 @@ class ValidatorList
std::vector<std::string> const& configKeys,
std::vector<std::string> const& publisherKeys);

void setMessageCallback(
std::function<void(uint256 const& hash,
protocol::TMValidatorList const& msg,
PublicKey const& publisherKey,
size_t sequence)>&& sendMessage)
{
sendMessage_ = std::move(sendMessage);
}

void setLoadSitesCallback(
std::function<void(std::vector<std::string> const& sites)>&&
loadValidatorSites)
{
loadValidatorSites_ = std::move(loadValidatorSites);
}


/** Apply published list of public keys, then broadcast it to all
peers that have not seen it or sent it.
Expand All @@ -263,6 +240,11 @@ class ValidatorList
@param hash Hash of the data parameters
@param overlay Overlay object which will handle sending the message
@param hashRouter HashRouter object which will determine which
peers not to send to
@return `ListDisposition::accepted`, plus some of the publisher
information, if list was successfully applied
Expand All @@ -277,7 +259,9 @@ class ValidatorList
std::string const& signature,
std::uint32_t version,
std::string siteUri,
uint256 const& hash);
uint256 const& hash,
Overlay& overlay,
HashRouter& hashRouter);

/** Apply published list of public keys
Expand Down Expand Up @@ -313,11 +297,13 @@ class ValidatorList
/* Attempt to read previously stored list files. Expected to only be
called when loading from URL fails.
@return A list of valid file:// URLs, if any.
@par Thread Safety
May be called concurrently
*/
void
std::vector<std::string>
loadLists();

/** Update trusted nodes
Expand Down
5 changes: 5 additions & 0 deletions src/ripple/app/misc/ValidatorSite.h
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,11 @@ class ValidatorSite
detail::response_type& res,
std::size_t siteIdx,
std::lock_guard<std::mutex>& lock);

/// If no sites are provided, or a site fails to load,
/// get a list of local cache files from the ValidatorList.
bool
missingSite();
};

} // ripple
Expand Down
50 changes: 44 additions & 6 deletions src/ripple/app/misc/impl/ValidatorList.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,15 @@
//==============================================================================

#include <ripple/app/misc/ValidatorList.h>
#include <ripple/app/misc/HashRouter.h>
#include <ripple/basics/base64.h>
#include <ripple/basics/date.h>
#include <ripple/basics/FileUtilities.h>
#include <ripple/basics/Slice.h>
#include <ripple/basics/StringUtilities.h>
#include <ripple/json/json_reader.h>
#include <ripple/overlay/Overlay.h>
#include <ripple/overlay/predicates.h>
#include <ripple/protocol/jss.h>
#include <ripple/protocol/messages.h>
#include <boost/regex.hpp>
Expand Down Expand Up @@ -203,7 +206,9 @@ ValidatorList::applyListAndBroadcast(
std::string const& signature,
std::uint32_t version,
std::string siteUri,
uint256 const& hash)
uint256 const& hash,
Overlay& overlay,
HashRouter& hashRouter)
{
auto const result = applyList(manifest, blob, signature,
version, siteUri, hash);
Expand All @@ -212,7 +217,7 @@ ValidatorList::applyListAndBroadcast(
bool broadcast = disposition == ListDisposition::accepted ||
disposition == ListDisposition::same_sequence;

if (broadcast && sendMessage_)
if (broadcast)
{
assert(result.available);
protocol::TMValidatorList msg;
Expand All @@ -221,7 +226,41 @@ ValidatorList::applyListAndBroadcast(
msg.set_signature(signature);
msg.set_version(version);

sendMessage_(hash, msg, result.publisherKey, result.sequence);
auto const toSkip = hashRouter.shouldRelay(hash);

if (toSkip)
{
auto const& publisherKey = result.publisherKey;
auto const sequence = result.sequence;

auto peercondition =
[&publisherKey, &sequence]
(std::shared_ptr<Peer> const& peer)
{
return peer->publisherListSequence(publisherKey)
== sequence;
};
auto peerupdate =
[&publisherKey, sequence, &hashRouter, &hash, &j =j_]
(std::shared_ptr<Peer>& peer)
{
JLOG(j.debug()) <<
"Sent validator list for " <<
strHex(publisherKey) << " with sequence " <<
sequence << " to " <<
peer->getRemoteAddress().to_string() <<
" (" << peer->id() << ")";
// Don't send it next time.
hashRouter.addSuppressionPeer(
hash, peer->id());
peer->setPublisherListSequence(
publisherKey, sequence);
};
overlay.foreach_mutable(send_and_modify_if_not(
std::make_shared<Message>(msg, protocol::mtVALIDATORLIST),
peer_in_set_or_condition(*toSkip, peercondition),
peerupdate));
}
}

return result;
Expand Down Expand Up @@ -410,7 +449,7 @@ ValidatorList::applyList (
return applyResult;
}

void
std::vector<std::string>
ValidatorList::loadLists()
{
using namespace std::string_literals;
Expand Down Expand Up @@ -463,8 +502,7 @@ ValidatorList::loadLists()
}

// Then let the ValidatorSites do the rest of the work.
if (!sites.empty() && loadValidatorSites_)
loadValidatorSites_(sites);
return sites;
}

ListDisposition
Expand Down
16 changes: 12 additions & 4 deletions src/ripple/app/misc/impl/ValidatorSite.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,15 +119,21 @@ ValidatorSite::~ValidatorSite()
}
}

bool
ValidatorSite::missingSite()
{
auto const sites = app_.validators().loadLists();
return sites.empty() || load(sites);
}

bool
ValidatorSite::load (
std::vector<std::string> const& siteURIs)
{
// If no sites are provided, act as if a site failed to load.
if (siteURIs.empty())
{
app_.validators().loadLists();
return true;
return missingSite();
}

JLOG (j_.debug()) <<
Expand Down Expand Up @@ -396,7 +402,9 @@ ValidatorSite::parseJsonResponse (
signature,
version,
uri,
hash);
hash,
app_.overlay(),
app_.getHashRouter());
auto const disp = applyResult.disposition;

sites_[siteIdx].lastRefreshStatus.emplace(
Expand Down Expand Up @@ -525,7 +533,7 @@ ValidatorSite::onSiteFetch(

// See if there's a copy saved locally from last time we
// saw the list.
app_.validators().loadLists();
missingSite();
};
if (ec)
{
Expand Down
4 changes: 3 additions & 1 deletion src/ripple/overlay/impl/PeerImp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2042,7 +2042,9 @@ PeerImp::onMessage (std::shared_ptr <protocol::TMValidatorList> const& m)
signature,
version,
remote_address_.to_string(),
hash);
hash,
app_.overlay(),
app_.getHashRouter());
auto const disp = applyResult.disposition;

JLOG(p_journal_.debug()) << "Processed validator list from " <<
Expand Down

1 comment on commit a50bb59

@mellery451
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Please sign in to comment.