Skip to content

Commit 785f55f

Browse files
committed
[net processing] Move m_wtxid_relay to Peer
Also, remove cs_main guard from m_wtxid_relay_peers and make it atomic. This should be fine since we don't need m_wtxid_relay_peers to be synchronized with m_wtxid_relay exactly at all times. After this change, RelayTransaction no longer requires cs_main.
1 parent 3634670 commit 785f55f

File tree

1 file changed

+35
-41
lines changed

1 file changed

+35
-41
lines changed

src/net_processing.cpp

Lines changed: 35 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,9 @@ struct Peer {
232232
/** Whether a ping has been requested by the user */
233233
std::atomic<bool> m_ping_queued{false};
234234

235+
/** Whether this peer relays txs via wtxid */
236+
std::atomic<bool> m_wtxid_relay{false};
237+
235238
/** A vector of addresses to send to the peer, limited to MAX_ADDR_TO_SEND. */
236239
std::vector<CAddress> m_addrs_to_send;
237240
/** Probabilistic filter to track recent addr messages relayed with this
@@ -331,9 +334,6 @@ class PeerManagerImpl final : public PeerManager
331334
const std::chrono::microseconds time_received, const std::atomic<bool>& interruptMsgProc) override;
332335

333336
private:
334-
void _RelayTransaction(const uint256& txid, const uint256& wtxid)
335-
EXCLUSIVE_LOCKS_REQUIRED(cs_main);
336-
337337
/** Consider evicting an outbound peer based on the amount of time they've been behind our tip */
338338
void ConsiderEviction(CNode& pto, std::chrono::seconds time_in_seconds) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
339339

@@ -464,7 +464,7 @@ class PeerManagerImpl final : public PeerManager
464464
std::map<uint256, std::pair<NodeId, bool>> mapBlockSource GUARDED_BY(cs_main);
465465

466466
/** Number of peers with wtxid relay. */
467-
int m_wtxid_relay_peers GUARDED_BY(cs_main) = 0;
467+
std::atomic<int> m_wtxid_relay_peers{0};
468468

469469
/** Number of outbound peers with m_chain_sync.m_protect. */
470470
int m_outbound_peers_with_protect_from_disconnect GUARDED_BY(cs_main) = 0;
@@ -779,9 +779,6 @@ struct CNodeState {
779779
//! A rolling bloom filter of all announced tx CInvs to this peer.
780780
CRollingBloomFilter m_recently_announced_invs = CRollingBloomFilter{INVENTORY_MAX_RECENT_RELAY, 0.000001};
781781

782-
//! Whether this peer relays txs via wtxid
783-
bool m_wtxid_relay{false};
784-
785782
CNodeState(bool is_inbound) : m_is_inbound(is_inbound) {}
786783
};
787784

@@ -1211,8 +1208,7 @@ void PeerManagerImpl::ReattemptInitialBroadcast(CScheduler& scheduler)
12111208
CTransactionRef tx = m_mempool.get(txid);
12121209

12131210
if (tx != nullptr) {
1214-
LOCK(cs_main);
1215-
_RelayTransaction(txid, tx->GetWitnessHash());
1211+
RelayTransaction(txid, tx->GetWitnessHash());
12161212
} else {
12171213
m_mempool.RemoveUnbroadcastTx(txid, true);
12181214
}
@@ -1239,6 +1235,8 @@ void PeerManagerImpl::FinalizeNode(const CNode& node)
12391235
PeerRef peer = RemovePeer(nodeid);
12401236
assert(peer != nullptr);
12411237
misbehavior = WITH_LOCK(peer->m_misbehavior_mutex, return peer->m_misbehavior_score);
1238+
m_wtxid_relay_peers -= peer->m_wtxid_relay;
1239+
assert(m_wtxid_relay_peers >= 0);
12421240
}
12431241
CNodeState *state = State(nodeid);
12441242
assert(state != nullptr);
@@ -1256,8 +1254,6 @@ void PeerManagerImpl::FinalizeNode(const CNode& node)
12561254
assert(m_peers_downloading_from >= 0);
12571255
m_outbound_peers_with_protect_from_disconnect -= state->m_chain_sync.m_protect;
12581256
assert(m_outbound_peers_with_protect_from_disconnect >= 0);
1259-
m_wtxid_relay_peers -= state->m_wtxid_relay;
1260-
assert(m_wtxid_relay_peers >= 0);
12611257

12621258
mapNodeState.erase(nodeid);
12631259

@@ -1742,21 +1738,22 @@ void PeerManagerImpl::SendPings()
17421738

