scripted-diff: Use the C++11 keyword nullptr to denote the pointer literal instead...
[bitcoinplatinum.git] / test / functional / bipdersig-p2p.py
blob31c7ebba90c820b6dd910c9bac9b1a986774d40a
1 #!/usr/bin/env python3
2 # Copyright (c) 2015-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 """Test BIP66 (DER SIG).
7 Connect to a single node.
8 Mine 2 (version 2) blocks (save the coinbases for later).
9 Generate 98 more version 2 blocks, verify the node accepts.
10 Mine 749 version 3 blocks, verify the node accepts.
11 Check that the new DERSIG rules are not enforced on the 750th version 3 block.
12 Check that the new DERSIG rules are enforced on the 751st version 3 block.
13 Mine 199 new version blocks.
14 Mine 1 old-version block.
15 Mine 1 new version block.
16 Mine 1 old version block, see that the node rejects.
17 """
19 from test_framework.test_framework import ComparisonTestFramework
20 from test_framework.util import *
21 from test_framework.mininode import CTransaction, NetworkThread
22 from test_framework.blocktools import create_coinbase, create_block
23 from test_framework.comptool import TestInstance, TestManager
24 from test_framework.script import CScript
25 from io import BytesIO
26 import time
28 # A canonical signature consists of:
29 # <30> <total len> <02> <len R> <R> <02> <len S> <S> <hashtype>
30 def unDERify(tx):
31 """
32 Make the signature in vin 0 of a tx non-DER-compliant,
33 by adding padding after the S-value.
34 """
35 scriptSig = CScript(tx.vin[0].scriptSig)
36 newscript = []
37 for i in scriptSig:
38 if (len(newscript) == 0):
39 newscript.append(i[0:-1] + b'\0' + i[-1:])
40 else:
41 newscript.append(i)
42 tx.vin[0].scriptSig = CScript(newscript)
44 class BIP66Test(ComparisonTestFramework):
46 def __init__(self):
47 super().__init__()
48 self.num_nodes = 1
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()
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 = BytesIO(hex_str_to_bytes(signresult['hex']))
64 tx.deserialize(f)
65 return tx
67 def get_tests(self):
69 self.coinbase_blocks = self.nodes[0].generate(2)
70 height = 3 # height of the next block to build
71 self.tip = int("0x" + self.nodes[0].getbestblockhash(), 0)
72 self.nodeaddress = self.nodes[0].getnewaddress()
73 self.last_block_time = int(time.time())
75 ''' 298 more version 2 blocks '''
76 test_blocks = []
77 for i in range(298):
78 block = create_block(self.tip, create_coinbase(height), self.last_block_time + 1)
79 block.nVersion = 2
80 block.rehash()
81 block.solve()
82 test_blocks.append([block, True])
83 self.last_block_time += 1
84 self.tip = block.sha256
85 height += 1
86 yield TestInstance(test_blocks, sync_every_block=False)
88 ''' Mine 749 version 3 blocks '''
89 test_blocks = []
90 for i in range(749):
91 block = create_block(self.tip, create_coinbase(height), self.last_block_time + 1)
92 block.nVersion = 3
93 block.rehash()
94 block.solve()
95 test_blocks.append([block, True])
96 self.last_block_time += 1
97 self.tip = block.sha256
98 height += 1
99 yield TestInstance(test_blocks, sync_every_block=False)
101 '''
102 Check that the new DERSIG rules are not enforced in the 750th
103 version 3 block.
105 spendtx = self.create_transaction(self.nodes[0],
106 self.coinbase_blocks[0], self.nodeaddress, 1.0)
107 unDERify(spendtx)
108 spendtx.rehash()
110 block = create_block(self.tip, create_coinbase(height), self.last_block_time + 1)
111 block.nVersion = 3
112 block.vtx.append(spendtx)
113 block.hashMerkleRoot = block.calc_merkle_root()
114 block.rehash()
115 block.solve()
117 self.last_block_time += 1
118 self.tip = block.sha256
119 height += 1
120 yield TestInstance([[block, True]])
122 ''' Mine 199 new version blocks on last valid tip '''
123 test_blocks = []
124 for i in range(199):
125 block = create_block(self.tip, create_coinbase(height), self.last_block_time + 1)
126 block.nVersion = 3
127 block.rehash()
128 block.solve()
129 test_blocks.append([block, True])
130 self.last_block_time += 1
131 self.tip = block.sha256
132 height += 1
133 yield TestInstance(test_blocks, sync_every_block=False)
135 ''' Mine 1 old version block '''
136 block = create_block(self.tip, create_coinbase(height), self.last_block_time + 1)
137 block.nVersion = 2
138 block.rehash()
139 block.solve()
140 self.last_block_time += 1
141 self.tip = block.sha256
142 height += 1
143 yield TestInstance([[block, True]])
145 ''' Mine 1 new version block '''
146 block = create_block(self.tip, create_coinbase(height), self.last_block_time + 1)
147 block.nVersion = 3
148 block.rehash()
149 block.solve()
150 self.last_block_time += 1
151 self.tip = block.sha256
152 height += 1
153 yield TestInstance([[block, True]])
155 '''
156 Check that the new DERSIG rules are enforced in the 951st version 3
157 block.
159 spendtx = self.create_transaction(self.nodes[0],
160 self.coinbase_blocks[1], self.nodeaddress, 1.0)
161 unDERify(spendtx)
162 spendtx.rehash()
164 block = create_block(self.tip, create_coinbase(height), self.last_block_time + 1)
165 block.nVersion = 3
166 block.vtx.append(spendtx)
167 block.hashMerkleRoot = block.calc_merkle_root()
168 block.rehash()
169 block.solve()
170 self.last_block_time += 1
171 yield TestInstance([[block, False]])
173 ''' Mine 1 old version block, should be invalid '''
174 block = create_block(self.tip, create_coinbase(height), self.last_block_time + 1)
175 block.nVersion = 2
176 block.rehash()
177 block.solve()
178 self.last_block_time += 1
179 yield TestInstance([[block, False]])
181 if __name__ == '__main__':
182 BIP66Test().main()