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