Skip to content

Commit 9245f45

Browse files
committed
wallet: only update m_next_resend when actually resending
We only want to relay our resubmitted transactions once every 12-36h. By separating the timer update logic out of ResubmitWalletTransactions and into MaybeResendWalletTxs we avoid non-relay calls (previously in the separate ReacceptWalletTransactions function) from resetting that timer.
1 parent 7fbde8a commit 9245f45

File tree

2 files changed

+10
-6
lines changed

2 files changed

+10
-6
lines changed

src/wallet/wallet.cpp

Lines changed: 5 additions & 5 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>
@@ -1920,11 +1921,12 @@ bool CWallet::ShouldResend() const
19201921
return true;
19211922
}
19221923

1924+
int64_t CWallet::GetDefaultNextResend() { return GetTime() + (12 * 60 * 60) + GetRand(24 * 60 * 60); }
1925+
19231926
// Resubmit transactions from the wallet to the mempool, optionally asking the
19241927
// mempool to relay them. On startup, we will do this for all unconfirmed
19251928
// transactions but will not ask the mempool to relay them. We do this on startup
1926-
// to ensure that our own mempool is aware of our transactions, and to also
1927-
// 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
19281930
// is a privacy side effect here as not broadcasting on startup also means that we won't
19291931
// inform the world of our wallet's state, particularly if the wallet (or node) is not
19301932
// yet synced.
@@ -1951,9 +1953,6 @@ void CWallet::ResubmitWalletTransactions(bool relay, bool force)
19511953
// even if forcing.
19521954
if (!fBroadcastTransactions) return;
19531955

1954-
// resend 12-36 hours from now, ~1 day on average.
1955-
m_next_resend = GetTime() + (12 * 60 * 60) + GetRand(24 * 60 * 60);
1956-
19571956
int submitted_tx_count = 0;
19581957

19591958
{ // cs_wallet scope
@@ -1990,6 +1989,7 @@ void MaybeResendWalletTxs(WalletContext& context)
19901989
for (const std::shared_ptr<CWallet>& pwallet : GetWallets(context)) {
19911990
if (!pwallet->ShouldResend()) continue;
19921991
pwallet->ResubmitWalletTransactions(/*relay=*/true, /*force=*/false);
1992+
pwallet->SetNextResend();
19931993
}
19941994
}
19951995

src/wallet/wallet.h

Lines changed: 5 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+
std::atomic<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,8 @@ 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(); }
540544
/** Return true if all conditions for periodically resending transactions are met. */
541545
bool ShouldResend() const;
542546
void ResubmitWalletTransactions(bool relay, bool force);

0 commit comments

Comments
 (0)