Skip to content

Commit d4b5553

Browse files
committed
Merge bitcoin/bitcoin#30742: kernel: Use spans instead of vectors for passing block headers to validation functions
a2955f0 validation: Use span for ImportBlocks paths (TheCharlatan) 20515ea validation: Use span for CalculateClaimedHeadersWork (TheCharlatan) 52575e9 validation: Use span for ProcessNewBlockHeaders (TheCharlatan) Pull request description: Makes it friendlier for potential future users of the kernel library if they do not store the headers in a std::vector, but can guarantee contiguous memory. Take this opportunity to also change the argument of ImportBlocks previously taking a `std::vector` to a `std::span`. ACKs for top commit: stickies-v: re-ACK a2955f0 - no changes except further walking the ~file~ path of modernizing variable names. maflcko: ACK a2955f0 🕑 achow101: ACK a2955f0 danielabrozzoni: ACK a2955f0 Tree-SHA512: 8b07f4ad26e270b65600d1968cd78847b85caca5bfbb83fd9860389f26656b1d9a40b85e0990339f50403d18cedcd2456990054f3b8b0bedce943e50222d2709
2 parents fa5fc71 + a2955f0 commit d4b5553

File tree

9 files changed

+19
-16
lines changed

9 files changed

+19
-16
lines changed

