@ -51,12 +51,13 @@ struct COrphanTx {
@@ -51,12 +51,13 @@ struct COrphanTx {
NodeId fromPeer ;
int64_t nTimeExpire ;
} ;
std : : map < uint256 , COrphanTx > mapOrphanTransactions GUARDED_BY ( cs_main ) ;
std : : map < COutPoint , std : : set < std : : map < uint256 , COrphanTx > : : iterator , IteratorComparator > > mapOrphanTransactionsByPrev GUARDED_BY ( cs_main ) ;
void EraseOrphansFor ( NodeId peer ) EXCLUSIVE_LOCKS_REQUIRED ( cs_main ) ;
static CCriticalSection g_cs_orphans ;
std : : map < uint256 , COrphanTx > mapOrphanTransactions GUARDED_BY ( g_cs_orphans ) ;
std : : map < COutPoint , std : : set < std : : map < uint256 , COrphanTx > : : iterator , IteratorComparator > > mapOrphanTransactionsByPrev GUARDED_BY ( g_cs_orphans ) ;
void EraseOrphansFor ( NodeId peer ) ;
static size_t vExtraTxnForCompactIt = 0 ;
static std : : vector < std : : pair < uint256 , CTransactionRef > > vExtraTxnForCompact GUARDED_BY ( cs_main ) ;
static size_t vExtraTxnForCompactIt GUARDED_BY ( g_cs_orphans ) = 0 ;
static std : : vector < std : : pair < uint256 , CTransactionRef > > vExtraTxnForCompact GUARDED_BY ( g_cs_orphans ) ;
static const uint64_t RANDOMIZER_ID_ADDRESS_RELAY = 0x3cac0035b5866b90ULL ; // SHA256("main address relay")[0:8]
@ -127,7 +128,7 @@ namespace {
@@ -127,7 +128,7 @@ namespace {
int g_outbound_peers_with_protect_from_disconnect = 0 ;
/** When our tip was last updated. */
int64_t g_last_tip_update = 0 ;
std : : atomic < int64_t > g_last_tip_update ( 0 ) ;
/** Relay map, protected by cs_main. */
typedef std : : map < uint256 , CTransactionRef > MapRelay ;
@ -631,7 +632,7 @@ bool GetNodeStateStats(NodeId nodeid, CNodeStateStats &stats) {
@@ -631,7 +632,7 @@ bool GetNodeStateStats(NodeId nodeid, CNodeStateStats &stats) {
// mapOrphanTransactions
//
void AddToCompactExtraTransactions ( const CTransactionRef & tx ) EXCLUSIVE_LOCKS_REQUIRED ( cs_main )
void AddToCompactExtraTransactions ( const CTransactionRef & tx ) EXCLUSIVE_LOCKS_REQUIRED ( g_cs_orphans )
{
size_t max_extra_txn = gArgs . GetArg ( " -blockreconstructionextratxn " , DEFAULT_BLOCK_RECONSTRUCTION_EXTRA_TXN ) ;
if ( max_extra_txn < = 0 )
@ -642,7 +643,7 @@ void AddToCompactExtraTransactions(const CTransactionRef& tx) EXCLUSIVE_LOCKS_RE
@@ -642,7 +643,7 @@ void AddToCompactExtraTransactions(const CTransactionRef& tx) EXCLUSIVE_LOCKS_RE
vExtraTxnForCompactIt = ( vExtraTxnForCompactIt + 1 ) % max_extra_txn ;
}
bool AddOrphanTx ( const CTransactionRef & tx , NodeId peer ) EXCLUSIVE_LOCKS_REQUIRED ( cs_main )
bool AddOrphanTx ( const CTransactionRef & tx , NodeId peer ) EXCLUSIVE_LOCKS_REQUIRED ( g_cs_orphans )
{
const uint256 & hash = tx - > GetHash ( ) ;
if ( mapOrphanTransactions . count ( hash ) )
@ -675,7 +676,7 @@ bool AddOrphanTx(const CTransactionRef& tx, NodeId peer) EXCLUSIVE_LOCKS_REQUIRE
@@ -675,7 +676,7 @@ bool AddOrphanTx(const CTransactionRef& tx, NodeId peer) EXCLUSIVE_LOCKS_REQUIRE
return true ;
}
int static EraseOrphanTx ( uint256 hash ) EXCLUSIVE_LOCKS_REQUIRED ( cs_main )
int static EraseOrphanTx ( uint256 hash ) EXCLUSIVE_LOCKS_REQUIRED ( g_cs_orphans )
{
std : : map < uint256 , COrphanTx > : : iterator it = mapOrphanTransactions . find ( hash ) ;
if ( it = = mapOrphanTransactions . end ( ) )
@ -695,6 +696,7 @@ int static EraseOrphanTx(uint256 hash) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
@@ -695,6 +696,7 @@ int static EraseOrphanTx(uint256 hash) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
void EraseOrphansFor ( NodeId peer )
{
LOCK ( g_cs_orphans ) ;
int nErased = 0 ;
std : : map < uint256 , COrphanTx > : : iterator iter = mapOrphanTransactions . begin ( ) ;
while ( iter ! = mapOrphanTransactions . end ( ) )
@ -709,8 +711,10 @@ void EraseOrphansFor(NodeId peer)
@@ -709,8 +711,10 @@ void EraseOrphansFor(NodeId peer)
}
unsigned int LimitOrphanTxSize ( unsigned int nMaxOrphans ) EXCLUSIVE_LOCKS_REQUIRED ( cs_main )
unsigned int LimitOrphanTxSize ( unsigned int nMaxOrphans )
{
LOCK ( g_cs_orphans ) ;
unsigned int nEvicted = 0 ;
static int64_t nNextSweep ;
int64_t nNow = GetTime ( ) ;
@ -804,7 +808,7 @@ PeerLogicValidation::PeerLogicValidation(CConnman* connmanIn, CScheduler &schedu
@@ -804,7 +808,7 @@ PeerLogicValidation::PeerLogicValidation(CConnman* connmanIn, CScheduler &schedu
}
void PeerLogicValidation : : BlockConnected ( const std : : shared_ptr < const CBlock > & pblock , const CBlockIndex * pindex , const std : : vector < CTransactionRef > & vtxConflicted ) {
LOCK ( cs_main ) ;
LOCK ( g_cs_orphans ) ;
std : : vector < uint256 > vOrphanErase ;
@ -971,9 +975,13 @@ bool static AlreadyHave(const CInv& inv) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
@@ -971,9 +975,13 @@ bool static AlreadyHave(const CInv& inv) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
recentRejects - > reset ( ) ;
}
{
LOCK ( g_cs_orphans ) ;
if ( mapOrphanTransactions . count ( inv . hash ) ) return true ;
}
return recentRejects - > contains ( inv . hash ) | |
mempool . exists ( inv . hash ) | |
mapOrphanTransactions . count ( inv . hash ) | |
pcoinsTip - > HaveCoinInCache ( COutPoint ( inv . hash , 0 ) ) | | // Best effort: only try output 0 and 1
pcoinsTip - > HaveCoinInCache ( COutPoint ( inv . hash , 1 ) ) ;
}
@ -1030,29 +1038,9 @@ static void RelayAddress(const CAddress& addr, bool fReachable, CConnman* connma
@@ -1030,29 +1038,9 @@ static void RelayAddress(const CAddress& addr, bool fReachable, CConnman* connma
connman - > ForEachNodeThen ( std : : move ( sortfunc ) , std : : move ( pushfunc ) ) ;
}
void static ProcessGetData ( CNode * pfrom , const Consensus : : Params & consensusParams , CConnman * connman , const std : : atomic < bool > & interruptMsgProc )
{
std : : deque < CInv > : : iterator it = pfrom - > vRecvGetData . begin ( ) ;
std : : vector < CInv > vNotFound ;
const CNetMsgMaker msgMaker ( pfrom - > GetSendVersion ( ) ) ;
LOCK ( cs_main ) ;
while ( it ! = pfrom - > vRecvGetData . end ( ) ) {
// Don't bother if send buffer is too full to respond anyway
if ( pfrom - > fPauseSend )
break ;
const CInv & inv = * it ;
{
if ( interruptMsgProc )
return ;
it + + ;
if ( inv . type = = MSG_BLOCK | | inv . type = = MSG_FILTERED_BLOCK | | inv . type = = MSG_CMPCT_BLOCK | | inv . type = = MSG_WITNESS_BLOCK )
void static ProcessGetBlockData ( CNode * pfrom , const Consensus : : Params & consensusParams , const CInv & inv , CConnman * connman , const std : : atomic < bool > & interruptMsgProc )
{
bool send = false ;
BlockMap : : iterator mi = mapBlockIndex . find ( inv . hash ) ;
std : : shared_ptr < const CBlock > a_recent_block ;
std : : shared_ptr < const CBlockHeaderAndShortTxIDs > a_recent_compact_block ;
bool fWitnessesPresentInARecentCompactBlock ;
@ -1062,6 +1050,11 @@ void static ProcessGetData(CNode* pfrom, const Consensus::Params& consensusParam
@@ -1062,6 +1050,11 @@ void static ProcessGetData(CNode* pfrom, const Consensus::Params& consensusParam
a_recent_compact_block = most_recent_compact_block ;
fWitnessesPresentInARecentCompactBlock = fWitnessesPresentInMostRecentCompactBlock ;
}
bool need_activate_chain = false ;
{
LOCK ( cs_main ) ;
BlockMap : : iterator mi = mapBlockIndex . find ( inv . hash ) ;
if ( mi ! = mapBlockIndex . end ( ) )
{
if ( mi - > second - > nChainTx & & ! mi - > second - > IsValid ( BLOCK_VALID_SCRIPTS ) & &
@ -1071,14 +1064,24 @@ void static ProcessGetData(CNode* pfrom, const Consensus::Params& consensusParam
@@ -1071,14 +1064,24 @@ void static ProcessGetData(CNode* pfrom, const Consensus::Params& consensusParam
// before ActivateBestChain but after AcceptBlock).
// In this case, we need to run ActivateBestChain prior to checking the relay
// conditions below.
need_activate_chain = true ;
}
}
} // release cs_main before calling ActivateBestChain
if ( need_activate_chain ) {
CValidationState dummy ;
ActivateBestChain ( dummy , Params ( ) , a_recent_block ) ;
}
LOCK ( cs_main ) ;
BlockMap : : iterator mi = mapBlockIndex . find ( inv . hash ) ;
if ( mi ! = mapBlockIndex . end ( ) ) {
send = BlockRequestAllowed ( mi - > second , consensusParams ) ;
if ( ! send ) {
LogPrint ( BCLog : : NET , " %s: ignoring request from peer=%i for old block that isn't in the main chain \n " , __func__ , pfrom - > GetId ( ) ) ;
}
}
const CNetMsgMaker msgMaker ( pfrom - > GetSendVersion ( ) ) ;
// disconnect node in case we have reached the outbound limit for serving historical blocks
// never disconnect whitelisted nodes
if ( send & & connman - > OutboundTargetReached ( true ) & & ( ( ( pindexBestHeader ! = nullptr ) & & ( pindexBestHeader - > GetBlockTime ( ) - mi - > second - > GetBlockTime ( ) > HISTORICAL_BLOCK_AGE ) ) | | inv . type = = MSG_FILTERED_BLOCK ) & & ! pfrom - > fWhitelisted )
@ -1176,8 +1179,27 @@ void static ProcessGetData(CNode* pfrom, const Consensus::Params& consensusParam
@@ -1176,8 +1179,27 @@ void static ProcessGetData(CNode* pfrom, const Consensus::Params& consensusParam
}
}
}
else if ( inv . type = = MSG_TX | | inv . type = = MSG_WITNESS_TX )
void static ProcessGetData ( CNode * pfrom , const Consensus : : Params & consensusParams , CConnman * connman , const std : : atomic < bool > & interruptMsgProc )
{
AssertLockNotHeld ( cs_main ) ;
std : : deque < CInv > : : iterator it = pfrom - > vRecvGetData . begin ( ) ;
std : : vector < CInv > vNotFound ;
const CNetMsgMaker msgMaker ( pfrom - > GetSendVersion ( ) ) ;
{
LOCK ( cs_main ) ;
while ( it ! = pfrom - > vRecvGetData . end ( ) & & ( it - > type = = MSG_TX | | it - > type = = MSG_WITNESS_TX ) ) {
if ( interruptMsgProc )
return ;
// Don't bother if send buffer is too full to respond anyway
if ( pfrom - > fPauseSend )
break ;
const CInv & inv = * it ;
it + + ;
// Send stream from relay memory
bool push = false ;
auto mi = mapRelay . find ( inv . hash ) ;
@ -1197,13 +1219,17 @@ void static ProcessGetData(CNode* pfrom, const Consensus::Params& consensusParam
@@ -1197,13 +1219,17 @@ void static ProcessGetData(CNode* pfrom, const Consensus::Params& consensusParam
if ( ! push ) {
vNotFound . push_back ( inv ) ;
}
}
// Track requests for our stuff.
GetMainSignals ( ) . Inventory ( inv . hash ) ;
}
} // release cs_main
if ( inv . type = = MSG_BLOCK | | inv . type = = MSG_FILTERED_BLOCK | | inv . type = = MSG_CMPCT_BLOCK | | inv . type = = MSG_WITNESS_BLOCK )
break ;
if ( it ! = pfrom - > vRecvGetData . end ( ) ) {
const CInv & inv = * it ;
it + + ;
if ( inv . type = = MSG_BLOCK | | inv . type = = MSG_FILTERED_BLOCK | | inv . type = = MSG_CMPCT_BLOCK | | inv . type = = MSG_WITNESS_BLOCK ) {
ProcessGetBlockData ( pfrom , consensusParams , inv , connman , interruptMsgProc ) ;
}
}
@ -2008,7 +2034,7 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
@@ -2008,7 +2034,7 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
inv . type = State ( pfrom - > GetId ( ) ) - > fWantsCmpctWitness ? MSG_WITNESS_BLOCK : MSG_BLOCK ;
inv . hash = req . blockhash ;
pfrom - > vRecvGetData . push_back ( inv ) ;
ProcessGetData ( pfrom , chainparams . GetConsensus ( ) , connman , interruptMsgProc ) ;
// The message processing loop will go around again (without pausing) and we'll respond then (without cs_main)
return true ;
}
@ -2101,7 +2127,7 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
@@ -2101,7 +2127,7 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
CInv inv ( MSG_TX , tx . GetHash ( ) ) ;
pfrom - > AddInventoryKnown ( inv ) ;
LOCK ( cs_main ) ;
LOCK2 ( cs_main , g_cs_orphans ) ;
bool fMissingInputs = false ;
CValidationState state ;
@ -2324,7 +2350,7 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
@@ -2324,7 +2350,7 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
bool fBlockReconstructed = false ;
{
LOCK ( cs_main ) ;
LOCK2 ( cs_main , g_cs_orphans ) ;
// If AcceptBlockHeader returned true, it set pindex
assert ( pindex ) ;
UpdateBlockAvailability ( pfrom - > GetId ( ) , pindex - > GetBlockHash ( ) ) ;