Changeset View
Changeset View
Standalone View
Standalone View
test/functional/bip65-cltv-p2p.py
Show First 20 Lines • Show All 47 Lines • ▼ Show 20 Lines | def cltv_lock_to_height(node, tx, height=-1): | ||||
new_tx = FromHex(CTransaction(), signed_result['hex']) | new_tx = FromHex(CTransaction(), signed_result['hex']) | ||||
pad_tx(new_tx) | pad_tx(new_tx) | ||||
new_tx.rehash() | new_tx.rehash() | ||||
return new_tx | return new_tx | ||||
def create_transaction(node, coinbase, to_address, amount): | def spend_from_coinbase(node, coinbase, to_address, amount): | ||||
from_txid = node.getblock(coinbase)['tx'][0] | from_txid = node.getblock(coinbase)['tx'][0] | ||||
inputs = [{"txid": from_txid, "vout": 0}] | inputs = [{"txid": from_txid, "vout": 0}] | ||||
outputs = {to_address: amount} | outputs = {to_address: amount} | ||||
rawtx = node.createrawtransaction(inputs, outputs) | rawtx = node.createrawtransaction(inputs, outputs) | ||||
signresult = node.signrawtransaction(rawtx) | signresult = node.signrawtransaction(rawtx) | ||||
tx = FromHex(CTransaction(), signresult['hex']) | tx = FromHex(CTransaction(), signresult['hex']) | ||||
return tx | return tx | ||||
Show All 20 Lines | def run_test(self): | ||||
self.log.info("Mining %d blocks", CLTV_HEIGHT - 2) | self.log.info("Mining %d blocks", CLTV_HEIGHT - 2) | ||||
self.coinbase_blocks = self.nodes[0].generate(CLTV_HEIGHT - 2) | self.coinbase_blocks = 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") | ||||
spendtx = create_transaction(self.nodes[0], self.coinbase_blocks[0], | spendtx = spend_from_coinbase(self.nodes[0], self.coinbase_blocks[0], | ||||
self.nodeaddress, 50.0) | self.nodeaddress, 50.0) | ||||
spendtx = cltv_lock_to_height(self.nodes[0], spendtx) | spendtx = cltv_lock_to_height(self.nodes[0], spendtx) | ||||
# Make sure the tx is valid | # Make sure the tx is valid | ||||
self.nodes[0].sendrawtransaction(ToHex(spendtx)) | self.nodes[0].sendrawtransaction(ToHex(spendtx)) | ||||
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( | ||||
Show All 23 Lines | def run_test(self): | ||||
node0.last_message["reject"].reason, b'bad-version(0x00000003)') | node0.last_message["reject"].reason, b'bad-version(0x00000003)') | ||||
assert_equal(node0.last_message["reject"].data, block.sha256) | assert_equal(node0.last_message["reject"].data, block.sha256) | ||||
del node0.last_message["reject"] | del node0.last_message["reject"] | ||||
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 | ||||
spendtx = create_transaction(self.nodes[0], self.coinbase_blocks[1], | spendtx = spend_from_coinbase(self.nodes[0], self.coinbase_blocks[1], | ||||
self.nodeaddress, 49.99) | self.nodeaddress, 49.99) | ||||
spendtx = cltv_lock_to_height(self.nodes[0], spendtx) | spendtx = cltv_lock_to_height(self.nodes[0], spendtx) | ||||
# First we show that this tx is valid except for CLTV by getting it | # First we show that this tx is valid except for CLTV by getting it | ||||
# accepted to the mempool (which we can achieve with | # accepted to the mempool (which we can achieve with | ||||
# -promiscuousmempoolflags). | # -promiscuousmempoolflags). | ||||
node0.send_and_ping(msg_tx(spendtx)) | node0.send_and_ping(msg_tx(spendtx)) | ||||
assert spendtx.hash in self.nodes[0].getrawmempool() | assert spendtx.hash in self.nodes[0].getrawmempool() | ||||
▲ Show 20 Lines • Show All 47 Lines • ▼ Show 20 Lines | def run_test(self): | ||||
# Generic rejection when a block is invalid | # Generic rejection when a block is invalid | ||||
assert_equal( | assert_equal( | ||||
node0.last_message["reject"].reason, b'blk-bad-inputs') | node0.last_message["reject"].reason, b'blk-bad-inputs') | ||||
else: | else: | ||||
assert b'Negative locktime' in node0.last_message["reject"].reason | assert b'Negative locktime' in node0.last_message["reject"].reason | ||||
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") | ||||
spendtx = create_transaction(self.nodes[0], self.coinbase_blocks[2], | spendtx = spend_from_coinbase(self.nodes[0], self.coinbase_blocks[2], | ||||
self.nodeaddress, 49.99) | self.nodeaddress, 49.99) | ||||
spendtx = cltv_lock_to_height(self.nodes[0], spendtx, CLTV_HEIGHT - 1) | spendtx = cltv_lock_to_height(self.nodes[0], spendtx, CLTV_HEIGHT - 1) | ||||
# Modify the transaction in the block to be valid against CLTV | # Modify the transaction in the block to be valid against CLTV | ||||
block.vtx.pop(1) | block.vtx.pop(1) | ||||
block.vtx.append(spendtx) | block.vtx.append(spendtx) | ||||
block.hashMerkleRoot = block.calc_merkle_root() | block.hashMerkleRoot = block.calc_merkle_root() | ||||
block.solve() | block.solve() | ||||
Show All 40 Lines |