Skip to content

Commit e9035f8

Browse files
committed
Merge bitcoin/bitcoin#25717: p2p: Implement anti-DoS headers sync
3add234 ui: show header pre-synchronization progress (Pieter Wuille) 738421c Emit NotifyHeaderTip signals for pre-synchronization progress (Pieter Wuille) 376086f Make validation interface capable of signalling header presync (Pieter Wuille) 93eae27 Test large reorgs with headerssync logic (Suhas Daftuar) 3555473 Track headers presync progress and log it (Pieter Wuille) 03712dd Expose HeadersSyncState::m_current_height in getpeerinfo() (Suhas Daftuar) 150a548 Test headers sync using minchainwork threshold (Suhas Daftuar) 0b6aa82 Add unit test for HeadersSyncState (Suhas Daftuar) 83c6a0c Reduce spurious messages during headers sync (Suhas Daftuar) ed6cddd Require callers of AcceptBlockHeader() to perform anti-dos checks (Suhas Daftuar) 551a8d9 Utilize anti-DoS headers download strategy (Suhas Daftuar) ed47094 Add functions to construct locators without CChain (Pieter Wuille) 84852bb Add bitdeque, an std::deque<bool> analogue that does bit packing. (Pieter Wuille) 1d4cfa4 Add function to validate difficulty changes (Suhas Daftuar) Pull request description: New nodes starting up for the first time lack protection against DoS from low-difficulty headers. While checkpoints serve as our protection against headers that fork from the main chain below the known checkpointed values, this protection only applies to nodes that have been able to download the honest chain to the checkpointed heights. We can protect all nodes from DoS from low-difficulty headers by adopting a different strategy: before we commit to storing a header in permanent storage, first verify that the header is part of a chain that has sufficiently high work (either `nMinimumChainWork`, or something comparable to our tip). This means that we will download headers from a given peer twice: once to verify the work on the chain, and a second time when permanently storing the headers. The p2p protocol doesn't provide an easy way for us to ensure that we receive the same headers during the second download of peer's headers chain. To ensure that a peer doesn't (say) give us the main chain in phase 1 to trick us into permanently storing an alternate, low-work chain in phase 2, we store commitments to the headers during our first download, which we validate in the second download. Some parameters must be chosen for commitment size/frequency in phase 1, and validation of commitments in phase 2. In this PR, those parameters are chosen to both (a) minimize the per-peer memory usage that an attacker could utilize, and (b) bound the expected amount of permanent memory that an attacker could get us to use to be well-below the memory growth that we'd get from the honest chain (where we expect 1 new block header every 10 minutes). After this PR, we should be able to remove checkpoints from our code, which is a nice philosophical change for us to make as well, as there has been confusion over the years about the role checkpoints play in Bitcoin's consensus algorithm. Thanks to Pieter Wuille for collaborating on this design. ACKs for top commit: Sjors: re-tACK 3add234 mzumsande: re-ACK 3add234 sipa: re-ACK 3add234 glozow: ACK 3add234 Tree-SHA512: e7789d65f62f72141b8899eb4a2fb3d0621278394d2d7adaa004675250118f89a4e4cb42777fe56649d744ec445ad95141e10f6def65f0a58b7b35b2e654a875
2 parents cfda740 + 3add234 commit e9035f8

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+2710
-149
lines changed

src/Makefile.am

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ BITCOIN_CORE_H = \
151151
external_signer.h \
152152
flatfile.h \
153153
fs.h \
154+
headerssync.h \
154155
httprpc.h \
155156
httpserver.h \
156157
i2p.h \
@@ -264,6 +265,7 @@ BITCOIN_CORE_H = \
264265
undo.h \
265266
util/asmap.h \
266267
util/bip32.h \
268+
util/bitdeque.h \
267269
util/bytevectorhash.h \
268270
util/check.h \
269271
util/epochguard.h \
@@ -360,6 +362,7 @@ libbitcoin_node_a_SOURCES = \
360362
dbwrapper.cpp \
361363
deploymentstatus.cpp \
362364
flatfile.cpp \
365+
headerssync.cpp \
363366
httprpc.cpp \
364367
httpserver.cpp \
365368
i2p.cpp \

src/Makefile.test.include

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ BITCOIN_TESTS =\
9393
test/fs_tests.cpp \
9494
test/getarg_tests.cpp \
9595
test/hash_tests.cpp \
96+
test/headers_sync_chainwork_tests.cpp \
9697
test/httpserver_tests.cpp \
9798
test/i2p_tests.cpp \
9899
test/interfaces_tests.cpp \
@@ -235,6 +236,7 @@ test_fuzz_fuzz_SOURCES = \
235236
test/fuzz/banman.cpp \
236237
test/fuzz/base_encode_decode.cpp \
237238
test/fuzz/bech32.cpp \
239+
test/fuzz/bitdeque.cpp \
238240
test/fuzz/block.cpp \
239241
test/fuzz/block_header.cpp \
240242
test/fuzz/blockfilter.cpp \

