Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
448 changes: 413 additions & 35 deletions src/herder/HerderImpl.cpp

Large diffs are not rendered by default.

88 changes: 87 additions & 1 deletion src/herder/HerderImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "herder/LedgerCloseData.h"
#include "herder/PendingEnvelopes.h"
#include "herder/QuorumIntersectionChecker.h"
#include "herder/TxSetFrame.h"
#include "herder/Upgrades.h"
#include "ledger/LedgerHeaderUtils.h"
#include "overlay/NetworkConstants.h"
Expand Down Expand Up @@ -145,6 +146,7 @@ class HerderImpl : public Herder
std::function<std::vector<TransactionEnvelope>(size_t)>
mGetTopTransactionsForTesting;
friend class LeaderBuildTestAccess;
friend class ProposalPrevalidationTestAccess;

std::optional<uint32_t> mMaxClassicTxSize;
std::optional<uint32_t> mMaxTxSizeOverride;
Expand Down Expand Up @@ -273,8 +275,33 @@ class HerderImpl : public Herder
TxSetXDRFrameConstPtr txSet;
ApplicableTxSetFrameConstPtr applicableTxSet;
std::vector<Hash> invalidTxHashes;
bool capacityLimited;
};
// How long before the trigger the first-round leader builds its set, so
// that the set is built and on its way to the peers when consensus
// starts; what arrives in between waits for the next ledger.
// * At least a quarter of the target close time, but no more than
// 250 ms of a long one.
// * Long enough for the build this node expects (`expectedBuild`, see
// expectedTxSetBuildDuration; with a quarter to spare) plus the time
// the pushed set needs to reach the peers.
// * Never more than half of the target close time.
static std::chrono::milliseconds
prepareTxSetLeadTime(std::chrono::milliseconds expectedCloseTime,
std::chrono::milliseconds expectedBuild);
// How long this node's latest tx set build took (zero before the first),
// what it cost per transaction of the set it built (zero until it has
// built a set of some size), and how many transactions the last closed
// ledger's set held.
std::chrono::milliseconds mLastTxSetBuildDuration{0};
std::chrono::nanoseconds mTxSetBuildCostPerTx{0};
size_t mLastClosedTxSetSize{0};
// What the next build is expected to take: a build like this node's last
// one, or what a set the size of the last closed ledger's costs at this
// node's rate, whichever is longer. The second matters for a node that
// has not built for a while: its last build may be from before the load
// rose (every node's first term under load starts that way), and a lead
// time sized for it would make the first ledger of the term late.
std::chrono::milliseconds expectedTxSetBuildDuration() const;
std::optional<PreparedTxSet> mPreparedTxSet;
// `pushToPeers`: this is the set the node is about to propose as a round
// leader, so the overlay sends it to every peer as soon as its XDR exists
Expand Down Expand Up @@ -328,6 +355,65 @@ class HerderImpl : public Herder
std::vector<size_t> const& pendingPerPhase);
std::shared_ptr<SignaturePrewarm> mSignaturePrewarm;

// Pre-validation of the next ledger's candidates.
//
// A node that expects to lead ledger N+1 (in round 1, or in round 2 if
// the first leader times out) starts validating its candidates as soon as
// it has executed ledger N speculatively, while N is still going through
// consensus: background tasks run `checkValid` on the mempool's top
// transactions against the state that execution left (see
// ExecutedLedgerState). When N has closed with the value that was
// executed, the build of N+1 takes those results instead of validating
// the same transactions on the main thread (see mValidationCache).
struct ProposalPrevalidation
{
// The executed ledger (N) and its value: the results hold on top of
// N if and only if N closes with this value.
uint32_t ledgerSeq{0};
StellarValue value;
std::chrono::steady_clock::time_point start;
std::atomic<bool> stop{false};
// Decoded and selected once, by whichever task gets there first.
std::once_flag selected;
std::vector<TransactionFrameBasePtr> candidates;
// Guards everything below. Tasks hand over their results in small
// batches, so whoever stops the job takes what is there and never
// waits for a task: nothing is published after `stop` is set.
std::mutex mutex;
std::vector<std::pair<Hash, TxValidationResult>> published;
size_t pendingTasks{0};
size_t activeTasks{0};
// Nanoseconds from `start` until the last task finished, or -1.
std::atomic<int64_t> finishedAfterNs{-1};
};
std::shared_ptr<ProposalPrevalidation> mProposalPrevalidation;
// Whether the local node would lead round 1 or 2 of the ledger after
// `slotIndex` if `slotIndex` closes with `value`.
bool mayLeadAfter(uint64_t slotIndex, StellarValue const& value,
bool appliedTransactions);
// Called on the main thread when the speculative execution of a ledger
// has finished (`state` is null if it was not executed).
void startProposalPrevalidation(
std::shared_ptr<ExecutedLedgerState const> state);
// Stops the running pre-validation, if any. With `lcl`, its results are
// moved into mValidationCache if they hold on top of `lcl`.
void stopProposalPrevalidation(LedgerHeaderHistoryEntry const* lcl);

