From 1e18a9c5d868657c5b5d68d6f3305bf82f7d80c3 Mon Sep 17 00:00:00 2001 From: EnzinoBB Date: Sat, 31 Oct 2026 13:47:08 +0100 Subject: [PATCH] csnode: backward sync never asks for more than its buffer holds The request window let the blocks asked for and not stored reach the buffer plus one batch, so with slow stores a reply could meet a full buffer. Its extra blocks were dropped and left missing until the batch timed out, which counted against the peer that had sent them: on mainnet, after a restart with slow first stores and the 50 ms turn budget, most peers were left out for an hour and the backfill fell to about 30 blocks/s. A batch is now asked for only when it fits, and blocks without room are asked for again at once without blaming the peer. Co-Authored-By: Claude Opus 5.5 --- csnode/src/backwardsync.cpp | 14 ++++++++++++-- docs/CHANGES.md | 1 + tests/csnode/backwardsync_tests.cpp | 18 ++++++++++++++++++ 3 files changed, 31 insertions(+), 2 deletions(-) diff --git a/csnode/src/backwardsync.cpp b/csnode/src/backwardsync.cpp index 616c15482..d47e16efb 100644 --- a/csnode/src/backwardsync.cpp +++ b/csnode/src/backwardsync.cpp @@ -206,7 +206,9 @@ void BackwardSync::request(const Context& ctx) { requestedAll_ = true; // the floor rose (pruning) past what is left to ask for break; } - if (next_ - nextToRequest_ >= window) { + // what is asked for and not stored yet, this batch included, fits in the buffer: a reply + // never meets a full buffer however slowly the blocks are stored + if (next_ - nextToRequest_ + settings_.batch > window) { break; } const Sequence hi = nextToRequest_; @@ -299,6 +301,7 @@ void BackwardSync::onBlocks(const PublicKey& sender, PoolsBlock&& blocks) { } Batch& batch = it->second; const size_t limit = kBufferBatches * settings_.batch; + std::set noRoom; // sent, but not kept: asked for again, not held against the peer for (auto& block : blocks) { const Sequence seq = block.sequence(); if (!block.is_valid() || batch.missing.count(seq) == 0) { @@ -306,8 +309,8 @@ void BackwardSync::onBlocks(const PublicKey& sender, PoolsBlock&& blocks) { continue; } if (buffer_.size() >= limit && buffer_.count(seq) == 0) { - // left missing: asked for again when the batch times out ++dropped_; + noRoom.insert(seq); if (!bufferFullLogged_) { bufferFullLogged_ = true; log("BACKFILL: " + std::to_string(buffer_.size()) + " blocks wait to be stored: the others of " + keyText(sender) + @@ -318,6 +321,13 @@ void BackwardSync::onBlocks(const PublicKey& sender, PoolsBlock&& blocks) { buffer_[seq] = Buffered{std::move(block), sender}; batch.missing.erase(seq); } + if (!noRoom.empty()) { + for (const Sequence seq : noRoom) { + batch.missing.erase(seq); + } + auto& again = retry_[*noRoom.rbegin()]; + again.insert(noRoom.begin(), noRoom.end()); + } if (batch.missing.empty()) { inFlight_.erase(it); auto& state = peers_[sender]; diff --git a/docs/CHANGES.md b/docs/CHANGES.md index d08784ea3..c4721869c 100644 --- a/docs/CHANGES.md +++ b/docs/CHANGES.md @@ -64,6 +64,7 @@ lines under the new release. | #142 | node: a `NodeStopRequest` from the network no longer stops the node (its signature was not checked since upstream `af4de3f8`, so it was not authenticated); it is counted and logged; a node too old for the network still stops by the special order of the version change | | #143 | node: a `Utility` message (ban or unban a node key) is obeyed only with the blockchain key's signature, an exact length and a round at most 100 rounds old (its signature was not checked since upstream `af4de3f8`, so it was not authenticated) | | #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) | ## community.14 — 2026-10-27 diff --git a/tests/csnode/backwardsync_tests.cpp b/tests/csnode/backwardsync_tests.cpp index 7e41ffb4b..3a9bbfa13 100644 --- a/tests/csnode/backwardsync_tests.cpp +++ b/tests/csnode/backwardsync_tests.cpp @@ -555,6 +555,24 @@ TEST_F(BackfillTest, TheWorkPerTurnIsBounded) { // a slow store ends the turn once it has used its time, however many blocks it may still store; // such a turn is logged with where its time went +// stores slower than the replies arrive: the requests never ask for more than the buffer holds, +// so no block is dropped and no peer that answered is taken for one that did not +TEST_F(BackfillTest, SlowStoresNeitherDropBlocksNorMarkPeers) { + auto settings = settingsOf(20, 0, 50); + settings.turnBudgetMs = 30; + make(settings); + storeDelayMs_ = 12; // three blocks a turn + for (int i = 0; i < 400 && !sync_->stats().done; ++i) { + tick(100); + answerAll(); + } + EXPECT_TRUE(sync_->stats().done); + EXPECT_EQ(first_, 0u); + EXPECT_EQ(sync_->stats().dropped, 0u); + EXPECT_EQ(logged("are dropped"), 0u); + EXPECT_EQ(logged("did not answer"), 0u); +} + TEST_F(BackfillTest, ASlowTurnStopsAtItsTimeBudget) { auto settings = settingsOf(100, 0, 50); settings.turnBudgetMs = 30;