diff --git a/src/wallet/wallet.h b/src/wallet/wallet.h --- a/src/wallet/wallet.h +++ b/src/wallet/wallet.h @@ -626,7 +626,7 @@ int64_t GetTxTime() const; - // RelayWalletTransaction may only be called if fBroadcastTransactions! + // Pass this transaction to the node to relay to its peers bool RelayWalletTransaction(interfaces::Chain::Lock &locked_chain); /** diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp --- a/src/wallet/wallet.cpp +++ b/src/wallet/wallet.cpp @@ -2169,23 +2169,37 @@ } bool CWalletTx::RelayWalletTransaction(interfaces::Chain::Lock &locked_chain) { - assert(pwallet->GetBroadcastTransactions()); - if (IsCoinBase() || isAbandoned() || - GetDepthInMainChain(locked_chain) != 0) { + // Can't relay if wallet is not broadcasting + if (!pwallet->GetBroadcastTransactions()) { return false; } - - CValidationState state; - // GetDepthInMainChain already catches known conflicts. - if (InMempool() || AcceptToMemoryPool(locked_chain, state)) { - pwallet->WalletLogPrintf("Relaying wtx %s\n", GetId().ToString()); - if (pwallet->chain().p2pEnabled()) { - pwallet->chain().relayTransaction(GetId()); - return true; - } + // Don't relay coinbase transactions outside blocks + if (IsCoinBase()) { + return false; + } + // Don't relay abandoned transactions + if (isAbandoned()) { + return false; + } + // Don't relay conflicted or already confirmed transactions + if (GetDepthInMainChain(locked_chain) != 0) { + return false; + } + // Don't relay transactions that aren't accepted to the mempool + CValidationState unused_state; + if (!InMempool() && !AcceptToMemoryPool(locked_chain, unused_state)) { + return false; + } + // Don't try to relay if the node is not connected to the p2p network + if (!pwallet->chain().p2pEnabled()) { + return false; } - return false; + // Try to relay the transaction + pwallet->WalletLogPrintf("Relaying wtx %s\n", GetId().ToString()); + pwallet->chain().relayTransaction(GetId()); + + return true; } std::set CWalletTx::GetConflicts() const {