Files
thehub/libs/p2p/CleanPeersAction.cpp
T
tomFlowee fe0115a5bc Fix distance to limits of the headers call
as the max number of headers is 2000, we should avoid asking any peers
for this data before we are close enough to the tip for it to be easy to
verify as correct.
2023-04-19 15:34:19 +02:00

61 lines
2.3 KiB
C++

/*
* This file is part of the Flowee project
* Copyright (C) 2021 Tom Zander <tom@flowee.org>
*
* 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;
if (std::abs(m_dlm->blockchain().expectedBlockHeight() - m_dlm->blockHeight()) > 1000) {
// don't do anything until we got at least close enough to have useful 'HEADER' calls.
again();
return;
}
for (auto peer : m_dlm->connectionManager().connectedPeers()) {
logDebug() << "peer" << peer->connectionId() << "headers" << peer->peerAddress().lastReceivedGoodHeaders()
<< time(nullptr) - peer->peerAddress().lastConnected() << "s";
if (!peer->receivedHeaders() && peer->peerAddress().lastReceivedGoodHeaders() == 0) {
// make sure that we actually request headers from peers.
if (!peer->requestedHeader()) {
m_dlm->connectionManager().requestHeaders(peer);
}
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";
m_dlm->connectionManager().punish(peer, PUNISHMENT_MAX);
}
}
}
again();
}