Merge bitcoin/bitcoin#27712: test: p2p: check misbehavior for non-continuous headers messages

a97c59f12d test: p2p: check misbehavior for non-continuous headers messages (Sebastian Falbesoner)

Pull request description:

  This PR adds missing test coverage for a peer sending a `headers` message where the headers don't connect to each other, which should be treated as misbehaving (not disconnecting though, as the score increase is only 20). The relevant code path is `PeerManagerImpl::ProcessHeadersMessage` -> `PeerManagerImpl::CheckHeadersPoW` -> `PeerManagerImpl::CheckHeadersAreContinuous`:

  17acb2782a/src/net_processing.cpp (L2415-L2419)

  17acb2782a/src/net_processing.cpp (L2474-L2484)

ACKs for top commit:
  sr-gi:
    ACK a97c59f12d
  achow101:
    ACK a97c59f12d
  instagibbs:
    ACK a97c59f12d

Tree-SHA512: 3f8d6a2492e5c8b63c7b11be2e4ec455f83581b2c58f2d4e705baadfe8d7c6377296d6cd0eda679d291a13d8930b09443f8e3d183795df34b780c703d5d3aeb3
This commit is contained in:
Andrew Chow 2023-06-15 14:44:30 -04:00
commit b3db18a012
No known key found for this signature in database
GPG Key ID: 17565732E08E5E41

View File

@ -4,6 +4,7 @@
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
"""Test node responses to invalid network messages."""
import random
import struct
import time
@ -75,6 +76,7 @@ class InvalidMessagesTest(BitcoinTestFramework):
self.test_oversized_getdata_msg()
self.test_oversized_headers_msg()
self.test_invalid_pow_headers_msg()
self.test_noncontinuous_headers_msg()
self.test_resource_exhaustion()
def test_buffer(self):
@ -283,6 +285,25 @@ class InvalidMessagesTest(BitcoinTestFramework):
peer.send_message(msg_headers([blockheader]))
peer.wait_for_disconnect()
def test_noncontinuous_headers_msg(self):
self.log.info("Test headers message with non-continuous headers sequence is logged as misbehaving")
block_hashes = self.generate(self.nodes[0], 10)
block_headers = []
for block_hash in block_hashes:
block_headers.append(from_hex(CBlockHeader(), self.nodes[0].getblockheader(block_hash, False)))
# continuous headers sequence should be fine
MISBEHAVING_NONCONTINUOUS_HEADERS_MSGS = ['Misbehaving', 'non-continuous headers sequence']
peer = self.nodes[0].add_p2p_connection(P2PInterface())
with self.nodes[0].assert_debug_log([], unexpected_msgs=MISBEHAVING_NONCONTINUOUS_HEADERS_MSGS):
peer.send_and_ping(msg_headers(block_headers))
# delete arbitrary block header somewhere in the middle to break link
del block_headers[random.randrange(1, len(block_headers)-1)]
with self.nodes[0].assert_debug_log(expected_msgs=MISBEHAVING_NONCONTINUOUS_HEADERS_MSGS):
peer.send_and_ping(msg_headers(block_headers))
self.nodes[0].disconnect_p2ps()
def test_resource_exhaustion(self):
self.log.info("Test node stays up despite many large junk messages")
conn = self.nodes[0].add_p2p_connection(P2PDataStore())