@@ -51,12 +51,13 @@ struct COrphanTx {
51
51
NodeId fromPeer;
52
52
int64_t nTimeExpire;
53
53
};
54
- std::map<uint256, COrphanTx> mapOrphanTransactions GUARDED_BY (cs_main);
55
- std::map<COutPoint, std::set<std::map<uint256, COrphanTx>::iterator, IteratorComparator>> mapOrphanTransactionsByPrev GUARDED_BY (cs_main);
56
- void EraseOrphansFor (NodeId peer) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
54
+ static CCriticalSection g_cs_orphans;
55
+ std::map<uint256, COrphanTx> mapOrphanTransactions GUARDED_BY (g_cs_orphans);
56
+ std::map<COutPoint, std::set<std::map<uint256, COrphanTx>::iterator, IteratorComparator>> mapOrphanTransactionsByPrev GUARDED_BY (g_cs_orphans);
57
+ void EraseOrphansFor (NodeId peer);
57
58
58
- static size_t vExtraTxnForCompactIt = 0 ;
59
- static std::vector<std::pair<uint256, CTransactionRef>> vExtraTxnForCompact GUARDED_BY (cs_main );
59
+ static size_t vExtraTxnForCompactIt GUARDED_BY (g_cs_orphans) = 0;
60
+ static std::vector<std::pair<uint256, CTransactionRef>> vExtraTxnForCompact GUARDED_BY (g_cs_orphans );
60
61
61
62
static const uint64_t RANDOMIZER_ID_ADDRESS_RELAY = 0x3cac0035b5866b90ULL ; // SHA256("main address relay")[0:8]
62
63
@@ -127,7 +128,7 @@ namespace {
127
128
int g_outbound_peers_with_protect_from_disconnect = 0 ;
128
129
129
130
/* * When our tip was last updated. */
130
- int64_t g_last_tip_update = 0 ;
131
+ std::atomic< int64_t > g_last_tip_update ( 0 ) ;
131
132
132
133
/* * Relay map, protected by cs_main. */
133
134
typedef std::map<uint256, CTransactionRef> MapRelay;
@@ -631,7 +632,7 @@ bool GetNodeStateStats(NodeId nodeid, CNodeStateStats &stats) {
631
632
// mapOrphanTransactions
632
633
//
633
634
634
- void AddToCompactExtraTransactions (const CTransactionRef& tx) EXCLUSIVE_LOCKS_REQUIRED(cs_main )
635
+ void AddToCompactExtraTransactions (const CTransactionRef& tx) EXCLUSIVE_LOCKS_REQUIRED(g_cs_orphans )
635
636
{
636
637
size_t max_extra_txn = gArgs .GetArg (" -blockreconstructionextratxn" , DEFAULT_BLOCK_RECONSTRUCTION_EXTRA_TXN);
637
638
if (max_extra_txn <= 0 )
@@ -642,7 +643,7 @@ void AddToCompactExtraTransactions(const CTransactionRef& tx) EXCLUSIVE_LOCKS_RE
642
643
vExtraTxnForCompactIt = (vExtraTxnForCompactIt + 1 ) % max_extra_txn;
643
644
}
644
645
645
- bool AddOrphanTx (const CTransactionRef& tx, NodeId peer) EXCLUSIVE_LOCKS_REQUIRED(cs_main )
646
+ bool AddOrphanTx (const CTransactionRef& tx, NodeId peer) EXCLUSIVE_LOCKS_REQUIRED(g_cs_orphans )
646
647
{
647
648
const uint256& hash = tx->GetHash ();
648
649
if (mapOrphanTransactions.count (hash))
@@ -675,7 +676,7 @@ bool AddOrphanTx(const CTransactionRef& tx, NodeId peer) EXCLUSIVE_LOCKS_REQUIRE
675
676
return true ;
676
677
}
677
678
678
- int static EraseOrphanTx (uint256 hash) EXCLUSIVE_LOCKS_REQUIRED(cs_main )
679
+ int static EraseOrphanTx (uint256 hash) EXCLUSIVE_LOCKS_REQUIRED(g_cs_orphans )
679
680
{
680
681
std::map<uint256, COrphanTx>::iterator it = mapOrphanTransactions.find (hash);
681
682
if (it == mapOrphanTransactions.end ())
@@ -695,6 +696,7 @@ int static EraseOrphanTx(uint256 hash) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
695
696
696
697
void EraseOrphansFor (NodeId peer)
697
698
{
699
+ LOCK (g_cs_orphans);
698
700
int nErased = 0 ;
699
701
std::map<uint256, COrphanTx>::iterator iter = mapOrphanTransactions.begin ();
700
702
while (iter != mapOrphanTransactions.end ())
@@ -709,8 +711,10 @@ void EraseOrphansFor(NodeId peer)
709
711
}
710
712
711
713
712
- unsigned int LimitOrphanTxSize (unsigned int nMaxOrphans) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
714
+ unsigned int LimitOrphanTxSize (unsigned int nMaxOrphans)
713
715
{
716
+ LOCK (g_cs_orphans);
717
+
714
718
unsigned int nEvicted = 0 ;
715
719
static int64_t nNextSweep;
716
720
int64_t nNow = GetTime ();
@@ -804,7 +808,7 @@ PeerLogicValidation::PeerLogicValidation(CConnman* connmanIn, CScheduler &schedu
804
808
}
805
809
806
810
void PeerLogicValidation::BlockConnected (const std::shared_ptr<const CBlock>& pblock, const CBlockIndex* pindex, const std::vector<CTransactionRef>& vtxConflicted) {
807
- LOCK (cs_main );
811
+ LOCK (g_cs_orphans );
808
812
809
813
std::vector<uint256> vOrphanErase;
810
814
@@ -971,9 +975,13 @@ bool static AlreadyHave(const CInv& inv) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
971
975
recentRejects->reset ();
972
976
}
973
977
978
+ {
979
+ LOCK (g_cs_orphans);
980
+ if (mapOrphanTransactions.count (inv.hash )) return true ;
981
+ }
982
+
974
983
return recentRejects->contains (inv.hash ) ||
975
984
mempool.exists (inv.hash ) ||
976
- mapOrphanTransactions.count (inv.hash ) ||
977
985
pcoinsTip->HaveCoinInCache (COutPoint (inv.hash , 0 )) || // Best effort: only try output 0 and 1
978
986
pcoinsTip->HaveCoinInCache (COutPoint (inv.hash , 1 ));
979
987
}
@@ -2101,7 +2109,7 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
2101
2109
CInv inv (MSG_TX, tx.GetHash ());
2102
2110
pfrom->AddInventoryKnown (inv);
2103
2111
2104
- LOCK (cs_main);
2112
+ LOCK2 (cs_main, g_cs_orphans );
2105
2113
2106
2114
bool fMissingInputs = false ;
2107
2115
CValidationState state;
@@ -2324,7 +2332,7 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
2324
2332
bool fBlockReconstructed = false ;
2325
2333
2326
2334
{
2327
- LOCK (cs_main);
2335
+ LOCK2 (cs_main, g_cs_orphans );
2328
2336
// If AcceptBlockHeader returned true, it set pindex
2329
2337
assert (pindex);
2330
2338
UpdateBlockAvailability (pfrom->GetId (), pindex->GetBlockHash ());
0 commit comments