refactor(headerssync): Process spans of headers

More lightweight than vectors which needed to be copied in tests. Also good to get rid of headers_batch-vector before breaking up test.
This commit is contained in:
Hodlinator committed 2025-09-12 22:28:41 +02:00
1 parent a4ac9915a9
commit e984618d0b
3 files changed
+10 -16

No files matched your search

+4 -4
View File
@@ -1,4 +1,4 @@
// Copyright (c) 2022 The Bitcoin Core developers // Copyright (c) 2022-present The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying // Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php. // file COPYING or http://www.opensource.org/licenses/mit-license.php.
@@ -66,8 +66,8 @@ void HeadersSyncState::Finalize()
/** Process the next batch of headers received from our peer. /** Process the next batch of headers received from our peer.
* Validate and store commitments, and compare total chainwork to our target to * Validate and store commitments, and compare total chainwork to our target to
* see if we can switch to REDOWNLOAD mode. */ * see if we can switch to REDOWNLOAD mode. */
HeadersSyncState::ProcessingResult HeadersSyncState::ProcessNextHeaders(const HeadersSyncState::ProcessingResult HeadersSyncState::ProcessNextHeaders(
std::vector<CBlockHeader>& received_headers, const bool full_headers_message) std::span<const CBlockHeader> received_headers, const bool full_headers_message)
{ {
ProcessingResult ret; ProcessingResult ret;
@@ -137,7 +137,7 @@ HeadersSyncState::ProcessingResult HeadersSyncState::ProcessNextHeaders(const
return ret; return ret;
} }
bool HeadersSyncState::ValidateAndStoreHeadersCommitments(const std::vector<CBlockHeader>& headers) bool HeadersSyncState::ValidateAndStoreHeadersCommitments(std::span<const CBlockHeader> headers)
{ {
// The caller should not give us an empty set of headers. // The caller should not give us an empty set of headers.
Assume(headers.size() > 0); Assume(headers.size() > 0);
+2 -2
View File
@@ -165,7 +165,7 @@ public:
* ProcessingResult.request_more: if true, the caller is suggested to call * ProcessingResult.request_more: if true, the caller is suggested to call
* NextHeadersRequestLocator and send a getheaders message using it. * NextHeadersRequestLocator and send a getheaders message using it.
*/ */
ProcessingResult ProcessNextHeaders(const std::vector<CBlockHeader>& ProcessingResult ProcessNextHeaders(std::span<const CBlockHeader>
received_headers, bool full_headers_message); received_headers, bool full_headers_message);
/** Issue the next GETHEADERS message to our peer. /** Issue the next GETHEADERS message to our peer.
@@ -195,7 +195,7 @@ private:
* processed headers. * processed headers.
* On failure, this invokes Finalize() and returns false. * On failure, this invokes Finalize() and returns false.
*/ */
bool ValidateAndStoreHeadersCommitments(const std::vector<CBlockHeader>& headers); bool ValidateAndStoreHeadersCommitments(std::span<const CBlockHeader> headers);
/** In PRESYNC, process and update state for a single header */ /** In PRESYNC, process and update state for a single header */
bool ValidateAndProcessSingleHeader(const CBlockHeader& current); bool ValidateAndProcessSingleHeader(const CBlockHeader& current);
+4 -10
View File
@@ -92,16 +92,12 @@ BOOST_AUTO_TEST_CASE(headers_sync_state)
std::unique_ptr<HeadersSyncState> hss; std::unique_ptr<HeadersSyncState> hss;
std::vector<CBlockHeader> headers_batch;
// Feed the first chain to HeadersSyncState, by delivering 1 header // Feed the first chain to HeadersSyncState, by delivering 1 header
// initially and then the rest. // initially and then the rest.
headers_batch.insert(headers_batch.end(), std::next(first_chain.begin()), first_chain.end());
hss.reset(new HeadersSyncState(0, Params().GetConsensus(), chain_start, CHAIN_WORK)); hss.reset(new HeadersSyncState(0, Params().GetConsensus(), chain_start, CHAIN_WORK));
(void)hss->ProcessNextHeaders({first_chain.front()}, true); (void)hss->ProcessNextHeaders({{first_chain.front()}}, true);
// Pretend the first header is still "full", so we don't abort. // Pretend the first header is still "full", so we don't abort.
auto result = hss->ProcessNextHeaders(headers_batch, true); auto result = hss->ProcessNextHeaders(std::span{first_chain}.subspan(1), true);
// This chain should look valid, and we should have met the proof-of-work // This chain should look valid, and we should have met the proof-of-work
// requirement. // requirement.
@@ -132,15 +128,13 @@ BOOST_AUTO_TEST_CASE(headers_sync_state)
hss.reset(new HeadersSyncState(0, Params().GetConsensus(), chain_start, CHAIN_WORK)); hss.reset(new HeadersSyncState(0, Params().GetConsensus(), chain_start, CHAIN_WORK));
BOOST_CHECK(hss->GetState() == HeadersSyncState::State::PRESYNC); BOOST_CHECK(hss->GetState() == HeadersSyncState::State::PRESYNC);
// Pretend just the first message is "full", so we don't abort. // Pretend just the first message is "full", so we don't abort.
(void)hss->ProcessNextHeaders({second_chain.front()}, true); (void)hss->ProcessNextHeaders({{second_chain.front()}}, true);
BOOST_CHECK(hss->GetState() == HeadersSyncState::State::PRESYNC); BOOST_CHECK(hss->GetState() == HeadersSyncState::State::PRESYNC);
headers_batch.clear();
headers_batch.insert(headers_batch.end(), std::next(second_chain.begin(), 1), second_chain.end());
// Tell the sync logic that the headers message was not full, implying no // Tell the sync logic that the headers message was not full, implying no
// more headers can be requested. For a low-work-chain, this should causes // more headers can be requested. For a low-work-chain, this should causes
// the sync to end with no headers for acceptance. // the sync to end with no headers for acceptance.
result = hss->ProcessNextHeaders(headers_batch, false); result = hss->ProcessNextHeaders(std::span{second_chain}.subspan(1), false);
BOOST_CHECK(hss->GetState() == HeadersSyncState::State::FINAL); BOOST_CHECK(hss->GetState() == HeadersSyncState::State::FINAL);
BOOST_CHECK(result.pow_validated_headers.empty()); BOOST_CHECK(result.pow_validated_headers.empty());
BOOST_CHECK(!result.request_more); BOOST_CHECK(!result.request_more);