mirror of
https://github.com/bitcoin/bitcoin.git
synced 2026-01-19 06:43:45 +01:00
Clean up separated ban/discourage interface
This commit is contained in:
@@ -252,8 +252,8 @@ struct CNodeState {
|
||||
bool fCurrentlyConnected;
|
||||
//! Accumulated misbehaviour score for this peer.
|
||||
int nMisbehavior;
|
||||
//! Whether this peer should be disconnected and banned (unless whitelisted).
|
||||
bool fShouldBan;
|
||||
//! Whether this peer should be disconnected and marked as discouraged (unless whitelisted with noban).
|
||||
bool m_should_discourage;
|
||||
//! String name of this peer (debugging/logging purposes).
|
||||
const std::string name;
|
||||
//! The best known block we know this peer has announced.
|
||||
@@ -404,7 +404,7 @@ struct CNodeState {
|
||||
{
|
||||
fCurrentlyConnected = false;
|
||||
nMisbehavior = 0;
|
||||
fShouldBan = false;
|
||||
m_should_discourage = false;
|
||||
pindexBestKnownBlock = nullptr;
|
||||
hashLastUnknownBlock.SetNull();
|
||||
pindexLastCommonBlock = nullptr;
|
||||
@@ -1036,7 +1036,7 @@ void Misbehaving(NodeId pnode, int howmuch, const std::string& message) EXCLUSIV
|
||||
if (state->nMisbehavior >= banscore && state->nMisbehavior - howmuch < banscore)
|
||||
{
|
||||
LogPrint(BCLog::NET, "%s: %s peer=%d (%d -> %d) DISCOURAGE THRESHOLD EXCEEDED%s\n", __func__, state->name, pnode, state->nMisbehavior-howmuch, state->nMisbehavior, message_prefixed);
|
||||
state->fShouldBan = true;
|
||||
state->m_should_discourage = true;
|
||||
} else
|
||||
LogPrint(BCLog::NET, "%s: %s peer=%d (%d -> %d)%s\n", __func__, state->name, pnode, state->nMisbehavior-howmuch, state->nMisbehavior, message_prefixed);
|
||||
}
|
||||
@@ -2476,7 +2476,8 @@ void ProcessMessage(
|
||||
if (addr.nTime <= 100000000 || addr.nTime > nNow + 10 * 60)
|
||||
addr.nTime = nNow - 5 * 24 * 60 * 60;
|
||||
pfrom.AddAddressKnown(addr);
|
||||
if (banman->IsBanned(addr)) continue; // Do not process banned addresses beyond remembering we received them
|
||||
if (banman->IsDiscouraged(addr)) continue; // Do not process banned/discouraged addresses beyond remembering we received them
|
||||
if (banman->IsBanned(addr)) continue;
|
||||
bool fReachable = IsReachable(addr);
|
||||
if (addr.nTime > nSince && !pfrom.fGetAddr && vAddr.size() <= 10 && addr.IsRoutable())
|
||||
{
|
||||
@@ -3329,7 +3330,7 @@ void ProcessMessage(
|
||||
std::vector<CAddress> vAddr = connman->GetAddresses();
|
||||
FastRandomContext insecure_rand;
|
||||
for (const CAddress &addr : vAddr) {
|
||||
if (!banman->IsBanned(addr)) {
|
||||
if (!banman->IsDiscouraged(addr) && !banman->IsBanned(addr)) {
|
||||
pfrom.PushAddress(addr, insecure_rand);
|
||||
}
|
||||
}
|
||||
@@ -3564,26 +3565,26 @@ void ProcessMessage(
|
||||
return;
|
||||
}
|
||||
|
||||
bool PeerLogicValidation::CheckIfBanned(CNode& pnode)
|
||||
bool PeerLogicValidation::MaybeDiscourageAndDisconnect(CNode& pnode)
|
||||
{
|
||||
AssertLockHeld(cs_main);
|
||||
CNodeState &state = *State(pnode.GetId());
|
||||
|
||||
if (state.fShouldBan) {
|
||||
state.fShouldBan = false;
|
||||
if (pnode.HasPermission(PF_NOBAN))
|
||||
if (state.m_should_discourage) {
|
||||
state.m_should_discourage = false;
|
||||
if (pnode.HasPermission(PF_NOBAN)) {
|
||||
LogPrintf("Warning: not punishing whitelisted peer %s!\n", pnode.addr.ToString());
|
||||
else if (pnode.m_manual_connection)
|
||||
} else if (pnode.m_manual_connection) {
|
||||
LogPrintf("Warning: not punishing manually-connected peer %s!\n", pnode.addr.ToString());
|
||||
else if (pnode.addr.IsLocal()) {
|
||||
} else if (pnode.addr.IsLocal()) {
|
||||
// Disconnect but don't discourage this local node
|
||||
LogPrintf("Warning: disconnecting but not discouraging local peer %s!\n", pnode.addr.ToString());
|
||||
pnode.fDisconnect = true;
|
||||
} else {
|
||||
// Disconnect and ban all nodes sharing the address
|
||||
// Disconnect and discourage all nodes sharing the address
|
||||
LogPrintf("Disconnecting and discouraging peer %s!\n", pnode.addr.ToString());
|
||||
if (m_banman) {
|
||||
m_banman->Ban(pnode.addr, BanReasonNodeMisbehaving);
|
||||
m_banman->Discourage(pnode.addr);
|
||||
}
|
||||
connman->DisconnectNode(pnode.addr);
|
||||
}
|
||||
@@ -3683,7 +3684,7 @@ bool PeerLogicValidation::ProcessMessages(CNode* pfrom, std::atomic<bool>& inter
|
||||
}
|
||||
|
||||
LOCK(cs_main);
|
||||
CheckIfBanned(*pfrom);
|
||||
MaybeDiscourageAndDisconnect(*pfrom);
|
||||
|
||||
return fMoreWork;
|
||||
}
|
||||
@@ -3886,7 +3887,7 @@ bool PeerLogicValidation::SendMessages(CNode* pto)
|
||||
if (!lockMain)
|
||||
return true;
|
||||
|
||||
if (CheckIfBanned(*pto)) return true;
|
||||
if (MaybeDiscourageAndDisconnect(*pto)) return true;
|
||||
|
||||
CNodeState &state = *State(pto->GetId());
|
||||
|
||||
|
||||
Reference in New Issue
Block a user