Files
thehub/libs/p2p/CleanPeersAction.cpp
T

61 lines
2.3 KiB
C++
Raw Permalink Normal View History

2021-02-03 14:35:06 +01:00
/*
* This file is part of the Flowee project
* Copyright (C) 2021-2024 Tom Zander <tom@flowee.org>
2021-02-03 14:35:06 +01:00
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "CleanPeersAction.h"
#include "DownloadManager.h"
#include "Peer.h"
#include <Logger.h>
CleanPeersAction::CleanPeersAction(DownloadManager *parent)
: Action(parent)
{
setInterval(35 * 1000);
}
void CleanPeersAction::execute(const boost::system::error_code &error)
{
if (error)
return;
2023-04-19 15:34:19 +02:00
if (std::abs(m_dlm->blockchain().expectedBlockHeight() - m_dlm->blockHeight()) > 1000) {
2021-10-27 17:42:27 +02:00
// don't do anything until we got at least close enough to have useful 'HEADER' calls.
again();
return;
}
2021-02-03 14:35:06 +01:00
for (auto peer : m_dlm->connectionManager().connectedPeers()) {
logDebug() << "peer" << peer->connectionId() << "headers" << peer->peerAddress().lastReceivedGoodHeaders()
<< time(nullptr) - peer->peerAddress().lastConnected() << "s";
2021-10-27 17:42:27 +02:00
if (!peer->receivedHeaders() && peer->peerAddress().lastReceivedGoodHeaders() == 0) {
// make sure that we actually request headers from peers.
if (!peer->requestedHeader()) {
m_dlm->connectionManager().requestHeaders(peer, 5);
2021-10-27 17:42:27 +02:00
}
else if (time(nullptr) - peer->peerAddress().lastConnected() > 90) {
// ban peers that never responded to our request for headers
logWarning() << "CleanPeersAction disconnects peer"
<< peer->connectionId() << "that has not responded to 'HEADERS' call for 90 seconds";
2021-07-30 14:02:33 +02:00
2021-10-27 17:42:27 +02:00
m_dlm->connectionManager().punish(peer, PUNISHMENT_MAX);
}
2021-02-03 14:35:06 +01:00
}
}
again();
}