Skip to content

Commit 224e90d

Browse files
author
MarcoFalke
committed
Merge bitcoin/bitcoin#23336: refactor: Make GenTxid boolean constructor private
fa4ec1c Make GenTxid boolean constructor private (MarcoFalke) faeb9a5 remove unused CTxMemPool::info(const uint256& txid) (MarcoFalke) Pull request description: This boolean argument is either verbose (when used with a named arg) or unintuitive and dangerous (when used as a plain bool). Fix that by making the constructor private. ACKs for top commit: laanwj: Code review ACK fa4ec1c jnewbery: Code review ACK fa4ec1c glozow: code review ACK fa4ec1c Tree-SHA512: bf08ee09168885cfda71e5a01ec412b93964662a90dd9d91e75f7fdf2eaff7c21a95204d0e90b00438bfeab564d0aea66bdb9c0394ee7a05743e65a817159446
2 parents a1d55ce + fa4ec1c commit 224e90d

File tree

9 files changed

+13
-15
lines changed

9 files changed

+13
-15
lines changed

src/net_processing.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3243,7 +3243,7 @@ void PeerManagerImpl::ProcessMessage(CNode& pfrom, const std::string& msg_type,
32433243
// already; and an adversary can already relay us old transactions
32443244
// (older than our recency filter) if trying to DoS us, without any need
32453245
// for witness malleation.
3246-
if (AlreadyHaveTx(GenTxid(/* is_wtxid=*/true, wtxid))) {
3246+
if (AlreadyHaveTx(GenTxid::Wtxid(wtxid))) {
32473247
if (pfrom.HasPermission(NetPermissionFlags::ForceRelay)) {
32483248
// Always relay transactions received from peers with forcerelay
32493249
// permission, even if they were already in the mempool, allowing
@@ -3313,7 +3313,7 @@ void PeerManagerImpl::ProcessMessage(CNode& pfrom, const std::string& msg_type,
33133313
// wtxidrelay peers.
33143314
// Eventually we should replace this with an improved
33153315
// protocol for getting all unconfirmed parents.
3316-
const GenTxid gtxid{/* is_wtxid=*/false, parent_txid};
3316+
const auto gtxid{GenTxid::Txid(parent_txid)};
33173317
pfrom.AddKnownTx(parent_txid);
33183318
if (!AlreadyHaveTx(gtxid)) AddTxAnnouncement(pfrom, gtxid, current_time);
33193319
}

src/primitives/transaction.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -391,8 +391,9 @@ class GenTxid
391391
{
392392
bool m_is_wtxid;
393393
uint256 m_hash;
394-
public:
395394
GenTxid(bool is_wtxid, const uint256& hash) : m_is_wtxid(is_wtxid), m_hash(hash) {}
395+
396+
public:
396397
static GenTxid Txid(const uint256& hash) { return GenTxid{false, hash}; }
397398
static GenTxid Wtxid(const uint256& hash) { return GenTxid{true, hash}; }
398399
bool IsWtxid() const { return m_is_wtxid; }

src/protocol.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,5 +223,5 @@ std::vector<std::string> serviceFlagsToStr(uint64_t flags)
223223
GenTxid ToGenTxid(const CInv& inv)
224224
{
225225
assert(inv.IsGenTxMsg());
226-
return {inv.IsMsgWtx(), inv.hash};
226+
return inv.IsMsgWtx() ? GenTxid::Wtxid(inv.hash) : GenTxid::Txid(inv.hash);
227227
}

src/test/fuzz/txrequest.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ class Tester
204204
}
205205

206206
// Call TxRequestTracker's implementation.
207-
m_tracker.ReceivedInv(peer, GenTxid{is_wtxid, TXHASHES[txhash]}, preferred, reqtime);
207+
m_tracker.ReceivedInv(peer, is_wtxid ? GenTxid::Wtxid(TXHASHES[txhash]) : GenTxid::Txid(TXHASHES[txhash]), preferred, reqtime);
208208
}
209209

210210
void RequestedTx(int peer, int txhash, std::chrono::microseconds exptime)
@@ -252,7 +252,7 @@ class Tester
252252
for (int peer2 = 0; peer2 < MAX_PEERS; ++peer2) {
253253
Announcement& ann2 = m_announcements[txhash][peer2];
254254
if (ann2.m_state == State::REQUESTED && ann2.m_time <= m_now) {
255-
expected_expired.emplace_back(peer2, GenTxid{ann2.m_is_wtxid, TXHASHES[txhash]});
255+
expected_expired.emplace_back(peer2, ann2.m_is_wtxid ? GenTxid::Wtxid(TXHASHES[txhash]) : GenTxid::Txid(TXHASHES[txhash]));
256256
ann2.m_state = State::COMPLETED;
257257
break;
258258
}

src/test/txrequest_tests.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ class Scenario
221221
/** Generate a random GenTxid; the txhash follows NewTxHash; the is_wtxid flag is random. */
222222
GenTxid NewGTxid(const std::vector<std::vector<NodeId>>& orders = {})
223223
{
224-
return {InsecureRandBool(), NewTxHash(orders)};
224+
return InsecureRandBool() ? GenTxid::Wtxid(NewTxHash(orders)) : GenTxid::Txid(NewTxHash(orders));
225225
}
226226

227227
/** Generate a new random NodeId to use as peer. The same NodeId is never returned twice
@@ -494,8 +494,8 @@ void BuildWtxidTest(Scenario& scenario, int config)
494494
auto peerT = scenario.NewPeer();
495495
auto peerW = scenario.NewPeer();
496496
auto txhash = scenario.NewTxHash();
497-
GenTxid txid{false, txhash};
498-
GenTxid wtxid{true, txhash};
497+
auto txid{GenTxid::Txid(txhash)};
498+
auto wtxid{GenTxid::Wtxid(txhash)};
499499

500500
auto reqtimeT = InsecureRandBool() ? MIN_TIME : scenario.Now() + RandomTime8s();
501501
auto reqtimeW = InsecureRandBool() ? MIN_TIME : scenario.Now() + RandomTime8s();

src/txmempool.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -895,8 +895,6 @@ TxMempoolInfo CTxMemPool::info(const GenTxid& gtxid) const
895895
return GetInfo(i);
896896
}
897897

898-
TxMempoolInfo CTxMemPool::info(const uint256& txid) const { return info(GenTxid{false, txid}); }
899-
900898
void CTxMemPool::PrioritiseTransaction(const uint256& hash, const CAmount& nFeeDelta)
901899
{
902900
{

src/txmempool.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -789,7 +789,6 @@ class CTxMemPool
789789
AssertLockHeld(cs);
790790
return mapTx.project<0>(mapTx.get<index_by_wtxid>().find(wtxid));
791791
}
792-
TxMempoolInfo info(const uint256& hash) const;
793792
TxMempoolInfo info(const GenTxid& gtxid) const;
794793
std::vector<TxMempoolInfo> infoAll() const;
795794

src/txrequest.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ std::map<uint256, TxHashInfo> ComputeTxHashInfo(const Index& index, const Priori
300300

301301
GenTxid ToGenTxid(const Announcement& ann)
302302
{
303-
return {ann.m_is_wtxid, ann.m_txhash};
303+
return ann.m_is_wtxid ? GenTxid::Wtxid(ann.m_txhash) : GenTxid::Txid(ann.m_txhash);
304304
}
305305

306306
} // namespace

src/validation.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -585,10 +585,10 @@ bool MemPoolAccept::PreChecks(ATMPArgs& args, Workspace& ws)
585585
if (!CheckFinalTx(m_active_chainstate.m_chain.Tip(), tx, STANDARD_LOCKTIME_VERIFY_FLAGS))
586586
return state.Invalid(TxValidationResult::TX_PREMATURE_SPEND, "non-final");
587587

588-
if (m_pool.exists(GenTxid(true, tx.GetWitnessHash()))) {
588+
if (m_pool.exists(GenTxid::Wtxid(tx.GetWitnessHash()))) {
589589
// Exact transaction already exists in the mempool.
590590
return state.Invalid(TxValidationResult::TX_CONFLICT, "txn-already-in-mempool");
591-
} else if (m_pool.exists(GenTxid(false, tx.GetHash()))) {
591+
} else if (m_pool.exists(GenTxid::Txid(tx.GetHash()))) {
592592
// Transaction with the same non-witness data but different witness (same txid, different
593593
// wtxid) already exists in the mempool.
594594
return state.Invalid(TxValidationResult::TX_CONFLICT, "txn-same-nonwitness-data-in-mempool");

0 commit comments

Comments
 (0)