Skip to content

Commit 9344697

Browse files
committed
Merge bitcoin/bitcoin#21160: net/net processing: Move tx inventory into net_processing
1066d10 scripted-diff: rename TxRelay members (John Newbery) 575bbd0 [net processing] Move tx relay data to Peer (John Newbery) 785f55f [net processing] Move m_wtxid_relay to Peer (John Newbery) 3634670 [net] Add CNode.m_relays_txs and CNode.m_bloom_filter_loaded (John Newbery) Pull request description: This continues the work of moving application layer data into net_processing, by moving all tx data into the new Peer object added in #19607. For motivation, see #19398. ACKs for top commit: dergoegge: ACK 1066d10 - This is a good layer separation improvement with no behavior changes. glozow: utACK 1066d10 Tree-SHA512: 0c9d6b8a0a05e2d816b6d6588b7df133842ec960ae67667813422aa7bd8eb5308599c714f3822a98ddbdf364ffab9050b055079277ba4aff24092557ff99ebcc
2 parents 7c08d81 + 1066d10 commit 9344697

File tree

13 files changed

+188
-220
lines changed

13 files changed

+188
-220
lines changed

src/net.cpp

Lines changed: 5 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -626,12 +626,6 @@ void CNode::CopyStats(CNodeStats& stats)
626626
X(addr);
627627
X(addrBind);
628628
stats.m_network = ConnectedThroughNetwork();
629-
if (m_tx_relay != nullptr) {
630-
LOCK(m_tx_relay->cs_filter);
631-
stats.fRelayTxes = m_tx_relay->fRelayTxes;
632-
} else {
633-
stats.fRelayTxes = false;
634-
}
635629
X(m_last_send);
636630
X(m_last_recv);
637631
X(m_last_tx_time);
@@ -658,11 +652,6 @@ void CNode::CopyStats(CNodeStats& stats)
658652
X(nRecvBytes);
659653
}
660654
X(m_permissionFlags);
661-
if (m_tx_relay != nullptr) {
662-
stats.minFeeFilter = m_tx_relay->minFeeFilter;
663-
} else {
664-
stats.minFeeFilter = 0;
665-
}
666655

