2017-10-11 10:49:43 +02:00
|
|
|
#!/usr/bin/env python3
|
2020-12-31 18:50:11 +01:00
|
|
|
# Copyright (c) 2017-2020 The Bitcoin Core developers
|
2017-10-11 10:49:43 +02:00
|
|
|
# Distributed under the MIT software license, see the accompanying
|
|
|
|
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
"""Test various fingerprinting protections.
|
|
|
|
|
2018-02-12 10:47:05 +01:00
|
|
|
If a stale block more than a month old or its header are requested by a peer,
|
2017-10-11 10:49:43 +02:00
|
|
|
the node should pretend that it does not have it to avoid fingerprinting.
|
|
|
|
"""
|
|
|
|
import time
|
|
|
|
|
|
|
|
from test_framework.blocktools import (create_block, create_coinbase)
|
2022-09-20 09:52:32 +02:00
|
|
|
from test_framework.messages import CInv, MSG_BLOCK
|
2024-01-15 20:35:29 +01:00
|
|
|
from test_framework.p2p import (
|
2017-11-30 23:58:58 +01:00
|
|
|
P2PInterface,
|
2017-10-11 10:49:43 +02:00
|
|
|
msg_headers,
|
|
|
|
msg_block,
|
|
|
|
msg_getdata,
|
|
|
|
msg_getheaders,
|
2020-11-19 15:38:06 +01:00
|
|
|
p2p_lock,
|
2017-10-11 10:49:43 +02:00
|
|
|
)
|
|
|
|
from test_framework.test_framework import BitcoinTestFramework
|
|
|
|
from test_framework.util import (
|
|
|
|
assert_equal,
|
2017-11-26 03:54:34 +01:00
|
|
|
)
|
2017-10-11 10:49:43 +02:00
|
|
|
|
2020-08-27 08:21:53 +02:00
|
|
|
|
2017-10-11 10:49:43 +02:00
|
|
|
class P2PFingerprintTest(BitcoinTestFramework):
|
2019-03-12 10:15:45 +01:00
|
|
|
|
2017-10-11 10:49:43 +02:00
|
|
|
def set_test_params(self):
|
|
|
|
self.setup_clean_chain = True
|
|
|
|
self.num_nodes = 1
|
|
|
|
|
|
|
|
# Build a chain of blocks on top of given one
|
|
|
|
def build_chain(self, nblocks, prev_hash, prev_height, prev_median_time):
|
|
|
|
blocks = []
|
|
|
|
for _ in range(nblocks):
|
|
|
|
coinbase = create_coinbase(prev_height + 1)
|
|
|
|
block_time = prev_median_time + 1
|
|
|
|
block = create_block(int(prev_hash, 16), coinbase, block_time)
|
|
|
|
block.solve()
|
|
|
|
|
|
|
|
blocks.append(block)
|
|
|
|
prev_hash = block.hash
|
|
|
|
prev_height += 1
|
|
|
|
prev_median_time = block_time
|
|
|
|
return blocks
|
|
|
|
|
|
|
|
# Send a getdata request for a given block hash
|
|
|
|
def send_block_request(self, block_hash, node):
|
|
|
|
msg = msg_getdata()
|
2022-09-20 09:52:32 +02:00
|
|
|
msg.inv.append(CInv(MSG_BLOCK, block_hash))
|
2017-10-11 10:49:43 +02:00
|
|
|
node.send_message(msg)
|
|
|
|
|
|
|
|
# Send a getheaders request for a given single block hash
|
|
|
|
def send_header_request(self, block_hash, node):
|
|
|
|
msg = msg_getheaders()
|
|
|
|
msg.hashstop = block_hash
|
|
|
|
node.send_message(msg)
|
|
|
|
|
|
|
|
# Checks that stale blocks timestamped more than a month ago are not served
|
|
|
|
# by the node while recent stale blocks and old active chain blocks are.
|
|
|
|
# This does not currently test that stale blocks timestamped within the
|
|
|
|
# last month but that have over a month's worth of work are also withheld.
|
|
|
|
def run_test(self):
|
2017-11-30 23:58:58 +01:00
|
|
|
node0 = self.nodes[0].add_p2p_connection(P2PInterface())
|
2017-10-11 10:49:43 +02:00
|
|
|
|
|
|
|
# Set node time to 60 days ago
|
|
|
|
self.nodes[0].setmocktime(int(time.time()) - 60 * 24 * 60 * 60)
|
|
|
|
|
|
|
|
# Generating a chain of 10 blocks
|
2024-10-01 21:25:52 +02:00
|
|
|
block_hashes = self.generatetoaddress(self.nodes[0], 10, self.nodes[0].get_deterministic_priv_key().address)
|
2017-10-11 10:49:43 +02:00
|
|
|
|
|
|
|
# Create longer chain starting 2 blocks before current tip
|
|
|
|
height = len(block_hashes) - 2
|
|
|
|
block_hash = block_hashes[height - 1]
|
|
|
|
block_time = self.nodes[0].getblockheader(block_hash)["mediantime"] + 1
|
|
|
|
new_blocks = self.build_chain(5, block_hash, height, block_time)
|
|
|
|
|
|
|
|
# Force reorg to a longer chain
|
|
|
|
node0.send_message(msg_headers(new_blocks))
|
2020-04-23 12:37:35 +02:00
|
|
|
node0.wait_for_getdata([x.sha256 for x in new_blocks])
|
2017-10-11 10:49:43 +02:00
|
|
|
for block in new_blocks:
|
|
|
|
node0.send_and_ping(msg_block(block))
|
|
|
|
|
|
|
|
# Check that reorg succeeded
|
|
|
|
assert_equal(self.nodes[0].getblockcount(), 13)
|
|
|
|
|
|
|
|
stale_hash = int(block_hashes[-1], 16)
|
|
|
|
|
|
|
|
# Check that getdata request for stale block succeeds
|
|
|
|
self.send_block_request(stale_hash, node0)
|
2020-11-19 15:38:06 +01:00
|
|
|
node0.wait_for_block(stale_hash, timeout=3)
|
2017-10-11 10:49:43 +02:00
|
|
|
|
|
|
|
# Check that getheader request for stale block header succeeds
|
|
|
|
self.send_header_request(stale_hash, node0)
|
2020-11-19 15:38:06 +01:00
|
|
|
node0.wait_for_header(hex(stale_hash), timeout=3)
|
2017-10-11 10:49:43 +02:00
|
|
|
|
|
|
|
# Longest chain is extended so stale is much older than chain tip
|
|
|
|
self.nodes[0].setmocktime(0)
|
2024-10-01 21:25:52 +02:00
|
|
|
block_hash = int(self.generatetoaddress(self.nodes[0], 1, self.nodes[0].get_deterministic_priv_key().address)[-1], 16)
|
2017-10-11 10:49:43 +02:00
|
|
|
assert_equal(self.nodes[0].getblockcount(), 14)
|
2020-12-03 10:09:23 +01:00
|
|
|
node0.wait_for_block(block_hash, timeout=3)
|
2017-10-11 10:49:43 +02:00
|
|
|
|
|
|
|
# Request for very old stale block should now fail
|
2020-11-19 15:38:06 +01:00
|
|
|
with p2p_lock:
|
|
|
|
node0.last_message.pop("block", None)
|
2017-10-11 10:49:43 +02:00
|
|
|
self.send_block_request(stale_hash, node0)
|
2020-11-19 15:38:06 +01:00
|
|
|
node0.sync_with_ping()
|
|
|
|
assert "block" not in node0.last_message
|
2017-10-11 10:49:43 +02:00
|
|
|
|
|
|
|
# Request for very old stale block header should now fail
|
2020-11-19 15:38:06 +01:00
|
|
|
with p2p_lock:
|
|
|
|
node0.last_message.pop("headers", None)
|
2017-10-11 10:49:43 +02:00
|
|
|
self.send_header_request(stale_hash, node0)
|
2020-11-19 15:38:06 +01:00
|
|
|
node0.sync_with_ping()
|
|
|
|
assert "headers" not in node0.last_message
|
2017-10-11 10:49:43 +02:00
|
|
|
|
|
|
|
# Verify we can fetch very old blocks and headers on the active chain
|
|
|
|
block_hash = int(block_hashes[2], 16)
|
|
|
|
self.send_block_request(block_hash, node0)
|
|
|
|
self.send_header_request(block_hash, node0)
|
|
|
|
node0.sync_with_ping()
|
|
|
|
|
|
|
|
self.send_block_request(block_hash, node0)
|
2020-11-19 15:38:06 +01:00
|
|
|
node0.wait_for_block(block_hash, timeout=3)
|
2017-10-11 10:49:43 +02:00
|
|
|
|
|
|
|
self.send_header_request(block_hash, node0)
|
2020-11-19 15:38:06 +01:00
|
|
|
node0.wait_for_header(hex(block_hash), timeout=3)
|
2017-10-11 10:49:43 +02:00
|
|
|
|
2020-12-03 10:09:23 +01:00
|
|
|
|
2017-10-11 10:49:43 +02:00
|
|
|
if __name__ == '__main__':
|
|
|
|
P2PFingerprintTest().main()
|