Skip to content

Commit 58e0239

Browse files
committed
Merge bitcoin/bitcoin#22955: p2p: Rename fBlocksOnly, Add test
fa66a7d p2p: Rename fBlocksOnly, Add test (MarcoFalke) fac66d0 test: Simplify p2p_blocksonly test with new miniwallet rescan_utxos method (MarcoFalke) Pull request description: `fBlocksOnly` has several issues: * The name is confusing * It is untested Fix both. ACKs for top commit: laanwj: Code review ACK fa66a7d Tree-SHA512: 4218f455eeb37297f74603d7d44895288605844ae828a40dfb7a70215f1a058ac5ad945a22732f5ebcad3ad375d54ba360bea69ea79639a30d4c88b042448f0f
2 parents 6d76b57 + fa66a7d commit 58e0239

File tree

3 files changed

+25
-15
lines changed

3 files changed

+25
-15
lines changed

src/net_processing.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2909,13 +2909,13 @@ void PeerManagerImpl::ProcessMessage(CNode& pfrom, const std::string& msg_type,
29092909
return;
29102910
}
29112911

2912-
// We won't accept tx inv's if we're in blocks-only mode, or this is a
2912+
// Reject tx INVs when the -blocksonly setting is enabled, or this is a
29132913
// block-relay-only peer
2914-
bool fBlocksOnly = m_ignore_incoming_txs || (pfrom.m_tx_relay == nullptr);
2914+
bool reject_tx_invs{m_ignore_incoming_txs || (pfrom.m_tx_relay == nullptr)};
29152915

29162916
// Allow peers with relay permission to send data other than blocks in blocks only mode
29172917
if (pfrom.HasPermission(NetPermissionFlags::Relay)) {
2918-
fBlocksOnly = false;
2918+
reject_tx_invs = false;
29192919
}
29202920

