aboutsummaryrefslogtreecommitdiff
path: root/src/validation.cpp
diff options
context:
space:
mode:
authorSuhas Daftuar <sdaftuar@gmail.com>2022-02-09 09:38:52 -0500
committerSuhas Daftuar <sdaftuar@gmail.com>2022-08-29 08:10:35 -0400
commit551a8d957c4c44afbd0d608fcdf7c6a4352babce (patch)
tree774014202e3ad8560a3be4d59f94b8443e672a67 /src/validation.cpp
parented470940cddbeb40425960d51cefeec4948febe4 (diff)
downloadbitcoin-551a8d957c4c44afbd0d608fcdf7c6a4352babce.tar.xz
Utilize anti-DoS headers download strategy
Avoid permanently storing headers from a peer, unless the headers are part of a chain with sufficiently high work. This prevents memory attacks using low-work headers. Designed and co-authored with Pieter Wuille.
Diffstat (limited to 'src/validation.cpp')
-rw-r--r--src/validation.cpp16
1 files changed, 16 insertions, 0 deletions
diff --git a/src/validation.cpp b/src/validation.cpp
index f4cae07b9e..cb8ac664d8 100644
--- a/src/validation.cpp
+++ b/src/validation.cpp
@@ -3432,6 +3432,22 @@ std::vector<unsigned char> ChainstateManager::GenerateCoinbaseCommitment(CBlock&
return commitment;
}
+bool HasValidProofOfWork(const std::vector<CBlockHeader>& headers, const Consensus::Params& consensusParams)
+{
+ return std::all_of(headers.cbegin(), headers.cend(),
+ [&](const auto& header) { return CheckProofOfWork(header.GetHash(), header.nBits, consensusParams);});
+}
+
+arith_uint256 CalculateHeadersWork(const std::vector<CBlockHeader>& headers)
+{
+ arith_uint256 total_work{0};
+ for (const CBlockHeader& header : headers) {
+ CBlockIndex dummy(header);
+ total_work += GetBlockProof(dummy);
+ }
+ return total_work;
+}
+
/** Context-dependent validity checks.
* By "context", we mean only the previous block headers, but not the UTXO
* set; UTXO-related validity checks are done in ConnectBlock().