Skip to content

Commit aae58c4

Browse files
fanquakesidhujag
authored and
sidhujag
committed
Merge bitcoin#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 bitcoin#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#18044 (comment)) [2](bitcoin#18044 (comment)) [3](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
1 parent 34dda01 commit aae58c4

File tree

4 files changed

+29
-32
lines changed

4 files changed

+29
-32
lines changed

src/net_processing.cpp

+10-10
Original file line numberDiff line numberDiff line change
@@ -1035,15 +1035,16 @@ void PeerManager::InitializeNode(CNode *pnode) {
10351035

10361036
void PeerManager::ReattemptInitialBroadcast(CScheduler& scheduler) const
10371037
{
1038-
std::map<uint256, uint256> unbroadcast_txids = m_mempool.GetUnbroadcastTxs();
1038+
std::set<uint256> unbroadcast_txids = m_mempool.GetUnbroadcastTxs();
10391039

1040-
for (const auto& elem : unbroadcast_txids) {
1041-
// Sanity check: all unbroadcast txns should exist in the mempool
1042-
if (m_mempool.exists(elem.first)) {
1040+
for (const auto& txid : unbroadcast_txids) {
1041+
CTransactionRef tx = m_mempool.get(txid);
1042+
1043+
if (tx != nullptr) {
10431044
LOCK(cs_main);
1044-
RelayTransaction(elem.first, elem.second, m_connman);
1045+
RelayTransaction(txid, tx->GetWitnessHash(), m_connman);
10451046
} else {
1046-
m_mempool.RemoveUnbroadcastTx(elem.first, true);
1047+
m_mempool.RemoveUnbroadcastTx(txid, true);
10471048
}
10481049
}
10491050

@@ -1676,8 +1677,9 @@ void RelayTransaction(const uint256& txid, const uint256& wtxid, const CConnman&
16761677
if (!pnode->fMasternode)
16771678
{
16781679
LockAssertion lock(::cs_main);
1679-
CNodeState &state = *State(pnode->GetId());
1680-
if (state.m_wtxid_relay) {
1680+
CNodeState* state = State(pnode->GetId());
1681+
if (state == nullptr) return;
1682+
if (state->m_wtxid_relay) {
16811683
pnode->PushTxInventory(wtxid);
16821684
} else {
16831685
pnode->PushTxInventory(txid);
@@ -3501,8 +3503,6 @@ void PeerManager::ProcessMessage(CNode& pfrom, const std::string& msg_type, CDat
35013503
if (RecursiveDynamicUsage(*ptx) < 100000) {
35023504
AddToCompactExtraTransactions(ptx);
35033505
}
3504-
} else if (tx.HasWitness() && RecursiveDynamicUsage(*ptx) < 100000) {
3505-
AddToCompactExtraTransactions(ptx);
35063506
}
35073507

35083508
if (pfrom.HasPermission(PF_FORCERELAY)) {

src/node/transaction.cpp

+3-3
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

+10-10
Original file line numberDiff line numberDiff line change
@@ -607,10 +607,9 @@ class CTxMemPool
607607
std::vector<indexed_transaction_set::const_iterator> GetSortedDepthAndScore() const EXCLUSIVE_LOCKS_REQUIRED(cs);
608608

609609
/**
610-
* track locally submitted transactions to periodically retry initial broadcast
611-
* map of txid -> wtxid
610+
* Track locally submitted transactions to periodically retry initial broadcast.
612611
*/
613-
std::map<uint256, uint256> m_unbroadcast_txids GUARDED_BY(cs);
612+
std::set<uint256> m_unbroadcast_txids GUARDED_BY(cs);
614613
// SYSCOIN
615614
std::multimap<uint256, uint256> mapProTxRefs; // proTxHash -> transaction (all TXs that refer to an existing proTx)
616615
std::map<CService, uint256> mapProTxAddresses;
@@ -787,19 +786,20 @@ class CTxMemPool
787786
size_t DynamicMemoryUsage() const;
788787

789788
/** Adds a transaction to the unbroadcast set */
790-
void AddUnbroadcastTx(const uint256& txid, const uint256& wtxid) {
789+
void AddUnbroadcastTx(const uint256& txid)
790+
{
791791
LOCK(cs);
792-
// Sanity Check: the transaction should also be in the mempool
793-
if (exists(txid)) {
794-
m_unbroadcast_txids[txid] = wtxid;
795-
}
796-
}
792+
// Sanity check the transaction is in the mempool & insert into
793+
// unbroadcast set.
794+
if (exists(txid)) m_unbroadcast_txids.insert(txid);
795+
};
797796

798797
/** Removes a transaction from the unbroadcast set */
799798
void RemoveUnbroadcastTx(const uint256& txid, const bool unchecked = false);
800799

801800
/** Returns transactions in unbroadcast set */
802-
std::map<uint256, uint256> GetUnbroadcastTxs() const {
801+
std::set<uint256> GetUnbroadcastTxs() const
802+
{
803803
LOCK(cs);
804804
return m_unbroadcast_txids;
805805
}

src/validation.cpp

+6-9
Original file line numberDiff line numberDiff line change
@@ -5470,21 +5470,18 @@ bool LoadMempool(CTxMemPool& pool)
54705470
}
54715471

54725472
// TODO: remove this try except in v0.22
5473-
std::map<uint256, uint256> unbroadcast_txids;
5473+
std::set<uint256> unbroadcast_txids;
54745474
try {
54755475
file >> unbroadcast_txids;
54765476
unbroadcast = unbroadcast_txids.size();
54775477
} catch (const std::exception&) {
54785478
// mempool.dat files created prior to v0.21 will not have an
54795479
// unbroadcast set. No need to log a failure if parsing fails here.
54805480
}
5481-
for (const auto& elem : unbroadcast_txids) {
5482-
// Don't add unbroadcast transactions that didn't get back into the
5483-
// mempool.
5484-
const CTransactionRef& added_tx = pool.get(elem.first);
5485-
if (added_tx != nullptr) {
5486-
pool.AddUnbroadcastTx(elem.first, added_tx->GetWitnessHash());
5487-
}
5481+
for (const auto& txid : unbroadcast_txids) {
5482+
// Ensure transactions were accepted to mempool then add to
5483+
// unbroadcast set.
5484+
if (pool.get(txid) != nullptr) pool.AddUnbroadcastTx(txid);
54885485
}
54895486
} catch (const std::exception& e) {
54905487
LogPrintf("Failed to deserialize mempool data on disk: %s. Continuing anyway.\n", e.what());
@@ -5501,7 +5498,7 @@ bool DumpMempool(const CTxMemPool& pool)
55015498

55025499
std::map<uint256, CAmount> mapDeltas;
55035500
std::vector<TxMempoolInfo> vinfo;
5504-
std::map<uint256, uint256> unbroadcast_txids;
5501+
std::set<uint256> unbroadcast_txids;
55055502

55065503
static Mutex dump_mutex;
55075504
LOCK(dump_mutex);

0 commit comments

Comments
 (0)