Only forward to p2p nodes that are in sync

This commit is contained in:
CJ Cobb
2021-12-08 18:05:34 -05:00
committed by manojsdoshi
parent 45aa0142a6
commit 47376a0cc3
2 changed files with 26 additions and 5 deletions

View File

@@ -43,7 +43,7 @@ class NetworkValidatedLedgers
mutable std::mutex m_;
std::condition_variable cv_;
mutable std::condition_variable cv_;
bool stopping_ = false;
@@ -64,13 +64,23 @@ public:
/// @return sequence of most recently validated ledger. empty optional if
/// the datastructure has been stopped
std::optional<uint32_t>
getMostRecent()
getMostRecent() const
{
std::unique_lock lck(m_);
cv_.wait(lck, [this]() { return max_ || stopping_; });
return max_;
}
/// Get most recently validated sequence.
/// @return sequence of most recently validated ledger, or empty optional
/// if no ledgers are known to have been validated.
std::optional<uint32_t>
tryGetMostRecent() const
{
std::unique_lock lk(m_);
return max_;
}
/// Waits for the sequence to be validated by the network
/// @param sequence to wait for
/// @return true if sequence was validated, false otherwise

View File

@@ -760,13 +760,24 @@ ETLLoadBalancer::forwardToP2p(RPC::JsonContext& context) const
srand((unsigned)time(0));
auto sourceIdx = rand() % sources_.size();
auto numAttempts = 0;
auto mostRecent = etl_.getNetworkValidatedLedgers().tryGetMostRecent();
while (numAttempts < sources_.size())
{
res = sources_[sourceIdx]->forwardToP2p(context);
if (!res.isMember("forwarded") || res["forwarded"] != true)
{
auto increment = [&]() {
sourceIdx = (sourceIdx + 1) % sources_.size();
++numAttempts;
};
auto& src = sources_[sourceIdx];
if (mostRecent && !src->hasLedger(*mostRecent))
{
increment();
continue;
}
res = src->forwardToP2p(context);
if (!res.isMember("forwarded") || res["forwarded"] != true)
{
increment();
continue;
}
return res;