src/bitcoin-chainstate.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ int main(int argc, char* argv[])
195195
bool new_block;
196196
auto sc = std::make_shared<submitblock_StateCatcher>(block.GetHash());
197197
RegisterSharedValidationInterface(sc);
198-
bool accepted = chainman.ProcessNewBlock(blockptr, /*force_processing=*/true, /*new_block=*/&new_block);
198+
bool accepted = chainman.ProcessNewBlock(blockptr, /*force_processing=*/true, /*min_pow_checked=*/true, /*new_block=*/&new_block);
199199
UnregisterSharedValidationInterface(sc);
200200
if (!new_block && accepted) {
201201
std::cerr << "duplicate" << std::endl;
@@ -210,6 +210,9 @@ int main(int argc, char* argv[])
210210
case BlockValidationResult::BLOCK_RESULT_UNSET:
211211
std::cerr << "initial value. Block has not yet been rejected" << std::endl;
212212
break;
213+
case BlockValidationResult::BLOCK_HEADER_LOW_WORK:
214+
std::cerr << "the block header may be on a too-little-work chain" << std::endl;
215+
break;
213216
case BlockValidationResult::BLOCK_CONSENSUS:
214217
std::cerr << "invalid by consensus rules (excluding any below reasons)" << std::endl;
215218
break;

src/chain.cpp

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -28,32 +28,33 @@ void CChain::SetTip(CBlockIndex& block)
2828
}
2929
}
3030

31-
CBlockLocator CChain::GetLocator(const CBlockIndex *pindex) const {
32-
int nStep = 1;
33-
std::vector<uint256> vHave;
34-
vHave.reserve(32);
35-
36-
if (!pindex)
37-
pindex = Tip();
38-
while (pindex) {
39-
vHave.push_back(pindex->GetBlockHash());
40-
// Stop when we have added the genesis block.
41-
if (pindex->nHeight == 0)
42-
break;
31+
std::vector<uint256> LocatorEntries(const CBlockIndex* index)
32+
{
33+
int step = 1;
34+
std::vector<uint256> have;
35+
if (index == nullptr) return have;
36+
37+
have.reserve(32);
38+
while (index) {
39+
have.emplace_back(index->GetBlockHash());
40+
if (index->nHeight == 0) break;
4341
// Exponentially larger steps back, plus the genesis block.
44-
int nHeight = std::max(pindex->nHeight - nStep, 0);
45-
if (Contains(pindex)) {
46-
// Use O(1) CChain index if possible.
47-
pindex = (*this)[nHeight];
48-
} else {
49-
// Otherwise, use O(log n) skiplist.
50-
pindex = pindex->GetAncestor(nHeight);
51-
}
52-
if (vHave.size() > 10)
53-
nStep *= 2;
42+
int height = std::max(index->nHeight - step, 0);
43+
// Use skiplist.
44+
index = index->GetAncestor(height);
45+
if (have.size() > 10) step *= 2;
5446
}
47+
return have;
48+
}
5549

56-
return CBlockLocator(vHave);
50+
CBlockLocator GetLocator(const CBlockIndex* index)
51+
{
52+
return CBlockLocator{std::move(LocatorEntries(index))};
53+
}
54+
55+
CBlockLocator CChain::GetLocator() const
56+
{
57+
return ::GetLocator(Tip());
5758
}
5859

5960
const CBlockIndex *CChain::FindFork(const CBlockIndex *pindex) const {

src/chain.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -473,8 +473,8 @@ class CChain
473473
/** Set/initialize a chain with a given tip. */
474474
void SetTip(CBlockIndex& block);
475475

476-
/** Return a CBlockLocator that refers to a block in this chain (by default the tip). */
477-
CBlockLocator GetLocator(const CBlockIndex* pindex = nullptr) const;
476+
/** Return a CBlockLocator that refers to the tip in of this chain. */
477+
CBlockLocator GetLocator() const;
478478

479479
/** Find the last common block between this chain and a block index entry. */
480480
const CBlockIndex* FindFork(const CBlockIndex* pindex) const;
@@ -483,4 +483,10 @@ class CChain
483483
CBlockIndex* FindEarliestAtLeast(int64_t nTime, int height) const;
484484
};
485485

486+
/** Get a locator for a block index entry. */
487+
CBlockLocator GetLocator(const CBlockIndex* index);
488+
489+
/** Construct a list of hash entries to put in a locator. */
490+
std::vector<uint256> LocatorEntries(const CBlockIndex* index);
491+
486492
#endif // BITCOIN_CHAIN_H

src/consensus/validation.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ enum class BlockValidationResult {
7979
BLOCK_INVALID_PREV, //!< A block this one builds on is invalid
8080
BLOCK_TIME_FUTURE, //!< block timestamp was > 2 hours in the future (or our clock is bad)
8181
BLOCK_CHECKPOINT, //!< the block failed to meet one of our checkpoints
82+
BLOCK_HEADER_LOW_WORK //!< the block header may be on a too-little-work chain
8283
};
8384

8485

0 commit comments

Comments
 (0)