Skip to content

Commit 1c4f597

Browse files
committed
Merge #19879: [p2p] miscellaneous wtxid followups
a8a64ac [BroadcastTransaction] Remove unsafe move operator (Amiti Uttarwar) 125c038 [p2p] Remove dead code (Amiti Uttarwar) fc66d0a [p2p] Check for nullptr before dereferencing pointer (Adam Jonas) cb79b9d [mempool] Revert unbroadcast set to tracking just txid (Amiti Uttarwar) Pull request description: Addresses some outstanding review comments from #18044 - reverts unbroadcast txids to a set instead of a map (simpler, communicates intent better, takes less space, no efficiency advantages of map) - adds safety around two touchpoints (check for nullptr before dereferencing pointer, remove an inaccurate std::move operator) - removes some dead code Links to comments on wtxid PR: [1](bitcoin/bitcoin#18044 (comment)) [2](bitcoin/bitcoin#18044 (comment)) [3](bitcoin/bitcoin#18044 (comment)) thanks to jnewbery & adamjonas for flagging these ! ! ACKs for top commit: sdaftuar: utACK a8a64ac naumenkogs: utACK a8a64ac jnewbery: utACK a8a64ac Tree-SHA512: 7be669cb30cc17fb9e06b50e636ef7887c6a27354697987e4e4d38dba4b8f50e175647587430cd9bc3295bec01ce8b1e6639a50a4249d8fff9b1ca1b9ead3277
2 parents 62e3eb9 + a8a64ac commit 1c4f597

File tree

4 files changed

+29
-32
lines changed

4 files changed

+29
-32
lines changed

