From 6247799ea158b4f4a06d838591b98a5a986084ad Mon Sep 17 00:00:00 2001 From: Anthony Towns Date: Sun, 7 May 2023 12:34:40 +1000 Subject: [PATCH 1/4] rpc/net: add per-peer inv_to_send sizes --- src/net_processing.cpp | 2 ++ src/net_processing.h | 1 + src/rpc/net.cpp | 2 ++ test/functional/rpc_net.py | 1 + 4 files changed, 6 insertions(+) diff --git a/src/net_processing.cpp b/src/net_processing.cpp index 9287a67a04ea..abf57595c150 100644 --- a/src/net_processing.cpp +++ b/src/net_processing.cpp @@ -1728,9 +1728,11 @@ bool PeerManagerImpl::GetNodeStateStats(NodeId nodeid, CNodeStateStats& stats) c if (auto tx_relay = peer->GetTxRelay(); tx_relay != nullptr) { stats.m_relay_txs = WITH_LOCK(tx_relay->m_bloom_filter_mutex, return tx_relay->m_relay_txs); stats.m_fee_filter_received = tx_relay->m_fee_filter_received.load(); + stats.m_inv_to_send = WITH_LOCK(tx_relay->m_tx_inventory_mutex, return tx_relay->m_tx_inventory_to_send.size()); } else { stats.m_relay_txs = false; stats.m_fee_filter_received = 0; + stats.m_inv_to_send = 0; } stats.m_ping_wait = ping_wait; diff --git a/src/net_processing.h b/src/net_processing.h index 8c140d98ad66..ee12bd08d347 100644 --- a/src/net_processing.h +++ b/src/net_processing.h @@ -54,6 +54,7 @@ struct CNodeStateStats { std::chrono::microseconds m_ping_wait; std::vector vHeightInFlight; bool m_relay_txs; + int m_inv_to_send = 0; CAmount m_fee_filter_received; uint64_t m_addr_processed = 0; uint64_t m_addr_rate_limited = 0; diff --git a/src/rpc/net.cpp b/src/rpc/net.cpp index fbb70d72161d..e5b9880ad965 100644 --- a/src/rpc/net.cpp +++ b/src/rpc/net.cpp @@ -142,6 +142,7 @@ static RPCHelpMan getpeerinfo() {RPCResult::Type::STR, "SERVICE_NAME", "the service name if it is recognised"} }}, {RPCResult::Type::BOOL, "relaytxes", "Whether we relay transactions to this peer"}, + {RPCResult::Type::NUM, "inv_to_send", "How many txs we have queued to announce to this peer"}, {RPCResult::Type::NUM_TIME, "lastsend", "The " + UNIX_EPOCH_TIME + " of the last send"}, {RPCResult::Type::NUM_TIME, "lastrecv", "The " + UNIX_EPOCH_TIME + " of the last receive"}, {RPCResult::Type::NUM_TIME, "last_transaction", "The " + UNIX_EPOCH_TIME + " of the last valid transaction received from this peer"}, @@ -238,6 +239,7 @@ static RPCHelpMan getpeerinfo() obj.pushKV("services", strprintf("%016x", services)); obj.pushKV("servicesnames", GetServicesNames(services)); obj.pushKV("relaytxes", statestats.m_relay_txs); + obj.pushKV("inv_to_send", statestats.m_inv_to_send); obj.pushKV("lastsend", count_seconds(stats.m_last_send)); obj.pushKV("lastrecv", count_seconds(stats.m_last_recv)); obj.pushKV("last_transaction", count_seconds(stats.m_last_tx_time)); diff --git a/test/functional/rpc_net.py b/test/functional/rpc_net.py index 41ecbbed22d9..f52d59d7b3e8 100755 --- a/test/functional/rpc_net.py +++ b/test/functional/rpc_net.py @@ -166,6 +166,7 @@ def test_getpeerinfo(self): "permissions": [], "presynced_headers": -1, "relaytxes": False, + "inv_to_send": 0, "services": "0000000000000000", "servicesnames": [], "session_id": "" if not self.options.v2transport else no_version_peer.v2_state.peer['session_id'].hex(), From 32ac5795e08b9ce126f39101d79a516470325354 Mon Sep 17 00:00:00 2001 From: Anthony Towns Date: Wed, 17 Sep 2025 12:17:41 +1000 Subject: [PATCH 2/4] rpc/net: report per-peer last_inv_sequence --- src/net_processing.cpp | 12 +++++++----- src/net_processing.h | 1 + src/rpc/net.cpp | 2 ++ test/functional/rpc_net.py | 1 + 4 files changed, 11 insertions(+), 5 deletions(-) diff --git a/src/net_processing.cpp b/src/net_processing.cpp index abf57595c150..5a157b2b65f0 100644 --- a/src/net_processing.cpp +++ b/src/net_processing.cpp @@ -312,7 +312,7 @@ struct Peer { std::chrono::microseconds m_next_inv_send_time GUARDED_BY(m_tx_inventory_mutex){0}; /** The mempool sequence num at which we sent the last `inv` message to this peer. * Can relay txs with lower sequence numbers than this (see CTxMempool::info_for_relay). */ - uint64_t m_last_inv_sequence GUARDED_BY(NetEventsInterface::g_msgproc_mutex){1}; + uint64_t m_last_inv_sequence GUARDED_BY(m_tx_inventory_mutex){1}; /** Minimum fee rate with which to filter transaction announcements to this node. See BIP133. */ std::atomic m_fee_filter_received{0}; @@ -942,7 +942,7 @@ class PeerManagerImpl final : public PeerManager /** Determine whether or not a peer can request a transaction, and return it (or nullptr if not found or not allowed). */ CTransactionRef FindTxForGetData(const Peer::TxRelay& tx_relay, const GenTxid& gtxid) - EXCLUSIVE_LOCKS_REQUIRED(!m_most_recent_block_mutex, NetEventsInterface::g_msgproc_mutex); + EXCLUSIVE_LOCKS_REQUIRED(!m_most_recent_block_mutex, !tx_relay.m_tx_inventory_mutex); void ProcessGetData(CNode& pfrom, Peer& peer, const std::atomic& interruptMsgProc) EXCLUSIVE_LOCKS_REQUIRED(!m_most_recent_block_mutex, peer.m_getdata_requests_mutex, NetEventsInterface::g_msgproc_mutex) @@ -1728,7 +1728,9 @@ bool PeerManagerImpl::GetNodeStateStats(NodeId nodeid, CNodeStateStats& stats) c if (auto tx_relay = peer->GetTxRelay(); tx_relay != nullptr) { stats.m_relay_txs = WITH_LOCK(tx_relay->m_bloom_filter_mutex, return tx_relay->m_relay_txs); stats.m_fee_filter_received = tx_relay->m_fee_filter_received.load(); - stats.m_inv_to_send = WITH_LOCK(tx_relay->m_tx_inventory_mutex, return tx_relay->m_tx_inventory_to_send.size()); + LOCK(tx_relay->m_tx_inventory_mutex); + stats.m_last_inv_seq = tx_relay->m_last_inv_sequence; + stats.m_inv_to_send = tx_relay->m_tx_inventory_to_send.size(); } else { stats.m_relay_txs = false; stats.m_fee_filter_received = 0; @@ -2364,8 +2366,8 @@ CTransactionRef PeerManagerImpl::FindTxForGetData(const Peer::TxRelay& tx_relay, { // If a tx was in the mempool prior to the last INV for this peer, permit the request. auto txinfo{std::visit( - [&](const auto& id) EXCLUSIVE_LOCKS_REQUIRED(NetEventsInterface::g_msgproc_mutex) { - return m_mempool.info_for_relay(id, tx_relay.m_last_inv_sequence); + [&](const auto& id) { + return m_mempool.info_for_relay(id, WITH_LOCK(tx_relay.m_tx_inventory_mutex, return tx_relay.m_last_inv_sequence)); }, gtxid)}; if (txinfo.tx) { diff --git a/src/net_processing.h b/src/net_processing.h index ee12bd08d347..6eb4a5e16a2c 100644 --- a/src/net_processing.h +++ b/src/net_processing.h @@ -55,6 +55,7 @@ struct CNodeStateStats { std::vector vHeightInFlight; bool m_relay_txs; int m_inv_to_send = 0; + uint64_t m_last_inv_seq{0}; CAmount m_fee_filter_received; uint64_t m_addr_processed = 0; uint64_t m_addr_rate_limited = 0; diff --git a/src/rpc/net.cpp b/src/rpc/net.cpp index e5b9880ad965..ba74283d237c 100644 --- a/src/rpc/net.cpp +++ b/src/rpc/net.cpp @@ -142,6 +142,7 @@ static RPCHelpMan getpeerinfo() {RPCResult::Type::STR, "SERVICE_NAME", "the service name if it is recognised"} }}, {RPCResult::Type::BOOL, "relaytxes", "Whether we relay transactions to this peer"}, + {RPCResult::Type::NUM, "last_inv_sequence", "Mempool sequence number of this peer's last INV"}, {RPCResult::Type::NUM, "inv_to_send", "How many txs we have queued to announce to this peer"}, {RPCResult::Type::NUM_TIME, "lastsend", "The " + UNIX_EPOCH_TIME + " of the last send"}, {RPCResult::Type::NUM_TIME, "lastrecv", "The " + UNIX_EPOCH_TIME + " of the last receive"}, @@ -239,6 +240,7 @@ static RPCHelpMan getpeerinfo() obj.pushKV("services", strprintf("%016x", services)); obj.pushKV("servicesnames", GetServicesNames(services)); obj.pushKV("relaytxes", statestats.m_relay_txs); + obj.pushKV("last_inv_sequence", statestats.m_last_inv_seq); obj.pushKV("inv_to_send", statestats.m_inv_to_send); obj.pushKV("lastsend", count_seconds(stats.m_last_send)); obj.pushKV("lastrecv", count_seconds(stats.m_last_recv)); diff --git a/test/functional/rpc_net.py b/test/functional/rpc_net.py index f52d59d7b3e8..aeaf20c23d58 100755 --- a/test/functional/rpc_net.py +++ b/test/functional/rpc_net.py @@ -167,6 +167,7 @@ def test_getpeerinfo(self): "presynced_headers": -1, "relaytxes": False, "inv_to_send": 0, + "last_inv_sequence": 0, "services": "0000000000000000", "servicesnames": [], "session_id": "" if not self.options.v2transport else no_version_peer.v2_state.peer['session_id'].hex(), From 71f4796fd21015ecdc2894bc3418c5b01c1b4e1a Mon Sep 17 00:00:00 2001 From: Anthony Towns Date: Tue, 23 Sep 2025 14:52:13 +1000 Subject: [PATCH 3/4] test: validate behaviour of getpeerinfo last_inv_sequence and inv_to_send Co-Authored-By: MarcoFalke <*~=`'#}+{/-|&$^_@721217.xyz> --- test/functional/p2p_leak_tx.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/test/functional/p2p_leak_tx.py b/test/functional/p2p_leak_tx.py index a1a00751d12a..42e586fca3dd 100755 --- a/test/functional/p2p_leak_tx.py +++ b/test/functional/p2p_leak_tx.py @@ -12,6 +12,7 @@ ) from test_framework.wallet import MiniWallet +import time class P2PNode(P2PDataStore): def on_inv(self, msg): @@ -36,8 +37,24 @@ def test_tx_in_block(self): self.log.debug("Generate transaction and block") inbound_peer.last_message.pop("inv", None) + + self.gen_node.setmocktime(int(time.time())) # pause time based activities wtxid = self.miniwallet.send_self_transfer(from_node=self.gen_node)["wtxid"] + rawmp = self.gen_node.getrawmempool(False, True) + pi = self.gen_node.getpeerinfo()[0] + assert_equal(rawmp["mempool_sequence"], 2) # our tx cause mempool activity + assert_equal(pi["last_inv_sequence"], 1) # that is after the last inv + assert_equal(pi["inv_to_send"], 1) # and our tx has been queued + self.gen_node.setmocktime(0) + inbound_peer.wait_until(lambda: "inv" in inbound_peer.last_message and inbound_peer.last_message.get("inv").inv[0].hash == int(wtxid, 16)) + + rawmp = self.gen_node.getrawmempool(False, True) + pi = self.gen_node.getpeerinfo()[0] + assert_equal(rawmp["mempool_sequence"], 2) # no mempool update + assert_equal(pi["last_inv_sequence"], 2) # announced the current mempool + assert_equal(pi["inv_to_send"], 0) # nothing left in the queue + want_tx = msg_getdata(inv=inbound_peer.last_message.get("inv").inv) self.generate(self.gen_node, 1) From df5ea12840e20493dd04fdc2d0d8359a9b1db5be Mon Sep 17 00:00:00 2001 From: fi3 Date: Tue, 23 Sep 2025 20:51:39 +0200 Subject: [PATCH 4/4] Add diagnoseblock --- src/consensus/validation.h | 16 ++ src/rpc/mining.cpp | 78 +++++++++ src/validation.cpp | 336 +++++++++++++++++++++++++++++++++++++ src/validation.h | 7 + 4 files changed, 437 insertions(+) diff --git a/src/consensus/validation.h b/src/consensus/validation.h index 2b2dd4bb6865..8d9f5b4ea805 100644 --- a/src/consensus/validation.h +++ b/src/consensus/validation.h @@ -17,6 +17,22 @@ static constexpr int NO_WITNESS_COMMITMENT{-1}; /** Minimum size of a witness commitment structure. Defined in BIP 141. **/ static constexpr size_t MINIMUM_WITNESS_COMMITMENT{38}; +struct ValidationIssue { + enum class Level { ERROR, WARNING }; + std::string check; // e.g. "pow", "merkle", "witness_commitment", "tx[12].inputs" + std::string message; // human detail + Level level{Level::ERROR}; +}; + +struct BlockDiagnostics { + uint256 block_hash{}; + std::vector issues; + bool ok() const { return issues.empty(); } + void add(const std::string& check, const std::string& msg, ValidationIssue::Level lvl = ValidationIssue::Level::ERROR) { + issues.push_back(ValidationIssue{check, msg, lvl}); + } +}; + /** A "reason" why a transaction was invalid, suitable for determining whether the * provider of the transaction should be banned/ignored/disconnected/etc. */ diff --git a/src/rpc/mining.cpp b/src/rpc/mining.cpp index 850cdb70c4e3..e8bb9e0f4f20 100644 --- a/src/rpc/mining.cpp +++ b/src/rpc/mining.cpp @@ -1094,6 +1094,83 @@ static RPCHelpMan submitblock() }; } +static RPCHelpMan diagnoseblock() +{ + return RPCHelpMan{ + "diagnoseblock", + "Decode a raw block and run comprehensive diagnostics without early exit.\n" + "Returns all detected issues (PoW/merkle/commitment/tx inputs/scripts/context).\n", + { + {"hexdata", RPCArg::Type::STR_HEX, RPCArg::Optional::NO, "Hex-encoded block"}, + }, + RPCResult{ + RPCResult::Type::OBJ, "", "", + { + {RPCResult::Type::BOOL, "ok", "true if no issues found"}, + {RPCResult::Type::STR_HEX, "blockhash", "hash of the provided block"}, + {RPCResult::Type::ARR, "issues", "list of problems found", + { + {RPCResult::Type::OBJ, "", "", + { + {RPCResult::Type::STR, "level", "\"error\" or \"warning\""}, + {RPCResult::Type::STR, "check", "which check failed (e.g. pow, merkle, tx[i].inputs, ... )"}, + {RPCResult::Type::STR, "message", "human-readable detail"}, + } + }, + } + }, + } + }, + RPCExamples{ + HelpExampleCli("diagnoseblock", "\"\"") + + HelpExampleRpc("diagnoseblock", "\"\"") + }, + [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue + { + // Parse block + CBlock block; + if (!DecodeHexBlk(block, request.params[0].get_str())) { + throw JSONRPCError(RPC_DESERIALIZATION_ERROR, "Block decode failed"); + } + + ChainstateManager& chainman = EnsureAnyChainman(request.context); + + // If we know the parent, fill in uncommitted structures so witness commitment checks reflect reality. + { + LOCK(cs_main); + if (const CBlockIndex* pindexPrev = chainman.m_blockman.LookupBlockIndex(block.hashPrevBlock)) { + chainman.UpdateUncommittedBlockStructures(block, pindexPrev); + } + } + + // Run diagnostics + BlockDiagnostics diag; + { + LOCK(cs_main); + Chainstate& active = chainman.ActiveChainstate(); + DiagnoseBlock(block, active, diag); + } + + // Build result + UniValue out(UniValue::VOBJ); + out.pushKV("ok", diag.ok()); + out.pushKV("blockhash", diag.block_hash.GetHex()); + + UniValue issues(UniValue::VARR); + for (const auto& iss : diag.issues) { + UniValue it(UniValue::VOBJ); + it.pushKV("level", iss.level == ValidationIssue::Level::ERROR ? "error" : "warning"); + it.pushKV("check", iss.check); + it.pushKV("message", iss.message); + issues.push_back(std::move(it)); + } + out.pushKV("issues", std::move(issues)); + return out; + } + }; +} + + static RPCHelpMan submitheader() { return RPCHelpMan{ @@ -1149,6 +1226,7 @@ void RegisterMiningRPCCommands(CRPCTable& t) {"hidden", &generatetodescriptor}, {"hidden", &generateblock}, {"hidden", &generate}, + {"mining", &diagnoseblock}, }; for (const auto& c : commands) { t.appendCommand(c.name, &c); diff --git a/src/validation.cpp b/src/validation.cpp index 8fcc719a6848..f48227a90e8e 100644 --- a/src/validation.cpp +++ b/src/validation.cpp @@ -6,6 +6,11 @@ #include // IWYU pragma: keep #include +#include + +#include +#include