Skip to content

Commit 89144eb

Browse files
committed
Merge bitcoin/bitcoin#33448: net/rpc: Report inv information for debugging
2738b63 test: validate behaviour of getpeerinfo last_inv_sequence and inv_to_send (Anthony Towns) 77b2ebb rpc/net: report per-peer last_inv_sequence (Anthony Towns) adefb51 rpc/net: add per-peer inv_to_send sizes (Anthony Towns) Pull request description: Adds per-peer entries to `getpeerinfo` for the size of the inv_to_send queue and the mempool sequence number as at the last INV. Can be helpful for debugging tx relay performance and privacy/fingerprinting issues. ACKs for top commit: sipa: utACK 2738b63 instagibbs: ACK 2738b63 Tree-SHA512: e3c9c52e8e38b099d405a177ffba6783c5821cc5ce1432b98218843e00906986ce2141dcd5b04a67006c328211a672e519fa3390e012688499bfc9ac99767599
2 parents eaa1a3c + 2738b63 commit 89144eb

File tree

5 files changed

+33
-4
lines changed

5 files changed

+33
-4
lines changed

src/net_processing.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ struct Peer {
312312
std::chrono::microseconds m_next_inv_send_time GUARDED_BY(m_tx_inventory_mutex){0};
313313
/** The mempool sequence num at which we sent the last `inv` message to this peer.
314314
* Can relay txs with lower sequence numbers than this (see CTxMempool::info_for_relay). */
315-
uint64_t m_last_inv_sequence GUARDED_BY(NetEventsInterface::g_msgproc_mutex){1};
315+
uint64_t m_last_inv_sequence GUARDED_BY(m_tx_inventory_mutex){1};
316316

317317
/** Minimum fee rate with which to filter transaction announcements to this node. See BIP133. */
318318
std::atomic<CAmount> m_fee_filter_received{0};
@@ -942,7 +942,7 @@ class PeerManagerImpl final : public PeerManager
942942

943943
/** Determine whether or not a peer can request a transaction, and return it (or nullptr if not found or not allowed). */
944944
CTransactionRef FindTxForGetData(const Peer::TxRelay& tx_relay, const GenTxid& gtxid)
945-
EXCLUSIVE_LOCKS_REQUIRED(!m_most_recent_block_mutex, NetEventsInterface::g_msgproc_mutex);
945+
EXCLUSIVE_LOCKS_REQUIRED(!m_most_recent_block_mutex, !tx_relay.m_tx_inventory_mutex);
946946

947947
void ProcessGetData(CNode& pfrom, Peer& peer, const std::atomic<bool>& interruptMsgProc)
948948
EXCLUSIVE_LOCKS_REQUIRED(!m_most_recent_block_mutex, peer.m_getdata_requests_mutex, NetEventsInterface::g_msgproc_mutex)
@@ -1728,9 +1728,13 @@ bool PeerManagerImpl::GetNodeStateStats(NodeId nodeid, CNodeStateStats& stats) c
17281728
if (auto tx_relay = peer->GetTxRelay(); tx_relay != nullptr) {
17291729
stats.m_relay_txs = WITH_LOCK(tx_relay->m_bloom_filter_mutex, return tx_relay->m_relay_txs);
17301730
stats.m_fee_filter_received = tx_relay->m_fee_filter_received.load();
1731+
LOCK(tx_relay->m_tx_inventory_mutex);
1732+
stats.m_last_inv_seq = tx_relay->m_last_inv_sequence;
1733+
stats.m_inv_to_send = tx_relay->m_tx_inventory_to_send.size();
17311734
} else {
17321735
stats.m_relay_txs = false;
17331736
stats.m_fee_filter_received = 0;
1737+
stats.m_inv_to_send = 0;
17341738
}
17351739

17361740
stats.m_ping_wait = ping_wait;
@@ -2362,8 +2366,8 @@ CTransactionRef PeerManagerImpl::FindTxForGetData(const Peer::TxRelay& tx_relay,
23622366
{
23632367
// If a tx was in the mempool prior to the last INV for this peer, permit the request.
23642368
auto txinfo{std::visit(
2365-
[&](const auto& id) EXCLUSIVE_LOCKS_REQUIRED(NetEventsInterface::g_msgproc_mutex) {
2366-
return m_mempool.info_for_relay(id, tx_relay.m_last_inv_sequence);
2369+
[&](const auto& id) {
2370+
return m_mempool.info_for_relay(id, WITH_LOCK(tx_relay.m_tx_inventory_mutex, return tx_relay.m_last_inv_sequence));
23672371
},
23682372
gtxid)};
23692373
if (txinfo.tx) {

src/net_processing.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ struct CNodeStateStats {
5454
std::chrono::microseconds m_ping_wait;
5555
std::vector<int> vHeightInFlight;
5656
bool m_relay_txs;
57+
int m_inv_to_send = 0;
58+
uint64_t m_last_inv_seq{0};
5759
CAmount m_fee_filter_received;
5860
uint64_t m_addr_processed = 0;
5961
uint64_t m_addr_rate_limited = 0;

src/rpc/net.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,8 @@ static RPCHelpMan getpeerinfo()
142142
{RPCResult::Type::STR, "SERVICE_NAME", "the service name if it is recognised"}
143143
}},
144144
{RPCResult::Type::BOOL, "relaytxes", "Whether we relay transactions to this peer"},
145+
{RPCResult::Type::NUM, "last_inv_sequence", "Mempool sequence number of this peer's last INV"},
146+
{RPCResult::Type::NUM, "inv_to_send", "How many txs we have queued to announce to this peer"},
145147
{RPCResult::Type::NUM_TIME, "lastsend", "The " + UNIX_EPOCH_TIME + " of the last send"},
146148
{RPCResult::Type::NUM_TIME, "lastrecv", "The " + UNIX_EPOCH_TIME + " of the last receive"},
147149
{RPCResult::Type::NUM_TIME, "last_transaction", "The " + UNIX_EPOCH_TIME + " of the last valid transaction received from this peer"},
@@ -238,6 +240,8 @@ static RPCHelpMan getpeerinfo()
238240
obj.pushKV("services", strprintf("%016x", services));
239241
obj.pushKV("servicesnames", GetServicesNames(services));
240242
obj.pushKV("relaytxes", statestats.m_relay_txs);
243+
obj.pushKV("last_inv_sequence", statestats.m_last_inv_seq);
244+
obj.pushKV("inv_to_send", statestats.m_inv_to_send);
241245
obj.pushKV("lastsend", count_seconds(stats.m_last_send));
242246
obj.pushKV("lastrecv", count_seconds(stats.m_last_recv));
243247
obj.pushKV("last_transaction", count_seconds(stats.m_last_tx_time));

test/functional/p2p_leak_tx.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
)
1313
from test_framework.wallet import MiniWallet
1414

15+
import time
1516

1617
class P2PNode(P2PDataStore):
1718
def on_inv(self, msg):
@@ -36,8 +37,24 @@ def test_tx_in_block(self):
3637

3738
self.log.debug("Generate transaction and block")
3839
inbound_peer.last_message.pop("inv", None)
40+
41+
self.gen_node.setmocktime(int(time.time())) # pause time based activities
3942
wtxid = self.miniwallet.send_self_transfer(from_node=self.gen_node)["wtxid"]
43+
rawmp = self.gen_node.getrawmempool(False, True)
44+
pi = self.gen_node.getpeerinfo()[0]
45+
assert_equal(rawmp["mempool_sequence"], 2) # our tx cause mempool activity
46+
assert_equal(pi["last_inv_sequence"], 1) # that is after the last inv
47+
assert_equal(pi["inv_to_send"], 1) # and our tx has been queued
48+
self.gen_node.setmocktime(0)
49+
4050
inbound_peer.wait_until(lambda: "inv" in inbound_peer.last_message and inbound_peer.last_message.get("inv").inv[0].hash == int(wtxid, 16))
51+
52+
rawmp = self.gen_node.getrawmempool(False, True)
53+
pi = self.gen_node.getpeerinfo()[0]
54+
assert_equal(rawmp["mempool_sequence"], 2) # no mempool update
55+
assert_equal(pi["last_inv_sequence"], 2) # announced the current mempool
56+
assert_equal(pi["inv_to_send"], 0) # nothing left in the queue
57+
4158
want_tx = msg_getdata(inv=inbound_peer.last_message.get("inv").inv)
4259
self.generate(self.gen_node, 1)
4360

test/functional/rpc_net.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,8 @@ def test_getpeerinfo(self):
166166
"permissions": [],
167167
"presynced_headers": -1,
168168
"relaytxes": False,
169+
"inv_to_send": 0,
170+
"last_inv_sequence": 0,
169171
"services": "0000000000000000",
170172
"servicesnames": [],
171173
"session_id": "" if not self.options.v2transport else no_version_peer.v2_state.peer['session_id'].hex(),

0 commit comments

Comments
 (0)