Skip to content

Commit aa6fb37

Browse files
committed
Merge bitcoin/bitcoin#26205: wallet: #25768 follow ups
b01682a refactor: revert m_next_resend to not be std::atomic (stickies-v) 9245f45 wallet: only update m_next_resend when actually resending (stickies-v) 7fbde8a refactor: carve out tx resend timer logic into ShouldResend (stickies-v) 01f3534 refactor: remove unused locks for ResubmitWalletTransactions (stickies-v) c6e8e11 wallet: fix capitalization in docstring (stickies-v) Pull request description: This PR addresses the outstanding comments/issues from #25768: - capitalization [typo](bitcoin/bitcoin#25768 (comment)) in docstring - remove [unused locks](bitcoin/bitcoin@01f3534) that we previously needed for `ReacceptWalletTransactions()` - before #25768, only `ResendWalletTransactions()` would reset `m_next_resend` (formerly called `nNextResend`). By unifying it with `ReacceptWalletTransactions()` into `ResubmitWalletTransactions()`, the number of callsites that would reset the `m_next_resend` timer increased - since `m_next_resend` is only used in case of `relay=true` (formerly `ResendWalletTransactions()`), this is unintuitive - it leads to [unexpected behaviour](bitcoin/bitcoin#25768 (comment)) such as transactions potentially never being rebroadcasted. - it makes the ResubmitWalletTransactions()` logic [more complicated than strictly necessary](bitcoin/bitcoin#25768 (comment)) - since #25768, we relied on an earlier call of `ResubmitWalletTransactions(relay=false, force=true)` to initialize `m_next_resend()`, I think we can more elegantly do that by just providing `m_next_resend` with a default value - just to highlight: this commit introduces behaviour change Note: the `if (!fBroadcastTransactions)` in `CWallet:ShouldResend()` is duplicated on purpose, since it potentially avoids the slightly more expensive `if (!chain().isReadyToBroadcast())` check afterwards. I don't have a strong view on it, so happy to remove that additional check to reduce the diff, too. ACKs for top commit: aureleoules: ACK b01682a achow101: ACK b01682a Tree-SHA512: ac5f1d8858f8dd736dd1480f385984d660c1916b62a42562317020e8f9fd6a30bd8f23d973d47e4c9480d744c5ba39fdbefd69568a5eb0589a8422d7e5971c1c
2 parents 7e5fe03 + b01682a commit aa6fb37

File tree

3 files changed

+36
-34
lines changed

3 files changed

+36
-34
lines changed

src/wallet/rpc/backup.cpp

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -293,10 +293,7 @@ RPCHelpMan importaddress()
293293
if (fRescan)
294294
{
295295
RescanWallet(*pwallet, reserver);
296-
{
297-
LOCK(pwallet->cs_wallet);
298-
pwallet->ResubmitWalletTransactions(/*relay=*/false, /*force=*/true);
299-
}
296+
pwallet->ResubmitWalletTransactions(/*relay=*/false, /*force=*/true);
300297
}
301298

302299
return UniValue::VNULL;
@@ -474,10 +471,7 @@ RPCHelpMan importpubkey()
474471
if (fRescan)
475472
{
476473
RescanWallet(*pwallet, reserver);
477-
{
478-
LOCK(pwallet->cs_wallet);
479-
pwallet->ResubmitWalletTransactions(/*relay=*/false, /*force=*/true);
480-
}
474+
pwallet->ResubmitWalletTransactions(/*relay=*/false, /*force=*/true);
481475
}
482476

