Merge bitcoin/bitcoin#26132: wallet: Fix nNextResend data race in ResubmitWalletTransactions

fad61573ed Fix nNextResend data race in ResubmitWalletTransactions (MacroFake)

Pull request description:

  Now that `ResubmitWalletTransactions` is called from more than one thread, it is no longer thread-safe.

  Introduced in 5291933fed.

ACKs for top commit:
  achow101:
    ACK fad61573ed
  jonatack:
    ACK fad61573ed
  stickies-v:
    However, I think the current data race UB fix in fad61573e is the most critical to get into v24, so: ACK fad61573e - but open to further improvements.

Tree-SHA512: 54da2ed1c5f44e33588ac1d21ce26908fcf0bfe785c28ba8f6a479389b5ab7a0b32b016d4c482a2ccb405e0686efb61ffe23e427f5e589dc7d2b3c7469978977
This commit is contained in:
Andrew Chow 2022-09-20 18:38:19 -04:00
commit 9e2a2b88d5
No known key found for this signature in database
GPG key ID: 17565732E08E5E41
2 changed files with 4 additions and 4 deletions

View file

@ -1907,7 +1907,7 @@ std::set<uint256> CWallet::GetTxConflicts(const CWalletTx& wtx) const
// mempool to relay them. On startup, we will do this for all unconfirmed
// transactions but will not ask the mempool to relay them. We do this on startup
// to ensure that our own mempool is aware of our transactions, and to also
// initialize nNextResend so that the actual rebroadcast is scheduled. There
// initialize m_next_resend so that the actual rebroadcast is scheduled. There
// is a privacy side effect here as not broadcasting on startup also means that we won't
// inform the world of our wallet's state, particularly if the wallet (or node) is not
// yet synced.
@ -1941,9 +1941,9 @@ void CWallet::ResubmitWalletTransactions(bool relay, bool force)
// Do this infrequently and randomly to avoid giving away
// that these are our transactions.
if (!force && GetTime() < nNextResend) return;
if (!force && GetTime() < m_next_resend) return;
// resend 12-36 hours from now, ~1 day on average.
nNextResend = GetTime() + (12 * 60 * 60) + GetRand(24 * 60 * 60);
m_next_resend = GetTime() + (12 * 60 * 60) + GetRand(24 * 60 * 60);
int submitted_tx_count = 0;

View file

@ -250,7 +250,7 @@ private:
int nWalletVersion GUARDED_BY(cs_wallet){FEATURE_BASE};
/** The next scheduled rebroadcast of wallet transactions. */
int64_t nNextResend = 0;
std::atomic<int64_t> m_next_resend{};
/** Whether this wallet will submit newly created transactions to the node's mempool and
* prompt rebroadcasts (see ResendWalletTransactions()). */
bool fBroadcastTransactions = false;