Skip to content

Commit

Permalink
Merge bitcoin#27712: test: p2p: check misbehavior for non-continuous …
Browse files Browse the repository at this point in the history
…headers messages

a97c59f 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`:

  https://github.com/bitcoin/bitcoin/blob/17acb2782a66f033238340e4fb81009e7f79e97a/src/net_processing.cpp#L2415-L2419

  https://github.com/bitcoin/bitcoin/blob/17acb2782a66f033238340e4fb81009e7f79e97a/src/net_processing.cpp#L2474-L2484

ACKs for top commit:
  sr-gi:
    ACK bitcoin@a97c59f
  achow101:
    ACK a97c59f
  instagibbs:
    ACK a97c59f

Tree-SHA512: 3f8d6a2492e5c8b63c7b11be2e4ec455f83581b2c58f2d4e705baadfe8d7c6377296d6cd0eda679d291a13d8930b09443f8e3d183795df34b780c703d5d3aeb3
  • Loading branch information
achow101 committed Jun 15, 2023
2 parents 5b8e077 + a97c59f commit b3db18a
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions test/functional/p2p_invalid_messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -75,6 +76,7 @@ def run_test(self):
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):
Expand Down Expand Up @@ -283,6 +285,25 @@ def test_invalid_pow_headers_msg(self):
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())
Expand Down

0 comments on commit b3db18a

Please sign in to comment.