Changeset View
Changeset View
Standalone View
Standalone View
src/net_processing.cpp
- This file is larger than 256 KB, so syntax highlighting is disabled by default.
Show First 20 Lines • Show All 808 Lines • ▼ Show 20 Lines | private: | ||||
/** | /** | ||||
* Expiration-time ordered list of (expire time, relay map entry) pairs, | * Expiration-time ordered list of (expire time, relay map entry) pairs, | ||||
* protected by cs_main). | * protected by cs_main). | ||||
*/ | */ | ||||
std::deque<std::pair<std::chrono::microseconds, MapRelay::iterator>> | std::deque<std::pair<std::chrono::microseconds, MapRelay::iterator>> | ||||
g_relay_expiration GUARDED_BY(cs_main); | g_relay_expiration GUARDED_BY(cs_main); | ||||
/** | /** | ||||
* When a peer sends us a valid block, instruct it to announce blocks to us | |||||
* using CMPCTBLOCK if possible by adding its nodeid to the end of | |||||
* lNodesAnnouncingHeaderAndIDs, and keeping that list under a certain size | |||||
* by removing the first element if necessary. | |||||
*/ | |||||
void MaybeSetPeerAsAnnouncingHeaderAndIDs(NodeId nodeid) | |||||
EXCLUSIVE_LOCKS_REQUIRED(cs_main); | |||||
/** Stack of nodes which we have set to announce using compact blocks */ | |||||
std::list<NodeId> lNodesAnnouncingHeaderAndIDs GUARDED_BY(cs_main); | |||||
/** Number of peers from which we're downloading blocks. */ | |||||
int nPeersWithValidatedDownloads GUARDED_BY(cs_main) = 0; | |||||
/** | |||||
* Checks if address relay is permitted with peer. If needed, initializes | * Checks if address relay is permitted with peer. If needed, initializes | ||||
* the m_addr_known bloom filter and sets m_addr_relay_enabled to true. | * the m_addr_known bloom filter and sets m_addr_relay_enabled to true. | ||||
* | * | ||||
* @return True if address relay is enabled with peer | * @return True if address relay is enabled with peer | ||||
* False if address relay is disallowed | * False if address relay is disallowed | ||||
*/ | */ | ||||
bool SetupAddressRelay(CNode &node, Peer &peer); | bool SetupAddressRelay(CNode &node, Peer &peer); | ||||
}; | }; | ||||
Show All 9 Lines | |||||
* | * | ||||
* Decreasing the false positive rate is fairly cheap, so we pick one in a | * Decreasing the false positive rate is fairly cheap, so we pick one in a | ||||
* million to make it highly unlikely for users to have issues with this filter. | * million to make it highly unlikely for users to have issues with this filter. | ||||
*/ | */ | ||||
Mutex cs_rejectedProofs; | Mutex cs_rejectedProofs; | ||||
std::unique_ptr<CRollingBloomFilter> | std::unique_ptr<CRollingBloomFilter> | ||||
rejectedProofs GUARDED_BY(cs_rejectedProofs); | rejectedProofs GUARDED_BY(cs_rejectedProofs); | ||||
/** Stack of nodes which we have set to announce using compact blocks */ | |||||
std::list<NodeId> lNodesAnnouncingHeaderAndIDs GUARDED_BY(cs_main); | |||||
/** Number of preferable block download peers. */ | /** Number of preferable block download peers. */ | ||||
int nPreferredDownload GUARDED_BY(cs_main) = 0; | int nPreferredDownload GUARDED_BY(cs_main) = 0; | ||||
/** Number of peers from which we're downloading blocks. */ | |||||
int nPeersWithValidatedDownloads GUARDED_BY(cs_main) = 0; | |||||
struct IteratorComparator { | struct IteratorComparator { | ||||
template <typename I> bool operator()(const I &a, const I &b) const { | template <typename I> bool operator()(const I &a, const I &b) const { | ||||
return &(*a) < &(*b); | return &(*a) < &(*b); | ||||
} | } | ||||
}; | }; | ||||
/** | /** | ||||
* Index from the parents' COutPoint into the mapOrphanTransactions. Used | * Index from the parents' COutPoint into the mapOrphanTransactions. Used | ||||
▲ Show 20 Lines • Show All 336 Lines • ▼ Show 20 Lines | if (pindex && pindex->nChainWork > 0) { | ||||
} | } | ||||
} else { | } else { | ||||
// An unknown block was announced; just assume that the latest one is | // An unknown block was announced; just assume that the latest one is | ||||
// the best one. | // the best one. | ||||
state->hashLastUnknownBlock = hash; | state->hashLastUnknownBlock = hash; | ||||
} | } | ||||
} | } | ||||
/** | void PeerManagerImpl::MaybeSetPeerAsAnnouncingHeaderAndIDs(NodeId nodeid) { | ||||
* When a peer sends us a valid block, instruct it to announce blocks to us | |||||
* using CMPCTBLOCK if possible by adding its nodeid to the end of | |||||
* lNodesAnnouncingHeaderAndIDs, and keeping that list under a certain size by | |||||
* removing the first element if necessary. | |||||
*/ | |||||
static void MaybeSetPeerAsAnnouncingHeaderAndIDs(NodeId nodeid, | |||||
CConnman &connman) | |||||
EXCLUSIVE_LOCKS_REQUIRED(cs_main) { | |||||
AssertLockHeld(cs_main); | AssertLockHeld(cs_main); | ||||
CNodeState *nodestate = State(nodeid); | CNodeState *nodestate = State(nodeid); | ||||
if (!nodestate) { | if (!nodestate) { | ||||
LogPrint(BCLog::NET, "node state unavailable: peer=%d\n", nodeid); | LogPrint(BCLog::NET, "node state unavailable: peer=%d\n", nodeid); | ||||
return; | return; | ||||
} | } | ||||
if (!nodestate->fProvidesHeaderAndIDs) { | if (!nodestate->fProvidesHeaderAndIDs) { | ||||
return; | return; | ||||
} | } | ||||
for (std::list<NodeId>::iterator it = lNodesAnnouncingHeaderAndIDs.begin(); | for (std::list<NodeId>::iterator it = lNodesAnnouncingHeaderAndIDs.begin(); | ||||
it != lNodesAnnouncingHeaderAndIDs.end(); it++) { | it != lNodesAnnouncingHeaderAndIDs.end(); it++) { | ||||
if (*it == nodeid) { | if (*it == nodeid) { | ||||
lNodesAnnouncingHeaderAndIDs.erase(it); | lNodesAnnouncingHeaderAndIDs.erase(it); | ||||
lNodesAnnouncingHeaderAndIDs.push_back(nodeid); | lNodesAnnouncingHeaderAndIDs.push_back(nodeid); | ||||
return; | return; | ||||
} | } | ||||
} | } | ||||
connman.ForNode(nodeid, [&connman](CNode *pfrom) EXCLUSIVE_LOCKS_REQUIRED( | m_connman.ForNode(nodeid, [this](CNode *pfrom) EXCLUSIVE_LOCKS_REQUIRED( | ||||
::cs_main) { | ::cs_main) { | ||||
AssertLockHeld(::cs_main); | AssertLockHeld(::cs_main); | ||||
uint64_t nCMPCTBLOCKVersion = 1; | uint64_t nCMPCTBLOCKVersion = 1; | ||||
if (lNodesAnnouncingHeaderAndIDs.size() >= 3) { | if (lNodesAnnouncingHeaderAndIDs.size() >= 3) { | ||||
// As per BIP152, we only get 3 of our peers to announce | // As per BIP152, we only get 3 of our peers to announce | ||||
// blocks using compact encodings. | // blocks using compact encodings. | ||||
connman.ForNode( | m_connman.ForNode( | ||||
lNodesAnnouncingHeaderAndIDs.front(), | lNodesAnnouncingHeaderAndIDs.front(), | ||||
[&connman, nCMPCTBLOCKVersion](CNode *pnodeStop) { | [this, nCMPCTBLOCKVersion](CNode *pnodeStop) { | ||||
connman.PushMessage( | m_connman.PushMessage( | ||||
pnodeStop, CNetMsgMaker(pnodeStop->GetCommonVersion()) | pnodeStop, CNetMsgMaker(pnodeStop->GetCommonVersion()) | ||||
.Make(NetMsgType::SENDCMPCT, | .Make(NetMsgType::SENDCMPCT, | ||||
/*fAnnounceUsingCMPCTBLOCK=*/false, | /*fAnnounceUsingCMPCTBLOCK=*/false, | ||||
nCMPCTBLOCKVersion)); | nCMPCTBLOCKVersion)); | ||||
// save BIP152 bandwidth state: we select peer to be | // save BIP152 bandwidth state: we select peer to be | ||||
// low-bandwidth | // low-bandwidth | ||||
pnodeStop->m_bip152_highbandwidth_to = false; | pnodeStop->m_bip152_highbandwidth_to = false; | ||||
return true; | return true; | ||||
}); | }); | ||||
lNodesAnnouncingHeaderAndIDs.pop_front(); | lNodesAnnouncingHeaderAndIDs.pop_front(); | ||||
} | } | ||||
connman.PushMessage(pfrom, CNetMsgMaker(pfrom->GetCommonVersion()) | m_connman.PushMessage(pfrom, | ||||
CNetMsgMaker(pfrom->GetCommonVersion()) | |||||
.Make(NetMsgType::SENDCMPCT, | .Make(NetMsgType::SENDCMPCT, | ||||
/*fAnnounceUsingCMPCTBLOCK=*/true, | /*fAnnounceUsingCMPCTBLOCK=*/true, | ||||
nCMPCTBLOCKVersion)); | nCMPCTBLOCKVersion)); | ||||
// save BIP152 bandwidth state: we select peer to be high-bandwidth | // save BIP152 bandwidth state: we select peer to be high-bandwidth | ||||
pfrom->m_bip152_highbandwidth_to = true; | pfrom->m_bip152_highbandwidth_to = true; | ||||
lNodesAnnouncingHeaderAndIDs.push_back(pfrom->GetId()); | lNodesAnnouncingHeaderAndIDs.push_back(pfrom->GetId()); | ||||
return true; | return true; | ||||
}); | }); | ||||
} | } | ||||
bool PeerManagerImpl::TipMayBeStale() { | bool PeerManagerImpl::TipMayBeStale() { | ||||
▲ Show 20 Lines • Show All 1,064 Lines • ▼ Show 20 Lines | void PeerManagerImpl::BlockChecked(const CBlock &block, | ||||
// 2. We're not in initial block download | // 2. We're not in initial block download | ||||
// 3. This is currently the best block we're aware of. We haven't updated | // 3. This is currently the best block we're aware of. We haven't updated | ||||
// the tip yet so we have no way to check this directly here. Instead we | // the tip yet so we have no way to check this directly here. Instead we | ||||
// just check that there are currently no other blocks in flight. | // just check that there are currently no other blocks in flight. | ||||
else if (state.IsValid() && | else if (state.IsValid() && | ||||
!::ChainstateActive().IsInitialBlockDownload() && | !::ChainstateActive().IsInitialBlockDownload() && | ||||
mapBlocksInFlight.count(hash) == mapBlocksInFlight.size()) { | mapBlocksInFlight.count(hash) == mapBlocksInFlight.size()) { | ||||
if (it != mapBlockSource.end()) { | if (it != mapBlockSource.end()) { | ||||
MaybeSetPeerAsAnnouncingHeaderAndIDs(it->second.first, m_connman); | MaybeSetPeerAsAnnouncingHeaderAndIDs(it->second.first); | ||||
} | } | ||||
} | } | ||||
if (it != mapBlockSource.end()) { | if (it != mapBlockSource.end()) { | ||||
mapBlockSource.erase(it); | mapBlockSource.erase(it); | ||||
} | } | ||||
} | } | ||||
▲ Show 20 Lines • Show All 4,694 Lines • Show Last 20 Lines |