From 20515ea3f5bd426f6e3746cf5cddd2324dacae31 Mon Sep 17 00:00:00 2001 From: TheCharlatan Date: Wed, 28 Aug 2024 15:10:15 +0200 Subject: [PATCH] validation: Use span for CalculateClaimedHeadersWork 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. --- src/net_processing.cpp | 4 ++-- src/validation.cpp | 2 +- src/validation.h | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/net_processing.cpp b/src/net_processing.cpp index 1d3a0095255b5..7a4f4de23df2e 100644 --- a/src/net_processing.cpp +++ b/src/net_processing.cpp @@ -4751,7 +4751,7 @@ void PeerManagerImpl::ProcessMessage(CNode& pfrom, const std::string& msg_type, MaybeSendGetHeaders(pfrom, GetLocator(m_chainman.m_best_header), *peer); } return; - } else if (prev_block->nChainWork + CalculateClaimedHeadersWork({cmpctblock.header}) < GetAntiDoSWorkThreshold()) { + } else if (prev_block->nChainWork + CalculateClaimedHeadersWork({{cmpctblock.header}}) < GetAntiDoSWorkThreshold()) { // If we get a low-work header in a compact block, we can ignore it. LogPrint(BCLog::NET, "Ignoring low-work compact block from peer %d\n", pfrom.GetId()); return; @@ -5068,7 +5068,7 @@ void PeerManagerImpl::ProcessMessage(CNode& pfrom, const std::string& msg_type, mapBlockSource.emplace(hash, std::make_pair(pfrom.GetId(), true)); // Check claimed work on this block against our anti-dos thresholds. - if (prev_block && prev_block->nChainWork + CalculateClaimedHeadersWork({pblock->GetBlockHeader()}) >= GetAntiDoSWorkThreshold()) { + if (prev_block && prev_block->nChainWork + CalculateClaimedHeadersWork({{pblock->GetBlockHeader()}}) >= GetAntiDoSWorkThreshold()) { min_pow_checked = true; } } diff --git a/src/validation.cpp b/src/validation.cpp index 4868a776e44ce..04c04f5e3c320 100644 --- a/src/validation.cpp +++ b/src/validation.cpp @@ -4137,7 +4137,7 @@ bool IsBlockMutated(const CBlock& block, bool check_witness_root) return false; } -arith_uint256 CalculateClaimedHeadersWork(const std::vector& headers) +arith_uint256 CalculateClaimedHeadersWork(std::span headers) { arith_uint256 total_work{0}; for (const CBlockHeader& header : headers) { diff --git a/src/validation.h b/src/validation.h index 9ec592b03dd20..35a8c971bae5e 100644 --- a/src/validation.h +++ b/src/validation.h @@ -408,7 +408,7 @@ bool HasValidProofOfWork(const std::vector& headers, const Consens bool IsBlockMutated(const CBlock& block, bool check_witness_root); /** Return the sum of the claimed work on a given set of headers. No verification of PoW is done. */ -arith_uint256 CalculateClaimedHeadersWork(const std::vector& headers); +arith_uint256 CalculateClaimedHeadersWork(std::span headers); enum class VerifyDBResult { SUCCESS,