Changeset View
Changeset View
Standalone View
Standalone View
test/functional/p2p_fingerprint.py
Show All 22 Lines | |||||
from test_framework.util import assert_equal, wait_until | from test_framework.util import assert_equal, wait_until | ||||
class P2PFingerprintTest(BitcoinTestFramework): | class P2PFingerprintTest(BitcoinTestFramework): | ||||
def set_test_params(self): | def set_test_params(self): | ||||
self.setup_clean_chain = True | self.setup_clean_chain = True | ||||
self.num_nodes = 1 | self.num_nodes = 1 | ||||
def skip_test_if_missing_module(self): | |||||
self.skip_if_no_wallet() | |||||
# Build a chain of blocks on top of given one | # Build a chain of blocks on top of given one | ||||
def build_chain(self, nblocks, prev_hash, prev_height, prev_median_time): | def build_chain(self, nblocks, prev_hash, prev_height, prev_median_time): | ||||
blocks = [] | blocks = [] | ||||
for _ in range(nblocks): | for _ in range(nblocks): | ||||
coinbase = create_coinbase(prev_height + 1) | coinbase = create_coinbase(prev_height + 1) | ||||
block_time = prev_median_time + 1 | block_time = prev_median_time + 1 | ||||
block = create_block(int(prev_hash, 16), coinbase, block_time) | block = create_block(int(prev_hash, 16), coinbase, block_time) | ||||
block.solve() | block.solve() | ||||
Show All 35 Lines | class P2PFingerprintTest(BitcoinTestFramework): | ||||
# last month but that have over a month's worth of work are also withheld. | # last month but that have over a month's worth of work are also withheld. | ||||
def run_test(self): | def run_test(self): | ||||
node0 = self.nodes[0].add_p2p_connection(P2PInterface()) | node0 = self.nodes[0].add_p2p_connection(P2PInterface()) | ||||
# Set node time to 60 days ago | # Set node time to 60 days ago | ||||
self.nodes[0].setmocktime(int(time.time()) - 60 * 24 * 60 * 60) | self.nodes[0].setmocktime(int(time.time()) - 60 * 24 * 60 * 60) | ||||
# Generating a chain of 10 blocks | # Generating a chain of 10 blocks | ||||
block_hashes = self.nodes[0].generate(nblocks=10) | block_hashes = self.nodes[0].generatetoaddress( | ||||
10, self.nodes[0].get_deterministic_priv_key().address) | |||||
# Create longer chain starting 2 blocks before current tip | # Create longer chain starting 2 blocks before current tip | ||||
height = len(block_hashes) - 2 | height = len(block_hashes) - 2 | ||||
block_hash = block_hashes[height - 1] | block_hash = block_hashes[height - 1] | ||||
block_time = self.nodes[0].getblockheader(block_hash)["mediantime"] + 1 | block_time = self.nodes[0].getblockheader(block_hash)["mediantime"] + 1 | ||||
new_blocks = self.build_chain(5, block_hash, height, block_time) | new_blocks = self.build_chain(5, block_hash, height, block_time) | ||||
# Force reorg to a longer chain | # Force reorg to a longer chain | ||||
Show All 16 Lines | def run_test(self): | ||||
# Check that getheader request for stale block header succeeds | # Check that getheader request for stale block header succeeds | ||||
self.send_header_request(stale_hash, node0) | self.send_header_request(stale_hash, node0) | ||||
def test_function(): return self.last_header_equals(stale_hash, node0) | def test_function(): return self.last_header_equals(stale_hash, node0) | ||||
wait_until(test_function, timeout=3) | wait_until(test_function, timeout=3) | ||||
# Longest chain is extended so stale is much older than chain tip | # Longest chain is extended so stale is much older than chain tip | ||||
self.nodes[0].setmocktime(0) | self.nodes[0].setmocktime(0) | ||||
tip = self.nodes[0].generate(nblocks=1)[0] | tip = self.nodes[0].generatetoaddress( | ||||
1, self.nodes[0].get_deterministic_priv_key().address)[0] | |||||
assert_equal(self.nodes[0].getblockcount(), 14) | assert_equal(self.nodes[0].getblockcount(), 14) | ||||
# Send getdata & getheaders to refresh last received getheader message | # Send getdata & getheaders to refresh last received getheader message | ||||
block_hash = int(tip, 16) | block_hash = int(tip, 16) | ||||
self.send_block_request(block_hash, node0) | self.send_block_request(block_hash, node0) | ||||
self.send_header_request(block_hash, node0) | self.send_header_request(block_hash, node0) | ||||
node0.sync_with_ping() | node0.sync_with_ping() | ||||
Show All 29 Lines |