17431739
void PeerManagerImpl::RelayTransaction(const uint256& txid, const uint256& wtxid)
17441740
{
1745-
WITH_LOCK(cs_main, _RelayTransaction(txid, wtxid););
1746-
}
1741+
std::map<const NodeId, const uint256&> relay_peers;
1742+
{
1743+
// Don't hold m_peer_mutex while calling ForEachNode() to avoid an
1744+
// m_peer_mutex/cs_vNodes lock inversion. During shutdown, FinalizeNode()
1745+
// is called while holding cs_vNodes.
1746+
LOCK(m_peer_mutex);
1747+
for (auto& it : m_peer_map) {
1748+
relay_peers.emplace(it.first, it.second->m_wtxid_relay ? wtxid : txid);
1749+
}
1750+
}
17471751

1748-
void PeerManagerImpl::_RelayTransaction(const uint256& txid, const uint256& wtxid)
1749-
{
1750-
m_connman.ForEachNode([&txid, &wtxid](CNode* pnode) EXCLUSIVE_LOCKS_REQUIRED(::cs_main) {
1751-
AssertLockHeld(::cs_main);
1752+
m_connman.ForEachNode([&relay_peers](CNode* node) {
1753+
auto it = relay_peers.find(node->GetId());
1754+
if (it == relay_peers.end()) return; // Should never happen
17521755

1753-
CNodeState* state = State(pnode->GetId());
1754-
if (state == nullptr) return;
1755-
if (state->m_wtxid_relay) {
1756-
pnode->PushTxInventory(wtxid);
1757-
} else {
1758-
pnode->PushTxInventory(txid);
1759-
}
1756+
node->PushTxInventory(it->second);
17601757
});
17611758
}
17621759

@@ -2317,7 +2314,7 @@ void PeerManagerImpl::ProcessOrphanTx(std::set<uint256>& orphan_work_set)
23172314

23182315
if (result.m_result_type == MempoolAcceptResult::ResultType::VALID) {
23192316
LogPrint(BCLog::MEMPOOL, " accepted orphan tx %s\n", orphanHash.ToString());
2320-
_RelayTransaction(orphanHash, porphanTx->GetWitnessHash());
2317+
RelayTransaction(orphanHash, porphanTx->GetWitnessHash());
23212318
m_orphanage.AddChildrenToWorkSet(*porphanTx, orphan_work_set);
23222319
m_orphanage.EraseTx(orphanHash);
23232320
for (const CTransactionRef& removedTx : result.m_replaced_transactions.value()) {
@@ -2864,9 +2861,8 @@ void PeerManagerImpl::ProcessMessage(CNode& pfrom, const std::string& msg_type,
28642861
return;
28652862
}
28662863
if (pfrom.GetCommonVersion() >= WTXID_RELAY_VERSION) {
2867-
LOCK(cs_main);
2868-
if (!State(pfrom.GetId())->m_wtxid_relay) {
2869-
State(pfrom.GetId())->m_wtxid_relay = true;
2864+
if (!peer->m_wtxid_relay) {
2865+
peer->m_wtxid_relay = true;
28702866
m_wtxid_relay_peers++;
28712867
} else {
28722868
LogPrint(BCLog::NET, "ignoring duplicate wtxidrelay from peer=%d\n", pfrom.GetId());
@@ -3020,7 +3016,7 @@ void PeerManagerImpl::ProcessMessage(CNode& pfrom, const std::string& msg_type,
30203016
// Ignore INVs that don't match wtxidrelay setting.
30213017
// Note that orphan parent fetching always uses MSG_TX GETDATAs regardless of the wtxidrelay setting.
30223018
// This is fine as no INV messages are involved in that process.
3023-
if (State(pfrom.GetId())->m_wtxid_relay) {
3019+
if (peer->m_wtxid_relay) {
30243020
if (inv.IsMsgTx()) continue;
30253021
} else {
30263022
if (inv.IsMsgWtx()) continue;
@@ -3298,13 +3294,9 @@ void PeerManagerImpl::ProcessMessage(CNode& pfrom, const std::string& msg_type,
32983294
const uint256& txid = ptx->GetHash();
32993295
const uint256& wtxid = ptx->GetWitnessHash();
33003296

3301-
LOCK2(cs_main, g_cs_orphans);
3302-
3303-
CNodeState* nodestate = State(pfrom.GetId());
3304-
3305-
const uint256& hash = nodestate->m_wtxid_relay ? wtxid : txid;
3297+
const uint256& hash = peer->m_wtxid_relay ? wtxid : txid;
33063298
pfrom.AddKnownTx(hash);
3307-
if (nodestate->m_wtxid_relay && txid != wtxid) {
3299+
if (peer->m_wtxid_relay && txid != wtxid) {
33083300
// Insert txid into filterInventoryKnown, even for
33093301
// wtxidrelay peers. This prevents re-adding of
33103302
// unconfirmed parents to the recently_announced
@@ -3313,6 +3305,8 @@ void PeerManagerImpl::ProcessMessage(CNode& pfrom, const std::string& msg_type,
33133305
pfrom.AddKnownTx(txid);
33143306
}
33153307

3308+
LOCK2(cs_main, g_cs_orphans);
3309+
33163310
m_txrequest.ReceivedResponse(pfrom.GetId(), txid);
33173311
if (tx.HasWitness()) m_txrequest.ReceivedResponse(pfrom.GetId(), wtxid);
33183312

@@ -3337,7 +3331,7 @@ void PeerManagerImpl::ProcessMessage(CNode& pfrom, const std::string& msg_type,
33373331
LogPrintf("Not relaying non-mempool transaction %s from forcerelay peer=%d\n", tx.GetHash().ToString(), pfrom.GetId());
33383332
} else {
33393333
LogPrintf("Force relaying tx %s from peer=%d\n", tx.GetHash().ToString(), pfrom.GetId());
3340-
_RelayTransaction(tx.GetHash(), tx.GetWitnessHash());
3334+
RelayTransaction(tx.GetHash(), tx.GetWitnessHash());
33413335
}
33423336
}
33433337
return;
@@ -3351,7 +3345,7 @@ void PeerManagerImpl::ProcessMessage(CNode& pfrom, const std::string& msg_type,
33513345
// requests for it.
33523346
m_txrequest.ForgetTxHash(tx.GetHash());
33533347
m_txrequest.ForgetTxHash(tx.GetWitnessHash());
3354-
_RelayTransaction(tx.GetHash(), tx.GetWitnessHash());
3348+
RelayTransaction(tx.GetHash(), tx.GetWitnessHash());
33553349
m_orphanage.AddChildrenToWorkSet(tx, peer->m_orphan_work_set);
33563350

33573351
pfrom.m_last_tx_time = GetTime<std::chrono::seconds>();
@@ -4841,8 +4835,8 @@ bool PeerManagerImpl::SendMessages(CNode* pto)
48414835
LOCK(pto->m_tx_relay->cs_filter);
48424836

48434837
for (const auto& txinfo : vtxinfo) {
4844-
const uint256& hash = state.m_wtxid_relay ? txinfo.tx->GetWitnessHash() : txinfo.tx->GetHash();
4845-
CInv inv(state.m_wtxid_relay ? MSG_WTX : MSG_TX, hash);
4838+
const uint256& hash = peer->m_wtxid_relay ? txinfo.tx->GetWitnessHash() : txinfo.tx->GetHash();
4839+
CInv inv(peer->m_wtxid_relay ? MSG_WTX : MSG_TX, hash);
48464840
pto->m_tx_relay->setInventoryTxToSend.erase(hash);
48474841
// Don't send transactions that peers will not put into their mempool
48484842
if (txinfo.fee < filterrate.GetFee(txinfo.vsize)) {
@@ -4873,7 +4867,7 @@ bool PeerManagerImpl::SendMessages(CNode* pto)
48734867
const CFeeRate filterrate{pto->m_tx_relay->minFeeFilter.load()};
48744868
// Topologically and fee-rate sort the inventory we send for privacy and priority reasons.
48754869
// A heap is used so that not all items need sorting if only a few are being sent.
4876-
CompareInvMempoolOrder compareInvMempoolOrder(&m_mempool, state.m_wtxid_relay);
4870+
CompareInvMempoolOrder compareInvMempoolOrder(&m_mempool, peer->m_wtxid_relay);
48774871
std::make_heap(vInvTx.begin(), vInvTx.end(), compareInvMempoolOrder);
48784872
// No reason to drain out at many times the network's capacity,
48794873
// especially since we have many peers and some will draw much shorter delays.
@@ -4885,7 +4879,7 @@ bool PeerManagerImpl::SendMessages(CNode* pto)
48854879
std::set<uint256>::iterator it = vInvTx.back();
48864880
vInvTx.pop_back();
48874881
uint256 hash = *it;
4888-
CInv inv(state.m_wtxid_relay ? MSG_WTX : MSG_TX, hash);
4882+
CInv inv(peer->m_wtxid_relay ? MSG_WTX : MSG_TX, hash);
48894883
// Remove it from the to-be-sent set
48904884
pto->m_tx_relay->setInventoryTxToSend.erase(it);
48914885
// Check if not in the filter already

0 commit comments

Comments
 (0)