src/net_processing.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -889,15 +889,16 @@ void PeerManager::InitializeNode(CNode *pnode) {
889889

890890
void PeerManager::ReattemptInitialBroadcast(CScheduler& scheduler) const
891891
{
892-
std::map<uint256, uint256> unbroadcast_txids = m_mempool.GetUnbroadcastTxs();
892+
std::set<uint256> unbroadcast_txids = m_mempool.GetUnbroadcastTxs();
893893

894-
for (const auto& elem : unbroadcast_txids) {
895-
// Sanity check: all unbroadcast txns should exist in the mempool
896-
if (m_mempool.exists(elem.first)) {
894+
for (const auto& txid : unbroadcast_txids) {
895+
CTransactionRef tx = m_mempool.get(txid);
896+
897+
if (tx != nullptr) {
897898
LOCK(cs_main);
898-
RelayTransaction(elem.first, elem.second, m_connman);
899+
RelayTransaction(txid, tx->GetWitnessHash(), m_connman);
899900
} else {
900-
m_mempool.RemoveUnbroadcastTx(elem.first, true);
901+
m_mempool.RemoveUnbroadcastTx(txid, true);
901902
}
902903
}
903904

@@ -1492,8 +1493,9 @@ void RelayTransaction(const uint256& txid, const uint256& wtxid, const CConnman&
14921493
{
14931494
LockAssertion lock(::cs_main);
14941495

1495-
CNodeState &state = *State(pnode->GetId());
1496-
if (state.m_wtxid_relay) {
1496+
CNodeState* state = State(pnode->GetId());
1497+
if (state == nullptr) return;
1498+
if (state->m_wtxid_relay) {
14971499
pnode->PushTxInventory(wtxid);
14981500
} else {
14991501
pnode->PushTxInventory(txid);
@@ -3105,8 +3107,6 @@ void PeerManager::ProcessMessage(CNode& pfrom, const std::string& msg_type, CDat
31053107
if (RecursiveDynamicUsage(*ptx) < 100000) {
31063108
AddToCompactExtraTransactions(ptx);
31073109
}
3108-
} else if (tx.HasWitness() && RecursiveDynamicUsage(*ptx) < 100000) {
3109-
AddToCompactExtraTransactions(ptx);
31103110
}
31113111

31123112
if (pfrom.HasPermission(PF_FORCERELAY)) {

src/node/transaction.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ TransactionError BroadcastTransaction(NodeContext& node, const CTransactionRef t
3838
if (!node.mempool->exists(hashTx)) {
3939
// Transaction is not already in the mempool. Submit it.
4040
TxValidationState state;
41-
if (!AcceptToMemoryPool(*node.mempool, state, std::move(tx),
42-
nullptr /* plTxnReplaced */, false /* bypass_limits */, max_tx_fee)) {
41+
if (!AcceptToMemoryPool(*node.mempool, state, tx,
42+
nullptr /* plTxnReplaced */, false /* bypass_limits */, max_tx_fee)) {
4343
err_string = state.ToString();
4444
if (state.IsInvalid()) {
4545
if (state.GetResult() == TxValidationResult::TX_MISSING_INPUTS) {
@@ -80,7 +80,7 @@ TransactionError BroadcastTransaction(NodeContext& node, const CTransactionRef t
8080
if (relay) {
8181
// the mempool tracks locally submitted transactions to make a
8282
// best-effort of initial broadcast
83-
node.mempool->AddUnbroadcastTx(hashTx, tx->GetWitnessHash());
83+
node.mempool->AddUnbroadcastTx(hashTx);
8484

8585
LOCK(cs_main);
8686
RelayTransaction(hashTx, tx->GetWitnessHash(), *node.connman);

src/txmempool.h

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -587,10 +587,9 @@ class CTxMemPool
587587
std::vector<indexed_transaction_set::const_iterator> GetSortedDepthAndScore() const EXCLUSIVE_LOCKS_REQUIRED(cs);
588588

589589
/**
590-
* track locally submitted transactions to periodically retry initial broadcast
591-
* map of txid -> wtxid
590+
* Track locally submitted transactions to periodically retry initial broadcast.
592591
*/
593-
std::map<uint256, uint256> m_unbroadcast_txids GUARDED_BY(cs);
592+
std::set<uint256> m_unbroadcast_txids GUARDED_BY(cs);
594593

595594
public:
596595
indirectmap<COutPoint, const CTransaction*> mapNextTx GUARDED_BY(cs);
@@ -752,19 +751,20 @@ class CTxMemPool
752751
size_t DynamicMemoryUsage() const;
753752

754753
/** Adds a transaction to the unbroadcast set */
755-
void AddUnbroadcastTx(const uint256& txid, const uint256& wtxid) {
754+
void AddUnbroadcastTx(const uint256& txid)
755+
{
756756
LOCK(cs);
757-
// Sanity Check: the transaction should also be in the mempool
758-
if (exists(txid)) {
759-
m_unbroadcast_txids[txid] = wtxid;
760-
}
761-
}
757+
// Sanity check the transaction is in the mempool & insert into
758+
// unbroadcast set.
759+
if (exists(txid)) m_unbroadcast_txids.insert(txid);
760+
};
762761

763762
/** Removes a transaction from the unbroadcast set */
764763
void RemoveUnbroadcastTx(const uint256& txid, const bool unchecked = false);
765764

766765
/** Returns transactions in unbroadcast set */
767-
std::map<uint256, uint256> GetUnbroadcastTxs() const {
766+
std::set<uint256> GetUnbroadcastTxs() const
767+
{
768768
LOCK(cs);
769769
return m_unbroadcast_txids;
770770
}

src/validation.cpp

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5123,21 +5123,18 @@ bool LoadMempool(CTxMemPool& pool)
51235123
}
51245124

51255125
// TODO: remove this try except in v0.22
5126-
std::map<uint256, uint256> unbroadcast_txids;
5126+
std::set<uint256> unbroadcast_txids;
51275127
try {
51285128
file >> unbroadcast_txids;
51295129
unbroadcast = unbroadcast_txids.size();
51305130
} catch (const std::exception&) {
51315131
// mempool.dat files created prior to v0.21 will not have an
51325132
// unbroadcast set. No need to log a failure if parsing fails here.
51335133
}
5134-
for (const auto& elem : unbroadcast_txids) {
5135-
// Don't add unbroadcast transactions that didn't get back into the
5136-
// mempool.
5137-
const CTransactionRef& added_tx = pool.get(elem.first);
5138-
if (added_tx != nullptr) {
5139-
pool.AddUnbroadcastTx(elem.first, added_tx->GetWitnessHash());
5140-
}
5134+
for (const auto& txid : unbroadcast_txids) {
5135+
// Ensure transactions were accepted to mempool then add to
5136+
// unbroadcast set.
5137+
if (pool.get(txid) != nullptr) pool.AddUnbroadcastTx(txid);
51415138
}
51425139
} catch (const std::exception& e) {
51435140
LogPrintf("Failed to deserialize mempool data on disk: %s. Continuing anyway.\n", e.what());
@@ -5154,7 +5151,7 @@ bool DumpMempool(const CTxMemPool& pool)
51545151

51555152
std::map<uint256, CAmount> mapDeltas;
51565153
std::vector<TxMempoolInfo> vinfo;
5157-
std::map<uint256, uint256> unbroadcast_txids;
5154+
std::set<uint256> unbroadcast_txids;
51585155

51595156
static Mutex dump_mutex;
51605157
LOCK(dump_mutex);

0 commit comments

Comments
 (0)