diff --git a/csnode/include/csnode/backwardsync.hpp b/csnode/include/csnode/backwardsync.hpp index 82a7e6d1c..62d97f638 100644 --- a/csnode/include/csnode/backwardsync.hpp +++ b/csnode/include/csnode/backwardsync.hpp @@ -100,6 +100,9 @@ class BackwardSync { static constexpr uint64_t kStoreRetryMs = 60'000; static constexpr size_t kFlushBlocks = 10'000; static constexpr uint64_t kFlushMs = 60'000; + // a flush (up to 2.4 s on mainnet) waits while the node is a confidant, at most this long: blocks + // not flushed yet are fetched again after a crash, nothing else is lost + static constexpr uint64_t kTrustedFlushDeferMs = 600'000; static constexpr size_t kProgressBlocks = 100'000; static constexpr uint64_t kSlowTurnMs = 200; // a longer turn is logged, with where its time went diff --git a/csnode/src/backwardsync.cpp b/csnode/src/backwardsync.cpp index 95b2f37c6..ae122a7fb 100644 --- a/csnode/src/backwardsync.cpp +++ b/csnode/src/backwardsync.cpp @@ -597,6 +597,9 @@ void BackwardSync::flushIfDue(bool force) { if (!force && storedSinceFlush_ < kFlushBlocks && t < lastFlushAt_ + kFlushMs) { return; } + if (!force && trusted_ && t < lastFlushAt_ + kTrustedFlushDeferMs) { + return; // consensus first: flushed at the first turn outside the trusted set + } // on the processor thread too, outside the turn budget: timed, so that a slow disk shows const auto flushAt = std::chrono::steady_clock::now(); const bool ok = !hooks_.flush || hooks_.flush(); diff --git a/docs/CHANGES.md b/docs/CHANGES.md index eeb605443..c869529ab 100644 --- a/docs/CHANGES.md +++ b/docs/CHANGES.md @@ -66,6 +66,7 @@ lines under the new release. | #144 | csnode: stream reads bounded by the data: `IDataStream` checks a length without overflow, a vector reserves no more than the bytes left and stops at the first missing element, a smart stage-one message whose fee count exceeds its size or that lacks its hash is refused, the API checks a string field's length before reading it | | #145 | csnode: backward sync asks for no more blocks than its buffer holds (a request could overrun it by one batch); blocks a reply brings when the buffer is full are asked for again at once and no longer count against the peer that sent them (with slow stores, on mainnet after a restart, peers that had answered were left out for an hour and the backfill fell to ~30 blocks/s) | | #146 | csnode: the backfill turn budget depends on the node's role: 50 ms while it is a confidant (`backfill_turn_budget_ms`), 90 ms otherwise (`backfill_idle_turn_budget_ms`); on mainnet a node outside the trusted set backfills at about 450 blocks/s instead of about 250 | +| #147 | csnode: while the node is a confidant, the backfill storage flush (median 358 ms, up to 2.4 s on mainnet, on the processor thread) waits until the node is out of the trusted set, 10 minutes at most; blocks not flushed are fetched again after a crash | ## community.14 — 2026-10-27 diff --git a/tests/csnode/backwardsync_tests.cpp b/tests/csnode/backwardsync_tests.cpp index 4d1cab6fe..6bbcf14b8 100644 --- a/tests/csnode/backwardsync_tests.cpp +++ b/tests/csnode/backwardsync_tests.cpp @@ -591,6 +591,33 @@ TEST_F(BackfillTest, AConfidantTakesTheShorterBudget) { EXPECT_EQ(kFirst - first_, 8u); // trusted: 20 ms, two more } +// a confidant does not hold the processor thread with a flush: it waits until the node is out of +// the trusted set, or kTrustedFlushDeferMs at most +TEST_F(BackfillTest, AConfidantDefersTheFlush) { + make(settingsOf(100, 0, 50)); + tick(); + sync_->onBlocks(requests_[0].first, blocksFor(requests_[0].second)); + ASSERT_EQ(kFirst - first_, 50u); + const size_t before = flushes_; + trusted_ = true; + tick(cs::BackwardSync::kFlushMs + 1); // stores 50 more; the flush is due, but deferred + ASSERT_EQ(kFirst - first_, 100u); + EXPECT_EQ(flushes_, before) << "deferred while trusted"; + trusted_ = false; + tick(); + EXPECT_EQ(flushes_, before + 1) << "flushed once out of the trusted set"; + + // trusted for good: flushed once the deferral runs out + answerAll(); + const cs::Sequence stored = first_; + ASSERT_LT(stored, kFirst - 100) << "more blocks stored since the flush"; + trusted_ = true; + tick(cs::BackwardSync::kFlushMs + 1); + EXPECT_EQ(flushes_, before + 1); + tick(cs::BackwardSync::kTrustedFlushDeferMs); + EXPECT_EQ(flushes_, before + 2); +} + TEST_F(BackfillTest, ASlowTurnStopsAtItsTimeBudget) { auto settings = settingsOf(100, 0, 50); settings.idleTurnBudgetMs = 30;