Skip to content

Commit

Permalink
net: Simplify persistent misbehaviour counter.
Browse files Browse the repository at this point in the history
  • Loading branch information
tecnovert committed Apr 16, 2022
1 parent 3e76e96 commit c17a3db
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/net.cpp
Expand Up @@ -2358,7 +2358,7 @@ void CConnman::ThreadMessageHandler()
int64_t nTimeNow = GetTime();
if (nTimeNextBanReduced < nTimeNow) {
LOCK(cs_main);
m_msgproc->CheckUnreceivedHeaders(nTimeNow);
m_msgproc->CheckUnreceivedHeaders(nTimeNow); // Also reduces persistent misbehaviour score
for (CNode* pnode : snap.Nodes()) {
m_msgproc->DecMisbehaving(pnode->id, 1);
pnode->smsgData.DecSmsgMisbehaving();
Expand Down
53 changes: 42 additions & 11 deletions src/net_processing.cpp
Expand Up @@ -697,6 +697,7 @@ class PeerManagerImpl final : public PeerManager
public:
NodeId GetBlockSource(const uint256 &hash) override EXCLUSIVE_LOCKS_REQUIRED(cs_main);
void IncPersistentMisbehaviour(NodeId node_id, int howmuch) override EXCLUSIVE_LOCKS_REQUIRED(cs_main);
bool IncPersistentDiscouraged(NodeId node_id) override EXCLUSIVE_LOCKS_REQUIRED(cs_main);
void DecMisbehaving(NodeId nodeid, int howmuch) override;
void MisbehavingByAddr(CNetAddr addr, int misbehavior_cfwd, int howmuch, const std::string& message) override EXCLUSIVE_LOCKS_REQUIRED(cs_main);
bool IncDuplicateHeaders(NodeId node_id) override EXCLUSIVE_LOCKS_REQUIRED(cs_main);
Expand Down Expand Up @@ -831,6 +832,9 @@ class CNodeDOS

//! Persistent misbehaving counter
int m_misbehavior = 0;

//! Times node was discouraged
int m_discouraged_count = 0;
};

/** Map maintaining per-addr DOS state. */
Expand Down Expand Up @@ -1423,6 +1427,10 @@ void PeerManagerImpl::Misbehaving(const NodeId pnode, const int howmuch, const s

PeerRef peer = GetPeerRef(pnode);
if (peer == nullptr) return;
if (fParticlMode) {
LOCK(cs_main);
IncPersistentMisbehaviour(pnode, howmuch);
}

LOCK(peer->m_misbehavior_mutex);
const int score_before{peer->m_misbehavior_score};
Expand Down Expand Up @@ -1611,7 +1619,7 @@ bool PeerManagerImpl::MaybePunishNodeForBlock(NodeId nodeid, const BlockValidati
bool PeerManagerImpl::MaybePunishNodeForDuplicates(NodeId nodeid, const BlockValidationState& state)
{
if (state.m_punish_for_duplicates) {
Misbehaving(nodeid, 5, "Too many duplicates");
Misbehaving(nodeid, 10, "Too many duplicates");
return true;
}
return false;
Expand Down Expand Up @@ -1684,11 +1692,6 @@ void PeerManagerImpl::MisbehavingByAddr(CNetAddr addr, int misbehavior_cfwd, int
if (addr == (CNetAddr)it->second.address) {
PeerRef peer = GetPeerRef(it->first);
if (peer == nullptr) continue;

int misbehavior_score = WITH_LOCK(peer->m_misbehavior_mutex, return peer->m_misbehavior_score);
if (misbehavior_score < misbehavior_cfwd) {
PassOnMisbehaviour(it->first, misbehavior_cfwd);
}
Misbehaving(it->first, howmuch, message);
}
}
Expand All @@ -1711,11 +1714,6 @@ bool PeerManagerImpl::IncDuplicateHeaders(NodeId node_id) EXCLUSIVE_LOCKS_REQUIR
if (it->second.m_duplicate_count < MAX_DUPLICATE_HEADERS) {
return true;
}
int peer_misbehavior_score = WITH_LOCK(peer->m_misbehavior_mutex, return peer->m_misbehavior_score);
if (peer_misbehavior_score < it->second.m_misbehavior) {
PassOnMisbehaviour(node_id, it->second.m_misbehavior);
}
WITH_LOCK(peer->m_misbehavior_mutex, peer->m_misbehavior_score += 5);
return false;
}
map_dos_state[node_address].m_duplicate_count = 1;
Expand Down Expand Up @@ -1755,6 +1753,32 @@ void PeerManagerImpl::IncPersistentMisbehaviour(NodeId node_id, int howmuch)
return;
}

bool PeerManagerImpl::IncPersistentDiscouraged(NodeId node_id)
{
CNodeState *state = State(node_id);
if (state == nullptr) {
return false;
}
const CService &node_address = state->address;

PeerRef peer = GetPeerRef(node_id);
if (peer == nullptr) return false;
auto it = map_dos_state.find(node_address);
if (it != map_dos_state.end()) {
it->second.m_discouraged_count += 1;
if (it->second.m_discouraged_count > 5) {
WITH_LOCK(peer->m_misbehavior_mutex, peer->m_should_ban = true);
LogPrintf("Too often discouraged. Banning peer %d!\n", node_id);
it->second.m_discouraged_count = 0;
return true;
}
LogPrint(BCLog::NET, "peer=%d discouraged count (%d)\n", node_id, it->second.m_discouraged_count);
return false;
}
map_dos_state[node_address].m_discouraged_count = 1;
return false;
}

void PeerManagerImpl::CheckUnreceivedHeaders(int64_t now) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
{
auto it = map_dos_state.begin();
Expand Down Expand Up @@ -4518,6 +4542,13 @@ bool PeerManagerImpl::MaybeDiscourageAndDisconnect(CNode& pnode, Peer& peer)
peer.m_should_ban = false;
} // peer.m_misbehavior_mutex

if (fParticlMode && !banning) {
LOCK(cs_main);
if (IncPersistentDiscouraged(peer.m_id)) {
banning = true;
}
}

if (pnode.HasPermission(NetPermissionFlags::NoBan)) {
// We never disconnect or discourage peers for bad behavior if they have NetPermissionFlags::NoBan permission
LogPrintf("Warning: not punishing noban peer %d!\n", peer.m_id);
Expand Down
1 change: 1 addition & 0 deletions src/net_processing.h
Expand Up @@ -96,6 +96,7 @@ class PeerManager : public CValidationInterface, public NetEventsInterface
/** Particl */
virtual NodeId GetBlockSource(const uint256 &hash) = 0;
virtual void IncPersistentMisbehaviour(NodeId node_id, int howmuch) EXCLUSIVE_LOCKS_REQUIRED(cs_main) = 0;
virtual bool IncPersistentDiscouraged(NodeId node_id) EXCLUSIVE_LOCKS_REQUIRED(cs_main) = 0;
virtual void MisbehavingByAddr(CNetAddr addr, int misbehavior_cfwd, int howmuch, const std::string& message="") = 0;
virtual bool IncDuplicateHeaders(NodeId node_id) EXCLUSIVE_LOCKS_REQUIRED(cs_main) = 0;
};
Expand Down
1 change: 0 additions & 1 deletion src/validation.cpp
Expand Up @@ -4866,7 +4866,6 @@ bool CChainState::AcceptBlock(const std::shared_ptr<const CBlock>& pblock, Block

if (state.nFlags & BLOCK_STAKE_KERNEL_SPENT && !(state.nFlags & BLOCK_FAILED_DUPLICATE_STAKE)) {
if (state.nodeId > -1) {
state.m_peerman->IncPersistentMisbehaviour(state.nodeId, 20);
state.m_peerman->Misbehaving(state.nodeId, 20, "Spent kernel");
}
}
Expand Down

0 comments on commit c17a3db

Please sign in to comment.