483477
return UniValue::VNULL;
@@ -1406,10 +1400,7 @@ RPCHelpMan importmulti()
14061400
}
14071401
if (fRescan && fRunScan && requests.size()) {
14081402
int64_t scannedTime = pwallet->RescanFromTime(nLowestTimestamp, reserver, true /* update */);
1409-
{
1410-
LOCK(pwallet->cs_wallet);
1411-
pwallet->ResubmitWalletTransactions(/*relay=*/false, /*force=*/true);
1412-
}
1403+
pwallet->ResubmitWalletTransactions(/*relay=*/false, /*force=*/true);
14131404

14141405
if (pwallet->IsAbortingRescan()) {
14151406
throw JSONRPCError(RPC_MISC_ERROR, "Rescan aborted by user.");
@@ -1700,10 +1691,7 @@ RPCHelpMan importdescriptors()
17001691
// Rescan the blockchain using the lowest timestamp
17011692
if (rescan) {
17021693
int64_t scanned_time = pwallet->RescanFromTime(lowest_timestamp, reserver, true /* update */);
1703-
{
1704-
LOCK(pwallet->cs_wallet);
1705-
pwallet->ResubmitWalletTransactions(/*relay=*/false, /*force=*/true);
1706-
}
1694+
pwallet->ResubmitWalletTransactions(/*relay=*/false, /*force=*/true);
17071695

17081696
if (pwallet->IsAbortingRescan()) {
17091697
throw JSONRPCError(RPC_MISC_ERROR, "Rescan aborted by user.");

src/wallet/wallet.cpp

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <primitives/block.h>
2222
#include <primitives/transaction.h>
2323
#include <psbt.h>
24+
#include <random.h>
2425
#include <script/descriptor.h>
2526
#include <script/script.h>
2627
#include <script/signingprovider.h>
@@ -1903,11 +1904,29 @@ std::set<uint256> CWallet::GetTxConflicts(const CWalletTx& wtx) const
19031904
return result;
19041905
}
19051906

1907+
bool CWallet::ShouldResend() const
1908+
{
1909+
// Don't attempt to resubmit if the wallet is configured to not broadcast
1910+
if (!fBroadcastTransactions) return false;
1911+
1912+
// During reindex, importing and IBD, old wallet transactions become
1913+
// unconfirmed. Don't resend them as that would spam other nodes.
1914+
// We only allow forcing mempool submission when not relaying to avoid this spam.
1915+
if (!chain().isReadyToBroadcast()) return false;
1916+
1917+
// Do this infrequently and randomly to avoid giving away
1918+
// that these are our transactions.
1919+
if (GetTime() < m_next_resend) return false;
1920+
1921+
return true;
1922+
}
1923+
1924+
int64_t CWallet::GetDefaultNextResend() { return GetTime() + (12 * 60 * 60) + GetRand(24 * 60 * 60); }
1925+
19061926
// Resubmit transactions from the wallet to the mempool, optionally asking the
19071927
// mempool to relay them. On startup, we will do this for all unconfirmed
19081928
// transactions but will not ask the mempool to relay them. We do this on startup
1909-
// to ensure that our own mempool is aware of our transactions, and to also
1910-
// initialize m_next_resend so that the actual rebroadcast is scheduled. There
1929+
// to ensure that our own mempool is aware of our transactions. There
19111930
// is a privacy side effect here as not broadcasting on startup also means that we won't
19121931
// inform the world of our wallet's state, particularly if the wallet (or node) is not
19131932
// yet synced.
@@ -1934,17 +1953,6 @@ void CWallet::ResubmitWalletTransactions(bool relay, bool force)
19341953
// even if forcing.
19351954
if (!fBroadcastTransactions) return;
19361955

1937-
// During reindex, importing and IBD, old wallet transactions become
1938-
// unconfirmed. Don't resend them as that would spam other nodes.
1939-
// We only allow forcing mempool submission when not relaying to avoid this spam.
1940-
if (!force && relay && !chain().isReadyToBroadcast()) return;
1941-
1942-
// Do this infrequently and randomly to avoid giving away
1943-
// that these are our transactions.
1944-
if (!force && GetTime() < m_next_resend) return;
1945-
// resend 12-36 hours from now, ~1 day on average.
1946-
m_next_resend = GetTime() + (12 * 60 * 60) + GetRand(24 * 60 * 60);
1947-
19481956
int submitted_tx_count = 0;
19491957

19501958
{ // cs_wallet scope
@@ -1957,7 +1965,7 @@ void CWallet::ResubmitWalletTransactions(bool relay, bool force)
19571965
// Only rebroadcast unconfirmed txs
19581966
if (!wtx.isUnconfirmed()) continue;
19591967

1960-
// attempt to rebroadcast all txes more than 5 minutes older than
1968+
// Attempt to rebroadcast all txes more than 5 minutes older than
19611969
// the last block, or all txs if forcing.
19621970
if (!force && wtx.nTimeReceived > m_best_block_time - 5 * 60) continue;
19631971
to_submit.insert(&wtx);
@@ -1979,7 +1987,9 @@ void CWallet::ResubmitWalletTransactions(bool relay, bool force)
19791987
void MaybeResendWalletTxs(WalletContext& context)
19801988
{
19811989
for (const std::shared_ptr<CWallet>& pwallet : GetWallets(context)) {
1990+
if (!pwallet->ShouldResend()) continue;
19821991
pwallet->ResubmitWalletTransactions(/*relay=*/true, /*force=*/false);
1992+
pwallet->SetNextResend();
19831993
}
19841994
}
19851995

@@ -3198,14 +3208,12 @@ bool CWallet::UpgradeWallet(int version, bilingual_str& error)
31983208

31993209
void CWallet::postInitProcess()
32003210
{
3201-
LOCK(cs_wallet);
3202-
32033211
// Add wallet transactions that aren't already in a block to mempool
32043212
// Do this here as mempool requires genesis block to be loaded
32053213
ResubmitWalletTransactions(/*relay=*/false, /*force=*/true);
32063214

32073215
// Update wallet transactions with current mempool transactions.
3208-
chain().requestMempoolTransactions(*this);
3216+
WITH_LOCK(cs_wallet, chain().requestMempoolTransactions(*this));
32093217
}
32103218

32113219
bool CWallet::BackupWallet(const std::string& strDest) const

src/wallet/wallet.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ class CWallet final : public WalletStorage, public interfaces::Chain::Notificati
250250
int nWalletVersion GUARDED_BY(cs_wallet){FEATURE_BASE};
251251

252252
/** The next scheduled rebroadcast of wallet transactions. */
253-
std::atomic<int64_t> m_next_resend{};
253+
int64_t m_next_resend{GetDefaultNextResend()};
254254
/** Whether this wallet will submit newly created transactions to the node's mempool and
255255
* prompt rebroadcasts (see ResendWalletTransactions()). */
256256
bool fBroadcastTransactions = false;
@@ -348,6 +348,8 @@ class CWallet final : public WalletStorage, public interfaces::Chain::Notificati
348348
*/
349349
static bool AttachChain(const std::shared_ptr<CWallet>& wallet, interfaces::Chain& chain, const bool rescan_required, bilingual_str& error, std::vector<bilingual_str>& warnings);
350350

351+
static int64_t GetDefaultNextResend();
352+
351353
public:
352354
/**
353355
* Main wallet lock.
@@ -537,6 +539,10 @@ class CWallet final : public WalletStorage, public interfaces::Chain::Notificati
537539
};
538540
ScanResult ScanForWalletTransactions(const uint256& start_block, int start_height, std::optional<int> max_height, const WalletRescanReserver& reserver, bool fUpdate, const bool save_progress);
539541
void transactionRemovedFromMempool(const CTransactionRef& tx, MemPoolRemovalReason reason, uint64_t mempool_sequence) override;
542+
/** Set the next time this wallet should resend transactions to 12-36 hours from now, ~1 day on average. */
543+
void SetNextResend() { m_next_resend = GetDefaultNextResend(); }
544+
/** Return true if all conditions for periodically resending transactions are met. */
545+
bool ShouldResend() const;
540546
void ResubmitWalletTransactions(bool relay, bool force);
541547

542548
OutputType TransactionChangeType(const std::optional<OutputType>& change_type, const std::vector<CRecipient>& vecSend) const;

0 commit comments

Comments
 (0)