mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-02-09 10:43:19 -05:00
[net processing] Move mapNodeState into PeerManagerImpl
This commit is contained in:
parent
37ecaf3e7a
commit
a292df283a
3 changed files with 27 additions and 13 deletions
|
@ -462,6 +462,7 @@ public:
|
||||||
void Misbehaving(const NodeId pnode, const int howmuch, const std::string& message) override;
|
void Misbehaving(const NodeId pnode, const int howmuch, const std::string& message) override;
|
||||||
void ProcessMessage(CNode& pfrom, const std::string& msg_type, CDataStream& vRecv,
|
void ProcessMessage(CNode& pfrom, const std::string& msg_type, CDataStream& vRecv,
|
||||||
const std::chrono::microseconds time_received, const std::atomic<bool>& interruptMsgProc) override;
|
const std::chrono::microseconds time_received, const std::atomic<bool>& interruptMsgProc) override;
|
||||||
|
void UpdateLastBlockAnnounceTime(NodeId node, int64_t time_in_seconds) override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
/** Consider evicting an outbound peer based on the amount of time they've been behind our tip */
|
/** Consider evicting an outbound peer based on the amount of time they've been behind our tip */
|
||||||
|
@ -580,6 +581,16 @@ private:
|
||||||
*/
|
*/
|
||||||
std::map<NodeId, PeerRef> m_peer_map GUARDED_BY(m_peer_mutex);
|
std::map<NodeId, PeerRef> m_peer_map GUARDED_BY(m_peer_mutex);
|
||||||
|
|
||||||
|
/** Map maintaining per-node state. */
|
||||||
|
std::map<NodeId, CNodeState> mapNodeState GUARDED_BY(cs_main);
|
||||||
|
|
||||||
|
/** Get a pointer to a const CNodeState, used when not mutating the CNodeState object. */
|
||||||
|
const CNodeState* State(NodeId pnode) const EXCLUSIVE_LOCKS_REQUIRED(cs_main);
|
||||||
|
/** Get a pointer to a mutable CNodeState. */
|
||||||
|
CNodeState* State(NodeId pnode) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
|
||||||
|
|
||||||
|
uint32_t GetFetchFlags(const CNode& pfrom) const EXCLUSIVE_LOCKS_REQUIRED(cs_main);
|
||||||
|
|
||||||
std::atomic<std::chrono::microseconds> m_next_inv_to_inbounds{0us};
|
std::atomic<std::chrono::microseconds> m_next_inv_to_inbounds{0us};
|
||||||
|
|
||||||
/** Number of nodes with fSyncStarted. */
|
/** Number of nodes with fSyncStarted. */
|
||||||
|
@ -815,16 +826,19 @@ namespace {
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
|
|
||||||
/** Map maintaining per-node state. */
|
const CNodeState* PeerManagerImpl::State(NodeId pnode) const EXCLUSIVE_LOCKS_REQUIRED(cs_main)
|
||||||
static std::map<NodeId, CNodeState> mapNodeState GUARDED_BY(cs_main);
|
{
|
||||||
|
std::map<NodeId, CNodeState>::const_iterator it = mapNodeState.find(pnode);
|
||||||
static CNodeState *State(NodeId pnode) EXCLUSIVE_LOCKS_REQUIRED(cs_main) {
|
|
||||||
std::map<NodeId, CNodeState>::iterator it = mapNodeState.find(pnode);
|
|
||||||
if (it == mapNodeState.end())
|
if (it == mapNodeState.end())
|
||||||
return nullptr;
|
return nullptr;
|
||||||
return &it->second;
|
return &it->second;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
CNodeState* PeerManagerImpl::State(NodeId pnode) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
|
||||||
|
{
|
||||||
|
return const_cast<CNodeState*>(std::as_const(*this).State(pnode));
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Whether the peer supports the address. For example, a peer that does not
|
* Whether the peer supports the address. For example, a peer that does not
|
||||||
* implement BIP155 cannot receive Tor v3 addresses because it requires
|
* implement BIP155 cannot receive Tor v3 addresses because it requires
|
||||||
|
@ -1214,9 +1228,7 @@ void PeerManagerImpl::AddTxAnnouncement(const CNode& node, const GenTxid& gtxid,
|
||||||
m_txrequest.ReceivedInv(nodeid, gtxid, preferred, current_time + delay);
|
m_txrequest.ReceivedInv(nodeid, gtxid, preferred, current_time + delay);
|
||||||
}
|
}
|
||||||
|
|
||||||
// This function is used for testing the stale tip eviction logic, see
|
void PeerManagerImpl::UpdateLastBlockAnnounceTime(NodeId node, int64_t time_in_seconds)
|
||||||
// denialofservice_tests.cpp
|
|
||||||
void UpdateLastBlockAnnounceTime(NodeId node, int64_t time_in_seconds)
|
|
||||||
{
|
{
|
||||||
LOCK(cs_main);
|
LOCK(cs_main);
|
||||||
CNodeState *state = State(node);
|
CNodeState *state = State(node);
|
||||||
|
@ -1342,7 +1354,7 @@ bool PeerManagerImpl::GetNodeStateStats(NodeId nodeid, CNodeStateStats& stats) c
|
||||||
{
|
{
|
||||||
{
|
{
|
||||||
LOCK(cs_main);
|
LOCK(cs_main);
|
||||||
CNodeState* state = State(nodeid);
|
const CNodeState* state = State(nodeid);
|
||||||
if (state == nullptr)
|
if (state == nullptr)
|
||||||
return false;
|
return false;
|
||||||
stats.nSyncHeight = state->pindexBestKnownBlock ? state->pindexBestKnownBlock->nHeight : -1;
|
stats.nSyncHeight = state->pindexBestKnownBlock ? state->pindexBestKnownBlock->nHeight : -1;
|
||||||
|
@ -2122,7 +2134,8 @@ void PeerManagerImpl::ProcessGetData(CNode& pfrom, Peer& peer, const std::atomic
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static uint32_t GetFetchFlags(const CNode& pfrom) EXCLUSIVE_LOCKS_REQUIRED(cs_main) {
|
uint32_t PeerManagerImpl::GetFetchFlags(const CNode& pfrom) const EXCLUSIVE_LOCKS_REQUIRED(cs_main)
|
||||||
|
{
|
||||||
uint32_t nFetchFlags = 0;
|
uint32_t nFetchFlags = 0;
|
||||||
if (State(pfrom.GetId())->fHaveWitness) {
|
if (State(pfrom.GetId())->fHaveWitness) {
|
||||||
nFetchFlags |= MSG_WITNESS_FLAG;
|
nFetchFlags |= MSG_WITNESS_FLAG;
|
||||||
|
|
|
@ -87,6 +87,9 @@ public:
|
||||||
/** Process a single message from a peer. Public for fuzz testing */
|
/** Process a single message from a peer. Public for fuzz testing */
|
||||||
virtual void ProcessMessage(CNode& pfrom, const std::string& msg_type, CDataStream& vRecv,
|
virtual void ProcessMessage(CNode& pfrom, const std::string& msg_type, CDataStream& vRecv,
|
||||||
const std::chrono::microseconds time_received, const std::atomic<bool>& interruptMsgProc) = 0;
|
const std::chrono::microseconds time_received, const std::atomic<bool>& interruptMsgProc) = 0;
|
||||||
|
|
||||||
|
/** This function is used for testing the stale tip eviction logic, see denialofservice_tests.cpp */
|
||||||
|
virtual void UpdateLastBlockAnnounceTime(NodeId node, int64_t time_in_seconds) = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // BITCOIN_NET_PROCESSING_H
|
#endif // BITCOIN_NET_PROCESSING_H
|
||||||
|
|
|
@ -34,8 +34,6 @@ static CService ip(uint32_t i)
|
||||||
return CService(CNetAddr(s), Params().GetDefaultPort());
|
return CService(CNetAddr(s), Params().GetDefaultPort());
|
||||||
}
|
}
|
||||||
|
|
||||||
void UpdateLastBlockAnnounceTime(NodeId node, int64_t time_in_seconds);
|
|
||||||
|
|
||||||
BOOST_FIXTURE_TEST_SUITE(denialofservice_tests, TestingSetup)
|
BOOST_FIXTURE_TEST_SUITE(denialofservice_tests, TestingSetup)
|
||||||
|
|
||||||
// Test eviction of an outbound peer whose chain never advances
|
// Test eviction of an outbound peer whose chain never advances
|
||||||
|
@ -197,7 +195,7 @@ BOOST_AUTO_TEST_CASE(stale_tip_peer_management)
|
||||||
|
|
||||||
// Update the last announced block time for the last
|
// Update the last announced block time for the last
|
||||||
// peer, and check that the next newest node gets evicted.
|
// peer, and check that the next newest node gets evicted.
|
||||||
UpdateLastBlockAnnounceTime(vNodes.back()->GetId(), GetTime());
|
peerLogic->UpdateLastBlockAnnounceTime(vNodes.back()->GetId(), GetTime());
|
||||||
|
|
||||||
peerLogic->CheckForStaleTipAndEvictPeers();
|
peerLogic->CheckForStaleTipAndEvictPeers();
|
||||||
for (int i = 0; i < max_outbound_full_relay - 1; ++i) {
|
for (int i = 0; i < max_outbound_full_relay - 1; ++i) {
|
||||||
|
|
Loading…
Add table
Reference in a new issue