mirror of
https://github.com/bitcoin/bitcoin.git
synced 2026-06-16 17:48:59 +02:00
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:
@@ -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<CBlockHeader>& received_headers, const bool full_headers_message)
|
||||
HeadersSyncState::ProcessingResult HeadersSyncState::ProcessNextHeaders(
|
||||
std::span<const CBlockHeader> 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<CBlockHeader>& headers)
|
||||
bool HeadersSyncState::ValidateAndStoreHeadersCommitments(std::span<const CBlockHeader> headers)
|
||||
{
|
||||
// The caller should not give us an empty set of headers.
|
||||
Assume(headers.size() > 0);
|
||||
|
||||
@@ -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<CBlockHeader>&
|
||||
ProcessingResult ProcessNextHeaders(std::span<const CBlockHeader>
|
||||
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<CBlockHeader>& headers);
|
||||
bool ValidateAndStoreHeadersCommitments(std::span<const CBlockHeader> headers);
|
||||
|
||||
/** In PRESYNC, process and update state for a single header */
|
||||
bool ValidateAndProcessSingleHeader(const CBlockHeader& current);
|
||||
|
||||
@@ -92,16 +92,12 @@ BOOST_AUTO_TEST_CASE(headers_sync_state)
|
||||
|
||||
std::unique_ptr<HeadersSyncState> hss;
|
||||
|
||||
std::vector<CBlockHeader> 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);
|
||||
|
||||
Reference in New Issue
Block a user