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.
161 lines
6.5 KiB
161 lines
6.5 KiB
9 years ago
|
#!/usr/bin/env python3
|
||
7 years ago
|
# Copyright (c) 2015-2017 The Bitcoin Core developers
|
||
9 years ago
|
# 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 BIP65 (CHECKLOCKTIMEVERIFY).
|
||
|
|
||
8 years ago
|
Test that the CHECKLOCKTIMEVERIFY soft-fork activates at (regtest) block height
|
||
|
1351.
|
||
8 years ago
|
"""
|
||
10 years ago
|
|
||
8 years ago
|
from test_framework.test_framework import BitcoinTestFramework
|
||
10 years ago
|
from test_framework.util import *
|
||
8 years ago
|
from test_framework.mininode import *
|
||
10 years ago
|
from test_framework.blocktools import create_coinbase, create_block
|
||
8 years ago
|
from test_framework.script import CScript, OP_1NEGATE, OP_CHECKLOCKTIMEVERIFY, OP_DROP, CScriptNum
|
||
9 years ago
|
from io import BytesIO
|
||
8 years ago
|
|
||
|
CLTV_HEIGHT = 1351
|
||
|
|
||
|
# Reject codes that we might receive in this test
|
||
|
REJECT_INVALID = 16
|
||
|
REJECT_OBSOLETE = 17
|
||
|
REJECT_NONSTANDARD = 64
|
||
10 years ago
|
|
||
|
def cltv_invalidate(tx):
|
||
|
'''Modify the signature in vin 0 of the tx to fail CLTV
|
||
|
|
||
|
Prepends -1 CLTV DROP in the scriptSig itself.
|
||
8 years ago
|
|
||
|
TODO: test more ways that transactions using CLTV could be invalid (eg
|
||
|
locktime requirements fail, sequence time requirements fail, etc).
|
||
10 years ago
|
'''
|
||
9 years ago
|
tx.vin[0].scriptSig = CScript([OP_1NEGATE, OP_CHECKLOCKTIMEVERIFY, OP_DROP] +
|
||
10 years ago
|
list(CScript(tx.vin[0].scriptSig)))
|
||
|
|
||
8 years ago
|
def cltv_validate(node, tx, height):
|
||
|
'''Modify the signature in vin 0 of the tx to pass CLTV
|
||
|
Prepends <height> CLTV DROP in the scriptSig, and sets
|
||
|
the locktime to height'''
|
||
|
tx.vin[0].nSequence = 0
|
||
|
tx.nLockTime = height
|
||
|
|
||
|
# Need to re-sign, since nSequence and nLockTime changed
|
||
|
signed_result = node.signrawtransaction(ToHex(tx))
|
||
|
new_tx = CTransaction()
|
||
|
new_tx.deserialize(BytesIO(hex_str_to_bytes(signed_result['hex'])))
|
||
|
|
||
|
new_tx.vin[0].scriptSig = CScript([CScriptNum(height), OP_CHECKLOCKTIMEVERIFY, OP_DROP] +
|
||
|
list(CScript(new_tx.vin[0].scriptSig)))
|
||
|
return new_tx
|
||
|
|
||
|
def create_transaction(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()
|
||
|
tx.deserialize(BytesIO(hex_str_to_bytes(signresult['hex'])))
|
||
|
return tx
|
||
|
|
||
|
class BIP65Test(BitcoinTestFramework):
|
||
8 years ago
|
def set_test_params(self):
|
||
10 years ago
|
self.num_nodes = 1
|
||
8 years ago
|
self.extra_args = [['-promiscuousmempoolflags=1', '-whitelist=127.0.0.1']]
|
||
|
self.setup_clean_chain = True
|
||
10 years ago
|
|
||
|
def run_test(self):
|
||
7 years ago
|
self.nodes[0].add_p2p_connection(P2PInterface())
|
||
8 years ago
|
|
||
7 years ago
|
network_thread_start()
|
||
8 years ago
|
|
||
|
# wait_for_verack ensures that the P2P connection is fully up.
|
||
7 years ago
|
self.nodes[0].p2p.wait_for_verack()
|
||
8 years ago
|
|
||
|
self.log.info("Mining %d blocks", CLTV_HEIGHT - 2)
|
||
|
self.coinbase_blocks = self.nodes[0].generate(CLTV_HEIGHT - 2)
|
||
10 years ago
|
self.nodeaddress = self.nodes[0].getnewaddress()
|
||
8 years ago
|
|
||
|
self.log.info("Test that an invalid-according-to-CLTV transaction can still appear in a block")
|
||
|
|
||
|
spendtx = create_transaction(self.nodes[0], self.coinbase_blocks[0],
|
||
|
self.nodeaddress, 1.0)
|
||
10 years ago
|
cltv_invalidate(spendtx)
|
||
|
spendtx.rehash()
|
||
|
|
||
8 years ago
|
tip = self.nodes[0].getbestblockhash()
|
||
|
block_time = self.nodes[0].getblockheader(tip)['mediantime'] + 1
|
||
|
block = create_block(int(tip, 16), create_coinbase(CLTV_HEIGHT - 1), block_time)
|
||
|
block.nVersion = 3
|
||
10 years ago
|
block.vtx.append(spendtx)
|
||
|
block.hashMerkleRoot = block.calc_merkle_root()
|
||
|
block.solve()
|
||
|
|
||
7 years ago
|
self.nodes[0].p2p.send_and_ping(msg_block(block))
|
||
8 years ago
|
assert_equal(self.nodes[0].getbestblockhash(), block.hash)
|
||
|
|
||
|
self.log.info("Test that blocks must now be at least version 4")
|
||
|
tip = block.sha256
|
||
|
block_time += 1
|
||
|
block = create_block(tip, create_coinbase(CLTV_HEIGHT), block_time)
|
||
10 years ago
|
block.nVersion = 3
|
||
|
block.solve()
|
||
7 years ago
|
self.nodes[0].p2p.send_and_ping(msg_block(block))
|
||
8 years ago
|
assert_equal(int(self.nodes[0].getbestblockhash(), 16), tip)
|
||
10 years ago
|
|
||
7 years ago
|
wait_until(lambda: "reject" in self.nodes[0].p2p.last_message.keys(), lock=mininode_lock)
|
||
8 years ago
|
with mininode_lock:
|
||
7 years ago
|
assert_equal(self.nodes[0].p2p.last_message["reject"].code, REJECT_OBSOLETE)
|
||
|
assert_equal(self.nodes[0].p2p.last_message["reject"].reason, b'bad-version(0x00000003)')
|
||
|
assert_equal(self.nodes[0].p2p.last_message["reject"].data, block.sha256)
|
||
|
del self.nodes[0].p2p.last_message["reject"]
|
||
8 years ago
|
|
||
|
self.log.info("Test that invalid-according-to-cltv transactions cannot appear in a block")
|
||
10 years ago
|
block.nVersion = 4
|
||
8 years ago
|
|
||
|
spendtx = create_transaction(self.nodes[0], self.coinbase_blocks[1],
|
||
|
self.nodeaddress, 1.0)
|
||
8 years ago
|
cltv_invalidate(spendtx)
|
||
|
spendtx.rehash()
|
||
|
|
||
8 years ago
|
# First we show that this tx is valid except for CLTV by getting it
|
||
|
# accepted to the mempool (which we can achieve with
|
||
|
# -promiscuousmempoolflags).
|
||
7 years ago
|
self.nodes[0].p2p.send_and_ping(msg_tx(spendtx))
|
||
8 years ago
|
assert spendtx.hash in self.nodes[0].getrawmempool()
|
||
|
|
||
|
# Now we verify that a block with this transaction is invalid.
|
||
8 years ago
|
block.vtx.append(spendtx)
|
||
|
block.hashMerkleRoot = block.calc_merkle_root()
|
||
|
block.solve()
|
||
|
|
||
7 years ago
|
self.nodes[0].p2p.send_and_ping(msg_block(block))
|
||
8 years ago
|
assert_equal(int(self.nodes[0].getbestblockhash(), 16), tip)
|
||
|
|
||
7 years ago
|
wait_until(lambda: "reject" in self.nodes[0].p2p.last_message.keys(), lock=mininode_lock)
|
||
8 years ago
|
with mininode_lock:
|
||
7 years ago
|
assert self.nodes[0].p2p.last_message["reject"].code in [REJECT_INVALID, REJECT_NONSTANDARD]
|
||
|
assert_equal(self.nodes[0].p2p.last_message["reject"].data, block.sha256)
|
||
|
if self.nodes[0].p2p.last_message["reject"].code == REJECT_INVALID:
|
||
8 years ago
|
# Generic rejection when a block is invalid
|
||
7 years ago
|
assert_equal(self.nodes[0].p2p.last_message["reject"].reason, b'block-validation-failed')
|
||
8 years ago
|
else:
|
||
7 years ago
|
assert b'Negative locktime' in self.nodes[0].p2p.last_message["reject"].reason
|
||
8 years ago
|
|
||
|
self.log.info("Test that a version 4 block with a valid-according-to-CLTV transaction is accepted")
|
||
|
spendtx = cltv_validate(self.nodes[0], spendtx, CLTV_HEIGHT - 1)
|
||
|
spendtx.rehash()
|
||
|
|
||
|
block.vtx.pop(1)
|
||
|
block.vtx.append(spendtx)
|
||
|
block.hashMerkleRoot = block.calc_merkle_root()
|
||
10 years ago
|
block.solve()
|
||
8 years ago
|
|
||
7 years ago
|
self.nodes[0].p2p.send_and_ping(msg_block(block))
|
||
8 years ago
|
assert_equal(int(self.nodes[0].getbestblockhash(), 16), block.sha256)
|
||
|
|
||
10 years ago
|
|
||
|
if __name__ == '__main__':
|
||
|
BIP65Test().main()
|