src/net_processing.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4753,7 +4753,7 @@ void PeerManagerImpl::ProcessMessage(CNode& pfrom, const std::string& msg_type,
47534753
MaybeSendGetHeaders(pfrom, GetLocator(m_chainman.m_best_header), *peer);
47544754
}
47554755
return;
4756-
} else if (prev_block->nChainWork + CalculateClaimedHeadersWork({cmpctblock.header}) < GetAntiDoSWorkThreshold()) {
4756+
} else if (prev_block->nChainWork + CalculateClaimedHeadersWork({{cmpctblock.header}}) < GetAntiDoSWorkThreshold()) {
47574757
// If we get a low-work header in a compact block, we can ignore it.
47584758
LogDebug(BCLog::NET, "Ignoring low-work compact block from peer %d\n", pfrom.GetId());
47594759
return;
@@ -4766,7 +4766,7 @@ void PeerManagerImpl::ProcessMessage(CNode& pfrom, const std::string& msg_type,
47664766

47674767
const CBlockIndex *pindex = nullptr;
47684768
BlockValidationState state;
4769-
if (!m_chainman.ProcessNewBlockHeaders({cmpctblock.header}, /*min_pow_checked=*/true, state, &pindex)) {
4769+
if (!m_chainman.ProcessNewBlockHeaders({{cmpctblock.header}}, /*min_pow_checked=*/true, state, &pindex)) {
47704770
if (state.IsInvalid()) {
47714771
MaybePunishNodeForBlock(pfrom.GetId(), state, /*via_compact_block=*/true, "invalid header via cmpctblock");
47724772
return;
@@ -5070,7 +5070,7 @@ void PeerManagerImpl::ProcessMessage(CNode& pfrom, const std::string& msg_type,
50705070
mapBlockSource.emplace(hash, std::make_pair(pfrom.GetId(), true));
50715071

50725072
// Check claimed work on this block against our anti-dos thresholds.
5073-
if (prev_block && prev_block->nChainWork + CalculateClaimedHeadersWork({pblock->GetBlockHeader()}) >= GetAntiDoSWorkThreshold()) {
5073+
if (prev_block && prev_block->nChainWork + CalculateClaimedHeadersWork({{pblock->GetBlockHeader()}}) >= GetAntiDoSWorkThreshold()) {
50745074
min_pow_checked = true;
50755075
}
50765076
}

src/node/blockstorage.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1210,7 +1210,7 @@ class ImportingNow
12101210
}
12111211
};
12121212

1213-
void ImportBlocks(ChainstateManager& chainman, std::vector<fs::path> vImportFiles)
1213+
void ImportBlocks(ChainstateManager& chainman, std::span<const fs::path> import_paths)
12141214
{
12151215
ImportingNow imp{chainman.m_blockman.m_importing};
12161216

@@ -1245,7 +1245,7 @@ void ImportBlocks(ChainstateManager& chainman, std::vector<fs::path> vImportFile
12451245
}
12461246

12471247
// -loadblock=
1248-
for (const fs::path& path : vImportFiles) {
1248+
for (const fs::path& path : import_paths) {
12491249
AutoFile file{fsbridge::fopen(path, "rb")};
12501250
if (!file.IsNull()) {
12511251
LogPrintf("Importing blocks file %s...\n", fs::PathToString(path));

src/node/blockstorage.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include <memory>
3030
#include <optional>
3131
#include <set>
32+
#include <span>
3233
#include <string>
3334
#include <unordered_map>
3435
#include <utility>
@@ -429,7 +430,7 @@ class BlockManager
429430
void CleanupBlockRevFiles() const;
430431
};
431432

432-
void ImportBlocks(ChainstateManager& chainman, std::vector<fs::path> vImportFiles);
433+
void ImportBlocks(ChainstateManager& chainman, std::span<const fs::path> import_paths);
433434
} // namespace node
434435

435436
#endif // BITCOIN_NODE_BLOCKSTORAGE_H

src/rpc/mining.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1096,7 +1096,7 @@ static RPCHelpMan submitheader()
10961096
}
10971097

10981098
BlockValidationState state;
1099-
chainman.ProcessNewBlockHeaders({h}, /*min_pow_checked=*/true, state);
1099+
chainman.ProcessNewBlockHeaders({{h}}, /*min_pow_checked=*/true, state);
11001100
if (state.IsValid()) return UniValue::VNULL;
11011101
if (state.IsError()) {
11021102
throw JSONRPCError(RPC_VERIFY_ERROR, state.ToString());

src/test/blockfilter_index_tests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ bool BuildChainTestingSetup::BuildChain(const CBlockIndex* pindex,
103103
CBlockHeader header = block->GetBlockHeader();
104104

105105
BlockValidationState state;
106-
if (!Assert(m_node.chainman)->ProcessNewBlockHeaders({header}, true, state, &pindex)) {
106+
if (!Assert(m_node.chainman)->ProcessNewBlockHeaders({{header}}, true, state, &pindex)) {
107107
return false;
108108
}
109109
}

src/test/fuzz/utxo_snapshot.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ void initialize_chain()
5858
auto& chainman{*setup->m_node.chainman};
5959
for (const auto& block : chain) {
6060
BlockValidationState dummy;
61-
bool processed{chainman.ProcessNewBlockHeaders({*block}, true, dummy)};
61+
bool processed{chainman.ProcessNewBlockHeaders({{block->GetBlockHeader()}}, true, dummy)};
6262
Assert(processed);
6363
const auto* index{WITH_LOCK(::cs_main, return chainman.m_blockman.LookupBlockIndex(block->GetHash()))};
6464
Assert(index);
@@ -137,7 +137,7 @@ void utxo_snapshot_fuzz(FuzzBufferType buffer)
137137
if constexpr (!INVALID) {
138138
for (const auto& block : *g_chain) {
139139
BlockValidationState dummy;
140-
bool processed{chainman.ProcessNewBlockHeaders({*block}, true, dummy)};
140+
bool processed{chainman.ProcessNewBlockHeaders({{block->GetBlockHeader()}}, true, dummy)};
141141
Assert(processed);
142142
const auto* index{WITH_LOCK(::cs_main, return chainman.m_blockman.LookupBlockIndex(block->GetHash()))};
143143
Assert(index);

src/test/validation_block_tests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ std::shared_ptr<CBlock> MinerTestingSetup::FinalizeBlock(std::shared_ptr<CBlock>
101101
// submit block header, so that miner can get the block height from the
102102
// global state and the node has the topology of the chain
103103
BlockValidationState ignored;
104-
BOOST_CHECK(Assert(m_node.chainman)->ProcessNewBlockHeaders({pblock->GetBlockHeader()}, true, ignored));
104+
BOOST_CHECK(Assert(m_node.chainman)->ProcessNewBlockHeaders({{pblock->GetBlockHeader()}}, true, ignored));
105105

106106
return pblock;
107107
}

src/validation.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@
7070
#include <numeric>
7171
#include <optional>
7272
#include <ranges>
73+
#include <span>
7374
#include <string>
7475
#include <tuple>
7576
#include <utility>
@@ -4136,7 +4137,7 @@ bool IsBlockMutated(const CBlock& block, bool check_witness_root)
41364137
return false;
41374138
}
41384139

4139-
arith_uint256 CalculateClaimedHeadersWork(const std::vector<CBlockHeader>& headers)
4140+
arith_uint256 CalculateClaimedHeadersWork(std::span<const CBlockHeader> headers)
41404141
{
41414142
arith_uint256 total_work{0};
41424143
for (const CBlockHeader& header : headers) {
@@ -4384,7 +4385,7 @@ bool ChainstateManager::AcceptBlockHeader(const CBlockHeader& block, BlockValida
43844385
}
43854386

43864387
// Exposed wrapper for AcceptBlockHeader
4387-
bool ChainstateManager::ProcessNewBlockHeaders(const std::vector<CBlockHeader>& headers, bool min_pow_checked, BlockValidationState& state, const CBlockIndex** ppindex)
4388+
bool ChainstateManager::ProcessNewBlockHeaders(std::span<const CBlockHeader> headers, bool min_pow_checked, BlockValidationState& state, const CBlockIndex** ppindex)
43884389
{
43894390
AssertLockNotHeld(cs_main);
43904391
{

src/validation.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
#include <memory>
4040
#include <optional>
4141
#include <set>
42+
#include <span>
4243
#include <stdint.h>
4344
#include <string>
4445
#include <thread>
@@ -407,7 +408,7 @@ bool HasValidProofOfWork(const std::vector<CBlockHeader>& headers, const Consens
407408
bool IsBlockMutated(const CBlock& block, bool check_witness_root);
408409

409410
/** Return the sum of the claimed work on a given set of headers. No verification of PoW is done. */
410-
arith_uint256 CalculateClaimedHeadersWork(const std::vector<CBlockHeader>& headers);
411+
arith_uint256 CalculateClaimedHeadersWork(std::span<const CBlockHeader> headers);
411412

412413
enum class VerifyDBResult {
413414
SUCCESS,
@@ -1217,12 +1218,12 @@ class ChainstateManager
12171218
* May not be called in a
12181219
* validationinterface callback.
12191220
*
1220-
* @param[in] block The block headers themselves
1221+
* @param[in] headers The block headers themselves
12211222
* @param[in] min_pow_checked True if proof-of-work anti-DoS checks have been done by caller for headers chain
12221223
* @param[out] state This may be set to an Error state if any error occurred processing them
12231224
* @param[out] ppindex If set, the pointer will be set to point to the last new block index object for the given headers
12241225
*/
1225-
bool ProcessNewBlockHeaders(const std::vector<CBlockHeader>& block, bool min_pow_checked, BlockValidationState& state, const CBlockIndex** ppindex = nullptr) LOCKS_EXCLUDED(cs_main);
1226+
bool ProcessNewBlockHeaders(std::span<const CBlockHeader> headers, bool min_pow_checked, BlockValidationState& state, const CBlockIndex** ppindex = nullptr) LOCKS_EXCLUDED(cs_main);
12261227

12271228
/**
12281229
* Sufficiently validate a block for disk storage (and store on disk).

0 commit comments

Comments
 (0)