Skip to content

Commit 7a9046e

Browse files
committed
[wallet] Refactor CWalletTx::RelayWalletTransaction()
This refactors the CWalletTx::RelayWalletTransaction() function to be clearer and adds comments. It also makes two minor behavior changes: - no longer assert if fBroadcastTransactions is false. Just return false from the function. - no longer print the relay message if p2pEnabled is set to false (since the transaction is not actually relayed).
1 parent 8c022e8 commit 7a9046e

File tree

2 files changed

+20
-15
lines changed

2 files changed

+20
-15
lines changed

src/wallet/wallet.cpp

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1896,20 +1896,25 @@ void CWallet::ReacceptWalletTransactions(interfaces::Chain::Lock& locked_chain)
18961896

18971897
bool CWalletTx::RelayWalletTransaction(interfaces::Chain::Lock& locked_chain)
18981898
{
1899-
assert(pwallet->GetBroadcastTransactions());
1900-
if (!IsCoinBase() && !isAbandoned() && GetDepthInMainChain(locked_chain) == 0)
1901-
{
1902-
CValidationState state;
1903-
/* GetDepthInMainChain already catches known conflicts. */
1904-
if (InMempool() || AcceptToMemoryPool(locked_chain, state)) {
1905-
pwallet->WalletLogPrintf("Relaying wtx %s\n", GetHash().ToString());
1906-
if (pwallet->chain().p2pEnabled()) {
1907-
pwallet->chain().relayTransaction(GetHash());
1908-
return true;
1909-
}
1910-
}
1911-
}
1912-
return false;
1899+
// Can't relay if wallet is not broadcasting
1900+
if (!pwallet->GetBroadcastTransactions()) return false;
1901+
// Don't relay coinbase transactions outside blocks
1902+
if (IsCoinBase()) return false;
1903+
// Don't relay abandoned transactions
1904+
if (isAbandoned()) return false;
1905+
// Don't relay conflicted or already confirmed transactions
1906+
if (GetDepthInMainChain(locked_chain) != 0) return false;
1907+
// Don't relay transactions that aren't accepted to the mempool
1908+
CValidationState unused_state;
1909+
if (!InMempool() && !AcceptToMemoryPool(locked_chain, unused_state)) return false;
1910+
// Don't try to relay if the node is not connected to the p2p network
1911+
if (!pwallet->chain().p2pEnabled()) return false;
1912+
1913+
// Try to relay the transaction
1914+
pwallet->WalletLogPrintf("Relaying wtx %s\n", GetHash().ToString());
1915+
pwallet->chain().relayTransaction(GetHash());
1916+
1917+
return true;
19131918
}
19141919

19151920
std::set<uint256> CWalletTx::GetConflicts() const

src/wallet/wallet.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -535,7 +535,7 @@ class CWalletTx : public CMerkleTx
535535

536536
int64_t GetTxTime() const;
537537

538-
// RelayWalletTransaction may only be called if fBroadcastTransactions!
538+
// Pass this transaction to the node to relay to its peers
539539
bool RelayWalletTransaction(interfaces::Chain::Lock& locked_chain);
540540

541541
/** Pass this transaction to the mempool. Fails if absolute fee exceeds absurd fee. */

0 commit comments

Comments
 (0)