// Validation results for candidates of the next ledger, computed against
// the last closed ledger `lclHash`: by the pre-validation above, and by
// every build on that ledger. A later build on the same ledger (the build
// at the trigger after an early one, a later round's) only validates what
// it has not seen.
struct ValidationCache
{
Hash lclHash;
TxValidationCache results;
};
ValidationCache mValidationCache;
// The cache for a build on top of `lcl`, emptied if it was for another
// ledger and topped up with the pre-validation's results.
TxValidationCache* validationCacheFor(LedgerHeaderHistoryEntry const& lcl);

// Tx sets the overlay holds on stable storage: received from it, or
// handed to it after building them. persistSCPState leaves those out of
// the database; after a restart they come back from the overlay (see
Expand Down
93 changes: 90 additions & 3 deletions src/herder/HerderSCPDriver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1080,15 +1080,18 @@ HerderSCPDriver::getLeaderSchedule(uint64_t slotIndex)
auto const& lcl = mLedgerManager.getLastClosedLedgerHeader();
if (slotIndex != static_cast<uint64_t>(lcl.header.ledgerSeq) + 1)
{
// The seed is the hash of the previous ledger's header: unknown for
// any slot but the next one.
// The schedule is derived from the previous ledger's header (who
// closed it, and whether it applied): unknown for any slot but the
// next one. See getExpectedLeaderSchedule for the slot after that.
return empty;
}
// Force a pending rebuild of the transitive quorum, if any
mPendingEnvelopes.isNodeDefinitelyInQuorum(mSCP.getLocalNodeID());
auto const& quorum = mPendingEnvelopes.getCurrentlyTrackedQuorum();
auto& cache = mLeaderSchedule;
bool const haveResults = ledgersHaveResults();
if (cache.mSlotIndex != slotIndex || cache.mSeed != lcl.hash ||
cache.mLedgersHaveResults != haveResults ||
cache.mQuorumSize != quorum.size() ||
cache.mQuorumRebuildCount != mPendingEnvelopes.getQuorumRebuildCount())
{
Expand All @@ -1098,7 +1101,9 @@ HerderSCPDriver::getLeaderSchedule(uint64_t slotIndex)
{
nodes.emplace_back(n.first);
}
cache.mSchedule = computeLeaderSchedule(lcl.hash, std::move(nodes));
cache.mSchedule = computeNextLeaderSchedule(
lcl.header, std::move(nodes), haveResults);
cache.mLedgersHaveResults = haveResults;
cache.mSlotIndex = slotIndex;
cache.mSeed = lcl.hash;
cache.mQuorumSize = quorum.size();
Expand All @@ -1107,6 +1112,46 @@ HerderSCPDriver::getLeaderSchedule(uint64_t slotIndex)
return cache.mSchedule;
}

bool
HerderSCPDriver::ledgersHaveResults() const
{
#ifdef BUILD_TESTS
// Overlay-only mode closes ledgers without applying them.
return !mApp.getRunInOverlayOnlyMode();
#else
return true;
#endif
}

