Skip to content

Commit e60804f

Browse files
committed
Merge bitcoin/bitcoin#29524: p2p: Don't consider blocks mutated if they don't connect to known prev block
a1fbde0 p2p: Don't consider blocks mutated if they don't connect to known prev block (Greg Sanders) Pull request description: Followup to bitcoin/bitcoin#29412 to revert some of the behavior change that was likely unintentional. Based on comments from bitcoin/bitcoin#29412 (comment) ACKs for top commit: 0xB10C: utACK a1fbde0 dergoegge: Code review ACK a1fbde0 Sjors: ACK a1fbde0 sr-gi: tACK bitcoin/bitcoin@a1fbde0 Tree-SHA512: be6204c8cc57b271d55c1d02a5c77d03a37738d91cb5ac164f483b0bab3991c24679c5ff02fbaa52bf37ee625874b63f4c9f7b39ad6fd5f3a25386567a0942e4
2 parents fce53f1 + a1fbde0 commit e60804f

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

src/net_processing.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4721,7 +4721,8 @@ void PeerManagerImpl::ProcessMessage(CNode& pfrom, const std::string& msg_type,
47214721

47224722
const CBlockIndex* prev_block{WITH_LOCK(m_chainman.GetMutex(), return m_chainman.m_blockman.LookupBlockIndex(pblock->hashPrevBlock))};
47234723

4724-
if (IsBlockMutated(/*block=*/*pblock,
4724+
// Check for possible mutation if it connects to something we know so we can check for DEPLOYMENT_SEGWIT being active
4725+
if (prev_block && IsBlockMutated(/*block=*/*pblock,
47254726
/*check_witness_root=*/DeploymentActiveAfter(prev_block, m_chainman, Consensus::DEPLOYMENT_SEGWIT))) {
47264727
LogDebug(BCLog::NET, "Received mutated block from peer=%d\n", peer->m_id);
47274728
Misbehaving(*peer, 100, "mutated block");

test/functional/p2p_mutated_blocks.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ class MutatedBlocksTest(BitcoinTestFramework):
3131
def set_test_params(self):
3232
self.setup_clean_chain = True
3333
self.num_nodes = 1
34+
self.extra_args = [
35+
[
36+
"-testactivationheight=segwit@1", # causes unconnected headers/blocks to not have segwit considered deployed
37+
],
38+
]
3439

3540
def run_test(self):
3641
self.wallet = MiniWallet(self.nodes[0])
@@ -74,7 +79,7 @@ def self_transfer_requested():
7479

7580
# Attempt to clear the honest relayer's download request by sending the
7681
# mutated block (as the attacker).
77-
with self.nodes[0].assert_debug_log(expected_msgs=["bad-txnmrklroot, hashMerkleRoot mismatch"]):
82+
with self.nodes[0].assert_debug_log(expected_msgs=["Block mutated: bad-txnmrklroot, hashMerkleRoot mismatch"]):
7883
attacker.send_message(msg_block(mutated_block))
7984
# Attacker should get disconnected for sending a mutated block
8085
attacker.wait_for_disconnect(timeout=5)
@@ -92,5 +97,20 @@ def self_transfer_requested():
9297
honest_relayer.send_and_ping(block_txn)
9398
assert_equal(self.nodes[0].getbestblockhash(), block.hash)
9499

100+
# Check that unexpected-witness mutation check doesn't trigger on a header that doesn't connect to anything
101+
assert_equal(len(self.nodes[0].getpeerinfo()), 1)
102+
attacker = self.nodes[0].add_p2p_connection(P2PInterface())
103+
block_missing_prev = copy.deepcopy(block)
104+
block_missing_prev.hashPrevBlock = 123
105+
block_missing_prev.solve()
106+
107+
# Attacker gets a DoS score of 10, not immediately disconnected, so we do it 10 times to get to 100
108+
for _ in range(10):
109+
assert_equal(len(self.nodes[0].getpeerinfo()), 2)
110+
with self.nodes[0].assert_debug_log(expected_msgs=["AcceptBlock FAILED (prev-blk-not-found)"]):
111+
attacker.send_message(msg_block(block_missing_prev))
112+
attacker.wait_for_disconnect(timeout=5)
113+
114+
95115
if __name__ == '__main__':
96116
MutatedBlocksTest().main()

0 commit comments

Comments
 (0)