Changeset View
Changeset View
Standalone View
Standalone View
test/functional/abc-segwit-recovery.py
#!/usr/bin/env python3 | #!/usr/bin/env python3 | ||||
# Copyright (c) 2015-2016 The Bitcoin Core developers | # Copyright (c) 2019 The Bitcoin developers | ||||
# Copyright (c) 2017-2019 The Bitcoin 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. | ||||
""" | """ | ||||
This test checks that blocks containing segwit recovery transactions will be accepted, | This test checks that blocks containing segwit recovery transactions will be accepted, | ||||
that segwit recovery transactions are rejected from mempool acceptance (even with | that segwit recovery transactions are rejected from mempool acceptance (even with | ||||
-acceptnonstdtxn=1), and that segwit recovery transactions don't result in bans. | -acceptnonstdtxn=1), and that segwit recovery transactions don't result in bans. | ||||
""" | """ | ||||
import time | import time | ||||
from test_framework.blocktools import ( | from test_framework.blocktools import ( | ||||
create_block, | create_block, | ||||
create_coinbase, | create_coinbase, | ||||
make_conform_to_ctor, | make_conform_to_ctor, | ||||
) | ) | ||||
from test_framework.comptool import TestInstance, TestManager | |||||
from test_framework.messages import ( | from test_framework.messages import ( | ||||
COIN, | COIN, | ||||
COutPoint, | COutPoint, | ||||
CTransaction, | CTransaction, | ||||
CTxIn, | CTxIn, | ||||
CTxOut, | CTxOut, | ||||
msg_tx, | |||||
ToHex, | ToHex, | ||||
) | ) | ||||
from test_framework.mininode import ( | from test_framework.mininode import ( | ||||
mininode_lock, | network_thread_join, | ||||
network_thread_start, | network_thread_start, | ||||
P2PInterface, | P2PDataStore, | ||||
) | ) | ||||
from test_framework.script import ( | from test_framework.script import ( | ||||
CScript, | CScript, | ||||
hash160, | hash160, | ||||
OP_EQUAL, | OP_EQUAL, | ||||
OP_HASH160, | OP_HASH160, | ||||
OP_TRUE, | OP_TRUE, | ||||
) | ) | ||||
▲ Show 20 Lines • Show All 41 Lines • ▼ Show 20 Lines | def set_test_params(self): | ||||
# transactions that node_nonstd has accepted. Since we are checking | # transactions that node_nonstd has accepted. Since we are checking | ||||
# that segwit spending txn are not resulting in bans, node_nonstd | # that segwit spending txn are not resulting in bans, node_nonstd | ||||
# doesn't get banned when forwarding this kind of transactions to | # doesn't get banned when forwarding this kind of transactions to | ||||
# node_std. | # node_std. | ||||
self.extra_args = [['-whitelist=127.0.0.1', | self.extra_args = [['-whitelist=127.0.0.1', | ||||
"-acceptnonstdtxn"], | "-acceptnonstdtxn"], | ||||
["-acceptnonstdtxn=0"]] | ["-acceptnonstdtxn=0"]] | ||||
def run_test(self): | |||||
# Move the mocktime up to activation | |||||
for node in self.nodes: | |||||
node.setmocktime(TEST_TIME) | |||||
test = TestManager(self, self.options.tmpdir) | |||||
# TestManager only connects to node_nonstd (nodes[0]) | |||||
test.add_all_connections([self.nodes[0]]) | |||||
# We connect directly to node_std (nodes[1]) | |||||
self.nodes[1].add_p2p_connection(P2PInterface()) | |||||
network_thread_start() | |||||
test.run() | |||||
def next_block(self, number): | def next_block(self, number): | ||||
if self.tip == None: | if self.tip == None: | ||||
base_block_hash = self.genesis_hash | base_block_hash = self.genesis_hash | ||||
block_time = TEST_TIME | block_time = TEST_TIME | ||||
else: | else: | ||||
base_block_hash = self.tip.sha256 | base_block_hash = self.tip.sha256 | ||||
block_time = self.tip.nTime + 1 | block_time = self.tip.nTime + 1 | ||||
# First create the coinbase | # First create the coinbase | ||||
height = self.block_heights[base_block_hash] + 1 | height = self.block_heights[base_block_hash] + 1 | ||||
coinbase = create_coinbase(height) | coinbase = create_coinbase(height) | ||||
coinbase.rehash() | coinbase.rehash() | ||||
block = create_block(base_block_hash, coinbase, block_time) | block = create_block(base_block_hash, coinbase, block_time) | ||||
# Do PoW, which is cheap on regnet | # Do PoW, which is cheap on regnet | ||||
block.solve() | block.solve() | ||||
self.tip = block | self.tip = block | ||||
self.block_heights[block.sha256] = height | self.block_heights[block.sha256] = height | ||||
assert number not in self.blocks | assert number not in self.blocks | ||||
self.blocks[number] = block | self.blocks[number] = block | ||||
return block | return block | ||||
def get_tests(self): | def bootstrap_p2p(self, *, num_connections=1): | ||||
"""Add a P2P connection to the node. | |||||
Helper to connect and wait for version handshake.""" | |||||
for node in self.nodes: | |||||
for _ in range(num_connections): | |||||
node.add_p2p_connection(P2PDataStore()) | |||||
network_thread_start() | |||||
for node in self.nodes: | |||||
node.p2p.wait_for_verack() | |||||
def reconnect_p2p(self, **kwargs): | |||||
"""Tear down and bootstrap the P2P connection to the node. | |||||
The node gets disconnected several times in this test. This helper | |||||
method reconnects the p2p and restarts the network thread.""" | |||||
for node in self.nodes: | |||||
node.disconnect_p2ps() | |||||
network_thread_join() | |||||
self.bootstrap_p2p(**kwargs) | |||||
def run_test(self): | |||||
self.bootstrap_p2p() | |||||
self.genesis_hash = int(self.nodes[0].getbestblockhash(), 16) | self.genesis_hash = int(self.nodes[0].getbestblockhash(), 16) | ||||
self.block_heights[self.genesis_hash] = 0 | self.block_heights[self.genesis_hash] = 0 | ||||
spendable_outputs = [] | spendable_outputs = [] | ||||
# shorthand | # shorthand | ||||
block = self.next_block | block = self.next_block | ||||
node_nonstd = self.nodes[0] | node_nonstd = self.nodes[0] | ||||
node_std = self.nodes[1] | node_std = self.nodes[1] | ||||
# save the current tip so it can be spent by a later block | # save the current tip so it can be spent by a later block | ||||
def save_spendable_output(): | def save_spendable_output(): | ||||
spendable_outputs.append(self.tip) | spendable_outputs.append(self.tip) | ||||
# get an output that we previously marked as spendable | # get an output that we previously marked as spendable | ||||
def get_spendable_output(): | def get_spendable_output(): | ||||
return PreviousSpendableOutput(spendable_outputs.pop(0).vtx[0], 0) | return PreviousSpendableOutput(spendable_outputs.pop(0).vtx[0], 0) | ||||
# returns a test case that asserts that the current tip was accepted | # submit current tip and check it was accepted | ||||
def accepted(): | def accepted(node): | ||||
return TestInstance([[self.tip, True]]) | node.p2p.send_blocks_and_test([self.tip], node) | ||||
# returns a test case that asserts that the current tip was rejected | # submit current tip and check it was rejected (and we are banned) | ||||
def rejected(reject=None): | def rejected(node, reject_code, reject_reason): | ||||
if reject is None: | node.p2p.send_blocks_and_test( | ||||
return TestInstance([[self.tip, False]]) | [self.tip], node, success=False, reject_code=reject_code, reject_reason=reject_reason) | ||||
else: | node.p2p.wait_for_disconnect() | ||||
return TestInstance([[self.tip, reject]]) | self.reconnect_p2p() | ||||
# move the tip back to a previous block | # move the tip back to a previous block | ||||
def tip(number): | def tip(number): | ||||
self.tip = self.blocks[number] | self.tip = self.blocks[number] | ||||
# adds transactions to the block and updates state | # adds transactions to the block and updates state | ||||
def update_block(block_number, new_transactions): | def update_block(block_number, new_transactions): | ||||
block = self.blocks[block_number] | block = self.blocks[block_number] | ||||
▲ Show 20 Lines • Show All 53 Lines • ▼ Show 20 Lines | def run_test(self): | ||||
txspend.vin.append( | txspend.vin.append( | ||||
CTxIn(COutPoint(txfund.sha256, i), CScript([redeem_scripts[i]]))) | CTxIn(COutPoint(txfund.sha256, i), CScript([redeem_scripts[i]]))) | ||||
txspend.vout = [CTxOut(50 * COIN - 2000, | txspend.vout = [CTxOut(50 * COIN - 2000, | ||||
CScript([OP_HASH160, hash160(CScript([OP_TRUE])), OP_EQUAL]))] | CScript([OP_HASH160, hash160(CScript([OP_TRUE])), OP_EQUAL]))] | ||||
txspend.rehash() | txspend.rehash() | ||||
return txfund, txspend | return txfund, txspend | ||||
# Check we are not banned when sending a txn that node_nonstd rejects. | # Check we are not banned when sending a txn that is rejected. | ||||
def check_for_no_ban_on_rejected_tx(tx, reject_code, reject_reason): | def check_for_no_ban_on_rejected_tx(node, tx, reject_code, reject_reason): | ||||
# Check that our connection to node_std is open | node.p2p.send_txs_and_test( | ||||
assert(node_std.p2p.state == 'connected') | [tx], self.nodes[0], success=False, reject_code=reject_code, reject_reason=reject_reason) | ||||
# The P2PConnection stores a public counter for each message type | |||||
# and the last receive message of each type. We use this counter to | |||||
# identify that we received a new reject message. | |||||
with mininode_lock: | |||||
rejects_count = node_std.p2p.message_count['reject'] | |||||
# Send the transaction directly. We use a ping for synchronization: | |||||
# if we have been banned, the pong message won't be received, a | |||||
# timeout occurs and the test fails. | |||||
node_std.p2p.send_message(msg_tx(tx)) | |||||
node_std.p2p.sync_with_ping() | |||||
# Check we haven't been disconnected | |||||
assert(node_std.p2p.state == 'connected') | |||||
# Check the reject message matches what we expected | |||||
with mininode_lock: | |||||
assert(node_std.p2p.message_count['reject'] == | |||||
rejects_count + 1) | |||||
reject_msg = node_std.p2p.last_message['reject'] | |||||
assert(reject_msg.code == reject_code and | |||||
reject_msg.reason == reject_reason and | |||||
reject_msg.data == tx.sha256) | |||||
# Create a new block | # Create a new block | ||||
block(0) | block(0) | ||||
save_spendable_output() | save_spendable_output() | ||||
yield accepted() | accepted(node_nonstd) | ||||
# Now we need that block to mature so we can spend the coinbase. | # Now we need that block to mature so we can spend the coinbase. | ||||
test = TestInstance(sync_every_block=False) | matureblocks = [] | ||||
for i in range(100): | for i in range(199): | ||||
block(5000 + i) | block(5000 + i) | ||||
test.blocks_and_transactions.append([self.tip, True]) | matureblocks.append(self.tip) | ||||
save_spendable_output() | save_spendable_output() | ||||
yield test | node_nonstd.p2p.send_blocks_and_test(matureblocks, node_nonstd) | ||||
# collect spendable outputs now to avoid cluttering the code later on | # collect spendable outputs now to avoid cluttering the code later on | ||||
out = [] | out = [] | ||||
for i in range(100): | for i in range(100): | ||||
out.append(get_spendable_output()) | out.append(get_spendable_output()) | ||||
# Create segwit funding and spending transactions | # Create segwit funding and spending transactions | ||||
txfund, txspend = create_segwit_fund_and_spend_tx(out[0]) | txfund, txspend = create_segwit_fund_and_spend_tx(out[0]) | ||||
txfund_case0, txspend_case0 = create_segwit_fund_and_spend_tx( | txfund_case0, txspend_case0 = create_segwit_fund_and_spend_tx( | ||||
out[1], True) | out[1], True) | ||||
# Mine txfund, as it can't go into node_std mempool because it's | # Mine txfund, as it can't go into node_std mempool because it's | ||||
# nonstandard. | # nonstandard. | ||||
b = block(5555) | b = block(5555) | ||||
update_block(5555, [txfund, txfund_case0]) | update_block(5555, [txfund, txfund_case0]) | ||||
yield accepted() | accepted(node_nonstd) | ||||
# Since the TestManager is not connected to node_std, we must check | # Check both nodes are synchronized before continuing. | ||||
# both nodes are synchronized before continuing. | |||||
sync_blocks(self.nodes) | sync_blocks(self.nodes) | ||||
# Check that upgraded nodes checking for standardness are not banning | # Check that upgraded nodes checking for standardness are not banning | ||||
# nodes sending segwit spending txns. | # nodes sending segwit spending txns. | ||||
check_for_no_ban_on_rejected_tx(txspend, 64, CLEANSTACK_ERROR) | check_for_no_ban_on_rejected_tx( | ||||
check_for_no_ban_on_rejected_tx(txspend_case0, 64, EVAL_FALSE_ERROR) | node_nonstd, txspend, 64, CLEANSTACK_ERROR) | ||||
check_for_no_ban_on_rejected_tx( | |||||
node_nonstd, txspend_case0, 64, EVAL_FALSE_ERROR) | |||||
check_for_no_ban_on_rejected_tx( | |||||
node_std, txspend, 64, CLEANSTACK_ERROR) | |||||
check_for_no_ban_on_rejected_tx( | |||||
node_std, txspend_case0, 64, EVAL_FALSE_ERROR) | |||||
# Segwit recovery txns are never accepted into the mempool, | # Segwit recovery txns are never accepted into the mempool, | ||||
# as they are included in standard flags. | # as they are included in standard flags. | ||||
assert_raises_rpc_error(-26, RPC_CLEANSTACK_ERROR, | assert_raises_rpc_error(-26, RPC_CLEANSTACK_ERROR, | ||||
node_nonstd.sendrawtransaction, ToHex(txspend)) | node_nonstd.sendrawtransaction, ToHex(txspend)) | ||||
assert_raises_rpc_error(-26, RPC_EVAL_FALSE_ERROR, | assert_raises_rpc_error(-26, RPC_EVAL_FALSE_ERROR, | ||||
node_nonstd.sendrawtransaction, ToHex(txspend_case0)) | node_nonstd.sendrawtransaction, ToHex(txspend_case0)) | ||||
assert_raises_rpc_error(-26, RPC_CLEANSTACK_ERROR, | assert_raises_rpc_error(-26, RPC_CLEANSTACK_ERROR, | ||||
node_std.sendrawtransaction, ToHex(txspend)) | node_std.sendrawtransaction, ToHex(txspend)) | ||||
assert_raises_rpc_error(-26, RPC_EVAL_FALSE_ERROR, | assert_raises_rpc_error(-26, RPC_EVAL_FALSE_ERROR, | ||||
node_std.sendrawtransaction, ToHex(txspend_case0)) | node_std.sendrawtransaction, ToHex(txspend_case0)) | ||||
# Blocks containing segwit spending txns are accepted in both nodes. | # Blocks containing segwit spending txns are accepted in both nodes. | ||||
block(5) | block(5) | ||||
postforkblock = update_block(5, [txspend, txspend_case0]) | postforkblock = update_block(5, [txspend, txspend_case0]) | ||||
yield accepted() | accepted(node_nonstd) | ||||
sync_blocks(self.nodes) | sync_blocks(self.nodes) | ||||
if __name__ == '__main__': | if __name__ == '__main__': | ||||
SegwitRecoveryTest().main() | SegwitRecoveryTest().main() |