Skip to content

Commit f48d36c

Browse files
committed
[refactor] move peer (dis)connection logic to TxDownload
The information stored in TxDownloadConnectionInfo isn't used until the next commit.
1 parent f61d9e4 commit f48d36c

File tree

4 files changed

+73
-2
lines changed

4 files changed

+73
-2
lines changed

src/net_processing.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1686,8 +1686,7 @@ void PeerManagerImpl::FinalizeNode(const CNode& node)
16861686
}
16871687
{
16881688
LOCK(m_tx_download_mutex);
1689-
m_txdownloadman.GetOrphanageRef().EraseForPeer(nodeid);
1690-
m_txdownloadman.GetTxRequestRef().DisconnectedPeer(nodeid);
1689+
m_txdownloadman.DisconnectedPeer(nodeid);
16911690
}
16921691
if (m_txreconciliation) m_txreconciliation->ForgetPeer(nodeid);
16931692
m_num_preferred_download_peers -= state->fPreferredDownload;
@@ -3852,6 +3851,16 @@ void PeerManagerImpl::ProcessMessage(CNode& pfrom, const std::string& msg_type,
38523851
tx_relay->m_next_inv_send_time == 0s));
38533852
}
38543853

3854+
{
3855+
LOCK2(::cs_main, m_tx_download_mutex);
3856+
const CNodeState* state = State(pfrom.GetId());
3857+
m_txdownloadman.ConnectedPeer(pfrom.GetId(), node::TxDownloadConnectionInfo {
3858+
.m_preferred = state->fPreferredDownload,
3859+
.m_relay_permissions = pfrom.HasPermission(NetPermissionFlags::Relay),
3860+
.m_wtxid_relay = peer->m_wtxid_relay,
3861+
});
3862+
}
3863+
38553864
pfrom.fSuccessfullyConnected = true;
38563865
return;
38573866
}

src/node/txdownloadman.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
#ifndef BITCOIN_NODE_TXDOWNLOADMAN_H
66
#define BITCOIN_NODE_TXDOWNLOADMAN_H
77

8+
#include <net.h>
9+
810
#include <cstdint>
911
#include <memory>
1012

@@ -21,6 +23,14 @@ struct TxDownloadOptions {
2123
/** Read-only reference to mempool. */
2224
const CTxMemPool& m_mempool;
2325
};
26+
struct TxDownloadConnectionInfo {
27+
/** Whether this peer is preferred for transaction download. */
28+
const bool m_preferred;
29+
/** Whether this peer has Relay permissions. */
30+
const bool m_relay_permissions;
31+
/** Whether this peer supports wtxid relay. */
32+
const bool m_wtxid_relay;
33+
};
2434

2535
/**
2636
* Class responsible for deciding what transactions to request and, once
@@ -68,6 +78,12 @@ class TxDownloadManager {
6878
* - m_recent_confirmed_transactions
6979
* */
7080
bool AlreadyHaveTx(const GenTxid& gtxid, bool include_reconsiderable);
81+
82+
/** Creates a new PeerInfo. Saves the connection info to calculate tx announcement delays later. */
83+
void ConnectedPeer(NodeId nodeid, const TxDownloadConnectionInfo& info);
84+
85+
/** Deletes all txrequest announcements and orphans for a given peer. */
86+
void DisconnectedPeer(NodeId nodeid);
7187
};
7288
} // namespace node
7389
#endif // BITCOIN_NODE_TXDOWNLOADMAN_H

src/node/txdownloadman_impl.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,14 @@ bool TxDownloadManager::AlreadyHaveTx(const GenTxid& gtxid, bool include_reconsi
5050
{
5151
return m_impl->AlreadyHaveTx(gtxid, include_reconsiderable);
5252
}
53+
void TxDownloadManager::ConnectedPeer(NodeId nodeid, const TxDownloadConnectionInfo& info)
54+
{
55+
m_impl->ConnectedPeer(nodeid, info);
56+
}
57+
void TxDownloadManager::DisconnectedPeer(NodeId nodeid)
58+
{
59+
m_impl->DisconnectedPeer(nodeid);
60+
}
5361

5462
// TxDownloadManagerImpl
5563
void TxDownloadManagerImpl::ActiveTipChange()
@@ -113,4 +121,25 @@ bool TxDownloadManagerImpl::AlreadyHaveTx(const GenTxid& gtxid, bool include_rec
113121

114122
return RecentRejectsFilter().contains(hash) || m_opts.m_mempool.exists(gtxid);
115123
}
124+
125+
void TxDownloadManagerImpl::ConnectedPeer(NodeId nodeid, const TxDownloadConnectionInfo& info)
126+
{
127+
// If already connected (shouldn't happen in practice), exit early.
128+
if (m_peer_info.contains(nodeid)) return;
129+
130+
m_peer_info.try_emplace(nodeid, info);
131+
if (info.m_wtxid_relay) m_num_wtxid_peers += 1;
132+
}
133+
134+
void TxDownloadManagerImpl::DisconnectedPeer(NodeId nodeid)
135+
{
136+
m_orphanage.EraseForPeer(nodeid);
137+
m_txrequest.DisconnectedPeer(nodeid);
138+
139+
if (auto it = m_peer_info.find(nodeid); it != m_peer_info.end()) {
140+
if (it->second.m_connection_info.m_wtxid_relay) m_num_wtxid_peers -= 1;
141+
m_peer_info.erase(it);
142+
}
143+
144+
}
116145
} // namespace node

src/node/txdownloadman_impl.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,11 +128,28 @@ class TxDownloadManagerImpl {
128128

129129
TxDownloadManagerImpl(const TxDownloadOptions& options) : m_opts{options} {}
130130

131+
struct PeerInfo {
132+
/** Information relevant to scheduling tx requests. */
133+
const TxDownloadConnectionInfo m_connection_info;
134+
135+
PeerInfo(const TxDownloadConnectionInfo& info) : m_connection_info{info} {}
136+
};
137+
138+
/** Information for all of the peers we may download transactions from. This is not necessarily
139+
* all peers we are connected to (no block-relay-only and temporary connections). */
140+
std::map<NodeId, PeerInfo> m_peer_info;
141+
142+
/** Number of wtxid relay peers we have in m_peer_info. */
143+
uint32_t m_num_wtxid_peers{0};
144+
131145
void ActiveTipChange();
132146
void BlockConnected(const std::shared_ptr<const CBlock>& pblock);
133147
void BlockDisconnected();
134148

135149
bool AlreadyHaveTx(const GenTxid& gtxid, bool include_reconsiderable);
150+
151+
void ConnectedPeer(NodeId nodeid, const TxDownloadConnectionInfo& info);
152+
void DisconnectedPeer(NodeId nodeid);
136153
};
137154
} // namespace node
138155
#endif // BITCOIN_NODE_TXDOWNLOADMAN_IMPL_H

0 commit comments

Comments
 (0)