2015-06-28 11:42:17 -07:00
|
|
|
#!/usr/bin/env python2
|
2015-01-05 12:40:24 -08:00
|
|
|
# Copyright (c) 2015 The Bitcoin Core developers
|
2015-06-28 11:42:17 -07:00
|
|
|
# Distributed under the MIT/X11 software license, see the accompanying
|
|
|
|
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
#
|
|
|
|
|
|
|
|
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
|
2015-12-14 11:21:34 -08:00
|
|
|
from test_framework.script import CScript, OP_1NEGATE, OP_CHECKLOCKTIMEVERIFY, OP_DROP
|
2015-12-02 09:12:23 -08:00
|
|
|
from binascii import unhexlify
|
2016-03-19 13:36:32 -07:00
|
|
|
from io import BytesIO
|
2015-06-28 11:42:17 -07:00
|
|
|
import time
|
|
|
|
|
|
|
|
def cltv_invalidate(tx):
|
|
|
|
'''Modify the signature in vin 0 of the tx to fail CLTV
|
|
|
|
|
|
|
|
Prepends -1 CLTV DROP in the scriptSig itself.
|
|
|
|
'''
|
2015-12-14 11:21:34 -08:00
|
|
|
tx.vin[0].scriptSig = CScript([OP_1NEGATE, OP_CHECKLOCKTIMEVERIFY, OP_DROP] +
|
2015-06-28 11:42:17 -07:00
|
|
|
list(CScript(tx.vin[0].scriptSig)))
|
|
|
|
|
|
|
|
'''
|
|
|
|
This test is meant to exercise BIP65 (CHECKLOCKTIMEVERIFY)
|
|
|
|
Connect to a single node.
|
|
|
|
Mine 2 (version 3) blocks (save the coinbases for later).
|
|
|
|
Generate 98 more version 3 blocks, verify the node accepts.
|
|
|
|
Mine 749 version 4 blocks, verify the node accepts.
|
|
|
|
Check that the new CLTV rules are not enforced on the 750th version 4 block.
|
|
|
|
Check that the new CLTV rules are enforced on the 751st version 4 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.
|
|
|
|
'''
|
|
|
|
|
|
|
|
class BIP65Test(ComparisonTestFramework):
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
self.num_nodes = 1
|
|
|
|
|
|
|
|
def setup_network(self):
|
|
|
|
# Must set the blockversion for this test
|
|
|
|
self.nodes = start_nodes(1, self.options.tmpdir,
|
|
|
|
extra_args=[['-debug', '-whitelist=127.0.0.1', '-blockversion=3']],
|
|
|
|
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()
|
2016-03-19 13:36:32 -07:00
|
|
|
f = BytesIO(unhexlify(signresult['hex']))
|
2015-06-28 11:42:17 -07:00
|
|
|
tx.deserialize(f)
|
|
|
|
return tx
|
|
|
|
|
|
|
|
def get_tests(self):
|
|
|
|
|
|
|
|
self.coinbase_blocks = self.nodes[0].generate(2)
|
2015-10-27 12:36:06 -07:00
|
|
|
height = 3 # height of the next block to build
|
2015-06-28 11:42:17 -07:00
|
|
|
self.tip = int ("0x" + self.nodes[0].getbestblockhash() + "L", 0)
|
|
|
|
self.nodeaddress = self.nodes[0].getnewaddress()
|
|
|
|
self.last_block_time = time.time()
|
|
|
|
|
|
|
|
''' 98 more version 3 blocks '''
|
|
|
|
test_blocks = []
|
|
|
|
for i in xrange(98):
|
2015-10-27 12:36:06 -07:00
|
|
|
block = create_block(self.tip, create_coinbase(height), self.last_block_time + 1)
|
2015-06-28 11:42:17 -07:00
|
|
|
block.nVersion = 3
|
|
|
|
block.rehash()
|
|
|
|
block.solve()
|
|
|
|
test_blocks.append([block, True])
|
|
|
|
self.last_block_time += 1
|
|
|
|
self.tip = block.sha256
|
2015-10-27 12:36:06 -07:00
|
|
|
height += 1
|
2015-06-28 11:42:17 -07:00
|
|
|
yield TestInstance(test_blocks, sync_every_block=False)
|
|
|
|
|
|
|
|
''' Mine 749 version 4 blocks '''
|
|
|
|
test_blocks = []
|
|
|
|
for i in xrange(749):
|
2015-10-27 12:36:06 -07:00
|
|
|
block = create_block(self.tip, create_coinbase(height), self.last_block_time + 1)
|
2015-06-28 11:42:17 -07:00
|
|
|
block.nVersion = 4
|
|
|
|
block.rehash()
|
|
|
|
block.solve()
|
|
|
|
test_blocks.append([block, True])
|
|
|
|
self.last_block_time += 1
|
|
|
|
self.tip = block.sha256
|
2015-10-27 12:36:06 -07:00
|
|
|
height += 1
|
2015-06-28 11:42:17 -07:00
|
|
|
yield TestInstance(test_blocks, sync_every_block=False)
|
|
|
|
|
|
|
|
'''
|
|
|
|
Check that the new CLTV 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)
|
|
|
|
cltv_invalidate(spendtx)
|
|
|
|
spendtx.rehash()
|
|
|
|
|
2015-10-27 12:36:06 -07:00
|
|
|
block = create_block(self.tip, create_coinbase(height), self.last_block_time + 1)
|
2015-06-28 11:42:17 -07:00
|
|
|
block.nVersion = 4
|
|
|
|
block.vtx.append(spendtx)
|
|
|
|
block.hashMerkleRoot = block.calc_merkle_root()
|
|
|
|
block.rehash()
|
|
|
|
block.solve()
|
|
|
|
|
|
|
|
self.last_block_time += 1
|
|
|
|
self.tip = block.sha256
|
2015-10-27 12:36:06 -07:00
|
|
|
height += 1
|
2015-06-28 11:42:17 -07:00
|
|
|
yield TestInstance([[block, True]])
|
|
|
|
|
|
|
|
'''
|
|
|
|
Check that the new CLTV rules are enforced in the 751st version 4
|
|
|
|
block.
|
|
|
|
'''
|
|
|
|
spendtx = self.create_transaction(self.nodes[0],
|
|
|
|
self.coinbase_blocks[1], self.nodeaddress, 1.0)
|
|
|
|
cltv_invalidate(spendtx)
|
|
|
|
spendtx.rehash()
|
|
|
|
|
2015-10-27 12:36:06 -07:00
|
|
|
block = create_block(self.tip, create_coinbase(height), self.last_block_time + 1)
|
2015-06-28 11:42:17 -07:00
|
|
|
block.nVersion = 4
|
|
|
|
block.vtx.append(spendtx)
|
|
|
|
block.hashMerkleRoot = block.calc_merkle_root()
|
|
|
|
block.rehash()
|
|
|
|
block.solve()
|
|
|
|
self.last_block_time += 1
|
|
|
|
yield TestInstance([[block, False]])
|
|
|
|
|
|
|
|
''' Mine 199 new version blocks on last valid tip '''
|
|
|
|
test_blocks = []
|
|
|
|
for i in xrange(199):
|
2015-10-27 12:36:06 -07:00
|
|
|
block = create_block(self.tip, create_coinbase(height), self.last_block_time + 1)
|
2015-06-28 11:42:17 -07:00
|
|
|
block.nVersion = 4
|
|
|
|
block.rehash()
|
|
|
|
block.solve()
|
|
|
|
test_blocks.append([block, True])
|
|
|
|
self.last_block_time += 1
|
|
|
|
self.tip = block.sha256
|
2015-10-27 12:36:06 -07:00
|
|
|
height += 1
|
2015-06-28 11:42:17 -07:00
|
|
|
yield TestInstance(test_blocks, sync_every_block=False)
|
|
|
|
|
|
|
|
''' Mine 1 old version block '''
|
2015-10-27 12:36:06 -07:00
|
|
|
block = create_block(self.tip, create_coinbase(height), self.last_block_time + 1)
|
2015-06-28 11:42:17 -07:00
|
|
|
block.nVersion = 3
|
|
|
|
block.rehash()
|
|
|
|
block.solve()
|
|
|
|
self.last_block_time += 1
|
|
|
|
self.tip = block.sha256
|
2015-10-27 12:36:06 -07:00
|
|
|
height += 1
|
2015-06-28 11:42:17 -07:00
|
|
|
yield TestInstance([[block, True]])
|
|
|
|
|
|
|
|
''' Mine 1 new version block '''
|
2015-10-27 12:36:06 -07:00
|
|
|
block = create_block(self.tip, create_coinbase(height), self.last_block_time + 1)
|
2015-06-28 11:42:17 -07:00
|
|
|
block.nVersion = 4
|
|
|
|
block.rehash()
|
|
|
|
block.solve()
|
|
|
|
self.last_block_time += 1
|
|
|
|
self.tip = block.sha256
|
2015-10-27 12:36:06 -07:00
|
|
|
height += 1
|
2015-06-28 11:42:17 -07:00
|
|
|
yield TestInstance([[block, True]])
|
|
|
|
|
|
|
|
''' Mine 1 old version block, should be invalid '''
|
2015-10-27 12:36:06 -07:00
|
|
|
block = create_block(self.tip, create_coinbase(height), self.last_block_time + 1)
|
2015-06-28 11:42:17 -07:00
|
|
|
block.nVersion = 3
|
|
|
|
block.rehash()
|
|
|
|
block.solve()
|
|
|
|
self.last_block_time += 1
|
|
|
|
yield TestInstance([[block, False]])
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
BIP65Test().main()
|