|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# Copyright (c) 2019 The Bitcoin Core developers |
| 3 | +# Distributed under the MIT software license, see the accompanying |
| 4 | +# file COPYING or http://www.opensource.org/licenses/mit-license.php. |
| 5 | +"""Test p2p blocksonly""" |
| 6 | + |
| 7 | +from test_framework.messages import msg_tx, CTransaction, FromHex |
| 8 | +from test_framework.mininode import P2PInterface |
| 9 | +from test_framework.test_framework import BitcoinTestFramework |
| 10 | +from test_framework.util import assert_equal |
| 11 | + |
| 12 | + |
| 13 | +class P2PBlocksOnly(BitcoinTestFramework): |
| 14 | + def set_test_params(self): |
| 15 | + self.setup_clean_chain = False |
| 16 | + self.num_nodes = 1 |
| 17 | + self.extra_args = [["-blocksonly"]] |
| 18 | + |
| 19 | + def run_test(self): |
| 20 | + self.nodes[0].add_p2p_connection(P2PInterface()) |
| 21 | + |
| 22 | + self.log.info('Check that txs from p2p are rejected') |
| 23 | + prevtx = self.nodes[0].getblock(self.nodes[0].getblockhash(1), 2)['tx'][0] |
| 24 | + rawtx = self.nodes[0].createrawtransaction( |
| 25 | + inputs=[{ |
| 26 | + 'txid': prevtx['txid'], |
| 27 | + 'vout': 0 |
| 28 | + }], |
| 29 | + outputs=[{ |
| 30 | + self.nodes[0].get_deterministic_priv_key().address: 50 - 0.00125 |
| 31 | + }], |
| 32 | + ) |
| 33 | + sigtx = self.nodes[0].signrawtransactionwithkey( |
| 34 | + hexstring=rawtx, |
| 35 | + privkeys=[self.nodes[0].get_deterministic_priv_key().key], |
| 36 | + prevtxs=[{ |
| 37 | + 'txid': prevtx['txid'], |
| 38 | + 'vout': 0, |
| 39 | + 'scriptPubKey': prevtx['vout'][0]['scriptPubKey']['hex'], |
| 40 | + }], |
| 41 | + )['hex'] |
| 42 | + assert_equal(self.nodes[0].getnetworkinfo()['localrelay'], False) |
| 43 | + with self.nodes[0].assert_debug_log(['transaction sent in violation of protocol peer=0']): |
| 44 | + self.nodes[0].p2p.send_message(msg_tx(FromHex(CTransaction(), sigtx))) |
| 45 | + self.nodes[0].p2p.sync_with_ping() |
| 46 | + assert_equal(self.nodes[0].getmempoolinfo()['size'], 0) |
| 47 | + |
| 48 | + self.log.info('Check that txs from rpc are not rejected and relayed to other peers') |
| 49 | + assert_equal(self.nodes[0].getpeerinfo()[0]['relaytxes'], True) |
| 50 | + txid = self.nodes[0].testmempoolaccept([sigtx])[0]['txid'] |
| 51 | + with self.nodes[0].assert_debug_log(['received getdata for: tx {} peer=0'.format(txid)]): |
| 52 | + self.nodes[0].sendrawtransaction(sigtx) |
| 53 | + self.nodes[0].p2p.wait_for_tx(txid) |
| 54 | + assert_equal(self.nodes[0].getmempoolinfo()['size'], 1) |
| 55 | + |
| 56 | + |
| 57 | +if __name__ == '__main__': |
| 58 | + P2PBlocksOnly().main() |
0 commit comments