Skip to content

Commit d4e11c1

Browse files
SirTysonclaude
andcommitted
Let out-of-sync nodes step past a detached checkpoint slot
A node that is out of sync learns the network state from peers' SCP-state responses, which carry the most recent slots plus the most recent checkpoint slot, with a gap between them that nothing will ever fill. Each slot is handed to SCP once its tx set is available, so when the checkpoint slot's tx set arrives first the node externalizes that slot and starts tracking it. Tracking mode then only consumes envelopes up to the next slot, which never comes, and the node sat idle until the consensus-stuck timer fired before it looked at the recent slots. When an out-of-sync node is tracking a slot for which no successor has ever been seen while later slots are ready, stop tracking, hand the smallest later slot to SCP, and drain the ready slots that follow. The lost-sync step matters because the driver rejects values for slots beyond the next tracked one while tracking. In-sync nodes are unaffected. Add a herder unit test that feeds a watcher externalize envelopes for a checkpoint slot and four later slots in both orders and checks it ends up tracking the latest slot with all five buffered. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
1 parent 012e62f commit d4e11c1

5 files changed

Lines changed: 199 additions & 0 deletions

File tree

‎src/herder/HerderImpl.cpp‎

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1056,6 +1056,7 @@ HerderImpl::processSCPQueue(bool synchronous)
10561056
return;
10571057
}
10581058
processSCPQueueUpToIndex(nextIndex);
1059+
processReadySlotsPastGaps();
10591060
};
10601061

10611062
if (synchronous)
@@ -1085,6 +1086,71 @@ HerderImpl::processSCPQueue(bool synchronous)
10851086
break;
10861087
}
10871088
}
1089+
// The slot that externalized may have no successor on the network
1090+
processReadySlotsPastGaps();
1091+
}
1092+
}
1093+
1094+
// An out of sync node learns the network's state from peers' SCP state
1095+
// responses, which carry the most recent slots plus the most recent checkpoint
1096+
// slot with a gap between the two that no one will ever fill (see
1097+
// getSCPStateForPeer). Each slot is handed to SCP once its tx set is
1098+
// available, so if the checkpoint slot's tx set arrives first the node
1099+
// externalizes that slot and starts tracking it. Tracking mode then only
1100+
// consumes envelopes up to the next slot, which never comes, and the node
1101+
// would sit idle until the consensus-stuck timer fires before looking at the
1102+
// recent slots. LedgerApplyManager buffers out-of-order ledgers regardless, so
1103+
// while out of sync, step over such gaps to the ready slots beyond them.
1104+
void
1105+
HerderImpl::processReadySlotsPastGaps()
1106+
{
1107+
ZoneScoped;
1108+
// Only while out of sync: an in-sync node must wait for the next slot.
1109+
auto outOfSync = [this]() {
1110+
return isTracking() && !mLedgerManager.isSynced() &&
1111+
!mLedgerManager.isApplying() && !mApp.isStopping();
1112+
};
1113+
while (outOfSync())
1114+
{
1115+
auto const nextIndex = nextConsensusLedgerIndex();
1116+
if (mPendingEnvelopes.hasEnvelopesForSlot(nextIndex))
1117+
{
1118+
// The network has, or had, messages for the next slot, so this is
1119+
// not a gap; the regular flow consumes them as they become ready.
1120+
return;
1121+
}
1122+
auto const ready = mPendingEnvelopes.readySlots(); // ascending
1123+
auto it = std::upper_bound(ready.begin(), ready.end(), nextIndex);
1124+
if (it == ready.end())
1125+
{
1126+
return;
1127+
}
1128+
CLOG_INFO(Herder,
1129+
"Out of sync at slot {} with no SCP messages for slot {}, "
1130+
"processing ready slot {}",
1131+
nextIndex - 1, nextIndex, *it);
1132+
// The tracked slot is not the network's latest, so this node is not
1133+
// tracking the network. Record that before handing SCP the later
1134+
// slot: while tracking, the driver rejects values for any slot beyond
1135+
// the next one.
1136+
lostSync();
1137+
processSCPQueueUpToIndex(*it);
1138+
if (!isTracking())
1139+
{
1140+
// Nothing externalized. Stay out of sync and let the recovery
1141+
// timer ask peers for more state, as herderOutOfSync does.
1142+
startOutOfSyncTimer();
1143+
return;
1144+
}
1145+
// Tracking again from the later slot. Slots that became ready while
1146+
// the node was stuck would have been consumed one at a time as they
1147+
// arrived, so drain the ones that now directly follow.
1148+
uint64 before;
1149+
do
1150+
{
1151+
before = nextConsensusLedgerIndex();
1152+
processSCPQueueUpToIndex(before);
1153+
} while (outOfSync() && nextConsensusLedgerIndex() != before);
10881154
}
10891155
}
10901156

