2017-01-13 20:23:59 +01:00
|
|
|
#!/usr/bin/env python3
|
2022-12-25 00:49:50 +01:00
|
|
|
# Copyright (c) 2014-2022 The Bitcoin Core developers
|
2017-01-13 20:23:59 +01:00
|
|
|
# Distributed under the MIT software license, see the accompanying
|
|
|
|
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
2017-01-18 00:34:40 +01:00
|
|
|
"""Test logic for skipping signature validation on old blocks.
|
2017-01-13 20:23:59 +01:00
|
|
|
|
|
|
|
Test logic for skipping signature validation on blocks which we've assumed
|
|
|
|
valid (https://github.com/bitcoin/bitcoin/pull/9484)
|
|
|
|
|
|
|
|
We build a chain that includes and invalid signature for one of the
|
|
|
|
transactions:
|
|
|
|
|
|
|
|
0: genesis block
|
|
|
|
1: block 1 with coinbase transaction output.
|
|
|
|
2-101: bury that block with 100 blocks so the coinbase transaction
|
|
|
|
output can be spent
|
|
|
|
102: a block containing a transaction spending the coinbase
|
2017-02-24 00:00:33 +01:00
|
|
|
transaction output. The transaction has an invalid signature.
|
2017-01-13 20:23:59 +01:00
|
|
|
103-2202: bury the bad block with just over two weeks' worth of blocks
|
|
|
|
(2100 blocks)
|
|
|
|
|
|
|
|
Start three nodes:
|
|
|
|
|
|
|
|
- node0 has no -assumevalid parameter. Try to sync to block 2202. It will
|
|
|
|
reject block 102 and only sync as far as block 101
|
|
|
|
- node1 has -assumevalid set to the hash of block 102. Try to sync to
|
|
|
|
block 2202. node1 will sync all the way to block 2202.
|
|
|
|
- node2 has -assumevalid set to the hash of block 102. Try to sync to
|
|
|
|
block 200. node2 will reject block 102 since it's assumed valid, but it
|
|
|
|
isn't buried by at least two weeks' work.
|
2017-01-18 00:34:40 +01:00
|
|
|
"""
|
2017-01-13 20:23:59 +01:00
|
|
|
|
2021-02-08 15:40:46 +01:00
|
|
|
from test_framework.blocktools import (
|
2021-05-17 16:38:19 +02:00
|
|
|
COINBASE_MATURITY,
|
2021-02-08 15:40:46 +01:00
|
|
|
create_block,
|
|
|
|
create_coinbase,
|
|
|
|
)
|
2018-06-18 23:28:37 +02:00
|
|
|
from test_framework.messages import (
|
|
|
|
CBlockHeader,
|
|
|
|
COutPoint,
|
|
|
|
CTransaction,
|
|
|
|
CTxIn,
|
|
|
|
CTxOut,
|
|
|
|
msg_block,
|
2019-09-16 19:19:29 +02:00
|
|
|
msg_headers,
|
2018-06-18 23:28:37 +02:00
|
|
|
)
|
2020-07-19 09:47:05 +02:00
|
|
|
from test_framework.p2p import P2PInterface
|
2023-05-23 23:38:31 +02:00
|
|
|
from test_framework.script import (
|
|
|
|
CScript,
|
|
|
|
OP_TRUE,
|
|
|
|
)
|
2017-02-24 00:00:33 +01:00
|
|
|
from test_framework.test_framework import BitcoinTestFramework
|
2017-08-24 21:36:02 +02:00
|
|
|
from test_framework.util import assert_equal
|
2023-05-23 23:38:31 +02:00
|
|
|
from test_framework.wallet_util import generate_keypair
|
2017-01-13 20:23:59 +01:00
|
|
|
|
2020-03-11 18:29:20 +01:00
|
|
|
|
2017-10-17 22:16:39 +02:00
|
|
|
class BaseNode(P2PInterface):
|
2017-01-13 20:23:59 +01:00
|
|
|
def send_header_for_blocks(self, new_blocks):
|
|
|
|
headers_message = msg_headers()
|
2017-02-24 00:00:33 +01:00
|
|
|
headers_message.headers = [CBlockHeader(b) for b in new_blocks]
|
2017-01-13 20:23:59 +01:00
|
|
|
self.send_message(headers_message)
|
|
|
|
|
2020-03-11 18:29:20 +01:00
|
|
|
|
2017-02-24 00:00:33 +01:00
|
|
|
class AssumeValidTest(BitcoinTestFramework):
|
2017-06-10 00:21:21 +02:00
|
|
|
def set_test_params(self):
|
2017-01-13 20:23:59 +01:00
|
|
|
self.setup_clean_chain = True
|
|
|
|
self.num_nodes = 3
|
2020-03-11 18:29:20 +01:00
|
|
|
self.rpc_timeout = 120
|
2017-01-13 20:23:59 +01:00
|
|
|
|
|
|
|
def setup_network(self):
|
2017-08-24 17:37:25 +02:00
|
|
|
self.add_nodes(3)
|
2017-01-13 20:23:59 +01:00
|
|
|
# Start node0. We don't start the other nodes yet since
|
|
|
|
# we need to pre-mine a block with an invalid transaction
|
|
|
|
# signature so we can pass in the block hash as assumevalid.
|
2017-06-09 22:35:17 +02:00
|
|
|
self.start_node(0)
|
2017-02-24 00:00:33 +01:00
|
|
|
|
2017-08-24 21:36:02 +02:00
|
|
|
def send_blocks_until_disconnected(self, p2p_conn):
|
2017-02-24 00:00:33 +01:00
|
|
|
"""Keep sending blocks to the node until we're disconnected."""
|
|
|
|
for i in range(len(self.blocks)):
|
2018-06-21 03:24:29 +02:00
|
|
|
if not p2p_conn.is_connected:
|
2017-09-15 20:36:12 +02:00
|
|
|
break
|
2017-02-24 00:00:33 +01:00
|
|
|
try:
|
2017-08-24 21:36:02 +02:00
|
|
|
p2p_conn.send_message(msg_block(self.blocks[i]))
|
2019-01-25 06:16:35 +01:00
|
|
|
except IOError:
|
2018-06-21 03:24:29 +02:00
|
|
|
assert not p2p_conn.is_connected
|
2017-02-24 00:00:33 +01:00
|
|
|
break
|
|
|
|
|
2017-01-13 20:23:59 +01:00
|
|
|
def run_test(self):
|
|
|
|
# Build the blockchain
|
|
|
|
self.tip = int(self.nodes[0].getbestblockhash(), 16)
|
|
|
|
self.block_time = self.nodes[0].getblock(self.nodes[0].getbestblockhash())['time'] + 1
|
|
|
|
|
|
|
|
self.blocks = []
|
|
|
|
|
|
|
|
# Get a pubkey for the coinbase TXO
|
2023-05-23 23:38:31 +02:00
|
|
|
_, coinbase_pubkey = generate_keypair()
|
2017-01-13 20:23:59 +01:00
|
|
|
|
|
|
|
# Create the first block with a coinbase output to our key
|
|
|
|
height = 1
|
|
|
|
block = create_block(self.tip, create_coinbase(height, coinbase_pubkey), self.block_time)
|
|
|
|
self.blocks.append(block)
|
|
|
|
self.block_time += 1
|
|
|
|
block.solve()
|
|
|
|
# Save the coinbase for later
|
|
|
|
self.block1 = block
|
|
|
|
self.tip = block.sha256
|
|
|
|
height += 1
|
|
|
|
|
|
|
|
# Bury the block 100 deep so the coinbase output is spendable
|
2020-08-03 01:10:56 +02:00
|
|
|
for _ in range(100):
|
2017-01-13 20:23:59 +01:00
|
|
|
block = create_block(self.tip, create_coinbase(height), self.block_time)
|
|
|
|
block.solve()
|
|
|
|
self.blocks.append(block)
|
|
|
|
self.tip = block.sha256
|
|
|
|
self.block_time += 1
|
|
|
|
height += 1
|
|
|
|
|
|
|
|
# Create a transaction spending the coinbase output with an invalid (null) signature
|
|
|
|
tx = CTransaction()
|
|
|
|
tx.vin.append(CTxIn(COutPoint(self.block1.vtx[0].sha256, 0), scriptSig=b""))
|
2017-02-24 00:00:33 +01:00
|
|
|
tx.vout.append(CTxOut(49 * 100000000, CScript([OP_TRUE])))
|
2017-01-13 20:23:59 +01:00
|
|
|
tx.calc_sha256()
|
|
|
|
|
2021-11-15 18:29:13 +01:00
|
|
|
block102 = create_block(self.tip, create_coinbase(height), self.block_time, txlist=[tx])
|
2017-01-13 20:23:59 +01:00
|
|
|
self.block_time += 1
|
|
|
|
block102.solve()
|
|
|
|
self.blocks.append(block102)
|
|
|
|
self.tip = block102.sha256
|
|
|
|
self.block_time += 1
|
|
|
|
height += 1
|
|
|
|
|
|
|
|
# Bury the assumed valid block 2100 deep
|
2020-08-03 01:10:56 +02:00
|
|
|
for _ in range(2100):
|
2017-01-13 20:23:59 +01:00
|
|
|
block = create_block(self.tip, create_coinbase(height), self.block_time)
|
|
|
|
block.solve()
|
|
|
|
self.blocks.append(block)
|
|
|
|
self.tip = block.sha256
|
|
|
|
self.block_time += 1
|
|
|
|
height += 1
|
|
|
|
|
|
|
|
# Start node1 and node2 with assumevalid so they accept a block with a bad signature.
|
2024-07-26 16:10:25 +02:00
|
|
|
self.start_node(1, extra_args=["-assumevalid=" + block102.hash])
|
|
|
|
self.start_node(2, extra_args=["-assumevalid=" + block102.hash])
|
2017-12-07 19:40:39 +01:00
|
|
|
|
|
|
|
p2p0 = self.nodes[0].add_p2p_connection(BaseNode())
|
2017-08-24 21:36:02 +02:00
|
|
|
p2p0.send_header_for_blocks(self.blocks[0:2000])
|
|
|
|
p2p0.send_header_for_blocks(self.blocks[2000:])
|
2017-01-13 20:23:59 +01:00
|
|
|
|
2017-02-24 00:00:33 +01:00
|
|
|
# Send blocks to node0. Block 102 will be rejected.
|
2017-08-24 21:36:02 +02:00
|
|
|
self.send_blocks_until_disconnected(p2p0)
|
2021-05-17 16:38:19 +02:00
|
|
|
self.wait_until(lambda: self.nodes[0].getblockcount() >= COINBASE_MATURITY + 1)
|
|
|
|
assert_equal(self.nodes[0].getblockcount(), COINBASE_MATURITY + 1)
|
2017-01-13 20:23:59 +01:00
|
|
|
|
2022-12-07 10:47:23 +01:00
|
|
|
p2p1 = self.nodes[1].add_p2p_connection(BaseNode())
|
|
|
|
p2p1.send_header_for_blocks(self.blocks[0:2000])
|
|
|
|
p2p1.send_header_for_blocks(self.blocks[2000:])
|
|
|
|
|
2017-02-24 00:00:33 +01:00
|
|
|
# Send all blocks to node1. All blocks will be accepted.
|
2017-01-13 20:23:59 +01:00
|
|
|
for i in range(2202):
|
2017-08-24 21:36:02 +02:00
|
|
|
p2p1.send_message(msg_block(self.blocks[i]))
|
2017-03-29 17:37:00 +02:00
|
|
|
# Syncing 2200 blocks can take a while on slow systems. Give it plenty of time to sync.
|
2024-03-27 13:35:53 +01:00
|
|
|
p2p1.sync_with_ping(timeout=960)
|
2017-01-13 20:23:59 +01:00
|
|
|
assert_equal(self.nodes[1].getblock(self.nodes[1].getbestblockhash())['height'], 2202)
|
|
|
|
|
2022-12-07 10:47:23 +01:00
|
|
|
p2p2 = self.nodes[2].add_p2p_connection(BaseNode())
|
|
|
|
p2p2.send_header_for_blocks(self.blocks[0:200])
|
|
|
|
|
2017-02-24 00:00:33 +01:00
|
|
|
# Send blocks to node2. Block 102 will be rejected.
|
2017-08-24 21:36:02 +02:00
|
|
|
self.send_blocks_until_disconnected(p2p2)
|
2021-05-17 16:38:19 +02:00
|
|
|
self.wait_until(lambda: self.nodes[2].getblockcount() >= COINBASE_MATURITY + 1)
|
|
|
|
assert_equal(self.nodes[2].getblockcount(), COINBASE_MATURITY + 1)
|
2017-01-13 20:23:59 +01:00
|
|
|
|
2020-03-11 18:29:20 +01:00
|
|
|
|
2017-01-13 20:23:59 +01:00
|
|
|
if __name__ == '__main__':
|
2024-07-16 23:05:14 +02:00
|
|
|
AssumeValidTest(__file__).main()
|