Changeset View
Changeset View
Standalone View
Standalone View
test/functional/feature_cltv.py
Show First 20 Lines • Show All 88 Lines • ▼ Show 20 Lines | def set_test_params(self): | ||||
]] | ]] | ||||
self.setup_clean_chain = True | self.setup_clean_chain = True | ||||
self.rpc_timeout = 120 | self.rpc_timeout = 120 | ||||
def skip_test_if_missing_module(self): | def skip_test_if_missing_module(self): | ||||
self.skip_if_no_wallet() | self.skip_if_no_wallet() | ||||
def run_test(self): | def run_test(self): | ||||
self.nodes[0].add_p2p_connection(P2PInterface()) | peer = 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_txids = [self.nodes[0].getblock( | self.coinbase_txids = [self.nodes[0].getblock( | ||||
b)['tx'][0] for b in self.nodes[0].generate(CLTV_HEIGHT - 2)] | 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") | ||||
Show All 10 Lines | def run_test(self): | ||||
block.nVersion = 3 | block.nVersion = 3 | ||||
block.vtx.append(fundtx) | block.vtx.append(fundtx) | ||||
# include the -1 CLTV in block | # include the -1 CLTV in block | ||||
block.vtx.append(spendtx) | block.vtx.append(spendtx) | ||||
make_conform_to_ctor(block) | make_conform_to_ctor(block) | ||||
block.hashMerkleRoot = block.calc_merkle_root() | block.hashMerkleRoot = block.calc_merkle_root() | ||||
block.solve() | block.solve() | ||||
self.nodes[0].p2p.send_and_ping(msg_block(block)) | peer.send_and_ping(msg_block(block)) | ||||
# This block is valid | # This block is valid | ||||
assert_equal(self.nodes[0].getbestblockhash(), block.hash) | assert_equal(self.nodes[0].getbestblockhash(), block.hash) | ||||
self.log.info("Test that blocks must now be at least version 4") | self.log.info("Test that blocks must now be at least version 4") | ||||
tip = block.sha256 | tip = block.sha256 | ||||
block_time += 1 | block_time += 1 | ||||
block = create_block(tip, create_coinbase(CLTV_HEIGHT), block_time) | block = create_block(tip, create_coinbase(CLTV_HEIGHT), block_time) | ||||
block.nVersion = 3 | block.nVersion = 3 | ||||
block.solve() | block.solve() | ||||
with self.nodes[0].assert_debug_log(expected_msgs=['{}, bad-version(0x00000003)'.format(block.hash)]): | with self.nodes[0].assert_debug_log(expected_msgs=['{}, bad-version(0x00000003)'.format(block.hash)]): | ||||
self.nodes[0].p2p.send_and_ping(msg_block(block)) | peer.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() | peer.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 = create_transaction(self.nodes[0], self.coinbase_txids[1], | fundtx = create_transaction(self.nodes[0], self.coinbase_txids[1], | ||||
self.nodeaddress, amount=49990000) | self.nodeaddress, amount=49990000) | ||||
fundtx, spendtx = cltv_lock_to_height( | fundtx, spendtx = cltv_lock_to_height( | ||||
self.nodes[0], fundtx, self.nodeaddress, 49980000) | self.nodes[0], fundtx, self.nodeaddress, 49980000) | ||||
# The funding tx only has unexecuted bad CLTV, in scriptpubkey; this is | # The funding tx only has unexecuted bad CLTV, in scriptpubkey; this is | ||||
# valid. | # valid. | ||||
self.nodes[0].p2p.send_and_ping(msg_tx(fundtx)) | peer.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 | ||||
block.vtx.append(fundtx) | block.vtx.append(fundtx) | ||||
block.hashMerkleRoot = block.calc_merkle_root() | block.hashMerkleRoot = block.calc_merkle_root() | ||||
block.solve() | block.solve() | ||||
self.nodes[0].p2p.send_and_ping(msg_block(block)) | peer.send_and_ping(msg_block(block)) | ||||
# This block is valid | # This block is valid | ||||
assert_equal(self.nodes[0].getbestblockhash(), block.hash) | assert_equal(self.nodes[0].getbestblockhash(), block.hash) | ||||
# We show that this tx is invalid due to CLTV by getting it | # We show that this tx is invalid due to CLTV by getting it | ||||
# rejected from the mempool for exactly that reason. | # rejected from the mempool for exactly that reason. | ||||
assert_equal( | assert_equal( | ||||
[{'txid': spendtx.hash, 'allowed': False, | [{'txid': spendtx.hash, 'allowed': False, | ||||
'reject-reason': 'non-mandatory-script-verify-flag (Negative locktime)'}], | 'reject-reason': 'non-mandatory-script-verify-flag (Negative locktime)'}], | ||||
Show All 12 Lines | def run_test(self): | ||||
block = create_block( | block = create_block( | ||||
block.sha256, create_coinbase(CLTV_HEIGHT + 1), block_time) | block.sha256, create_coinbase(CLTV_HEIGHT + 1), block_time) | ||||
block.nVersion = 4 | block.nVersion = 4 | ||||
block.vtx.append(spendtx) | block.vtx.append(spendtx) | ||||
block.hashMerkleRoot = block.calc_merkle_root() | block.hashMerkleRoot = block.calc_merkle_root() | ||||
block.solve() | block.solve() | ||||
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)) | peer.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() | peer.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 = create_transaction(self.nodes[0], self.coinbase_txids[2], | fundtx = create_transaction(self.nodes[0], self.coinbase_txids[2], | ||||
self.nodeaddress, amount=49990000) | self.nodeaddress, amount=49990000) | ||||
fundtx, spendtx = cltv_lock_to_height( | fundtx, spendtx = cltv_lock_to_height( | ||||
self.nodes[0], fundtx, self.nodeaddress, 49980000, CLTV_HEIGHT) | self.nodes[0], fundtx, self.nodeaddress, 49980000, 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 | ||||
self.nodes[0].sendrawtransaction(ToHex(fundtx)) | self.nodes[0].sendrawtransaction(ToHex(fundtx)) | ||||
self.nodes[0].sendrawtransaction(ToHex(spendtx)) | self.nodes[0].sendrawtransaction(ToHex(spendtx)) | ||||
# Modify the transactions in the block to be valid against CLTV | # Modify the transactions in the block to be valid against CLTV | ||||
block.vtx.pop(1) | block.vtx.pop(1) | ||||
block.vtx.append(fundtx) | block.vtx.append(fundtx) | ||||
block.vtx.append(spendtx) | block.vtx.append(spendtx) | ||||
make_conform_to_ctor(block) | make_conform_to_ctor(block) | ||||
block.hashMerkleRoot = block.calc_merkle_root() | block.hashMerkleRoot = block.calc_merkle_root() | ||||
block.solve() | block.solve() | ||||
self.nodes[0].p2p.send_and_ping(msg_block(block)) | peer.send_and_ping(msg_block(block)) | ||||
# This block is now valid | # This block is now valid | ||||
assert_equal(self.nodes[0].getbestblockhash(), block.hash) | assert_equal(self.nodes[0].getbestblockhash(), block.hash) | ||||
if __name__ == '__main__': | if __name__ == '__main__': | ||||
BIP65Test().main() | BIP65Test().main() |