‎src/herder/HerderImpl.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,9 @@ class HerderImpl : public Herder
287287
void broadcast(SCPEnvelope const& e);
288288

289289
void processSCPQueueUpToIndex(uint64 slotIndex);
290+
// While out of sync, step over slots for which no SCP messages exist to
291+
// the ready slots beyond them; see the definition for why.
292+
void processReadySlotsPastGaps();
290293
void newSlotExternalized(StellarValue const& value);
291294
void purgeOldSlots();
292295
void purgeOldPersistedTxSets();

‎src/herder/PendingEnvelopes.cpp‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -787,6 +787,20 @@ PendingEnvelopes::readySlots()
787787
return result;
788788
}
789789

790+
bool
791+
PendingEnvelopes::hasEnvelopesForSlot(uint64 slotIndex) const
792+
{
793+
auto it = mEnvelopes.find(slotIndex);
794+
if (it == mEnvelopes.end())
795+
{
796+
return false;
797+
}
798+
auto const& envs = it->second;
799+
return !envs.mDiscardedEnvelopes.empty() ||
800+
!envs.mProcessedEnvelopes.empty() ||
801+
!envs.mFetchingEnvelopes.empty() || !envs.mReadyEnvelopes.empty();
802+
}
803+
790804
void
791805
PendingEnvelopes::eraseOutsideRange(std::optional<uint64> minSlot,
792806
std::optional<uint64> maxSlot,

‎src/herder/PendingEnvelopes.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,9 @@ class PendingEnvelopes
210210

211211
std::vector<uint64> readySlots();
212212

213+
// whether any envelope for `slotIndex` has been received, in any state
214+
bool hasEnvelopesForSlot(uint64 slotIndex) const;
215+
213216
Json::Value getJsonInfo(size_t limit);
214217

215218
TxSetResult getTxSet(Hash const& hash);

‎src/herder/test/HerderTests.cpp‎

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3463,6 +3463,119 @@ TEST_CASE("SCP checkpoint", "[catchup][herder]")
34633463
}
34643464
}
34653465

