Skip to content

Commit

Permalink
refactor: carve out tx resend timer logic into ShouldResend
Browse files Browse the repository at this point in the history
Moves the logic of whether or not transactions should actually be
resent out of the function that's resending them. This reduces
responsibilities of ResubmitWalletTransactions and allows
carving out the updating of m_next_resend in a future commit.

Github-Pull: #26205
Rebased-From: 7fbde8a
  • Loading branch information
stickies-v authored and fanquake committed Oct 13, 2022
1 parent a6fb674 commit fc8f2bf
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
26 changes: 18 additions & 8 deletions src/wallet/wallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1903,6 +1903,23 @@ std::set<uint256> CWallet::GetTxConflicts(const CWalletTx& wtx) const
return result;
}

bool CWallet::ShouldResend() const
{
// Don't attempt to resubmit if the wallet is configured to not broadcast
if (!fBroadcastTransactions) return false;

// During reindex, importing and IBD, old wallet transactions become
// unconfirmed. Don't resend them as that would spam other nodes.
// We only allow forcing mempool submission when not relaying to avoid this spam.
if (!chain().isReadyToBroadcast()) return false;

// Do this infrequently and randomly to avoid giving away
// that these are our transactions.
if (GetTime() < m_next_resend) return false;

return true;
}

// Resubmit transactions from the wallet to the mempool, optionally asking the
// 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
Expand Down Expand Up @@ -1934,14 +1951,6 @@ void CWallet::ResubmitWalletTransactions(bool relay, bool force)
// even if forcing.
if (!fBroadcastTransactions) return;

// During reindex, importing and IBD, old wallet transactions become
// unconfirmed. Don't resend them as that would spam other nodes.
// We only allow forcing mempool submission when not relaying to avoid this spam.
if (!force && relay && !chain().isReadyToBroadcast()) return;

// Do this infrequently and randomly to avoid giving away
// that these are our transactions.
if (!force && GetTime() < m_next_resend) return;
// resend 12-36 hours from now, ~1 day on average.
m_next_resend = GetTime() + (12 * 60 * 60) + GetRand(24 * 60 * 60);

Expand Down Expand Up @@ -1979,6 +1988,7 @@ void CWallet::ResubmitWalletTransactions(bool relay, bool force)
void MaybeResendWalletTxs(WalletContext& context)
{
for (const std::shared_ptr<CWallet>& pwallet : GetWallets(context)) {
if (!pwallet->ShouldResend()) continue;
pwallet->ResubmitWalletTransactions(/*relay=*/true, /*force=*/false);
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/wallet/wallet.h
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,8 @@ class CWallet final : public WalletStorage, public interfaces::Chain::Notificati
};
ScanResult ScanForWalletTransactions(const uint256& start_block, int start_height, std::optional<int> max_height, const WalletRescanReserver& reserver, bool fUpdate, const bool save_progress);
void transactionRemovedFromMempool(const CTransactionRef& tx, MemPoolRemovalReason reason, uint64_t mempool_sequence) override;
/** Return true if all conditions for periodically resending transactions are met. */
bool ShouldResend() const;
void ResubmitWalletTransactions(bool relay, bool force);

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

0 comments on commit fc8f2bf

Please sign in to comment.