Skip to content

Commit 940a499

Browse files
committed
Use type-safe txid types in orphanage
1 parent ed70e65 commit 940a499

File tree

4 files changed

+27
-25
lines changed

4 files changed

+27
-25
lines changed

src/net_processing.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2918,8 +2918,8 @@ bool PeerManagerImpl::ProcessOrphanTx(Peer& peer)
29182918
while (CTransactionRef porphanTx = m_orphanage.GetTxToReconsider(peer.m_id)) {
29192919
const MempoolAcceptResult result = m_chainman.ProcessTransaction(porphanTx);
29202920
const TxValidationState& state = result.m_state;
2921-
const uint256& orphanHash = porphanTx->GetHash();
2922-
const uint256& orphan_wtxid = porphanTx->GetWitnessHash();
2921+
const Txid& orphanHash = porphanTx->GetHash();
2922+
const Wtxid& orphan_wtxid = porphanTx->GetWitnessHash();
29232923

29242924
if (result.m_result_type == MempoolAcceptResult::ResultType::VALID) {
29252925
LogPrint(BCLog::TXPACKAGES, " accepted orphan tx %s (wtxid=%s)\n", orphanHash.ToString(), orphan_wtxid.ToString());

src/test/orphanage_tests.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
44

55
#include <arith_uint256.h>
6+
#include <primitives/transaction.h>
67
#include <pubkey.h>
78
#include <script/sign.h>
89
#include <script/signingprovider.h>
@@ -29,8 +30,8 @@ class TxOrphanageTest : public TxOrphanage
2930
CTransactionRef RandomOrphan() EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
3031
{
3132
LOCK(m_mutex);
32-
std::map<uint256, OrphanTx>::iterator it;
33-
it = m_orphans.lower_bound(InsecureRand256());
33+
std::map<Txid, OrphanTx>::iterator it;
34+
it = m_orphans.lower_bound(Txid::FromUint256(InsecureRand256()));
3435
if (it == m_orphans.end())
3536
it = m_orphans.begin();
3637
return it->second.tx;

src/txorphanage.cpp

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <consensus/validation.h>
88
#include <logging.h>
99
#include <policy/policy.h>
10+
#include <primitives/transaction.h>
1011

1112
#include <cassert>
1213

@@ -20,8 +21,8 @@ bool TxOrphanage::AddTx(const CTransactionRef& tx, NodeId peer)
2021
{
2122
LOCK(m_mutex);
2223

23-
const uint256& hash = tx->GetHash();
24-
const uint256& wtxid = tx->GetWitnessHash();
24+
const Txid& hash = tx->GetHash();
25+
const Wtxid& wtxid = tx->GetWitnessHash();
2526
if (m_orphans.count(hash))
2627
return false;
2728

@@ -53,16 +54,16 @@ bool TxOrphanage::AddTx(const CTransactionRef& tx, NodeId peer)
5354
return true;
5455
}
5556

56-
int TxOrphanage::EraseTx(const uint256& txid)
57+
int TxOrphanage::EraseTx(const Txid& txid)
5758
{
5859
LOCK(m_mutex);
5960
return EraseTxNoLock(txid);
6061
}
6162

62-
int TxOrphanage::EraseTxNoLock(const uint256& txid)
63+
int TxOrphanage::EraseTxNoLock(const Txid& txid)
6364
{
6465
AssertLockHeld(m_mutex);
65-
std::map<uint256, OrphanTx>::iterator it = m_orphans.find(txid);
66+
std::map<Txid, OrphanTx>::iterator it = m_orphans.find(txid);
6667
if (it == m_orphans.end())
6768
return 0;
6869
for (const CTxIn& txin : it->second.tx->vin)
@@ -100,10 +101,10 @@ void TxOrphanage::EraseForPeer(NodeId peer)
100101
m_peer_work_set.erase(peer);
101102

102103
int nErased = 0;
103-
std::map<uint256, OrphanTx>::iterator iter = m_orphans.begin();
104+
std::map<Txid, OrphanTx>::iterator iter = m_orphans.begin();
104105
while (iter != m_orphans.end())
105106
{
106-
std::map<uint256, OrphanTx>::iterator maybeErase = iter++; // increment to avoid iterator becoming invalid
107+
std::map<Txid, OrphanTx>::iterator maybeErase = iter++; // increment to avoid iterator becoming invalid
107108
if (maybeErase->second.fromPeer == peer)
108109
{
109110
nErased += EraseTxNoLock(maybeErase->second.tx->GetHash());
@@ -123,10 +124,10 @@ void TxOrphanage::LimitOrphans(unsigned int max_orphans)
123124
// Sweep out expired orphan pool entries:
124125
int nErased = 0;
125126
int64_t nMinExpTime = nNow + ORPHAN_TX_EXPIRE_TIME - ORPHAN_TX_EXPIRE_INTERVAL;
126-
std::map<uint256, OrphanTx>::iterator iter = m_orphans.begin();
127+
std::map<Txid, OrphanTx>::iterator iter = m_orphans.begin();
127128
while (iter != m_orphans.end())
128129
{
129-
std::map<uint256, OrphanTx>::iterator maybeErase = iter++;
130+
std::map<Txid, OrphanTx>::iterator maybeErase = iter++;
130131
if (maybeErase->second.nTimeExpire <= nNow) {
131132
nErased += EraseTxNoLock(maybeErase->second.tx->GetHash());
132133
} else {
@@ -159,7 +160,7 @@ void TxOrphanage::AddChildrenToWorkSet(const CTransaction& tx)
159160
for (const auto& elem : it_by_prev->second) {
160161
// Get this source peer's work set, emplacing an empty set if it didn't exist
161162
// (note: if this peer wasn't still connected, we would have removed the orphan tx already)
162-
std::set<uint256>& orphan_work_set = m_peer_work_set.try_emplace(elem->second.fromPeer).first->second;
163+
std::set<Txid>& orphan_work_set = m_peer_work_set.try_emplace(elem->second.fromPeer).first->second;
163164
// Add this tx to the work set
164165
orphan_work_set.insert(elem->first);
165166
LogPrint(BCLog::TXPACKAGES, "added %s (wtxid=%s) to peer %d workset\n",
@@ -173,9 +174,9 @@ bool TxOrphanage::HaveTx(const GenTxid& gtxid) const
173174
{
174175
LOCK(m_mutex);
175176
if (gtxid.IsWtxid()) {
176-
return m_wtxid_to_orphan_it.count(gtxid.GetHash());
177+
return m_wtxid_to_orphan_it.count(Wtxid::FromUint256(gtxid.GetHash()));
177178
} else {
178-
return m_orphans.count(gtxid.GetHash());
179+
return m_orphans.count(Txid::FromUint256(gtxid.GetHash()));
179180
}
180181
}
181182

@@ -187,7 +188,7 @@ CTransactionRef TxOrphanage::GetTxToReconsider(NodeId peer)
187188
if (work_set_it != m_peer_work_set.end()) {
188189
auto& work_set = work_set_it->second;
189190
while (!work_set.empty()) {
190-
uint256 txid = *work_set.begin();
191+
Txid txid = *work_set.begin();
191192
work_set.erase(work_set.begin());
192193

193194
const auto orphan_it = m_orphans.find(txid);
@@ -215,7 +216,7 @@ void TxOrphanage::EraseForBlock(const CBlock& block)
215216
{
216217
LOCK(m_mutex);
217218

218-
std::vector<uint256> vOrphanErase;
219+
std::vector<Txid> vOrphanErase;
219220

220221
for (const CTransactionRef& ptx : block.vtx) {
221222
const CTransaction& tx = *ptx;
@@ -226,7 +227,7 @@ void TxOrphanage::EraseForBlock(const CBlock& block)
226227
if (itByPrev == m_outpoint_to_orphan_it.end()) continue;
227228
for (auto mi = itByPrev->second.begin(); mi != itByPrev->second.end(); ++mi) {
228229
const CTransaction& orphanTx = *(*mi)->second.tx;
229-
const uint256& orphanHash = orphanTx.GetHash();
230+
const auto& orphanHash = orphanTx.GetHash();
230231
vOrphanErase.push_back(orphanHash);
231232
}
232233
}
@@ -235,7 +236,7 @@ void TxOrphanage::EraseForBlock(const CBlock& block)
235236
// Erase orphan transactions included or precluded by this block
236237
if (vOrphanErase.size()) {
237238
int nErased = 0;
238-
for (const uint256& orphanHash : vOrphanErase) {
239+
for (const auto& orphanHash : vOrphanErase) {
239240
nErased += EraseTxNoLock(orphanHash);
240241
}
241242
LogPrint(BCLog::TXPACKAGES, "Erased %d orphan tx included or conflicted by block\n", nErased);

src/txorphanage.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class TxOrphanage {
3434
CTransactionRef GetTxToReconsider(NodeId peer) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex);
3535

3636
/** Erase an orphan by txid */
37-
int EraseTx(const uint256& txid) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex);
37+
int EraseTx(const Txid& txid) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex);
3838

3939
/** Erase all orphans announced by a peer (eg, after that peer disconnects) */
4040
void EraseForPeer(NodeId peer) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex);
@@ -71,10 +71,10 @@ class TxOrphanage {
7171

7272
/** Map from txid to orphan transaction record. Limited by
7373
* -maxorphantx/DEFAULT_MAX_ORPHAN_TRANSACTIONS */
74-
std::map<uint256, OrphanTx> m_orphans GUARDED_BY(m_mutex);
74+
std::map<Txid, OrphanTx> m_orphans GUARDED_BY(m_mutex);
7575

7676
/** Which peer provided the orphans that need to be reconsidered */
77-
std::map<NodeId, std::set<uint256>> m_peer_work_set GUARDED_BY(m_mutex);
77+
std::map<NodeId, std::set<Txid>> m_peer_work_set GUARDED_BY(m_mutex);
7878

7979
using OrphanMap = decltype(m_orphans);
8080

@@ -96,10 +96,10 @@ class TxOrphanage {
9696

9797
/** Index from wtxid into the m_orphans to lookup orphan
9898
* transactions using their witness ids. */
99-
std::map<uint256, OrphanMap::iterator> m_wtxid_to_orphan_it GUARDED_BY(m_mutex);
99+
std::map<Wtxid, OrphanMap::iterator> m_wtxid_to_orphan_it GUARDED_BY(m_mutex);
100100

101101
/** Erase an orphan by txid */
102-
int EraseTxNoLock(const uint256& txid) EXCLUSIVE_LOCKS_REQUIRED(m_mutex);
102+
int EraseTxNoLock(const Txid& txid) EXCLUSIVE_LOCKS_REQUIRED(m_mutex);
103103
};
104104

105105
#endif // BITCOIN_TXORPHANAGE_H

0 commit comments

Comments
 (0)