3466+
// A node that is out of sync learns the network state from a peer's SCP
3467+
// state, which carries the most recent slots plus the earlier checkpoint slot
3468+
// with nothing in between (see HerderImpl::getSCPStateForPeer). Each slot is
3469+
// handed to SCP as soon as its tx set is available, so the checkpoint slot can
3470+
// become ready before the recent ones. The node then externalizes the
3471+
// checkpoint slot and tracks it; it must still go on to the recent slots
3472+
// rather than wait for a successor to the checkpoint slot that no one has.
3473+
TEST_CASE(
3474+
"out of sync node processes SCP state past a detached checkpoint slot",
3475+
"[herder][catchup]")
3476+
{
3477+
auto validator = SecretKey::pseudoRandomForTesting();
3478+
3479+
// A watcher that trusts only `validator`. FORCE_SCP is off so the node
3480+
// stays out of sync and learns everything from the envelopes below, and
3481+
// catchup is off so buffered ledgers remain buffered.
3482+
Config cfg = getTestConfig(0);
3483+
// 8-ledger checkpoints
3484+
cfg.ARTIFICIALLY_ACCELERATE_TIME_FOR_TESTING = true;
3485+
// Test configs default to manual close, under which the herder discards
3486+
// every SCP envelope
3487+
cfg.MANUAL_CLOSE = false;
3488+
cfg.FORCE_SCP = false;
3489+
cfg.NODE_IS_VALIDATOR = false;
3490+
cfg.MODE_DOES_CATCHUP = false;
3491+
cfg.QUORUM_SET.threshold = 1;
3492+
cfg.QUORUM_SET.validators.clear();
3493+
cfg.QUORUM_SET.validators.push_back(validator.getPublicKey());
3494+
3495+
VirtualClock clock;
3496+
auto app = createTestApplication(clock, cfg);
3497+
auto& herder = static_cast<HerderImpl&>(app->getHerder());
3498+
auto& lam =
3499+
static_cast<LedgerApplyManagerImpl&>(app->getLedgerApplyManager());
3500+
REQUIRE(!herder.isTracking());
3501+
3502+
// What a peer's SCP state contains: the checkpoint slot, a gap, and the
3503+
// slots in its retained window
3504+
uint32_t const checkpointSlot =
3505+
HistoryManager::firstLedgerAfterCheckpointContaining(1, cfg);
3506+
std::vector<uint32_t> const recentSlots = {
3507+
checkpointSlot + 2, checkpointSlot + 3, checkpointSlot + 4,
3508+
checkpointSlot + 5};
3509+
uint32_t const latestSlot = recentSlots.back();
3510+
3511+
SCPQuorumSet const& qSet = cfg.QUORUM_SET;
3512+
auto const qSetHash = xdrSha256(qSet);
3513+
auto const txSet = TxSetXDRFrame::makeEmpty(
3514+
app->getLedgerManager().getLastClosedLedgerHeader());
3515+
auto const now = VirtualClock::to_time_t(clock.system_now());
3516+
3517+
auto makeExternalize = [&](uint32_t slot) {
3518+
// Close times increase with the slot. They must be after the LCL's
3519+
// close time and no further than MAX_TIME_SLIP_SECONDS ahead of the
3520+
// clock, which for a virtual clock starts at zero.
3521+
uint64_t const closeTime = now + slot;
3522+
StellarValue sv = herder.makeStellarValue(
3523+
txSet->getContentsHash(), closeTime, emptyUpgradeSteps, validator);
3524+
SCPEnvelope envelope;
3525+
envelope.statement.nodeID = validator.getPublicKey();
3526+
envelope.statement.slotIndex = slot;
3527+
envelope.statement.pledges.type(SCP_ST_EXTERNALIZE);
3528+
auto& ext = envelope.statement.pledges.externalize();
3529+
ext.commit.counter = 1;
3530+
ext.commit.value = xdr::xdr_to_opaque(sv);
3531+
ext.nH = 1;
3532+
ext.commitQuorumSetHash = qSetHash;
3533+
herder.signEnvelope(validator, envelope);
3534+
return envelope;
3535+
};
3536+
// Supplying the qset and tx set makes the envelope ready at once, which
3537+
// is when the herder hands the slot to SCP
3538+
auto receive = [&](uint32_t slot) {
3539+
REQUIRE(herder.recvSCPEnvelope(makeExternalize(slot), qSet, txSet) ==
3540+
Herder::ENVELOPE_STATUS_READY);
3541+
};
3542+
auto bufferedLedgers = [&]() {
3543+
std::vector<uint32_t> seqs;
3544+
for (auto const& kv : lam.getBufferedLedgers())
3545+
{
3546+
seqs.push_back(kv.first);
3547+
}
3548+
return seqs;
3549+
};
3550+
3551+
SECTION("checkpoint slot becomes ready first")
3552+
{
3553+
receive(checkpointSlot);
3554+
REQUIRE(herder.isTracking());
3555+
REQUIRE(herder.trackingConsensusLedgerIndex() == checkpointSlot);
3556+
REQUIRE(bufferedLedgers() == std::vector<uint32_t>{checkpointSlot});
3557+
for (auto slot : recentSlots)
3558+
{
3559+
receive(slot);
3560+
}
3561+
}
3562+
3563+
SECTION("recent slots become ready first")
3564+
{
3565+
for (auto slot : recentSlots)
3566+
{
3567+
receive(slot);
3568+
}
3569+
REQUIRE(herder.trackingConsensusLedgerIndex() == latestSlot);
3570+
receive(checkpointSlot);
3571+
}
3572+
3573+
REQUIRE(herder.trackingConsensusLedgerIndex() == latestSlot);
3574+
std::vector<uint32_t> expected{checkpointSlot};
3575+
expected.insert(expected.end(), recentSlots.begin(), recentSlots.end());
3576+
REQUIRE(bufferedLedgers() == expected);
3577+
}
3578+
34663579
TEST_CASE("soroban txs each parameter surge priced", "[soroban][herder]")
34673580
{
34683581
auto networkID = sha256(getTestConfig().NETWORK_PASSPHRASE);

0 commit comments

Comments
 (0)