dash/test/functional/p2p_blocksonly.py

87 lines
4.2 KiB
Python
Raw Normal View History

#!/usr/bin/env python3
# Copyright (c) 2019 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"""
from test_framework.messages import msg_tx, CTransaction, FromHex
Merge #19760: test: Remove confusing mininode terminology d5800da5199527a366024bc80cad7fcca17d5c4a [test] Remove final references to mininode (John Newbery) 5e8df3312e47a73e747ee892face55ed9ababeea test: resort imports (John Newbery) 85165d4332b0f72d30e0c584b476249b542338e6 scripted-diff: Rename mininode to p2p (John Newbery) 9e2897d020b114a10c860f90c5405be029afddba scripted-diff: Rename mininode_lock to p2p_lock (John Newbery) Pull request description: New contributors are often confused by the terminology in the test framework, and what the difference between a _node_ and a _peer_ is. To summarize: - a 'node' is a bitcoind instance. This is the thing whose behavior is being tested. Each bitcoind node is managed by a python `TestNode` object which is used to start/stop the node, manage the node's data directory, read state about the node (eg process status, log file), and interact with the node over different interfaces. - one of the interfaces that we can use to interact with the node is the p2p interface. Each connection to a node using this interface is managed by a python `P2PInterface` or derived object (which is owned by the `TestNode` object). We can open zero, one or many p2p connections to each bitcoind node. The node sees these connections as 'peers'. For historic reasons, the word 'mininode' has been used to refer to those p2p interface objects that we use to connect to the bitcoind node (the code was originally taken from the 'mini-node' branch of https://github.com/jgarzik/pynode/tree/mini-node). However that name has proved to be confusing for new contributors, so rename the remaining references. ACKs for top commit: amitiuttarwar: ACK d5800da519 MarcoFalke: ACK d5800da5199527a366024bc80cad7fcca17d5c4a 🚞 Tree-SHA512: 2c46c2ac3c4278b6e3c647cfd8108428a41e80788fc4f0e386e5b0c47675bc687d94779496c09a3e5ea1319617295be10c422adeeff2d2bd68378e00e0eeb5de
2024-01-15 20:35:29 +01:00
from test_framework.p2p import P2PInterface
from test_framework.test_framework import BitcoinTestFramework
from test_framework.util import assert_equal
class P2PBlocksOnly(BitcoinTestFramework):
def set_test_params(self):
self.num_nodes = 1
self.extra_args = [["-blocksonly"]]
def run_test(self):
self.nodes[0].add_p2p_connection(P2PInterface())
Merge bitcoin#15759: p2p: Add 2 outbound block-relay-only connections (#4862) * Remove unused variable * [refactor] Move tx relay state to separate structure * [refactor] Change tx_relay structure to be unique_ptr * Check that tx_relay is initialized before access * Add comment explaining intended use of m_tx_relay * Add 2 outbound block-relay-only connections Transaction relay is primarily optimized for balancing redundancy/robustness with bandwidth minimization -- as a result transaction relay leaks information that adversaries can use to infer the network topology. Network topology is better kept private for (at least) two reasons: (a) Knowledge of the network graph can make it easier to find the source IP of a given transaction. (b) Knowledge of the network graph could be used to split a target node or nodes from the honest network (eg by knowing which peers to attack in order to achieve a network split). We can eliminate the risks of (b) by separating block relay from transaction relay; inferring network connectivity from the relay of blocks/block headers is much more expensive for an adversary. After this commit, bitcoind will make 2 additional outbound connections that are only used for block relay. (In the future, we might consider rotating our transaction-relay peers to help limit the effects of (a).) * Don't relay addr messages to block-relay-only peers We don't want relay of addr messages to leak information about these network links. * doc: improve comments relating to block-relay-only peers * Disconnect peers violating blocks-only mode If we set fRelay=false in our VERSION message, and a peer sends an INV or TX message anyway, disconnect. Since we use fRelay=false to minimize bandwidth, we should not tolerate remaining connected to a peer violating the protocol. * net_processing. Removed comment + fixed formatting * Refactoring net_processing, removed duplicated code * Refactor some bool in a many-arguments function to enum It's made to avoid possible typos with arguments, because some of them have default values and it's very high probability to make a mistake here. * Added UI debug option for Outbound * Fixed data race related to `setInventoryTxToSend`, introduced in `[refactor] Move tx relay state to separate structure` Co-authored-by: Suhas Daftuar <sdaftuar@gmail.com>
2022-06-19 08:02:28 +02:00
self.log.info('Check that txs from p2p are rejected and result in disconnect')
prevtx = self.nodes[0].getblock(self.nodes[0].getblockhash(1), 2)['tx'][0]
rawtx = self.nodes[0].createrawtransaction(
inputs=[{
'txid': prevtx['txid'],
'vout': 0
}],
outputs=[{
self.nodes[0].get_deterministic_priv_key()[0]: 500 - 0.00125
}],
)
sigtx = self.nodes[0].signrawtransactionwithkey(
hexstring=rawtx,
privkeys=[self.nodes[0].get_deterministic_priv_key()[1]],
prevtxs=[{
'txid': prevtx['txid'],
'vout': 0,
'scriptPubKey': prevtx['vout'][0]['scriptPubKey']['hex'],
}],
)['hex']
assert_equal(self.nodes[0].getnetworkinfo()['localrelay'], False)
with self.nodes[0].assert_debug_log(['tx sent in violation of protocol peer=0']):
self.nodes[0].p2p.send_message(msg_tx(FromHex(CTransaction(), sigtx)))
Merge bitcoin#15759: p2p: Add 2 outbound block-relay-only connections (#4862) * Remove unused variable * [refactor] Move tx relay state to separate structure * [refactor] Change tx_relay structure to be unique_ptr * Check that tx_relay is initialized before access * Add comment explaining intended use of m_tx_relay * Add 2 outbound block-relay-only connections Transaction relay is primarily optimized for balancing redundancy/robustness with bandwidth minimization -- as a result transaction relay leaks information that adversaries can use to infer the network topology. Network topology is better kept private for (at least) two reasons: (a) Knowledge of the network graph can make it easier to find the source IP of a given transaction. (b) Knowledge of the network graph could be used to split a target node or nodes from the honest network (eg by knowing which peers to attack in order to achieve a network split). We can eliminate the risks of (b) by separating block relay from transaction relay; inferring network connectivity from the relay of blocks/block headers is much more expensive for an adversary. After this commit, bitcoind will make 2 additional outbound connections that are only used for block relay. (In the future, we might consider rotating our transaction-relay peers to help limit the effects of (a).) * Don't relay addr messages to block-relay-only peers We don't want relay of addr messages to leak information about these network links. * doc: improve comments relating to block-relay-only peers * Disconnect peers violating blocks-only mode If we set fRelay=false in our VERSION message, and a peer sends an INV or TX message anyway, disconnect. Since we use fRelay=false to minimize bandwidth, we should not tolerate remaining connected to a peer violating the protocol. * net_processing. Removed comment + fixed formatting * Refactoring net_processing, removed duplicated code * Refactor some bool in a many-arguments function to enum It's made to avoid possible typos with arguments, because some of them have default values and it's very high probability to make a mistake here. * Added UI debug option for Outbound * Fixed data race related to `setInventoryTxToSend`, introduced in `[refactor] Move tx relay state to separate structure` Co-authored-by: Suhas Daftuar <sdaftuar@gmail.com>
2022-06-19 08:02:28 +02:00
self.nodes[0].p2p.wait_for_disconnect()
assert_equal(self.nodes[0].getmempoolinfo()['size'], 0)
Merge bitcoin#15759: p2p: Add 2 outbound block-relay-only connections (#4862) * Remove unused variable * [refactor] Move tx relay state to separate structure * [refactor] Change tx_relay structure to be unique_ptr * Check that tx_relay is initialized before access * Add comment explaining intended use of m_tx_relay * Add 2 outbound block-relay-only connections Transaction relay is primarily optimized for balancing redundancy/robustness with bandwidth minimization -- as a result transaction relay leaks information that adversaries can use to infer the network topology. Network topology is better kept private for (at least) two reasons: (a) Knowledge of the network graph can make it easier to find the source IP of a given transaction. (b) Knowledge of the network graph could be used to split a target node or nodes from the honest network (eg by knowing which peers to attack in order to achieve a network split). We can eliminate the risks of (b) by separating block relay from transaction relay; inferring network connectivity from the relay of blocks/block headers is much more expensive for an adversary. After this commit, bitcoind will make 2 additional outbound connections that are only used for block relay. (In the future, we might consider rotating our transaction-relay peers to help limit the effects of (a).) * Don't relay addr messages to block-relay-only peers We don't want relay of addr messages to leak information about these network links. * doc: improve comments relating to block-relay-only peers * Disconnect peers violating blocks-only mode If we set fRelay=false in our VERSION message, and a peer sends an INV or TX message anyway, disconnect. Since we use fRelay=false to minimize bandwidth, we should not tolerate remaining connected to a peer violating the protocol. * net_processing. Removed comment + fixed formatting * Refactoring net_processing, removed duplicated code * Refactor some bool in a many-arguments function to enum It's made to avoid possible typos with arguments, because some of them have default values and it's very high probability to make a mistake here. * Added UI debug option for Outbound * Fixed data race related to `setInventoryTxToSend`, introduced in `[refactor] Move tx relay state to separate structure` Co-authored-by: Suhas Daftuar <sdaftuar@gmail.com>
2022-06-19 08:02:28 +02:00
# Remove the disconnected peer and add a new one.
del self.nodes[0].p2ps[0]
self.nodes[0].add_p2p_connection(P2PInterface())
self.log.info('Check that txs from rpc are not rejected and relayed to other peers')
assert_equal(self.nodes[0].getpeerinfo()[0]['relaytxes'], True)
txid = self.nodes[0].testmempoolaccept([sigtx])[0]['txid']
Merge bitcoin#15759: p2p: Add 2 outbound block-relay-only connections (#4862) * Remove unused variable * [refactor] Move tx relay state to separate structure * [refactor] Change tx_relay structure to be unique_ptr * Check that tx_relay is initialized before access * Add comment explaining intended use of m_tx_relay * Add 2 outbound block-relay-only connections Transaction relay is primarily optimized for balancing redundancy/robustness with bandwidth minimization -- as a result transaction relay leaks information that adversaries can use to infer the network topology. Network topology is better kept private for (at least) two reasons: (a) Knowledge of the network graph can make it easier to find the source IP of a given transaction. (b) Knowledge of the network graph could be used to split a target node or nodes from the honest network (eg by knowing which peers to attack in order to achieve a network split). We can eliminate the risks of (b) by separating block relay from transaction relay; inferring network connectivity from the relay of blocks/block headers is much more expensive for an adversary. After this commit, bitcoind will make 2 additional outbound connections that are only used for block relay. (In the future, we might consider rotating our transaction-relay peers to help limit the effects of (a).) * Don't relay addr messages to block-relay-only peers We don't want relay of addr messages to leak information about these network links. * doc: improve comments relating to block-relay-only peers * Disconnect peers violating blocks-only mode If we set fRelay=false in our VERSION message, and a peer sends an INV or TX message anyway, disconnect. Since we use fRelay=false to minimize bandwidth, we should not tolerate remaining connected to a peer violating the protocol. * net_processing. Removed comment + fixed formatting * Refactoring net_processing, removed duplicated code * Refactor some bool in a many-arguments function to enum It's made to avoid possible typos with arguments, because some of them have default values and it's very high probability to make a mistake here. * Added UI debug option for Outbound * Fixed data race related to `setInventoryTxToSend`, introduced in `[refactor] Move tx relay state to separate structure` Co-authored-by: Suhas Daftuar <sdaftuar@gmail.com>
2022-06-19 08:02:28 +02:00
with self.nodes[0].assert_debug_log(['received getdata for: tx {} peer=1'.format(txid)]):
self.nodes[0].sendrawtransaction(sigtx)
self.bump_mocktime(60)
self.nodes[0].p2p.wait_for_tx(txid)
assert_equal(self.nodes[0].getmempoolinfo()['size'], 1)
self.log.info('Check that txs from forcerelay peers are not rejected and relayed to others')
self.log.info("Restarting node 0 with forcerelay permission and blocksonly")
self.restart_node(0, ["-persistmempool=0", "-whitelist=127.0.0.1", "-whitelistforcerelay", "-blocksonly"])
assert_equal(self.nodes[0].getrawmempool(), [])
first_peer = self.nodes[0].add_p2p_connection(P2PInterface())
second_peer = self.nodes[0].add_p2p_connection(P2PInterface())
peer_1_info = self.nodes[0].getpeerinfo()[0]
assert_equal(peer_1_info['whitelisted'], True)
assert_equal(peer_1_info['permissions'], ['noban', 'forcerelay', 'relay', 'mempool', 'download'])
peer_2_info = self.nodes[0].getpeerinfo()[1]
assert_equal(peer_2_info['whitelisted'], True)
assert_equal(peer_2_info['permissions'], ['noban', 'forcerelay', 'relay', 'mempool', 'download'])
assert_equal(self.nodes[0].testmempoolaccept([sigtx])[0]['allowed'], True)
txid = self.nodes[0].testmempoolaccept([sigtx])[0]['txid']
self.log.info('Check that the tx from forcerelay first_peer is relayed to others (ie.second_peer)')
with self.nodes[0].assert_debug_log(["received getdata"]):
first_peer.send_message(msg_tx(FromHex(CTransaction(), sigtx)))
self.log.info('Check that the forcerelay peer is still connected after sending the transaction')
assert_equal(first_peer.is_connected, True)
second_peer.wait_for_tx(txid)
assert_equal(self.nodes[0].getmempoolinfo()['size'], 1)
self.log.info("Forcerelay peer's transaction is accepted and relayed")
if __name__ == '__main__':
P2PBlocksOnly().main()