Skip to content

Commit eee473d

Browse files
Convert CompareInvMempoolOrder to GenTxidVariant
Now that we are storing `CTxMemPool::CompareDepthAndScore` parameters using `std::variant` we have no portable zero-overhead way of accessing them, so use `std::visit` and drop `bool wtxid` in-parameter. Co-authored-by: stickies-v <[email protected]>
1 parent 243553d commit eee473d

File tree

5 files changed

+30
-33
lines changed

5 files changed

+30
-33
lines changed

src/net_processing.cpp

Lines changed: 22 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ struct Peer {
302302
* non-wtxid-relay peers, wtxid for wtxid-relay peers). We use the
303303
* mempool to sort transactions in dependency order before relay, so
304304
* this does not have to be sorted. */
305-
std::set<uint256> m_tx_inventory_to_send GUARDED_BY(m_tx_inventory_mutex);
305+
std::set<GenTxidVariant> m_tx_inventory_to_send GUARDED_BY(m_tx_inventory_mutex);
306306
/** Whether the peer has requested us to send our complete mempool. Only
307307
* permitted if the peer has NetPermissionFlags::Mempool or we advertise
308308
* NODE_BLOOM. See BIP35. */
@@ -536,7 +536,7 @@ class PeerManagerImpl final : public PeerManager
536536
std::vector<TxOrphanage::OrphanTxBase> GetOrphanTransactions() override EXCLUSIVE_LOCKS_REQUIRED(!m_tx_download_mutex);
537537
PeerManagerInfo GetInfo() const override EXCLUSIVE_LOCKS_REQUIRED(!m_peer_mutex);
538538
void SendPings() override EXCLUSIVE_LOCKS_REQUIRED(!m_peer_mutex);
539-
void RelayTransaction(const uint256& txid, const uint256& wtxid) override EXCLUSIVE_LOCKS_REQUIRED(!m_peer_mutex);
539+
void RelayTransaction(const Txid& txid, const Wtxid& wtxid) override EXCLUSIVE_LOCKS_REQUIRED(!m_peer_mutex);
540540
void SetBestBlock(int height, std::chrono::seconds time) override
541541
{
542542
m_best_height = height;
@@ -1583,7 +1583,7 @@ void PeerManagerImpl::ReattemptInitialBroadcast(CScheduler& scheduler)
15831583
CTransactionRef tx = m_mempool.get(txid);
15841584

15851585
if (tx != nullptr) {
1586-
RelayTransaction(txid, tx->GetWitnessHash());
1586+
RelayTransaction(Txid::FromUint256(txid), tx->GetWitnessHash());
15871587
} else {
15881588
m_mempool.RemoveUnbroadcastTx(txid, true);
15891589
}
@@ -2150,7 +2150,7 @@ void PeerManagerImpl::SendPings()
21502150
for(auto& it : m_peer_map) it.second->m_ping_queued = true;
21512151
}
21522152

