Changeset View
Changeset View
Standalone View
Standalone View
test/functional/feature_cltv.py
#!/usr/bin/env python3 | #!/usr/bin/env python3 | ||||
# Copyright (c) 2015-2019 The Bitcoin Core developers | # Copyright (c) 2015-2019 The Bitcoin Core developers | ||||
# Distributed under the MIT software license, see the accompanying | # Distributed under the MIT software license, see the accompanying | ||||
# file COPYING or http://www.opensource.org/licenses/mit-license.php. | # file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||||
"""Test BIP65 (CHECKLOCKTIMEVERIFY). | """Test BIP65 (CHECKLOCKTIMEVERIFY). | ||||
Test that the CHECKLOCKTIMEVERIFY soft-fork activates at (regtest) block height | Test that the CHECKLOCKTIMEVERIFY soft-fork activates at (regtest) block height | ||||
1351. | 1351. | ||||
""" | """ | ||||
from test_framework.blocktools import create_block, create_coinbase, make_conform_to_ctor | from test_framework.blocktools import create_block, create_coinbase, create_transaction, make_conform_to_ctor | ||||
from test_framework.messages import ( | from test_framework.messages import ( | ||||
CTransaction, | CTransaction, | ||||
FromHex, | FromHex, | ||||
msg_block, | msg_block, | ||||
msg_tx, | msg_tx, | ||||
ToHex, | ToHex, | ||||
) | ) | ||||
from test_framework.mininode import ( | from test_framework.mininode import ( | ||||
▲ Show 20 Lines • Show All 56 Lines • ▼ Show 20 Lines | def cltv_lock_to_height(node, tx, to_address, amount, height=-1): | ||||
spendtx_raw = node.createrawtransaction(inputs, output) | spendtx_raw = node.createrawtransaction(inputs, output) | ||||
spendtx = FromHex(CTransaction(), spendtx_raw) | spendtx = FromHex(CTransaction(), spendtx_raw) | ||||
pad_tx(spendtx) | pad_tx(spendtx) | ||||
return fundtx, spendtx | return fundtx, spendtx | ||||
def spend_from_coinbase(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.signrawtransactionwithwallet(rawtx) | |||||
tx = FromHex(CTransaction(), signresult['hex']) | |||||
return tx | |||||
class BIP65Test(BitcoinTestFramework): | class BIP65Test(BitcoinTestFramework): | ||||
def set_test_params(self): | def set_test_params(self): | ||||
self.num_nodes = 1 | self.num_nodes = 1 | ||||
self.extra_args = [['-whitelist=127.0.0.1']] | self.extra_args = [['-whitelist=127.0.0.1']] | ||||
self.setup_clean_chain = True | self.setup_clean_chain = True | ||||
def run_test(self): | def run_test(self): | ||||
self.nodes[0].add_p2p_connection(P2PInterface()) | self.nodes[0].add_p2p_connection(P2PInterface()) | ||||
self.log.info("Mining {} blocks".format(CLTV_HEIGHT - 2)) | self.log.info("Mining {} blocks".format(CLTV_HEIGHT - 2)) | ||||
self.coinbase_blocks = self.nodes[0].generate(CLTV_HEIGHT - 2) | self.coinbase_txids = [self.nodes[0].getblock( | ||||
b)['tx'][0] for b in self.nodes[0].generate(CLTV_HEIGHT - 2)] | |||||
self.nodeaddress = self.nodes[0].getnewaddress() | self.nodeaddress = self.nodes[0].getnewaddress() | ||||
self.log.info( | self.log.info( | ||||
"Test that an invalid-according-to-CLTV transaction can still appear in a block") | "Test that an invalid-according-to-CLTV transaction can still appear in a block") | ||||
fundtx = spend_from_coinbase(self.nodes[0], self.coinbase_blocks[0], | fundtx = create_transaction(self.nodes[0], self.coinbase_txids[0], | ||||
self.nodeaddress, 49.99) | self.nodeaddress, 49.99) | ||||
fundtx, spendtx = cltv_lock_to_height( | fundtx, spendtx = cltv_lock_to_height( | ||||
self.nodes[0], fundtx, self.nodeaddress, 49.98) | self.nodes[0], fundtx, self.nodeaddress, 49.98) | ||||
tip = self.nodes[0].getbestblockhash() | tip = self.nodes[0].getbestblockhash() | ||||
block_time = self.nodes[0].getblockheader(tip)['mediantime'] + 1 | block_time = self.nodes[0].getblockheader(tip)['mediantime'] + 1 | ||||
block = create_block(int(tip, 16), create_coinbase( | block = create_block(int(tip, 16), create_coinbase( | ||||
CLTV_HEIGHT - 1), block_time) | CLTV_HEIGHT - 1), block_time) | ||||
block.nVersion = 3 | block.nVersion = 3 | ||||
Show All 19 Lines | def run_test(self): | ||||
self.nodes[0].p2p.send_and_ping(msg_block(block)) | self.nodes[0].p2p.send_and_ping(msg_block(block)) | ||||
assert_equal(int(self.nodes[0].getbestblockhash(), 16), tip) | assert_equal(int(self.nodes[0].getbestblockhash(), 16), tip) | ||||
self.nodes[0].p2p.sync_with_ping() | self.nodes[0].p2p.sync_with_ping() | ||||
self.log.info( | self.log.info( | ||||
"Test that invalid-according-to-cltv transactions cannot appear in a block") | "Test that invalid-according-to-cltv transactions cannot appear in a block") | ||||
block.nVersion = 4 | block.nVersion = 4 | ||||
fundtx = spend_from_coinbase(self.nodes[0], self.coinbase_blocks[1], | fundtx = create_transaction(self.nodes[0], self.coinbase_txids[1], | ||||
self.nodeaddress, 49.99) | self.nodeaddress, 49.99) | ||||
fundtx, spendtx = cltv_lock_to_height( | fundtx, spendtx = cltv_lock_to_height( | ||||
self.nodes[0], fundtx, self.nodeaddress, 49.98) | self.nodes[0], fundtx, self.nodeaddress, 49.98) | ||||
# The funding tx only has unexecuted bad CLTV, in scriptpubkey; this is valid. | # The funding tx only has unexecuted bad CLTV, in scriptpubkey; this is valid. | ||||
self.nodes[0].p2p.send_and_ping(msg_tx(fundtx)) | self.nodes[0].p2p.send_and_ping(msg_tx(fundtx)) | ||||
assert fundtx.hash in self.nodes[0].getrawmempool() | assert fundtx.hash in self.nodes[0].getrawmempool() | ||||
# Mine a block containing the funding transaction | # Mine a block containing the funding transaction | ||||
Show All 31 Lines | def run_test(self): | ||||
with self.nodes[0].assert_debug_log(expected_msgs=['ConnectBlock {} failed (blk-bad-inputs'.format(block.hash)]): | with self.nodes[0].assert_debug_log(expected_msgs=['ConnectBlock {} failed (blk-bad-inputs'.format(block.hash)]): | ||||
self.nodes[0].p2p.send_and_ping(msg_block(block)) | self.nodes[0].p2p.send_and_ping(msg_block(block)) | ||||
assert_equal(self.nodes[0].getbestblockhash(), tip) | assert_equal(self.nodes[0].getbestblockhash(), tip) | ||||
self.nodes[0].p2p.sync_with_ping() | self.nodes[0].p2p.sync_with_ping() | ||||
self.log.info( | self.log.info( | ||||
"Test that a version 4 block with a valid-according-to-CLTV transaction is accepted") | "Test that a version 4 block with a valid-according-to-CLTV transaction is accepted") | ||||
fundtx = spend_from_coinbase(self.nodes[0], self.coinbase_blocks[2], | fundtx = create_transaction(self.nodes[0], self.coinbase_txids[2], | ||||
self.nodeaddress, 49.99) | self.nodeaddress, 49.99) | ||||
fundtx, spendtx = cltv_lock_to_height( | fundtx, spendtx = cltv_lock_to_height( | ||||
self.nodes[0], fundtx, self.nodeaddress, 49.98, CLTV_HEIGHT) | self.nodes[0], fundtx, self.nodeaddress, 49.98, CLTV_HEIGHT) | ||||
# make sure sequence is nonfinal and locktime is good | # make sure sequence is nonfinal and locktime is good | ||||
spendtx.vin[0].nSequence = 0xfffffffe | spendtx.vin[0].nSequence = 0xfffffffe | ||||
spendtx.nLockTime = CLTV_HEIGHT | spendtx.nLockTime = CLTV_HEIGHT | ||||
# both transactions are fully valid | # both transactions are fully valid | ||||
Show All 18 Lines |