Skip to content

Commit 7efc9cf

Browse files
committed
Merge pull request #5951
77650cc add -walletbroadcast=0 rpc test (Jonas Schnelli) 6f25262 wallet: make it possible to disable transaction broadcast (Wladimir J. van der Laan)
2 parents eb87f84 + 77650cc commit 7efc9cf

File tree

4 files changed

+64
-7
lines changed

4 files changed

+64
-7
lines changed

qa/rpc-tests/wallet.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,47 @@ def run_test (self):
150150
sync_mempools(self.nodes)
151151

152152
assert(txid1 in self.nodes[3].getrawmempool())
153+
154+
155+
#do some -walletbroadcast tests
156+
stop_nodes(self.nodes)
157+
wait_bitcoinds()
158+
self.nodes = start_nodes(3, self.options.tmpdir, [["-walletbroadcast=0"],["-walletbroadcast=0"],["-walletbroadcast=0"]])
159+
connect_nodes_bi(self.nodes,0,1)
160+
connect_nodes_bi(self.nodes,1,2)
161+
connect_nodes_bi(self.nodes,0,2)
162+
self.sync_all()
153163

164+
txIdNotBroadcasted = self.nodes[0].sendtoaddress(self.nodes[2].getnewaddress(), 2);
165+
txObjNotBroadcasted = self.nodes[0].gettransaction(txIdNotBroadcasted)
166+
self.nodes[1].setgenerate(True, 1) #mine a block, tx should not be in there
167+
self.sync_all()
168+
assert_equal(self.nodes[2].getbalance(), Decimal('59.99800000')); #should not be changed because tx was not broadcasted
169+
170+
#now broadcast from another node, mine a block, sync, and check the balance
171+
self.nodes[1].sendrawtransaction(txObjNotBroadcasted['hex'])
172+
self.nodes[1].setgenerate(True, 1)
173+
self.sync_all()
174+
txObjNotBroadcasted = self.nodes[0].gettransaction(txIdNotBroadcasted)
175+
assert_equal(self.nodes[2].getbalance(), Decimal('61.99800000')); #should not be
176+
177+
#create another tx
178+
txIdNotBroadcasted = self.nodes[0].sendtoaddress(self.nodes[2].getnewaddress(), 2);
179+
180+
#restart the nodes with -walletbroadcast=1
181+
stop_nodes(self.nodes)
182+
wait_bitcoinds()
183+
self.nodes = start_nodes(3, self.options.tmpdir)
184+
connect_nodes_bi(self.nodes,0,1)
185+
connect_nodes_bi(self.nodes,1,2)
186+
connect_nodes_bi(self.nodes,0,2)
187+
sync_blocks(self.nodes)
188+
189+
self.nodes[0].setgenerate(True, 1)
190+
sync_blocks(self.nodes)
191+
192+
#tx should be added to balance because after restarting the nodes tx should be broadcastet
193+
assert_equal(self.nodes[2].getbalance(), Decimal('63.99800000')); #should not be
194+
154195
if __name__ == '__main__':
155196
WalletTest ().main ()

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)"));
@@ -1243,6 +1244,7 @@ bool AppInit2(boost::thread_group& threadGroup)
12431244
}
12441245
}
12451246
}
1247+
pwalletMain->SetBroadcastTransactions(GetBoolArg("-walletbroadcast", true));
12461248
} // (!fDisableWallet)
12471249
#else // ENABLE_WALLET
12481250
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)