667656
X(m_last_ping_time);
668657
X(m_min_ping_time);
@@ -913,15 +902,15 @@ static bool CompareNodeTXTime(const NodeEvictionCandidate &a, const NodeEviction
913902
{
914903
// There is a fall-through here because it is common for a node to have more than a few peers that have not yet relayed txn.
915904
if (a.m_last_tx_time != b.m_last_tx_time) return a.m_last_tx_time < b.m_last_tx_time;
916-
if (a.fRelayTxes != b.fRelayTxes) return b.fRelayTxes;
905+
if (a.m_relay_txs != b.m_relay_txs) return b.m_relay_txs;
917906
if (a.fBloomFilter != b.fBloomFilter) return a.fBloomFilter;
918907
return a.m_connected > b.m_connected;
919908
}
920909

921910
// Pick out the potential block-relay only peers, and sort them by last block time.
922911
static bool CompareNodeBlockRelayOnlyTime(const NodeEvictionCandidate &a, const NodeEvictionCandidate &b)
923912
{
924-
if (a.fRelayTxes != b.fRelayTxes) return a.fRelayTxes;
913+
if (a.m_relay_txs != b.m_relay_txs) return a.m_relay_txs;
925914
if (a.m_last_block_time != b.m_last_block_time) return a.m_last_block_time < b.m_last_block_time;
926915
if (a.fRelevantServices != b.fRelevantServices) return b.fRelevantServices;
927916
return a.m_connected > b.m_connected;
@@ -1046,7 +1035,7 @@ void ProtectEvictionCandidatesByRatio(std::vector<NodeEvictionCandidate>& evicti
10461035
EraseLastKElements(vEvictionCandidates, CompareNodeTXTime, 4);
10471036
// Protect up to 8 non-tx-relay peers that have sent us novel blocks.
10481037
EraseLastKElements(vEvictionCandidates, CompareNodeBlockRelayOnlyTime, 8,
1049-
[](const NodeEvictionCandidate& n) { return !n.fRelayTxes && n.fRelevantServices; });
1038+
[](const NodeEvictionCandidate& n) { return !n.m_relay_txs && n.fRelevantServices; });
10501039

10511040
// Protect 4 nodes that most recently sent us novel blocks.
10521041
// An attacker cannot manipulate this metric without performing useful work.
@@ -1112,18 +1101,11 @@ bool CConnman::AttemptToEvictConnection()
11121101
continue;
11131102
if (node->fDisconnect)
11141103
continue;
1115-
bool peer_relay_txes = false;
1116-
bool peer_filter_not_null = false;
1117-
if (node->m_tx_relay != nullptr) {
1118-
LOCK(node->m_tx_relay->cs_filter);
1119-
peer_relay_txes = node->m_tx_relay->fRelayTxes;
1120-
peer_filter_not_null = node->m_tx_relay->pfilter != nullptr;
1121-
}
11221104
NodeEvictionCandidate candidate = {node->GetId(), node->m_connected, node->m_min_ping_time,
11231105
node->m_last_block_time, node->m_last_tx_time,
11241106
HasAllDesirableServiceFlags(node->nServices),
1125-
peer_relay_txes, peer_filter_not_null, node->nKeyedNetGroup,
1126-
node->m_prefer_evict, node->addr.IsLocal(),
1107+
node->m_relays_txs.load(), node->m_bloom_filter_loaded.load(),
1108+
node->nKeyedNetGroup, node->m_prefer_evict, node->addr.IsLocal(),
11271109
node->ConnectedThroughNetwork()};
11281110
vEvictionCandidates.push_back(candidate);
11291111
}
@@ -3031,9 +3013,6 @@ CNode::CNode(NodeId idIn, ServiceFlags nLocalServicesIn, std::shared_ptr<Sock> s
30313013
nLocalServices(nLocalServicesIn)
30323014
{
30333015
if (inbound_onion) assert(conn_type_in == ConnectionType::INBOUND);
3034-
if (conn_type_in != ConnectionType::BLOCK_RELAY) {
3035-
m_tx_relay = std::make_unique<TxRelay>();
3036-
}
30373016

30383017
for (const std::string &msg : getAllNetMessageTypes())
30393018
mapRecvBytesPerMsgCmd[msg] = 0;

src/net.h

Lines changed: 9 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,6 @@ class CNodeStats
257257
public:
258258
NodeId nodeid;
259259
ServiceFlags nServices;
260-
bool fRelayTxes;
261260
std::chrono::seconds m_last_send;
262261
std::chrono::seconds m_last_recv;
263262
std::chrono::seconds m_last_tx_time;
@@ -278,7 +277,6 @@ class CNodeStats
278277
NetPermissionFlags m_permissionFlags;
279278
std::chrono::microseconds m_last_ping_time;
280279
std::chrono::microseconds m_min_ping_time;
281-
CAmount minFeeFilter;
282280
// Our address, as reported by the peer
283281
std::string addrLocal;
284282
// Address of this peer
@@ -555,34 +553,15 @@ class CNode
555553
// Peer selected us as (compact blocks) high-bandwidth peer (BIP152)
556554
std::atomic<bool> m_bip152_highbandwidth_from{false};
557555

558-
struct TxRelay {
559-
mutable RecursiveMutex cs_filter;
560-
// We use fRelayTxes for two purposes -
561-
// a) it allows us to not relay tx invs before receiving the peer's version message
562-
// b) the peer may tell us in its version message that we should not relay tx invs
563-
// unless it loads a bloom filter.
564-
bool fRelayTxes GUARDED_BY(cs_filter){false};
565-
std::unique_ptr<CBloomFilter> pfilter PT_GUARDED_BY(cs_filter) GUARDED_BY(cs_filter){nullptr};
566-
567-
mutable RecursiveMutex cs_tx_inventory;
568-
CRollingBloomFilter filterInventoryKnown GUARDED_BY(cs_tx_inventory){50000, 0.000001};
569-
// Set of transaction ids we still have to announce.
570-
// They are sorted by the mempool before relay, so the order is not important.
571-
std::set<uint256> setInventoryTxToSend;
572-
// Used for BIP35 mempool sending
573-
bool fSendMempool GUARDED_BY(cs_tx_inventory){false};
574-
// Last time a "MEMPOOL" request was serviced.
575-
std::atomic<std::chrono::seconds> m_last_mempool_req{0s};
576-
std::chrono::microseconds nNextInvSend{0};
577-
578-
/** Minimum fee rate with which to filter inv's to this node */
579-
std::atomic<CAmount> minFeeFilter{0};
580-
CAmount lastSentFeeFilter{0};
581-
std::chrono::microseconds m_next_send_feefilter{0};
582-
};
556+
/** Whether we should relay transactions to this peer (their version
557+
* message did not include fRelay=false and this is not a block-relay-only
558+
* connection). This only changes from false to true. It will never change
559+
* back to false. Used only in inbound eviction logic. */
560+
std::atomic_bool m_relays_txs{false};
583561

584-
// m_tx_relay == nullptr if we're not relaying transactions with this peer
585-
std::unique_ptr<TxRelay> m_tx_relay;
562+
/** Whether this peer has loaded a bloom filter. Used only in inbound
563+
* eviction logic. */
564+
std::atomic_bool m_bloom_filter_loaded{false};
586565

587566
/** UNIX epoch time of the last block received from this peer that we had
588567
* not yet seen (e.g. not already received from another peer), that passed
@@ -658,23 +637,6 @@ class CNode
658637
nRefCount--;
659638
}
660639

661-
void AddKnownTx(const uint256& hash)
662-
{
663-
if (m_tx_relay != nullptr) {
664-
LOCK(m_tx_relay->cs_tx_inventory);
665-
m_tx_relay->filterInventoryKnown.insert(hash);
666-
}
667-
}
668-
669-
void PushTxInventory(const uint256& hash)
670-
{
671-
if (m_tx_relay == nullptr) return;
672-
LOCK(m_tx_relay->cs_tx_inventory);
673-
if (!m_tx_relay->filterInventoryKnown.contains(hash)) {
674-
m_tx_relay->setInventoryTxToSend.insert(hash);
675-
}
676-
}
677-
678640
void CloseSocketDisconnect();
679641

680642
void CopyStats(CNodeStats& stats);
@@ -1308,7 +1270,7 @@ struct NodeEvictionCandidate
13081270
std::chrono::seconds m_last_block_time;
13091271
std::chrono::seconds m_last_tx_time;
13101272
bool fRelevantServices;
1311-
bool fRelayTxes;
1273+
bool m_relay_txs;
13121274
bool fBloomFilter;
13131275
uint64_t nKeyedNetGroup;
13141276
bool prefer_evict;

0 commit comments

Comments
 (0)