Skip to content

Commit 357f1f4

Browse files
committed
rpc: use private broadcast from sendrawtransaction RPC if -privatebroadcast is ON
1 parent dad949f commit 357f1f4

File tree

1 file changed

+29
-5
lines changed

1 file changed

+29
-5
lines changed

src/rpc/mempool.cpp

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@
88
#include <node/mempool_persist.h>
99

1010
#include <chainparams.h>
11+
#include <common/args.h>
1112
#include <consensus/validation.h>
1213
#include <core_io.h>
1314
#include <kernel/mempool_entry.h>
1415
#include <net_processing.h>
16+
#include <netbase.h> // for g_reachable_nets
1517
#include <node/mempool_persist_args.h>
1618
#include <node/types.h>
1719
#include <policy/rbf.h>
@@ -44,11 +46,21 @@ static RPCHelpMan sendrawtransaction()
4446
{
4547
return RPCHelpMan{
4648
"sendrawtransaction",
47-
"Submit a raw transaction (serialized, hex-encoded) to local node and network.\n"
48-
"\nThe transaction will be sent unconditionally to all peers, so using sendrawtransaction\n"
49-
"for manual rebroadcast may degrade privacy by leaking the transaction's origin, as\n"
50-
"nodes will normally not rebroadcast non-wallet transactions already in their mempool.\n"
49+
"Submit a raw transaction (serialized, hex-encoded) to the network.\n"
50+
51+
"\nIf -privatebroadcast is disabled, then the transaction will be put into the\n"
52+
"local mempool of the node and will be sent unconditionally to all currently\n"
53+
"connected peers, so using sendrawtransaction for manual rebroadcast will degrade\n"
54+
"privacy by leaking the transaction's origin, as nodes will normally not\n"
55+
"rebroadcast non-wallet transactions already in their mempool.\n"
56+
57+
"\nIf -privatebroadcast is enabled, then the transaction will be sent only via\n"
58+
"dedicated, short-lived connections to Tor or I2P peers or IPv4/IPv6 peers\n"
59+
"through the Tor network. This conceals the transaction origin. The transaction\n"
60+
"will only enter the local mempool when it is received back from the network.\n"
61+
5162
"\nA specific exception, RPC_TRANSACTION_ALREADY_IN_UTXO_SET, may throw if the transaction cannot be added to the mempool.\n"
63+
5264
"\nRelated RPCs: createrawtransaction, signrawtransactionwithkey\n",
5365
{
5466
{"hexstring", RPCArg::Type::STR_HEX, RPCArg::Optional::NO, "The hex string of the raw transaction"},
@@ -98,11 +110,23 @@ static RPCHelpMan sendrawtransaction()
98110
std::string err_string;
99111
AssertLockNotHeld(cs_main);
100112
NodeContext& node = EnsureAnyNodeContext(request.context);
113+
const bool private_broadcast_enabled{gArgs.GetBoolArg("-privatebroadcast", DEFAULT_PRIVATE_BROADCAST)};
114+
if (private_broadcast_enabled &&
115+
!g_reachable_nets.Contains(NET_ONION) &&
116+
!g_reachable_nets.Contains(NET_I2P)) {
117+
throw JSONRPCError(RPC_MISC_ERROR,
118+
"-privatebroadcast is enabled, but none of the Tor or I2P networks is "
119+
"reachable. Maybe the location of the Tor proxy couldn't be retrieved "
120+
"from the Tor daemon at startup. Check whether the Tor daemon is running "
121+
"and that -torcontrol, -torpassword and -i2psam are configured properly.");
122+
}
123+
const auto method = private_broadcast_enabled ? node::TxBroadcast::NO_MEMPOOL_PRIVATE_BROADCAST
124+
: node::TxBroadcast::MEMPOOL_AND_BROADCAST_TO_ALL;
101125
const TransactionError err = BroadcastTransaction(node,
102126
tx,
103127
err_string,
104128
max_raw_tx_fee,
105-
node::TxBroadcast::MEMPOOL_AND_BROADCAST_TO_ALL,
129+
method,
106130
/*wait_callback=*/true);
107131
if (TransactionError::OK != err) {
108132
throw JSONRPCTransactionError(err, err_string);

0 commit comments

Comments
 (0)