Skip to content

Commit 4de0dba

Browse files
committed
[refactor] Move tx relay state to separate structure
1 parent 26a93bc commit 4de0dba

File tree

3 files changed

+95
-89
lines changed

3 files changed

+95
-89
lines changed

src/net.cpp

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -500,8 +500,8 @@ void CNode::copyStats(CNodeStats &stats)
500500
X(addr);
501501
X(addrBind);
502502
{
503-
LOCK(cs_filter);
504-
X(fRelayTxes);
503+
LOCK(m_tx_relay.cs_filter);
504+
stats.fRelayTxes = m_tx_relay.fRelayTxes;
505505
}
506506
X(nLastSend);
507507
X(nLastRecv);
@@ -529,8 +529,8 @@ void CNode::copyStats(CNodeStats &stats)
529529
X(m_legacyWhitelisted);
530530
X(m_permissionFlags);
531531
{
532-
LOCK(cs_feeFilter);
533-
X(minFeeFilter);
532+
LOCK(m_tx_relay.cs_feeFilter);
533+
stats.minFeeFilter = m_tx_relay.minFeeFilter;
534534
}
535535

536536
// It is common for nodes with good ping times to suddenly become lagged,
@@ -818,11 +818,11 @@ bool CConnman::AttemptToEvictConnection()
818818
continue;
819819
if (node->fDisconnect)
820820
continue;
821-
LOCK(node->cs_filter);
821+
LOCK(node->m_tx_relay.cs_filter);
822822
NodeEvictionCandidate candidate = {node->GetId(), node->nTimeConnected, node->nMinPingUsecTime,
823823
node->nLastBlockTime, node->nLastTXTime,
824824
HasAllDesirableServiceFlags(node->nServices),
825-
node->fRelayTxes, node->pfilter != nullptr, node->addr, node->nKeyedNetGroup,
825+
node->m_tx_relay.fRelayTxes, node->m_tx_relay.pfilter != nullptr, node->addr, node->nKeyedNetGroup,
826826
node->m_prefer_evict};
827827
vEvictionCandidates.push_back(candidate);
828828
}
@@ -2625,7 +2625,6 @@ CNode::CNode(NodeId idIn, ServiceFlags nLocalServicesIn, int nMyStartingHeightIn
26252625
fInbound(fInboundIn),
26262626
nKeyedNetGroup(nKeyedNetGroupIn),
26272627
addrKnown(5000, 0.001),
2628-
filterInventoryKnown(50000, 0.000001),
26292628
id(idIn),
26302629
nLocalHostNonce(nLocalHostNonceIn),
26312630
nLocalServices(nLocalServicesIn),
@@ -2634,8 +2633,6 @@ CNode::CNode(NodeId idIn, ServiceFlags nLocalServicesIn, int nMyStartingHeightIn
26342633
hSocket = hSocketIn;
26352634
addrName = addrNameIn == "" ? addr.ToStringIPPort() : addrNameIn;
26362635
hashContinue = uint256();
2637-
filterInventoryKnown.reset();
2638-
pfilter = MakeUnique<CBloomFilter>();
26392636

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

src/net.h

Lines changed: 36 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -676,15 +676,8 @@ class CNode
676676
// Setting fDisconnect to true will cause the node to be disconnected the
677677
// next time DisconnectNodes() runs
678678
std::atomic_bool fDisconnect{false};
679-
// We use fRelayTxes for two purposes -
680-
// a) it allows us to not relay tx invs before receiving the peer's version message
681-
// b) the peer may tell us in its version message that we should not relay tx invs
682-
// unless it loads a bloom filter.
683-
bool fRelayTxes GUARDED_BY(cs_filter){false};
684679
bool fSentAddr{false};
685680
CSemaphoreGrant grantOutbound;
686-
mutable CCriticalSection cs_filter;
687-
std::unique_ptr<CBloomFilter> pfilter PT_GUARDED_BY(cs_filter);
688681
std::atomic<int> nRefCount{0};
689682

690683
const uint64_t nKeyedNetGroup;
@@ -706,24 +699,43 @@ class CNode
706699
int64_t nNextAddrSend GUARDED_BY(cs_sendProcessing){0};
707700
int64_t nNextLocalAddrSend GUARDED_BY(cs_sendProcessing){0};
708701

709-
// inventory based relay
710-
CRollingBloomFilter filterInventoryKnown GUARDED_BY(cs_inventory);
711-
// Set of transaction ids we still have to announce.
712-
// They are sorted by the mempool before relay, so the order is not important.
713-
std::set<uint256> setInventoryTxToSend;
714702
// List of block ids we still have announce.
715703
// There is no final sorting before sending, as they are always sent immediately
716704
// and in the order requested.
717705
std::vector<uint256> vInventoryBlockToSend GUARDED_BY(cs_inventory);
718706
CCriticalSection cs_inventory;
719-
int64_t nNextInvSend{0};
707+
708+
struct TxRelay {
709+
TxRelay() { pfilter = MakeUnique<CBloomFilter>(); }
710+
mutable CCriticalSection cs_filter;
711+
// We use fRelayTxes for two purposes -
712+
// a) it allows us to not relay tx invs before receiving the peer's version message
713+
// b) the peer may tell us in its version message that we should not relay tx invs
714+
// unless it loads a bloom filter.
715+
bool fRelayTxes GUARDED_BY(cs_filter){false};
716+
std::unique_ptr<CBloomFilter> pfilter PT_GUARDED_BY(cs_filter) GUARDED_BY(cs_filter);
717+
718+
mutable CCriticalSection cs_tx_inventory;
719+
CRollingBloomFilter filterInventoryKnown GUARDED_BY(cs_tx_inventory){50000, 0.000001};
720+
// Set of transaction ids we still have to announce.
721+
// They are sorted by the mempool before relay, so the order is not important.
722+
std::set<uint256> setInventoryTxToSend;
723+
// Used for BIP35 mempool sending
724+
bool fSendMempool GUARDED_BY(cs_tx_inventory){false};
725+
// Last time a "MEMPOOL" request was serviced.
726+
std::atomic<int64_t> timeLastMempoolReq{0};
727+
int64_t nNextInvSend{0};
728+
729+
CCriticalSection cs_feeFilter;
730+
// Minimum fee rate with which to filter inv's to this node
731+
CAmount minFeeFilter GUARDED_BY(cs_feeFilter){0};
732+
CAmount lastSentFeeFilter{0};
733+
int64_t nextSendTimeFeeFilter{0};
734+
};
735+
736+
TxRelay m_tx_relay;
720737
// Used for headers announcements - unfiltered blocks to relay
721738
std::vector<uint256> vBlockHashesToAnnounce GUARDED_BY(cs_inventory);
722-
// Used for BIP35 mempool sending
723-
bool fSendMempool GUARDED_BY(cs_inventory){false};
724-
725-
// Last time a "MEMPOOL" request was serviced.
726-
std::atomic<int64_t> timeLastMempoolReq{0};
727739

728740
// Block and TXN accept times
729741
std::atomic<int64_t> nLastBlockTime{0};
@@ -740,11 +752,6 @@ class CNode
740752
std::atomic<int64_t> nMinPingUsecTime{std::numeric_limits<int64_t>::max()};
741753
// Whether a ping is requested.
742754
std::atomic<bool> fPingQueued{false};
743-
// Minimum fee rate with which to filter inv's to this node
744-
CAmount minFeeFilter GUARDED_BY(cs_feeFilter){0};
745-
CCriticalSection cs_feeFilter;
746-
CAmount lastSentFeeFilter{0};
747-
int64_t nextSendTimeFeeFilter{0};
748755

749756
std::set<uint256> orphan_work_set;
750757

@@ -842,19 +849,20 @@ class CNode
842849
void AddInventoryKnown(const CInv& inv)
843850
{
844851
{
845-
LOCK(cs_inventory);
846-
filterInventoryKnown.insert(inv.hash);
852+
LOCK(m_tx_relay.cs_tx_inventory);
853+
m_tx_relay.filterInventoryKnown.insert(inv.hash);
847854
}
848855
}
849856

850857
void PushInventory(const CInv& inv)
851858
{
852-
LOCK(cs_inventory);
853859
if (inv.type == MSG_TX) {
854-
if (!filterInventoryKnown.contains(inv.hash)) {
855-
setInventoryTxToSend.insert(inv.hash);
860+
LOCK(m_tx_relay.cs_tx_inventory);
861+
if (!m_tx_relay.filterInventoryKnown.contains(inv.hash)) {
862+
m_tx_relay.setInventoryTxToSend.insert(inv.hash);
856863
}
857864
} else if (inv.type == MSG_BLOCK) {
865+
LOCK(cs_inventory);
858866
vInventoryBlockToSend.push_back(inv.hash);
859867
}
860868
}

0 commit comments

Comments
 (0)