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.
This commit is contained in:
parent
937eba91e1
commit
0ba08020c9
|
@ -2266,7 +2266,9 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
|
||||||
{
|
{
|
||||||
pfrom->AddInventoryKnown(inv);
|
pfrom->AddInventoryKnown(inv);
|
||||||
if (fBlocksOnly) {
|
if (fBlocksOnly) {
|
||||||
LogPrint(BCLog::NET, "transaction (%s) inv sent in violation of protocol peer=%d\n", inv.hash.ToString(), pfrom->GetId());
|
LogPrint(BCLog::NET, "transaction (%s) inv sent in violation of protocol, disconnecting peer=%d\n", inv.hash.ToString(), pfrom->GetId());
|
||||||
|
pfrom->fDisconnect = true;
|
||||||
|
return true;
|
||||||
} else if (!fAlreadyHave && !fImporting && !fReindex && !::ChainstateActive().IsInitialBlockDownload()) {
|
} else if (!fAlreadyHave && !fImporting && !fReindex && !::ChainstateActive().IsInitialBlockDownload()) {
|
||||||
RequestTx(State(pfrom->GetId()), inv.hash, current_time);
|
RequestTx(State(pfrom->GetId()), inv.hash, current_time);
|
||||||
}
|
}
|
||||||
|
@ -2483,9 +2485,11 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
|
||||||
if (strCommand == NetMsgType::TX) {
|
if (strCommand == NetMsgType::TX) {
|
||||||
// Stop processing the transaction early if
|
// Stop processing the transaction early if
|
||||||
// We are in blocks only mode and peer is either not whitelisted or whitelistrelay is off
|
// We are in blocks only mode and peer is either not whitelisted or whitelistrelay is off
|
||||||
if (!g_relay_txes && !pfrom->HasPermission(PF_RELAY))
|
// or if this peer is supposed to be a block-relay-only peer
|
||||||
|
if ((!g_relay_txes && !pfrom->HasPermission(PF_RELAY)) || (pfrom->m_tx_relay == nullptr))
|
||||||
{
|
{
|
||||||
LogPrint(BCLog::NET, "transaction sent in violation of protocol peer=%d\n", pfrom->GetId());
|
LogPrint(BCLog::NET, "transaction sent in violation of protocol peer=%d\n", pfrom->GetId());
|
||||||
|
pfrom->fDisconnect = true;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -19,7 +19,7 @@ class P2PBlocksOnly(BitcoinTestFramework):
|
||||||
def run_test(self):
|
def run_test(self):
|
||||||
self.nodes[0].add_p2p_connection(P2PInterface())
|
self.nodes[0].add_p2p_connection(P2PInterface())
|
||||||
|
|
||||||
self.log.info('Check that txs from p2p are rejected')
|
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]
|
prevtx = self.nodes[0].getblock(self.nodes[0].getblockhash(1), 2)['tx'][0]
|
||||||
rawtx = self.nodes[0].createrawtransaction(
|
rawtx = self.nodes[0].createrawtransaction(
|
||||||
inputs=[{
|
inputs=[{
|
||||||
|
@ -42,13 +42,17 @@ class P2PBlocksOnly(BitcoinTestFramework):
|
||||||
assert_equal(self.nodes[0].getnetworkinfo()['localrelay'], False)
|
assert_equal(self.nodes[0].getnetworkinfo()['localrelay'], False)
|
||||||
with self.nodes[0].assert_debug_log(['transaction sent in violation of protocol peer=0']):
|
with self.nodes[0].assert_debug_log(['transaction sent in violation of protocol peer=0']):
|
||||||
self.nodes[0].p2p.send_message(msg_tx(FromHex(CTransaction(), sigtx)))
|
self.nodes[0].p2p.send_message(msg_tx(FromHex(CTransaction(), sigtx)))
|
||||||
self.nodes[0].p2p.sync_with_ping()
|
self.nodes[0].p2p.wait_for_disconnect()
|
||||||
assert_equal(self.nodes[0].getmempoolinfo()['size'], 0)
|
assert_equal(self.nodes[0].getmempoolinfo()['size'], 0)
|
||||||
|
|
||||||
|
# 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')
|
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)
|
assert_equal(self.nodes[0].getpeerinfo()[0]['relaytxes'], True)
|
||||||
txid = self.nodes[0].testmempoolaccept([sigtx])[0]['txid']
|
txid = self.nodes[0].testmempoolaccept([sigtx])[0]['txid']
|
||||||
with self.nodes[0].assert_debug_log(['received getdata for: tx {} peer=0'.format(txid)]):
|
with self.nodes[0].assert_debug_log(['received getdata for: tx {} peer=1'.format(txid)]):
|
||||||
self.nodes[0].sendrawtransaction(sigtx)
|
self.nodes[0].sendrawtransaction(sigtx)
|
||||||
self.nodes[0].p2p.wait_for_tx(txid)
|
self.nodes[0].p2p.wait_for_tx(txid)
|
||||||
assert_equal(self.nodes[0].getmempoolinfo()['size'], 1)
|
assert_equal(self.nodes[0].getmempoolinfo()['size'], 1)
|
||||||
|
|
Loading…
Reference in a new issue