Skip to content

Commit cde7ab2

Browse files
committed
Add RPC tests for the CHECKLOCKTIMEVERIFY (BIP65) soft-fork
bip65-cltv.py is based on the earlier BIP66 soft-fork RPC test implemented by Pieter Wuille's 819bcf9 bip65-cltv-p2p.py is based on the earlier BIP66 P2P test by Suhas Daftuar's d76412b
1 parent 287f54f commit cde7ab2

File tree

3 files changed

+266
-0
lines changed

3 files changed

+266
-0
lines changed

qa/pull-tester/rpc-tests.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@
6969
'p2p-fullblocktest.py',
7070
]
7171
testScriptsExt = [
72+
'bip65-cltv.py',
73+
'bip65-cltv-p2p.py',
7274
'bipdersig-p2p.py',
7375
'bipdersig.py',
7476
'getblocktemplate_longpoll.py',

qa/rpc-tests/bip65-cltv-p2p.py

Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
#!/usr/bin/env python2
2+
#
3+
# Distributed under the MIT/X11 software license, see the accompanying
4+
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
5+
#
6+
7+
from test_framework.test_framework import ComparisonTestFramework
8+
from test_framework.util import *
9+
from test_framework.mininode import CTransaction, NetworkThread
10+
from test_framework.blocktools import create_coinbase, create_block
11+
from test_framework.comptool import TestInstance, TestManager
12+
from test_framework.script import CScript, OP_1NEGATE, OP_NOP2, OP_DROP
13+
from binascii import hexlify, unhexlify
14+
import cStringIO
15+
import time
16+
17+
def cltv_invalidate(tx):
18+
'''Modify the signature in vin 0 of the tx to fail CLTV
19+
20+
Prepends -1 CLTV DROP in the scriptSig itself.
21+
'''
22+
tx.vin[0].scriptSig = CScript([OP_1NEGATE, OP_NOP2, OP_DROP] +
23+
list(CScript(tx.vin[0].scriptSig)))
24+
25+
'''
26+
This test is meant to exercise BIP65 (CHECKLOCKTIMEVERIFY)
27+
Connect to a single node.
28+
Mine 2 (version 3) blocks (save the coinbases for later).
29+
Generate 98 more version 3 blocks, verify the node accepts.
30+
Mine 749 version 4 blocks, verify the node accepts.
31+
Check that the new CLTV rules are not enforced on the 750th version 4 block.
32+
Check that the new CLTV rules are enforced on the 751st version 4 block.
33+
Mine 199 new version blocks.
34+
Mine 1 old-version block.
35+
Mine 1 new version block.
36+
Mine 1 old version block, see that the node rejects.
37+
'''
38+
39+
class BIP65Test(ComparisonTestFramework):
40+
41+
def __init__(self):
42+
self.num_nodes = 1
43+
44+
def setup_network(self):
45+
# Must set the blockversion for this test
46+
self.nodes = start_nodes(1, self.options.tmpdir,
47+
extra_args=[['-debug', '-whitelist=127.0.0.1', '-blockversion=3']],
48+
binary=[self.options.testbinary])
49+
50+
def run_test(self):
51+
test = TestManager(self, self.options.tmpdir)
52+
test.add_all_connections(self.nodes)
53+
NetworkThread().start() # Start up network handling in another thread
54+
test.run()
55+
56+
def create_transaction(self, node, coinbase, to_address, amount):
57+
from_txid = node.getblock(coinbase)['tx'][0]
58+
inputs = [{ "txid" : from_txid, "vout" : 0}]
59+
outputs = { to_address : amount }
60+
rawtx = node.createrawtransaction(inputs, outputs)
61+
signresult = node.signrawtransaction(rawtx)
62+
tx = CTransaction()
63+
f = cStringIO.StringIO(unhexlify(signresult['hex']))
64+
tx.deserialize(f)
65+
return tx
66+
67+
def get_tests(self):
68+
69+
self.coinbase_blocks = self.nodes[0].generate(2)
70+
self.tip = int ("0x" + self.nodes[0].getbestblockhash() + "L", 0)
71+
self.nodeaddress = self.nodes[0].getnewaddress()
72+
self.last_block_time = time.time()
73+
74+
''' 98 more version 3 blocks '''
75+
test_blocks = []
76+
for i in xrange(98):
77+
block = create_block(self.tip, create_coinbase(2), self.last_block_time + 1)
78+
block.nVersion = 3
79+
block.rehash()
80+
block.solve()
81+
test_blocks.append([block, True])
82+
self.last_block_time += 1
83+
self.tip = block.sha256
84+
yield TestInstance(test_blocks, sync_every_block=False)
85+
86+
''' Mine 749 version 4 blocks '''
87+
test_blocks = []
88+
for i in xrange(749):
89+
block = create_block(self.tip, create_coinbase(2), self.last_block_time + 1)
90+
block.nVersion = 4
91+
block.rehash()
92+
block.solve()
93+
test_blocks.append([block, True])
94+
self.last_block_time += 1
95+
self.tip = block.sha256
96+
yield TestInstance(test_blocks, sync_every_block=False)
97+
98+
'''
99+
Check that the new CLTV rules are not enforced in the 750th
100+
version 3 block.
101+
'''
102+
spendtx = self.create_transaction(self.nodes[0],
103+
self.coinbase_blocks[0], self.nodeaddress, 1.0)
104+
cltv_invalidate(spendtx)
105+
spendtx.rehash()
106+
107+
block = create_block(self.tip, create_coinbase(2), self.last_block_time + 1)
108+
block.nVersion = 4
109+
block.vtx.append(spendtx)
110+
block.hashMerkleRoot = block.calc_merkle_root()
111+
block.rehash()
112+
block.solve()
113+
114+
self.last_block_time += 1
115+
self.tip = block.sha256
116+
yield TestInstance([[block, True]])
117+
118+
'''
119+
Check that the new CLTV rules are enforced in the 751st version 4
120+
block.
121+
'''
122+
spendtx = self.create_transaction(self.nodes[0],
123+
self.coinbase_blocks[1], self.nodeaddress, 1.0)
124+
cltv_invalidate(spendtx)
125+
spendtx.rehash()
126+
127+
block = create_block(self.tip, create_coinbase(1), self.last_block_time + 1)
128+
block.nVersion = 4
129+
block.vtx.append(spendtx)
130+
block.hashMerkleRoot = block.calc_merkle_root()
131+
block.rehash()
132+
block.solve()
133+
self.last_block_time += 1
134+
yield TestInstance([[block, False]])
135+
136+
''' Mine 199 new version blocks on last valid tip '''
137+
test_blocks = []
138+
for i in xrange(199):
139+
block = create_block(self.tip, create_coinbase(1), self.last_block_time + 1)
140+
block.nVersion = 4
141+
block.rehash()
142+
block.solve()
143+
test_blocks.append([block, True])
144+
self.last_block_time += 1
145+
self.tip = block.sha256
146+
yield TestInstance(test_blocks, sync_every_block=False)
147+
148+
''' Mine 1 old version block '''
149+
block = create_block(self.tip, create_coinbase(1), self.last_block_time + 1)
150+
block.nVersion = 3
151+
block.rehash()
152+
block.solve()
153+
self.last_block_time += 1
154+
self.tip = block.sha256
155+
yield TestInstance([[block, True]])
156+
157+
''' Mine 1 new version block '''
158+
block = create_block(self.tip, create_coinbase(1), self.last_block_time + 1)
159+
block.nVersion = 4
160+
block.rehash()
161+
block.solve()
162+
self.last_block_time += 1
163+
self.tip = block.sha256
164+
yield TestInstance([[block, True]])
165+
166+
''' Mine 1 old version block, should be invalid '''
167+
block = create_block(self.tip, create_coinbase(1), self.last_block_time + 1)
168+
block.nVersion = 3
169+
block.rehash()
170+
block.solve()
171+
self.last_block_time += 1
172+
yield TestInstance([[block, False]])
173+
174+
if __name__ == '__main__':
175+
BIP65Test().main()

qa/rpc-tests/bip65-cltv.py

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
#!/usr/bin/env python2
2+
# Copyright (c) 2015 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+
#
7+
# Test the CHECKLOCKTIMEVERIFY (BIP65) soft-fork logic
8+
#
9+
10+
from test_framework.test_framework import BitcoinTestFramework
11+
from test_framework.util import *
12+
import os
13+
import shutil
14+
15+
class BIP65Test(BitcoinTestFramework):
16+
17+
def setup_network(self):
18+
self.nodes = []
19+
self.nodes.append(start_node(0, self.options.tmpdir, []))
20+
self.nodes.append(start_node(1, self.options.tmpdir, ["-blockversion=3"]))
21+
self.nodes.append(start_node(2, self.options.tmpdir, ["-blockversion=4"]))
22+
connect_nodes(self.nodes[1], 0)
23+
connect_nodes(self.nodes[2], 0)
24+
self.is_network_split = False
25+
self.sync_all()
26+
27+
def run_test(self):
28+
cnt = self.nodes[0].getblockcount()
29+
30+
# Mine some old-version blocks
31+
self.nodes[1].generate(100)
32+
self.sync_all()
33+
if (self.nodes[0].getblockcount() != cnt + 100):
34+
raise AssertionError("Failed to mine 100 version=3 blocks")
35+
36+
# Mine 750 new-version blocks
37+
for i in xrange(15):
38+
self.nodes[2].generate(50)
39+
self.sync_all()
40+
if (self.nodes[0].getblockcount() != cnt + 850):
41+
raise AssertionError("Failed to mine 750 version=4 blocks")
42+
43+
# TODO: check that new CHECKLOCKTIMEVERIFY rules are not enforced
44+
45+
# Mine 1 new-version block
46+
self.nodes[2].generate(1)
47+
self.sync_all()
48+
if (self.nodes[0].getblockcount() != cnt + 851):
49+
raise AssertionFailure("Failed to mine a version=4 blocks")
50+
51+
# TODO: check that new CHECKLOCKTIMEVERIFY rules are enforced
52+
53+
# Mine 198 new-version blocks
54+
for i in xrange(2):
55+
self.nodes[2].generate(99)
56+
self.sync_all()
57+
if (self.nodes[0].getblockcount() != cnt + 1049):
58+
raise AssertionError("Failed to mine 198 version=4 blocks")
59+
60+
# Mine 1 old-version block
61+
self.nodes[1].generate(1)
62+
self.sync_all()
63+
if (self.nodes[0].getblockcount() != cnt + 1050):
64+
raise AssertionError("Failed to mine a version=3 block after 949 version=4 blocks")
65+
66+
# Mine 1 new-version blocks
67+
self.nodes[2].generate(1)
68+
self.sync_all()
69+
if (self.nodes[0].getblockcount() != cnt + 1051):
70+
raise AssertionError("Failed to mine a version=4 block")
71+
72+
# Mine 1 old-version blocks
73+
try:
74+
self.nodes[1].generate(1)
75+
raise AssertionError("Succeeded to mine a version=3 block after 950 version=4 blocks")
76+
except JSONRPCException:
77+
pass
78+
self.sync_all()
79+
if (self.nodes[0].getblockcount() != cnt + 1051):
80+
raise AssertionError("Accepted a version=3 block after 950 version=4 blocks")
81+
82+
# Mine 1 new-version blocks
83+
self.nodes[2].generate(1)
84+
self.sync_all()
85+
if (self.nodes[0].getblockcount() != cnt + 1052):
86+
raise AssertionError("Failed to mine a version=4 block")
87+
88+
if __name__ == '__main__':
89+
BIP65Test().main()

0 commit comments

Comments
 (0)