Skip to content

Commit 6f25262

Browse files
committed
wallet: make it possible to disable transaction broadcast
This is an advanced feature which will disable any kind of automatic transaction broadcasting in the wallet. This gives the user full control of how the transaction is sent. For example they can broadcast new transactions through some other mechanism themselves, after getting the transaction hex through `gettransaction`. This just adds the option `-walletbroadcast=<0,1>`. Right now these transactions will get the status Status: conflicted, has not been successfully broadcast yet They shouldn't be shown as conflicted at all (`walletconflicts` is empty). This status will go away when the transaction is received through the network.
1 parent 41113e3 commit 6f25262

File tree

3 files changed

+23
-7
lines changed

3 files changed

+23
-7
lines changed

src/init.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,7 @@ std::string HelpMessage(HelpMessageMode mode)
337337
FormatMoney(maxTxFee)));
338338
strUsage += HelpMessageOpt("-upgradewallet", _("Upgrade wallet to latest format") + " " + _("on startup"));
339339
strUsage += HelpMessageOpt("-wallet=<file>", _("Specify wallet file (within data directory)") + " " + strprintf(_("(default: %s)"), "wallet.dat"));
340+
strUsage += HelpMessageOpt("-walletbroadcast", _("Make the wallet broadcast transactions") + " " + strprintf(_("(default: %u)"), true));
340341
strUsage += HelpMessageOpt("-walletnotify=<cmd>", _("Execute command when a wallet transaction changes (%s in cmd is replaced by TxID)"));
341342
strUsage += HelpMessageOpt("-zapwallettxes=<mode>", _("Delete all wallet transactions and only recover those parts of the blockchain through -rescan on startup") +
342343
" " + _("(1 = keep tx meta data e.g. account owner and payment request information, 2 = drop tx meta data)"));
@@ -1242,6 +1243,7 @@ bool AppInit2(boost::thread_group& threadGroup)
12421243
}
12431244
}
12441245
}
1246+
pwalletMain->SetBroadcastTransactions(GetBoolArg("-walletbroadcast", true));
12451247
} // (!fDisableWallet)
12461248
#else // ENABLE_WALLET
12471249
LogPrintf("No wallet compiled in!\n");

src/wallet/wallet.cpp

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1096,6 +1096,9 @@ int CWallet::ScanForWalletTransactions(CBlockIndex* pindexStart, bool fUpdate)
10961096

10971097
void CWallet::ReacceptWalletTransactions()
10981098
{
1099+
// If transcations aren't broadcasted, don't let them into local mempool either
1100+
if (!fBroadcastTransactions)
1101+
return;
10991102
LOCK2(cs_main, cs_wallet);
11001103
BOOST_FOREACH(PAIRTYPE(const uint256, CWalletTx)& item, mapWallet)
11011104
{
@@ -1116,6 +1119,7 @@ void CWallet::ReacceptWalletTransactions()
11161119

11171120
bool CWalletTx::RelayWalletTransaction()
11181121
{
1122+
assert(pwallet->GetBroadcastTransactions());
11191123
if (!IsCoinBase())
11201124
{
11211125
if (GetDepthInMainChain() == 0) {
@@ -1354,7 +1358,7 @@ void CWallet::ResendWalletTransactions(int64_t nBestBlockTime)
13541358
{
13551359
// Do this infrequently and randomly to avoid giving away
13561360
// that these are our transactions.
1357-
if (GetTime() < nNextResend)
1361+
if (GetTime() < nNextResend || !fBroadcastTransactions)
13581362
return;
13591363
bool fFirst = (nNextResend == 0);
13601364
nNextResend = GetTime() + GetRand(30 * 60);
@@ -1979,14 +1983,17 @@ bool CWallet::CommitTransaction(CWalletTx& wtxNew, CReserveKey& reservekey)
19791983
// Track how many getdata requests our transaction gets
19801984
mapRequestCount[wtxNew.GetHash()] = 0;
19811985

1982-
// Broadcast
1983-
if (!wtxNew.AcceptToMemoryPool(false))
1986+
if (fBroadcastTransactions)
19841987
{
1985-
// This must not fail. The transaction has already been signed and recorded.
1986-
LogPrintf("CommitTransaction(): Error: Transaction not valid");
1987-
return false;
1988+
// Broadcast
1989+
if (!wtxNew.AcceptToMemoryPool(false))
1990+
{
1991+
// This must not fail. The transaction has already been signed and recorded.
1992+
LogPrintf("CommitTransaction(): Error: Transaction not valid");
1993+
return false;
1994+
}
1995+
wtxNew.RelayWalletTransaction();
19881996
}
1989-
wtxNew.RelayWalletTransaction();
19901997
}
19911998
return true;
19921999
}

src/wallet/wallet.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,7 @@ class CWallet : public CCryptoKeyStore, public CValidationInterface
455455

456456
int64_t nNextResend;
457457
int64_t nLastResend;
458+
bool fBroadcastTransactions;
458459

459460
/**
460461
* Used to keep track of spent outpoints, and
@@ -518,6 +519,7 @@ class CWallet : public CCryptoKeyStore, public CValidationInterface
518519
nNextResend = 0;
519520
nLastResend = 0;
520521
nTimeFirstKey = 0;
522+
fBroadcastTransactions = false;
521523
}
522524

523525
std::map<uint256, CWalletTx> mapWallet;
@@ -723,6 +725,11 @@ class CWallet : public CCryptoKeyStore, public CValidationInterface
723725

724726
/** Watch-only address added */
725727
boost::signals2::signal<void (bool fHaveWatchOnly)> NotifyWatchonlyChanged;
728+
729+
/** Inquire whether this wallet broadcasts transactions. */
730+
bool GetBroadcastTransactions() const { return fBroadcastTransactions; }
731+
/** Set whether this wallet broadcasts transactions. */
732+
void SetBroadcastTransactions(bool broadcast) { fBroadcastTransactions = broadcast; }
726733
};
727734

728735
/** A key allocated from the key pool. */

0 commit comments

Comments
 (0)