2153-
void PeerManagerImpl::RelayTransaction(const uint256& txid, const uint256& wtxid)
2153+
void PeerManagerImpl::RelayTransaction(const Txid& txid, const Wtxid& wtxid)
21542154
{
21552155
LOCK(m_peer_mutex);
21562156
for(auto& it : m_peer_map) {
@@ -2166,11 +2166,11 @@ void PeerManagerImpl::RelayTransaction(const uint256& txid, const uint256& wtxid
21662166
// in the announcement.
21672167
if (tx_relay->m_next_inv_send_time == 0s) continue;
21682168

2169-
const uint256& hash{peer.m_wtxid_relay ? wtxid : txid};
2170-
if (!tx_relay->m_tx_inventory_known_filter.contains(hash)) {
2171-
tx_relay->m_tx_inventory_to_send.insert(hash);
2169+
const auto gtxid{peer.m_wtxid_relay ? GenTxidVariant{wtxid} : GenTxidVariant{txid}};
2170+
if (!tx_relay->m_tx_inventory_known_filter.contains(gtxid.ToUint256())) {
2171+
tx_relay->m_tx_inventory_to_send.insert(gtxid);
21722172
}
2173-
};
2173+
}
21742174
}
21752175

21762176
void PeerManagerImpl::RelayAddress(NodeId originator,
@@ -5442,20 +5442,15 @@ void PeerManagerImpl::MaybeSendFeefilter(CNode& pto, Peer& peer, std::chrono::mi
54425442
namespace {
54435443
class CompareInvMempoolOrder
54445444
{
5445-
CTxMemPool* mp;
5446-
bool m_wtxid_relay;
5445+
const CTxMemPool* m_mempool;
54475446
public:
5448-
explicit CompareInvMempoolOrder(CTxMemPool *_mempool, bool use_wtxid)
5449-
{
5450-
mp = _mempool;
5451-
m_wtxid_relay = use_wtxid;
5452-
}
5447+
explicit CompareInvMempoolOrder(CTxMemPool* mempool) : m_mempool{mempool} {}
54535448

5454-
bool operator()(std::set<uint256>::iterator a, std::set<uint256>::iterator b)
5449+
bool operator()(std::set<GenTxidVariant>::iterator a, std::set<GenTxidVariant>::iterator b)
54555450
{
54565451
/* As std::make_heap produces a max-heap, we want the entries with the
54575452
* fewest ancestors/highest fee to sort later. */
5458-
return mp->CompareDepthAndScore(*b, *a, m_wtxid_relay);
5453+
return m_mempool->CompareDepthAndScore(*b, *a);
54595454
}
54605455
};
54615456
} // namespace
@@ -5773,7 +5768,7 @@ bool PeerManagerImpl::SendMessages(CNode* pto)
57735768
txinfo.tx->GetWitnessHash().ToUint256() :
57745769
txinfo.tx->GetHash().ToUint256(),
57755770
};
5776-
tx_relay->m_tx_inventory_to_send.erase(inv.hash);
5771+
tx_relay->m_tx_inventory_to_send.erase(ToGenTxid(inv).ToVariant());
57775772

57785773
// Don't send transactions that peers will not put into their mempool
57795774
if (txinfo.fee < filterrate.GetFee(txinfo.vsize)) {
@@ -5794,15 +5789,15 @@ bool PeerManagerImpl::SendMessages(CNode* pto)
57945789
// Determine transactions to relay
57955790
if (fSendTrickle) {
57965791
// Produce a vector with all candidates for sending
5797-
std::vector<std::set<uint256>::iterator> vInvTx;
5792+
std::vector<std::set<GenTxidVariant>::iterator> vInvTx;
57985793
vInvTx.reserve(tx_relay->m_tx_inventory_to_send.size());
5799-
for (std::set<uint256>::iterator it = tx_relay->m_tx_inventory_to_send.begin(); it != tx_relay->m_tx_inventory_to_send.end(); it++) {
5794+
for (std::set<GenTxidVariant>::iterator it = tx_relay->m_tx_inventory_to_send.begin(); it != tx_relay->m_tx_inventory_to_send.end(); it++) {
58005795
vInvTx.push_back(it);
58015796
}
58025797
const CFeeRate filterrate{tx_relay->m_fee_filter_received.load()};
58035798
// Topologically and fee-rate sort the inventory we send for privacy and priority reasons.
58045799
// A heap is used so that not all items need sorting if only a few are being sent.
5805-
CompareInvMempoolOrder compareInvMempoolOrder(&m_mempool, peer->m_wtxid_relay);
5800+
CompareInvMempoolOrder compareInvMempoolOrder(&m_mempool);
58065801
std::make_heap(vInvTx.begin(), vInvTx.end(), compareInvMempoolOrder);
58075802
// No reason to drain out at many times the network's capacity,
58085803
// especially since we have many peers and some will draw much shorter delays.
@@ -5813,14 +5808,15 @@ bool PeerManagerImpl::SendMessages(CNode* pto)
58135808
while (!vInvTx.empty() && nRelayedTransactions < broadcast_max) {
58145809
// Fetch the top element from the heap
58155810
std::pop_heap(vInvTx.begin(), vInvTx.end(), compareInvMempoolOrder);
5816-
std::set<uint256>::iterator it = vInvTx.back();
5811+
std::set<GenTxidVariant>::iterator it = vInvTx.back();
58175812
vInvTx.pop_back();
5818-
uint256 hash = *it;
5819-
CInv inv(peer->m_wtxid_relay ? MSG_WTX : MSG_TX, hash);
5813+
GenTxidVariant hash = *it;
5814+
Assume(peer->m_wtxid_relay == hash.IsWtxid());
5815+
CInv inv(peer->m_wtxid_relay ? MSG_WTX : MSG_TX, hash.ToUint256());
58205816
// Remove it from the to-be-sent set
58215817
tx_relay->m_tx_inventory_to_send.erase(it);
58225818
// Check if not in the filter already
5823-
if (tx_relay->m_tx_inventory_known_filter.contains(hash)) {
5819+
if (tx_relay->m_tx_inventory_known_filter.contains(hash.ToUint256())) {
58245820
continue;
58255821
}
58265822
// Not in the mempool anymore? don't bother sending it.
@@ -5840,7 +5836,7 @@ bool PeerManagerImpl::SendMessages(CNode* pto)
58405836
MakeAndPushMessage(*pto, NetMsgType::INV, vInv);
58415837
vInv.clear();
58425838
}
5843-
tx_relay->m_tx_inventory_known_filter.insert(hash);
5839+
tx_relay->m_tx_inventory_known_filter.insert(hash.ToUint256());
58445840
}
58455841

58465842
// Ensure we'll respond to GETDATA requests for anything we've just announced

src/net_processing.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ class PeerManager : public CValidationInterface, public NetEventsInterface
119119
virtual PeerManagerInfo GetInfo() const = 0;
120120

121121
/** Relay transaction to all peers. */
122-
virtual void RelayTransaction(const uint256& txid, const uint256& wtxid) = 0;
122+
virtual void RelayTransaction(const Txid& txid, const Wtxid& wtxid) = 0;
123123

124124
/** Send ping message to all peers */
125125
virtual void SendPings() = 0;

src/node/transaction.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ TransactionError BroadcastTransaction(NodeContext& node, const CTransactionRef t
4242

4343
std::promise<void> promise;
4444
Txid txid = tx->GetHash();
45-
uint256 wtxid = tx->GetWitnessHash();
45+
Wtxid wtxid = tx->GetWitnessHash();
4646
bool callback_set = false;
4747

4848
{

src/txmempool.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -794,17 +794,17 @@ void CTxMemPool::check(const CCoinsViewCache& active_coins_tip, int64_t spendhei
794794
assert(innerUsage == cachedInnerUsage);
795795
}
796796

797-
bool CTxMemPool::CompareDepthAndScore(const uint256& hasha, const uint256& hashb, bool wtxid)
797+
bool CTxMemPool::CompareDepthAndScore(const GenTxidVariant& hasha, const GenTxidVariant& hashb) const
798798
{
799799
/* Return `true` if hasha should be considered sooner than hashb. Namely when:
800800
* a is not in the mempool, but b is
801801
* both are in the mempool and a has fewer ancestors than b
802802
* both are in the mempool and a has a higher score than b
803803
*/
804804
LOCK(cs);
805-
auto j = wtxid ? GetIter(Wtxid::FromUint256(hashb)) : GetIter(Txid::FromUint256(hashb));
805+
auto j{std::visit([&](const auto& id) EXCLUSIVE_LOCKS_REQUIRED(cs) { return GetIter(id); }, hashb)};
806806
if (!j.has_value()) return false;
807-
auto i = wtxid ? GetIter(Wtxid::FromUint256(hasha)) : GetIter(Txid::FromUint256(hasha));
807+
auto i{std::visit([&](const auto& id) EXCLUSIVE_LOCKS_REQUIRED(cs) { return GetIter(id); }, hasha)};
808808
if (!i.has_value()) return true;
809809
uint64_t counta = i.value()->GetCountWithAncestors();
810810
uint64_t countb = j.value()->GetCountWithAncestors();

src/txmempool.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,10 @@
1919
#include <primitives/transaction.h>
2020
#include <sync.h>
2121
#include <util/epochguard.h>
22+
#include <util/feefrac.h>
2223
#include <util/hasher.h>
2324
#include <util/result.h>
24-
#include <util/feefrac.h>
25+
#include <util/transaction_identifier.h>
2526

2627
#include <boost/multi_index/hashed_index.hpp>
2728
#include <boost/multi_index/identity.hpp>
@@ -466,7 +467,7 @@ class CTxMemPool
466467
void removeConflicts(const CTransaction& tx) EXCLUSIVE_LOCKS_REQUIRED(cs);
467468
void removeForBlock(const std::vector<CTransactionRef>& vtx, unsigned int nBlockHeight) EXCLUSIVE_LOCKS_REQUIRED(cs);
468469

469-
bool CompareDepthAndScore(const uint256& hasha, const uint256& hashb, bool wtxid=false);
470+
bool CompareDepthAndScore(const GenTxidVariant& hasha, const GenTxidVariant& hashb) const;
470471
bool isSpent(const COutPoint& outpoint) const;
471472
unsigned int GetTransactionsUpdated() const;
472473
void AddTransactionsUpdated(unsigned int n);

0 commit comments

Comments
 (0)