Dash Core  0.12.2.1
P2P Digital Currency
bip65-cltv-p2p.py
Go to the documentation of this file.
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_CHECKLOCKTIMEVERIFY, OP_DROP
13 from io import BytesIO
14 import time
15 
17  '''Modify the signature in vin 0 of the tx to fail CLTV
18 
19  Prepends -1 CLTV DROP in the scriptSig itself.
20  '''
21  tx.vin[0].scriptSig = CScript([OP_1NEGATE, OP_CHECKLOCKTIMEVERIFY, OP_DROP] +
22  list(CScript(tx.vin[0].scriptSig)))
23 
24 '''
25 This test is meant to exercise BIP65 (CHECKLOCKTIMEVERIFY)
26 Connect to a single node.
27 Mine 2 (version 3) blocks (save the coinbases for later).
28 Generate 98 more version 3 blocks, verify the node accepts.
29 Mine 749 version 4 blocks, verify the node accepts.
30 Check that the new CLTV rules are not enforced on the 750th version 4 block.
31 Check that the new CLTV rules are enforced on the 751st version 4 block.
32 Mine 199 new version blocks.
33 Mine 1 old-version block.
34 Mine 1 new version block.
35 Mine 1 old version block, see that the node rejects.
36 '''
37 
39 
40  def __init__(self):
41  self.num_nodes = 1
42 
43  def setup_network(self):
44  # Must set the blockversion for this test
45  self.nodes = start_nodes(1, self.options.tmpdir,
46  extra_args=[['-debug', '-whitelist=127.0.0.1', '-blockversion=3']],
47  binary=[self.options.testbinary])
48 
49  def run_test(self):
50  test = TestManager(self, self.options.tmpdir)
51  test.add_all_connections(self.nodes)
52  NetworkThread().start() # Start up network handling in another thread
53  test.run()
54 
55  def create_transaction(self, node, coinbase, to_address, amount):
56  from_txid = node.getblock(coinbase)['tx'][0]
57  inputs = [{ "txid" : from_txid, "vout" : 0}]
58  outputs = { to_address : amount }
59  rawtx = node.createrawtransaction(inputs, outputs)
60  signresult = node.signrawtransaction(rawtx)
61  tx = CTransaction()
62  f = BytesIO(hex_str_to_bytes(signresult['hex']))
63  tx.deserialize(f)
64  return tx
65 
66  def get_tests(self):
67 
68  self.coinbase_blocks = self.nodes[0].generate(2)
69  height = 3 # height of the next block to build
70  self.tip = int ("0x" + self.nodes[0].getbestblockhash() + "L", 0)
71  self.nodeaddress = self.nodes[0].getnewaddress()
72  self.last_block_time = int(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(height), 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  height += 1
85  yield TestInstance(test_blocks, sync_every_block=False)
86 
87  ''' Mine 749 version 4 blocks '''
88  test_blocks = []
89  for i in xrange(749):
90  block = create_block(self.tip, create_coinbase(height), self.last_block_time + 1)
91  block.nVersion = 4
92  block.rehash()
93  block.solve()
94  test_blocks.append([block, True])
95  self.last_block_time += 1
96  self.tip = block.sha256
97  height += 1
98  yield TestInstance(test_blocks, sync_every_block=False)
99 
100  '''
101  Check that the new CLTV rules are not enforced in the 750th
102  version 3 block.
103  '''
104  spendtx = self.create_transaction(self.nodes[0],
105  self.coinbase_blocks[0], self.nodeaddress, 1.0)
106  cltv_invalidate(spendtx)
107  spendtx.rehash()
108 
109  block = create_block(self.tip, create_coinbase(height), self.last_block_time + 1)
110  block.nVersion = 4
111  block.vtx.append(spendtx)
112  block.hashMerkleRoot = block.calc_merkle_root()
113  block.rehash()
114  block.solve()
115 
116  self.last_block_time += 1
117  self.tip = block.sha256
118  height += 1
119  yield TestInstance([[block, True]])
120 
121  '''
122  Check that the new CLTV rules are enforced in the 751st version 4
123  block.
124  '''
125  spendtx = self.create_transaction(self.nodes[0],
126  self.coinbase_blocks[1], self.nodeaddress, 1.0)
127  cltv_invalidate(spendtx)
128  spendtx.rehash()
129 
130  block = create_block(self.tip, create_coinbase(height), self.last_block_time + 1)
131  block.nVersion = 4
132  block.vtx.append(spendtx)
133  block.hashMerkleRoot = block.calc_merkle_root()
134  block.rehash()
135  block.solve()
136  self.last_block_time += 1
137  yield TestInstance([[block, False]])
138 
139  ''' Mine 199 new version blocks on last valid tip '''
140  test_blocks = []
141  for i in xrange(199):
142  block = create_block(self.tip, create_coinbase(height), self.last_block_time + 1)
143  block.nVersion = 4
144  block.rehash()
145  block.solve()
146  test_blocks.append([block, True])
147  self.last_block_time += 1
148  self.tip = block.sha256
149  height += 1
150  yield TestInstance(test_blocks, sync_every_block=False)
151 
152  ''' Mine 1 old version block '''
153  block = create_block(self.tip, create_coinbase(height), self.last_block_time + 1)
154  block.nVersion = 3
155  block.rehash()
156  block.solve()
157  self.last_block_time += 1
158  self.tip = block.sha256
159  height += 1
160  yield TestInstance([[block, True]])
161 
162  ''' Mine 1 new version block '''
163  block = create_block(self.tip, create_coinbase(height), self.last_block_time + 1)
164  block.nVersion = 4
165  block.rehash()
166  block.solve()
167  self.last_block_time += 1
168  self.tip = block.sha256
169  height += 1
170  yield TestInstance([[block, True]])
171 
172  ''' Mine 1 old version block, should be invalid '''
173  block = create_block(self.tip, create_coinbase(height), self.last_block_time + 1)
174  block.nVersion = 3
175  block.rehash()
176  block.solve()
177  self.last_block_time += 1
178  yield TestInstance([[block, False]])
179 
180 if __name__ == '__main__':
181  BIP65Test().main()
def create_block(hashprev, coinbase, nTime=None)
Definition: blocktools.py:11
def cltv_invalidate(tx)
def hex_str_to_bytes(hex_str)
Definition: util.py:111
def start_nodes(num_nodes, dirname, extra_args=None, rpchost=None, binary=None)
Definition: util.py:305
UniValue getnewaddress(const UniValue &params, bool fHelp)
Definition: rpcwallet.cpp:113
def create_coinbase(height, pubkey=None)
Definition: blocktools.py:43
UniValue generate(const UniValue &params, bool fHelp)
Definition: mining.cpp:122
def create_transaction(self, node, coinbase, to_address, amount)
UniValue getbestblockhash(const UniValue &params, bool fHelp)
Definition: blockchain.cpp:148