diff --git a/src/headerssync.cpp b/src/headerssync.cpp index ae7187f48b2..f966b13a922 100644 --- a/src/headerssync.cpp +++ b/src/headerssync.cpp @@ -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 // 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. * Validate and store commitments, and compare total chainwork to our target to * see if we can switch to REDOWNLOAD mode. */ -HeadersSyncState::ProcessingResult HeadersSyncState::ProcessNextHeaders(const - std::vector& received_headers, const bool full_headers_message) +HeadersSyncState::ProcessingResult HeadersSyncState::ProcessNextHeaders( + std::span received_headers, const bool full_headers_message) { ProcessingResult ret; @@ -137,7 +137,7 @@ HeadersSyncState::ProcessingResult HeadersSyncState::ProcessNextHeaders(const return ret; } -bool HeadersSyncState::ValidateAndStoreHeadersCommitments(const std::vector& headers) +bool HeadersSyncState::ValidateAndStoreHeadersCommitments(std::span headers) { // The caller should not give us an empty set of headers. Assume(headers.size() > 0); diff --git a/src/headerssync.h b/src/headerssync.h index 56380c66fe2..12afe66097b 100644 --- a/src/headerssync.h +++ b/src/headerssync.h @@ -165,7 +165,7 @@ public: * ProcessingResult.request_more: if true, the caller is suggested to call * NextHeadersRequestLocator and send a getheaders message using it. */ - ProcessingResult ProcessNextHeaders(const std::vector& + ProcessingResult ProcessNextHeaders(std::span received_headers, bool full_headers_message); /** Issue the next GETHEADERS message to our peer. @@ -195,7 +195,7 @@ private: * processed headers. * On failure, this invokes Finalize() and returns false. */ - bool ValidateAndStoreHeadersCommitments(const std::vector& headers); + bool ValidateAndStoreHeadersCommitments(std::span headers); /** In PRESYNC, process and update state for a single header */ bool ValidateAndProcessSingleHeader(const CBlockHeader& current); diff --git a/src/test/headers_sync_chainwork_tests.cpp b/src/test/headers_sync_chainwork_tests.cpp index db58970f8a5..511933284d6 100644 --- a/src/test/headers_sync_chainwork_tests.cpp +++ b/src/test/headers_sync_chainwork_tests.cpp @@ -92,16 +92,12 @@ BOOST_AUTO_TEST_CASE(headers_sync_state) std::unique_ptr hss; - std::vector headers_batch; - // Feed the first chain to HeadersSyncState, by delivering 1 header // 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)); - (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. - 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 // requirement. @@ -132,15 +128,13 @@ BOOST_AUTO_TEST_CASE(headers_sync_state) hss.reset(new HeadersSyncState(0, Params().GetConsensus(), chain_start, CHAIN_WORK)); BOOST_CHECK(hss->GetState() == HeadersSyncState::State::PRESYNC); // 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); - 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 // more headers can be requested. For a low-work-chain, this should causes // 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(result.pow_validated_headers.empty()); BOOST_CHECK(!result.request_more);