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.
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
28 # A canonical signature consists of:
29 # <30> <total len> <02> <len R> <R> <02> <len S> <S> <hashtype>
32 Make the signature in vin 0 of a tx non-DER-compliant,
33 by adding padding after the S-value.
35 scriptSig
= CScript(tx
.vin
[0].scriptSig
)
38 if (len(newscript
) == 0):
39 newscript
.append(i
[0:-1] + b
'\0' + i
[-1:])
42 tx
.vin
[0].scriptSig
= CScript(newscript
)
44 class BIP66Test(ComparisonTestFramework
):
51 test
= TestManager(self
, self
.options
.tmpdir
)
52 test
.add_all_connections(self
.nodes
)
53 NetworkThread().start() # Start up network handling in another thread
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
)
63 f
= BytesIO(hex_str_to_bytes(signresult
['hex']))
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 '''
78 block
= create_block(self
.tip
, create_coinbase(height
), self
.last_block_time
+ 1)
82 test_blocks
.append([block
, True])
83 self
.last_block_time
+= 1
84 self
.tip
= block
.sha256
86 yield TestInstance(test_blocks
, sync_every_block
=False)
88 ''' Mine 749 version 3 blocks '''
91 block
= create_block(self
.tip
, create_coinbase(height
), self
.last_block_time
+ 1)
95 test_blocks
.append([block
, True])
96 self
.last_block_time
+= 1
97 self
.tip
= block
.sha256
99 yield TestInstance(test_blocks
, sync_every_block
=False)
102 Check that the new DERSIG rules are not enforced in the 750th
105 spendtx
= self
.create_transaction(self
.nodes
[0],
106 self
.coinbase_blocks
[0], self
.nodeaddress
, 1.0)
110 block
= create_block(self
.tip
, create_coinbase(height
), self
.last_block_time
+ 1)
112 block
.vtx
.append(spendtx
)
113 block
.hashMerkleRoot
= block
.calc_merkle_root()
117 self
.last_block_time
+= 1
118 self
.tip
= block
.sha256
120 yield TestInstance([[block
, True]])
122 ''' Mine 199 new version blocks on last valid tip '''
125 block
= create_block(self
.tip
, create_coinbase(height
), self
.last_block_time
+ 1)
129 test_blocks
.append([block
, True])
130 self
.last_block_time
+= 1
131 self
.tip
= block
.sha256
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)
140 self
.last_block_time
+= 1
141 self
.tip
= block
.sha256
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)
150 self
.last_block_time
+= 1
151 self
.tip
= block
.sha256
153 yield TestInstance([[block
, True]])
156 Check that the new DERSIG rules are enforced in the 951st version 3
159 spendtx
= self
.create_transaction(self
.nodes
[0],
160 self
.coinbase_blocks
[1], self
.nodeaddress
, 1.0)
164 block
= create_block(self
.tip
, create_coinbase(height
), self
.last_block_time
+ 1)
166 block
.vtx
.append(spendtx
)
167 block
.hashMerkleRoot
= block
.calc_merkle_root()
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)
178 self
.last_block_time
+= 1
179 yield TestInstance([[block
, False]])
181 if __name__
== '__main__':