message: [test] P2PBlocksOnly - Test block-relay-only connections.
  Ensure we will disconnect if the peer sends us a transaction & we don't
  announce transactions to the peer.
diff: # Copyright (c) 2019-2020 The Bitcoin Core developers
  # Distributed under the MIT software license, see the accompanying
  # file COPYING or http://www.opensource.org/licenses/mit-license.php.
  -"""Test p2p blocksonly"""
  +"""Test p2p blocksonly mode & block-relay-only connections."""
  +
  +import time
  from test_framework.blocktools import create_transaction
  from test_framework.messages import msg_tx
  -from test_framework.p2p import P2PInterface
  +from test_framework.p2p import P2PInterface, P2PTxInvStore
  from test_framework.test_framework import BitcoinTestFramework
  from test_framework.util import assert_equal
  @@ -22,6 +24,7 @@ class P2PBlocksOnly(BitcoinTestFramework):
  def run_test(self):
  self.blocksonly_mode_tests()
  + self.blocks_relay_conn_tests()
  def blocksonly_mode_tests(self):
  self.log.info("Tests with node running in -blocksonly mode")
  @@ -66,10 +69,37 @@ class P2PBlocksOnly(BitcoinTestFramework):
  assert_equal(self.nodes[0].getmempoolinfo()['size'], 1)
  self.log.info("Relay-permission peer's transaction is accepted and relayed")
  - def check_p2p_tx_violation(self):
  - self.log.info('Check that txs from P2P are rejected and result in disconnect')
  + self.nodes[0].disconnect_p2ps()
  + self.nodes[0].generate(1)
  +
  + def blocks_relay_conn_tests(self):
  + self.log.info('Tests with node in normal mode with block-relay-only connections')
  + self.restart_node(0, ["-noblocksonly"]) # disables blocks only mode
  + assert_equal(self.nodes[0].getnetworkinfo()['localrelay'], True)
  +
  + # Ensure we disconnect if a block-relay-only connection sends us a transaction
  + self.nodes[0].add_outbound_p2p_connection(P2PInterface(), p2p_idx=0, connection_type="block-relay-only")
  + assert_equal(self.nodes[0].getpeerinfo()[0]['relaytxes'], False)
  + _, txid, tx_hex = self.check_p2p_tx_violation(index=2)
  +
  + self.log.info("Check that txs from RPC are not sent to blockrelay connection")
  + conn = self.nodes[0].add_outbound_p2p_connection(P2PTxInvStore(), p2p_idx=1, connection_type="block-relay-only")
  - input_txid = self.nodes[0].getblock(self.nodes[0].getblockhash(1), 2)['tx'][0]['txid']
  + self.nodes[0].sendrawtransaction(tx_hex)
  +
  + # Bump time forward to ensure nNextInvSend timer pops
  + self.nodes[0].setmocktime(int(time.time()) + 60)
  +
  + # Calling sync_with_ping twice requires that the node calls
  + # `ProcessMessage` twice, and thus ensures `SendMessages` must have
  + # been called at least once
  + conn.sync_with_ping()
  + conn.sync_with_ping()
  + assert(int(txid, 16) not in conn.get_invs())
  +
  + def check_p2p_tx_violation(self, index=1):
  + self.log.info('Check that txs from P2P are rejected and result in disconnect')
  + input_txid = self.nodes[0].getblock(self.nodes[0].getblockhash(index), 2)['tx'][0]['txid']
  tx = create_transaction(self.nodes[0], input_txid, self.nodes[0].getnewaddress(), amount=(50 - 0.001))
  txid = tx.rehash()
  tx_hex = tx.serialize().hex()
  