Skip to content

Commit 7b5e3fe

Browse files
jnewberygmaxwell
authored andcommitted
Add assumevalid testcase
Adds a qa testcase testing the new "-assumevalid" option. The testcase builds a chain that includes and invalid signature for one of the transactions and sends that chain to three nodes: - node0 has no -assumevalid parameter and rejects the invalid chain. - node1 has -assumevalid set and accepts the invalid chain. - node2 has -assumevalid set but the invalid block is not buried deep enough to assume invalid, and so rejects the invalid chain.
1 parent e440ac7 commit 7b5e3fe

File tree

1 file changed

+191
-0
lines changed

1 file changed

+191
-0
lines changed

qa/rpc-tests/assumevalid.py

Lines changed: 191 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
1+
#!/usr/bin/env python3
2+
# Copyright (c) 2014-2016 The Bitcoin Core developers
3+
# Distributed under the MIT software license, see the accompanying
4+
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
5+
'''
6+
assumevalid.py
7+
8+
Test logic for skipping signature validation on blocks which we've assumed
9+
valid (https://github.com/bitcoin/bitcoin/pull/9484)
10+
11+
We build a chain that includes and invalid signature for one of the
12+
transactions:
13+
14+
0: genesis block
15+
1: block 1 with coinbase transaction output.
16+
2-101: bury that block with 100 blocks so the coinbase transaction
17+
output can be spent
18+
102: a block containing a transaction spending the coinbase
19+
transaction output. The transaction has an invalid signature.
20+
103-2202: bury the bad block with just over two weeks' worth of blocks
21+
(2100 blocks)
22+
23+
Start three nodes:
24+
25+
- node0 has no -assumevalid parameter. Try to sync to block 2202. It will
26+
reject block 102 and only sync as far as block 101
27+
- node1 has -assumevalid set to the hash of block 102. Try to sync to
28+
block 2202. node1 will sync all the way to block 2202.
29+
- node2 has -assumevalid set to the hash of block 102. Try to sync to
30+
block 200. node2 will reject block 102 since it's assumed valid, but it
31+
isn't buried by at least two weeks' work.
32+
'''
33+
34+
from test_framework.mininode import *
35+
from test_framework.test_framework import BitcoinTestFramework
36+
from test_framework.util import *
37+
from test_framework.blocktools import create_block, create_coinbase
38+
from test_framework.key import CECKey
39+
from test_framework.script import *
40+
41+
class BaseNode(SingleNodeConnCB):
42+
def __init__(self):
43+
SingleNodeConnCB.__init__(self)
44+
self.last_inv = None
45+
self.last_headers = None
46+
self.last_block = None
47+
self.last_getdata = None
48+
self.block_announced = False
49+
self.last_getheaders = None
50+
self.disconnected = False
51+
self.last_blockhash_announced = None
52+
53+
def on_close(self, conn):
54+
self.disconnected = True
55+
56+
def wait_for_disconnect(self, timeout=60):
57+
test_function = lambda: self.disconnected
58+
assert(wait_until(test_function, timeout=timeout))
59+
return
60+
61+
def send_header_for_blocks(self, new_blocks):
62+
headers_message = msg_headers()
63+
headers_message.headers = [ CBlockHeader(b) for b in new_blocks ]
64+
self.send_message(headers_message)
65+
66+
class SendHeadersTest(BitcoinTestFramework):
67+
def __init__(self):
68+
super().__init__()
69+
self.setup_clean_chain = True
70+
self.num_nodes = 3
71+
72+
def setup_network(self):
73+
# Start node0. We don't start the other nodes yet since
74+
# we need to pre-mine a block with an invalid transaction
75+
# signature so we can pass in the block hash as assumevalid.
76+
self.nodes = []
77+
self.nodes.append(start_node(0, self.options.tmpdir, ["-debug"]))
78+
79+
def run_test(self):
80+
81+
# Connect to node0
82+
node0 = BaseNode()
83+
connections = []
84+
connections.append(NodeConn('127.0.0.1', p2p_port(0), self.nodes[0], node0))
85+
node0.add_connection(connections[0])
86+
87+
NetworkThread().start() # Start up network handling in another thread
88+
node0.wait_for_verack()
89+
90+
# Build the blockchain
91+
self.tip = int(self.nodes[0].getbestblockhash(), 16)
92+
self.block_time = self.nodes[0].getblock(self.nodes[0].getbestblockhash())['time'] + 1
93+
94+
self.blocks = []
95+
96+
# Get a pubkey for the coinbase TXO
97+
coinbase_key = CECKey()
98+
coinbase_key.set_secretbytes(b"horsebattery")
99+
coinbase_pubkey = coinbase_key.get_pubkey()
100+
101+
# Create the first block with a coinbase output to our key
102+
height = 1
103+
block = create_block(self.tip, create_coinbase(height, coinbase_pubkey), self.block_time)
104+
self.blocks.append(block)
105+
self.block_time += 1
106+
block.solve()
107+
# Save the coinbase for later
108+
self.block1 = block
109+
self.tip = block.sha256
110+
height += 1
111+
112+
# Bury the block 100 deep so the coinbase output is spendable
113+
for i in range(100):
114+
block = create_block(self.tip, create_coinbase(height), self.block_time)
115+
block.solve()
116+
self.blocks.append(block)
117+
self.tip = block.sha256
118+
self.block_time += 1
119+
height += 1
120+
121+
# Create a transaction spending the coinbase output with an invalid (null) signature
122+
tx = CTransaction()
123+
tx.vin.append(CTxIn(COutPoint(self.block1.vtx[0].sha256, 0), scriptSig=b""))
124+
tx.vout.append(CTxOut(49*100000000, CScript([OP_TRUE])))
125+
tx.calc_sha256()
126+
127+
block102 = create_block(self.tip, create_coinbase(height), self.block_time)
128+
self.block_time += 1
129+
block102.vtx.extend([tx])
130+
block102.hashMerkleRoot = block102.calc_merkle_root()
131+
block102.rehash()
132+
block102.solve()
133+
self.blocks.append(block102)
134+
self.tip = block102.sha256
135+
self.block_time += 1
136+
height += 1
137+
138+
# Bury the assumed valid block 2100 deep
139+
for i in range(2100):
140+
block = create_block(self.tip, create_coinbase(height), self.block_time)
141+
block.nVersion = 4
142+
block.solve()
143+
self.blocks.append(block)
144+
self.tip = block.sha256
145+
self.block_time += 1
146+
height += 1
147+
148+
# Start node1 and node2 with assumevalid so they accept a block with a bad signature.
149+
self.nodes.append(start_node(1, self.options.tmpdir,
150+
["-debug", "-assumevalid=" + hex(block102.sha256)]))
151+
node1 = BaseNode() # connects to node1
152+
connections.append(NodeConn('127.0.0.1', p2p_port(1), self.nodes[1], node1))
153+
node1.add_connection(connections[1])
154+
node1.wait_for_verack()
155+
156+
self.nodes.append(start_node(2, self.options.tmpdir,
157+
["-debug", "-assumevalid=" + hex(block102.sha256)]))
158+
node2 = BaseNode() # connects to node2
159+
connections.append(NodeConn('127.0.0.1', p2p_port(2), self.nodes[2], node2))
160+
node2.add_connection(connections[2])
161+
node2.wait_for_verack()
162+
163+
# send header lists to all three nodes
164+
node0.send_header_for_blocks(self.blocks[0:2000])
165+
node0.send_header_for_blocks(self.blocks[2000:])
166+
node1.send_header_for_blocks(self.blocks[0:2000])
167+
node1.send_header_for_blocks(self.blocks[2000:])
168+
node2.send_header_for_blocks(self.blocks[0:200])
169+
170+
# Send 102 blocks to node0. Block 102 will be rejected.
171+
for i in range(101):
172+
node0.send_message(msg_block(self.blocks[i]))
173+
node0.sync_with_ping() # make sure the most recent block is synced
174+
node0.send_message(msg_block(self.blocks[101]))
175+
assert_equal(self.nodes[0].getblock(self.nodes[0].getbestblockhash())['height'], 101)
176+
177+
# Send 3102 blocks to node1. All blocks will be accepted.
178+
for i in range(2202):
179+
node1.send_message(msg_block(self.blocks[i]))
180+
node1.sync_with_ping() # make sure the most recent block is synced
181+
assert_equal(self.nodes[1].getblock(self.nodes[1].getbestblockhash())['height'], 2202)
182+
183+
# Send 102 blocks to node2. Block 102 will be rejected.
184+
for i in range(101):
185+
node2.send_message(msg_block(self.blocks[i]))
186+
node2.sync_with_ping() # make sure the most recent block is synced
187+
node2.send_message(msg_block(self.blocks[101]))
188+
assert_equal(self.nodes[2].getblock(self.nodes[2].getbestblockhash())['height'], 101)
189+
190+
if __name__ == '__main__':
191+
SendHeadersTest().main()

0 commit comments

Comments
 (0)