You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
189 lines
6.7 KiB
189 lines
6.7 KiB
9 years ago
|
#!/usr/bin/env python3
|
||
|
# Copyright (c) 2015-2016 The Bitcoin Core developers
|
||
|
# Distributed under the MIT software license, see the accompanying
|
||
10 years ago
|
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
||
8 years ago
|
"""Test BIP66 (DER SIG).
|
||
|
|
||
|
Connect to a single node.
|
||
|
Mine 2 (version 2) blocks (save the coinbases for later).
|
||
|
Generate 98 more version 2 blocks, verify the node accepts.
|
||
|
Mine 749 version 3 blocks, verify the node accepts.
|
||
|
Check that the new DERSIG rules are not enforced on the 750th version 3 block.
|
||
|
Check that the new DERSIG rules are enforced on the 751st version 3 block.
|
||
|
Mine 199 new version blocks.
|
||
|
Mine 1 old-version block.
|
||
|
Mine 1 new version block.
|
||
|
Mine 1 old version block, see that the node rejects.
|
||
|
"""
|
||
10 years ago
|
|
||
10 years ago
|
from test_framework.test_framework import ComparisonTestFramework
|
||
|
from test_framework.util import *
|
||
|
from test_framework.mininode import CTransaction, NetworkThread
|
||
|
from test_framework.blocktools import create_coinbase, create_block
|
||
|
from test_framework.comptool import TestInstance, TestManager
|
||
|
from test_framework.script import CScript
|
||
9 years ago
|
from io import BytesIO
|
||
10 years ago
|
import time
|
||
|
|
||
|
# A canonical signature consists of:
|
||
|
# <30> <total len> <02> <len R> <R> <02> <len S> <S> <hashtype>
|
||
|
def unDERify(tx):
|
||
8 years ago
|
"""
|
||
10 years ago
|
Make the signature in vin 0 of a tx non-DER-compliant,
|
||
|
by adding padding after the S-value.
|
||
8 years ago
|
"""
|
||
10 years ago
|
scriptSig = CScript(tx.vin[0].scriptSig)
|
||
|
newscript = []
|
||
|
for i in scriptSig:
|
||
|
if (len(newscript) == 0):
|
||
9 years ago
|
newscript.append(i[0:-1] + b'\0' + i[-1:])
|
||
10 years ago
|
else:
|
||
|
newscript.append(i)
|
||
|
tx.vin[0].scriptSig = CScript(newscript)
|
||
|
|
||
|
class BIP66Test(ComparisonTestFramework):
|
||
|
|
||
|
def __init__(self):
|
||
9 years ago
|
super().__init__()
|
||
10 years ago
|
self.num_nodes = 1
|
||
|
|
||
|
def setup_network(self):
|
||
|
# Must set the blockversion for this test
|
||
9 years ago
|
self.nodes = start_nodes(self.num_nodes, self.options.tmpdir,
|
||
8 years ago
|
extra_args=[['-whitelist=127.0.0.1', '-blockversion=2']],
|
||
10 years ago
|
binary=[self.options.testbinary])
|
||
|
|
||
|
def run_test(self):
|
||
|
test = TestManager(self, self.options.tmpdir)
|
||
|
test.add_all_connections(self.nodes)
|
||
|
NetworkThread().start() # Start up network handling in another thread
|
||
|
test.run()
|
||
|
|
||
|
def create_transaction(self, node, coinbase, to_address, amount):
|
||
|
from_txid = node.getblock(coinbase)['tx'][0]
|
||
|
inputs = [{ "txid" : from_txid, "vout" : 0}]
|
||
|
outputs = { to_address : amount }
|
||
|
rawtx = node.createrawtransaction(inputs, outputs)
|
||
|
signresult = node.signrawtransaction(rawtx)
|
||
|
tx = CTransaction()
|
||
9 years ago
|
f = BytesIO(hex_str_to_bytes(signresult['hex']))
|
||
10 years ago
|
tx.deserialize(f)
|
||
|
return tx
|
||
|
|
||
|
def get_tests(self):
|
||
|
|
||
|
self.coinbase_blocks = self.nodes[0].generate(2)
|
||
10 years ago
|
height = 3 # height of the next block to build
|
||
9 years ago
|
self.tip = int("0x" + self.nodes[0].getbestblockhash(), 0)
|
||
10 years ago
|
self.nodeaddress = self.nodes[0].getnewaddress()
|
||
9 years ago
|
self.last_block_time = int(time.time())
|
||
10 years ago
|
|
||
9 years ago
|
''' 298 more version 2 blocks '''
|
||
10 years ago
|
test_blocks = []
|
||
9 years ago
|
for i in range(298):
|
||
10 years ago
|
block = create_block(self.tip, create_coinbase(height), self.last_block_time + 1)
|
||
10 years ago
|
block.nVersion = 2
|
||
|
block.rehash()
|
||
|
block.solve()
|
||
|
test_blocks.append([block, True])
|
||
|
self.last_block_time += 1
|
||
|
self.tip = block.sha256
|
||
10 years ago
|
height += 1
|
||
10 years ago
|
yield TestInstance(test_blocks, sync_every_block=False)
|
||
|
|
||
|
''' Mine 749 version 3 blocks '''
|
||
|
test_blocks = []
|
||
9 years ago
|
for i in range(749):
|
||
10 years ago
|
block = create_block(self.tip, create_coinbase(height), self.last_block_time + 1)
|
||
10 years ago
|
block.nVersion = 3
|
||
|
block.rehash()
|
||
|
block.solve()
|
||
|
test_blocks.append([block, True])
|
||
|
self.last_block_time += 1
|
||
|
self.tip = block.sha256
|
||
10 years ago
|
height += 1
|
||
10 years ago
|
yield TestInstance(test_blocks, sync_every_block=False)
|
||
|
|
||
|
'''
|
||
|
Check that the new DERSIG rules are not enforced in the 750th
|
||
|
version 3 block.
|
||
|
'''
|
||
|
spendtx = self.create_transaction(self.nodes[0],
|
||
|
self.coinbase_blocks[0], self.nodeaddress, 1.0)
|
||
|
unDERify(spendtx)
|
||
|
spendtx.rehash()
|
||
|
|
||
10 years ago
|
block = create_block(self.tip, create_coinbase(height), self.last_block_time + 1)
|
||
10 years ago
|
block.nVersion = 3
|
||
|
block.vtx.append(spendtx)
|
||
|
block.hashMerkleRoot = block.calc_merkle_root()
|
||
|
block.rehash()
|
||
|
block.solve()
|
||
|
|
||
|
self.last_block_time += 1
|
||
|
self.tip = block.sha256
|
||
10 years ago
|
height += 1
|
||
9 years ago
|
yield TestInstance([[block, True]])
|
||
10 years ago
|
|
||
|
''' Mine 199 new version blocks on last valid tip '''
|
||
|
test_blocks = []
|
||
9 years ago
|
for i in range(199):
|
||
10 years ago
|
block = create_block(self.tip, create_coinbase(height), self.last_block_time + 1)
|
||
10 years ago
|
block.nVersion = 3
|
||
|
block.rehash()
|
||
|
block.solve()
|
||
|
test_blocks.append([block, True])
|
||
|
self.last_block_time += 1
|
||
|
self.tip = block.sha256
|
||
10 years ago
|
height += 1
|
||
10 years ago
|
yield TestInstance(test_blocks, sync_every_block=False)
|
||
|
|
||
|
''' Mine 1 old version block '''
|
||
10 years ago
|
block = create_block(self.tip, create_coinbase(height), self.last_block_time + 1)
|
||
10 years ago
|
block.nVersion = 2
|
||
|
block.rehash()
|
||
|
block.solve()
|
||
|
self.last_block_time += 1
|
||
|
self.tip = block.sha256
|
||
10 years ago
|
height += 1
|
||
10 years ago
|
yield TestInstance([[block, True]])
|
||
|
|
||
|
''' Mine 1 new version block '''
|
||
10 years ago
|
block = create_block(self.tip, create_coinbase(height), self.last_block_time + 1)
|
||
10 years ago
|
block.nVersion = 3
|
||
|
block.rehash()
|
||
|
block.solve()
|
||
|
self.last_block_time += 1
|
||
|
self.tip = block.sha256
|
||
10 years ago
|
height += 1
|
||
10 years ago
|
yield TestInstance([[block, True]])
|
||
|
|
||
9 years ago
|
'''
|
||
|
Check that the new DERSIG rules are enforced in the 951st version 3
|
||
|
block.
|
||
|
'''
|
||
|
spendtx = self.create_transaction(self.nodes[0],
|
||
|
self.coinbase_blocks[1], self.nodeaddress, 1.0)
|
||
|
unDERify(spendtx)
|
||
|
spendtx.rehash()
|
||
|
|
||
|
block = create_block(self.tip, create_coinbase(height), self.last_block_time + 1)
|
||
|
block.nVersion = 3
|
||
|
block.vtx.append(spendtx)
|
||
|
block.hashMerkleRoot = block.calc_merkle_root()
|
||
|
block.rehash()
|
||
|
block.solve()
|
||
|
self.last_block_time += 1
|
||
|
yield TestInstance([[block, False]])
|
||
|
|
||
10 years ago
|
''' Mine 1 old version block, should be invalid '''
|
||
10 years ago
|
block = create_block(self.tip, create_coinbase(height), self.last_block_time + 1)
|
||
10 years ago
|
block.nVersion = 2
|
||
|
block.rehash()
|
||
|
block.solve()
|
||
|
self.last_block_time += 1
|
||
|
yield TestInstance([[block, False]])
|
||
|
|
||
|
if __name__ == '__main__':
|
||
|
BIP66Test().main()
|