std::vector<NodeID>
HerderSCPDriver::getExpectedLeaderSchedule(uint64_t slotIndex,
StellarValue const& closingValue,
bool appliedTransactions)
{
releaseAssert(threadIsMain());
auto const& lcl = mLedgerManager.getLastClosedLedgerHeader();
if (slotIndex != static_cast<uint64_t>(lcl.header.ledgerSeq) + 2)
{
return {};
}
// The tracked quorum as it is: this is called while SCP emits an
// envelope, where a pending rebuild (database reads) does not belong,
// and a prediction made on a slightly older node set is still one.
auto const& quorum = mPendingEnvelopes.getCurrentlyTrackedQuorum();
std::vector<NodeID> nodes;
nodes.reserve(quorum.size());
for (auto const& n : quorum)
{
nodes.emplace_back(n.first);
}
// The ledger in between (LCL+1) will name the LCL's hash as its previous
// ledger hash: that is the seed of the slot after it.
return computeLeaderSchedule(
lcl.hash, std::move(nodes),
leaderTermOutcome(lcl.header.ledgerSeq + 1, closingValue,
appliedTransactions || !ledgersHaveResults()));
}

std::optional<NodeID>
HerderSCPDriver::getRoundLeader(uint64 slotIndex, uint32 round)
{
Expand Down Expand Up @@ -1177,6 +1222,48 @@ HerderSCPDriver::measureAndRecordBallotBlockedOnTxSet(uint64_t slotIndex,
mSCPMetrics.mBallotBlockedOnTxSet.Update(std::chrono::milliseconds(0));
}

ValueWrapperPtr
HerderSCPDriver::reproposeValue(uint64_t slotIndex, uint32 round,
Value const& value)
{
// The leader of a later round keeps the value it already votes for (its
// transaction set is known to the nodes that voted for it), but puts it
// forward under its own signature: the node whose value closes a ledger
// leads the next one (see leaderTermOutcome), and a leader whose round
// timed out should not be that node. SCP only asks while the node is
// not tied to the value (it has not accepted it as prepared); after
// that the value, and with it the term, stays as it is.
StellarValue sv;
if (round <= 1 || !toStellarValue(value, sv) ||
sv.ext.v() == STELLAR_VALUE_BASIC || isEmptyTxSetStellarValue(sv))
{
return nullptr;
}
auto const& self = mSCP.getLocalNodeID();
if (getLcValueSignature(sv).nodeID == self)
{
return nullptr;
}
// Only a set this node holds: its signature makes it answerable for the
// set (a ledger that applies nothing costs the signer its term).
auto txSet = mPendingEnvelopes.getTxSet(sv.txSetHash);
if (auto const* frame = std::get_if<TxSetXDRFrameConstPtr>(&txSet);
!frame || !*frame)
{
return nullptr;
}
mApp.getMetrics().NewMeter({"scp", "leader", "resigned"}, "value").Mark();
auto resigned =
mHerder.makeStellarValue(sv.txSetHash, getConsensusTime(sv),
sv.upgrades, mApp.getConfig().NODE_SEED);
CLOG_INFO(Herder,
"Slot {} round {}: proposing the value of {} under our own "
"signature",
slotIndex, round,
mApp.getConfig().toShortString(getLcValueSignature(sv).nodeID));
return wrapStellarValue(resigned);
}

void
HerderSCPDriver::proposedValue(uint64_t slotIndex, uint32 round,
Value const& value)
Expand Down
19 changes: 16 additions & 3 deletions src/herder/HerderSCPDriver.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,9 @@ class HerderSCPDriver : public SCPDriver
void valueExternalized(uint64_t slotIndex, Value const& value) override;

// Deterministic leader schedule: the leader of `round` for `slotIndex`.
// The schedule is a permutation of the transitive quorum seeded by the
// hash of the last closed ledger header, so it is only defined for the
// next ledger to close (LCL+1).
// The schedule is derived from the header of the last closed ledger and
// the transitive quorum (see computeNextLeaderSchedule), so it is only
// defined for the next ledger to close (LCL+1).
bool isProposalPending(uint64 slotIndex,
NodeID const& leader) const override;
std::optional<NodeID> getRoundLeader(uint64 slotIndex,
Expand All @@ -122,6 +122,18 @@ class HerderSCPDriver : public SCPDriver
std::vector<NodeID> const& getLeaderSchedule(uint64_t slotIndex);
// True if the local node leads `round` of slotIndex.
bool isLocalNodeRoundLeader(uint64_t slotIndex, uint32_t round);
// The schedule `slotIndex` = LCL+2 will have if LCL+1 closes with
// `closingValue` and applies transactions or not. Empty for any other
// slot. This is a prediction: nothing in consensus depends on it.
std::vector<NodeID>
getExpectedLeaderSchedule(uint64_t slotIndex,
StellarValue const& closingValue,
bool appliedTransactions);
// False when ledgers close without recording transaction results (the
// test-only overlay-only mode).
bool ledgersHaveResults() const;
ValueWrapperPtr reproposeValue(uint64_t slotIndex, uint32 round,
Value const& value) override;

// Update metrics indicating that a value was replaced with an empty-tx-set
// value for slotIndex.
Expand Down Expand Up @@ -338,6 +350,7 @@ class HerderSCPDriver : public SCPDriver
Hash mSeed;
size_t mQuorumSize{0};
uint64_t mQuorumRebuildCount{0};
bool mLedgersHaveResults{true};
std::vector<NodeID> mSchedule;
};
LeaderScheduleCache mLeaderSchedule;
Expand Down
66 changes: 66 additions & 0 deletions src/herder/LeaderSchedule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include "herder/LeaderSchedule.h"
#include "crypto/SHA.h"
#include "ledger/LedgerHeaderUtils.h"
#include "util/XDROperators.h"
#include "xdrpp/marshal.h"
#include <algorithm>
Expand Down Expand Up @@ -35,4 +36,69 @@ computeLeaderSchedule(Hash const& seed, std::vector<NodeID> nodes)
}
return res;
}

