merge bitcoin#24178: Respond to getheaders if we have sufficient chainwork

This commit is contained in:
Kittywhiskers Van Gogh 2024-08-01 16:40:38 +00:00 committed by pasta
parent bcafa282a3
commit a04290fc5c
No known key found for this signature in database
GPG Key ID: 52527BEDABE87984
2 changed files with 37 additions and 2 deletions

View File

@ -4032,9 +4032,23 @@ void PeerManagerImpl::ProcessMessage(
return;
}
if (fImporting || fReindex) {
LogPrint(BCLog::NET, "Ignoring %s from peer=%d while importing/reindexing\n", msg_type, pfrom.GetId());
return;
}
LOCK(cs_main);
if (m_chainman.ActiveChainstate().IsInitialBlockDownload() && !pfrom.HasPermission(NetPermissionFlags::Download)) {
LogPrint(BCLog::NET, "Ignoring %s from peer=%d because node is in initial block download\n", msg_type, pfrom.GetId());
// Note that if we were to be on a chain that forks from the checkpointed
// chain, then serving those headers to a peer that has seen the
// checkpointed chain would cause that peer to disconnect us. Requiring
// that our chainwork exceed nMinimumChainWork is a protection against
// being fed a bogus chain when we started up for the first time and
// getting partitioned off the honest network for serving that chain to
// others.
if (m_chainman.ActiveTip() == nullptr ||
(m_chainman.ActiveTip()->nChainWork < nMinimumChainWork && !pfrom.HasPermission(NetPermissionFlags::Download))) {
LogPrint(BCLog::NET, "Ignoring %s from peer=%d because active chain has too little work\n", msg_type, pfrom.GetId());
return;
}

View File

@ -17,6 +17,7 @@ only succeeds past a given node once its nMinimumChainWork has been exceeded.
import time
from test_framework.p2p import P2PInterface, msg_getheaders
from test_framework.test_framework import BitcoinTestFramework
from test_framework.util import assert_equal
@ -43,6 +44,9 @@ class MinimumChainWorkTest(BitcoinTestFramework):
for i in range(self.num_nodes-1):
self.connect_nodes(i+1, i)
# Set clock of node2 2 days ahead, to keep it in IBD during this test.
self.nodes[2].setmocktime(int(time.time()) + 48*60*60)
def run_test(self):
# Start building a chain on node0. node2 shouldn't be able to sync until node1's
# minchainwork is exceeded
@ -74,6 +78,15 @@ class MinimumChainWorkTest(BitcoinTestFramework):
assert self.nodes[1].getbestblockhash() != self.nodes[0].getbestblockhash()
assert_equal(self.nodes[2].getblockcount(), starting_blockcount)
self.log.info("Check that getheaders requests to node2 are ignored")
peer = self.nodes[2].add_p2p_connection(P2PInterface())
msg = msg_getheaders()
msg.locator.vHave = [int(self.nodes[2].getbestblockhash(), 16)]
msg.hashstop = 0
peer.send_and_ping(msg)
time.sleep(5)
assert "headers" not in peer.last_message
self.log.info("Generating one more block")
self.nodes[0].generatetoaddress(1, self.nodes[0].get_deterministic_priv_key().address)
@ -88,6 +101,14 @@ class MinimumChainWorkTest(BitcoinTestFramework):
self.sync_all()
self.log.info("Blockcounts: %s", [n.getblockcount() for n in self.nodes])
self.log.info("Test that getheaders requests to node2 are not ignored")
peer.send_and_ping(msg)
assert "headers" in peer.last_message
# Verify that node2 is in fact still in IBD (otherwise this test may
# not be exercising the logic we want!)
assert_equal(self.nodes[2].getblockchaininfo()['initialblockdownload'], True)
self.log.info("Test -minimumchainwork with a non-hex value")
self.stop_node(0)
self.nodes[0].assert_start_raises_init_error(