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
3 changes: 3 additions & 0 deletions csnode/include/csnode/backwardsync.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
3 changes: 3 additions & 0 deletions csnode/src/backwardsync.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
1 change: 1 addition & 0 deletions docs/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
27 changes: 27 additions & 0 deletions tests/csnode/backwardsync_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Loading