LeaderTermOutcome
leaderTermOutcome(uint32_t ledgerSeq, StellarValue const& value,
bool appliedTransactions)
{
LeaderTermOutcome outcome;
if (value.ext.v() == STELLAR_VALUE_BASIC)
{
return outcome;
}
auto const& signer = getLcValueSignature(value).nodeID;
if (isEmptyTxSetStellarValue(value) || !appliedTransactions)
{
outcome.mForfeited = signer;
}
else if ((static_cast<uint64_t>(ledgerSeq) + 1) % LEADER_TERM_LEDGERS != 0)
{
outcome.mIncumbent = signer;
}
return outcome;
}

LeaderTermOutcome
leaderTermOutcome(LedgerHeader const& lcl, bool ledgersHaveResults)
{
static Hash const emptyResults = xdrSha256(TransactionResultSet{});
return leaderTermOutcome(lcl.ledgerSeq, lcl.scpValue,
!ledgersHaveResults ||
lcl.txSetResultHash != emptyResults);
}

std::vector<NodeID>
computeLeaderSchedule(Hash const& seed, std::vector<NodeID> nodes,
LeaderTermOutcome const& outcome)
{
auto schedule = computeLeaderSchedule(seed, std::move(nodes));
// Both keep the seeded order of everyone else.
if (outcome.mIncumbent)
{
auto it =
std::find(schedule.begin(), schedule.end(), *outcome.mIncumbent);
if (it != schedule.end())
{
std::rotate(schedule.begin(), it, it + 1);
}
}
if (outcome.mForfeited)
{
auto it =
std::find(schedule.begin(), schedule.end(), *outcome.mForfeited);
if (it != schedule.end())
{
std::rotate(it, it + 1, schedule.end());
}
}
return schedule;
}

std::vector<NodeID>
computeNextLeaderSchedule(LedgerHeader const& lcl, std::vector<NodeID> nodes,
bool ledgersHaveResults)
{
return computeLeaderSchedule(lcl.previousLedgerHash, std::move(nodes),
leaderTermOutcome(lcl, ledgersHaveResults));
}
}
Loading
Loading