29212921
LOCK(cs_main);
@@ -2954,7 +2954,7 @@ void PeerManagerImpl::ProcessMessage(CNode& pfrom, const std::string& msg_type,
29542954
LogPrint(BCLog::NET, "got inv: %s %s peer=%d\n", inv.ToString(), fAlreadyHave ? "have" : "new", pfrom.GetId());
29552955

29562956
pfrom.AddKnownTx(inv.hash);
2957-
if (fBlocksOnly) {
2957+
if (reject_tx_invs) {
29582958
LogPrint(BCLog::NET, "transaction (%s) inv sent in violation of protocol, disconnecting peer=%d\n", inv.hash.ToString(), pfrom.GetId());
29592959
pfrom.fDisconnect = true;
29602960
return;

test/functional/p2p_blocksonly.py

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@
66

77
import time
88

9-
from test_framework.blocktools import COINBASE_MATURITY
10-
from test_framework.messages import msg_tx
9+
from test_framework.messages import msg_tx, msg_inv, CInv, MSG_WTX
1110
from test_framework.p2p import P2PInterface, P2PTxInvStore
1211
from test_framework.test_framework import BitcoinTestFramework
1312
from test_framework.util import assert_equal
@@ -16,15 +15,13 @@
1615

1716
class P2PBlocksOnly(BitcoinTestFramework):
1817
def set_test_params(self):
19-
self.setup_clean_chain = True
2018
self.num_nodes = 1
2119
self.extra_args = [["-blocksonly"]]
2220

2321
def run_test(self):
2422
self.miniwallet = MiniWallet(self.nodes[0])
2523
# Add enough mature utxos to the wallet, so that all txs spend confirmed coins
26-
self.generate(self.miniwallet, 2)
27-
self.generate(self.nodes[0], COINBASE_MATURITY)
24+
self.miniwallet.rescan_utxos()
2825

2926
self.blocksonly_mode_tests()
3027
self.blocks_relay_conn_tests()
@@ -36,12 +33,19 @@ def blocksonly_mode_tests(self):
3633
self.nodes[0].add_p2p_connection(P2PInterface())
3734
tx, txid, wtxid, tx_hex = self.check_p2p_tx_violation()
3835

36+
self.log.info('Check that tx invs also violate the protocol')
37+
self.nodes[0].add_p2p_connection(P2PInterface())
38+
with self.nodes[0].assert_debug_log(['transaction (0000000000000000000000000000000000000000000000000000000000001234) inv sent in violation of protocol, disconnecting peer']):
39+
self.nodes[0].p2ps[0].send_message(msg_inv([CInv(t=MSG_WTX, h=0x1234)]))
40+
self.nodes[0].p2ps[0].wait_for_disconnect()
41+
del self.nodes[0].p2ps[0]
42+
3943
self.log.info('Check that txs from rpc are not rejected and relayed to other peers')
4044
tx_relay_peer = self.nodes[0].add_p2p_connection(P2PInterface())
4145
assert_equal(self.nodes[0].getpeerinfo()[0]['relaytxes'], True)
4246

4347
assert_equal(self.nodes[0].testmempoolaccept([tx_hex])[0]['allowed'], True)
44-
with self.nodes[0].assert_debug_log(['received getdata for: wtx {} peer=1'.format(wtxid)]):
48+
with self.nodes[0].assert_debug_log(['received getdata for: wtx {} peer'.format(wtxid)]):
4549
self.nodes[0].sendrawtransaction(tx_hex)
4650
tx_relay_peer.wait_for_tx(txid)
4751
assert_equal(self.nodes[0].getmempoolinfo()['size'], 1)
@@ -83,7 +87,7 @@ def blocks_relay_conn_tests(self):
8387
# Ensure we disconnect if a block-relay-only connection sends us a transaction
8488
self.nodes[0].add_outbound_p2p_connection(P2PInterface(), p2p_idx=0, connection_type="block-relay-only")
8589
assert_equal(self.nodes[0].getpeerinfo()[0]['relaytxes'], False)
86-
_, txid, _, tx_hex = self.check_p2p_tx_violation(index=2)
90+
_, txid, _, tx_hex = self.check_p2p_tx_violation()
8791

8892
self.log.info("Check that txs from RPC are not sent to blockrelay connection")
8993
conn = self.nodes[0].add_outbound_p2p_connection(P2PTxInvStore(), p2p_idx=1, connection_type="block-relay-only")
@@ -96,11 +100,9 @@ def blocks_relay_conn_tests(self):
96100
conn.sync_send_with_ping()
97101
assert(int(txid, 16) not in conn.get_invs())
98102

99-
def check_p2p_tx_violation(self, index=1):
103+
def check_p2p_tx_violation(self):
100104
self.log.info('Check that txs from P2P are rejected and result in disconnect')
101-
input_txid = self.nodes[0].getblock(self.nodes[0].getblockhash(index), 2)['tx'][0]['txid']
102-
utxo_to_spend = self.miniwallet.get_utxo(txid=input_txid)
103-
spendtx = self.miniwallet.create_self_transfer(from_node=self.nodes[0], utxo_to_spend=utxo_to_spend)
105+
spendtx = self.miniwallet.create_self_transfer(from_node=self.nodes[0])
104106

105107
with self.nodes[0].assert_debug_log(['transaction sent in violation of protocol peer=0']):
106108
self.nodes[0].p2ps[0].send_message(msg_tx(spendtx['tx']))

test/functional/test_framework/wallet.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,14 @@ def __init__(self, test_node, *, mode=MiniWalletMode.ADDRESS_OP_TRUE):
7979
self._address = ADDRESS_BCRT1_P2WSH_OP_TRUE
8080
self._scriptPubKey = bytes.fromhex(self._test_node.validateaddress(self._address)['scriptPubKey'])
8181

82+
def rescan_utxos(self):
83+
"""Drop all utxos and rescan the utxo set"""
84+
self._utxos = []
85+
res = self._test_node.scantxoutset(action="start", scanobjects=[f'raw({self._scriptPubKey.hex()})'])
86+
assert_equal(True, res['success'])
87+
for utxo in res['unspents']:
88+
self._utxos.append({'txid': utxo['txid'], 'vout': utxo['vout'], 'value': utxo['amount']})
89+
8290
def scan_blocks(self, *, start=1, num):
8391
"""Scan the blocks for self._address outputs and add them to self._utxos"""
8492
for i in range(start, start + num):

0 commit comments

Comments
 (0)