Conditional registration seems pretty straightforward here (when relying on gArgs):
<details>
<summary>git diff on e10f3ed76f</summary>
diff --git a/doc/release-notes-35267.md b/doc/release-notes-35267.md
index 07a36d4d13..d5d54c762f 100644
--- a/doc/release-notes-35267.md
+++ b/doc/release-notes-35267.md
@@ -1,5 +1,7 @@
RPC
---
-The `getprivatebroadcastinfo` RPC now throws with error code `-32601` if the node is not running with the `-privatebroadcast` startup option enabled.
+The `getprivatebroadcastinfo` and `abortprivatebroadcast` RPCs are now only available
+when the node is running with the `-privatebroadcast` startup option enabled. When it is
+disabled, calling them returns the `-32601` ("Method not found") error.
diff --git a/src/net_processing.cpp b/src/net_processing.cpp
index 0ca56e7041..50ea62ff05 100644
--- a/src/net_processing.cpp
+++ b/src/net_processing.cpp
@@ -1852,7 +1852,6 @@ PeerManagerInfo PeerManagerImpl::GetInfo() const
return PeerManagerInfo{
.median_outbound_time_offset = m_outbound_time_offsets.Median(),
.ignores_incoming_txs = m_opts.ignore_incoming_txs,
- .private_broadcast = m_opts.private_broadcast,
};
}
diff --git a/src/net_processing.h b/src/net_processing.h
index 630656e2af..4ae48f84e4 100644
--- a/src/net_processing.h
+++ b/src/net_processing.h
@@ -71,7 +71,6 @@ struct CNodeStateStats {
struct PeerManagerInfo {
std::chrono::seconds median_outbound_time_offset{0s};
bool ignores_incoming_txs{false};
- bool private_broadcast{DEFAULT_PRIVATE_BROADCAST};
};
class PeerManager : public CValidationInterface, public NetEventsInterface
diff --git a/src/rpc/mempool.cpp b/src/rpc/mempool.cpp
index 6747458a12..acd5fa6e2f 100644
--- a/src/rpc/mempool.cpp
+++ b/src/rpc/mempool.cpp
@@ -178,9 +178,6 @@ static RPCMethod getprivatebroadcastinfo()
const PeerManager& peerman{EnsurePeerman(node)};
const auto txs{peerman.GetPrivateBroadcastInfo()};
- if (!peerman.GetInfo().private_broadcast) {
- throw JSONRPCError(RPC_METHOD_NOT_FOUND, "Private broadcast is not enabled. Ensure you're running Bitcoin Core with -privatebroadcast=1.");
- }
UniValue transactions(UniValue::VARR);
for (const auto& tx_info : txs) {
UniValue o(UniValue::VOBJ);
@@ -1534,8 +1531,6 @@ void RegisterMempoolRPCCommands(CRPCTable& t)
{
static const CRPCCommand commands[]{
{"rawtransactions", &sendrawtransaction},
- {"rawtransactions", &getprivatebroadcastinfo},
- {"rawtransactions", &abortprivatebroadcast},
{"rawtransactions", &testmempoolaccept},
{"blockchain", &getmempoolancestors},
{"blockchain", &getmempooldescendants},
@@ -1553,4 +1548,14 @@ void RegisterMempoolRPCCommands(CRPCTable& t)
for (const auto& c : commands) {
t.appendCommand(c.name, &c);
}
+
+ if (gArgs.GetBoolArg("-privatebroadcast", DEFAULT_PRIVATE_BROADCAST)) {
+ static const CRPCCommand private_broadcast_commands[]{
+ {"rawtransactions", &getprivatebroadcastinfo},
+ {"rawtransactions", &abortprivatebroadcast},
+ };
+ for (const auto& c : private_broadcast_commands) {
+ t.appendCommand(c.name, &c);
+ }
+ }
}
diff --git a/test/functional/p2p_private_broadcast.py b/test/functional/p2p_private_broadcast.py
index 7afedea476..dfea0ce979 100755
--- a/test/functional/p2p_private_broadcast.py
+++ b/test/functional/p2p_private_broadcast.py
@@ -223,11 +223,13 @@ class P2PPrivateBroadcast(BitcoinTestFramework):
tx_receiver = self.nodes[1]
far_observer = tx_receiver.add_p2p_connection(P2PInterface())
- self.log.info("Test getprivatebroadcastinfo fails if the node is running without -privatebroadcast set")
- assert_raises_rpc_error(-32601, "Private broadcast is not enabled. Ensure you're running Bitcoin Core with -privatebroadcast=1.",
- tx_receiver.getprivatebroadcastinfo)
-
- wallet = MiniWallet(tx_originator)
+ self.log.info("Test the private broadcast RPCs are only registered when -privatebroadcast is set")
+ assert "getprivatebroadcastinfo" not in tx_receiver.help()
+ assert "abortprivatebroadcast" not in tx_receiver.help()
+ assert_raises_rpc_error(-32601, "Method not found", tx_receiver.getprivatebroadcastinfo)
+ assert_raises_rpc_error(-32601, "Method not found", tx_receiver.abortprivatebroadcast, "00" * 32)
+ assert "getprivatebroadcastinfo" in tx_originator.help()
+ assert "abortprivatebroadcast" in tx_originator.help()
self.fill_node_addrman(node_index=0, address_types_to_add=[CAddress.NET_IPV4, CAddress.NET_IPV6, CAddress.NET_TORV3, CAddress.NET_I2P, CAddress.NET_CJDNS])
</details>