2020-05-24 13:20:03 +02:00
|
|
|
/*
|
|
|
|
|
* This file is part of the Flowee project
|
2021-01-22 18:46:27 +01:00
|
|
|
* Copyright (C) 2020-2021 Tom Zander <tom@flowee.org>
|
2020-05-24 13:20:03 +02: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 "NetDataProvider.h"
|
2021-07-30 10:52:09 +02:00
|
|
|
#include "NetPeer.h"
|
|
|
|
|
#include "FloweePay.h"
|
|
|
|
|
#include <ConnectionManager.h>
|
|
|
|
|
#include <Peer.h>
|
2020-05-24 13:20:03 +02:00
|
|
|
|
2021-01-22 18:46:27 +01:00
|
|
|
#include <QThread>
|
2021-07-30 10:52:09 +02:00
|
|
|
#include <QTimer>
|
2021-01-22 18:46:27 +01:00
|
|
|
|
2020-05-24 13:20:03 +02:00
|
|
|
NetDataProvider::NetDataProvider(int initialBlockHeight, QObject *parent)
|
|
|
|
|
: QObject(parent),
|
|
|
|
|
m_blockHeight(initialBlockHeight)
|
|
|
|
|
{
|
2021-07-30 10:52:09 +02:00
|
|
|
connect (this, SIGNAL(peerDeleted(int)), this, SLOT(deleteNetPeer(int)), Qt::QueuedConnection); // Make this thread-safe
|
2020-05-24 13:20:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void NetDataProvider::newPeer(int peerId, const std::string &userAgent, int startHeight, PeerAddress address)
|
|
|
|
|
{
|
|
|
|
|
QMutexLocker l(&m_peerMutex);
|
|
|
|
|
NetPeer *newPeer = new NetPeer(peerId, QString::fromStdString(userAgent), startHeight, address);
|
|
|
|
|
// assume this interface method is called in a thread that is not the Qt main one.
|
|
|
|
|
newPeer->moveToThread(thread());
|
|
|
|
|
newPeer->setParent(this);
|
|
|
|
|
m_peers.append(newPeer);
|
|
|
|
|
emit peerListChanged();
|
2021-07-30 10:52:09 +02:00
|
|
|
|
|
|
|
|
if (m_refreshTimer)
|
|
|
|
|
QTimer::singleShot(0, m_refreshTimer, SLOT(start()));
|
2020-05-24 13:20:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void NetDataProvider::lostPeer(int peerId)
|
|
|
|
|
{
|
2021-07-30 10:52:09 +02:00
|
|
|
// this callback is not guarenteed to be made in our thread.
|
2021-01-22 18:46:27 +01:00
|
|
|
emit peerDeleted(peerId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void NetDataProvider::deleteNetPeer(int peerId)
|
|
|
|
|
{
|
|
|
|
|
Q_ASSERT(QThread::currentThread() == thread()); // make sure we have no threading issues
|
2020-05-24 13:20:03 +02:00
|
|
|
QMutexLocker l(&m_peerMutex);
|
2021-02-03 16:36:01 +01:00
|
|
|
QList<NetPeer *> peers(m_peers);
|
|
|
|
|
for (int i = 0; i < peers.size(); ++i) {
|
|
|
|
|
if (peers.at(i)->connectionId() == peerId) {
|
|
|
|
|
auto oldPeer = peers.takeAt(i);
|
2021-02-04 18:54:05 +01:00
|
|
|
m_peers = peers;
|
2020-05-24 13:20:03 +02:00
|
|
|
emit peerListChanged();
|
2021-01-29 10:24:39 +01:00
|
|
|
oldPeer->deleteLater();
|
2020-05-24 13:20:03 +02:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-30 10:52:09 +02:00
|
|
|
void NetDataProvider::updatePeers()
|
|
|
|
|
{
|
|
|
|
|
auto &conMan = FloweePay::instance()->p2pNet()->connectionManager();
|
|
|
|
|
QMutexLocker l(&m_peerMutex);
|
|
|
|
|
QList<NetPeer *> peers(m_peers);
|
|
|
|
|
bool stopTimer = true;
|
|
|
|
|
for (auto &p : peers) {
|
|
|
|
|
auto peer = conMan.peer(p->connectionId());
|
|
|
|
|
// update 'p' with up to date data from the peer.
|
|
|
|
|
p->setRelaysTransactions(peer->relaysTransactions());
|
|
|
|
|
p->setHeadersReceived(peer->receivedHeaders());
|
|
|
|
|
|
|
|
|
|
if (peer->privacySegment() == nullptr)
|
|
|
|
|
stopTimer = false;
|
|
|
|
|
}
|
|
|
|
|
if (stopTimer)
|
|
|
|
|
m_refreshTimer->stop();
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-24 13:20:03 +02:00
|
|
|
void NetDataProvider::blockchainHeightChanged(int newHeight)
|
|
|
|
|
{
|
|
|
|
|
m_blockHeight.storeRelease(newHeight);
|
|
|
|
|
emit blockHeightChanged();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void NetDataProvider::punishMentChanged(int peerId)
|
|
|
|
|
{
|
|
|
|
|
QMutexLocker l(&m_peerMutex);
|
2021-02-03 16:36:01 +01:00
|
|
|
QList<NetPeer *> peers(m_peers);
|
|
|
|
|
for (auto &p : peers) {
|
2020-05-24 13:20:03 +02:00
|
|
|
if (p->connectionId() == peerId) {
|
|
|
|
|
p->notifyPunishmentChanged();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-29 20:57:41 +01:00
|
|
|
QList<QObject *> NetDataProvider::peers() const
|
2020-05-24 13:20:03 +02:00
|
|
|
{
|
2021-11-29 20:57:41 +01:00
|
|
|
QList<QObject *> answer;
|
|
|
|
|
for (auto *p : m_peers) {
|
|
|
|
|
answer.append(p);
|
|
|
|
|
}
|
|
|
|
|
return answer;
|
2020-05-24 13:20:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int NetDataProvider::blockheight() const
|
|
|
|
|
{
|
|
|
|
|
return m_blockHeight.loadAcquire();
|
|
|
|
|
}
|
2021-07-30 10:52:09 +02:00
|
|
|
|
|
|
|
|
void NetDataProvider::startRefreshTimer()
|
|
|
|
|
{
|
|
|
|
|
/*
|
|
|
|
|
* Start a timer that checks every second if the peer has changed one of the not-broadcast status.
|
|
|
|
|
* When a segment is assigned we can stop the timer.
|
|
|
|
|
*/
|
|
|
|
|
QMutexLocker l(&m_peerMutex);
|
|
|
|
|
assert(m_refreshTimer == nullptr); // can be called only once
|
|
|
|
|
m_refreshTimer = new QTimer(this);
|
|
|
|
|
connect(m_refreshTimer, SIGNAL(timeout()), this, SLOT(updatePeers()));
|
|
|
|
|
m_refreshTimer->setTimerType(Qt::VeryCoarseTimer);
|
|
|
|
|
m_refreshTimer->setInterval(950);
|
|
|
|
|
}
|