Skip to content

Commit 0ba0802

Browse files
committed
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.
1 parent 937eba9 commit 0ba0802

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed

src/net_processing.cpp

+6-2
Original file line numberDiff line numberDiff line change
@@ -2266,7 +2266,9 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
22662266
{
22672267
pfrom->AddInventoryKnown(inv);
22682268
if (fBlocksOnly) {
2269-
LogPrint(BCLog::NET, "transaction (%s) inv sent in violation of protocol peer=%d\n", inv.hash.ToString(), pfrom->GetId());
2269+
LogPrint(BCLog::NET, "transaction (%s) inv sent in violation of protocol, disconnecting peer=%d\n", inv.hash.ToString(), pfrom->GetId());
2270+
pfrom->fDisconnect = true;
2271+
return true;
22702272
} else if (!fAlreadyHave && !fImporting && !fReindex && !::ChainstateActive().IsInitialBlockDownload()) {
22712273
RequestTx(State(pfrom->GetId()), inv.hash, current_time);
22722274
}
@@ -2483,9 +2485,11 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
24832485
if (strCommand == NetMsgType::TX) {
24842486
// Stop processing the transaction early if
24852487
// We are in blocks only mode and peer is either not whitelisted or whitelistrelay is off
2486-
if (!g_relay_txes && !pfrom->HasPermission(PF_RELAY))
2488+
// or if this peer is supposed to be a block-relay-only peer
2489+
if ((!g_relay_txes && !pfrom->HasPermission(PF_RELAY)) || (pfrom->m_tx_relay == nullptr))
24872490
{
24882491
LogPrint(BCLog::NET, "transaction sent in violation of protocol peer=%d\n", pfrom->GetId());
2492+
pfrom->fDisconnect = true;
24892493
return true;
24902494
}
24912495

test/functional/p2p_blocksonly.py

+7-3
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def set_test_params(self):
1919
def run_test(self):
2020
self.nodes[0].add_p2p_connection(P2PInterface())
2121

22-
self.log.info('Check that txs from p2p are rejected')
22+
self.log.info('Check that txs from p2p are rejected and result in disconnect')
2323
prevtx = self.nodes[0].getblock(self.nodes[0].getblockhash(1), 2)['tx'][0]
2424
rawtx = self.nodes[0].createrawtransaction(
2525
inputs=[{
@@ -42,13 +42,17 @@ def run_test(self):
4242
assert_equal(self.nodes[0].getnetworkinfo()['localrelay'], False)
4343
with self.nodes[0].assert_debug_log(['transaction sent in violation of protocol peer=0']):
4444
self.nodes[0].p2p.send_message(msg_tx(FromHex(CTransaction(), sigtx)))
45-
self.nodes[0].p2p.sync_with_ping()
45+
self.nodes[0].p2p.wait_for_disconnect()
4646
assert_equal(self.nodes[0].getmempoolinfo()['size'], 0)
4747

48+
# Remove the disconnected peer and add a new one.
49+
del self.nodes[0].p2ps[0]
50+
self.nodes[0].add_p2p_connection(P2PInterface())
51+
4852
self.log.info('Check that txs from rpc are not rejected and relayed to other peers')
4953
assert_equal(self.nodes[0].getpeerinfo()[0]['relaytxes'], True)
5054
txid = self.nodes[0].testmempoolaccept([sigtx])[0]['txid']
51-
with self.nodes[0].assert_debug_log(['received getdata for: tx {} peer=0'.format(txid)]):
55+
with self.nodes[0].assert_debug_log(['received getdata for: tx {} peer=1'.format(txid)]):
5256
self.nodes[0].sendrawtransaction(sigtx)
5357
self.nodes[0].p2p.wait_for_tx(txid)
5458
assert_equal(self.nodes[0].getmempoolinfo()['size'], 1)

0 commit comments

Comments
 (0)