Skip to content

Commit

Permalink
Optimize when to acquire ledgers from the network.
Browse files Browse the repository at this point in the history
Particulary avoid acquiring ledgers likely to be
produced locally very soon.
  • Loading branch information
mtrippled committed Nov 28, 2023
1 parent 8ec475b commit c3c37ff
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 4 deletions.
44 changes: 40 additions & 4 deletions src/ripple/app/ledger/impl/InboundLedgers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,46 @@ class InboundLedgersImp : public InboundLedgers
reason != InboundLedger::Reason::SHARD ||
(seq != 0 && app_.getShardStore()));

// probably not the right rule
if (app_.getOPs().isNeedNetworkLedger() &&
(reason != InboundLedger::Reason::GENERIC) &&
(reason != InboundLedger::Reason::CONSENSUS))
/* Acquiring ledgers is somewhat expensive. It requires lots of
* computation and network communication. Avoid it when it's not
* appropriate. Every validation from a peer for a ledger that
* we do not have locally results in a call to this function: even
* if we are moments away from validating the same ledger.
*
* When the following are all true, it is very likely that we will
* soon validate the ledger ourselves. Therefore, avoid acquiring
* ledgers from the network if:
* + Our mode is "full". It is very likely that we will build
* the ledger through the normal consensus process, and
* + Our latest ledger is close to the most recently validated ledger.
* Otherwise, we are likely falling behind the network because
* we have been closing ledgers that have not been validated, and
* + The requested ledger sequence is greater than our validated
* ledger, but not far into the future. Otherwise, it is either a
* request for an historical ledger or, if far into the future,
* likely we're quite behind and will benefit from acquiring it
* from the network.
*/
bool const isFull = app_.getOPs().isFull();
bool const fallingBehind = app_.getOPs().isFallingBehind();
LedgerIndex const validSeq =
app_.getLedgerMaster().getValidLedgerIndex();
constexpr std::size_t lagLeeway = 20;
bool const nearFuture =
(seq > validSeq) && (seq < validSeq + lagLeeway);
bool const shouldAcquire = !(isFull && !fallingBehind && nearFuture);
JLOG(j_.trace()) << "Evaluating whether to acquire ledger " << hash
<< ". full: " << (isFull ? "true" : "false")
<< ". falling behind: "
<< (fallingBehind ? "true" : "false")
<< ". ledger sequence " << seq
<< ". Valid sequence: " << validSeq
<< ". Lag leeway: " << lagLeeway
<< ". request for near future ledger: "
<< (nearFuture ? "true" : "false")
<< ". Acquiring ledger? "
<< (shouldAcquire ? "true" : "false");
if (!shouldAcquire)
return {};

bool isNew = true;
Expand Down
21 changes: 21 additions & 0 deletions src/ripple/app/misc/NetworkOPs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,8 @@ class NetworkOPsImp final : public NetworkOPs
clearLedgerFetch() override;
Json::Value
getLedgerFetchInfo() override;
bool
isFallingBehind() const override;
std::uint32_t
acceptLedger(
std::optional<std::chrono::milliseconds> consensusDelay) override;
Expand Down Expand Up @@ -713,6 +715,7 @@ class NetworkOPsImp final : public NetworkOPs
std::atomic<bool> amendmentBlocked_{false};
std::atomic<bool> amendmentWarned_{false};
std::atomic<bool> unlBlocked_{false};
std::atomic<bool> fallingBehind_{false};

ClosureCounter<void, boost::system::error_code const&> waitHandlerCounter_;
boost::asio::steady_timer heartbeatTimer_;
Expand Down Expand Up @@ -1826,6 +1829,18 @@ NetworkOPsImp::beginConsensus(uint256 const& networkClosed)
JLOG(m_journal.info()) << "Consensus time for #" << closingInfo.seq
<< " with LCL " << closingInfo.parentHash;

if (closingInfo.seq > m_ledgerMaster.getValidLedgerIndex() + 1)
{
fallingBehind_ = true;
JLOG(m_journal.warn()) << "Current ledger " << closingInfo.seq
<< "has advanced at least 2 past validated "
<< m_ledgerMaster.getValidLedgerIndex();
}
else
{
fallingBehind_ = false;
}

auto prevLedger = m_ledgerMaster.getLedgerByHash(closingInfo.parentHash);

if (!prevLedger)
Expand Down Expand Up @@ -2743,6 +2758,12 @@ NetworkOPsImp::getLedgerFetchInfo()
return app_.getInboundLedgers().getInfo();
}

bool
NetworkOPsImp::isFallingBehind() const
{
return fallingBehind_;
}

void
NetworkOPsImp::pubProposedTransaction(
std::shared_ptr<ReadView const> const& ledger,
Expand Down
2 changes: 2 additions & 0 deletions src/ripple/app/misc/NetworkOPs.h
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,8 @@ class NetworkOPs : public InfoSub::Source
clearLedgerFetch() = 0;
virtual Json::Value
getLedgerFetchInfo() = 0;
virtual bool
isFallingBehind() const = 0;

/** Accepts the current transaction tree, return the new ledger's sequence
Expand Down

0 comments on commit c3c37ff

Please sign in to comment.