Skip to content

Commit a292df2

Browse files
committed
[net processing] Move mapNodeState into PeerManagerImpl
1 parent 37ecaf3 commit a292df2

File tree

3 files changed

+27
-13
lines changed

3 files changed

+27
-13
lines changed

src/net_processing.cpp

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,7 @@ class PeerManagerImpl final : public PeerManager
462462
void Misbehaving(const NodeId pnode, const int howmuch, const std::string& message) override;
463463
void ProcessMessage(CNode& pfrom, const std::string& msg_type, CDataStream& vRecv,
464464
const std::chrono::microseconds time_received, const std::atomic<bool>& interruptMsgProc) override;
465+
void UpdateLastBlockAnnounceTime(NodeId node, int64_t time_in_seconds) override;
465466

466467
private:
467468
/** Consider evicting an outbound peer based on the amount of time they've been behind our tip */
@@ -580,6 +581,16 @@ class PeerManagerImpl final : public PeerManager
580581
*/
581582
std::map<NodeId, PeerRef> m_peer_map GUARDED_BY(m_peer_mutex);
582583

584+
/** Map maintaining per-node state. */
585+
std::map<NodeId, CNodeState> mapNodeState GUARDED_BY(cs_main);
586+
587+
/** Get a pointer to a const CNodeState, used when not mutating the CNodeState object. */
588+
const CNodeState* State(NodeId pnode) const EXCLUSIVE_LOCKS_REQUIRED(cs_main);
589+
/** Get a pointer to a mutable CNodeState. */
590+
CNodeState* State(NodeId pnode) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
591+
592+
uint32_t GetFetchFlags(const CNode& pfrom) const EXCLUSIVE_LOCKS_REQUIRED(cs_main);
593+
583594
std::atomic<std::chrono::microseconds> m_next_inv_to_inbounds{0us};
584595

585596
/** Number of nodes with fSyncStarted. */
@@ -815,16 +826,19 @@ namespace {
815826

816827
namespace {
817828

818-
/** Map maintaining per-node state. */
819-
static std::map<NodeId, CNodeState> mapNodeState GUARDED_BY(cs_main);
820-
821-
static CNodeState *State(NodeId pnode) EXCLUSIVE_LOCKS_REQUIRED(cs_main) {
822-
std::map<NodeId, CNodeState>::iterator it = mapNodeState.find(pnode);
829+
const CNodeState* PeerManagerImpl::State(NodeId pnode) const EXCLUSIVE_LOCKS_REQUIRED(cs_main)
830+
{
831+
std::map<NodeId, CNodeState>::const_iterator it = mapNodeState.find(pnode);
823832
if (it == mapNodeState.end())
824833
return nullptr;
825834
return &it->second;
826835
}
827836

837+
CNodeState* PeerManagerImpl::State(NodeId pnode) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
838+
{
839+
return const_cast<CNodeState*>(std::as_const(*this).State(pnode));
840+
}
841+
828842
/**
829843
* Whether the peer supports the address. For example, a peer that does not
830844
* implement BIP155 cannot receive Tor v3 addresses because it requires
@@ -1214,9 +1228,7 @@ void PeerManagerImpl::AddTxAnnouncement(const CNode& node, const GenTxid& gtxid,
12141228
m_txrequest.ReceivedInv(nodeid, gtxid, preferred, current_time + delay);
12151229
}
12161230

1217-
// This function is used for testing the stale tip eviction logic, see
1218-
// denialofservice_tests.cpp
1219-
void UpdateLastBlockAnnounceTime(NodeId node, int64_t time_in_seconds)
1231+
void PeerManagerImpl::UpdateLastBlockAnnounceTime(NodeId node, int64_t time_in_seconds)
12201232
{
12211233
LOCK(cs_main);
12221234
CNodeState *state = State(node);
@@ -1342,7 +1354,7 @@ bool PeerManagerImpl::GetNodeStateStats(NodeId nodeid, CNodeStateStats& stats) c
13421354
{
13431355
{
13441356
LOCK(cs_main);
1345-
CNodeState* state = State(nodeid);
1357+
const CNodeState* state = State(nodeid);
13461358
if (state == nullptr)
13471359
return false;
13481360
stats.nSyncHeight = state->pindexBestKnownBlock ? state->pindexBestKnownBlock->nHeight : -1;
@@ -2122,7 +2134,8 @@ void PeerManagerImpl::ProcessGetData(CNode& pfrom, Peer& peer, const std::atomic
21222134
}
21232135
}
21242136

2125-
static uint32_t GetFetchFlags(const CNode& pfrom) EXCLUSIVE_LOCKS_REQUIRED(cs_main) {
2137+
uint32_t PeerManagerImpl::GetFetchFlags(const CNode& pfrom) const EXCLUSIVE_LOCKS_REQUIRED(cs_main)
2138+
{
21262139
uint32_t nFetchFlags = 0;
21272140
if (State(pfrom.GetId())->fHaveWitness) {
21282141
nFetchFlags |= MSG_WITNESS_FLAG;

src/net_processing.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,9 @@ class PeerManager : public CValidationInterface, public NetEventsInterface
8787
/** Process a single message from a peer. Public for fuzz testing */
8888
virtual void ProcessMessage(CNode& pfrom, const std::string& msg_type, CDataStream& vRecv,
8989
const std::chrono::microseconds time_received, const std::atomic<bool>& interruptMsgProc) = 0;
90+
91+
/** This function is used for testing the stale tip eviction logic, see denialofservice_tests.cpp */
92+
virtual void UpdateLastBlockAnnounceTime(NodeId node, int64_t time_in_seconds) = 0;
9093
};
9194

9295
#endif // BITCOIN_NET_PROCESSING_H

src/test/denialofservice_tests.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,6 @@ static CService ip(uint32_t i)
3434
return CService(CNetAddr(s), Params().GetDefaultPort());
3535
}
3636

37-
void UpdateLastBlockAnnounceTime(NodeId node, int64_t time_in_seconds);
38-
3937
BOOST_FIXTURE_TEST_SUITE(denialofservice_tests, TestingSetup)
4038

4139
// Test eviction of an outbound peer whose chain never advances
@@ -197,7 +195,7 @@ BOOST_AUTO_TEST_CASE(stale_tip_peer_management)
197195

198196
// Update the last announced block time for the last
199197
// peer, and check that the next newest node gets evicted.
200-
UpdateLastBlockAnnounceTime(vNodes.back()->GetId(), GetTime());
198+
peerLogic->UpdateLastBlockAnnounceTime(vNodes.back()->GetId(), GetTime());
201199

202200
peerLogic->CheckForStaleTipAndEvictPeers();
203201
for (int i = 0; i < max_outbound_full_relay - 1; ++i) {

0 commit comments

Comments
 (0)